etc/protocols - sync with NetBSD-8
[minix.git] / external / mit / lua / dist / src / lobject.h
blobb5676d3f52973c5f2f3ffd4011e235aa83b18fa8
1 /* $NetBSD: lobject.h,v 1.4 2015/10/08 13:21:00 mbalmer Exp $ */
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 */
10 #ifndef lobject_h
11 #define lobject_h
14 #include <stdarg.h>
17 #include "llimits.h"
18 #include "lua.h"
22 ** Extra tags for non-values
24 #define LUA_TPROTO LUA_NUMTAGS
25 #define LUA_TDEADKEY (LUA_NUMTAGS+1)
28 ** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
30 #define LUA_TOTALTAGS (LUA_TPROTO + 2)
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
42 ** LUA_TFUNCTION variants:
43 ** 0 - Lua function
44 ** 1 - light C function
45 ** 2 - regular C function (closure)
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 */
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 */
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 */
66 /* Bit mark for collectable types */
67 #define BIT_ISCOLLECTABLE (1 << 6)
69 /* mark a tag as collectable */
70 #define ctb(t) ((t) | BIT_ISCOLLECTABLE)
74 ** Common type for all collectable objects
76 typedef struct GCObject GCObject;
80 ** Common Header for all collectable objects (in macro form, to be
81 ** included in other objects)
83 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
87 ** Common type has only the common header
89 struct GCObject {
90 CommonHeader;
96 ** Union of all Lua values
98 typedef union Value Value;
104 ** Tagged Values. This is the basic representation of values in Lua,
105 ** an actual value plus a tag with its type.
108 #define TValuefields Value value_; int tt_
110 typedef struct lua_TValue TValue;
113 /* macro defining a nil value */
114 #define NILCONSTANT {NULL}, LUA_TNIL
117 #define val_(o) ((o)->value_)
120 /* raw type tag of a TValue */
121 #define rttype(o) ((o)->tt_)
123 /* tag with no variants (bits 0-3) */
124 #define novariant(x) ((x) & 0x0F)
126 /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
127 #define ttype(o) (rttype(o) & 0x3F)
129 /* type tag of a TValue with no variants (bits 0-3) */
130 #define ttnov(o) (novariant(rttype(o)))
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)
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))
181 #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
184 #define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE)
187 /* Macros for internal tests */
188 #define righttt(obj) (ttype(obj) == gcvalue(obj)->tt)
190 #define checkliveness(g,obj) \
191 lua_longassert(!iscollectable(obj) || \
192 (righttt(obj) && !isdead(g,gcvalue(obj))))
195 /* Macros to set values */
196 #define settt_(o,t) ((o)->tt_=(t))
198 #ifndef _KERNEL
199 #define setfltvalue(obj,x) \
200 { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
201 #endif
203 #define chgfltvalue(obj,x) \
204 { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
206 #define setivalue(obj,x) \
207 { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); }
209 #define chgivalue(obj,x) \
210 { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
212 #define setnilvalue(obj) settt_(obj, LUA_TNIL)
214 #define setfvalue(obj,x) \
215 { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
217 #define setpvalue(obj,x) \
218 { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
220 #define setbvalue(obj,x) \
221 { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
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)); }
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); }
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); }
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); }
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); }
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); }
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); }
257 #define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
261 #define setobj(L,obj1,obj2) \
262 { TValue *io1=(obj1); *io1 = *(obj2); \
263 (void)L; checkliveness(G(L),io1); }
267 ** different types of assignments, according to destination
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
289 ** {======================================================
290 ** types and prototypes
291 ** =======================================================
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
307 struct lua_TValue {
308 TValuefields;
312 typedef TValue *StkId; /* index to stack elements */
318 ** Header for string value; string bytes follow the end of this structure
319 ** (aligned according to 'UTString'; see next).
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;
334 ** Ensures that address after this type is always fully aligned.
336 typedef union UTString {
337 L_Umaxalign dummy; /* ensures maximum alignment for strings */
338 TString tsv;
339 } UTString;
343 ** Get the actual string (array of bytes) from a 'TString'.
344 ** (Access to 'extra' ensures that value is really a 'TString'.)
346 #define getaddrstr(ts) (cast(char *, (ts)) + sizeof(UTString))
347 #define getstr(ts) \
348 check_exp(sizeof((ts)->extra), cast(const char*, getaddrstr(ts)))
350 /* get the actual string (array of bytes) from a Lua value */
351 #define svalue(o) getstr(tsvalue(o))
353 /* get string length from 'TString *s' */
354 #define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen)
356 /* get string length from 'TValue *o' */
357 #define vslen(o) tsslen(tsvalue(o))
361 ** Header for userdata; memory area follows the end of this structure
362 ** (aligned according to 'UUdata'; see next).
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;
374 ** Ensures that address after this type is always fully aligned.
376 typedef union UUdata {
377 L_Umaxalign dummy; /* ensures maximum alignment for 'local' udata */
378 Udata uv;
379 } UUdata;
383 ** Get the address of memory block inside 'Udata'.
384 ** (Access to 'ttuv_' ensures that value is really a 'Udata'.)
386 #define getudatamem(u) \
387 check_exp(sizeof((u)->ttuv_), (cast(char*, (u)) + sizeof(UUdata)))
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); }
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); }
402 ** Description of an upvalue for function prototypes
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;
412 ** Description of a local variable for function prototypes
413 ** (used for debug information)
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;
423 ** Function Prototypes
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;
452 ** Lua Upvalues
454 typedef struct UpVal UpVal;
458 ** Closures
461 #define ClosureHeader \
462 CommonHeader; lu_byte nupvalues; GCObject *gclist
464 typedef struct CClosure {
465 ClosureHeader;
466 lua_CFunction f;
467 TValue upvalue[1]; /* list of upvalues */
468 } CClosure;
471 typedef struct LClosure {
472 ClosureHeader;
473 struct Proto *p;
474 UpVal *upvals[1]; /* list of upvalues */
475 } LClosure;
478 typedef union Closure {
479 CClosure c;
480 LClosure l;
481 } Closure;
484 #define isLfunction(o) ttisLclosure(o)
486 #define getproto(o) (clLvalue(o)->p)
490 ** Tables
493 typedef union TKey {
494 struct {
495 TValuefields;
496 int next; /* for chaining (offset for next node) */
497 } nk;
498 TValue tvk;
499 } TKey;
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_); }
509 typedef struct Node {
510 TValue i_val;
511 TKey i_key;
512 } Node;
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;
530 ** 'module' operation for hashing (size is always a power of 2)
532 #define lmod(s,size) \
533 (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
536 #define twoto(x) (1<<(x))
537 #define sizenode(t) (twoto((t)->lsizenode))
541 ** (address of) a fixed nil value
543 #define luaO_nilobject (&luaO_nilobject_)
546 LUAI_DDEC const TValue luaO_nilobject_;
548 /* size of buffer for 'luaO_utf8esc' function */
549 #define UTF8BUFFSZ 8
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);
566 #endif