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