lj_obj.h (36384B)
1 /* 2 ** LuaJIT VM tags, values and objects. 3 ** Copyright (C) 2005-2016 Mike Pall. See Copyright Notice in luajit.h 4 ** 5 ** Portions taken verbatim or adapted from the Lua interpreter. 6 ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h 7 */ 8 9 #ifndef _LJ_OBJ_H 10 #define _LJ_OBJ_H 11 12 #include "lua.h" 13 #include "lj_def.h" 14 #include "lj_arch.h" 15 16 /* -- Memory references (32 bit address space) ---------------------------- */ 17 18 /* Memory and GC object sizes. */ 19 typedef uint32_t MSize; 20 #if LJ_64 21 typedef uint64_t GCSize; 22 #else 23 typedef uint32_t GCSize; 24 #endif 25 26 /* Memory size difference */ 27 typedef int32_t MDiff; 28 29 /* Memory reference */ 30 typedef struct MRef { 31 #if LJ_GC64 32 uint64_t ptr64; /* True 64 bit pointer. */ 33 #else 34 uint32_t ptr32; /* Pseudo 32 bit pointer. */ 35 #endif 36 } MRef; 37 38 #if LJ_GC64 39 #define mref(r, t) ((t *)(void *)(r).ptr64) 40 41 #define setmref(r, p) ((r).ptr64 = (uint64_t)(void *)(p)) 42 #define setmrefr(r, v) ((r).ptr64 = (v).ptr64) 43 #else 44 #define mref(r, t) ((t *)(void *)(uintptr_t)(r).ptr32) 45 46 #define setmref(r, p) ((r).ptr32 = (uint32_t)(uintptr_t)(void *)(p)) 47 #define setmrefr(r, v) ((r).ptr32 = (v).ptr32) 48 #endif 49 50 /* -- GC object references (32 bit address space) ------------------------- */ 51 52 /* GCobj reference */ 53 typedef struct GCRef { 54 #if LJ_GC64 55 uint64_t gcptr64; /* True 64 bit pointer. */ 56 #else 57 uint32_t gcptr32; /* Pseudo 32 bit pointer. */ 58 #endif 59 } GCRef; 60 61 /* Common GC header for all collectable objects. */ 62 #define GCHeader GCRef nextgc; uint8_t marked; uint8_t gct 63 /* This occupies 6 bytes, so use the next 2 bytes for non-32 bit fields. */ 64 65 #if LJ_GC64 66 #define gcref(r) ((GCobj *)(r).gcptr64) 67 #define gcrefp(r, t) ((t *)(void *)(r).gcptr64) 68 #define gcrefu(r) ((r).gcptr64) 69 #define gcrefeq(r1, r2) ((r1).gcptr64 == (r2).gcptr64) 70 71 #define setgcref(r, gc) ((r).gcptr64 = (uint64_t)&(gc)->gch) 72 #define setgcreft(r, gc, it) \ 73 (r).gcptr64 = (uint64_t)&(gc)->gch | (((uint64_t)(it)) << 47) 74 #define setgcrefp(r, p) ((r).gcptr64 = (uint64_t)(p)) 75 #define setgcrefnull(r) ((r).gcptr64 = 0) 76 #define setgcrefr(r, v) ((r).gcptr64 = (v).gcptr64) 77 #else 78 #define gcref(r) ((GCobj *)(uintptr_t)(r).gcptr32) 79 #define gcrefp(r, t) ((t *)(void *)(uintptr_t)(r).gcptr32) 80 #define gcrefu(r) ((r).gcptr32) 81 #define gcrefeq(r1, r2) ((r1).gcptr32 == (r2).gcptr32) 82 83 #define setgcref(r, gc) ((r).gcptr32 = (uint32_t)(uintptr_t)&(gc)->gch) 84 #define setgcrefp(r, p) ((r).gcptr32 = (uint32_t)(uintptr_t)(p)) 85 #define setgcrefnull(r) ((r).gcptr32 = 0) 86 #define setgcrefr(r, v) ((r).gcptr32 = (v).gcptr32) 87 #endif 88 89 #define gcnext(gc) (gcref((gc)->gch.nextgc)) 90 91 /* IMPORTANT NOTE: 92 ** 93 ** All uses of the setgcref* macros MUST be accompanied with a write barrier. 94 ** 95 ** This is to ensure the integrity of the incremental GC. The invariant 96 ** to preserve is that a black object never points to a white object. 97 ** I.e. never store a white object into a field of a black object. 98 ** 99 ** It's ok to LEAVE OUT the write barrier ONLY in the following cases: 100 ** - The source is not a GC object (NULL). 101 ** - The target is a GC root. I.e. everything in global_State. 102 ** - The target is a lua_State field (threads are never black). 103 ** - The target is a stack slot, see setgcV et al. 104 ** - The target is an open upvalue, i.e. pointing to a stack slot. 105 ** - The target is a newly created object (i.e. marked white). But make 106 ** sure nothing invokes the GC inbetween. 107 ** - The target and the source are the same object (self-reference). 108 ** - The target already contains the object (e.g. moving elements around). 109 ** 110 ** The most common case is a store to a stack slot. All other cases where 111 ** a barrier has been omitted are annotated with a NOBARRIER comment. 112 ** 113 ** The same logic applies for stores to table slots (array part or hash 114 ** part). ALL uses of lj_tab_set* require a barrier for the stored value 115 ** *and* the stored key, based on the above rules. In practice this means 116 ** a barrier is needed if *either* of the key or value are a GC object. 117 ** 118 ** It's ok to LEAVE OUT the write barrier in the following special cases: 119 ** - The stored value is nil. The key doesn't matter because it's either 120 ** not resurrected or lj_tab_newkey() will take care of the key barrier. 121 ** - The key doesn't matter if the *previously* stored value is guaranteed 122 ** to be non-nil (because the key is kept alive in the table). 123 ** - The key doesn't matter if it's guaranteed not to be part of the table, 124 ** since lj_tab_newkey() takes care of the key barrier. This applies 125 ** trivially to new tables, but watch out for resurrected keys. Storing 126 ** a nil value leaves the key in the table! 127 ** 128 ** In case of doubt use lj_gc_anybarriert() as it's rather cheap. It's used 129 ** by the interpreter for all table stores. 130 ** 131 ** Note: In contrast to Lua's GC, LuaJIT's GC does *not* specially mark 132 ** dead keys in tables. The reference is left in, but it's guaranteed to 133 ** be never dereferenced as long as the value is nil. It's ok if the key is 134 ** freed or if any object subsequently gets the same address. 135 ** 136 ** Not destroying dead keys helps to keep key hash slots stable. This avoids 137 ** specialization back-off for HREFK when a value flips between nil and 138 ** non-nil and the GC gets in the way. It also allows safely hoisting 139 ** HREF/HREFK across GC steps. Dead keys are only removed if a table is 140 ** resized (i.e. by NEWREF) and xREF must not be CSEd across a resize. 141 ** 142 ** The trade-off is that a write barrier for tables must take the key into 143 ** account, too. Implicitly resurrecting the key by storing a non-nil value 144 ** may invalidate the incremental GC invariant. 145 */ 146 147 /* -- Common type definitions --------------------------------------------- */ 148 149 /* Types for handling bytecodes. Need this here, details in lj_bc.h. */ 150 typedef uint32_t BCIns; /* Bytecode instruction. */ 151 typedef uint32_t BCPos; /* Bytecode position. */ 152 typedef uint32_t BCReg; /* Bytecode register. */ 153 typedef int32_t BCLine; /* Bytecode line number. */ 154 155 /* Internal assembler functions. Never call these directly from C. */ 156 typedef void (*ASMFunction)(void); 157 158 /* Resizable string buffer. Need this here, details in lj_buf.h. */ 159 typedef struct SBuf { 160 MRef p; /* String buffer pointer. */ 161 MRef e; /* String buffer end pointer. */ 162 MRef b; /* String buffer base. */ 163 MRef L; /* lua_State, used for buffer resizing. */ 164 } SBuf; 165 166 /* Match state for pattern captures. Directly accesed by emitted JIT code. */ 167 typedef struct MatchState { 168 uint32_t findret1, findret2; 169 uint32_t level; /* total number of captures (finished or unfinished) */ 170 struct { 171 MRef init; 172 MSize len; 173 } capture[LUA_MAXCAPTURES]; 174 const char *src_init; /* init of source string */ 175 const char *src_end; /* end (`\0') of source string */ 176 const char *p_end; /* end ('\0') of pattern */ 177 lua_State *L; 178 int depth; 179 int backtracks; 180 } MatchState; 181 #define CAP_UNFINISHED ((MSize)(-1)) 182 #define CAP_POSITION ((MSize)(-2)) 183 184 185 /* -- Tags and values ----------------------------------------------------- */ 186 187 /* Frame link. */ 188 typedef union { 189 int32_t ftsz; /* Frame type and size of previous frame. */ 190 MRef pcr; /* Or PC for Lua frames. */ 191 } FrameLink; 192 193 /* Tagged value. */ 194 typedef LJ_ALIGN(8) union TValue { 195 uint64_t u64; /* 64 bit pattern overlaps number. */ 196 lua_Number n; /* Number object overlaps split tag/value object. */ 197 #if LJ_GC64 198 GCRef gcr; /* GCobj reference with tag. */ 199 int64_t it64; 200 struct { 201 LJ_ENDIAN_LOHI( 202 int32_t i; /* Integer value. */ 203 , uint32_t it; /* Internal object tag. Must overlap MSW of number. */ 204 ) 205 }; 206 #else 207 struct { 208 LJ_ENDIAN_LOHI( 209 union { 210 GCRef gcr; /* GCobj reference (if any). */ 211 int32_t i; /* Integer value. */ 212 }; 213 , uint32_t it; /* Internal object tag. Must overlap MSW of number. */ 214 ) 215 }; 216 #endif 217 #if LJ_FR2 218 int64_t ftsz; /* Frame type and size of previous frame, or PC. */ 219 #else 220 struct { 221 LJ_ENDIAN_LOHI( 222 GCRef func; /* Function for next frame (or dummy L). */ 223 , FrameLink tp; /* Link to previous frame. */ 224 ) 225 } fr; 226 #endif 227 struct { 228 LJ_ENDIAN_LOHI( 229 uint32_t lo; /* Lower 32 bits of number. */ 230 , uint32_t hi; /* Upper 32 bits of number. */ 231 ) 232 } u32; 233 } TValue; 234 235 typedef const TValue cTValue; 236 237 #define tvref(r) (mref(r, TValue)) 238 239 /* More external and GCobj tags for internal objects. */ 240 #define LAST_TT LUA_TTHREAD 241 #define LUA_TPROTO (LAST_TT+1) 242 #define LUA_TCDATA (LAST_TT+2) 243 244 /* Internal object tags. 245 ** 246 ** Format for 32 bit GC references (!LJ_GC64): 247 ** 248 ** Internal tags overlap the MSW of a number object (must be a double). 249 ** Interpreted as a double these are special NaNs. The FPU only generates 250 ** one type of NaN (0xfff8_0000_0000_0000). So MSWs > 0xfff80000 are available 251 ** for use as internal tags. Small negative numbers are used to shorten the 252 ** encoding of type comparisons (reg/mem against sign-ext. 8 bit immediate). 253 ** 254 ** ---MSW---.---LSW--- 255 ** primitive types | itype | | 256 ** lightuserdata | itype | void * | (32 bit platforms) 257 ** lightuserdata |ffff| void * | (64 bit platforms, 47 bit pointers) 258 ** GC objects | itype | GCRef | 259 ** int (LJ_DUALNUM)| itype | int | 260 ** number -------double------ 261 ** 262 ** Format for 64 bit GC references (LJ_GC64): 263 ** 264 ** The upper 13 bits must be 1 (0xfff8...) for a special NaN. The next 265 ** 4 bits hold the internal tag. The lowest 47 bits either hold a pointer, 266 ** a zero-extended 32 bit integer or all bits set to 1 for primitive types. 267 ** 268 ** ------MSW------.------LSW------ 269 ** primitive types |1..1|itype|1..................1| 270 ** GC objects/lightud |1..1|itype|-------GCRef--------| 271 ** int (LJ_DUALNUM) |1..1|itype|0..0|-----int-------| 272 ** number ------------double------------- 273 ** 274 ** ORDER LJ_T 275 ** Primitive types nil/false/true must be first, lightuserdata next. 276 ** GC objects are at the end, table/userdata must be lowest. 277 ** Also check lj_ir.h for similar ordering constraints. 278 */ 279 #define LJ_TNIL (~0u) 280 #define LJ_TFALSE (~1u) 281 #define LJ_TTRUE (~2u) 282 #define LJ_TLIGHTUD (~3u) 283 #define LJ_TSTR (~4u) 284 #define LJ_TUPVAL (~5u) 285 #define LJ_TTHREAD (~6u) 286 #define LJ_TPROTO (~7u) 287 #define LJ_TFUNC (~8u) 288 #define LJ_TTRACE (~9u) 289 #define LJ_TCDATA (~10u) 290 #define LJ_TTAB (~11u) 291 #define LJ_TUDATA (~12u) 292 /* This is just the canonical number type used in some places. */ 293 #define LJ_TNUMX (~13u) 294 295 /* Integers have itype == LJ_TISNUM doubles have itype < LJ_TISNUM */ 296 #if LJ_64 && !LJ_GC64 297 #define LJ_TISNUM 0xfffeffffu 298 #else 299 #define LJ_TISNUM LJ_TNUMX 300 #endif 301 #define LJ_TISTRUECOND LJ_TFALSE 302 #define LJ_TISPRI LJ_TTRUE 303 #define LJ_TISGCV (LJ_TSTR+1) 304 #define LJ_TISTABUD LJ_TTAB 305 306 #if LJ_GC64 307 #define LJ_GCVMASK (((uint64_t)1 << 47) - 1) 308 #endif 309 310 /* -- String object ------------------------------------------------------- */ 311 312 /* String object header. String payload follows. */ 313 typedef struct GCstr { 314 GCHeader; 315 uint8_t reserved; /* Used by lexer for fast lookup of reserved words. */ 316 uint8_t unused; 317 MSize hash; /* Hash of string. */ 318 MSize len; /* Size of string. */ 319 } GCstr; 320 321 #define strref(r) (&gcref((r))->str) 322 #define strdata(s) ((const char *)((s)+1)) 323 #define strdatawr(s) ((char *)((s)+1)) 324 #define strVdata(o) strdata(strV(o)) 325 #define sizestring(s) (sizeof(struct GCstr)+(s)->len+1) 326 327 /* -- Userdata object ----------------------------------------------------- */ 328 329 /* Userdata object. Payload follows. */ 330 typedef struct GCudata { 331 GCHeader; 332 uint8_t udtype; /* Userdata type. */ 333 uint8_t envtt; /* Userdata ttype. */ 334 GCRef env; /* Should be at same offset in GCfunc. */ 335 MSize len; /* Size of payload. */ 336 GCRef metatable; /* Must be at same offset in GCtab. */ 337 #ifdef LJ_64 338 uint32_t align1; /* To force 8 byte alignment of the payload. */ 339 #endif 340 } GCudata; 341 342 /* Userdata types. */ 343 enum { 344 UDTYPE_USERDATA, /* Regular userdata. */ 345 UDTYPE_IO_FILE, /* I/O library FILE. */ 346 UDTYPE_FFI_CLIB, /* FFI C library namespace. */ 347 UDTYPE__MAX 348 }; 349 350 #define uddata(u) ((void *)((u)+1)) 351 #define sizeudata(u) (sizeof(struct GCudata)+(u)->len) 352 353 /* -- C data object ------------------------------------------------------- */ 354 355 /* C data object. Payload follows. */ 356 typedef struct GCcdata { 357 GCHeader; 358 uint16_t ctypeid; /* C type ID. */ 359 } GCcdata; 360 361 /* Prepended to variable-sized or realigned C data objects. */ 362 typedef struct GCcdataVar { 363 uint16_t offset; /* Offset to allocated memory (relative to GCcdata). */ 364 uint16_t extra; /* Extra space allocated (incl. GCcdata + GCcdatav). */ 365 MSize len; /* Size of payload. */ 366 } GCcdataVar; 367 368 #define cdataptr(cd) ((void *)((cd)+1)) 369 #define cdataisv(cd) ((cd)->marked & LJ_GC_CDATAV) 370 #define cdatav(cd) ((GCcdataVar *)((char *)(cd) - sizeof(GCcdataVar))) 371 #define cdatavlen(cd) check_exp(cdataisv(cd), cdatav(cd)->len) 372 #define sizecdatav(cd) (cdatavlen(cd) + cdatav(cd)->extra) 373 #define memcdatav(cd) ((void *)((char *)(cd) - cdatav(cd)->offset)) 374 375 /* -- Prototype object ---------------------------------------------------- */ 376 377 #define SCALE_NUM_GCO ((int32_t)sizeof(lua_Number)/sizeof(GCRef)) 378 #define round_nkgc(n) (((n) + SCALE_NUM_GCO-1) & ~(SCALE_NUM_GCO-1)) 379 380 typedef struct GCproto { 381 GCHeader; 382 uint8_t numparams; /* Number of parameters. */ 383 uint8_t framesize; /* Fixed frame size. */ 384 MSize sizebc; /* Number of bytecode instructions. */ 385 #if LJ_GC64 386 uint32_t unused_gc64; 387 #endif 388 GCRef gclist; 389 MRef k; /* Split constant array (points to the middle). */ 390 MRef uv; /* Upvalue list. local slot|0x8000 or parent uv idx. */ 391 MSize sizekgc; /* Number of collectable constants. */ 392 MSize sizekn; /* Number of lua_Number constants. */ 393 MSize sizept; /* Total size including colocated arrays. */ 394 uint8_t sizeuv; /* Number of upvalues. */ 395 uint8_t flags; /* Miscellaneous flags (see below). */ 396 uint16_t trace; /* Anchor for chain of root traces. */ 397 /* ------ The following fields are for debugging/tracebacks only ------ */ 398 GCRef chunkname; /* Name of the chunk this function was defined in. */ 399 BCLine firstline; /* First line of the function definition. */ 400 BCLine numline; /* Number of lines for the function definition. */ 401 MRef lineinfo; /* Compressed map from bytecode ins. to source line. */ 402 MRef uvinfo; /* Upvalue names. */ 403 MRef varinfo; /* Names and compressed extents of local variables. */ 404 } GCproto; 405 406 /* Flags for prototype. */ 407 #define PROTO_CHILD 0x01 /* Has child prototypes. */ 408 #define PROTO_VARARG 0x02 /* Vararg function. */ 409 #define PROTO_FFI 0x04 /* Uses BC_KCDATA for FFI datatypes. */ 410 #define PROTO_NOJIT 0x08 /* JIT disabled for this function. */ 411 #define PROTO_ILOOP 0x10 /* Patched bytecode with ILOOP etc. */ 412 /* Only used during parsing. */ 413 #define PROTO_HAS_RETURN 0x20 /* Already emitted a return. */ 414 #define PROTO_FIXUP_RETURN 0x40 /* Need to fixup emitted returns. */ 415 #define PROTO_LIFTED 0x80 /* This closure has been lifted. */ 416 /* Top bits used for counting created closures. */ 417 #define PROTO_CLCOUNT 0x20 /* Base of saturating 3 bit counter. */ 418 #define PROTO_CLC_BITS 3 419 #define PROTO_CLC_POLY (3*PROTO_CLCOUNT) /* Polymorphic threshold. */ 420 421 /* UV flags in bytecode/proto */ 422 #define PROTO_UV_CHAINED 0x8000 /* Chained upvalue. */ 423 #define PROTO_UV_IMMUTABLE 0x4000 /* Immutable upvalue. */ 424 #define PROTO_UV_ENV 0x2000 /* Refers to _ENV. */ 425 #define PROTO_UV_HOLE 0x6000 /* ENV+RO = hole in uv list. */ 426 #define PROTO_UV_CLOSURE 0xc000 /* CHAINED+RO = closure */ 427 #define PROTO_UV_MASK 0x1fff /* Can have 8k upvalues */ 428 #define PROTO_UV_SHIFT 13 429 430 /* For uv->flags */ 431 #define UV_CHAINED (PROTO_UV_CHAINED>>PROTO_UV_SHIFT) 432 #define UV_IMMUTABLE (PROTO_UV_IMMUTABLE>>PROTO_UV_SHIFT) 433 #define UV_ENV (PROTO_UV_ENV>>PROTO_UV_SHIFT) 434 #define UV_CLOSURE (PROTO_UV_CLOSURE>>PROTO_UV_SHIFT) 435 #define UV_HOLE (PROTO_UV_HOLE>>PROTO_UV_SHIFT) 436 #define UV_LOCAL 0 /* Always. */ 437 438 #define proto_kgc(pt, idx) \ 439 check_exp((uintptr_t)(intptr_t)(idx) >= (uintptr_t)-(intptr_t)(pt)->sizekgc, \ 440 gcref(mref((pt)->k, GCRef)[(idx)])) 441 #define proto_knumtv(pt, idx) \ 442 check_exp((uintptr_t)(idx) < (pt)->sizekn, &mref((pt)->k, TValue)[(idx)]) 443 #define proto_bc(pt) ((BCIns *)((char *)(pt) + sizeof(GCproto))) 444 #define proto_bcpos(pt, pc) ((BCPos)((pc) - proto_bc(pt))) 445 #define proto_uv(pt) (mref((pt)->uv, uint16_t)) 446 447 #define proto_chunkname(pt) (strref((pt)->chunkname)) 448 #define proto_chunknamestr(pt) (strdata(proto_chunkname((pt)))) 449 #define proto_lineinfo(pt) (mref((pt)->lineinfo, const void)) 450 #define proto_uvinfo(pt) (mref((pt)->uvinfo, const uint8_t)) 451 #define proto_varinfo(pt) (mref((pt)->varinfo, const uint8_t)) 452 453 /* -- Upvalue object ------------------------------------------------------ */ 454 455 typedef struct GCupval { 456 GCHeader; 457 uint8_t closed; /* Set if closed (i.e. uv->v == &uv->u.value). */ 458 uint8_t flags; /* Immutable value. */ 459 union { 460 TValue tv; /* If closed: the value itself. */ 461 struct { /* If open: double linked list, anchored at thread. */ 462 GCRef prev; 463 GCRef next; 464 }; 465 }; 466 MRef v; /* Points to stack slot (open) or above (closed). */ 467 uint32_t dhash; /* Disambiguation hash: dh1 != dh2 => cannot alias. */ 468 } GCupval; 469 470 #define uvprev(uv_) (&gcref((uv_)->prev)->uv) 471 #define uvnext(uv_) (&gcref((uv_)->next)->uv) 472 #define uvval(uv_) (mref((uv_)->v, TValue)) 473 474 /* -- Function object (closures) ------------------------------------------ */ 475 476 /* Common header for functions. env should be at same offset in GCudata. */ 477 #define GCfuncHeader \ 478 GCHeader; uint8_t ffid; uint8_t nupvalues; \ 479 GCRef env; GCRef gclist; MRef pc 480 481 typedef struct GCfuncC { 482 GCfuncHeader; 483 lua_CFunction f; /* C function to be called. */ 484 TValue upvalue[1]; /* Array of upvalues (TValue). */ 485 } GCfuncC; 486 487 typedef struct GCfuncL { 488 GCfuncHeader; 489 GCRef next_ENV, prev_ENV; /* Chain of closures we share _ENV with */ 490 GCRef uvptr[1]; /* Array of _pointers_ to upvalue objects (GCupval). */ 491 } GCfuncL; 492 493 typedef union GCfunc { 494 GCfuncC c; 495 GCfuncL l; 496 } GCfunc; 497 498 #define FF_LUA 0 499 #define FF_C 1 500 #define isluafunc(fn) ((fn)->c.ffid == FF_LUA) 501 #define iscfunc(fn) ((fn)->c.ffid == FF_C) 502 #define isffunc(fn) ((fn)->c.ffid > FF_C) 503 #define funcproto(fn) \ 504 check_exp(isluafunc(fn), (GCproto *)(mref((fn)->l.pc, char)-sizeof(GCproto))) 505 #define sizeCfunc(n) (sizeof(GCfuncC)-sizeof(TValue)+sizeof(TValue)*(n)) 506 #define sizeLfunc(n) (sizeof(GCfuncL)-sizeof(GCRef)+sizeof(GCRef)*(n)) 507 508 /* -- Table object -------------------------------------------------------- */ 509 510 /* Hash node. */ 511 typedef struct Node { 512 TValue val; /* Value object. Must be first field. */ 513 TValue key; /* Key object. */ 514 MRef next; /* Hash chain. */ 515 #if !LJ_GC64 516 MRef freetop; /* Top of free elements (stored in t->node[0]). */ 517 #endif 518 } Node; 519 520 LJ_STATIC_ASSERT(offsetof(Node, val) == 0); 521 522 typedef struct GCtab { 523 GCHeader; 524 uint8_t nomm; /* Negative cache for fast metamethods. */ 525 int8_t colo; /* Array colocation. */ 526 MRef array; /* Array part. Aliases env. */ 527 GCRef gclist; 528 GCRef metatable; /* Must be at same offset in GCudata. */ 529 MRef node; /* Hash part. */ 530 uint32_t asize; /* Size of array part (keys [0, asize-1]). */ 531 uint32_t hmask; /* Hash part mask (size of hash part - 1). */ 532 #if LJ_GC64 533 MRef freetop; /* Top of free elements. */ 534 #endif 535 } GCtab; 536 537 #define sizetabcolo(n) ((n)*sizeof(TValue) + sizeof(GCtab)) 538 #define tabref(r) (&gcref((r))->tab) 539 #define noderef(r) (mref((r), Node)) 540 #define nextnode(n) (mref((n)->next, Node)) 541 #if LJ_GC64 542 #define getfreetop(t, n) (noderef((t)->freetop)) 543 #define setfreetop(t, n, v) (setmref((t)->freetop, (v))) 544 #else 545 #define getfreetop(t, n) (noderef((n)->freetop)) 546 #define setfreetop(t, n, v) (setmref((n)->freetop, (v))) 547 #endif 548 549 /* -- State objects ------------------------------------------------------- */ 550 551 /* VM states. */ 552 enum { 553 LJ_VMST_INTERP, /* Interpreter. */ 554 LJ_VMST_C, /* C function. */ 555 LJ_VMST_GC, /* Garbage collector. */ 556 LJ_VMST_EXIT, /* Trace exit handler. */ 557 LJ_VMST_RECORD, /* Trace recorder. */ 558 LJ_VMST_OPT, /* Optimizer. */ 559 LJ_VMST_ASM, /* Assembler. */ 560 LJ_VMST__MAX 561 }; 562 563 #define setvmstate(g, st) ((g)->vmstate = ~LJ_VMST_##st) 564 565 /* Metamethods. ORDER MM */ 566 #ifdef LJ_HASFFI 567 #define MMDEF_FFI(_) _(new) 568 #else 569 #define MMDEF_FFI(_) 570 #endif 571 572 #if LJ_51 && !LJ_HASFFI 573 #define MMDEF_PAIRS(_) 574 #define MMDEF_PAIRS(_) 575 #define MM_pairs 255 576 #define MM_ipairs 255 577 #else 578 #define MMDEF_PAIRS(_) _(pairs) _(ipairs) 579 #endif 580 581 #if LJ_53 582 #define MMDEF_BITWISE(_) _(bnot) _(idiv) _(band) _(bor) _(bxor) _(shl) _(shr) 583 #else 584 #define MMDEF_BITWISE(_) 585 #define MM_bnot 255 586 #define MM_idiv 255 587 #define MM_band 255 588 #define MM_bor 255 589 #define MM_bxor 255 590 #define MM_shl 255 591 #define MM_shr 255 592 #endif 593 594 #define MMDEF(_) \ 595 _(index) _(newindex) _(gc) _(mode) _(eq) _(len) \ 596 /* Only the above (fast) metamethods are negative cached (max. 8). */ \ 597 _(lt) _(le) _(concat) _(call) \ 598 /* The following must be in ORDER ARITH. */ \ 599 _(add) _(sub) _(mul) _(div) _(mod) _(pow) _(unm) \ 600 MMDEF_BITWISE(_) \ 601 /* The following are used in the standard libraries. */ \ 602 _(metatable) _(tostring) MMDEF_FFI(_) MMDEF_PAIRS(_) 603 604 typedef enum { 605 #define MMENUM(name) MM_##name, 606 MMDEF(MMENUM) 607 #undef MMENUM 608 MM__MAX, 609 MM____ = MM__MAX, 610 MM_FAST = MM_len 611 } MMS; 612 613 /* GC root IDs. */ 614 typedef enum { 615 GCROOT_MMNAME, /* Metamethod names. */ 616 GCROOT_MMNAME_LAST = GCROOT_MMNAME + MM__MAX-1, 617 GCROOT_BASEMT, /* Metatables for base types. */ 618 GCROOT_BASEMT_NUM = GCROOT_BASEMT + ~LJ_TNUMX, 619 GCROOT_IO_INPUT, /* Userdata for default I/O input file. */ 620 GCROOT_IO_OUTPUT, /* Userdata for default I/O output file. */ 621 GCROOT_MAX 622 } GCRootID; 623 624 #define basemt_it(g, it) ((g)->gcroot[GCROOT_BASEMT+~(it)]) 625 #define basemt_obj(g, o) ((g)->gcroot[GCROOT_BASEMT+itypemap(o)]) 626 #define mmname_str(g, mm) (strref((g)->gcroot[GCROOT_MMNAME+(mm)])) 627 628 typedef struct GCState { 629 GCSize total; /* Memory currently allocated. */ 630 GCSize threshold; /* Memory threshold. */ 631 uint8_t currentwhite; /* Current white color. */ 632 uint8_t state; /* GC state. */ 633 uint8_t nocdatafin; /* No cdata finalizer called. */ 634 uint8_t unused2; 635 MSize sweepstr; /* Sweep position in string table. */ 636 GCRef root; /* List of all collectable objects. */ 637 MRef sweep; /* Sweep position in root list. */ 638 GCRef gray; /* List of gray objects. */ 639 GCRef grayagain; /* List of objects for atomic traversal. */ 640 GCRef weak; /* List of weak tables (to be cleared). */ 641 GCRef mmudata; /* List of userdata (to be finalized). */ 642 GCSize debt; /* Debt (how much GC is behind schedule). */ 643 GCSize estimate; /* Estimate of memory actually in use. */ 644 MSize stepmul; /* Incremental GC step granularity. */ 645 MSize pause; /* Pause between successive GC cycles. */ 646 } GCState; 647 648 /* Global state, shared by all threads of a Lua universe. */ 649 typedef struct global_State { 650 GCRef *strhash; /* String hash table (hash chain anchors). */ 651 MSize strmask; /* String hash mask (size of hash table - 1). */ 652 MSize strnum; /* Number of strings in hash table. */ 653 lua_Alloc allocf; /* Memory allocator. */ 654 void *allocd; /* Memory allocator data. */ 655 GCState gc; /* Garbage collector. */ 656 volatile int32_t vmstate; /* VM state or current JIT code trace number. */ 657 SBuf tmpbuf; /* Temporary string buffer. */ 658 GCstr strempty; /* Empty string. */ 659 uint8_t stremptyz; /* Zero terminator of empty string. */ 660 uint8_t hookmask; /* Hook mask. */ 661 uint8_t dispatchmode; /* Dispatch mode. */ 662 uint8_t vmevmask; /* VM event mask. */ 663 GCRef mainthref; /* Link to main thread. */ 664 TValue registrytv; /* Anchor for registry. */ 665 TValue tmptv, tmptv2; /* Temporary TValues. */ 666 Node nilnode; /* Fallback 1-element hash part (nil key and value). */ 667 GCupval uvhead; /* Head of double-linked list of all open upvalues. */ 668 int32_t hookcount; /* Instruction hook countdown. */ 669 int32_t hookcstart; /* Start count for instruction hook counter. */ 670 lua_Hook hookf; /* Hook function. */ 671 lua_CFunction wrapf; /* Wrapper for C function calls. */ 672 lua_CFunction panic; /* Called as a last resort for errors. */ 673 BCIns bc_cfunc_int; /* Bytecode for internal C function calls. */ 674 BCIns bc_cfunc_ext; /* Bytecode for external C function calls. */ 675 GCRef cur_L; /* Currently executing lua_State. */ 676 MRef jit_base; /* Current JIT code L->base or NULL. */ 677 MRef ctype_state; /* Pointer to C type state. */ 678 GCRef gcroot[GCROOT_MAX]; /* GC roots. */ 679 MatchState ms; /* Capture buffer for JIT mcode. */ 680 const void *cframe_limit; /* CPU stack overflows below this. */ 681 const lua_Number *version; 682 } global_State; 683 684 #define mainthread(g) (&gcref(g->mainthref)->th) 685 #define niltv(L) \ 686 check_exp(tvisnil(&G(L)->nilnode.val), &G(L)->nilnode.val) 687 #define niltvg(g) \ 688 check_exp(tvisnil(&(g)->nilnode.val), &(g)->nilnode.val) 689 690 /* Hook management. Hook event masks are defined in lua.h. */ 691 #define HOOK_EVENTMASK 0x0f 692 #define HOOK_ACTIVE 0x10 693 #define HOOK_ACTIVE_SHIFT 4 694 #define HOOK_VMEVENT 0x20 695 #define HOOK_GC 0x40 696 #define HOOK_PROFILE 0x80 697 #define hook_active(g) ((g)->hookmask & HOOK_ACTIVE) 698 #define hook_enter(g) ((g)->hookmask |= HOOK_ACTIVE) 699 #define hook_entergc(g) ((g)->hookmask |= (HOOK_ACTIVE|HOOK_GC)) 700 #define hook_vmevent(g) ((g)->hookmask |= (HOOK_ACTIVE|HOOK_VMEVENT)) 701 #define hook_leave(g) ((g)->hookmask &= ~HOOK_ACTIVE) 702 #define hook_save(g) ((g)->hookmask & ~HOOK_EVENTMASK) 703 #define hook_restore(g, h) \ 704 ((g)->hookmask = ((g)->hookmask & HOOK_EVENTMASK) | (h)) 705 706 /* Per-thread state object. */ 707 struct lua_State { 708 GCHeader; 709 uint8_t dummy_ffid; /* Fake FF_C for curr_funcisL() on dummy frames. */ 710 uint8_t status; /* Thread status. */ 711 MRef glref; /* Link to global state. */ 712 GCRef gclist; /* GC chain. */ 713 TValue *base; /* Base of currently executing function. */ 714 TValue *top; /* First free slot in the stack. */ 715 MRef maxstack; /* Last free slot in the stack. */ 716 MRef stack; /* Stack base. */ 717 GCRef openupval; /* List of open upvalues in the stack. */ 718 GCRef env; /* Thread environment (table of globals). */ 719 void *cframe; /* End of C stack frame chain. */ 720 MSize stacksize; /* True stack size (incl. LJ_STACK_EXTRA). */ 721 }; 722 723 #define G(L) (mref(L->glref, global_State)) 724 #define registry(L) (&G(L)->registrytv) 725 726 /* Macros to access the currently executing (Lua) function. */ 727 #if LJ_GC64 728 #define curr_func(L) (&gcval(L->base-2)->fn) 729 #elif LJ_FR2 730 #define curr_func(L) (&gcref((L->base-2)->gcr)->fn) 731 #else 732 #define curr_func(L) (&gcref((L->base-1)->fr.func)->fn) 733 #endif 734 #define curr_funcisL(L) (isluafunc(curr_func(L))) 735 #define curr_proto(L) (funcproto(curr_func(L))) 736 #define curr_topL(L) (L->base + curr_proto(L)->framesize) 737 #define curr_top(L) (curr_funcisL(L) ? curr_topL(L) : L->top) 738 739 /* -- GC object definition and conversions -------------------------------- */ 740 741 /* GC header for generic access to common fields of GC objects. */ 742 typedef struct GChead { 743 GCHeader; 744 uint8_t unused1; 745 uint8_t unused2; 746 GCRef env; 747 GCRef gclist; 748 GCRef metatable; 749 } GChead; 750 751 /* The env field SHOULD be at the same offset for all GC objects. */ 752 LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCfuncL, env)); 753 LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCudata, env)); 754 755 /* The metatable field MUST be at the same offset for all GC objects. */ 756 LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCtab, metatable)); 757 LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCudata, metatable)); 758 759 /* The gclist field MUST be at the same offset for all GC objects. */ 760 LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(lua_State, gclist)); 761 LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCproto, gclist)); 762 LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCfuncL, gclist)); 763 LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtab, gclist)); 764 765 typedef union GCobj { 766 GChead gch; 767 GCstr str; 768 GCupval uv; 769 lua_State th; 770 GCproto pt; 771 GCfunc fn; 772 GCcdata cd; 773 GCtab tab; 774 GCudata ud; 775 } GCobj; 776 777 /* Macros to convert a GCobj pointer into a specific value. */ 778 #define gco2str(o) check_exp((o)->gch.gct == ~LJ_TSTR, &(o)->str) 779 #define gco2uv(o) check_exp((o)->gch.gct == ~LJ_TUPVAL, &(o)->uv) 780 #define gco2th(o) check_exp((o)->gch.gct == ~LJ_TTHREAD, &(o)->th) 781 #define gco2pt(o) check_exp((o)->gch.gct == ~LJ_TPROTO, &(o)->pt) 782 #define gco2func(o) check_exp((o)->gch.gct == ~LJ_TFUNC, &(o)->fn) 783 #define gco2cd(o) check_exp((o)->gch.gct == ~LJ_TCDATA, &(o)->cd) 784 #define gco2tab(o) check_exp((o)->gch.gct == ~LJ_TTAB, &(o)->tab) 785 #define gco2ud(o) check_exp((o)->gch.gct == ~LJ_TUDATA, &(o)->ud) 786 787 /* Macro to convert any collectable object into a GCobj pointer. */ 788 #define obj2gco(v) ((GCobj *)(v)) 789 790 /* -- TValue getters/setters ---------------------------------------------- */ 791 792 #ifdef LUA_USE_ASSERT 793 #include "lj_gc.h" 794 #endif 795 796 /* Macros to test types. */ 797 #if LJ_GC64 798 #define itype(o) ((uint32_t)((o)->it64 >> 47)) 799 #define tvisnil(o) ((o)->it64 == -1) 800 #else 801 #define itype(o) ((o)->it) 802 #define tvisnil(o) (itype(o) == LJ_TNIL) 803 #endif 804 #define tvisfalse(o) (itype(o) == LJ_TFALSE) 805 #define tvistrue(o) (itype(o) == LJ_TTRUE) 806 #define tvisbool(o) (tvisfalse(o) || tvistrue(o)) 807 #if LJ_64 && !LJ_GC64 808 #define tvislightud(o) (((int32_t)itype(o) >> 15) == -2) 809 #else 810 #define tvislightud(o) (itype(o) == LJ_TLIGHTUD) 811 #endif 812 #define tvisstr(o) (itype(o) == LJ_TSTR) 813 #define tvisfunc(o) (itype(o) == LJ_TFUNC) 814 #define tvisthread(o) (itype(o) == LJ_TTHREAD) 815 #define tvisproto(o) (itype(o) == LJ_TPROTO) 816 #define tviscdata(o) (itype(o) == LJ_TCDATA) 817 #define tvistab(o) (itype(o) == LJ_TTAB) 818 #define tvisudata(o) (itype(o) == LJ_TUDATA) 819 #define tvisnumber(o) (itype(o) <= LJ_TISNUM) 820 #define tvisint(o) (LJ_DUALNUM && itype(o) == LJ_TISNUM) 821 #define tvisnum(o) (itype(o) < LJ_TISNUM) 822 823 #define tvistruecond(o) (itype(o) < LJ_TISTRUECOND) 824 #define tvispri(o) (itype(o) >= LJ_TISPRI) 825 #define tvistabud(o) (itype(o) <= LJ_TISTABUD) /* && !tvisnum() */ 826 #define tvisgcv(o) ((itype(o) - LJ_TISGCV) > (LJ_TNUMX - LJ_TISGCV)) 827 828 /* Special macros to test numbers for NaN, +0, -0, +1 and raw equality. */ 829 #define tvisnan(o) ((o)->n != (o)->n) 830 #if LJ_64 831 #define tviszero(o) (((o)->u64 << 1) == 0) 832 #else 833 #define tviszero(o) (((o)->u32.lo | ((o)->u32.hi << 1)) == 0) 834 #endif 835 #define tvispzero(o) ((o)->u64 == 0) 836 #define tvismzero(o) ((o)->u64 == U64x(80000000,00000000)) 837 #define tvispone(o) ((o)->u64 == U64x(3ff00000,00000000)) 838 #define rawnumequal(o1, o2) ((o1)->u64 == (o2)->u64) 839 840 /* Macros to convert type ids. */ 841 #if LJ_64 && !LJ_GC64 842 #define itypemap(o) \ 843 (tvisnumber(o) ? ~LJ_TNUMX : tvislightud(o) ? ~LJ_TLIGHTUD : ~itype(o)) 844 #else 845 #define itypemap(o) (tvisnumber(o) ? ~LJ_TNUMX : ~itype(o)) 846 #endif 847 848 /* Macros to get tagged values. */ 849 #if LJ_GC64 850 #define gcval(o) ((GCobj *)(gcrefu((o)->gcr) & LJ_GCVMASK)) 851 #else 852 #define gcval(o) (gcref((o)->gcr)) 853 #endif 854 #define boolV(o) check_exp(tvisbool(o), (LJ_TFALSE - itype(o))) 855 #if LJ_64 856 #define lightudV(o) \ 857 check_exp(tvislightud(o), (void *)((o)->u64 & U64x(00007fff,ffffffff))) 858 #else 859 #define lightudV(o) check_exp(tvislightud(o), gcrefp((o)->gcr, void)) 860 #endif 861 #define gcV(o) check_exp(tvisgcv(o), gcval(o)) 862 #define strV(o) check_exp(tvisstr(o), &gcval(o)->str) 863 #define funcV(o) check_exp(tvisfunc(o), &gcval(o)->fn) 864 #define threadV(o) check_exp(tvisthread(o), &gcval(o)->th) 865 #define protoV(o) check_exp(tvisproto(o), &gcval(o)->pt) 866 #define cdataV(o) check_exp(tviscdata(o), &gcval(o)->cd) 867 #define tabV(o) check_exp(tvistab(o), &gcval(o)->tab) 868 #define udataV(o) check_exp(tvisudata(o), &gcval(o)->ud) 869 #define numV(o) check_exp(tvisnum(o), (o)->n) 870 #define intV(o) check_exp(tvisint(o), (int32_t)(o)->i) 871 872 /* Macros to set tagged values. */ 873 #if LJ_GC64 874 #define setitype(o, i) ((o)->it = ((i) << 15)) 875 #define setnilV(o) ((o)->it64 = -1) 876 #define setpriV(o, x) ((o)->it64 = (int64_t)~((uint64_t)~(x)<<47)) 877 #define setboolV(o, x) ((o)->it64 = (int64_t)~((uint64_t)((x)+1)<<47)) 878 #else 879 #define setitype(o, i) ((o)->it = (i)) 880 #define setnilV(o) ((o)->it = LJ_TNIL) 881 #define setboolV(o, x) ((o)->it = LJ_TFALSE-(uint32_t)(x)) 882 #define setpriV(o, i) (setitype((o), (i))) 883 #endif 884 885 static LJ_AINLINE void setlightudV(TValue *o, void *p) 886 { 887 #if LJ_GC64 888 o->u64 = (uint64_t)p | (((uint64_t)LJ_TLIGHTUD) << 47); 889 #elif LJ_64 890 o->u64 = (uint64_t)p | (((uint64_t)0xffff) << 48); 891 #else 892 setgcrefp(o->gcr, p); setitype(o, LJ_TLIGHTUD); 893 #endif 894 } 895 896 #if LJ_64 897 #define checklightudptr(L, p) \ 898 (((uint64_t)(p) >> 47) ? (lj_err_msg(L, LJ_ERR_BADLU), NULL) : (p)) 899 #else 900 #define checklightudptr(L, p) (p) 901 #endif 902 903 #if LJ_FR2 904 #define contptr(f) ((void *)(f)) 905 #define setcont(o, f) ((o)->u64 = (uint64_t)(uintptr_t)contptr(f)) 906 #elif LJ_64 907 #define contptr(f) \ 908 ((void *)(uintptr_t)(uint32_t)((intptr_t)(f) - (intptr_t)lj_vm_asm_begin)) 909 #define setcont(o, f) \ 910 ((o)->u64 = (uint64_t)(void *)(f) - (uint64_t)lj_vm_asm_begin) 911 #else 912 #define contptr(f) ((void *)(f)) 913 #define setcont(o, f) setlightudV((o), contptr(f)) 914 #endif 915 916 #define tvchecklive(L, o) \ 917 UNUSED(L), lua_assert(!tvisgcv(o) || \ 918 ((~itype(o) == gcval(o)->gch.gct) && !isdead(G(L), gcval(o)))) 919 920 static LJ_AINLINE void setgcVraw(TValue *o, GCobj *v, uint32_t itype) 921 { 922 #if LJ_GC64 923 setgcreft(o->gcr, v, itype); 924 #else 925 setgcref(o->gcr, v); setitype(o, itype); 926 #endif 927 } 928 929 static LJ_AINLINE void setgcV(lua_State *L, TValue *o, GCobj *v, uint32_t it) 930 { 931 setgcVraw(o, v, it); tvchecklive(L, o); 932 } 933 934 #define setuservalue(L,u,o) \ 935 { setgcref((u)->env, gcV(o)); (u)->envtt = ~itype(o); tvchecklive(L, (o)); } 936 937 #define getuservalue(L,u,o) \ 938 setgcV(L, (o), gcref((u)->env), ~(u)->envtt) 939 940 941 #define define_setV(name, type, tag) \ 942 static LJ_AINLINE void name(lua_State *L, TValue *o, type *v) \ 943 { \ 944 setgcV(L, o, obj2gco(v), tag); \ 945 } 946 define_setV(setstrV, GCstr, LJ_TSTR) 947 define_setV(setthreadV, lua_State, LJ_TTHREAD) 948 define_setV(setprotoV, GCproto, LJ_TPROTO) 949 define_setV(setfuncV, GCfunc, LJ_TFUNC) 950 define_setV(setcdataV, GCcdata, LJ_TCDATA) 951 define_setV(settabV, GCtab, LJ_TTAB) 952 define_setV(setudataV, GCudata, LJ_TUDATA) 953 954 #define setnumV(o, x) ((o)->n = (x)) 955 #define setnanV(o) ((o)->u64 = U64x(fff80000,00000000)) 956 #define setpinfV(o) ((o)->u64 = U64x(7ff00000,00000000)) 957 #define setminfV(o) ((o)->u64 = U64x(fff00000,00000000)) 958 959 static LJ_AINLINE void setintV(TValue *o, int32_t i) 960 { 961 #if LJ_DUALNUM 962 o->i = (uint32_t)i; setitype(o, LJ_TISNUM); 963 #else 964 o->n = (lua_Number)i; 965 #endif 966 } 967 968 static LJ_AINLINE void setint64V(TValue *o, int64_t i) 969 { 970 if (LJ_DUALNUM && LJ_LIKELY(i == (int64_t)(int32_t)i)) 971 setintV(o, (int32_t)i); 972 else 973 setnumV(o, (lua_Number)i); 974 } 975 976 #if LJ_64 977 #define setintptrV(o, i) setint64V((o), (i)) 978 #else 979 #define setintptrV(o, i) setintV((o), (i)) 980 #endif 981 982 /* Copy tagged values. */ 983 static LJ_AINLINE void copyTV(lua_State *L, TValue *o1, const TValue *o2) 984 { 985 *o1 = *o2; tvchecklive(L, o1); 986 } 987 988 /* -- Number to integer conversion ---------------------------------------- */ 989 990 #if LJ_SOFTFP 991 LJ_ASMF int32_t lj_vm_tobit(double x); 992 #endif 993 994 static LJ_AINLINE int32_t lj_num2bit(lua_Number n) 995 { 996 #if LJ_SOFTFP 997 return lj_vm_tobit(n); 998 #else 999 TValue o; 1000 o.n = n + 6755399441055744.0; /* 2^52 + 2^51 */ 1001 return (int32_t)o.u32.lo; 1002 #endif 1003 } 1004 1005 #define lj_num2int(n) ((int32_t)(n)) 1006 1007 static LJ_AINLINE uint64_t lj_num2u64(lua_Number n) 1008 { 1009 #ifdef _MSC_VER 1010 if (n >= 9223372036854775808.0) /* They think it's a feature. */ 1011 return (uint64_t)(int64_t)(n - 18446744073709551616.0); 1012 else 1013 #endif 1014 return (uint64_t)n; 1015 } 1016 static LJ_AINLINE LUA_UNSIGNED lj_num2u(lua_Number n) 1017 { 1018 #ifdef _MSC_VER 1019 if (n >= 9223372036854775808.0) /* They think it's a feature. */ 1020 return (uint64_t)(int64_t)(n - 18446744073709551616.0); 1021 else 1022 #endif 1023 return (LUA_UNSIGNED)n; 1024 } 1025 1026 static LJ_AINLINE int32_t numberVint(cTValue *o) 1027 { 1028 if (LJ_LIKELY(tvisint(o))) 1029 return intV(o); 1030 else 1031 return lj_num2int(numV(o)); 1032 } 1033 1034 static LJ_AINLINE lua_Number numberVnum(cTValue *o) 1035 { 1036 if (LJ_UNLIKELY(tvisint(o))) 1037 return (lua_Number)intV(o); 1038 else 1039 return numV(o); 1040 } 1041 1042 /* -- Miscellaneous object handling --------------------------------------- */ 1043 1044 /* Names and maps for internal and external object tags. */ 1045 LJ_DATA const char *const lj_obj_typename[1+LUA_TCDATA+1]; 1046 LJ_DATA const char *const lj_obj_itypename[~LJ_TNUMX+1]; 1047 1048 #define lj_typename(o) (lj_obj_itypename[itypemap(o)]) 1049 1050 /* Compare two objects without calling metamethods. */ 1051 LJ_FUNC int LJ_FASTCALL lj_obj_equal(cTValue *o1, cTValue *o2); 1052 LJ_FUNC const void * LJ_FASTCALL lj_obj_ptr(cTValue *o); 1053 1054 #endif