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