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