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

tarantool / luajit / 6298233961

25 Sep 2023 10:42AM UTC coverage: 88.2%. First build
6298233961

push

github

fckxorg
Handle OOM error on stack resize in coroutine.resume and lua_checkstack.

Thanks to Peter Cawley. #1066

5336 of 5970 branches covered (0.0%)

Branch coverage included in aggregate %.

14 of 14 new or added lines in 3 files covered. (100.0%)

20482 of 23302 relevant lines covered (87.9%)

1286297.15 hits per line

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

72.92
/src/lj_api.c
1
/*
2
** Public Lua/C API.
3
** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
4
**
5
** Major 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
#define lj_api_c
10
#define LUA_CORE
11

12
#include "lj_obj.h"
13
#include "lj_gc.h"
14
#include "lj_err.h"
15
#include "lj_debug.h"
16
#include "lj_str.h"
17
#include "lj_tab.h"
18
#include "lj_func.h"
19
#include "lj_udata.h"
20
#include "lj_meta.h"
21
#include "lj_state.h"
22
#include "lj_bc.h"
23
#include "lj_frame.h"
24
#include "lj_trace.h"
25
#include "lj_vm.h"
26
#include "lj_strscan.h"
27
#include "lj_strfmt.h"
28

29
/* -- Common helper functions --------------------------------------------- */
30

31
#define lj_checkapi_slot(idx) \
32
  lj_checkapi((idx) <= (L->top - L->base), "stack slot %d out of range", (idx))
33

34
static TValue *index2adr(lua_State *L, int idx)
25,219,806✔
35
{
36
  if (idx > 0) {
25,219,806✔
37
    TValue *o = L->base + (idx - 1);
9,553,901✔
38
    return o < L->top ? o : niltv(L);
9,553,901✔
39
  } else if (idx > LUA_REGISTRYINDEX) {
15,665,905✔
40
    lj_checkapi(idx != 0 && -idx <= L->top - L->base,
10,365,833✔
41
                "bad stack slot %d", idx);
42
    return L->top + idx;
10,365,833✔
43
  } else if (idx == LUA_GLOBALSINDEX) {
5,300,072✔
44
    TValue *o = &G(L)->tmptv;
4,362✔
45
    settabV(L, o, tabref(L->env));
4,362✔
46
    return o;
4,362✔
47
  } else if (idx == LUA_REGISTRYINDEX) {
5,295,710✔
48
    return registry(L);
4,293,270✔
49
  } else {
50
    GCfunc *fn = curr_func(L);
1,002,440✔
51
    lj_checkapi(fn->c.gct == ~LJ_TFUNC && !isluafunc(fn),
1,002,440✔
52
                "calling frame is not a C function");
53
    if (idx == LUA_ENVIRONINDEX) {
1,002,440✔
54
      TValue *o = &G(L)->tmptv;
1,472✔
55
      settabV(L, o, tabref(fn->c.env));
1,472✔
56
      return o;
1,472✔
57
    } else {
58
      idx = LUA_GLOBALSINDEX - idx;
1,000,968✔
59
      return idx <= fn->c.nupvalues ? &fn->c.upvalue[idx-1] : niltv(L);
1,000,968✔
60
    }
61
  }
62
}
63

64
static LJ_AINLINE TValue *index2adr_check(lua_State *L, int idx)
1,166,012✔
65
{
66
  TValue *o = index2adr(L, idx);
2,332,024✔
67
  lj_checkapi(o != niltv(L), "invalid stack slot %d", idx);
1,166,012✔
68
  return o;
1,166,012✔
69
}
70

71
static TValue *index2adr_stack(lua_State *L, int idx)
15,006✔
72
{
73
  if (idx > 0) {
15,006✔
74
    TValue *o = L->base + (idx - 1);
1,277✔
75
    if (o < L->top) {
1,277✔
76
      return o;
77
    } else {
78
      lj_checkapi(0, "invalid stack slot %d", idx);
×
79
      return niltv(L);
×
80
    }
81
    return o < L->top ? o : niltv(L);
82
  } else {
83
    lj_checkapi(idx != 0 && -idx <= L->top - L->base,
13,729✔
84
                "invalid stack slot %d", idx);
85
    return L->top + idx;
13,729✔
86
  }
87
}
88

89
static GCtab *getcurrenv(lua_State *L)
1,013,845✔
90
{
91
  GCfunc *fn = curr_func(L);
1,013,845✔
92
  return fn->c.gct == ~LJ_TFUNC ? tabref(fn->c.env) : tabref(L->env);
1,013,845✔
93
}
94

95
static void jit_secure_call(lua_State *L, TValue *base, int nres) {
5,209,087✔
96
  global_State *g = G(L);
5,209,087✔
97
  /* Forbid Lua world re-entrancy while running the trace */
98
  if (tvref(g->jit_base)) {
5,209,087✔
99
    setstrV(L, L->top++, lj_err_str(L, LJ_ERR_JITCALL));
1✔
100
    if (g->panic) g->panic(L);
1✔
101
    exit(EXIT_FAILURE);
1✔
102
  }
103
  lj_trace_abort(g);  /* Never record across Lua VM entrance */
5,209,086✔
104
  lj_vm_call(L, base, nres);
5,209,086✔
105
}
5,209,083✔
106

107
/* -- Miscellaneous API functions ----------------------------------------- */
108

109
LUA_API int lua_status(lua_State *L)
×
110
{
111
  return L->status;
×
112
}
113

114
LUA_API int lua_checkstack(lua_State *L, int size)
14,339✔
115
{
116
  if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) {
14,339✔
117
    return 0;  /* Stack overflow. */
118
  } else if (size > 0) {
14,339✔
119
    int avail = (int)(mref(L->maxstack, TValue) - L->top);
13,647✔
120
    if (size > avail &&
13,648✔
121
        lj_state_cpgrowstack(L, (MSize)(size - avail)) != LUA_OK) {
1✔
122
      L->top--;
×
123
      return 0;  /* Out of memory. */
×
124
    }
125
  }
126
  return 1;
127
}
128

129
LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg)
14,283✔
130
{
131
  if (!lua_checkstack(L, size))
14,283✔
132
    lj_err_callerv(L, LJ_ERR_STKOVM, msg);
×
133
}
14,283✔
134

135
LUA_API void lua_xmove(lua_State *L, lua_State *to, int n)
17✔
136
{
137
  TValue *f, *t;
17✔
138
  if (L == to) return;
17✔
139
  lj_checkapi_slot(n);
4✔
140
  lj_checkapi(G(L) == G(to), "move across global states");
4✔
141
  lj_state_checkstack(to, (MSize)n);
4✔
142
  f = L->top;
4✔
143
  t = to->top = to->top + n;
4✔
144
  while (--n >= 0) copyTV(to, --t, --f);
8✔
145
  L->top = f;
4✔
146
}
147

148
LUA_API const lua_Number *lua_version(lua_State *L)
×
149
{
150
  static const lua_Number version = LUA_VERSION_NUM;
×
151
  UNUSED(L);
×
152
  return &version;
×
153
}
154

155
/* -- Stack manipulation -------------------------------------------------- */
156

157
LUA_API int lua_gettop(lua_State *L)
501✔
158
{
159
  return (int)(L->top - L->base);
501✔
160
}
161

162
LUA_API void lua_settop(lua_State *L, int idx)
3,108,431✔
163
{
164
  if (idx >= 0) {
3,108,431✔
165
    lj_checkapi(idx <= tvref(L->maxstack) - L->base, "bad stack slot %d", idx);
1,060,066✔
166
    if (L->base + idx > L->top) {
1,060,066✔
167
      if (L->base + idx >= tvref(L->maxstack))
58,204✔
168
        lj_state_growstack(L, (MSize)idx - (MSize)(L->top - L->base));
×
169
      do { setnilV(L->top++); } while (L->top < L->base + idx);
158,646✔
170
    } else {
171
      L->top = L->base + idx;
1,001,862✔
172
    }
173
  } else {
174
    lj_checkapi(-(idx+1) <= (L->top - L->base), "bad stack slot %d", idx);
2,048,365✔
175
    L->top += idx+1;  /* Shrinks top (idx < 0). */
2,048,365✔
176
  }
177
}
3,108,431✔
178

179
LUA_API void lua_remove(lua_State *L, int idx)
13,730✔
180
{
181
  TValue *p = index2adr_stack(L, idx);
13,730✔
182
  while (++p < L->top) copyTV(L, p-1, p);
27,121✔
183
  L->top--;
13,730✔
184
}
13,730✔
185

186
LUA_API void lua_insert(lua_State *L, int idx)
825✔
187
{
188
  TValue *q, *p = index2adr_stack(L, idx);
825✔
189
  for (q = L->top; q > p; q--) copyTV(L, q, q-1);
2,350✔
190
  copyTV(L, p, L->top);
825✔
191
}
825✔
192

193
static void copy_slot(lua_State *L, TValue *f, int idx)
232✔
194
{
195
  if (idx == LUA_GLOBALSINDEX) {
232✔
196
    lj_checkapi(tvistab(f), "stack slot %d is not a table", idx);
×
197
    /* NOBARRIER: A thread (i.e. L) is never black. */
198
    setgcref(L->env, obj2gco(tabV(f)));
×
199
  } else if (idx == LUA_ENVIRONINDEX) {
232✔
200
    GCfunc *fn = curr_func(L);
232✔
201
    if (fn->c.gct != ~LJ_TFUNC)
232✔
202
      lj_err_msg(L, LJ_ERR_NOENV);
×
203
    lj_checkapi(tvistab(f), "stack slot %d is not a table", idx);
232✔
204
    setgcref(fn->c.env, obj2gco(tabV(f)));
232✔
205
    lj_gc_barrier(L, fn, f);
232✔
206
  } else {
207
    TValue *o = index2adr_check(L, idx);
×
208
    copyTV(L, o, f);
×
209
    if (idx < LUA_GLOBALSINDEX)  /* Need a barrier for upvalues. */
×
210
      lj_gc_barrier(L, curr_func(L), f);
×
211
  }
212
}
232✔
213

214
LUA_API void lua_replace(lua_State *L, int idx)
×
215
{
216
  lj_checkapi_slot(1);
×
217
  copy_slot(L, L->top - 1, idx);
×
218
  L->top--;
×
219
}
×
220

221
LUA_API void lua_copy(lua_State *L, int fromidx, int toidx)
232✔
222
{
223
  copy_slot(L, index2adr(L, fromidx), toidx);
232✔
224
}
232✔
225

226
LUA_API void lua_pushvalue(lua_State *L, int idx)
3,760,356✔
227
{
228
  copyTV(L, L->top, index2adr(L, idx));
3,760,356✔
229
  incr_top(L);
3,760,356✔
230
}
3,760,356✔
231

232
/* -- Stack getters ------------------------------------------------------- */
233

234
LUA_API int lua_type(lua_State *L, int idx)
7,320,293✔
235
{
236
  cTValue *o = index2adr(L, idx);
7,320,293✔
237
  if (tvisnumber(o)) {
7,320,293✔
238
    return LUA_TNUMBER;
239
#if LJ_64 && !LJ_GC64
240
  } else if (tvislightud(o)) {
241
    return LUA_TLIGHTUSERDATA;
242
#endif
243
  } else if (o == niltv(L)) {
7,320,272✔
244
    return LUA_TNONE;
245
  } else {  /* Magic internal/external tag conversion. ORDER LJ_T */
246
    uint32_t t = ~itype(o);
7,320,242✔
247
#if LJ_64
248
    int tt = (int)((U64x(75a06,98042110) >> 4*t) & 15u);
7,320,242✔
249
#else
250
    int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u);
251
#endif
252
    lj_assertL(tt != LUA_TNIL || tvisnil(o), "bad tag conversion");
7,320,242✔
253
    return tt;
7,320,242✔
254
  }
255
}
256

257
LUALIB_API void luaL_checktype(lua_State *L, int idx, int tt)
42✔
258
{
259
  if (lua_type(L, idx) != tt)
42✔
260
    lj_err_argt(L, idx, tt);
×
261
}
42✔
262

263
LUALIB_API void luaL_checkany(lua_State *L, int idx)
×
264
{
265
  if (index2adr(L, idx) == niltv(L))
×
266
    lj_err_arg(L, idx, LJ_ERR_NOVAL);
×
267
}
×
268

269
LUA_API const char *lua_typename(lua_State *L, int t)
1✔
270
{
271
  UNUSED(L);
1✔
272
  return lj_obj_typename[t+1];
1✔
273
}
274

275
LUA_API int lua_iscfunction(lua_State *L, int idx)
12✔
276
{
277
  cTValue *o = index2adr(L, idx);
12✔
278
  return tvisfunc(o) && !isluafunc(funcV(o));
12✔
279
}
280

281
LUA_API int lua_isnumber(lua_State *L, int idx)
71,269✔
282
{
283
  cTValue *o = index2adr(L, idx);
71,269✔
284
  TValue tmp;
71,269✔
285
  return (tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), &tmp)));
71,269✔
286
}
287

288
LUA_API int lua_isstring(lua_State *L, int idx)
11,334✔
289
{
290
  cTValue *o = index2adr(L, idx);
11,334✔
291
  return (tvisstr(o) || tvisnumber(o));
11,334✔
292
}
293

294
LUA_API int lua_isuserdata(lua_State *L, int idx)
×
295
{
296
  cTValue *o = index2adr(L, idx);
×
297
  return (tvisudata(o) || tvislightud(o));
×
298
}
299

300
LUA_API int lua_rawequal(lua_State *L, int idx1, int idx2)
×
301
{
302
  cTValue *o1 = index2adr(L, idx1);
×
303
  cTValue *o2 = index2adr(L, idx2);
×
304
  return (o1 == niltv(L) || o2 == niltv(L)) ? 0 : lj_obj_equal(o1, o2);
×
305
}
306

307
LUA_API int lua_equal(lua_State *L, int idx1, int idx2)
×
308
{
309
  cTValue *o1 = index2adr(L, idx1);
×
310
  cTValue *o2 = index2adr(L, idx2);
×
311
  if (tvisint(o1) && tvisint(o2)) {
×
312
    return intV(o1) == intV(o2);
313
  } else if (tvisnumber(o1) && tvisnumber(o2)) {
×
314
    return numberVnum(o1) == numberVnum(o2);
×
315
  } else if (itype(o1) != itype(o2)) {
×
316
    return 0;
317
  } else if (tvispri(o1)) {
×
318
    return o1 != niltv(L) && o2 != niltv(L);
×
319
#if LJ_64 && !LJ_GC64
320
  } else if (tvislightud(o1)) {
321
    return o1->u64 == o2->u64;
322
#endif
323
  } else if (gcrefeq(o1->gcr, o2->gcr)) {
×
324
    return 1;
325
  } else if (!tvistabud(o1)) {
×
326
    return 0;
327
  } else {
328
    TValue *base = lj_meta_equal(L, gcV(o1), gcV(o2), 0);
×
329
    if ((uintptr_t)base <= 1) {
×
330
      return (int)(uintptr_t)base;
×
331
    } else {
332
      L->top = base+2;
×
333
      jit_secure_call(L, base, 1+1);
×
334
      L->top -= 2+LJ_FR2;
×
335
      return tvistruecond(L->top+1+LJ_FR2);
×
336
    }
337
  }
338
}
339

340
LUA_API int lua_lessthan(lua_State *L, int idx1, int idx2)
1,049,215✔
341
{
342
  cTValue *o1 = index2adr(L, idx1);
1,049,215✔
343
  cTValue *o2 = index2adr(L, idx2);
1,049,215✔
344
  if (o1 == niltv(L) || o2 == niltv(L)) {
1,049,215✔
345
    return 0;
346
  } else if (tvisint(o1) && tvisint(o2)) {
1,049,215✔
347
    return intV(o1) < intV(o2);
348
  } else if (tvisnumber(o1) && tvisnumber(o2)) {
1,049,215✔
349
    return numberVnum(o1) < numberVnum(o2);
937,096✔
350
  } else {
351
    TValue *base = lj_meta_comp(L, o1, o2, 0);
112,119✔
352
    if ((uintptr_t)base <= 1) {
112,119✔
353
      return (int)(uintptr_t)base;
102,133✔
354
    } else {
355
      L->top = base+2;
9,986✔
356
      jit_secure_call(L, base, 1+1);
9,986✔
357
      L->top -= 2+LJ_FR2;
9,986✔
358
      return tvistruecond(L->top+1+LJ_FR2);
9,986✔
359
    }
360
  }
361
}
362

363
LUA_API lua_Number lua_tonumber(lua_State *L, int idx)
269✔
364
{
365
  cTValue *o = index2adr(L, idx);
269✔
366
  TValue tmp;
269✔
367
  if (LJ_LIKELY(tvisnumber(o)))
269✔
368
    return numberVnum(o);
265✔
369
  else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp))
4✔
370
    return numV(&tmp);
4✔
371
  else
372
    return 0;
×
373
}
374

375
LUA_API lua_Number lua_tonumberx(lua_State *L, int idx, int *ok)
×
376
{
377
  cTValue *o = index2adr(L, idx);
×
378
  TValue tmp;
×
379
  if (LJ_LIKELY(tvisnumber(o))) {
×
380
    if (ok) *ok = 1;
×
381
    return numberVnum(o);
×
382
  } else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp)) {
×
383
    if (ok) *ok = 1;
×
384
    return numV(&tmp);
×
385
  } else {
386
    if (ok) *ok = 0;
×
387
    return 0;
×
388
  }
389
}
390

391
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx)
12✔
392
{
393
  cTValue *o = index2adr(L, idx);
12✔
394
  TValue tmp;
12✔
395
  if (LJ_LIKELY(tvisnumber(o)))
12✔
396
    return numberVnum(o);
12✔
397
  else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp)))
×
398
    lj_err_argt(L, idx, LUA_TNUMBER);
×
399
  return numV(&tmp);
×
400
}
401

402
LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def)
4✔
403
{
404
  cTValue *o = index2adr(L, idx);
4✔
405
  TValue tmp;
4✔
406
  if (LJ_LIKELY(tvisnumber(o)))
4✔
407
    return numberVnum(o);
4✔
408
  else if (tvisnil(o))
×
409
    return def;
410
  else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp)))
×
411
    lj_err_argt(L, idx, LUA_TNUMBER);
×
412
  return numV(&tmp);
×
413
}
414

415
LUA_API lua_Integer lua_tointeger(lua_State *L, int idx)
71,232✔
416
{
417
  cTValue *o = index2adr(L, idx);
71,232✔
418
  TValue tmp;
71,232✔
419
  lua_Number n;
71,232✔
420
  if (LJ_LIKELY(tvisint(o))) {
71,232✔
421
    return intV(o);
422
  } else if (LJ_LIKELY(tvisnum(o))) {
71,232✔
423
    n = numV(o);
71,230✔
424
  } else {
425
    if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
2✔
426
      return 0;
2✔
427
    if (tvisint(&tmp))
×
428
      return intV(&tmp);
429
    n = numV(&tmp);
×
430
  }
431
#if LJ_64
432
  return (lua_Integer)n;
71,230✔
433
#else
434
  return lj_num2int(n);
435
#endif
436
}
437

438
LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *ok)
×
439
{
440
  cTValue *o = index2adr(L, idx);
×
441
  TValue tmp;
×
442
  lua_Number n;
×
443
  if (LJ_LIKELY(tvisint(o))) {
×
444
    if (ok) *ok = 1;
445
    return intV(o);
446
  } else if (LJ_LIKELY(tvisnum(o))) {
×
447
    n = numV(o);
×
448
  } else {
449
    if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) {
×
450
      if (ok) *ok = 0;
×
451
      return 0;
×
452
    }
453
    if (tvisint(&tmp)) {
×
454
      if (ok) *ok = 1;
455
      return intV(&tmp);
456
    }
457
    n = numV(&tmp);
×
458
  }
459
  if (ok) *ok = 1;
×
460
#if LJ_64
461
  return (lua_Integer)n;
×
462
#else
463
  return lj_num2int(n);
464
#endif
465
}
466

467
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx)
×
468
{
469
  cTValue *o = index2adr(L, idx);
×
470
  TValue tmp;
×
471
  lua_Number n;
×
472
  if (LJ_LIKELY(tvisint(o))) {
×
473
    return intV(o);
474
  } else if (LJ_LIKELY(tvisnum(o))) {
×
475
    n = numV(o);
×
476
  } else {
477
    if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
×
478
      lj_err_argt(L, idx, LUA_TNUMBER);
×
479
    if (tvisint(&tmp))
×
480
      return (lua_Integer)intV(&tmp);
481
    n = numV(&tmp);
×
482
  }
483
#if LJ_64
484
  return (lua_Integer)n;
×
485
#else
486
  return lj_num2int(n);
487
#endif
488
}
489

490
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def)
22,329✔
491
{
492
  cTValue *o = index2adr(L, idx);
22,329✔
493
  TValue tmp;
22,329✔
494
  lua_Number n;
22,329✔
495
  if (LJ_LIKELY(tvisint(o))) {
22,329✔
496
    return intV(o);
497
  } else if (LJ_LIKELY(tvisnum(o))) {
22,329✔
498
    n = numV(o);
33✔
499
  } else if (tvisnil(o)) {
22,296✔
500
    return def;
501
  } else {
502
    if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
×
503
      lj_err_argt(L, idx, LUA_TNUMBER);
×
504
    if (tvisint(&tmp))
×
505
      return (lua_Integer)intV(&tmp);
506
    n = numV(&tmp);
×
507
  }
508
#if LJ_64
509
  return (lua_Integer)n;
33✔
510
#else
511
  return lj_num2int(n);
512
#endif
513
}
514

515
LUA_API int lua_toboolean(lua_State *L, int idx)
1,912,721✔
516
{
517
  cTValue *o = index2adr(L, idx);
1,912,721✔
518
  return tvistruecond(o);
1,912,721✔
519
}
520

521
LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len)
52,975✔
522
{
523
  TValue *o = index2adr(L, idx);
52,975✔
524
  GCstr *s;
52,975✔
525
  if (LJ_LIKELY(tvisstr(o))) {
52,975✔
526
    s = strV(o);
47,943✔
527
  } else if (tvisnumber(o)) {
5,032✔
528
    lj_gc_check(L);
5,008✔
529
    o = index2adr(L, idx);  /* GC may move the stack. */
5,008✔
530
    s = lj_strfmt_number(L, o);
5,008✔
531
    setstrV(L, o, s);
5,008✔
532
  } else {
533
    if (len != NULL) *len = 0;
24✔
534
    return NULL;
24✔
535
  }
536
  if (len != NULL) *len = s->len;
52,951✔
537
  return strdata(s);
52,951✔
538
}
539

540
LUA_API uint32_t lua_hashstring(lua_State *L, int idx)
×
541
{
542
  TValue *o = index2adr(L, idx);
×
543
  lj_checkapi(tvisstr(o), "stack slot %d is not a string", idx);
×
544
  GCstr *s = strV(o);
×
545
  if (! strsmart(s))
×
546
    return s->hash;
×
547
  return lua_hash(strdata(s), s->len);
×
548
}
549

550
LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len)
46,543✔
551
{
552
  TValue *o = index2adr(L, idx);
46,543✔
553
  GCstr *s;
46,543✔
554
  if (LJ_LIKELY(tvisstr(o))) {
46,543✔
555
    s = strV(o);
46,543✔
556
  } else if (tvisnumber(o)) {
×
557
    lj_gc_check(L);
×
558
    o = index2adr(L, idx);  /* GC may move the stack. */
×
559
    s = lj_strfmt_number(L, o);
×
560
    setstrV(L, o, s);
×
561
  } else {
562
    lj_err_argt(L, idx, LUA_TSTRING);
×
563
  }
564
  if (len != NULL) *len = s->len;
46,543✔
565
  return strdata(s);
46,543✔
566
}
567

568
LUALIB_API const char *luaL_optlstring(lua_State *L, int idx,
71,250✔
569
                                       const char *def, size_t *len)
570
{
571
  TValue *o = index2adr(L, idx);
71,250✔
572
  GCstr *s;
71,250✔
573
  if (LJ_LIKELY(tvisstr(o))) {
71,250✔
574
    s = strV(o);
71,159✔
575
  } else if (tvisnil(o)) {
91✔
576
    if (len != NULL) *len = def ? strlen(def) : 0;
91✔
577
    return def;
91✔
578
  } else if (tvisnumber(o)) {
×
579
    lj_gc_check(L);
×
580
    o = index2adr(L, idx);  /* GC may move the stack. */
×
581
    s = lj_strfmt_number(L, o);
×
582
    setstrV(L, o, s);
×
583
  } else {
584
    lj_err_argt(L, idx, LUA_TSTRING);
×
585
  }
586
  if (len != NULL) *len = s->len;
71,159✔
587
  return strdata(s);
71,159✔
588
}
589

590
LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def,
×
591
                                const char *const lst[])
592
{
593
  ptrdiff_t i;
×
594
  const char *s = lua_tolstring(L, idx, NULL);
×
595
  if (s == NULL && (s = def) == NULL)
×
596
    lj_err_argt(L, idx, LUA_TSTRING);
×
597
  for (i = 0; lst[i]; i++)
×
598
    if (strcmp(lst[i], s) == 0)
×
599
      return (int)i;
×
600
  lj_err_argv(L, idx, LJ_ERR_INVOPTM, s);
×
601
}
602

603
LUA_API size_t lua_objlen(lua_State *L, int idx)
167✔
604
{
605
  TValue *o = index2adr(L, idx);
167✔
606
  if (tvisstr(o)) {
167✔
607
    return strV(o)->len;
165✔
608
  } else if (tvistab(o)) {
2✔
609
    return (size_t)lj_tab_len(tabV(o));
2✔
610
  } else if (tvisudata(o)) {
×
611
    return udataV(o)->len;
×
612
  } else if (tvisnumber(o)) {
×
613
    GCstr *s = lj_strfmt_number(L, o);
×
614
    setstrV(L, o, s);
×
615
    return s->len;
×
616
  } else {
617
    return 0;
618
  }
619
}
620

621
LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)
×
622
{
623
  cTValue *o = index2adr(L, idx);
×
624
  if (tvisfunc(o)) {
×
625
    BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns));
×
626
    if (op == BC_FUNCC || op == BC_FUNCCW)
×
627
      return funcV(o)->c.f;
×
628
  }
629
  return NULL;
630
}
631

632
LUA_API void *lua_touserdata(lua_State *L, int idx)
4✔
633
{
634
  cTValue *o = index2adr(L, idx);
4✔
635
  if (tvisudata(o))
4✔
636
    return uddata(udataV(o));
4✔
637
  else if (tvislightud(o))
×
638
    return lightudV(G(L), o);
×
639
  else
640
    return NULL;
641
}
642

643
LUA_API lua_State *lua_tothread(lua_State *L, int idx)
×
644
{
645
  cTValue *o = index2adr(L, idx);
×
646
  return (!tvisthread(o)) ? NULL : threadV(o);
×
647
}
648

649
LUA_API const void *lua_topointer(lua_State *L, int idx)
256✔
650
{
651
  return lj_obj_ptr(G(L), index2adr(L, idx));
256✔
652
}
653

654
/* -- Stack setters (object creation) ------------------------------------- */
655

656
LUA_API void lua_pushnil(lua_State *L)
1,604✔
657
{
658
  setnilV(L->top);
1,604✔
659
  incr_top(L);
1,604✔
660
}
1,604✔
661

662
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
283✔
663
{
664
  setnumV(L->top, n);
283✔
665
  if (LJ_UNLIKELY(tvisnan(L->top)))
283✔
666
    setnanV(L->top);  /* Canonicalize injected NaNs. */
×
667
  incr_top(L);
283✔
668
}
283✔
669

670
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
4,308,734✔
671
{
672
  setintptrV(L->top, n);
4,308,734✔
673
  incr_top(L);
4,308,734✔
674
}
4,308,734✔
675

676
LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len)
57,157✔
677
{
678
  GCstr *s;
57,157✔
679
  lj_gc_check(L);
57,157✔
680
  s = lj_str_new(L, str, len);
57,157✔
681
  setstrV(L, L->top, s);
57,157✔
682
  incr_top(L);
57,157✔
683
}
57,157✔
684

685
LUA_API void lua_pushstring(lua_State *L, const char *str)
4,435,124✔
686
{
687
  if (str == NULL) {
4,435,124✔
688
    setnilV(L->top);
41✔
689
  } else {
690
    GCstr *s;
4,435,083✔
691
    lj_gc_check(L);
4,435,083✔
692
    s = lj_str_newz(L, str);
4,435,083✔
693
    setstrV(L, L->top, s);
4,435,083✔
694
  }
695
  incr_top(L);
4,435,124✔
696
}
4,435,124✔
697

698
LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,
×
699
                                     va_list argp)
700
{
701
  lj_gc_check(L);
×
702
  return lj_strfmt_pushvf(L, fmt, argp);
×
703
}
704

705
LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...)
2,952✔
706
{
707
  const char *ret;
2,952✔
708
  va_list argp;
2,952✔
709
  lj_gc_check(L);
2,952✔
710
  va_start(argp, fmt);
2,952✔
711
  ret = lj_strfmt_pushvf(L, fmt, argp);
2,952✔
712
  va_end(argp);
2,952✔
713
  return ret;
2,952✔
714
}
715

716
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n)
7,557✔
717
{
718
  GCfunc *fn;
7,557✔
719
  lj_gc_check(L);
7,557✔
720
  lj_checkapi_slot(n);
7,557✔
721
  fn = lj_func_newC(L, (MSize)n, getcurrenv(L));
15,114✔
722
  fn->c.f = f;
7,557✔
723
  L->top -= n;
7,557✔
724
  while (n--)
7,557✔
725
    copyTV(L, &fn->c.upvalue[n], L->top+n);
8,506✔
726
  setfuncV(L, L->top, fn);
7,557✔
727
  lj_assertL(iswhite(obj2gco(fn)), "new GC object is not white");
7,557✔
728
  incr_top(L);
7,557✔
729
}
7,557✔
730

731
LUA_API void lua_pushboolean(lua_State *L, int b)
1,000,234✔
732
{
733
  setboolV(L->top, (b != 0));
1,000,234✔
734
  incr_top(L);
1,000,234✔
735
}
1,000,234✔
736

737
LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
260✔
738
{
739
#if LJ_64
740
  p = lj_lightud_intern(L, p);
260✔
741
#endif
742
  setrawlightudV(L->top, p);
258✔
743
  incr_top(L);
258✔
744
}
258✔
745

746
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
1,076,341✔
747
{
748
  lj_gc_check(L);
1,076,341✔
749
  settabV(L, L->top, lj_tab_new_ah(L, narray, nrec));
1,076,341✔
750
  incr_top(L);
1,076,341✔
751
}
1,076,341✔
752

753
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
234✔
754
{
755
  GCtab *regt = tabV(registry(L));
234✔
756
  TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname));
234✔
757
  if (tvisnil(tv)) {
234✔
758
    GCtab *mt = lj_tab_new(L, 0, 1);
234✔
759
    settabV(L, tv, mt);
234✔
760
    settabV(L, L->top++, mt);
234✔
761
    lj_gc_anybarriert(L, regt);
234✔
762
    return 1;
234✔
763
  } else {
764
    copyTV(L, L->top++, tv);
×
765
    return 0;
×
766
  }
767
}
768

769
LUA_API int lua_pushthread(lua_State *L)
4✔
770
{
771
  setthreadV(L, L->top, L);
4✔
772
  incr_top(L);
4✔
773
  return (mainthread(G(L)) == L);
4✔
774
}
775

776
LUA_API lua_State *lua_newthread(lua_State *L)
1,095✔
777
{
778
  lua_State *L1;
1,095✔
779
  lj_gc_check(L);
1,095✔
780
  L1 = lj_state_new(L);
1,095✔
781
  setthreadV(L, L->top, L1);
1,095✔
782
  incr_top(L);
1,095✔
783
  return L1;
1,095✔
784
}
785

786
LUA_API void *lua_newuserdata(lua_State *L, size_t size)
1,006,054✔
787
{
788
  GCudata *ud;
1,006,054✔
789
  lj_gc_check(L);
1,006,054✔
790
  if (size > LJ_MAX_UDATA)
1,006,054✔
791
    lj_err_msg(L, LJ_ERR_UDATAOV);
×
792
  ud = lj_udata_new(L, (MSize)size, getcurrenv(L));
2,012,108✔
793
  setudataV(L, L->top, ud);
1,006,054✔
794
  incr_top(L);
1,006,054✔
795
  return uddata(ud);
1,006,054✔
796
}
797

798
LUA_API void lua_concat(lua_State *L, int n)
26,229✔
799
{
800
  lj_checkapi_slot(n);
26,229✔
801
  if (n >= 2) {
26,229✔
802
    n--;
712✔
803
    do {
712✔
804
      TValue *top = lj_meta_cat(L, L->top-1, -n);
712✔
805
      if (top == NULL) {
712✔
806
        L->top -= n;
712✔
807
        break;
712✔
808
      }
809
      n -= (int)(L->top - top);
×
810
      L->top = top+2;
×
811
      jit_secure_call(L, top, 1+1);
×
812
      L->top -= 1+LJ_FR2;
×
813
      copyTV(L, L->top-1, L->top+LJ_FR2);
×
814
    } while (--n > 0);
×
815
  } else if (n == 0) {  /* Push empty string. */
25,517✔
816
    setstrV(L, L->top, &G(L)->strempty);
1✔
817
    incr_top(L);
1✔
818
  }
819
  /* else n == 1: nothing to do. */
820
}
26,229✔
821

822
/* -- Object getters ------------------------------------------------------ */
823

824
LUA_API void lua_gettable(lua_State *L, int idx)
118✔
825
{
826
  cTValue *t = index2adr_check(L, idx);
118✔
827
  cTValue *v = lj_meta_tget(L, t, L->top-1);
118✔
828
  if (v == NULL) {
118✔
829
    L->top += 2;
2✔
830
    jit_secure_call(L, L->top-2, 1+1);
2✔
831
    L->top -= 2+LJ_FR2;
2✔
832
    v = L->top+1+LJ_FR2;
2✔
833
  }
834
  copyTV(L, L->top-1, v);
118✔
835
}
118✔
836

837
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
8,256✔
838
{
839
  cTValue *v, *t = index2adr_check(L, idx);
8,256✔
840
  TValue key;
8,256✔
841
  setstrV(L, &key, lj_str_newz(L, k));
8,256✔
842
  v = lj_meta_tget(L, t, &key);
8,256✔
843
  if (v == NULL) {
8,256✔
844
    L->top += 2;
×
845
    jit_secure_call(L, L->top-2, 1+1);
×
846
    L->top -= 2+LJ_FR2;
×
847
    v = L->top+1+LJ_FR2;
×
848
  }
849
  copyTV(L, L->top, v);
8,256✔
850
  incr_top(L);
8,256✔
851
}
8,256✔
852

853
LUA_API void lua_rawget(lua_State *L, int idx)
4,297,072✔
854
{
855
  cTValue *t = index2adr(L, idx);
4,297,072✔
856
  lj_checkapi(tvistab(t), "stack slot %d is not a table", idx);
4,297,072✔
857
  copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1));
4,297,072✔
858
}
4,297,072✔
859

860
LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
2,295,432✔
861
{
862
  cTValue *v, *t = index2adr(L, idx);
2,295,432✔
863
  lj_checkapi(tvistab(t), "stack slot %d is not a table", idx);
2,295,432✔
864
  v = lj_tab_getint(tabV(t), n);
2,295,432✔
865
  if (v) {
2,295,432✔
866
    copyTV(L, L->top, v);
2,295,374✔
867
  } else {
868
    setnilV(L->top);
58✔
869
  }
870
  incr_top(L);
2,295,432✔
871
}
2,295,432✔
872

873
LUA_API int lua_getmetatable(lua_State *L, int idx)
1,928✔
874
{
875
  cTValue *o = index2adr(L, idx);
1,928✔
876
  GCtab *mt = NULL;
1,928✔
877
  if (tvistab(o))
1,928✔
878
    mt = tabref(tabV(o)->metatable);
14✔
879
  else if (tvisudata(o))
1,914✔
880
    mt = tabref(udataV(o)->metatable);
1,909✔
881
  else
882
    mt = tabref(basemt_obj(G(L), o));
5✔
883
  if (mt == NULL)
1,928✔
884
    return 0;
885
  settabV(L, L->top, mt);
1,916✔
886
  incr_top(L);
1,916✔
887
  return 1;
888
}
889

890
LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field)
1✔
891
{
892
  if (lua_getmetatable(L, idx)) {
1✔
893
    cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field));
×
894
    if (tv && !tvisnil(tv)) {
×
895
      copyTV(L, L->top-1, tv);
×
896
      return 1;
×
897
    }
898
    L->top--;
×
899
  }
900
  return 0;
901
}
902

903
LUA_API void lua_getfenv(lua_State *L, int idx)
14✔
904
{
905
  cTValue *o = index2adr_check(L, idx);
14✔
906
  if (tvisfunc(o)) {
14✔
907
    settabV(L, L->top, tabref(funcV(o)->c.env));
8✔
908
  } else if (tvisudata(o)) {
6✔
909
    settabV(L, L->top, tabref(udataV(o)->env));
×
910
  } else if (tvisthread(o)) {
6✔
911
    settabV(L, L->top, tabref(threadV(o)->env));
5✔
912
  } else {
913
    setnilV(L->top);
1✔
914
  }
915
  incr_top(L);
14✔
916
}
14✔
917

918
LUA_API int lua_next(lua_State *L, int idx)
18✔
919
{
920
  cTValue *t = index2adr(L, idx);
18✔
921
  int more;
18✔
922
  lj_checkapi(tvistab(t), "stack slot %d is not a table", idx);
18✔
923
  more = lj_tab_next(L, tabV(t), L->top-1);
18✔
924
  if (more) {
18✔
925
    incr_top(L);  /* Return new key and value slot. */
9✔
926
  } else {  /* End of traversal. */
927
    L->top--;  /* Remove key slot. */
9✔
928
  }
929
  return more;
18✔
930
}
931

932
LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n)
12✔
933
{
934
  TValue *val;
12✔
935
  GCobj *o;
12✔
936
  const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val, &o);
12✔
937
  if (name) {
12✔
938
    copyTV(L, L->top, val);
7✔
939
    incr_top(L);
7✔
940
  }
941
  return name;
12✔
942
}
943

944
LUA_API void *lua_upvalueid(lua_State *L, int idx, int n)
×
945
{
946
  GCfunc *fn = funcV(index2adr(L, idx));
×
947
  n--;
×
948
  lj_checkapi((uint32_t)n < fn->l.nupvalues, "bad upvalue %d", n);
×
949
  return isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) :
×
950
                         (void *)&fn->c.upvalue[n];
951
}
952

953
LUA_API void lua_upvaluejoin(lua_State *L, int idx1, int n1, int idx2, int n2)
×
954
{
955
  GCfunc *fn1 = funcV(index2adr(L, idx1));
×
956
  GCfunc *fn2 = funcV(index2adr(L, idx2));
×
957
  n1--; n2--;
×
958
  lj_checkapi(isluafunc(fn1), "stack slot %d is not a Lua function", idx1);
×
959
  lj_checkapi(isluafunc(fn2), "stack slot %d is not a Lua function", idx2);
×
960
  lj_checkapi((uint32_t)n1 < fn1->l.nupvalues, "bad upvalue %d", n1+1);
×
961
  lj_checkapi((uint32_t)n2 < fn2->l.nupvalues, "bad upvalue %d", n2+1);
×
962
  setgcrefr(fn1->l.uvptr[n1], fn2->l.uvptr[n2]);
×
963
  lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1]));
×
964
}
×
965

966
LUALIB_API void *luaL_testudata(lua_State *L, int idx, const char *tname)
15✔
967
{
968
  cTValue *o = index2adr(L, idx);
15✔
969
  if (tvisudata(o)) {
15✔
970
    GCudata *ud = udataV(o);
15✔
971
    cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname));
15✔
972
    if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable))
15✔
973
      return uddata(ud);
15✔
974
  }
975
  return NULL;  /* value is not a userdata with a metatable */
976
}
977

978
LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)
15✔
979
{
980
  void *p = luaL_testudata(L, idx, tname);
15✔
981
  if (!p) lj_err_argtype(L, idx, tname);
15✔
982
  return p;
15✔
983
}
984

985
/* -- Object setters ------------------------------------------------------ */
986

987
LUA_API void lua_settable(lua_State *L, int idx)
3,751✔
988
{
989
  TValue *o;
3,751✔
990
  cTValue *t = index2adr_check(L, idx);
3,751✔
991
  lj_checkapi_slot(2);
3,751✔
992
  o = lj_meta_tset(L, t, L->top-2);
3,751✔
993
  if (o) {
3,751✔
994
    /* NOBARRIER: lj_meta_tset ensures the table is not black. */
995
    L->top -= 2;
3,751✔
996
    copyTV(L, o, L->top+1);
3,751✔
997
  } else {
998
    TValue *base = L->top;
×
999
    copyTV(L, base+2, base-3-2*LJ_FR2);
×
1000
    L->top = base+3;
×
1001
    jit_secure_call(L, base, 0+1);
×
1002
    L->top -= 3+LJ_FR2;
×
1003
  }
1004
}
3,751✔
1005

1006
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
152,832✔
1007
{
1008
  TValue *o;
152,832✔
1009
  TValue key;
152,832✔
1010
  cTValue *t = index2adr_check(L, idx);
152,832✔
1011
  lj_checkapi_slot(1);
152,832✔
1012
  setstrV(L, &key, lj_str_newz(L, k));
152,832✔
1013
  o = lj_meta_tset(L, t, &key);
152,832✔
1014
  if (o) {
152,832✔
1015
    /* NOBARRIER: lj_meta_tset ensures the table is not black. */
1016
    copyTV(L, o, --L->top);
152,832✔
1017
  } else {
1018
    TValue *base = L->top;
×
1019
    copyTV(L, base+2, base-3-2*LJ_FR2);
×
1020
    L->top = base+3;
×
1021
    jit_secure_call(L, base, 0+1);
×
1022
    L->top -= 2+LJ_FR2;
×
1023
  }
1024
}
152,832✔
1025

1026
LUA_API void lua_rawset(lua_State *L, int idx)
1,000,293✔
1027
{
1028
  GCtab *t = tabV(index2adr(L, idx));
1,000,293✔
1029
  TValue *dst, *key;
1,000,293✔
1030
  lj_checkapi_slot(2);
1,000,293✔
1031
  key = L->top-2;
1,000,293✔
1032
  dst = lj_tab_set(L, t, key);
1,000,293✔
1033
  copyTV(L, dst, key+1);
1,000,292✔
1034
  lj_gc_anybarriert(L, t);
1,000,292✔
1035
  L->top = key;
1,000,292✔
1036
}
1,000,292✔
1037

1038
LUA_API void lua_rawseti(lua_State *L, int idx, int n)
1,014,319✔
1039
{
1040
  GCtab *t = tabV(index2adr(L, idx));
1,014,319✔
1041
  TValue *dst, *src;
1,014,319✔
1042
  lj_checkapi_slot(1);
1,014,319✔
1043
  dst = lj_tab_setint(L, t, n);
1,014,319✔
1044
  src = L->top-1;
1,014,319✔
1045
  copyTV(L, dst, src);
1,014,319✔
1046
  lj_gc_barriert(L, t, dst);
1,014,319✔
1047
  L->top = src;
1,014,319✔
1048
}
1,014,319✔
1049

1050
LUA_API int lua_setmetatable(lua_State *L, int idx)
1,001,022✔
1051
{
1052
  global_State *g;
1,001,022✔
1053
  GCtab *mt;
1,001,022✔
1054
  cTValue *o = index2adr_check(L, idx);
1,001,022✔
1055
  lj_checkapi_slot(1);
1,001,022✔
1056
  if (tvisnil(L->top-1)) {
1,001,022✔
1057
    mt = NULL;
1058
  } else {
1059
    lj_checkapi(tvistab(L->top-1), "top stack slot is not a table");
1,001,016✔
1060
    mt = tabV(L->top-1);
1,001,016✔
1061
  }
1062
  g = G(L);
1,001,022✔
1063
  if (tvistab(o)) {
1,001,022✔
1064
    setgcref(tabV(o)->metatable, obj2gco(mt));
10✔
1065
    if (mt)
10✔
1066
      lj_gc_objbarriert(L, tabV(o), mt);
10✔
1067
  } else if (tvisudata(o)) {
1,001,012✔
1068
    setgcref(udataV(o)->metatable, obj2gco(mt));
1,000,989✔
1069
    if (mt)
1,000,989✔
1070
      lj_gc_objbarrier(L, udataV(o), mt);
1,000,989✔
1071
  } else {
1072
    /* Flush cache, since traces specialize to basemt. But not during __gc. */
1073
    if (lj_trace_flushall(L))
23✔
1074
      lj_err_caller(L, LJ_ERR_NOGCMM);
×
1075
    if (tvisbool(o)) {
23✔
1076
      /* NOBARRIER: basemt is a GC root. */
1077
      setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt));
4✔
1078
      setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt));
4✔
1079
    } else {
1080
      /* NOBARRIER: basemt is a GC root. */
1081
      setgcref(basemt_obj(g, o), obj2gco(mt));
19✔
1082
    }
1083
  }
1084
  L->top--;
1,001,022✔
1085
  return 1;
1,001,022✔
1086
}
1087

1088
LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname)
16✔
1089
{
1090
  lua_getfield(L, LUA_REGISTRYINDEX, tname);
16✔
1091
  lua_setmetatable(L, -2);
16✔
1092
}
16✔
1093

1094
LUA_API int lua_setfenv(lua_State *L, int idx)
19✔
1095
{
1096
  cTValue *o = index2adr_check(L, idx);
19✔
1097
  GCtab *t;
19✔
1098
  lj_checkapi_slot(1);
19✔
1099
  lj_checkapi(tvistab(L->top-1), "top stack slot is not a table");
19✔
1100
  t = tabV(L->top-1);
19✔
1101
  if (tvisfunc(o)) {
19✔
1102
    setgcref(funcV(o)->c.env, obj2gco(t));
14✔
1103
  } else if (tvisudata(o)) {
5✔
1104
    setgcref(udataV(o)->env, obj2gco(t));
×
1105
  } else if (tvisthread(o)) {
5✔
1106
    setgcref(threadV(o)->env, obj2gco(t));
4✔
1107
  } else {
1108
    L->top--;
1✔
1109
    return 0;
1✔
1110
  }
1111
  lj_gc_objbarrier(L, gcV(o), t);
18✔
1112
  L->top--;
18✔
1113
  return 1;
18✔
1114
}
1115

1116
LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n)
9✔
1117
{
1118
  cTValue *f = index2adr(L, idx);
9✔
1119
  TValue *val;
9✔
1120
  GCobj *o;
9✔
1121
  const char *name;
9✔
1122
  lj_checkapi_slot(1);
9✔
1123
  name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val, &o);
9✔
1124
  if (name) {
9✔
1125
    L->top--;
5✔
1126
    copyTV(L, val, L->top);
5✔
1127
    lj_gc_barrier(L, o, L->top);
5✔
1128
  }
1129
  return name;
9✔
1130
}
1131

1132
/* -- Calls --------------------------------------------------------------- */
1133

1134
#if LJ_FR2
1135
static TValue *api_call_base(lua_State *L, int nargs)
5,199,905✔
1136
{
1137
  TValue *o = L->top, *base = o - nargs;
5,199,905✔
1138
  L->top = o+1;
5,199,905✔
1139
  for (; o > base; o--) copyTV(L, o, o-1);
15,582,381✔
1140
  setnilV(o);
5,199,905✔
1141
  return o+1;
5,199,905✔
1142
}
1143
#else
1144
#define api_call_base(L, nargs)        (L->top - (nargs))
1145
#endif
1146

1147
LUA_API void lua_call(lua_State *L, int nargs, int nresults)
5,199,099✔
1148
{
1149
  lj_checkapi(L->status == LUA_OK || L->status == LUA_ERRERR,
5,199,099✔
1150
              "thread called in wrong state %d", L->status);
1151
  lj_checkapi_slot(nargs+1);
5,199,099✔
1152
  jit_secure_call(L, api_call_base(L, nargs), nresults+1);
10,398,198✔
1153
}
5,199,095✔
1154

1155
LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
806✔
1156
{
1157
  global_State *g = G(L);
806✔
1158
  uint8_t oldh = hook_save(g);
806✔
1159
  ptrdiff_t ef;
806✔
1160
  int status;
806✔
1161
  lj_checkapi(L->status == LUA_OK || L->status == LUA_ERRERR,
806✔
1162
              "thread called in wrong state %d", L->status);
1163
  lj_checkapi_slot(nargs+1);
806✔
1164
  if (errfunc == 0) {
806✔
1165
    ef = 0;
1166
  } else {
1167
    cTValue *o = index2adr_stack(L, errfunc);
451✔
1168
    ef = savestack(L, o);
451✔
1169
  }
1170
  /* Forbid Lua world re-entrancy while running the trace */
1171
  if (tvref(g->jit_base)) {
806✔
1172
    setstrV(L, L->top++, lj_err_str(L, LJ_ERR_JITCALL));
×
1173
    if (g->panic) g->panic(L);
×
1174
    exit(EXIT_FAILURE);
×
1175
  }
1176
  lj_trace_abort(g);  /* Never record across Lua VM entrance */
806✔
1177
  status = lj_vm_pcall(L, api_call_base(L, nargs), nresults+1, ef);
1,612✔
1178
  if (status) hook_restore(g, oldh);
706✔
1179
  return status;
706✔
1180
}
1181

1182
static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud)
234✔
1183
{
1184
  GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L));
468✔
1185
  TValue *top = L->top;
234✔
1186
  fn->c.f = func;
234✔
1187
  setfuncV(L, top++, fn);
234✔
1188
  if (LJ_FR2) setnilV(top++);
234✔
1189
#if LJ_64
1190
  ud = lj_lightud_intern(L, ud);
234✔
1191
#endif
1192
  setrawlightudV(top++, ud);
234✔
1193
  cframe_nres(L->cframe) = 1+0;  /* Zero results. */
234✔
1194
  L->top = top;
234✔
1195
  return top-1;  /* Now call the newly allocated C function. */
234✔
1196
}
1197

1198
LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
234✔
1199
{
1200
  global_State *g = G(L);
234✔
1201
  uint8_t oldh = hook_save(g);
234✔
1202
  int status;
234✔
1203
  lj_checkapi(L->status == LUA_OK || L->status == LUA_ERRERR,
234✔
1204
              "thread called in wrong state %d", L->status);
1205
  /* Forbid Lua world re-entrancy while running the trace */
1206
  if (tvref(g->jit_base)) {
234✔
1207
    setstrV(L, L->top++, lj_err_str(L, LJ_ERR_JITCALL));
×
1208
    if (g->panic) g->panic(L);
×
1209
    exit(EXIT_FAILURE);
×
1210
  }
1211
  lj_trace_abort(g);  /* Never record across Lua VM entrance */
234✔
1212
  status = lj_vm_cpcall(L, func, ud, cpcall);
234✔
1213
  if (status) hook_restore(g, oldh);
134✔
1214
  return status;
134✔
1215
}
1216

1217
LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field)
1✔
1218
{
1219
  if (luaL_getmetafield(L, idx, field)) {
1✔
1220
    TValue *top = L->top--;
×
1221
    if (LJ_FR2) setnilV(top++);
×
1222
    copyTV(L, top++, index2adr(L, idx));
×
1223
    L->top = top;
×
1224
    jit_secure_call(L, top-1, 1+1);
×
1225
    return 1;
×
1226
  }
1227
  return 0;
1228
}
1229

1230
/* -- Coroutine yield and resume ------------------------------------------ */
1231

1232
LUA_API int lua_isyieldable(lua_State *L)
×
1233
{
1234
  return cframe_canyield(L->cframe);
×
1235
}
1236

1237
LUA_API int lua_yield(lua_State *L, int nresults)
2✔
1238
{
1239
  void *cf = L->cframe;
2✔
1240
  global_State *g = G(L);
2✔
1241
  if (cframe_canyield(cf)) {
2✔
1242
    cf = cframe_raw(cf);
2✔
1243
    if (!hook_active(g)) {  /* Regular yield: move results down if needed. */
2✔
1244
      cTValue *f = L->top - nresults;
×
1245
      if (f > L->base) {
×
1246
        TValue *t = L->base;
1247
        while (--nresults >= 0) copyTV(L, t++, f++);
×
1248
        L->top = t;
×
1249
      }
1250
      L->cframe = NULL;
×
1251
      L->status = LUA_YIELD;
×
1252
      return -1;
×
1253
    } else {  /* Yield from hook: add a pseudo-frame. */
1254
      TValue *top = L->top;
2✔
1255
      hook_leave(g);
2✔
1256
      (top++)->u64 = cframe_multres(cf);
2✔
1257
      setcont(top, lj_cont_hook);
2✔
1258
      if (LJ_FR2) top++;
2✔
1259
      setframe_pc(top, cframe_pc(cf)-1);
2✔
1260
      top++;
2✔
1261
      setframe_gc(top, obj2gco(L), LJ_TTHREAD);
2✔
1262
      if (LJ_FR2) top++;
2✔
1263
      setframe_ftsz(top, ((char *)(top+1)-(char *)L->base)+FRAME_CONT);
2✔
1264
      L->top = L->base = top+1;
2✔
1265
#if ((defined(__GNUC__) || defined(__clang__)) && (LJ_TARGET_X64 || defined(LUAJIT_UNWIND_EXTERNAL)) && !LJ_NO_UNWIND) || LJ_TARGET_WINDOWS
1266
      lj_err_throw(L, LUA_YIELD);
2✔
1267
#else
1268
      L->cframe = NULL;
1269
      L->status = LUA_YIELD;
1270
      lj_vm_unwind_c(cf, LUA_YIELD);
1271
#endif
1272
    }
1273
  }
1274
  lj_err_msg(L, LJ_ERR_CYIELD);
×
1275
  return 0;  /* unreachable */
1276
}
1277

1278
LUA_API int lua_resume(lua_State *L, int nargs)
×
1279
{
1280
  if (L->cframe == NULL && L->status <= LUA_YIELD)
×
1281
    return lj_vm_resume(L,
×
1282
      L->status == LUA_OK ? api_call_base(L, nargs) : L->top - nargs,
×
1283
      0, 0);
1284
  L->top = L->base;
×
1285
  setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP));
×
1286
  incr_top(L);
×
1287
  return LUA_ERRRUN;
1288
}
1289

1290
/* -- GC and memory management -------------------------------------------- */
1291

1292
LUA_API int lua_gc(lua_State *L, int what, int data)
1,804✔
1293
{
1294
  global_State *g = G(L);
1,804✔
1295
  int res = 0;
1,804✔
1296
  switch (what) {
1,804✔
1297
  case LUA_GCSTOP:
245✔
1298
    g->gc.threshold = LJ_MAX_MEM;
245✔
1299
    break;
245✔
1300
  case LUA_GCRESTART:
234✔
1301
    g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total;
234✔
1302
    break;
234✔
1303
  case LUA_GCCOLLECT:
889✔
1304
    lj_gc_fullgc(L);
889✔
1305
    break;
889✔
1306
  case LUA_GCCOUNT:
×
1307
    res = (int)(g->gc.total >> 10);
×
1308
    break;
×
1309
  case LUA_GCCOUNTB:
×
1310
    res = (int)(g->gc.total & 0x3ff);
×
1311
    break;
×
1312
  case LUA_GCSTEP: {
430✔
1313
    GCSize a = (GCSize)data << 10;
430✔
1314
    g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0;
430✔
1315
    while (g->gc.total >= g->gc.threshold)
984✔
1316
      if (lj_gc_step(L) > 0) {
561✔
1317
        res = 1;
1318
        break;
1319
      }
1320
    break;
1321
  }
1322
  case LUA_GCSETPAUSE:
2✔
1323
    res = (int)(g->gc.pause);
2✔
1324
    g->gc.pause = (MSize)data;
2✔
1325
    break;
2✔
1326
  case LUA_GCSETSTEPMUL:
4✔
1327
    res = (int)(g->gc.stepmul);
4✔
1328
    g->gc.stepmul = (MSize)data;
4✔
1329
    break;
4✔
1330
  case LUA_GCISRUNNING:
×
1331
    res = (g->gc.threshold != LJ_MAX_MEM);
×
1332
    break;
×
1333
  default:
1334
    res = -1;  /* Invalid option. */
1335
  }
1336
  return res;
1,802✔
1337
}
1338

1339
LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud)
9✔
1340
{
1341
  global_State *g = G(L);
9✔
1342
  if (ud) *ud = g->allocd;
9✔
1343
  return g->allocf;
9✔
1344
}
1345

1346
LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)
18✔
1347
{
1348
  global_State *g = G(L);
18✔
1349
  g->allocd = ud;
18✔
1350
  g->allocf = f;
18✔
1351
}
18✔
1352

1353
LUA_API uint32_t lua_hash(const char *str, uint32_t len)
8,247,196✔
1354
{
1355
  uint32_t h = len, a, b;
8,247,196✔
1356
  if (len >= 4) {
8,247,196✔
1357
    a = lj_getu32(str);
6,432,655✔
1358
    h ^= lj_getu32(str+len-4);
6,432,655✔
1359
    b = lj_getu32(str+(len>>1)-2);
6,432,655✔
1360
    h ^= b; h -= lj_rol(b, 14);
6,432,655✔
1361
    b += lj_getu32(str+(len>>2)-1);
6,432,655✔
1362
  } else if (len > 0) {
1,814,541✔
1363
    a = *str;
1,814,541✔
1364
    h ^= *(str+len-1);
1,814,541✔
1365
    b = *(str+(len>>1));
1,814,541✔
1366
    h ^= b; h -= lj_rol(b, 14);
1,814,541✔
1367
  } else {
1368
    return 0;
1369
  }
1370
  a ^= h; a -= lj_rol(h, 11);
8,247,196✔
1371
  b ^= a; b -= lj_rol(a, 25);
8,247,196✔
1372
  h ^= b; h -= lj_rol(b, 16);
8,247,196✔
1373
  return h;
8,247,196✔
1374
}
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