2 ** $Id: lobject.h,v 2.117.1.1 2017/04/19 17:39:34 roberto Exp $
3 ** Type definitions for Lua objects
4 ** See Copyright Notice in lua.h
20 ** Extra tags for non-values
22 #define LUA_TPROTO LUA_NUMTAGS /* function prototypes */
23 #define LUA_TDEADKEY (LUA_NUMTAGS+1) /* removed keys in tables */
26 ** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
28 #define LUA_TOTALTAGS (LUA_TPROTO + 2)
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
40 ** LUA_TFUNCTION variants:
42 ** 1 - light C function
43 ** 2 - regular C function (closure)
46 /* Variant tags for functions */
47 #define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */
48 #define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */
49 #define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */
52 /* Variant tags for strings */
53 #define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */
54 #define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */
57 /* Variant tags for numbers */
58 #define LUA_TNUMFLT (LUA_TNUMBER | (0 << 4)) /* float numbers */
59 #define LUA_TNUMINT (LUA_TNUMBER | (1 << 4)) /* integer numbers */
62 /* Bit mark for collectable types */
63 #define BIT_ISCOLLECTABLE (1 << 6)
65 /* mark a tag as collectable */
66 #define ctb(t) ((t) | BIT_ISCOLLECTABLE)
70 ** Common type for all collectable objects
72 typedef struct GCObject GCObject
;
76 ** Common Header for all collectable objects (in macro form, to be
77 ** included in other objects)
79 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
83 ** Common type has only the common header
93 ** Tagged Values. This is the basic representation of values in Lua,
94 ** an actual value plus a tag with its type.
98 ** Union of all Lua values
100 typedef union Value
{
101 GCObject
*gc
; /* collectable objects */
102 void *p
; /* light userdata */
103 int b
; /* booleans */
104 lua_CFunction f
; /* light C functions */
105 lua_Integer i
; /* integer numbers */
106 lua_Number n
; /* float numbers */
110 #define TValuefields Value value_; int tt_
113 typedef struct lua_TValue
{
119 /* macro defining a nil value */
120 #define NILCONSTANT {NULL}, LUA_TNIL
123 #define val_(o) ((o)->value_)
126 /* raw type tag of a TValue */
127 #define rttype(o) ((o)->tt_)
129 /* tag with no variants (bits 0-3) */
130 #define novariant(x) ((x) & 0x0F)
132 /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
133 #define ttype(o) (rttype(o) & 0x3F)
135 /* type tag of a TValue with no variants (bits 0-3) */
136 #define ttnov(o) (novariant(rttype(o)))
139 /* Macros to test type */
140 #define checktag(o,t) (rttype(o) == (t))
141 #define checktype(o,t) (ttnov(o) == (t))
142 #define ttisnumber(o) checktype((o), LUA_TNUMBER)
143 #define ttisfloat(o) checktag((o), LUA_TNUMFLT)
144 #define ttisinteger(o) checktag((o), LUA_TNUMINT)
145 #define ttisnil(o) checktag((o), LUA_TNIL)
146 #define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
147 #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
148 #define ttisstring(o) checktype((o), LUA_TSTRING)
149 #define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR))
150 #define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR))
151 #define ttistable(o) checktag((o), ctb(LUA_TTABLE))
152 #define ttisfunction(o) checktype(o, LUA_TFUNCTION)
153 #define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION)
154 #define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
155 #define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
156 #define ttislcf(o) checktag((o), LUA_TLCF)
157 #define ttisfulluserdata(o) checktag((o), ctb(LUA_TUSERDATA))
158 #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
159 #define ttisdeadkey(o) checktag((o), LUA_TDEADKEY)
162 /* Macros to access values */
163 #define ivalue(o) check_exp(ttisinteger(o), val_(o).i)
164 #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
165 #define nvalue(o) check_exp(ttisnumber(o), \
166 (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
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(L,obj) \
191 lua_longassert(!iscollectable(obj) || \
192 (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))
195 /* Macros to set values */
196 #define settt_(o,t) ((o)->tt_=(t))
198 #define setfltvalue(obj,x) \
199 { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
201 #define chgfltvalue(obj,x) \
202 { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
204 #define setivalue(obj,x) \
205 { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); }
207 #define chgivalue(obj,x) \
208 { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
210 #define setnilvalue(obj) settt_(obj, LUA_TNIL)
212 #define setfvalue(obj,x) \
213 { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
215 #define setpvalue(obj,x) \
216 { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
218 #define setbvalue(obj,x) \
219 { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
221 #define setgcovalue(L,obj,x) \
222 { TValue *io = (obj); GCObject *i_g=(x); \
223 val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); }
225 #define setsvalue(L,obj,x) \
226 { TValue *io = (obj); TString *x_ = (x); \
227 val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
228 checkliveness(L,io); }
230 #define setuvalue(L,obj,x) \
231 { TValue *io = (obj); Udata *x_ = (x); \
232 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \
233 checkliveness(L,io); }
235 #define setthvalue(L,obj,x) \
236 { TValue *io = (obj); lua_State *x_ = (x); \
237 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \
238 checkliveness(L,io); }
240 #define setclLvalue(L,obj,x) \
241 { TValue *io = (obj); LClosure *x_ = (x); \
242 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \
243 checkliveness(L,io); }
245 #define setclCvalue(L,obj,x) \
246 { TValue *io = (obj); CClosure *x_ = (x); \
247 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \
248 checkliveness(L,io); }
250 #define sethvalue(L,obj,x) \
251 { TValue *io = (obj); Table *x_ = (x); \
252 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \
253 checkliveness(L,io); }
255 #define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
259 #define setobj(L,obj1,obj2) \
260 { TValue *io1=(obj1); *io1 = *(obj2); \
261 (void)L; checkliveness(L,io1); }
265 ** different types of assignments, according to destination
268 /* from stack to (same) stack */
269 #define setobjs2s setobj
270 /* to stack (not from same stack) */
271 #define setobj2s setobj
272 #define setsvalue2s setsvalue
273 #define sethvalue2s sethvalue
274 #define setptvalue2s setptvalue
275 /* from table to same table */
276 #define setobjt2t setobj
278 #define setobj2n setobj
279 #define setsvalue2n setsvalue
281 /* to table (define it as an expression to be used in macros) */
282 #define setobj2t(L,o1,o2) ((void)L, *(o1)=*(o2), checkliveness(L,(o1)))
288 ** {======================================================
289 ** types and prototypes
290 ** =======================================================
294 typedef TValue
*StkId
; /* index to stack elements */
300 ** Header for string value; string bytes follow the end of this structure
301 ** (aligned according to 'UTString'; see next).
303 typedef struct TString
{
305 #if defined(__LUATOS_SMALL_RAM__) && defined(__LUATOS_SCRIPT_BASE__)
310 uint16_t static_flag
:5;
316 uint32_t lnglen
:17;/* length for long strings */
317 uint32_t static_offset
:15;
319 struct TString
*hnext
; /* linked list for hash table */
323 lu_byte extra
; /* reserved words for short strings; "has hash" for longs */
324 lu_byte shrlen
; /* length for short strings */
327 size_t lnglen
; /* length for long strings */
328 struct TString
*hnext
; /* linked list for hash table */
335 ** Ensures that address after this type is always fully aligned.
337 typedef union UTString
{
338 L_Umaxalign dummy
; /* ensures maximum alignment for strings */
344 ** Get the actual string (array of bytes) from a 'TString'.
345 ** (Access to 'extra' ensures that value is really a 'TString'.)
347 #if defined(__LUATOS_SMALL_RAM__) && defined(__LUATOS_SCRIPT_BASE__)
348 char *getstr(TString
*ts
);
351 check_exp(sizeof((ts)->extra), cast(char *, (ts)) + sizeof(UTString))
354 /* get the actual string (array of bytes) from a Lua value */
355 #define svalue(o) getstr(tsvalue(o))
357 /* get string length from 'TString *s' */
358 #define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen)
360 /* get string length from 'TValue *o' */
361 #define vslen(o) tsslen(tsvalue(o))
365 ** Header for userdata; memory area follows the end of this structure
366 ** (aligned according to 'UUdata'; see next).
368 typedef struct Udata
{
370 lu_byte ttuv_
; /* user value's tag */
371 struct Table
*metatable
;
372 size_t len
; /* number of bytes */
373 union Value user_
; /* user value */
378 ** Ensures that address after this type is always fully aligned.
380 typedef union UUdata
{
381 L_Umaxalign dummy
; /* ensures maximum alignment for 'local' udata */
387 ** Get the address of memory block inside 'Udata'.
388 ** (Access to 'ttuv_' ensures that value is really a 'Udata'.)
390 #define getudatamem(u) \
391 check_exp(sizeof((u)->ttuv_), (cast(char*, (u)) + sizeof(UUdata)))
393 #define setuservalue(L,u,o) \
394 { const TValue *io=(o); Udata *iu = (u); \
395 iu->user_ = io->value_; iu->ttuv_ = rttype(io); \
396 checkliveness(L,io); }
399 #define getuservalue(L,u,o) \
400 { TValue *io=(o); const Udata *iu = (u); \
401 io->value_ = iu->user_; settt_(io, iu->ttuv_); \
402 checkliveness(L,io); }
406 ** Description of an upvalue for function prototypes
408 typedef struct Upvaldesc
{
409 TString
*name
; /* upvalue name (for debug information) */
410 lu_byte instack
; /* whether it is in stack (register) */
411 lu_byte idx
; /* index of upvalue (in stack or in outer function's list) */
416 ** Description of a local variable for function prototypes
417 ** (used for debug information)
419 typedef struct LocVar
{
421 #ifdef __LUATOS_SMALL_RAM__
425 int startpc
; /* first point where variable is active */
426 int endpc
; /* first point where variable is dead */
432 ** Function Prototypes
434 typedef struct Proto
{
436 lu_byte numparams
; /* number of fixed parameters */
438 lu_byte maxstacksize
; /* number of registers needed by this function */
439 #ifdef __LUATOS_SMALL_RAM__
440 uint16_t sizeupvalues
; /* size of 'upvalues' */
441 uint16_t sizek
; /* size of 'k' */
443 uint16_t sizelineinfo
;
444 uint16_t sizep
; /* size of 'p' */
445 uint16_t sizelocvars
;
446 uint16_t linedefined
;
447 uint16_t lastlinedefined
;
449 int sizeupvalues
; /* size of 'upvalues' */
450 int sizek
; /* size of 'k' */
453 int sizep
; /* size of 'p' */
455 int linedefined
; /* debug information */
456 int lastlinedefined
; /* debug information */
458 TValue
*k
; /* constants used by the function */
459 Instruction
*code
; /* opcodes */
460 struct Proto
**p
; /* functions defined inside the function */
461 int *lineinfo
; /* map from opcodes to source lines (debug information) */
462 LocVar
*locvars
; /* information about local variables (debug information) */
463 Upvaldesc
*upvalues
; /* upvalue information */
464 struct LClosure
*cache
; /* last-created closure with this prototype */
465 TString
*source
; /* used for debug information */
474 typedef struct UpVal UpVal
;
481 #define ClosureHeader \
482 CommonHeader; lu_byte nupvalues; GCObject *gclist
484 typedef struct CClosure
{
487 TValue upvalue
[1]; /* list of upvalues */
491 typedef struct LClosure
{
494 UpVal
*upvals
[1]; /* list of upvalues */
498 typedef union Closure
{
504 #define isLfunction(o) ttisLclosure(o)
506 #define getproto(o) (clLvalue(o)->p)
516 int next
; /* for chaining (offset for next node) */
522 /* copy a value into a key without messing up field 'next' */
523 #define setnodekey(L,key,obj) \
524 { TKey *k_=(key); const TValue *io_=(obj); \
525 k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \
526 (void)L; checkliveness(L,io_); }
529 typedef struct Node
{
535 typedef struct Table
{
537 lu_byte flags
; /* 1<<p means tagmethod(p) is not present */
538 lu_byte lsizenode
; /* log2 of size of 'node' array */
539 unsigned int sizearray
; /* size of 'array' array */
540 TValue
*array
; /* array part */
542 Node
*lastfree
; /* any free position is before this position */
543 struct Table
*metatable
;
550 ** 'module' operation for hashing (size is always a power of 2)
552 #define lmod(s,size) \
553 (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
556 #define twoto(x) (1<<(x))
557 #define sizenode(t) (twoto((t)->lsizenode))
561 ** (address of) a fixed nil value
563 #define luaO_nilobject (&luaO_nilobject_)
566 LUAI_DDEC
const TValue luaO_nilobject_
;
568 /* size of buffer for 'luaO_utf8esc' function */
571 LUAI_FUNC
int luaO_int2fb (unsigned int x
);
572 LUAI_FUNC
int luaO_fb2int (int x
);
573 LUAI_FUNC
int luaO_utf8esc (char *buff
, unsigned long x
);
574 LUAI_FUNC
int luaO_ceillog2 (unsigned int x
);
575 LUAI_FUNC
void luaO_arith (lua_State
*L
, int op
, const TValue
*p1
,
576 const TValue
*p2
, TValue
*res
);
577 LUAI_FUNC
size_t luaO_str2num (const char *s
, TValue
*o
);
578 LUAI_FUNC
int luaO_hexavalue (int c
);
579 LUAI_FUNC
void luaO_tostring (lua_State
*L
, StkId obj
);
580 LUAI_FUNC
const char *luaO_pushvfstring (lua_State
*L
, const char *fmt
,
582 LUAI_FUNC
const char *luaO_pushfstring (lua_State
*L
, const char *fmt
, ...);
583 LUAI_FUNC
void luaO_chunkid (char *out
, const char *source
, size_t len
);