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

tarantool / luajit / 5785127203

07 Aug 2023 12:31PM UTC coverage: 84.248% (-0.03%) from 84.282%
5785127203

push

github

ligurio
ci: support coveralls

The patch adds an action that installs gcovr and a workflow that runs
regression test suites, produces a summary of current code coverage and
sends code coverage data to Coveralls. Coveralls is a web service that
lets you inspect every detail of your coverage. See Tarantool's LuaJIT
page on Coveralls [1].

1. https://coveralls.io/github/tarantool/luajit

5123 of 6002 branches covered (85.35%)

Branch coverage included in aggregate %.

19656 of 23410 relevant lines covered (83.96%)

244602.24 hits per line

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

67.64
/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 api_checknelems(L, n)                api_check(L, (n) <= (L->top - L->base))
32
#define api_checkvalidindex(L, i)        api_check(L, (i) != niltv(L))
33

34
static TValue *index2adr(lua_State *L, int idx)
579,474✔
35
{
36
  if (idx > 0) {
579,474✔
37
    TValue *o = L->base + (idx - 1);
346,954✔
38
    return o < L->top ? o : niltv(L);
346,954✔
39
  } else if (idx > LUA_REGISTRYINDEX) {
232,520✔
40
    api_check(L, idx != 0 && -idx <= L->top - L->base);
225,947✔
41
    return L->top + idx;
225,947✔
42
  } else if (idx == LUA_GLOBALSINDEX) {
6,573✔
43
    TValue *o = &G(L)->tmptv;
2,408✔
44
    settabV(L, o, tabref(L->env));
2,408✔
45
    return o;
2,408✔
46
  } else if (idx == LUA_REGISTRYINDEX) {
4,165✔
47
    return registry(L);
3,511✔
48
  } else {
49
    GCfunc *fn = curr_func(L);
654✔
50
    api_check(L, fn->c.gct == ~LJ_TFUNC && !isluafunc(fn));
654✔
51
    if (idx == LUA_ENVIRONINDEX) {
654✔
52
      TValue *o = &G(L)->tmptv;
652✔
53
      settabV(L, o, tabref(fn->c.env));
652✔
54
      return o;
652✔
55
    } else {
56
      idx = LUA_GLOBALSINDEX - idx;
2✔
57
      return idx <= fn->c.nupvalues ? &fn->c.upvalue[idx-1] : niltv(L);
2✔
58
    }
59
  }
60
}
61

62
static TValue *stkindex2adr(lua_State *L, int idx)
8,305✔
63
{
64
  if (idx > 0) {
8,305✔
65
    TValue *o = L->base + (idx - 1);
733✔
66
    return o < L->top ? o : niltv(L);
733✔
67
  } else {
68
    api_check(L, idx != 0 && -idx <= L->top - L->base);
7,572✔
69
    return L->top + idx;
7,572✔
70
  }
71
}
72

73
static GCtab *getcurrenv(lua_State *L)
5,257✔
74
{
75
  GCfunc *fn = curr_func(L);
5,257✔
76
  return fn->c.gct == ~LJ_TFUNC ? tabref(fn->c.env) : tabref(L->env);
5,257✔
77
}
78

79
static void jit_secure_call(lua_State *L, TValue *base, int nres) {
2,352✔
80
  global_State *g = G(L);
2,352✔
81
  /* Forbid Lua world re-entrancy while running the trace */
82
  if (tvref(g->jit_base)) {
2,352✔
83
    setstrV(L, L->top++, lj_err_str(L, LJ_ERR_JITCALL));
×
84
    if (g->panic) g->panic(L);
×
85
    exit(EXIT_FAILURE);
×
86
  }
87
  lj_trace_abort(g);  /* Never record across Lua VM entrance */
2,352✔
88
  lj_vm_call(L, base, nres);
2,352✔
89
}
2,351✔
90

91
/* -- Miscellaneous API functions ----------------------------------------- */
92

93
LUA_API int lua_status(lua_State *L)
×
94
{
95
  return L->status;
×
96
}
97

98
LUA_API int lua_checkstack(lua_State *L, int size)
890✔
99
{
100
  if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) {
890✔
101
    return 0;  /* Stack overflow. */
102
  } else if (size > 0) {
890✔
103
    lj_state_checkstack(L, (MSize)size);
636✔
104
  }
105
  return 1;
106
}
107

108
LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg)
880✔
109
{
110
  if (!lua_checkstack(L, size))
880✔
111
    lj_err_callerv(L, LJ_ERR_STKOVM, msg);
×
112
}
880✔
113

114
LUA_API void lua_xmove(lua_State *from, lua_State *to, int n)
1✔
115
{
116
  TValue *f, *t;
1✔
117
  if (from == to) return;
1✔
118
  api_checknelems(from, n);
×
119
  api_check(from, G(from) == G(to));
×
120
  lj_state_checkstack(to, (MSize)n);
×
121
  f = from->top;
×
122
  t = to->top = to->top + n;
×
123
  while (--n >= 0) copyTV(to, --t, --f);
×
124
  from->top = f;
×
125
}
126

127
LUA_API const lua_Number *lua_version(lua_State *L)
×
128
{
129
  static const lua_Number version = LUA_VERSION_NUM;
×
130
  UNUSED(L);
×
131
  return &version;
×
132
}
133

134
/* -- Stack manipulation -------------------------------------------------- */
135

136
LUA_API int lua_gettop(lua_State *L)
275✔
137
{
138
  return (int)(L->top - L->base);
275✔
139
}
140

141
LUA_API void lua_settop(lua_State *L, int idx)
66,333✔
142
{
143
  if (idx >= 0) {
66,333✔
144
    api_check(L, idx <= tvref(L->maxstack) - L->base);
7,440✔
145
    if (L->base + idx > L->top) {
7,440✔
146
      if (L->base + idx >= tvref(L->maxstack))
7,146✔
147
        lj_state_growstack(L, (MSize)idx - (MSize)(L->top - L->base));
×
148
      do { setnilV(L->top++); } while (L->top < L->base + idx);
7,596✔
149
    } else {
150
      L->top = L->base + idx;
294✔
151
    }
152
  } else {
153
    api_check(L, -(idx+1) <= (L->top - L->base));
58,893✔
154
    L->top += idx+1;  /* Shrinks top (idx < 0). */
58,893✔
155
  }
156
}
66,333✔
157

158
LUA_API void lua_remove(lua_State *L, int idx)
7,582✔
159
{
160
  TValue *p = stkindex2adr(L, idx);
7,582✔
161
  api_checkvalidindex(L, p);
162
  while (++p < L->top) copyTV(L, p-1, p);
14,894✔
163
  L->top--;
7,582✔
164
}
7,582✔
165

166
LUA_API void lua_insert(lua_State *L, int idx)
483✔
167
{
168
  TValue *q, *p = stkindex2adr(L, idx);
483✔
169
  api_checkvalidindex(L, p);
483✔
170
  for (q = L->top; q > p; q--) copyTV(L, q, q-1);
1,412✔
171
  copyTV(L, p, L->top);
483✔
172
}
483✔
173

174
static void copy_slot(lua_State *L, TValue *f, int idx)
125✔
175
{
176
  if (idx == LUA_GLOBALSINDEX) {
125✔
177
    api_check(L, tvistab(f));
×
178
    /* NOBARRIER: A thread (i.e. L) is never black. */
179
    setgcref(L->env, obj2gco(tabV(f)));
×
180
  } else if (idx == LUA_ENVIRONINDEX) {
125✔
181
    GCfunc *fn = curr_func(L);
125✔
182
    if (fn->c.gct != ~LJ_TFUNC)
125✔
183
      lj_err_msg(L, LJ_ERR_NOENV);
×
184
    api_check(L, tvistab(f));
125✔
185
    setgcref(fn->c.env, obj2gco(tabV(f)));
125✔
186
    lj_gc_barrier(L, fn, f);
125✔
187
  } else {
188
    TValue *o = index2adr(L, idx);
×
189
    api_checkvalidindex(L, o);
×
190
    copyTV(L, o, f);
×
191
    if (idx < LUA_GLOBALSINDEX)  /* Need a barrier for upvalues. */
×
192
      lj_gc_barrier(L, curr_func(L), f);
×
193
  }
194
}
125✔
195

196
LUA_API void lua_replace(lua_State *L, int idx)
×
197
{
198
  api_checknelems(L, 1);
×
199
  copy_slot(L, L->top - 1, idx);
×
200
  L->top--;
×
201
}
×
202

203
LUA_API void lua_copy(lua_State *L, int fromidx, int toidx)
125✔
204
{
205
  copy_slot(L, index2adr(L, fromidx), toidx);
125✔
206
}
125✔
207

208
LUA_API void lua_pushvalue(lua_State *L, int idx)
16,938✔
209
{
210
  copyTV(L, L->top, index2adr(L, idx));
16,938✔
211
  incr_top(L);
16,938✔
212
}
16,938✔
213

214
/* -- Stack getters ------------------------------------------------------- */
215

216
LUA_API int lua_type(lua_State *L, int idx)
102,142✔
217
{
218
  cTValue *o = index2adr(L, idx);
102,142✔
219
  if (tvisnumber(o)) {
102,142✔
220
    return LUA_TNUMBER;
221
#if LJ_64 && !LJ_GC64
222
  } else if (tvislightud(o)) {
223
    return LUA_TLIGHTUSERDATA;
224
#endif
225
  } else if (o == niltv(L)) {
102,138✔
226
    return LUA_TNONE;
227
  } else {  /* Magic internal/external tag conversion. ORDER LJ_T */
228
    uint32_t t = ~itype(o);
102,135✔
229
#if LJ_64
230
    int tt = (int)((U64x(75a06,98042110) >> 4*t) & 15u);
102,135✔
231
#else
232
    int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u);
233
#endif
234
    lua_assert(tt != LUA_TNIL || tvisnil(o));
102,135✔
235
    return tt;
102,135✔
236
  }
237
}
238

239
LUALIB_API void luaL_checktype(lua_State *L, int idx, int tt)
8✔
240
{
241
  if (lua_type(L, idx) != tt)
8✔
242
    lj_err_argt(L, idx, tt);
×
243
}
8✔
244

245
LUALIB_API void luaL_checkany(lua_State *L, int idx)
×
246
{
247
  if (index2adr(L, idx) == niltv(L))
×
248
    lj_err_arg(L, idx, LJ_ERR_NOVAL);
×
249
}
×
250

251
LUA_API const char *lua_typename(lua_State *L, int t)
1✔
252
{
253
  UNUSED(L);
1✔
254
  return lj_obj_typename[t+1];
1✔
255
}
256

257
LUA_API int lua_iscfunction(lua_State *L, int idx)
2✔
258
{
259
  cTValue *o = index2adr(L, idx);
2✔
260
  return tvisfunc(o) && !isluafunc(funcV(o));
2✔
261
}
262

263
LUA_API int lua_isnumber(lua_State *L, int idx)
36✔
264
{
265
  cTValue *o = index2adr(L, idx);
36✔
266
  TValue tmp;
36✔
267
  return (tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), &tmp)));
36✔
268
}
269

270
LUA_API int lua_isstring(lua_State *L, int idx)
348✔
271
{
272
  cTValue *o = index2adr(L, idx);
348✔
273
  return (tvisstr(o) || tvisnumber(o));
348✔
274
}
275

276
LUA_API int lua_isuserdata(lua_State *L, int idx)
×
277
{
278
  cTValue *o = index2adr(L, idx);
×
279
  return (tvisudata(o) || tvislightud(o));
×
280
}
281

282
LUA_API int lua_rawequal(lua_State *L, int idx1, int idx2)
×
283
{
284
  cTValue *o1 = index2adr(L, idx1);
×
285
  cTValue *o2 = index2adr(L, idx2);
×
286
  return (o1 == niltv(L) || o2 == niltv(L)) ? 0 : lj_obj_equal(o1, o2);
×
287
}
288

289
LUA_API int lua_equal(lua_State *L, int idx1, int idx2)
×
290
{
291
  cTValue *o1 = index2adr(L, idx1);
×
292
  cTValue *o2 = index2adr(L, idx2);
×
293
  if (tvisint(o1) && tvisint(o2)) {
×
294
    return intV(o1) == intV(o2);
295
  } else if (tvisnumber(o1) && tvisnumber(o2)) {
×
296
    return numberVnum(o1) == numberVnum(o2);
×
297
  } else if (itype(o1) != itype(o2)) {
×
298
    return 0;
299
  } else if (tvispri(o1)) {
×
300
    return o1 != niltv(L) && o2 != niltv(L);
×
301
#if LJ_64 && !LJ_GC64
302
  } else if (tvislightud(o1)) {
303
    return o1->u64 == o2->u64;
304
#endif
305
  } else if (gcrefeq(o1->gcr, o2->gcr)) {
×
306
    return 1;
307
  } else if (!tvistabud(o1)) {
×
308
    return 0;
309
  } else {
310
    TValue *base = lj_meta_equal(L, gcV(o1), gcV(o2), 0);
×
311
    if ((uintptr_t)base <= 1) {
×
312
      return (int)(uintptr_t)base;
×
313
    } else {
314
      L->top = base+2;
×
315
      jit_secure_call(L, base, 1+1);
×
316
      L->top -= 2+LJ_FR2;
×
317
      return tvistruecond(L->top+1+LJ_FR2);
×
318
    }
319
  }
320
}
321

322
LUA_API int lua_lessthan(lua_State *L, int idx1, int idx2)
91,608✔
323
{
324
  cTValue *o1 = index2adr(L, idx1);
91,608✔
325
  cTValue *o2 = index2adr(L, idx2);
91,608✔
326
  if (o1 == niltv(L) || o2 == niltv(L)) {
91,608✔
327
    return 0;
328
  } else if (tvisint(o1) && tvisint(o2)) {
91,608✔
329
    return intV(o1) < intV(o2);
330
  } else if (tvisnumber(o1) && tvisnumber(o2)) {
91,608✔
331
    return numberVnum(o1) < numberVnum(o2);
×
332
  } else {
333
    TValue *base = lj_meta_comp(L, o1, o2, 0);
91,608✔
334
    if ((uintptr_t)base <= 1) {
91,608✔
335
      return (int)(uintptr_t)base;
91,608✔
336
    } else {
337
      L->top = base+2;
×
338
      jit_secure_call(L, base, 1+1);
×
339
      L->top -= 2+LJ_FR2;
×
340
      return tvistruecond(L->top+1+LJ_FR2);
×
341
    }
342
  }
343
}
344

345
LUA_API lua_Number lua_tonumber(lua_State *L, int idx)
5✔
346
{
347
  cTValue *o = index2adr(L, idx);
5✔
348
  TValue tmp;
5✔
349
  if (LJ_LIKELY(tvisnumber(o)))
5✔
350
    return numberVnum(o);
5✔
351
  else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp))
×
352
    return numV(&tmp);
×
353
  else
354
    return 0;
×
355
}
356

357
LUA_API lua_Number lua_tonumberx(lua_State *L, int idx, int *ok)
×
358
{
359
  cTValue *o = index2adr(L, idx);
×
360
  TValue tmp;
×
361
  if (LJ_LIKELY(tvisnumber(o))) {
×
362
    if (ok) *ok = 1;
×
363
    return numberVnum(o);
×
364
  } else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp)) {
×
365
    if (ok) *ok = 1;
×
366
    return numV(&tmp);
×
367
  } else {
368
    if (ok) *ok = 0;
×
369
    return 0;
×
370
  }
371
}
372

373
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx)
5✔
374
{
375
  cTValue *o = index2adr(L, idx);
5✔
376
  TValue tmp;
5✔
377
  if (LJ_LIKELY(tvisnumber(o)))
5✔
378
    return numberVnum(o);
5✔
379
  else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp)))
×
380
    lj_err_argt(L, idx, LUA_TNUMBER);
×
381
  return numV(&tmp);
×
382
}
383

384
LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def)
1✔
385
{
386
  cTValue *o = index2adr(L, idx);
1✔
387
  TValue tmp;
1✔
388
  if (LJ_LIKELY(tvisnumber(o)))
1✔
389
    return numberVnum(o);
1✔
390
  else if (tvisnil(o))
×
391
    return def;
392
  else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp)))
×
393
    lj_err_argt(L, idx, LUA_TNUMBER);
×
394
  return numV(&tmp);
×
395
}
396

397
LUA_API lua_Integer lua_tointeger(lua_State *L, int idx)
15✔
398
{
399
  cTValue *o = index2adr(L, idx);
15✔
400
  TValue tmp;
15✔
401
  lua_Number n;
15✔
402
  if (LJ_LIKELY(tvisint(o))) {
15✔
403
    return intV(o);
404
  } else if (LJ_LIKELY(tvisnum(o))) {
15✔
405
    n = numV(o);
15✔
406
  } else {
407
    if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
×
408
      return 0;
×
409
    if (tvisint(&tmp))
×
410
      return intV(&tmp);
411
    n = numV(&tmp);
×
412
  }
413
#if LJ_64
414
  return (lua_Integer)n;
15✔
415
#else
416
  return lj_num2int(n);
417
#endif
418
}
419

420
LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *ok)
×
421
{
422
  cTValue *o = index2adr(L, idx);
×
423
  TValue tmp;
×
424
  lua_Number n;
×
425
  if (LJ_LIKELY(tvisint(o))) {
×
426
    if (ok) *ok = 1;
427
    return intV(o);
428
  } else if (LJ_LIKELY(tvisnum(o))) {
×
429
    n = numV(o);
×
430
  } else {
431
    if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) {
×
432
      if (ok) *ok = 0;
×
433
      return 0;
×
434
    }
435
    if (tvisint(&tmp)) {
×
436
      if (ok) *ok = 1;
437
      return intV(&tmp);
438
    }
439
    n = numV(&tmp);
×
440
  }
441
  if (ok) *ok = 1;
×
442
#if LJ_64
443
  return (lua_Integer)n;
×
444
#else
445
  return lj_num2int(n);
446
#endif
447
}
448

449
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx)
×
450
{
451
  cTValue *o = index2adr(L, idx);
×
452
  TValue tmp;
×
453
  lua_Number n;
×
454
  if (LJ_LIKELY(tvisint(o))) {
×
455
    return intV(o);
456
  } else if (LJ_LIKELY(tvisnum(o))) {
×
457
    n = numV(o);
×
458
  } else {
459
    if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
×
460
      lj_err_argt(L, idx, LUA_TNUMBER);
×
461
    if (tvisint(&tmp))
×
462
      return (lua_Integer)intV(&tmp);
463
    n = numV(&tmp);
×
464
  }
465
#if LJ_64
466
  return (lua_Integer)n;
×
467
#else
468
  return lj_num2int(n);
469
#endif
470
}
471

472
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def)
95✔
473
{
474
  cTValue *o = index2adr(L, idx);
95✔
475
  TValue tmp;
95✔
476
  lua_Number n;
95✔
477
  if (LJ_LIKELY(tvisint(o))) {
95✔
478
    return intV(o);
479
  } else if (LJ_LIKELY(tvisnum(o))) {
95✔
480
    n = numV(o);
4✔
481
  } else if (tvisnil(o)) {
91✔
482
    return def;
483
  } else {
484
    if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))
×
485
      lj_err_argt(L, idx, LUA_TNUMBER);
×
486
    if (tvisint(&tmp))
×
487
      return (lua_Integer)intV(&tmp);
488
    n = numV(&tmp);
×
489
  }
490
#if LJ_64
491
  return (lua_Integer)n;
4✔
492
#else
493
  return lj_num2int(n);
494
#endif
495
}
496

497
LUA_API int lua_toboolean(lua_State *L, int idx)
1,401✔
498
{
499
  cTValue *o = index2adr(L, idx);
1,401✔
500
  return tvistruecond(o);
1,401✔
501
}
502

503
LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len)
4,429✔
504
{
505
  TValue *o = index2adr(L, idx);
4,429✔
506
  GCstr *s;
4,429✔
507
  if (LJ_LIKELY(tvisstr(o))) {
4,429✔
508
    s = strV(o);
4,421✔
509
  } else if (tvisnumber(o)) {
8✔
510
    lj_gc_check(L);
×
511
    o = index2adr(L, idx);  /* GC may move the stack. */
×
512
    s = lj_strfmt_number(L, o);
×
513
    setstrV(L, o, s);
×
514
  } else {
515
    if (len != NULL) *len = 0;
8✔
516
    return NULL;
8✔
517
  }
518
  if (len != NULL) *len = s->len;
4,421✔
519
  return strdata(s);
4,421✔
520
}
521

522
LUA_API uint32_t lua_hashstring(lua_State *L, int idx)
×
523
{
524
  TValue *o = index2adr(L, idx);
×
525
  lua_assert(tvisstr(o));
×
526
  GCstr *s = strV(o);
×
527
  if (! strsmart(s))
×
528
    return s->hash;
×
529
  return lua_hash(strdata(s), s->len);
×
530
}
531

532
LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len)
946✔
533
{
534
  TValue *o = index2adr(L, idx);
946✔
535
  GCstr *s;
946✔
536
  if (LJ_LIKELY(tvisstr(o))) {
946✔
537
    s = strV(o);
946✔
538
  } else if (tvisnumber(o)) {
×
539
    lj_gc_check(L);
×
540
    o = index2adr(L, idx);  /* GC may move the stack. */
×
541
    s = lj_strfmt_number(L, o);
×
542
    setstrV(L, o, s);
×
543
  } else {
544
    lj_err_argt(L, idx, LUA_TSTRING);
×
545
  }
546
  if (len != NULL) *len = s->len;
946✔
547
  return strdata(s);
946✔
548
}
549

550
LUALIB_API const char *luaL_optlstring(lua_State *L, int idx,
28✔
551
                                       const char *def, size_t *len)
552
{
553
  TValue *o = index2adr(L, idx);
28✔
554
  GCstr *s;
28✔
555
  if (LJ_LIKELY(tvisstr(o))) {
28✔
556
    s = strV(o);
17✔
557
  } else if (tvisnil(o)) {
11✔
558
    if (len != NULL) *len = def ? strlen(def) : 0;
11✔
559
    return def;
11✔
560
  } else if (tvisnumber(o)) {
×
561
    lj_gc_check(L);
×
562
    o = index2adr(L, idx);  /* GC may move the stack. */
×
563
    s = lj_strfmt_number(L, o);
×
564
    setstrV(L, o, s);
×
565
  } else {
566
    lj_err_argt(L, idx, LUA_TSTRING);
×
567
  }
568
  if (len != NULL) *len = s->len;
17✔
569
  return strdata(s);
17✔
570
}
571

572
LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def,
×
573
                                const char *const lst[])
574
{
575
  ptrdiff_t i;
×
576
  const char *s = lua_tolstring(L, idx, NULL);
×
577
  if (s == NULL && (s = def) == NULL)
×
578
    lj_err_argt(L, idx, LUA_TSTRING);
×
579
  for (i = 0; lst[i]; i++)
×
580
    if (strcmp(lst[i], s) == 0)
×
581
      return (int)i;
×
582
  lj_err_argv(L, idx, LJ_ERR_INVOPTM, s);
×
583
}
584

585
LUA_API size_t lua_objlen(lua_State *L, int idx)
127✔
586
{
587
  TValue *o = index2adr(L, idx);
127✔
588
  if (tvisstr(o)) {
127✔
589
    return strV(o)->len;
127✔
590
  } else if (tvistab(o)) {
×
591
    return (size_t)lj_tab_len(tabV(o));
×
592
  } else if (tvisudata(o)) {
×
593
    return udataV(o)->len;
×
594
  } else if (tvisnumber(o)) {
×
595
    GCstr *s = lj_strfmt_number(L, o);
×
596
    setstrV(L, o, s);
×
597
    return s->len;
×
598
  } else {
599
    return 0;
600
  }
601
}
602

603
LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)
×
604
{
605
  cTValue *o = index2adr(L, idx);
×
606
  if (tvisfunc(o)) {
×
607
    BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns));
×
608
    if (op == BC_FUNCC || op == BC_FUNCCW)
×
609
      return funcV(o)->c.f;
×
610
  }
611
  return NULL;
612
}
613

614
LUA_API void *lua_touserdata(lua_State *L, int idx)
1✔
615
{
616
  cTValue *o = index2adr(L, idx);
1✔
617
  if (tvisudata(o))
1✔
618
    return uddata(udataV(o));
1✔
619
  else if (tvislightud(o))
×
620
    return lightudV(G(L), o);
×
621
  else
622
    return NULL;
623
}
624

625
LUA_API lua_State *lua_tothread(lua_State *L, int idx)
×
626
{
627
  cTValue *o = index2adr(L, idx);
×
628
  return (!tvisthread(o)) ? NULL : threadV(o);
×
629
}
630

631
LUA_API const void *lua_topointer(lua_State *L, int idx)
2✔
632
{
633
  return lj_obj_ptr(G(L), index2adr(L, idx));
2✔
634
}
635

636
/* -- Stack setters (object creation) ------------------------------------- */
637

638
LUA_API void lua_pushnil(lua_State *L)
244✔
639
{
640
  setnilV(L->top);
244✔
641
  incr_top(L);
244✔
642
}
244✔
643

644
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
6✔
645
{
646
  setnumV(L->top, n);
6✔
647
  if (LJ_UNLIKELY(tvisnan(L->top)))
6✔
648
    setnanV(L->top);  /* Canonicalize injected NaNs. */
×
649
  incr_top(L);
6✔
650
}
6✔
651

652
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
238✔
653
{
654
  setintptrV(L->top, n);
238✔
655
  incr_top(L);
238✔
656
}
238✔
657

658
LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len)
11,458✔
659
{
660
  GCstr *s;
11,458✔
661
  lj_gc_check(L);
11,458✔
662
  s = lj_str_new(L, str, len);
11,458✔
663
  setstrV(L, L->top, s);
11,458✔
664
  incr_top(L);
11,458✔
665
}
11,458✔
666

667
LUA_API void lua_pushstring(lua_State *L, const char *str)
3,138✔
668
{
669
  if (str == NULL) {
3,138✔
670
    setnilV(L->top);
5✔
671
  } else {
672
    GCstr *s;
3,133✔
673
    lj_gc_check(L);
3,133✔
674
    s = lj_str_newz(L, str);
3,133✔
675
    setstrV(L, L->top, s);
3,133✔
676
  }
677
  incr_top(L);
3,138✔
678
}
3,138✔
679

680
LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,
×
681
                                     va_list argp)
682
{
683
  lj_gc_check(L);
×
684
  return lj_strfmt_pushvf(L, fmt, argp);
×
685
}
686

687
LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...)
1,491✔
688
{
689
  const char *ret;
1,491✔
690
  va_list argp;
1,491✔
691
  lj_gc_check(L);
1,491✔
692
  va_start(argp, fmt);
1,491✔
693
  ret = lj_strfmt_pushvf(L, fmt, argp);
1,491✔
694
  va_end(argp);
1,491✔
695
  return ret;
1,491✔
696
}
697

698
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n)
3,511✔
699
{
700
  GCfunc *fn;
3,511✔
701
  lj_gc_check(L);
3,511✔
702
  api_checknelems(L, n);
3,511✔
703
  fn = lj_func_newC(L, (MSize)n, getcurrenv(L));
7,022✔
704
  fn->c.f = f;
3,511✔
705
  L->top -= n;
3,511✔
706
  while (n--)
3,511✔
707
    copyTV(L, &fn->c.upvalue[n], L->top+n);
3,537✔
708
  setfuncV(L, L->top, fn);
3,511✔
709
  lua_assert(iswhite(obj2gco(fn)));
3,511✔
710
  incr_top(L);
3,511✔
711
}
3,511✔
712

713
LUA_API void lua_pushboolean(lua_State *L, int b)
60✔
714
{
715
  setboolV(L->top, (b != 0));
60✔
716
  incr_top(L);
60✔
717
}
60✔
718

719
LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
3✔
720
{
721
#if LJ_64
722
  p = lj_lightud_intern(L, p);
3✔
723
#endif
724
  setrawlightudV(L->top, p);
3✔
725
  incr_top(L);
3✔
726
}
3✔
727

728
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
2,432✔
729
{
730
  lj_gc_check(L);
2,432✔
731
  settabV(L, L->top, lj_tab_new_ah(L, narray, nrec));
2,432✔
732
  incr_top(L);
2,432✔
733
}
2,432✔
734

735
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
125✔
736
{
737
  GCtab *regt = tabV(registry(L));
125✔
738
  TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname));
125✔
739
  if (tvisnil(tv)) {
125✔
740
    GCtab *mt = lj_tab_new(L, 0, 1);
125✔
741
    settabV(L, tv, mt);
125✔
742
    settabV(L, L->top++, mt);
125✔
743
    lj_gc_anybarriert(L, regt);
125✔
744
    return 1;
125✔
745
  } else {
746
    copyTV(L, L->top++, tv);
×
747
    return 0;
×
748
  }
749
}
750

751
LUA_API int lua_pushthread(lua_State *L)
1✔
752
{
753
  setthreadV(L, L->top, L);
1✔
754
  incr_top(L);
1✔
755
  return (mainthread(G(L)) == L);
1✔
756
}
757

758
LUA_API lua_State *lua_newthread(lua_State *L)
29✔
759
{
760
  lua_State *L1;
29✔
761
  lj_gc_check(L);
29✔
762
  L1 = lj_state_new(L);
29✔
763
  setthreadV(L, L->top, L1);
29✔
764
  incr_top(L);
29✔
765
  return L1;
29✔
766
}
767

768
LUA_API void *lua_newuserdata(lua_State *L, size_t size)
1,620✔
769
{
770
  GCudata *ud;
1,620✔
771
  lj_gc_check(L);
1,620✔
772
  if (size > LJ_MAX_UDATA)
1,620✔
773
    lj_err_msg(L, LJ_ERR_UDATAOV);
×
774
  ud = lj_udata_new(L, (MSize)size, getcurrenv(L));
3,240✔
775
  setudataV(L, L->top, ud);
1,620✔
776
  incr_top(L);
1,620✔
777
  return uddata(ud);
1,620✔
778
}
779

780
LUA_API void lua_concat(lua_State *L, int n)
2,342✔
781
{
782
  api_checknelems(L, n);
2,342✔
783
  if (n >= 2) {
2,342✔
784
    n--;
450✔
785
    do {
450✔
786
      TValue *top = lj_meta_cat(L, L->top-1, -n);
450✔
787
      if (top == NULL) {
450✔
788
        L->top -= n;
450✔
789
        break;
450✔
790
      }
791
      n -= (int)(L->top - top);
×
792
      L->top = top+2;
×
793
      jit_secure_call(L, top, 1+1);
×
794
      L->top -= 1+LJ_FR2;
×
795
      copyTV(L, L->top-1, L->top+LJ_FR2);
×
796
    } while (--n > 0);
×
797
  } else if (n == 0) {  /* Push empty string. */
1,892✔
798
    setstrV(L, L->top, &G(L)->strempty);
×
799
    incr_top(L);
×
800
  }
801
  /* else n == 1: nothing to do. */
802
}
2,342✔
803

804
/* -- Object getters ------------------------------------------------------ */
805

806
LUA_API void lua_gettable(lua_State *L, int idx)
14✔
807
{
808
  cTValue *v, *t = index2adr(L, idx);
14✔
809
  api_checkvalidindex(L, t);
14✔
810
  v = lj_meta_tget(L, t, L->top-1);
14✔
811
  if (v == NULL) {
14✔
812
    L->top += 2;
×
813
    jit_secure_call(L, L->top-2, 1+1);
×
814
    L->top -= 2+LJ_FR2;
×
815
    v = L->top+1+LJ_FR2;
×
816
  }
817
  copyTV(L, L->top-1, v);
14✔
818
}
14✔
819

820
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
3,840✔
821
{
822
  cTValue *v, *t = index2adr(L, idx);
3,840✔
823
  TValue key;
3,840✔
824
  api_checkvalidindex(L, t);
3,840✔
825
  setstrV(L, &key, lj_str_newz(L, k));
3,840✔
826
  v = lj_meta_tget(L, t, &key);
3,840✔
827
  if (v == NULL) {
3,840✔
828
    L->top += 2;
×
829
    jit_secure_call(L, L->top-2, 1+1);
×
830
    L->top -= 2+LJ_FR2;
×
831
    v = L->top+1+LJ_FR2;
×
832
  }
833
  copyTV(L, L->top, v);
3,840✔
834
  incr_top(L);
3,840✔
835
}
3,840✔
836

837
LUA_API void lua_rawget(lua_State *L, int idx)
5,248✔
838
{
839
  cTValue *t = index2adr(L, idx);
5,248✔
840
  api_check(L, tvistab(t));
5,248✔
841
  copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1));
5,248✔
842
}
5,248✔
843

844
LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
166,399✔
845
{
846
  cTValue *v, *t = index2adr(L, idx);
166,399✔
847
  api_check(L, tvistab(t));
166,399✔
848
  v = lj_tab_getint(tabV(t), n);
166,399✔
849
  if (v) {
166,399✔
850
    copyTV(L, L->top, v);
166,349✔
851
  } else {
852
    setnilV(L->top);
50✔
853
  }
854
  incr_top(L);
166,399✔
855
}
166,399✔
856

857
LUA_API int lua_getmetatable(lua_State *L, int idx)
13✔
858
{
859
  cTValue *o = index2adr(L, idx);
13✔
860
  GCtab *mt = NULL;
13✔
861
  if (tvistab(o))
13✔
862
    mt = tabref(tabV(o)->metatable);
8✔
863
  else if (tvisudata(o))
5✔
864
    mt = tabref(udataV(o)->metatable);
2✔
865
  else
866
    mt = tabref(basemt_obj(G(L), o));
3✔
867
  if (mt == NULL)
13✔
868
    return 0;
869
  settabV(L, L->top, mt);
6✔
870
  incr_top(L);
6✔
871
  return 1;
872
}
873

874
LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field)
1✔
875
{
876
  if (lua_getmetatable(L, idx)) {
1✔
877
    cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field));
×
878
    if (tv && !tvisnil(tv)) {
×
879
      copyTV(L, L->top-1, tv);
×
880
      return 1;
×
881
    }
882
    L->top--;
×
883
  }
884
  return 0;
885
}
886

887
LUA_API void lua_getfenv(lua_State *L, int idx)
13✔
888
{
889
  cTValue *o = index2adr(L, idx);
13✔
890
  api_checkvalidindex(L, o);
13✔
891
  if (tvisfunc(o)) {
13✔
892
    settabV(L, L->top, tabref(funcV(o)->c.env));
8✔
893
  } else if (tvisudata(o)) {
5✔
894
    settabV(L, L->top, tabref(udataV(o)->env));
×
895
  } else if (tvisthread(o)) {
5✔
896
    settabV(L, L->top, tabref(threadV(o)->env));
4✔
897
  } else {
898
    setnilV(L->top);
1✔
899
  }
900
  incr_top(L);
13✔
901
}
13✔
902

903
LUA_API int lua_next(lua_State *L, int idx)
×
904
{
905
  cTValue *t = index2adr(L, idx);
×
906
  int more;
×
907
  api_check(L, tvistab(t));
×
908
  more = lj_tab_next(L, tabV(t), L->top-1);
×
909
  if (more) {
×
910
    incr_top(L);  /* Return new key and value slot. */
×
911
  } else {  /* End of traversal. */
912
    L->top--;  /* Remove key slot. */
×
913
  }
914
  return more;
×
915
}
916

917
LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n)
1✔
918
{
919
  TValue *val;
1✔
920
  GCobj *o;
1✔
921
  const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val, &o);
1✔
922
  if (name) {
1✔
923
    copyTV(L, L->top, val);
1✔
924
    incr_top(L);
1✔
925
  }
926
  return name;
1✔
927
}
928

929
LUA_API void *lua_upvalueid(lua_State *L, int idx, int n)
×
930
{
931
  GCfunc *fn = funcV(index2adr(L, idx));
×
932
  n--;
×
933
  api_check(L, (uint32_t)n < fn->l.nupvalues);
×
934
  return isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) :
×
935
                         (void *)&fn->c.upvalue[n];
936
}
937

938
LUA_API void lua_upvaluejoin(lua_State *L, int idx1, int n1, int idx2, int n2)
×
939
{
940
  GCfunc *fn1 = funcV(index2adr(L, idx1));
×
941
  GCfunc *fn2 = funcV(index2adr(L, idx2));
×
942
  n1--; n2--;
×
943
  api_check(L, isluafunc(fn1) && (uint32_t)n1 < fn1->l.nupvalues);
×
944
  api_check(L, isluafunc(fn2) && (uint32_t)n2 < fn2->l.nupvalues);
×
945
  setgcrefr(fn1->l.uvptr[n1], fn2->l.uvptr[n2]);
×
946
  lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1]));
×
947
}
×
948

949
LUALIB_API void *luaL_testudata(lua_State *L, int idx, const char *tname)
1✔
950
{
951
  cTValue *o = index2adr(L, idx);
1✔
952
  if (tvisudata(o)) {
1✔
953
    GCudata *ud = udataV(o);
1✔
954
    cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname));
1✔
955
    if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable))
1✔
956
      return uddata(ud);
1✔
957
  }
958
  return NULL;  /* value is not a userdata with a metatable */
959
}
960

961
LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)
1✔
962
{
963
  void *p = luaL_testudata(L, idx, tname);
1✔
964
  if (!p) lj_err_argtype(L, idx, tname);
1✔
965
  return p;
1✔
966
}
967

968
/* -- Object setters ------------------------------------------------------ */
969

970
LUA_API void lua_settable(lua_State *L, int idx)
2,003✔
971
{
972
  TValue *o;
2,003✔
973
  cTValue *t = index2adr(L, idx);
2,003✔
974
  api_checknelems(L, 2);
2,003✔
975
  api_checkvalidindex(L, t);
2,003✔
976
  o = lj_meta_tset(L, t, L->top-2);
2,003✔
977
  if (o) {
2,003✔
978
    /* NOBARRIER: lj_meta_tset ensures the table is not black. */
979
    L->top -= 2;
2,003✔
980
    copyTV(L, o, L->top+1);
2,003✔
981
  } else {
982
    TValue *base = L->top;
×
983
    copyTV(L, base+2, base-3-2*LJ_FR2);
×
984
    L->top = base+3;
×
985
    jit_secure_call(L, base, 0+1);
×
986
    L->top -= 3+LJ_FR2;
×
987
  }
988
}
2,003✔
989

990
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
4,957✔
991
{
992
  TValue *o;
4,957✔
993
  TValue key;
4,957✔
994
  cTValue *t = index2adr(L, idx);
4,957✔
995
  api_checknelems(L, 1);
4,957✔
996
  api_checkvalidindex(L, t);
4,957✔
997
  setstrV(L, &key, lj_str_newz(L, k));
4,957✔
998
  o = lj_meta_tset(L, t, &key);
4,957✔
999
  if (o) {
4,957✔
1000
    /* NOBARRIER: lj_meta_tset ensures the table is not black. */
1001
    copyTV(L, o, --L->top);
4,957✔
1002
  } else {
1003
    TValue *base = L->top;
×
1004
    copyTV(L, base+2, base-3-2*LJ_FR2);
×
1005
    L->top = base+3;
×
1006
    jit_secure_call(L, base, 0+1);
×
1007
    L->top -= 2+LJ_FR2;
×
1008
  }
1009
}
4,957✔
1010

1011
LUA_API void lua_rawset(lua_State *L, int idx)
7✔
1012
{
1013
  GCtab *t = tabV(index2adr(L, idx));
7✔
1014
  TValue *dst, *key;
7✔
1015
  api_checknelems(L, 2);
7✔
1016
  key = L->top-2;
7✔
1017
  dst = lj_tab_set(L, t, key);
7✔
1018
  copyTV(L, dst, key+1);
6✔
1019
  lj_gc_anybarriert(L, t);
6✔
1020
  L->top = key;
6✔
1021
}
6✔
1022

1023
LUA_API void lua_rawseti(lua_State *L, int idx, int n)
87,099✔
1024
{
1025
  GCtab *t = tabV(index2adr(L, idx));
87,099✔
1026
  TValue *dst, *src;
87,099✔
1027
  api_checknelems(L, 1);
87,099✔
1028
  dst = lj_tab_setint(L, t, n);
87,099✔
1029
  src = L->top-1;
87,099✔
1030
  copyTV(L, dst, src);
87,099✔
1031
  lj_gc_barriert(L, t, dst);
87,099✔
1032
  L->top = src;
87,099✔
1033
}
87,099✔
1034

1035
LUA_API int lua_setmetatable(lua_State *L, int idx)
9✔
1036
{
1037
  global_State *g;
9✔
1038
  GCtab *mt;
9✔
1039
  cTValue *o = index2adr(L, idx);
9✔
1040
  api_checknelems(L, 1);
9✔
1041
  api_checkvalidindex(L, o);
9✔
1042
  if (tvisnil(L->top-1)) {
9✔
1043
    mt = NULL;
1044
  } else {
1045
    api_check(L, tvistab(L->top-1));
9✔
1046
    mt = tabV(L->top-1);
9✔
1047
  }
1048
  g = G(L);
9✔
1049
  if (tvistab(o)) {
9✔
1050
    setgcref(tabV(o)->metatable, obj2gco(mt));
4✔
1051
    if (mt)
4✔
1052
      lj_gc_objbarriert(L, tabV(o), mt);
4✔
1053
  } else if (tvisudata(o)) {
5✔
1054
    setgcref(udataV(o)->metatable, obj2gco(mt));
3✔
1055
    if (mt)
3✔
1056
      lj_gc_objbarrier(L, udataV(o), mt);
3✔
1057
  } else {
1058
    /* Flush cache, since traces specialize to basemt. But not during __gc. */
1059
    if (lj_trace_flushall(L))
2✔
1060
      lj_err_caller(L, LJ_ERR_NOGCMM);
×
1061
    if (tvisbool(o)) {
2✔
1062
      /* NOBARRIER: basemt is a GC root. */
1063
      setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt));
1✔
1064
      setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt));
1✔
1065
    } else {
1066
      /* NOBARRIER: basemt is a GC root. */
1067
      setgcref(basemt_obj(g, o), obj2gco(mt));
1✔
1068
    }
1069
  }
1070
  L->top--;
9✔
1071
  return 1;
9✔
1072
}
1073

1074
LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname)
1✔
1075
{
1076
  lua_getfield(L, LUA_REGISTRYINDEX, tname);
1✔
1077
  lua_setmetatable(L, -2);
1✔
1078
}
1✔
1079

1080
LUA_API int lua_setfenv(lua_State *L, int idx)
6✔
1081
{
1082
  cTValue *o = index2adr(L, idx);
6✔
1083
  GCtab *t;
6✔
1084
  api_checknelems(L, 1);
6✔
1085
  api_checkvalidindex(L, o);
6✔
1086
  api_check(L, tvistab(L->top-1));
6✔
1087
  t = tabV(L->top-1);
6✔
1088
  if (tvisfunc(o)) {
6✔
1089
    setgcref(funcV(o)->c.env, obj2gco(t));
4✔
1090
  } else if (tvisudata(o)) {
2✔
1091
    setgcref(udataV(o)->env, obj2gco(t));
×
1092
  } else if (tvisthread(o)) {
2✔
1093
    setgcref(threadV(o)->env, obj2gco(t));
1✔
1094
  } else {
1095
    L->top--;
1✔
1096
    return 0;
1✔
1097
  }
1098
  lj_gc_objbarrier(L, gcV(o), t);
5✔
1099
  L->top--;
5✔
1100
  return 1;
5✔
1101
}
1102

1103
LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n)
2✔
1104
{
1105
  cTValue *f = index2adr(L, idx);
2✔
1106
  TValue *val;
2✔
1107
  GCobj *o;
2✔
1108
  const char *name;
2✔
1109
  api_checknelems(L, 1);
2✔
1110
  name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val, &o);
2✔
1111
  if (name) {
2✔
1112
    L->top--;
1✔
1113
    copyTV(L, val, L->top);
1✔
1114
    lj_gc_barrier(L, o, L->top);
1✔
1115
  }
1116
  return name;
2✔
1117
}
1118

1119
/* -- Calls --------------------------------------------------------------- */
1120

1121
#if LJ_FR2
1122
static TValue *api_call_base(lua_State *L, int nargs)
2,630✔
1123
{
1124
  TValue *o = L->top, *base = o - nargs;
2,630✔
1125
  L->top = o+1;
2,630✔
1126
  for (; o > base; o--) copyTV(L, o, o-1);
5,281✔
1127
  setnilV(o);
2,630✔
1128
  return o+1;
2,630✔
1129
}
1130
#else
1131
#define api_call_base(L, nargs)        (L->top - (nargs))
1132
#endif
1133

1134
LUA_API void lua_call(lua_State *L, int nargs, int nresults)
2,352✔
1135
{
1136
  api_check(L, L->status == LUA_OK || L->status == LUA_ERRERR);
2,352✔
1137
  api_checknelems(L, nargs+1);
2,352✔
1138
  jit_secure_call(L, api_call_base(L, nargs), nresults+1);
4,704✔
1139
}
2,351✔
1140

1141
LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
278✔
1142
{
1143
  global_State *g = G(L);
278✔
1144
  uint8_t oldh = hook_save(g);
278✔
1145
  ptrdiff_t ef;
278✔
1146
  int status;
278✔
1147
  api_check(L, L->status == LUA_OK || L->status == LUA_ERRERR);
278✔
1148
  api_checknelems(L, nargs+1);
278✔
1149
  if (errfunc == 0) {
278✔
1150
    ef = 0;
1151
  } else {
1152
    cTValue *o = stkindex2adr(L, errfunc);
240✔
1153
    api_checkvalidindex(L, o);
240✔
1154
    ef = savestack(L, o);
240✔
1155
  }
1156
  /* Forbid Lua world re-entrancy while running the trace */
1157
  if (tvref(g->jit_base)) {
278✔
1158
    setstrV(L, L->top++, lj_err_str(L, LJ_ERR_JITCALL));
×
1159
    if (g->panic) g->panic(L);
×
1160
    exit(EXIT_FAILURE);
×
1161
  }
1162
  lj_trace_abort(g);  /* Never record across Lua VM entrance */
278✔
1163
  status = lj_vm_pcall(L, api_call_base(L, nargs), nresults+1, ef);
556✔
1164
  if (status) hook_restore(g, oldh);
268✔
1165
  return status;
268✔
1166
}
1167

1168
static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud)
126✔
1169
{
1170
  GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L));
252✔
1171
  TValue *top = L->top;
126✔
1172
  fn->c.f = func;
126✔
1173
  setfuncV(L, top++, fn);
126✔
1174
  if (LJ_FR2) setnilV(top++);
126✔
1175
#if LJ_64
1176
  ud = lj_lightud_intern(L, ud);
126✔
1177
#endif
1178
  setrawlightudV(top++, ud);
126✔
1179
  cframe_nres(L->cframe) = 1+0;  /* Zero results. */
126✔
1180
  L->top = top;
126✔
1181
  return top-1;  /* Now call the newly allocated C function. */
126✔
1182
}
1183

1184
LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
126✔
1185
{
1186
  global_State *g = G(L);
126✔
1187
  uint8_t oldh = hook_save(g);
126✔
1188
  int status;
126✔
1189
  api_check(L, L->status == LUA_OK || L->status == LUA_ERRERR);
126✔
1190
  /* Forbid Lua world re-entrancy while running the trace */
1191
  if (tvref(g->jit_base)) {
126✔
1192
    setstrV(L, L->top++, lj_err_str(L, LJ_ERR_JITCALL));
×
1193
    if (g->panic) g->panic(L);
×
1194
    exit(EXIT_FAILURE);
×
1195
  }
1196
  lj_trace_abort(g);  /* Never record across Lua VM entrance */
126✔
1197
  status = lj_vm_cpcall(L, func, ud, cpcall);
126✔
1198
  if (status) hook_restore(g, oldh);
116✔
1199
  return status;
116✔
1200
}
1201

1202
LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field)
1✔
1203
{
1204
  if (luaL_getmetafield(L, idx, field)) {
1✔
1205
    TValue *top = L->top--;
×
1206
    if (LJ_FR2) setnilV(top++);
×
1207
    copyTV(L, top++, index2adr(L, idx));
×
1208
    L->top = top;
×
1209
    jit_secure_call(L, top-1, 1+1);
×
1210
    return 1;
×
1211
  }
1212
  return 0;
1213
}
1214

1215
/* -- Coroutine yield and resume ------------------------------------------ */
1216

1217
LUA_API int lua_isyieldable(lua_State *L)
×
1218
{
1219
  return cframe_canyield(L->cframe);
×
1220
}
1221

1222
LUA_API int lua_yield(lua_State *L, int nresults)
2✔
1223
{
1224
  void *cf = L->cframe;
2✔
1225
  global_State *g = G(L);
2✔
1226
  if (cframe_canyield(cf)) {
2✔
1227
    cf = cframe_raw(cf);
2✔
1228
    if (!hook_active(g)) {  /* Regular yield: move results down if needed. */
2✔
1229
      cTValue *f = L->top - nresults;
×
1230
      if (f > L->base) {
×
1231
        TValue *t = L->base;
1232
        while (--nresults >= 0) copyTV(L, t++, f++);
×
1233
        L->top = t;
×
1234
      }
1235
      L->cframe = NULL;
×
1236
      L->status = LUA_YIELD;
×
1237
      return -1;
×
1238
    } else {  /* Yield from hook: add a pseudo-frame. */
1239
      TValue *top = L->top;
2✔
1240
      hook_leave(g);
2✔
1241
      (top++)->u64 = cframe_multres(cf);
2✔
1242
      setcont(top, lj_cont_hook);
2✔
1243
      if (LJ_FR2) top++;
2✔
1244
      setframe_pc(top, cframe_pc(cf)-1);
2✔
1245
      top++;
2✔
1246
      setframe_gc(top, obj2gco(L), LJ_TTHREAD);
2✔
1247
      if (LJ_FR2) top++;
2✔
1248
      setframe_ftsz(top, ((char *)(top+1)-(char *)L->base)+FRAME_CONT);
2✔
1249
      L->top = L->base = top+1;
2✔
1250
#if ((defined(__GNUC__) || defined(__clang__)) && (LJ_TARGET_X64 || defined(LUAJIT_UNWIND_EXTERNAL)) && !LJ_NO_UNWIND) || LJ_TARGET_WINDOWS
1251
      lj_err_throw(L, LUA_YIELD);
2✔
1252
#else
1253
      L->cframe = NULL;
1254
      L->status = LUA_YIELD;
1255
      lj_vm_unwind_c(cf, LUA_YIELD);
1256
#endif
1257
    }
1258
  }
1259
  lj_err_msg(L, LJ_ERR_CYIELD);
×
1260
  return 0;  /* unreachable */
1261
}
1262

1263
LUA_API int lua_resume(lua_State *L, int nargs)
×
1264
{
1265
  if (L->cframe == NULL && L->status <= LUA_YIELD)
×
1266
    return lj_vm_resume(L,
×
1267
      L->status == LUA_OK ? api_call_base(L, nargs) : L->top - nargs,
×
1268
      0, 0);
1269
  L->top = L->base;
×
1270
  setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP));
×
1271
  incr_top(L);
×
1272
  return LUA_ERRRUN;
1273
}
1274

1275
/* -- GC and memory management -------------------------------------------- */
1276

1277
LUA_API int lua_gc(lua_State *L, int what, int data)
273✔
1278
{
1279
  global_State *g = G(L);
273✔
1280
  int res = 0;
273✔
1281
  switch (what) {
273✔
1282
  case LUA_GCSTOP:
126✔
1283
    g->gc.threshold = LJ_MAX_MEM;
126✔
1284
    break;
126✔
1285
  case LUA_GCRESTART:
126✔
1286
    g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total;
126✔
1287
    break;
126✔
1288
  case LUA_GCCOLLECT:
17✔
1289
    lj_gc_fullgc(L);
17✔
1290
    break;
17✔
1291
  case LUA_GCCOUNT:
×
1292
    res = (int)(g->gc.total >> 10);
×
1293
    break;
×
1294
  case LUA_GCCOUNTB:
×
1295
    res = (int)(g->gc.total & 0x3ff);
×
1296
    break;
×
1297
  case LUA_GCSTEP: {
2✔
1298
    GCSize a = (GCSize)data << 10;
2✔
1299
    g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0;
2✔
1300
    while (g->gc.total >= g->gc.threshold)
4✔
1301
      if (lj_gc_step(L) > 0) {
2✔
1302
        res = 1;
1303
        break;
1304
      }
1305
    break;
1306
  }
1307
  case LUA_GCSETPAUSE:
1✔
1308
    res = (int)(g->gc.pause);
1✔
1309
    g->gc.pause = (MSize)data;
1✔
1310
    break;
1✔
1311
  case LUA_GCSETSTEPMUL:
1✔
1312
    res = (int)(g->gc.stepmul);
1✔
1313
    g->gc.stepmul = (MSize)data;
1✔
1314
    break;
1✔
1315
  case LUA_GCISRUNNING:
×
1316
    res = (g->gc.threshold != LJ_MAX_MEM);
×
1317
    break;
×
1318
  default:
1319
    res = -1;  /* Invalid option. */
1320
  }
1321
  return res;
273✔
1322
}
1323

1324
LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud)
×
1325
{
1326
  global_State *g = G(L);
×
1327
  if (ud) *ud = g->allocd;
×
1328
  return g->allocf;
×
1329
}
1330

1331
LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)
×
1332
{
1333
  global_State *g = G(L);
×
1334
  g->allocd = ud;
×
1335
  g->allocf = f;
×
1336
}
×
1337

1338
LUA_API uint32_t lua_hash(const char *str, uint32_t len)
161,597✔
1339
{
1340
  uint32_t h = len, a, b;
161,597✔
1341
  if (len >= 4) {
161,597✔
1342
    a = lj_getu32(str);
111,263✔
1343
    h ^= lj_getu32(str+len-4);
111,263✔
1344
    b = lj_getu32(str+(len>>1)-2);
111,263✔
1345
    h ^= b; h -= lj_rol(b, 14);
111,263✔
1346
    b += lj_getu32(str+(len>>2)-1);
111,263✔
1347
  } else if (len > 0) {
50,334✔
1348
    a = *str;
50,334✔
1349
    h ^= *(str+len-1);
50,334✔
1350
    b = *(str+(len>>1));
50,334✔
1351
    h ^= b; h -= lj_rol(b, 14);
50,334✔
1352
  } else {
1353
    return 0;
1354
  }
1355
  a ^= h; a -= lj_rol(h, 11);
161,597✔
1356
  b ^= a; b -= lj_rol(a, 25);
161,597✔
1357
  h ^= b; h -= lj_rol(b, 16);
161,597✔
1358
  return h;
161,597✔
1359
}
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