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