• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tarantool / luajit / 5784751762

07 Aug 2023 11:54AM UTC coverage: 84.282% (-3.1%) from 87.373%
5784751762

push

github

ligurio
setup-gcovr [TO SQUASH]

5157 of 6002 branches covered (85.92%)

Branch coverage included in aggregate %.

19632 of 23410 relevant lines covered (83.86%)

229380.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.72
/src/lj_obj.h
1
/*
2
** LuaJIT VM tags, values and objects.
3
** Copyright (C) 2005-2017 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_GC64
21
typedef uint64_t GCSize;
22
#else
23
typedef uint32_t GCSize;
24
#endif
25

26
/* Memory reference */
27
typedef struct MRef {
28
#if LJ_GC64
29
  uint64_t ptr64;        /* True 64 bit pointer. */
30
#else
31
  uint32_t ptr32;        /* Pseudo 32 bit pointer. */
32
#endif
33
} MRef;
34

35
#if LJ_GC64
36
#define mref(r, t)        ((t *)(void *)(r).ptr64)
37

38
#define setmref(r, p)        ((r).ptr64 = (uint64_t)(void *)(p))
39
#define setmrefr(r, v)        ((r).ptr64 = (v).ptr64)
40
#else
41
#define mref(r, t)        ((t *)(void *)(uintptr_t)(r).ptr32)
42

43
#define setmref(r, p)        ((r).ptr32 = (uint32_t)(uintptr_t)(void *)(p))
44
#define setmrefr(r, v)        ((r).ptr32 = (v).ptr32)
45
#endif
46

47
/* -- GC object references (32 bit address space) ------------------------- */
48

49
/* GCobj reference */
50
typedef struct GCRef {
51
#if LJ_GC64
52
  uint64_t gcptr64;        /* True 64 bit pointer. */
53
#else
54
  uint32_t gcptr32;        /* Pseudo 32 bit pointer. */
55
#endif
56
} GCRef;
57

58
/* Common GC header for all collectable objects. */
59
#define GCHeader        GCRef nextgc; uint8_t marked; uint8_t gct
60
/* This occupies 6 bytes, so use the next 2 bytes for non-32 bit fields. */
61

62
#if LJ_GC64
63
#define gcref(r)        ((GCobj *)(r).gcptr64)
64
#define gcrefp(r, t)        ((t *)(void *)(r).gcptr64)
65
#define gcrefu(r)        ((r).gcptr64)
66
#define gcrefeq(r1, r2)        ((r1).gcptr64 == (r2).gcptr64)
67

68
#define setgcref(r, gc)        ((r).gcptr64 = (uint64_t)&(gc)->gch)
69
#define setgcreft(r, gc, it) \
70
  (r).gcptr64 = (uint64_t)&(gc)->gch | (((uint64_t)(it)) << 47)
71
#define setgcrefp(r, p)        ((r).gcptr64 = (uint64_t)(p))
72
#define setgcrefnull(r)        ((r).gcptr64 = 0)
73
#define setgcrefr(r, v)        ((r).gcptr64 = (v).gcptr64)
74
#else
75
#define gcref(r)        ((GCobj *)(uintptr_t)(r).gcptr32)
76
#define gcrefp(r, t)        ((t *)(void *)(uintptr_t)(r).gcptr32)
77
#define gcrefu(r)        ((r).gcptr32)
78
#define gcrefeq(r1, r2)        ((r1).gcptr32 == (r2).gcptr32)
79

80
#define setgcref(r, gc)        ((r).gcptr32 = (uint32_t)(uintptr_t)&(gc)->gch)
81
#define setgcrefp(r, p)        ((r).gcptr32 = (uint32_t)(uintptr_t)(p))
82
#define setgcrefnull(r)        ((r).gcptr32 = 0)
83
#define setgcrefr(r, v)        ((r).gcptr32 = (v).gcptr32)
84
#endif
85

86
#define gcnext(gc)        (gcref((gc)->gch.nextgc))
87

88
/* IMPORTANT NOTE:
89
**
90
** All uses of the setgcref* macros MUST be accompanied with a write barrier.
91
**
92
** This is to ensure the integrity of the incremental GC. The invariant
93
** to preserve is that a black object never points to a white object.
94
** I.e. never store a white object into a field of a black object.
95
**
96
** It's ok to LEAVE OUT the write barrier ONLY in the following cases:
97
** - The source is not a GC object (NULL).
98
** - The target is a GC root. I.e. everything in global_State.
99
** - The target is a lua_State field (threads are never black).
100
** - The target is a stack slot, see setgcV et al.
101
** - The target is an open upvalue, i.e. pointing to a stack slot.
102
** - The target is a newly created object (i.e. marked white). But make
103
**   sure nothing invokes the GC inbetween.
104
** - The target and the source are the same object (self-reference).
105
** - The target already contains the object (e.g. moving elements around).
106
**
107
** The most common case is a store to a stack slot. All other cases where
108
** a barrier has been omitted are annotated with a NOBARRIER comment.
109
**
110
** The same logic applies for stores to table slots (array part or hash
111
** part). ALL uses of lj_tab_set* require a barrier for the stored value
112
** *and* the stored key, based on the above rules. In practice this means
113
** a barrier is needed if *either* of the key or value are a GC object.
114
**
115
** It's ok to LEAVE OUT the write barrier in the following special cases:
116
** - The stored value is nil. The key doesn't matter because it's either
117
**   not resurrected or lj_tab_newkey() will take care of the key barrier.
118
** - The key doesn't matter if the *previously* stored value is guaranteed
119
**   to be non-nil (because the key is kept alive in the table).
120
** - The key doesn't matter if it's guaranteed not to be part of the table,
121
**   since lj_tab_newkey() takes care of the key barrier. This applies
122
**   trivially to new tables, but watch out for resurrected keys. Storing
123
**   a nil value leaves the key in the table!
124
**
125
** In case of doubt use lj_gc_anybarriert() as it's rather cheap. It's used
126
** by the interpreter for all table stores.
127
**
128
** Note: In contrast to Lua's GC, LuaJIT's GC does *not* specially mark
129
** dead keys in tables. The reference is left in, but it's guaranteed to
130
** be never dereferenced as long as the value is nil. It's ok if the key is
131
** freed or if any object subsequently gets the same address.
132
**
133
** Not destroying dead keys helps to keep key hash slots stable. This avoids
134
** specialization back-off for HREFK when a value flips between nil and
135
** non-nil and the GC gets in the way. It also allows safely hoisting
136
** HREF/HREFK across GC steps. Dead keys are only removed if a table is
137
** resized (i.e. by NEWREF) and xREF must not be CSEd across a resize.
138
**
139
** The trade-off is that a write barrier for tables must take the key into
140
** account, too. Implicitly resurrecting the key by storing a non-nil value
141
** may invalidate the incremental GC invariant.
142
*/
143

144
/* -- Common type definitions --------------------------------------------- */
145

146
/* Types for handling bytecodes. Need this here, details in lj_bc.h. */
147
typedef uint32_t BCIns;  /* Bytecode instruction. */
148
typedef uint32_t BCPos;  /* Bytecode position. */
149
typedef uint32_t BCReg;  /* Bytecode register. */
150
typedef int32_t BCLine;  /* Bytecode line number. */
151

152
/* Internal assembler functions. Never call these directly from C. */
153
typedef void (*ASMFunction)(void);
154

155
/* Resizable string buffer. Need this here, details in lj_buf.h. */
156
typedef struct SBuf {
157
  MRef p;                /* String buffer pointer. */
158
  MRef e;                /* String buffer end pointer. */
159
  MRef b;                /* String buffer base. */
160
  MRef L;                /* lua_State, used for buffer resizing. */
161
} SBuf;
162

163
/* -- Tags and values ----------------------------------------------------- */
164

165
/* Frame link. */
166
typedef union {
167
  int32_t ftsz;                /* Frame type and size of previous frame. */
168
  MRef pcr;                /* Or PC for Lua frames. */
169
} FrameLink;
170

171
/* Tagged value. */
172
typedef LJ_ALIGN(8) union TValue {
173
  uint64_t u64;                /* 64 bit pattern overlaps number. */
174
  lua_Number n;                /* Number object overlaps split tag/value object. */
175
#if LJ_GC64
176
  GCRef gcr;                /* GCobj reference with tag. */
177
  int64_t it64;
178
  struct {
179
    LJ_ENDIAN_LOHI(
180
      int32_t i;        /* Integer value. */
181
    , uint32_t it;        /* Internal object tag. Must overlap MSW of number. */
182
    )
183
  };
184
#else
185
  struct {
186
    LJ_ENDIAN_LOHI(
187
      union {
188
        GCRef gcr;        /* GCobj reference (if any). */
189
        int32_t i;        /* Integer value. */
190
      };
191
    , uint32_t it;        /* Internal object tag. Must overlap MSW of number. */
192
    )
193
  };
194
#endif
195
#if LJ_FR2
196
  int64_t ftsz;                /* Frame type and size of previous frame, or PC. */
197
#else
198
  struct {
199
    LJ_ENDIAN_LOHI(
200
      GCRef func;        /* Function for next frame (or dummy L). */
201
    , FrameLink tp;        /* Link to previous frame. */
202
    )
203
  } fr;
204
#endif
205
  struct {
206
    LJ_ENDIAN_LOHI(
207
      uint32_t lo;        /* Lower 32 bits of number. */
208
    , uint32_t hi;        /* Upper 32 bits of number. */
209
    )
210
  } u32;
211
} TValue;
212

213
typedef const TValue cTValue;
214

215
#define tvref(r)        (mref(r, TValue))
216

217
/* More external and GCobj tags for internal objects. */
218
#define LAST_TT                LUA_TTHREAD
219
#define LUA_TPROTO        (LAST_TT+1)
220
#define LUA_TCDATA        (LAST_TT+2)
221

222
/* Internal object tags.
223
**
224
** Format for 32 bit GC references (!LJ_GC64):
225
**
226
** Internal tags overlap the MSW of a number object (must be a double).
227
** Interpreted as a double these are special NaNs. The FPU only generates
228
** one type of NaN (0xfff8_0000_0000_0000). So MSWs > 0xfff80000 are available
229
** for use as internal tags. Small negative numbers are used to shorten the
230
** encoding of type comparisons (reg/mem against sign-ext. 8 bit immediate).
231
**
232
**                  ---MSW---.---LSW---
233
** primitive types |  itype  |         |
234
** lightuserdata   |  itype  |  void * |  (32 bit platforms)
235
** lightuserdata   |ffff|seg|    ofs   |  (64 bit platforms)
236
** GC objects      |  itype  |  GCRef  |
237
** int (LJ_DUALNUM)|  itype  |   int   |
238
** number           -------double------
239
**
240
** Format for 64 bit GC references (LJ_GC64):
241
**
242
** The upper 13 bits must be 1 (0xfff8...) for a special NaN. The next
243
** 4 bits hold the internal tag. The lowest 47 bits either hold a pointer,
244
** a zero-extended 32 bit integer or all bits set to 1 for primitive types.
245
**
246
**                     ------MSW------.------LSW------
247
** primitive types    |1..1|itype|1..................1|
248
** GC objects         |1..1|itype|-------GCRef--------|
249
** lightuserdata      |1..1|itype|seg|------ofs-------|
250
** int (LJ_DUALNUM)   |1..1|itype|0..0|-----int-------|
251
** number              ------------double-------------
252
**
253
** ORDER LJ_T
254
** Primitive types nil/false/true must be first, lightuserdata next.
255
** GC objects are at the end, table/userdata must be lowest.
256
** Also check lj_ir.h for similar ordering constraints.
257
*/
258
#define LJ_TNIL                        (~0u)
259
#define LJ_TFALSE                (~1u)
260
#define LJ_TTRUE                (~2u)
261
#define LJ_TLIGHTUD                (~3u)
262
#define LJ_TSTR                        (~4u)
263
#define LJ_TUPVAL                (~5u)
264
#define LJ_TTHREAD                (~6u)
265
#define LJ_TPROTO                (~7u)
266
#define LJ_TFUNC                (~8u)
267
#define LJ_TTRACE                (~9u)
268
#define LJ_TCDATA                (~10u)
269
#define LJ_TTAB                        (~11u)
270
#define LJ_TUDATA                (~12u)
271
/* This is just the canonical number type used in some places. */
272
#define LJ_TNUMX                (~13u)
273

274
/* Integers have itype == LJ_TISNUM doubles have itype < LJ_TISNUM */
275
#if LJ_64 && !LJ_GC64
276
#define LJ_TISNUM                0xfffeffffu
277
#else
278
#define LJ_TISNUM                LJ_TNUMX
279
#endif
280
#define LJ_TISTRUECOND                LJ_TFALSE
281
#define LJ_TISPRI                LJ_TTRUE
282
#define LJ_TISGCV                (LJ_TSTR+1)
283
#define LJ_TISTABUD                LJ_TTAB
284

285
#if LJ_GC64
286
#define LJ_GCVMASK                (((uint64_t)1 << 47) - 1)
287
#endif
288

289
#if LJ_64
290
/* To stay within 47 bits, lightuserdata is segmented. */
291
#define LJ_LIGHTUD_BITS_SEG        8
292
#define LJ_LIGHTUD_BITS_LO        (47 - LJ_LIGHTUD_BITS_SEG)
293
#endif
294

295
/* -- String object ------------------------------------------------------- */
296

297
/* String object header. String payload follows. */
298
typedef struct GCstr {
299
  GCHeader;
300
  uint8_t reserved;        /* Used by lexer for fast lookup of reserved words. */
301
  uint8_t strflags;        /* If LUAJIT_SMART_STRINGS: hash function used(+). */
302
  MSize hash;                /* Hash of string. */
303
  MSize len;                /* Size of string. */
304
} GCstr;
305

306
#define strref(r)        (&gcref((r))->str)
307
#define strdata(s)        ((const char *)((s)+1))
308
#define strdatawr(s)        ((char *)((s)+1))
309
#define strVdata(o)        strdata(strV(o))
310
#define sizestring(s)        (sizeof(struct GCstr)+(s)->len+1)
311
#define strsmart(s)        ((s)->strflags >= 0xc0)
312

313
/* -- Userdata object ----------------------------------------------------- */
314

315
/* Userdata object. Payload follows. */
316
typedef struct GCudata {
317
  GCHeader;
318
  uint8_t udtype;        /* Userdata type. */
319
  uint8_t unused2;
320
  GCRef env;                /* Should be at same offset in GCfunc. */
321
  MSize len;                /* Size of payload. */
322
  GCRef metatable;        /* Must be at same offset in GCtab. */
323
  uint32_t align1;        /* To force 8 byte alignment of the payload. */
324
} GCudata;
325

326
/* Userdata types. */
327
enum {
328
  UDTYPE_USERDATA,        /* Regular userdata. */
329
  UDTYPE_IO_FILE,        /* I/O library FILE. */
330
  UDTYPE_FFI_CLIB,        /* FFI C library namespace. */
331
  UDTYPE__MAX
332
};
333

334
#define uddata(u)        ((void *)((u)+1))
335
#define sizeudata(u)        (sizeof(struct GCudata)+(u)->len)
336

337
/* -- C data object ------------------------------------------------------- */
338

339
/* C data object. Payload follows. */
340
typedef struct GCcdata {
341
  GCHeader;
342
  uint16_t ctypeid;        /* C type ID. */
343
} GCcdata;
344

345
/* Prepended to variable-sized or realigned C data objects. */
346
typedef struct GCcdataVar {
347
  uint16_t offset;        /* Offset to allocated memory (relative to GCcdata). */
348
  uint16_t extra;        /* Extra space allocated (incl. GCcdata + GCcdatav). */
349
  MSize len;                /* Size of payload. */
350
} GCcdataVar;
351

352
#define cdataptr(cd)        ((void *)((cd)+1))
353
#define cdataisv(cd)        ((cd)->marked & 0x80)
354
#define cdatav(cd)        ((GCcdataVar *)((char *)(cd) - sizeof(GCcdataVar)))
355
#define cdatavlen(cd)        check_exp(cdataisv(cd), cdatav(cd)->len)
356
#define sizecdatav(cd)        (cdatavlen(cd) + cdatav(cd)->extra)
357
#define memcdatav(cd)        ((void *)((char *)(cd) - cdatav(cd)->offset))
358

359
/* -- Prototype object ---------------------------------------------------- */
360

361
#define SCALE_NUM_GCO        ((int32_t)sizeof(lua_Number)/sizeof(GCRef))
362
#define round_nkgc(n)        (((n) + SCALE_NUM_GCO-1) & ~(SCALE_NUM_GCO-1))
363

364
typedef struct GCproto {
365
  GCHeader;
366
  uint8_t numparams;        /* Number of parameters. */
367
  uint8_t framesize;        /* Fixed frame size. */
368
  MSize sizebc;                /* Number of bytecode instructions. */
369
#if LJ_GC64
370
  uint32_t unused_gc64;
371
#endif
372
  GCRef gclist;
373
  MRef k;                /* Split constant array (points to the middle). */
374
  MRef uv;                /* Upvalue list. local slot|0x8000 or parent uv idx. */
375
  MSize sizekgc;        /* Number of collectable constants. */
376
  MSize sizekn;                /* Number of lua_Number constants. */
377
  MSize sizept;                /* Total size including colocated arrays. */
378
  uint8_t sizeuv;        /* Number of upvalues. */
379
  uint8_t flags;        /* Miscellaneous flags (see below). */
380
  uint16_t trace;        /* Anchor for chain of root traces. */
381
  /* ------ The following fields are for debugging/tracebacks only ------ */
382
  GCRef chunkname;        /* Name of the chunk this function was defined in. */
383
  BCLine firstline;        /* First line of the function definition. */
384
  BCLine numline;        /* Number of lines for the function definition. */
385
  MRef lineinfo;        /* Compressed map from bytecode ins. to source line. */
386
  MRef uvinfo;                /* Upvalue names. */
387
  MRef varinfo;                /* Names and compressed extents of local variables. */
388
} GCproto;
389

390
/* Flags for prototype. */
391
#define PROTO_CHILD                0x01        /* Has child prototypes. */
392
#define PROTO_VARARG                0x02        /* Vararg function. */
393
#define PROTO_FFI                0x04        /* Uses BC_KCDATA for FFI datatypes. */
394
#define PROTO_NOJIT                0x08        /* JIT disabled for this function. */
395
#define PROTO_ILOOP                0x10        /* Patched bytecode with ILOOP etc. */
396
/* Only used during parsing. */
397
#define PROTO_HAS_RETURN        0x20        /* Already emitted a return. */
398
#define PROTO_FIXUP_RETURN        0x40        /* Need to fixup emitted returns. */
399
/* Top bits used for counting created closures. */
400
#define PROTO_CLCOUNT                0x20        /* Base of saturating 3 bit counter. */
401
#define PROTO_CLC_BITS                3
402
#define PROTO_CLC_POLY                (3*PROTO_CLCOUNT)  /* Polymorphic threshold. */
403

404
#define PROTO_UV_LOCAL                0x8000        /* Upvalue for local slot. */
405
#define PROTO_UV_IMMUTABLE        0x4000        /* Immutable upvalue. */
406

407
#define proto_kgc(pt, idx) \
408
  check_exp((uintptr_t)(intptr_t)(idx) >= (uintptr_t)-(intptr_t)(pt)->sizekgc, \
409
            gcref(mref((pt)->k, GCRef)[(idx)]))
410
#define proto_knumtv(pt, idx) \
411
  check_exp((uintptr_t)(idx) < (pt)->sizekn, &mref((pt)->k, TValue)[(idx)])
412
#define proto_bc(pt)                ((BCIns *)((char *)(pt) + sizeof(GCproto)))
413
#define proto_bcpos(pt, pc)        ((BCPos)((pc) - proto_bc(pt)))
414
#define proto_uv(pt)                (mref((pt)->uv, uint16_t))
415

416
#define proto_chunkname(pt)        (strref((pt)->chunkname))
417
#define proto_chunknamestr(pt)        (strdata(proto_chunkname((pt))))
418
#define proto_lineinfo(pt)        (mref((pt)->lineinfo, const void))
419
#define proto_uvinfo(pt)        (mref((pt)->uvinfo, const uint8_t))
420
#define proto_varinfo(pt)        (mref((pt)->varinfo, const uint8_t))
421

422
/* -- Upvalue object ------------------------------------------------------ */
423

424
typedef struct GCupval {
425
  GCHeader;
426
  uint8_t closed;        /* Set if closed (i.e. uv->v == &uv->u.value). */
427
  uint8_t immutable;        /* Immutable value. */
428
  union {
429
    TValue tv;                /* If closed: the value itself. */
430
    struct {                /* If open: double linked list, anchored at thread. */
431
      GCRef prev;
432
      GCRef next;
433
    };
434
  };
435
  MRef v;                /* Points to stack slot (open) or above (closed). */
436
  uint32_t dhash;        /* Disambiguation hash: dh1 != dh2 => cannot alias. */
437
} GCupval;
438

439
#define uvprev(uv_)        (&gcref((uv_)->prev)->uv)
440
#define uvnext(uv_)        (&gcref((uv_)->next)->uv)
441
#define uvval(uv_)        (mref((uv_)->v, TValue))
442

443
/* -- Function object (closures) ------------------------------------------ */
444

445
/* Common header for functions. env should be at same offset in GCudata. */
446
#define GCfuncHeader \
447
  GCHeader; uint8_t ffid; uint8_t nupvalues; \
448
  GCRef env; GCRef gclist; MRef pc
449

450
typedef struct GCfuncC {
451
  GCfuncHeader;
452
  lua_CFunction f;        /* C function to be called. */
453
  TValue upvalue[1];        /* Array of upvalues (TValue). */
454
} GCfuncC;
455

456
typedef struct GCfuncL {
457
  GCfuncHeader;
458
  GCRef uvptr[1];        /* Array of _pointers_ to upvalue objects (GCupval). */
459
} GCfuncL;
460

461
typedef union GCfunc {
462
  GCfuncC c;
463
  GCfuncL l;
464
} GCfunc;
465

466
#define FF_LUA                0
467
#define FF_C                1
468
#define isluafunc(fn)        ((fn)->c.ffid == FF_LUA)
469
#define iscfunc(fn)        ((fn)->c.ffid == FF_C)
470
#define isffunc(fn)        ((fn)->c.ffid > FF_C)
471
#define funcproto(fn) \
472
  check_exp(isluafunc(fn), (GCproto *)(mref((fn)->l.pc, char)-sizeof(GCproto)))
473
#define sizeCfunc(n)        (sizeof(GCfuncC)-sizeof(TValue)+sizeof(TValue)*(n))
474
#define sizeLfunc(n)        (sizeof(GCfuncL)-sizeof(GCRef)+sizeof(GCRef)*(n))
475

476
/* -- Table object -------------------------------------------------------- */
477

478
/* Hash node. */
479
typedef struct Node {
480
  TValue val;                /* Value object. Must be first field. */
481
  TValue key;                /* Key object. */
482
  MRef next;                /* Hash chain. */
483
#if !LJ_GC64
484
  MRef freetop;                /* Top of free elements (stored in t->node[0]). */
485
#endif
486
} Node;
487

488
LJ_STATIC_ASSERT(offsetof(Node, val) == 0);
489

490
typedef struct GCtab {
491
  GCHeader;
492
  uint8_t nomm;                /* Negative cache for fast metamethods. */
493
  int8_t colo;                /* Array colocation. */
494
  MRef array;                /* Array part. */
495
  GCRef gclist;
496
  GCRef metatable;        /* Must be at same offset in GCudata. */
497
  MRef node;                /* Hash part. */
498
  uint32_t asize;        /* Size of array part (keys [0, asize-1]). */
499
  uint32_t hmask;        /* Hash part mask (size of hash part - 1). */
500
#if LJ_GC64
501
  MRef freetop;                /* Top of free elements. */
502
#endif
503
} GCtab;
504

505
#define sizetabcolo(n)        ((n)*sizeof(TValue) + sizeof(GCtab))
506
#define tabref(r)        (&gcref((r))->tab)
507
#define noderef(r)        (mref((r), Node))
508
#define nextnode(n)        (mref((n)->next, Node))
509
#if LJ_GC64
510
#define getfreetop(t, n)        (noderef((t)->freetop))
511
#define setfreetop(t, n, v)        (setmref((t)->freetop, (v)))
512
#else
513
#define getfreetop(t, n)        (noderef((n)->freetop))
514
#define setfreetop(t, n, v)        (setmref((n)->freetop, (v)))
515
#endif
516

517
/* -- State objects ------------------------------------------------------- */
518

519
/* VM states. */
520
enum {
521
  LJ_VMST_INTERP,        /* Interpreter. */
522
  LJ_VMST_LFUNC,        /* Lua function. */
523
  LJ_VMST_FFUNC,        /* Fast function. */
524
  LJ_VMST_CFUNC,        /* C function. */
525
  LJ_VMST_GC,                /* Garbage collector. */
526
  LJ_VMST_EXIT,                /* Trace exit handler. */
527
  LJ_VMST_RECORD,        /* Trace recorder. */
528
  LJ_VMST_OPT,                /* Optimizer. */
529
  LJ_VMST_ASM,                /* Assembler. */
530
  LJ_VMST__MAX
531
};
532

533
/*
534
** In fact, when VM executes a trace, vmstate is set to the trace number,
535
** but we set the boundary to group all traces in a single pseudo-vmstate.
536
*/
537

538
#define LJ_VMST_TRACE                (LJ_VMST__MAX)
539

540
#define setvmstate(g, st)        ((g)->vmstate = ~LJ_VMST_##st)
541

542
/* Metamethods. ORDER MM */
543
#ifdef LJ_HASFFI
544
#define MMDEF_FFI(_) _(new)
545
#else
546
#define MMDEF_FFI(_)
547
#endif
548

549
#if LJ_52 || LJ_HASFFI
550
#define MMDEF_PAIRS(_) _(pairs) _(ipairs)
551
#else
552
#define MMDEF_PAIRS(_)
553
#define MM_pairs        255
554
#define MM_ipairs        255
555
#endif
556

557
#define MMDEF(_) \
558
  _(index) _(newindex) _(gc) _(mode) _(eq) _(len) \
559
  /* Only the above (fast) metamethods are negative cached (max. 8). */ \
560
  _(lt) _(le) _(concat) _(call) \
561
  /* The following must be in ORDER ARITH. */ \
562
  _(add) _(sub) _(mul) _(div) _(mod) _(pow) _(unm) \
563
  /* The following are used in the standard libraries. */ \
564
  _(metatable) _(tostring) MMDEF_FFI(_) MMDEF_PAIRS(_)
565

566
typedef enum {
567
#define MMENUM(name)        MM_##name,
568
MMDEF(MMENUM)
569
#undef MMENUM
570
  MM__MAX,
571
  MM____ = MM__MAX,
572
  MM_FAST = MM_len
573
} MMS;
574

575
/* GC root IDs. */
576
typedef enum {
577
  GCROOT_MMNAME,        /* Metamethod names. */
578
  GCROOT_MMNAME_LAST = GCROOT_MMNAME + MM__MAX-1,
579
  GCROOT_BASEMT,        /* Metatables for base types. */
580
  GCROOT_BASEMT_NUM = GCROOT_BASEMT + ~LJ_TNUMX,
581
  GCROOT_IO_INPUT,        /* Userdata for default I/O input file. */
582
  GCROOT_IO_OUTPUT,        /* Userdata for default I/O output file. */
583
  GCROOT_MAX
584
} GCRootID;
585

586
#define basemt_it(g, it)        ((g)->gcroot[GCROOT_BASEMT+~(it)])
587
#define basemt_obj(g, o)        ((g)->gcroot[GCROOT_BASEMT+itypemap(o)])
588
#define mmname_str(g, mm)        (strref((g)->gcroot[GCROOT_MMNAME+(mm)]))
589

590
/* Garbage collector states. Order matters. */
591
enum {
592
  GCSpause,                /* Start a GC cycle and mark the root set.*/
593
  GCSpropagate,                /* One gray object is processed. */
594
  GCSatomic,                /* Atomic transition from mark to sweep phase. */
595
  GCSsweepstring,        /* Sweep one chain of strings. */
596
  GCSsweep,                /* Sweep few objects from root. */
597
  GCSfinalize,                /* Finalize one userdata or cdata object. */
598
  GCSmax
599
};
600

601
typedef struct GCState {
602
  GCSize total;                /* Memory currently allocated. */
603
  GCSize threshold;        /* Memory threshold. */
604
  uint8_t currentwhite;        /* Current white color. */
605
  uint8_t state;        /* GC state. */
606
  uint8_t nocdatafin;        /* No cdata finalizer called. */
607
#if LJ_64
608
  uint8_t lightudnum;        /* Number of lightuserdata segments - 1. */
609
#else
610
  uint8_t unused1;
611
#endif
612
  MSize sweepstr;        /* Sweep position in string table. */
613
  GCRef root;                /* List of all collectable objects. */
614
  MRef sweep;                /* Sweep position in root list. */
615
  GCRef gray;                /* List of gray objects. */
616
  GCRef grayagain;        /* List of objects for atomic traversal. */
617
  GCRef weak;                /* List of weak tables (to be cleared). */
618
  GCRef mmudata;        /* List of userdata (to be finalized). */
619
  GCSize debt;                /* Debt (how much GC is behind schedule). */
620
  GCSize estimate;        /* Estimate of memory actually in use. */
621
  MSize stepmul;        /* Incremental GC step granularity. */
622
  MSize pause;                /* Pause between successive GC cycles. */
623
#if LJ_64
624
  MRef lightudseg;        /* Upper bits of lightuserdata segments. */
625
#endif
626

627
  size_t freed;                /* Total amount of freed memory. */
628
  size_t allocated;        /* Total amount of allocated memory. */
629
  size_t state_count[GCSmax]; /* Count of incremental GC steps per state. */
630
  size_t tabnum;        /* Amount of allocated table objects. */
631
  size_t udatanum;        /* Amount of allocated udata objects. */
632
#ifdef LJ_HASFFI
633
  size_t cdatanum;        /* Amount of allocated cdata objects. */
634
#endif
635
} GCState;
636

637
/* Global state, shared by all threads of a Lua universe. */
638
typedef struct global_State {
639
  GCRef *strhash;        /* String hash table (hash chain anchors). */
640
  MSize strmask;        /* String hash mask (size of hash table - 1). */
641
  MSize strnum;                /* Number of strings in hash table. */
642
#if LUAJIT_SMART_STRINGS
643
  struct {
644
    BloomFilter cur[2];
645
    BloomFilter next[2];
646
  } strbloom;
647
#endif
648
  size_t strhash_hit;        /* Strings amount found in string hash. */
649
  size_t strhash_miss;        /* Strings amount allocated and put into string hash. */
650
  lua_Alloc allocf;        /* Memory allocator. */
651
  void *allocd;                /* Memory allocator data. */
652
  GCState gc;                /* Garbage collector. */
653
  SBuf tmpbuf;                /* Temporary string buffer. */
654
  GCstr strempty;        /* Empty string. */
655
  uint8_t stremptyz;        /* Zero terminator of empty string. */
656
  uint8_t hookmask;        /* Hook mask. */
657
  uint8_t dispatchmode;        /* Dispatch mode. */
658
  uint8_t vmevmask;        /* VM event mask. */
659
  int32_t hookcount;        /* Instruction hook countdown. */
660
  GCRef mainthref;        /* Link to main thread. */
661
  TValue registrytv;        /* Anchor for registry. */
662
  TValue tmptv2, tmptv;        /* Temporary TValues. */
663
  Node nilnode;                /* Fallback 1-element hash part (nil key and value). */
664
  GCupval uvhead;        /* Head of double-linked list of all open upvalues. */
665
  volatile int32_t vmstate;  /* VM state or current JIT code trace number. */
666
  int32_t hookcstart;        /* Start count for instruction hook counter. */
667
  lua_Hook hookf;        /* Hook function. */
668
  lua_CFunction wrapf;        /* Wrapper for C function calls. */
669
  lua_CFunction panic;        /* Called as a last resort for errors. */
670
  BCIns bc_cfunc_int;        /* Bytecode for internal C function calls. */
671
  BCIns bc_cfunc_ext;        /* Bytecode for external C function calls. */
672
  GCRef cur_L;                /* Currently executing lua_State. */
673
  GCRef mem_L;                /* Currently allocating lua_State. */
674
  MRef jit_base;        /* Current JIT code L->base or NULL. */
675
  MRef ctype_state;        /* Pointer to C type state. */
676
  GCRef gcroot[GCROOT_MAX];  /* GC roots. */
677
#ifdef LJ_HASSYSPROF
678
  TValue *top_frame;        /* Top frame for sysprof. */
679
#endif
680
} global_State;
681

682
#define mainthread(g)        (&gcref(g->mainthref)->th)
683
#define niltv(L) \
684
  check_exp(tvisnil(&G(L)->nilnode.val), &G(L)->nilnode.val)
685
#define niltvg(g) \
686
  check_exp(tvisnil(&(g)->nilnode.val), &(g)->nilnode.val)
687

688
/* Hook management. Hook event masks are defined in lua.h. */
689
#define HOOK_EVENTMASK                0x0f
690
#define HOOK_ACTIVE                0x10
691
#define HOOK_ACTIVE_SHIFT        4
692
#define HOOK_VMEVENT                0x20
693
#define HOOK_GC                        0x40
694
#define HOOK_PROFILE                0x80
695
#define hook_active(g)                ((g)->hookmask & HOOK_ACTIVE)
696
#define hook_enter(g)                ((g)->hookmask |= HOOK_ACTIVE)
697
#define hook_entergc(g) \
698
  ((g)->hookmask = ((g)->hookmask | (HOOK_ACTIVE|HOOK_GC)) & ~HOOK_PROFILE)
699
#define hook_vmevent(g)                ((g)->hookmask |= (HOOK_ACTIVE|HOOK_VMEVENT))
700
#define hook_leave(g)                ((g)->hookmask &= ~HOOK_ACTIVE)
701
#define hook_save(g)                ((g)->hookmask & ~HOOK_EVENTMASK)
702
#define hook_restore(g, h) \
703
  ((g)->hookmask = ((g)->hookmask & HOOK_EVENTMASK) | (h))
704

705
/* Per-thread state object. */
706
struct lua_State {
707
  GCHeader;
708
  uint8_t dummy_ffid;        /* Fake FF_C for curr_funcisL() on dummy frames. */
709
  uint8_t status;        /* Thread status. */
710
  MRef glref;                /* Link to global state. */
711
  GCRef gclist;                /* GC chain. */
712
  TValue *base;                /* Base of currently executing function. */
713
  TValue *top;                /* First free slot in the stack. */
714
  MRef maxstack;        /* Last free slot in the stack. */
715
  MRef stack;                /* Stack base. */
716
  GCRef openupval;        /* List of open upvalues in the stack. */
717
  GCRef env;                /* Thread environment (table of globals). */
718
  void *cframe;                /* End of C stack frame chain. */
719
  MSize stacksize;        /* True stack size (incl. LJ_STACK_EXTRA). */
720
};
721

722
#define G(L)                        (mref(L->glref, global_State))
723
#define registry(L)                (&G(L)->registrytv)
724

725
/* Macros to access the currently executing (Lua) function. */
726
#if LJ_GC64
727
#define curr_func(L)                (&gcval(L->base-2)->fn)
728
#elif LJ_FR2
729
#define curr_func(L)                (&gcref((L->base-2)->gcr)->fn)
730
#else
731
#define curr_func(L)                (&gcref((L->base-1)->fr.func)->fn)
732
#endif
733
#define curr_funcisL(L)                (isluafunc(curr_func(L)))
734
#define curr_proto(L)                (funcproto(curr_func(L)))
735
#define curr_topL(L)                (L->base + curr_proto(L)->framesize)
736
#define curr_top(L)                (curr_funcisL(L) ? curr_topL(L) : L->top)
737

738
/* -- GC object definition and conversions -------------------------------- */
739

740
/* GC header for generic access to common fields of GC objects. */
741
typedef struct GChead {
742
  GCHeader;
743
  uint8_t unused1;
744
  uint8_t unused2;
745
  GCRef env;
746
  GCRef gclist;
747
  GCRef metatable;
748
} GChead;
749

750
/* The env field SHOULD be at the same offset for all GC objects. */
751
LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCfuncL, env));
752
LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCudata, env));
753

754
/* The metatable field MUST be at the same offset for all GC objects. */
755
LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCtab, metatable));
756
LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCudata, metatable));
757

758
/* The gclist field MUST be at the same offset for all GC objects. */
759
LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(lua_State, gclist));
760
LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCproto, gclist));
761
LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCfuncL, gclist));
762
LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtab, gclist));
763

764
typedef union GCobj {
765
  GChead gch;
766
  GCstr str;
767
  GCupval uv;
768
  lua_State th;
769
  GCproto pt;
770
  GCfunc fn;
771
  GCcdata cd;
772
  GCtab tab;
773
  GCudata ud;
774
} GCobj;
775

776
/* Macros to convert a GCobj pointer into a specific value. */
777
#define gco2str(o)        check_exp((o)->gch.gct == ~LJ_TSTR, &(o)->str)
778
#define gco2uv(o)        check_exp((o)->gch.gct == ~LJ_TUPVAL, &(o)->uv)
779
#define gco2th(o)        check_exp((o)->gch.gct == ~LJ_TTHREAD, &(o)->th)
780
#define gco2pt(o)        check_exp((o)->gch.gct == ~LJ_TPROTO, &(o)->pt)
781
#define gco2func(o)        check_exp((o)->gch.gct == ~LJ_TFUNC, &(o)->fn)
782
#define gco2cd(o)        check_exp((o)->gch.gct == ~LJ_TCDATA, &(o)->cd)
783
#define gco2tab(o)        check_exp((o)->gch.gct == ~LJ_TTAB, &(o)->tab)
784
#define gco2ud(o)        check_exp((o)->gch.gct == ~LJ_TUDATA, &(o)->ud)
785

786
/* Macro to convert any collectable object into a GCobj pointer. */
787
#define obj2gco(v)        ((GCobj *)(v))
788

789
/* -- TValue getters/setters ---------------------------------------------- */
790

791
#ifdef LUA_USE_ASSERT
792
#include "lj_gc.h"
793
#endif
794

795
/* Macros to test types. */
796
#if LJ_GC64
797
#define itype(o)        ((uint32_t)((o)->it64 >> 47))
798
#define tvisnil(o)        ((o)->it64 == -1)
799
#else
800
#define itype(o)        ((o)->it)
801
#define tvisnil(o)        (itype(o) == LJ_TNIL)
802
#endif
803
#define tvisfalse(o)        (itype(o) == LJ_TFALSE)
804
#define tvistrue(o)        (itype(o) == LJ_TTRUE)
805
#define tvisbool(o)        (tvisfalse(o) || tvistrue(o))
806
#if LJ_64 && !LJ_GC64
807
#define tvislightud(o)        (((int32_t)itype(o) >> 15) == -2)
808
#else
809
#define tvislightud(o)        (itype(o) == LJ_TLIGHTUD)
810
#endif
811
#define tvisstr(o)        (itype(o) == LJ_TSTR)
812
#define tvisfunc(o)        (itype(o) == LJ_TFUNC)
813
#define tvisthread(o)        (itype(o) == LJ_TTHREAD)
814
#define tvisproto(o)        (itype(o) == LJ_TPROTO)
815
#define tviscdata(o)        (itype(o) == LJ_TCDATA)
816
#define tvistab(o)        (itype(o) == LJ_TTAB)
817
#define tvisudata(o)        (itype(o) == LJ_TUDATA)
818
#define tvisnumber(o)        (itype(o) <= LJ_TISNUM)
819
#define tvisint(o)        (LJ_DUALNUM && itype(o) == LJ_TISNUM)
820
#define tvisnum(o)        (itype(o) < LJ_TISNUM)
821

822
#define tvistruecond(o)        (itype(o) < LJ_TISTRUECOND)
823
#define tvispri(o)        (itype(o) >= LJ_TISPRI)
824
#define tvistabud(o)        (itype(o) <= LJ_TISTABUD)  /* && !tvisnum() */
825
#define tvisgcv(o)        ((itype(o) - LJ_TISGCV) > (LJ_TNUMX - LJ_TISGCV))
826

827
/* Special macros to test numbers for NaN, +0, -0, +1 and raw equality. */
828
#define tvisnan(o)        ((o)->n != (o)->n)
829
#if LJ_64
830
#define tviszero(o)        (((o)->u64 << 1) == 0)
831
#else
832
#define tviszero(o)        (((o)->u32.lo | ((o)->u32.hi << 1)) == 0)
833
#endif
834
#define tvispzero(o)        ((o)->u64 == 0)
835
#define tvismzero(o)        ((o)->u64 == U64x(80000000,00000000))
836
#define tvispone(o)        ((o)->u64 == U64x(3ff00000,00000000))
837
#define rawnumequal(o1, o2)        ((o1)->u64 == (o2)->u64)
838

839
/* Macros to convert type ids. */
840
#if LJ_64 && !LJ_GC64
841
#define itypemap(o) \
842
  (tvisnumber(o) ? ~LJ_TNUMX : tvislightud(o) ? ~LJ_TLIGHTUD : ~itype(o))
843
#else
844
#define itypemap(o)        (tvisnumber(o) ? ~LJ_TNUMX : ~itype(o))
845
#endif
846

847
/* Macros to get tagged values. */
848
#if LJ_GC64
849
#define gcval(o)        ((GCobj *)(gcrefu((o)->gcr) & LJ_GCVMASK))
850
#else
851
#define gcval(o)        (gcref((o)->gcr))
852
#endif
853
#define boolV(o)        check_exp(tvisbool(o), (LJ_TFALSE - itype(o)))
854
#if LJ_64
855
#define lightudseg(u) \
856
  (((u) >> LJ_LIGHTUD_BITS_LO) & ((1 << LJ_LIGHTUD_BITS_SEG)-1))
857
#define lightudlo(u) \
858
  ((u) & (((uint64_t)1 << LJ_LIGHTUD_BITS_LO) - 1))
859
#define lightudup(p) \
860
  ((uint32_t)(((p) >> LJ_LIGHTUD_BITS_LO) << (LJ_LIGHTUD_BITS_LO-32)))
861
static LJ_AINLINE void *lightudV(global_State *g, cTValue *o)
×
862
{
863
  uint64_t u = o->u64;
×
864
  uint64_t seg = lightudseg(u);
×
865
  uint32_t *segmap = mref(g->gc.lightudseg, uint32_t);
×
866
  lua_assert(tvislightud(o));
×
867
  lua_assert(seg <= g->gc.lightudnum);
×
868
  return (void *)(((uint64_t)segmap[seg] << 32) | lightudlo(u));
×
869
}
870
#else
871
#define lightudV(g, o)        check_exp(tvislightud(o), gcrefp((o)->gcr, void))
872
#endif
873
#define gcV(o)                check_exp(tvisgcv(o), gcval(o))
874
#define strV(o)                check_exp(tvisstr(o), &gcval(o)->str)
875
#define funcV(o)        check_exp(tvisfunc(o), &gcval(o)->fn)
876
#define threadV(o)        check_exp(tvisthread(o), &gcval(o)->th)
877
#define protoV(o)        check_exp(tvisproto(o), &gcval(o)->pt)
878
#define cdataV(o)        check_exp(tviscdata(o), &gcval(o)->cd)
879
#define tabV(o)                check_exp(tvistab(o), &gcval(o)->tab)
880
#define udataV(o)        check_exp(tvisudata(o), &gcval(o)->ud)
881
#define numV(o)                check_exp(tvisnum(o), (o)->n)
882
#define intV(o)                check_exp(tvisint(o), (int32_t)(o)->i)
883

884
/* Macros to set tagged values. */
885
#if LJ_GC64
886
#define setitype(o, i)                ((o)->it = ((i) << 15))
887
#define setnilV(o)                ((o)->it64 = -1)
888
#define setpriV(o, x)                ((o)->it64 = (int64_t)~((uint64_t)~(x)<<47))
889
#define setboolV(o, x)                ((o)->it64 = (int64_t)~((uint64_t)((x)+1)<<47))
890
#else
891
#define setitype(o, i)                ((o)->it = (i))
892
#define setnilV(o)                ((o)->it = LJ_TNIL)
893
#define setboolV(o, x)                ((o)->it = LJ_TFALSE-(uint32_t)(x))
894
#define setpriV(o, i)                (setitype((o), (i)))
895
#endif
896

897
static LJ_AINLINE void setrawlightudV(TValue *o, void *p)
475✔
898
{
899
#if LJ_GC64
900
  o->u64 = (uint64_t)p | (((uint64_t)LJ_TLIGHTUD) << 47);
258✔
901
#elif LJ_64
902
  o->u64 = (uint64_t)p | (((uint64_t)0xffff) << 48);
903
#else
904
  setgcrefp(o->gcr, p); setitype(o, LJ_TLIGHTUD);
905
#endif
906
}
907

908
#if LJ_FR2 || LJ_32
909
#define contptr(f)                ((void *)(f))
910
#define setcont(o, f)                ((o)->u64 = (uint64_t)(uintptr_t)contptr(f))
911
#else
912
#define contptr(f) \
913
  ((void *)(uintptr_t)(uint32_t)((intptr_t)(f) - (intptr_t)lj_vm_asm_begin))
914
#define setcont(o, f) \
915
  ((o)->u64 = (uint64_t)(void *)(f) - (uint64_t)lj_vm_asm_begin)
916
#endif
917

918
#define tvchecklive(L, o) \
919
  UNUSED(L), lua_assert(!tvisgcv(o) || \
920
  ((~itype(o) == gcval(o)->gch.gct) && !isdead(G(L), gcval(o))))
921

922
static LJ_AINLINE void setgcVraw(TValue *o, GCobj *v, uint32_t itype)
9,655,687✔
923
{
924
#if LJ_GC64
925
  setgcreft(o->gcr, v, itype);
7,592,341✔
926
#else
927
  setgcref(o->gcr, v); setitype(o, itype);
928
#endif
929
}
130,480✔
930

931
static LJ_AINLINE void setgcV(lua_State *L, TValue *o, GCobj *v, uint32_t it)
9,513,695✔
932
{
933
  setgcVraw(o, v, it); tvchecklive(L, o);
9,314,540✔
934
}
26,526✔
935

936
#define define_setV(name, type, tag) \
937
static LJ_AINLINE void name(lua_State *L, TValue *o, type *v) \
938
{ \
939
  setgcV(L, o, obj2gco(v), tag); \
940
}
941
define_setV(setstrV, GCstr, LJ_TSTR)
6,180,614✔
942
define_setV(setthreadV, lua_State, LJ_TTHREAD)
1,441✔
943
define_setV(setprotoV, GCproto, LJ_TPROTO)
36✔
944
define_setV(setfuncV, GCfunc, LJ_TFUNC)
82,664✔
945
define_setV(setcdataV, GCcdata, LJ_TCDATA)
503,946✔
946
define_setV(settabV, GCtab, LJ_TTAB)
1,087,196✔
947
define_setV(setudataV, GCudata, LJ_TUDATA)
1,003,831✔
948

949
#define setnumV(o, x)                ((o)->n = (x))
950
#define setnanV(o)                ((o)->u64 = U64x(fff80000,00000000))
951
#define setpinfV(o)                ((o)->u64 = U64x(7ff00000,00000000))
952
#define setminfV(o)                ((o)->u64 = U64x(fff00000,00000000))
953

954
static LJ_AINLINE void setintV(TValue *o, int32_t i)
186,061✔
955
{
956
#if LJ_DUALNUM
957
  o->i = (uint32_t)i; setitype(o, LJ_TISNUM);
958
#else
959
  o->n = (lua_Number)i;
167,910✔
960
#endif
961
}
5,618✔
962

963
static LJ_AINLINE void setint64V(TValue *o, int64_t i)
1,698✔
964
{
965
  if (LJ_DUALNUM && LJ_LIKELY(i == (int64_t)(int32_t)i))
1,698✔
966
    setintV(o, (int32_t)i);
967
  else
968
    setnumV(o, (lua_Number)i);
1,698✔
969
}
970

971
#if LJ_64
972
#define setintptrV(o, i)        setint64V((o), (i))
973
#else
974
#define setintptrV(o, i)        setintV((o), (i))
975
#endif
976

977
/* Copy tagged values. */
978
static LJ_AINLINE void copyTV(lua_State *L, TValue *o1, const TValue *o2)
8,853,173✔
979
{
980
  *o1 = *o2; tvchecklive(L, o1);
5,551,996✔
981
}
3,935,493✔
982

983
/* -- Number to integer conversion ---------------------------------------- */
984

985
#if LJ_SOFTFP
986
LJ_ASMF int32_t lj_vm_tobit(double x);
987
#endif
988

989
static LJ_AINLINE int32_t lj_num2bit(lua_Number n)
899✔
990
{
991
#if LJ_SOFTFP
992
  return lj_vm_tobit(n);
993
#else
994
  TValue o;
899✔
995
  o.n = n + 6755399441055744.0;  /* 2^52 + 2^51 */
899✔
996
  return (int32_t)o.u32.lo;
899✔
997
#endif
998
}
999

1000
#define lj_num2int(n)   ((int32_t)(n))
1001

1002
static LJ_AINLINE uint64_t lj_num2u64(lua_Number n)
1,913,236✔
1003
{
1004
#ifdef _MSC_VER
1005
  if (n >= 9223372036854775808.0)  /* They think it's a feature. */
1006
    return (uint64_t)(int64_t)(n - 18446744073709551616.0);
1007
  else
1008
#endif
1009
    return (uint64_t)n;
1,913,236✔
1010
}
1011

1012
static LJ_AINLINE int32_t numberVint(cTValue *o)
10,047✔
1013
{
1014
  if (LJ_LIKELY(tvisint(o)))
10,047✔
1015
    return intV(o);
1016
  else
1017
    return lj_num2int(numV(o));
10,047✔
1018
}
1019

1020
static LJ_AINLINE lua_Number numberVnum(cTValue *o)
559,381✔
1021
{
1022
  if (LJ_UNLIKELY(tvisint(o)))
559,381✔
1023
    return (lua_Number)intV(o);
1024
  else
1025
    return numV(o);
559,381✔
1026
}
1027

1028
/* -- Miscellaneous object handling --------------------------------------- */
1029

1030
/* Names and maps for internal and external object tags. */
1031
LJ_DATA const char *const lj_obj_typename[1+LUA_TCDATA+1];
1032
LJ_DATA const char *const lj_obj_itypename[~LJ_TNUMX+1];
1033

1034
#define lj_typename(o)        (lj_obj_itypename[itypemap(o)])
1035

1036
/* Compare two objects without calling metamethods. */
1037
LJ_FUNC int LJ_FASTCALL lj_obj_equal(cTValue *o1, cTValue *o2);
1038
LJ_FUNC const void * LJ_FASTCALL lj_obj_ptr(global_State *g, cTValue *o);
1039

1040
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc