1 /*
2 ** $Id: lobject.h,v 2.106 2015/01/05 13:52:37 roberto Exp $
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 tags for non-values
21 */
22 #define LUA_TPROTO	LUA_NUMTAGS
23 #define LUA_TDEADKEY	(LUA_NUMTAGS+1)
24 
25 /*
26 ** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
27 */
28 #define LUA_TOTALTAGS	(LUA_TPROTO + 2)
29 
30 
31 /*
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
36 */
37 
38 #define VARBITS		(3 << 4)
39 
40 
41 /*
42 ** LUA_TFUNCTION variants:
43 ** 0 - Lua function
44 ** 1 - light C function
45 ** 2 - regular C function (closure)
46 */
47 
48 /* Variant tags for functions */
49 #define LUA_TLCL	(LUA_TFUNCTION | (0 << 4))  /* Lua closure */
50 #define LUA_TLCF	(LUA_TFUNCTION | (1 << 4))  /* light C function */
51 #define LUA_TCCL	(LUA_TFUNCTION | (2 << 4))  /* C closure */
52 
53 
54 /* Variant tags for strings */
55 #define LUA_TSHRSTR	(LUA_TSTRING | (0 << 4))  /* short strings */
56 #define LUA_TLNGSTR	(LUA_TSTRING | (1 << 4))  /* long strings */
57 
58 
59 /* Variant tags for numbers */
60 #define LUA_TNUMFLT	(LUA_TNUMBER | (0 << 4))  /* float numbers */
61 #define LUA_TNUMINT	(LUA_TNUMBER | (1 << 4))  /* integer numbers */
62 
63 
64 /* Bit mark for collectable types */
65 #define BIT_ISCOLLECTABLE	(1 << 6)
66 
67 /* mark a tag as collectable */
68 #define ctb(t)			((t) | BIT_ISCOLLECTABLE)
69 
70 
71 /*
72 ** Common type for all collectable objects
73 */
74 typedef struct GCObject GCObject;
75 
76 
77 /*
78 ** Common Header for all collectable objects (in macro form, to be
79 ** included in other objects)
80 */
81 #define CommonHeader	GCObject *next; lu_byte tt; lu_byte marked
82 
83 
84 /*
85 ** Common type has only the common header
86 */
87 struct GCObject {
88   CommonHeader;
89 };
90 
91 
92 
93 /*
94 ** Union of all Lua values
95 */
96 typedef union Value Value;
97 
98 
99 
100 
101 /*
102 ** Tagged Values. This is the basic representation of values in Lua,
103 ** an actual value plus a tag with its type.
104 */
105 
106 #define TValuefields	Value value_; int tt_
107 
108 typedef struct lua_TValue TValue;
109 
110 
111 /* macro defining a nil value */
112 #define NILCONSTANT	{NULL}, LUA_TNIL
113 
114 
115 #define val_(o)		((o)->value_)
116 
117 
118 /* raw type tag of a TValue */
119 #define rttype(o)	((o)->tt_)
120 
121 /* tag with no variants (bits 0-3) */
122 #define novariant(x)	((x) & 0x0F)
123 
124 /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
125 #define ttype(o)	(rttype(o) & 0x3F)
126 
127 /* type tag of a TValue with no variants (bits 0-3) */
128 #define ttnov(o)	(novariant(rttype(o)))
129 
130 
131 /* Macros to test type */
132 #define checktag(o,t)		(rttype(o) == (t))
133 #define checktype(o,t)		(ttnov(o) == (t))
134 #define ttisnumber(o)		checktype((o), LUA_TNUMBER)
135 #define ttisfloat(o)		checktag((o), LUA_TNUMFLT)
136 #define ttisinteger(o)		checktag((o), LUA_TNUMINT)
137 #define ttisnil(o)		checktag((o), LUA_TNIL)
138 #define ttisboolean(o)		checktag((o), LUA_TBOOLEAN)
139 #define ttislightuserdata(o)	checktag((o), LUA_TLIGHTUSERDATA)
140 #define ttisstring(o)		checktype((o), LUA_TSTRING)
141 #define ttisshrstring(o)	checktag((o), ctb(LUA_TSHRSTR))
142 #define ttislngstring(o)	checktag((o), ctb(LUA_TLNGSTR))
143 #define ttistable(o)		checktag((o), ctb(LUA_TTABLE))
144 #define ttisfunction(o)		checktype(o, LUA_TFUNCTION)
145 #define ttisclosure(o)		((rttype(o) & 0x1F) == LUA_TFUNCTION)
146 #define ttisCclosure(o)		checktag((o), ctb(LUA_TCCL))
147 #define ttisLclosure(o)		checktag((o), ctb(LUA_TLCL))
148 #define ttislcf(o)		checktag((o), LUA_TLCF)
149 #define ttisfulluserdata(o)	checktag((o), ctb(LUA_TUSERDATA))
150 #define ttisthread(o)		checktag((o), ctb(LUA_TTHREAD))
151 #define ttisdeadkey(o)		checktag((o), LUA_TDEADKEY)
152 
153 
154 /* Macros to access values */
155 #define ivalue(o)	check_exp(ttisinteger(o), val_(o).i)
156 #define fltvalue(o)	check_exp(ttisfloat(o), val_(o).n)
157 #define nvalue(o)	check_exp(ttisnumber(o), \
158 	(ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
159 #define gcvalue(o)	check_exp(iscollectable(o), val_(o).gc)
160 #define pvalue(o)	check_exp(ttislightuserdata(o), val_(o).p)
161 #define tsvalue(o)	check_exp(ttisstring(o), gco2ts(val_(o).gc))
162 #define uvalue(o)	check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
163 #define clvalue(o)	check_exp(ttisclosure(o), gco2cl(val_(o).gc))
164 #define clLvalue(o)	check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
165 #define clCvalue(o)	check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
166 #define fvalue(o)	check_exp(ttislcf(o), val_(o).f)
167 #define hvalue(o)	check_exp(ttistable(o), gco2t(val_(o).gc))
168 #define bvalue(o)	check_exp(ttisboolean(o), val_(o).b)
169 #define thvalue(o)	check_exp(ttisthread(o), gco2th(val_(o).gc))
170 /* a dead value may get the 'gc' field, but cannot access its contents */
171 #define deadvalue(o)	check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
172 
173 #define l_isfalse(o)	(ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
174 
175 
176 #define iscollectable(o)	(rttype(o) & BIT_ISCOLLECTABLE)
177 
178 
179 /* Macros for internal tests */
180 #define righttt(obj)		(ttype(obj) == gcvalue(obj)->tt)
181 
182 #define checkliveness(g,obj) \
183 	lua_longassert(!iscollectable(obj) || \
184 			(righttt(obj) && !isdead(g,gcvalue(obj))))
185 
186 
187 /* Macros to set values */
188 #define settt_(o,t)	((o)->tt_=(t))
189 
190 #define setfltvalue(obj,x) \
191   { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
192 
193 #define setivalue(obj,x) \
194   { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); }
195 
196 #define setnilvalue(obj) settt_(obj, LUA_TNIL)
197 
198 #define setfvalue(obj,x) \
199   { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
200 
201 #define setpvalue(obj,x) \
202   { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
203 
204 #define setbvalue(obj,x) \
205   { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
206 
207 #define setgcovalue(L,obj,x) \
208   { TValue *io = (obj); GCObject *i_g=(x); \
209     val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); }
210 
211 #define setsvalue(L,obj,x) \
212   { TValue *io = (obj); TString *x_ = (x); \
213     val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
214     checkliveness(G(L),io); }
215 
216 #define setuvalue(L,obj,x) \
217   { TValue *io = (obj); Udata *x_ = (x); \
218     val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \
219     checkliveness(G(L),io); }
220 
221 #define setthvalue(L,obj,x) \
222   { TValue *io = (obj); lua_State *x_ = (x); \
223     val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \
224     checkliveness(G(L),io); }
225 
226 #define setclLvalue(L,obj,x) \
227   { TValue *io = (obj); LClosure *x_ = (x); \
228     val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \
229     checkliveness(G(L),io); }
230 
231 #define setclCvalue(L,obj,x) \
232   { TValue *io = (obj); CClosure *x_ = (x); \
233     val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \
234     checkliveness(G(L),io); }
235 
236 #define sethvalue(L,obj,x) \
237   { TValue *io = (obj); Table *x_ = (x); \
238     val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \
239     checkliveness(G(L),io); }
240 
241 #define setdeadvalue(obj)	settt_(obj, LUA_TDEADKEY)
242 
243 
244 
245 #define setobj(L,obj1,obj2) \
246 	{ TValue *io1=(obj1); *io1 = *(obj2); \
247 	  (void)L; checkliveness(G(L),io1); }
248 
249 
250 /*
251 ** different types of assignments, according to destination
252 */
253 
254 /* from stack to (same) stack */
255 #define setobjs2s	setobj
256 /* to stack (not from same stack) */
257 #define setobj2s	setobj
258 #define setsvalue2s	setsvalue
259 #define sethvalue2s	sethvalue
260 #define setptvalue2s	setptvalue
261 /* from table to same table */
262 #define setobjt2t	setobj
263 /* to table */
264 #define setobj2t	setobj
265 /* to new object */
266 #define setobj2n	setobj
267 #define setsvalue2n	setsvalue
268 
269 
270 
271 
272 /*
273 ** {======================================================
274 ** types and prototypes
275 ** =======================================================
276 */
277 
278 
279 union Value {
280   GCObject *gc;    /* collectable objects */
281   void *p;         /* light userdata */
282   int b;           /* booleans */
283   lua_CFunction f; /* light C functions */
284   lua_Integer i;   /* integer numbers */
285   lua_Number n;    /* float numbers */
286 };
287 
288 
289 struct lua_TValue {
290   TValuefields;
291 };
292 
293 
294 typedef TValue *StkId;  /* index to stack elements */
295 
296 
297 
298 
299 /*
300 ** Header for string value; string bytes follow the end of this structure
301 ** (aligned according to 'UTString'; see next).
302 */
303 typedef struct TString {
304   CommonHeader;
305   lu_byte extra;  /* reserved words for short strings; "has hash" for longs */
306   unsigned int hash;
307   size_t len;  /* number of characters in string */
308   struct TString *hnext;  /* linked list for hash table */
309 } TString;
310 
311 
312 /*
313 ** Ensures that address after this type is always fully aligned.
314 */
315 typedef union UTString {
316   L_Umaxalign dummy;  /* ensures maximum alignment for strings */
317   TString tsv;
318 } UTString;
319 
320 
321 /*
322 ** Get the actual string (array of bytes) from a 'TString'.
323 ** (Access to 'extra' ensures that value is really a 'TString'.)
324 */
325 #define getaddrstr(ts)	(cast(char *, (ts)) + sizeof(UTString))
326 #define getstr(ts)  \
327   check_exp(sizeof((ts)->extra), cast(const char*, getaddrstr(ts)))
328 
329 /* get the actual string (array of bytes) from a Lua value */
330 #define svalue(o)       getstr(tsvalue(o))
331 
332 
333 /*
334 ** Header for userdata; memory area follows the end of this structure
335 ** (aligned according to 'UUdata'; see next).
336 */
337 typedef struct Udata {
338   CommonHeader;
339   lu_byte ttuv_;  /* user value's tag */
340   struct Table *metatable;
341   size_t len;  /* number of bytes */
342   union Value user_;  /* user value */
343 } Udata;
344 
345 
346 /*
347 ** Ensures that address after this type is always fully aligned.
348 */
349 typedef union UUdata {
350   L_Umaxalign dummy;  /* ensures maximum alignment for 'local' udata */
351   Udata uv;
352 } UUdata;
353 
354 
355 /*
356 **  Get the address of memory block inside 'Udata'.
357 ** (Access to 'ttuv_' ensures that value is really a 'Udata'.)
358 */
359 #define getudatamem(u)  \
360   check_exp(sizeof((u)->ttuv_), (cast(char*, (u)) + sizeof(UUdata)))
361 
362 #define setuservalue(L,u,o) \
363 	{ const TValue *io=(o); Udata *iu = (u); \
364 	  iu->user_ = io->value_; iu->ttuv_ = io->tt_; \
365 	  checkliveness(G(L),io); }
366 
367 
368 #define getuservalue(L,u,o) \
369 	{ TValue *io=(o); const Udata *iu = (u); \
370 	  io->value_ = iu->user_; io->tt_ = iu->ttuv_; \
371 	  checkliveness(G(L),io); }
372 
373 
374 /*
375 ** Description of an upvalue for function prototypes
376 */
377 typedef struct Upvaldesc {
378   TString *name;  /* upvalue name (for debug information) */
379   lu_byte instack;  /* whether it is in stack */
380   lu_byte idx;  /* index of upvalue (in stack or in outer function's list) */
381 } Upvaldesc;
382 
383 
384 /*
385 ** Description of a local variable for function prototypes
386 ** (used for debug information)
387 */
388 typedef struct LocVar {
389   TString *varname;
390   int startpc;  /* first point where variable is active */
391   int endpc;    /* first point where variable is dead */
392 } LocVar;
393 
394 
395 /*
396 ** Function Prototypes
397 */
398 typedef struct Proto {
399   CommonHeader;
400   lu_byte numparams;  /* number of fixed parameters */
401   lu_byte is_vararg;
402   lu_byte maxstacksize;  /* maximum stack used by this function */
403   int sizeupvalues;  /* size of 'upvalues' */
404   int sizek;  /* size of 'k' */
405   int sizecode;
406   int sizelineinfo;
407   int sizep;  /* size of 'p' */
408   int sizelocvars;
409   int linedefined;
410   int lastlinedefined;
411   TValue *k;  /* constants used by the function */
412   Instruction *code;
413   struct Proto **p;  /* functions defined inside the function */
414   int *lineinfo;  /* map from opcodes to source lines (debug information) */
415   LocVar *locvars;  /* information about local variables (debug information) */
416   Upvaldesc *upvalues;  /* upvalue information */
417   struct LClosure *cache;  /* last created closure with this prototype */
418   TString  *source;  /* used for debug information */
419   GCObject *gclist;
420 } Proto;
421 
422 
423 
424 /*
425 ** Lua Upvalues
426 */
427 typedef struct UpVal UpVal;
428 
429 
430 /*
431 ** Closures
432 */
433 
434 #define ClosureHeader \
435 	CommonHeader; lu_byte nupvalues; GCObject *gclist
436 
437 typedef struct CClosure {
438   ClosureHeader;
439   lua_CFunction f;
440   TValue upvalue[1];  /* list of upvalues */
441 } CClosure;
442 
443 
444 typedef struct LClosure {
445   ClosureHeader;
446   struct Proto *p;
447   UpVal *upvals[1];  /* list of upvalues */
448 } LClosure;
449 
450 
451 typedef union Closure {
452   CClosure c;
453   LClosure l;
454 } Closure;
455 
456 
457 #define isLfunction(o)	ttisLclosure(o)
458 
459 #define getproto(o)	(clLvalue(o)->p)
460 
461 
462 /*
463 ** Tables
464 */
465 
466 typedef union TKey {
467   struct {
468     TValuefields;
469     int next;  /* for chaining (offset for next node) */
470   } nk;
471   TValue tvk;
472 } TKey;
473 
474 
475 /* copy a value into a key without messing up field 'next' */
476 #define setnodekey(L,key,obj) \
477 	{ TKey *k_=(key); const TValue *io_=(obj); \
478 	  k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \
479 	  (void)L; checkliveness(G(L),io_); }
480 
481 
482 typedef struct Node {
483   TValue i_val;
484   TKey i_key;
485 } Node;
486 
487 
488 typedef struct Table {
489   CommonHeader;
490   lu_byte flags;  /* 1<<p means tagmethod(p) is not present */
491   lu_byte lsizenode;  /* log2 of size of 'node' array */
492   unsigned int sizearray;  /* size of 'array' array */
493   TValue *array;  /* array part */
494   Node *node;
495   Node *lastfree;  /* any free position is before this position */
496   struct Table *metatable;
497   GCObject *gclist;
498 } Table;
499 
500 
501 
502 /*
503 ** 'module' operation for hashing (size is always a power of 2)
504 */
505 #define lmod(s,size) \
506 	(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
507 
508 
509 #define twoto(x)	(1<<(x))
510 #define sizenode(t)	(twoto((t)->lsizenode))
511 
512 
513 /*
514 ** (address of) a fixed nil value
515 */
516 #define luaO_nilobject		(&luaO_nilobject_)
517 
518 
519 LUAI_DDEC const TValue luaO_nilobject_;
520 
521 /* size of buffer for 'luaO_utf8esc' function */
522 #define UTF8BUFFSZ	8
523 
524 LUAI_FUNC int luaO_int2fb (unsigned int x);
525 LUAI_FUNC int luaO_fb2int (int x);
526 LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
527 LUAI_FUNC int luaO_ceillog2 (unsigned int x);
528 LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
529                            const TValue *p2, TValue *res);
530 LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
531 LUAI_FUNC int luaO_hexavalue (int c);
532 LUAI_FUNC void luaO_tostring (lua_State *L, StkId obj);
533 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
534                                                        va_list argp);
535 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
536 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
537 
538 
539 #endif
540 
541