lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
lua / lobject.h
| 1 | /* |
| 2 | ** $Id: lobject.h $ |
| 3 | ** Type definitions for Lua objects |
| 4 | ** See Copyright Notice in lua.h |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | #ifndef lobject_h |
| 9 | #define lobject_h |
| 10 | |
| 11 | |
| 12 | #include <stdarg.h> |
| 13 | |
| 14 | |
| 15 | #include "llimits.h" |
| 16 | #include "lua.h" |
| 17 | |
| 18 | |
| 19 | /* |
| 20 | ** Extra types for collectable non-values |
| 21 | */ |
| 22 | #define LUA_TUPVAL LUA_NUMTYPES /* upvalues */ |
| 23 | #define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */ |
| 24 | #define LUA_TDEADKEY (LUA_NUMTYPES+2) /* removed keys in tables */ |
| 25 | |
| 26 | |
| 27 | |
| 28 | /* |
| 29 | ** number of all possible types (including LUA_TNONE but excluding DEADKEY) |
| 30 | */ |
| 31 | #define LUA_TOTALTYPES (LUA_TPROTO + 2) |
| 32 | |
| 33 | |
| 34 | /* |
| 35 | ** tags for Tagged Values have the following use of bits: |
| 36 | ** bits 0-3: actual tag (a LUA_T* constant) |
| 37 | ** bits 4-5: variant bits |
| 38 | ** bit 6: whether value is collectable |
| 39 | */ |
| 40 | |
| 41 | /* add variant bits to a type */ |
| 42 | #define makevariant(t,v) ((t) | ((v) << 4)) |
| 43 | |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | ** Union of all Lua values |
| 48 | */ |
| 49 | typedef union Value { |
| 50 | struct GCObject *gc; /* collectable objects */ |
| 51 | void *p; /* light userdata */ |
| 52 | lua_CFunction f; /* light C functions */ |
| 53 | lua_Integer i; /* integer numbers */ |
| 54 | lua_Number n; /* float numbers */ |
| 55 | /* not used, but may avoid warnings for uninitialized value */ |
| 56 | lu_byte ub; |
| 57 | } Value; |
| 58 | |
| 59 | |
| 60 | /* |
| 61 | ** Tagged Values. This is the basic representation of values in Lua: |
| 62 | ** an actual value plus a tag with its type. |
| 63 | */ |
| 64 | |
| 65 | #define TValuefields Value value_; lu_byte tt_ |
| 66 | |
| 67 | typedef struct TValue { |
| 68 | TValuefields; |
| 69 | } TValue; |
| 70 | |
| 71 | |
| 72 | #define val_(o) ((o)->value_) |
| 73 | #define valraw(o) (val_(o)) |
| 74 | |
| 75 | |
| 76 | /* raw type tag of a TValue */ |
| 77 | #define rawtt(o) ((o)->tt_) |
| 78 | |
| 79 | /* tag with no variants (bits 0-3) */ |
| 80 | #define novariant(t) ((t) & 0x0F) |
| 81 | |
| 82 | /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ |
| 83 | #define withvariant(t) ((t) & 0x3F) |
| 84 | #define ttypetag(o) withvariant(rawtt(o)) |
| 85 | |
| 86 | /* type of a TValue */ |
| 87 | #define ttype(o) (novariant(rawtt(o))) |
| 88 | |
| 89 | |
| 90 | /* Macros to test type */ |
| 91 | #define checktag(o,t) (rawtt(o) == (t)) |
| 92 | #define checktype(o,t) (ttype(o) == (t)) |
| 93 | |
| 94 | |
| 95 | /* Macros for internal tests */ |
| 96 | |
| 97 | /* collectable object has the same tag as the original value */ |
| 98 | #define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt) |
| 99 | |
| 100 | /* |
| 101 | ** Any value being manipulated by the program either is non |
| 102 | ** collectable, or the collectable object has the right tag |
| 103 | ** and it is not dead. The option 'L == NULL' allows other |
| 104 | ** macros using this one to be used where L is not available. |
| 105 | */ |
| 106 | #define checkliveness(L,obj) \ |
| 107 | ((void)L, lua_longassert(!iscollectable(obj) || \ |
| 108 | (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))) |
| 109 | |
| 110 | |
| 111 | /* Macros to set values */ |
| 112 | |
| 113 | /* set a value's tag */ |
| 114 | #define settt_(o,t) ((o)->tt_=(t)) |
| 115 | |
| 116 | |
| 117 | /* main macro to copy values (from 'obj2' to 'obj1') */ |
| 118 | #define setobj(L,obj1,obj2) \ |
| 119 | { TValue *io1=(obj1); const TValue *io2=(obj2); \ |
| 120 | io1->value_ = io2->value_; settt_(io1, io2->tt_); \ |
| 121 | checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); } |
| 122 | |
| 123 | /* |
| 124 | ** Different types of assignments, according to source and destination. |
| 125 | ** (They are mostly equal now, but may be different in the future.) |
| 126 | */ |
| 127 | |
| 128 | /* from stack to stack */ |
| 129 | #define setobjs2s(L,o1,o2) setobj(L,s2v(o1),s2v(o2)) |
| 130 | /* to stack (not from same stack) */ |
| 131 | #define setobj2s(L,o1,o2) setobj(L,s2v(o1),o2) |
| 132 | /* from table to same table */ |
| 133 | #define setobjt2t setobj |
| 134 | /* to new object */ |
| 135 | #define setobj2n setobj |
| 136 | /* to table */ |
| 137 | #define setobj2t setobj |
| 138 | |
| 139 | |
| 140 | /* |
| 141 | ** Entries in a Lua stack. Field 'tbclist' forms a list of all |
| 142 | ** to-be-closed variables active in this stack. Dummy entries are |
| 143 | ** used when the distance between two tbc variables does not fit |
| 144 | ** in an unsigned short. They are represented by delta==0, and |
| 145 | ** their real delta is always the maximum value that fits in |
| 146 | ** that field. |
| 147 | */ |
| 148 | typedef union StackValue { |
| 149 | TValue val; |
| 150 | struct { |
| 151 | TValuefields; |
| 152 | unsigned short delta; |
| 153 | } tbclist; |
| 154 | } StackValue; |
| 155 | |
| 156 | |
| 157 | /* index to stack elements */ |
| 158 | typedef StackValue *StkId; |
| 159 | |
| 160 | |
| 161 | /* |
| 162 | ** When reallocating the stack, change all pointers to the stack into |
| 163 | ** proper offsets. |
| 164 | */ |
| 165 | typedef union { |
| 166 | StkId p; /* actual pointer */ |
| 167 | ptrdiff_t offset; /* used while the stack is being reallocated */ |
| 168 | } StkIdRel; |
| 169 | |
| 170 | |
| 171 | /* convert a 'StackValue' to a 'TValue' */ |
| 172 | #define s2v(o) (&(o)->val) |
| 173 | |
| 174 | |
| 175 | |
| 176 | /* |
| 177 | ** {================================================================== |
| 178 | ** Nil |
| 179 | ** =================================================================== |
| 180 | */ |
| 181 | |
| 182 | /* Standard nil */ |
| 183 | #define LUA_VNIL makevariant(LUA_TNIL, 0) |
| 184 | |
| 185 | /* Empty slot (which might be different from a slot containing nil) */ |
| 186 | #define LUA_VEMPTY makevariant(LUA_TNIL, 1) |
| 187 | |
| 188 | /* Value returned for a key not found in a table (absent key) */ |
| 189 | #define LUA_VABSTKEY makevariant(LUA_TNIL, 2) |
| 190 | |
| 191 | /* Special variant to signal that a fast get is accessing a non-table */ |
| 192 | #define LUA_VNOTABLE makevariant(LUA_TNIL, 3) |
| 193 | |
| 194 | |
| 195 | /* macro to test for (any kind of) nil */ |
| 196 | #define ttisnil(v) checktype((v), LUA_TNIL) |
| 197 | |
| 198 | /* |
| 199 | ** Macro to test the result of a table access. Formally, it should |
| 200 | ** distinguish between LUA_VEMPTY/LUA_VABSTKEY/LUA_VNOTABLE and |
| 201 | ** other tags. As currently nil is equivalent to LUA_VEMPTY, it is |
| 202 | ** simpler to just test whether the value is nil. |
| 203 | */ |
| 204 | #define tagisempty(tag) (novariant(tag) == LUA_TNIL) |
| 205 | |
| 206 | |
| 207 | /* macro to test for a standard nil */ |
| 208 | #define ttisstrictnil(o) checktag((o), LUA_VNIL) |
| 209 | |
| 210 | |
| 211 | #define setnilvalue(obj) settt_(obj, LUA_VNIL) |
| 212 | #define setnilvalue2s(stk) setnilvalue(s2v(stk)) |
| 213 | |
| 214 | |
| 215 | #define isabstkey(v) checktag((v), LUA_VABSTKEY) |
| 216 | |
| 217 | |
| 218 | /* |
| 219 | ** macro to detect non-standard nils (used only in assertions) |
| 220 | */ |
| 221 | #define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v)) |
| 222 | |
| 223 | |
| 224 | /* |
| 225 | ** By default, entries with any kind of nil are considered empty. |
| 226 | ** (In any definition, values associated with absent keys must also |
| 227 | ** be accepted as empty.) |
| 228 | */ |
| 229 | #define isempty(v) ttisnil(v) |
| 230 | |
| 231 | |
| 232 | /* macro defining a value corresponding to an absent key */ |
| 233 | #define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY |
| 234 | |
| 235 | |
| 236 | /* mark an entry as empty */ |
| 237 | #define setempty(v) settt_(v, LUA_VEMPTY) |
| 238 | |
| 239 | |
| 240 | |
| 241 | /* }================================================================== */ |
| 242 | |
| 243 | |
| 244 | /* |
| 245 | ** {================================================================== |
| 246 | ** Booleans |
| 247 | ** =================================================================== |
| 248 | */ |
| 249 | |
| 250 | |
| 251 | #define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0) |
| 252 | #define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1) |
| 253 | |
| 254 | #define ttisboolean(o) checktype((o), LUA_TBOOLEAN) |
| 255 | #define ttisfalse(o) checktag((o), LUA_VFALSE) |
| 256 | #define ttistrue(o) checktag((o), LUA_VTRUE) |
| 257 | |
| 258 | |
| 259 | #define l_isfalse(o) (ttisfalse(o) || ttisnil(o)) |
| 260 | #define tagisfalse(t) ((t) == LUA_VFALSE || novariant(t) == LUA_TNIL) |
| 261 | |
| 262 | |
| 263 | |
| 264 | #define setbfvalue(obj) settt_(obj, LUA_VFALSE) |
| 265 | #define setbtvalue(obj) settt_(obj, LUA_VTRUE) |
| 266 | |
| 267 | /* }================================================================== */ |
| 268 | |
| 269 | |
| 270 | /* |
| 271 | ** {================================================================== |
| 272 | ** Threads |
| 273 | ** =================================================================== |
| 274 | */ |
| 275 | |
| 276 | #define LUA_VTHREAD makevariant(LUA_TTHREAD, 0) |
| 277 | |
| 278 | #define ttisthread(o) checktag((o), ctb(LUA_VTHREAD)) |
| 279 | |
| 280 | #define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc)) |
| 281 | |
| 282 | #define setthvalue(L,obj,x) \ |
| 283 | { TValue *io = (obj); lua_State *x_ = (x); \ |
| 284 | val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \ |
| 285 | checkliveness(L,io); } |
| 286 | |
| 287 | #define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t) |
| 288 | |
| 289 | /* }================================================================== */ |
| 290 | |
| 291 | |
| 292 | /* |
| 293 | ** {================================================================== |
| 294 | ** Collectable Objects |
| 295 | ** =================================================================== |
| 296 | */ |
| 297 | |
| 298 | /* |
| 299 | ** Common Header for all collectable objects (in macro form, to be |
| 300 | ** included in other objects) |
| 301 | */ |
| 302 | #define CommonHeader struct GCObject *next; lu_byte tt; lu_byte marked |
| 303 | |
| 304 | |
| 305 | /* Common type for all collectable objects */ |
| 306 | typedef struct GCObject { |
| 307 | CommonHeader; |
| 308 | } GCObject; |
| 309 | |
| 310 | |
| 311 | /* Bit mark for collectable types */ |
| 312 | #define BIT_ISCOLLECTABLE (1 << 6) |
| 313 | |
| 314 | #define iscollectable(o) (rawtt(o) & BIT_ISCOLLECTABLE) |
| 315 | |
| 316 | /* mark a tag as collectable */ |
| 317 | #define ctb(t) ((t) | BIT_ISCOLLECTABLE) |
| 318 | |
| 319 | #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) |
| 320 | |
| 321 | #define gcvalueraw(v) ((v).gc) |
| 322 | |
| 323 | #define setgcovalue(L,obj,x) \ |
| 324 | { TValue *io = (obj); GCObject *i_g=(x); \ |
| 325 | val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); } |
| 326 | |
| 327 | /* }================================================================== */ |
| 328 | |
| 329 | |
| 330 | /* |
| 331 | ** {================================================================== |
| 332 | ** Numbers |
| 333 | ** =================================================================== |
| 334 | */ |
| 335 | |
| 336 | /* Variant tags for numbers */ |
| 337 | #define LUA_VNUMINT makevariant(LUA_TNUMBER, 0) /* integer numbers */ |
| 338 | #define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1) /* float numbers */ |
| 339 | |
| 340 | #define ttisnumber(o) checktype((o), LUA_TNUMBER) |
| 341 | #define ttisfloat(o) checktag((o), LUA_VNUMFLT) |
| 342 | #define ttisinteger(o) checktag((o), LUA_VNUMINT) |
| 343 | |
| 344 | #define nvalue(o) check_exp(ttisnumber(o), \ |
| 345 | (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) |
| 346 | #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n) |
| 347 | #define ivalue(o) check_exp(ttisinteger(o), val_(o).i) |
| 348 | |
| 349 | #define fltvalueraw(v) ((v).n) |
| 350 | #define ivalueraw(v) ((v).i) |
| 351 | |
| 352 | #define setfltvalue(obj,x) \ |
| 353 | { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); } |
| 354 | |
| 355 | #define chgfltvalue(obj,x) \ |
| 356 | { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); } |
| 357 | |
| 358 | #define setivalue(obj,x) \ |
| 359 | { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); } |
| 360 | |
| 361 | #define chgivalue(obj,x) \ |
| 362 | { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); } |
| 363 | |
| 364 | /* }================================================================== */ |
| 365 | |
| 366 | |
| 367 | /* |
| 368 | ** {================================================================== |
| 369 | ** Strings |
| 370 | ** =================================================================== |
| 371 | */ |
| 372 | |
| 373 | /* Variant tags for strings */ |
| 374 | #define LUA_VSHRSTR makevariant(LUA_TSTRING, 0) /* short strings */ |
| 375 | #define LUA_VLNGSTR makevariant(LUA_TSTRING, 1) /* long strings */ |
| 376 | |
| 377 | #define ttisstring(o) checktype((o), LUA_TSTRING) |
| 378 | #define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR)) |
| 379 | #define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR)) |
| 380 | |
| 381 | #define tsvalueraw(v) (gco2ts((v).gc)) |
| 382 | |
| 383 | #define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc)) |
| 384 | |
| 385 | #define setsvalue(L,obj,x) \ |
| 386 | { TValue *io = (obj); TString *x_ = (x); \ |
| 387 | val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \ |
| 388 | checkliveness(L,io); } |
| 389 | |
| 390 | /* set a string to the stack */ |
| 391 | #define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s) |
| 392 | |
| 393 | /* set a string to a new object */ |
| 394 | #define setsvalue2n setsvalue |
| 395 | |
| 396 | |
| 397 | /* Kinds of long strings (stored in 'shrlen') */ |
| 398 | #define LSTRREG -1 /* regular long string */ |
| 399 | #define LSTRFIX -2 /* fixed external long string */ |
| 400 | #define LSTRMEM -3 /* external long string with deallocation */ |
| 401 | |
| 402 | |
| 403 | /* |
| 404 | ** Header for a string value. |
| 405 | */ |
| 406 | typedef struct TString { |
| 407 | CommonHeader; |
| 408 | lu_byte extra; /* reserved words for short strings; "has hash" for longs */ |
| 409 | ls_byte shrlen; /* length for short strings, negative for long strings */ |
| 410 | unsigned int hash; |
| 411 | union { |
| 412 | size_t lnglen; /* length for long strings */ |
| 413 | struct TString *hnext; /* linked list for hash table */ |
| 414 | } u; |
| 415 | char *contents; /* pointer to content in long strings */ |
| 416 | lua_Alloc falloc; /* deallocation function for external strings */ |
| 417 | void *ud; /* user data for external strings */ |
| 418 | } TString; |
| 419 | |
| 420 | |
| 421 | #define strisshr(ts) ((ts)->shrlen >= 0) |
| 422 | #define isextstr(ts) (ttislngstring(ts) && tsvalue(ts)->shrlen != LSTRREG) |
| 423 | |
| 424 | |
| 425 | /* |
| 426 | ** Get the actual string (array of bytes) from a 'TString'. (Generic |
| 427 | ** version and specialized versions for long and short strings.) |
| 428 | */ |
| 429 | #define rawgetshrstr(ts) (cast_charp(&(ts)->contents)) |
| 430 | #define getshrstr(ts) check_exp(strisshr(ts), rawgetshrstr(ts)) |
| 431 | #define getlngstr(ts) check_exp(!strisshr(ts), (ts)->contents) |
| 432 | #define getstr(ts) (strisshr(ts) ? rawgetshrstr(ts) : (ts)->contents) |
| 433 | |
| 434 | |
| 435 | /* get string length from 'TString *ts' */ |
| 436 | #define tsslen(ts) \ |
| 437 | (strisshr(ts) ? cast_sizet((ts)->shrlen) : (ts)->u.lnglen) |
| 438 | |
| 439 | /* |
| 440 | ** Get string and length */ |
| 441 | #define getlstr(ts, len) \ |
| 442 | (strisshr(ts) \ |
| 443 | ? (cast_void((len) = cast_sizet((ts)->shrlen)), rawgetshrstr(ts)) \ |
| 444 | : (cast_void((len) = (ts)->u.lnglen), (ts)->contents)) |
| 445 | |
| 446 | /* }================================================================== */ |
| 447 | |
| 448 | |
| 449 | /* |
| 450 | ** {================================================================== |
| 451 | ** Userdata |
| 452 | ** =================================================================== |
| 453 | */ |
| 454 | |
| 455 | |
| 456 | /* |
| 457 | ** Light userdata should be a variant of userdata, but for compatibility |
| 458 | ** reasons they are also different types. |
| 459 | */ |
| 460 | #define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0) |
| 461 | |
| 462 | #define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0) |
| 463 | |
| 464 | #define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA) |
| 465 | #define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA)) |
| 466 | |
| 467 | #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) |
| 468 | #define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc)) |
| 469 | |
| 470 | #define pvalueraw(v) ((v).p) |
| 471 | |
| 472 | #define setpvalue(obj,x) \ |
| 473 | { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); } |
| 474 | |
| 475 | #define setuvalue(L,obj,x) \ |
| 476 | { TValue *io = (obj); Udata *x_ = (x); \ |
| 477 | val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \ |
| 478 | checkliveness(L,io); } |
| 479 | |
| 480 | |
| 481 | /* Ensures that addresses after this type are always fully aligned. */ |
| 482 | typedef union UValue { |
| 483 | TValue uv; |
| 484 | LUAI_MAXALIGN; /* ensures maximum alignment for udata bytes */ |
| 485 | } UValue; |
| 486 | |
| 487 | |
| 488 | /* |
| 489 | ** Header for userdata with user values; |
| 490 | ** memory area follows the end of this structure. |
| 491 | */ |
| 492 | typedef struct Udata { |
| 493 | CommonHeader; |
| 494 | unsigned short nuvalue; /* number of user values */ |
| 495 | size_t len; /* number of bytes */ |
| 496 | struct Table *metatable; |
| 497 | GCObject *gclist; |
| 498 | UValue uv[1]; /* user values */ |
| 499 | } Udata; |
| 500 | |
| 501 | |
| 502 | /* |
| 503 | ** Header for userdata with no user values. These userdata do not need |
| 504 | ** to be gray during GC, and therefore do not need a 'gclist' field. |
| 505 | ** To simplify, the code always use 'Udata' for both kinds of userdata, |
| 506 | ** making sure it never accesses 'gclist' on userdata with no user values. |
| 507 | ** This structure here is used only to compute the correct size for |
| 508 | ** this representation. (The 'bindata' field in its end ensures correct |
| 509 | ** alignment for binary data following this header.) |
| 510 | */ |
| 511 | typedef struct Udata0 { |
| 512 | CommonHeader; |
| 513 | unsigned short nuvalue; /* number of user values */ |
| 514 | size_t len; /* number of bytes */ |
| 515 | struct Table *metatable; |
| 516 | union {LUAI_MAXALIGN;} bindata; |
| 517 | } Udata0; |
| 518 | |
| 519 | |
| 520 | /* compute the offset of the memory area of a userdata */ |
| 521 | #define udatamemoffset(nuv) \ |
| 522 | ((nuv) == 0 ? offsetof(Udata0, bindata) \ |
| 523 | : offsetof(Udata, uv) + (sizeof(UValue) * (nuv))) |
| 524 | |
| 525 | /* get the address of the memory block inside 'Udata' */ |
| 526 | #define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue)) |
| 527 | |
| 528 | /* compute the size of a userdata */ |
| 529 | #define sizeudata(nuv,nb) (udatamemoffset(nuv) + (nb)) |
| 530 | |
| 531 | /* }================================================================== */ |
| 532 | |
| 533 | |
| 534 | /* |
| 535 | ** {================================================================== |
| 536 | ** Prototypes |
| 537 | ** =================================================================== |
| 538 | */ |
| 539 | |
| 540 | #define LUA_VPROTO makevariant(LUA_TPROTO, 0) |
| 541 | |
| 542 | |
| 543 | typedef l_uint32 Instruction; |
| 544 | |
| 545 | |
| 546 | /* |
| 547 | ** Description of an upvalue for function prototypes |
| 548 | */ |
| 549 | typedef struct Upvaldesc { |
| 550 | TString *name; /* upvalue name (for debug information) */ |
| 551 | lu_byte instack; /* whether it is in stack (register) */ |
| 552 | lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ |
| 553 | lu_byte kind; /* kind of corresponding variable */ |
| 554 | } Upvaldesc; |
| 555 | |
| 556 | |
| 557 | /* |
| 558 | ** Description of a local variable for function prototypes |
| 559 | ** (used for debug information) |
| 560 | */ |
| 561 | typedef struct LocVar { |
| 562 | TString *varname; |
| 563 | int startpc; /* first point where variable is active */ |
| 564 | int endpc; /* first point where variable is dead */ |
| 565 | } LocVar; |
| 566 | |
| 567 | |
| 568 | /* |
| 569 | ** Associates the absolute line source for a given instruction ('pc'). |
| 570 | ** The array 'lineinfo' gives, for each instruction, the difference in |
| 571 | ** lines from the previous instruction. When that difference does not |
| 572 | ** fit into a byte, Lua saves the absolute line for that instruction. |
| 573 | ** (Lua also saves the absolute line periodically, to speed up the |
| 574 | ** computation of a line number: we can use binary search in the |
| 575 | ** absolute-line array, but we must traverse the 'lineinfo' array |
| 576 | ** linearly to compute a line.) |
| 577 | */ |
| 578 | typedef struct AbsLineInfo { |
| 579 | int pc; |
| 580 | int line; |
| 581 | } AbsLineInfo; |
| 582 | |
| 583 | |
| 584 | /* |
| 585 | ** Flags in Prototypes |
| 586 | */ |
| 587 | #define PF_VAHID 1 /* function has hidden vararg arguments */ |
| 588 | #define PF_VATAB 2 /* function has vararg table */ |
| 589 | #define PF_FIXED 4 /* prototype has parts in fixed memory */ |
| 590 | |
| 591 | /* a vararg function either has hidden args. or a vararg table */ |
| 592 | #define isvararg(p) ((p)->flag & (PF_VAHID | PF_VATAB)) |
| 593 | |
| 594 | /* |
| 595 | ** mark that a function needs a vararg table. (The flag PF_VAHID will |
| 596 | ** be cleared later.) |
| 597 | */ |
| 598 | #define needvatab(p) ((p)->flag |= PF_VATAB) |
| 599 | |
| 600 | /* |
| 601 | ** Function Prototypes |
| 602 | */ |
| 603 | typedef struct Proto { |
| 604 | CommonHeader; |
| 605 | lu_byte numparams; /* number of fixed (named) parameters */ |
| 606 | lu_byte flag; |
| 607 | lu_byte maxstacksize; /* number of registers needed by this function */ |
| 608 | int sizeupvalues; /* size of 'upvalues' */ |
| 609 | int sizek; /* size of 'k' */ |
| 610 | int sizecode; |
| 611 | int sizelineinfo; |
| 612 | int sizep; /* size of 'p' */ |
| 613 | int sizelocvars; |
| 614 | int sizeabslineinfo; /* size of 'abslineinfo' */ |
| 615 | int linedefined; /* debug information */ |
| 616 | int lastlinedefined; /* debug information */ |
| 617 | TValue *k; /* constants used by the function */ |
| 618 | Instruction *code; /* opcodes */ |
| 619 | struct Proto **p; /* functions defined inside the function */ |
| 620 | Upvaldesc *upvalues; /* upvalue information */ |
| 621 | ls_byte *lineinfo; /* information about source lines (debug information) */ |
| 622 | AbsLineInfo *abslineinfo; /* idem */ |
| 623 | LocVar *locvars; /* information about local variables (debug information) */ |
| 624 | TString *source; /* used for debug information */ |
| 625 | GCObject *gclist; |
| 626 | } Proto; |
| 627 | |
| 628 | /* }================================================================== */ |
| 629 | |
| 630 | |
| 631 | /* |
| 632 | ** {================================================================== |
| 633 | ** Functions |
| 634 | ** =================================================================== |
| 635 | */ |
| 636 | |
| 637 | #define LUA_VUPVAL makevariant(LUA_TUPVAL, 0) |
| 638 | |
| 639 | |
| 640 | /* Variant tags for functions */ |
| 641 | #define LUA_VLCL makevariant(LUA_TFUNCTION, 0) /* Lua closure */ |
| 642 | #define LUA_VLCF makevariant(LUA_TFUNCTION, 1) /* light C function */ |
| 643 | #define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */ |
| 644 | |
| 645 | #define ttisfunction(o) checktype(o, LUA_TFUNCTION) |
| 646 | #define ttisLclosure(o) checktag((o), ctb(LUA_VLCL)) |
| 647 | #define ttislcf(o) checktag((o), LUA_VLCF) |
| 648 | #define ttisCclosure(o) checktag((o), ctb(LUA_VCCL)) |
| 649 | #define ttisclosure(o) (ttisLclosure(o) || ttisCclosure(o)) |
| 650 | |
| 651 | |
| 652 | #define isLfunction(o) ttisLclosure(o) |
| 653 | |
| 654 | #define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc)) |
| 655 | #define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc)) |
| 656 | #define fvalue(o) check_exp(ttislcf(o), val_(o).f) |
| 657 | #define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc)) |
| 658 | |
| 659 | #define fvalueraw(v) ((v).f) |
| 660 | |
| 661 | #define setclLvalue(L,obj,x) \ |
| 662 | { TValue *io = (obj); LClosure *x_ = (x); \ |
| 663 | val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \ |
| 664 | checkliveness(L,io); } |
| 665 | |
| 666 | #define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl) |
| 667 | |
| 668 | #define setfvalue(obj,x) \ |
| 669 | { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); } |
| 670 | |
| 671 | #define setclCvalue(L,obj,x) \ |
| 672 | { TValue *io = (obj); CClosure *x_ = (x); \ |
| 673 | val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \ |
| 674 | checkliveness(L,io); } |
| 675 | |
| 676 | |
| 677 | /* |
| 678 | ** Upvalues for Lua closures |
| 679 | */ |
| 680 | typedef struct UpVal { |
| 681 | CommonHeader; |
| 682 | union { |
| 683 | TValue *p; /* points to stack or to its own value */ |
| 684 | ptrdiff_t offset; /* used while the stack is being reallocated */ |
| 685 | } v; |
| 686 | union { |
| 687 | struct { /* (when open) */ |
| 688 | struct UpVal *next; /* linked list */ |
| 689 | struct UpVal **previous; |
| 690 | } open; |
| 691 | TValue value; /* the value (when closed) */ |
| 692 | } u; |
| 693 | } UpVal; |
| 694 | |
| 695 | |
| 696 | |
| 697 | #define ClosureHeader \ |
| 698 | CommonHeader; lu_byte nupvalues; GCObject *gclist |
| 699 | |
| 700 | typedef struct CClosure { |
| 701 | ClosureHeader; |
| 702 | lua_CFunction f; |
| 703 | TValue upvalue[1]; /* list of upvalues */ |
| 704 | } CClosure; |
| 705 | |
| 706 | |
| 707 | typedef struct LClosure { |
| 708 | ClosureHeader; |
| 709 | struct Proto *p; |
| 710 | UpVal *upvals[1]; /* list of upvalues */ |
| 711 | } LClosure; |
| 712 | |
| 713 | |
| 714 | typedef union Closure { |
| 715 | CClosure c; |
| 716 | LClosure l; |
| 717 | } Closure; |
| 718 | |
| 719 | |
| 720 | #define getproto(o) (clLvalue(o)->p) |
| 721 | |
| 722 | /* }================================================================== */ |
| 723 | |
| 724 | |
| 725 | /* |
| 726 | ** {================================================================== |
| 727 | ** Tables |
| 728 | ** =================================================================== |
| 729 | */ |
| 730 | |
| 731 | #define LUA_VTABLE makevariant(LUA_TTABLE, 0) |
| 732 | |
| 733 | #define ttistable(o) checktag((o), ctb(LUA_VTABLE)) |
| 734 | |
| 735 | #define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc)) |
| 736 | |
| 737 | #define sethvalue(L,obj,x) \ |
| 738 | { TValue *io = (obj); Table *x_ = (x); \ |
| 739 | val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \ |
| 740 | checkliveness(L,io); } |
| 741 | |
| 742 | #define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h) |
| 743 | |
| 744 | |
| 745 | /* |
| 746 | ** Nodes for Hash tables: A pack of two TValue's (key-value pairs) |
| 747 | ** plus a 'next' field to link colliding entries. The distribution |
| 748 | ** of the key's fields ('key_tt' and 'key_val') not forming a proper |
| 749 | ** 'TValue' allows for a smaller size for 'Node' both in 4-byte |
| 750 | ** and 8-byte alignments. |
| 751 | */ |
| 752 | typedef union Node { |
| 753 | struct NodeKey { |
| 754 | TValuefields; /* fields for value */ |
| 755 | lu_byte key_tt; /* key type */ |
| 756 | int next; /* for chaining */ |
| 757 | Value key_val; /* key value */ |
| 758 | } u; |
| 759 | TValue i_val; /* direct access to node's value as a proper 'TValue' */ |
| 760 | } Node; |
| 761 | |
| 762 | |
| 763 | /* copy a value into a key */ |
| 764 | #define setnodekey(node,obj) \ |
| 765 | { Node *n_=(node); const TValue *io_=(obj); \ |
| 766 | n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; } |
| 767 | |
| 768 | |
| 769 | /* copy a value from a key */ |
| 770 | #define getnodekey(L,obj,node) \ |
| 771 | { TValue *io_=(obj); const Node *n_=(node); \ |
| 772 | io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \ |
| 773 | checkliveness(L,io_); } |
| 774 | |
| 775 | |
| 776 | |
| 777 | typedef struct Table { |
| 778 | CommonHeader; |
| 779 | lu_byte flags; /* 1<<p means tagmethod(p) is not present */ |
| 780 | lu_byte lsizenode; /* log2 of number of slots of 'node' array */ |
| 781 | unsigned int asize; /* number of slots in 'array' array */ |
| 782 | Value *array; /* array part */ |
| 783 | Node *node; |
| 784 | struct Table *metatable; |
| 785 | GCObject *gclist; |
| 786 | } Table; |
| 787 | |
| 788 | |
| 789 | /* |
| 790 | ** Macros to manipulate keys inserted in nodes |
| 791 | */ |
| 792 | #define keytt(node) ((node)->u.key_tt) |
| 793 | #define keyval(node) ((node)->u.key_val) |
| 794 | |
| 795 | #define keyisnil(node) (keytt(node) == LUA_TNIL) |
| 796 | #define keyisinteger(node) (keytt(node) == LUA_VNUMINT) |
| 797 | #define keyival(node) (keyval(node).i) |
| 798 | #define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR)) |
| 799 | #define keystrval(node) (gco2ts(keyval(node).gc)) |
| 800 | |
| 801 | #define setnilkey(node) (keytt(node) = LUA_TNIL) |
| 802 | |
| 803 | #define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE) |
| 804 | |
| 805 | #define gckey(n) (keyval(n).gc) |
| 806 | #define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL) |
| 807 | |
| 808 | |
| 809 | /* |
| 810 | ** Dead keys in tables have the tag DEADKEY but keep their original |
| 811 | ** gcvalue. This distinguishes them from regular keys but allows them to |
| 812 | ** be found when searched in a special way. ('next' needs that to find |
| 813 | ** keys removed from a table during a traversal.) |
| 814 | */ |
| 815 | #define setdeadkey(node) (keytt(node) = LUA_TDEADKEY) |
| 816 | #define keyisdead(node) (keytt(node) == LUA_TDEADKEY) |
| 817 | |
| 818 | /* }================================================================== */ |
| 819 | |
| 820 | |
| 821 | |
| 822 | /* |
| 823 | ** 'module' operation for hashing (size is always a power of 2) |
| 824 | */ |
| 825 | #define lmod(s,size) \ |
| 826 | (check_exp((size&(size-1))==0, (cast_uint(s) & cast_uint((size)-1)))) |
| 827 | |
| 828 | |
| 829 | #define twoto(x) (1u<<(x)) |
| 830 | #define sizenode(t) cast_uint(twoto((t)->lsizenode)) |
| 831 | |
| 832 | |
| 833 | /* size of buffer for 'luaO_utf8esc' function */ |
| 834 | #define UTF8BUFFSZ 8 |
| 835 | |
| 836 | |
| 837 | /* macro to call 'luaO_pushvfstring' correctly */ |
| 838 | #define pushvfstring(L, argp, fmt, msg) \ |
| 839 | { va_start(argp, fmt); \ |
| 840 | msg = luaO_pushvfstring(L, fmt, argp); \ |
| 841 | va_end(argp); \ |
| 842 | if (msg == NULL) luaD_throw(L, LUA_ERRMEM); /* only after 'va_end' */ } |
| 843 | |
| 844 | |
| 845 | LUAI_FUNC int luaO_utf8esc (char *buff, l_uint32 x); |
| 846 | LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x); |
| 847 | LUAI_FUNC lu_byte luaO_codeparam (unsigned int p); |
| 848 | LUAI_FUNC l_mem luaO_applyparam (lu_byte p, l_mem x); |
| 849 | |
| 850 | LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1, |
| 851 | const TValue *p2, TValue *res); |
| 852 | LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, |
| 853 | const TValue *p2, StkId res); |
| 854 | LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o); |
| 855 | LUAI_FUNC unsigned luaO_tostringbuff (const TValue *obj, char *buff); |
| 856 | LUAI_FUNC lu_byte luaO_hexavalue (int c); |
| 857 | LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj); |
| 858 | LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, |
| 859 | va_list argp); |
| 860 | LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); |
| 861 | LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen); |
| 862 | |
| 863 | |
| 864 | #endif |
| 865 |