Coverage Report

Created: 2025-11-11 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/testdir/build/lua-master/source/lapi.c
Line
Count
Source
1
/*
2
** $Id: lapi.c $
3
** Lua API
4
** See Copyright Notice in lua.h
5
*/
6
7
#define lapi_c
8
#define LUA_CORE
9
10
#include "lprefix.h"
11
12
13
#include <limits.h>
14
#include <stdarg.h>
15
#include <string.h>
16
17
#include "lua.h"
18
19
#include "lapi.h"
20
#include "ldebug.h"
21
#include "ldo.h"
22
#include "lfunc.h"
23
#include "lgc.h"
24
#include "lmem.h"
25
#include "lobject.h"
26
#include "lstate.h"
27
#include "lstring.h"
28
#include "ltable.h"
29
#include "ltm.h"
30
#include "lundump.h"
31
#include "lvm.h"
32
33
34
35
const char lua_ident[] =
36
  "$LuaVersion: " LUA_COPYRIGHT " $"
37
  "$LuaAuthors: " LUA_AUTHORS " $";
38
39
40
41
/*
42
** Test for a valid index (one that is not the 'nilvalue').
43
*/
44
1.28G
#define isvalid(L, o) ((o) != &G(L)->nilvalue)
45
46
47
/* test for pseudo index */
48
763M
#define ispseudo(i)   ((i) <= LUA_REGISTRYINDEX)
49
50
/* test for upvalue */
51
2.53M
#define isupvalue(i)    ((i) < LUA_REGISTRYINDEX)
52
53
54
/*
55
** Convert an acceptable index to a pointer to its respective value.
56
** Non-valid indices return the special nil value 'G(L)->nilvalue'.
57
*/
58
1.85G
static TValue *index2value (lua_State *L, int idx) {
59
1.85G
  CallInfo *ci = L->ci;
60
1.85G
  if (idx > 0) {
61
1.08G
    StkId o = ci->func.p + idx;
62
1.08G
    api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index");
63
1.08G
    if (o >= L->top.p) return &G(L)->nilvalue;
64
926M
    else return s2v(o);
65
1.08G
  }
66
763M
  else if (!ispseudo(idx)) {  /* negative index */
67
740M
    api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
68
740M
                 "invalid index");
69
740M
    return s2v(L->top.p + idx);
70
740M
  }
71
22.8M
  else if (idx == LUA_REGISTRYINDEX)
72
22.0M
    return &G(L)->l_registry;
73
855k
  else {  /* upvalues */
74
855k
    idx = LUA_REGISTRYINDEX - idx;
75
855k
    api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
76
855k
    if (ttisCclosure(s2v(ci->func.p))) {  /* C closure? */
77
855k
      CClosure *func = clCvalue(s2v(ci->func.p));
78
855k
      return (idx <= func->nupvalues) ? &func->upvalue[idx-1]
79
855k
                                      : &G(L)->nilvalue;
80
855k
    }
81
0
    else {  /* light C function or Lua function (through a hook)?) */
82
0
      api_check(L, ttislcf(s2v(ci->func.p)), "caller not a C function");
83
0
      return &G(L)->nilvalue;  /* no upvalues */
84
0
    }
85
855k
  }
86
1.85G
}
87
88
89
90
/*
91
** Convert a valid actual index (not a pseudo-index) to its address.
92
*/
93
23.2M
static StkId index2stack (lua_State *L, int idx) {
94
23.2M
  CallInfo *ci = L->ci;
95
23.2M
  if (idx > 0) {
96
4.59M
    StkId o = ci->func.p + idx;
97
4.59M
    api_check(L, o < L->top.p, "invalid index");
98
4.59M
    return o;
99
4.59M
  }
100
18.6M
  else {    /* non-positive index */
101
18.6M
    api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
102
18.6M
                 "invalid index");
103
18.6M
    api_check(L, !ispseudo(idx), "invalid index");
104
18.6M
    return L->top.p + idx;
105
18.6M
  }
106
23.2M
}
107
108
109
5.88M
LUA_API int lua_checkstack (lua_State *L, int n) {
110
5.88M
  int res;
111
5.88M
  CallInfo *ci;
112
5.88M
  lua_lock(L);
113
5.88M
  ci = L->ci;
114
5.88M
  api_check(L, n >= 0, "negative 'n'");
115
5.88M
  if (L->stack_last.p - L->top.p > n)  /* stack large enough? */
116
5.86M
    res = 1;  /* yes; check is OK */
117
18.4k
  else  /* need to grow stack */
118
18.4k
    res = luaD_growstack(L, n, 0);
119
5.88M
  if (res && ci->top.p < L->top.p + n)
120
162k
    ci->top.p = L->top.p + n;  /* adjust frame top */
121
5.88M
  lua_unlock(L);
122
5.88M
  return res;
123
5.88M
}
124
125
126
660k
LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
127
660k
  int i;
128
660k
  if (from == to) return;
129
107k
  lua_lock(to);
130
107k
  api_checkpop(from, n);
131
107k
  api_check(from, G(from) == G(to), "moving among independent states");
132
107k
  api_check(from, to->ci->top.p - to->top.p >= n, "stack overflow");
133
107k
  from->top.p -= n;
134
396k
  for (i = 0; i < n; i++) {
135
289k
    setobjs2s(to, to->top.p, from->top.p + i);
136
289k
    to->top.p++;  /* stack already checked by previous 'api_check' */
137
289k
  }
138
107k
  lua_unlock(to);
139
107k
}
140
141
142
62.6k
LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
143
62.6k
  lua_CFunction old;
144
62.6k
  lua_lock(L);
145
62.6k
  old = G(L)->panic;
146
62.6k
  G(L)->panic = panicf;
147
62.6k
  lua_unlock(L);
148
62.6k
  return old;
149
62.6k
}
150
151
152
265k
LUA_API lua_Number lua_version (lua_State *L) {
153
265k
  UNUSED(L);
154
265k
  return LUA_VERSION_NUM;
155
265k
}
156
157
158
159
/*
160
** basic stack manipulation
161
*/
162
163
164
/*
165
** convert an acceptable stack index into an absolute index
166
*/
167
11.5M
LUA_API int lua_absindex (lua_State *L, int idx) {
168
11.5M
  return (idx > 0 || ispseudo(idx))
169
11.5M
         ? idx
170
11.5M
         : cast_int(L->top.p - L->ci->func.p) + idx;
171
11.5M
}
172
173
174
22.5M
LUA_API int lua_gettop (lua_State *L) {
175
22.5M
  return cast_int(L->top.p - (L->ci->func.p + 1));
176
22.5M
}
177
178
179
242M
LUA_API void lua_settop (lua_State *L, int idx) {
180
242M
  CallInfo *ci;
181
242M
  StkId func, newtop;
182
242M
  ptrdiff_t diff;  /* difference for new top */
183
242M
  lua_lock(L);
184
242M
  ci = L->ci;
185
242M
  func = ci->func.p;
186
242M
  if (idx >= 0) {
187
24.8M
    api_check(L, idx <= ci->top.p - (func + 1), "new top too large");
188
24.8M
    diff = ((func + 1) + idx) - L->top.p;
189
35.8M
    for (; diff > 0; diff--)
190
24.8M
      setnilvalue(s2v(L->top.p++));  /* clear new slots */
191
24.8M
  }
192
217M
  else {
193
217M
    api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top");
194
217M
    diff = idx + 1;  /* will "subtract" index (as it is negative) */
195
217M
  }
196
242M
  newtop = L->top.p + diff;
197
242M
  if (diff < 0 && L->tbclist.p >= newtop) {
198
0
    lua_assert(ci->callstatus & CIST_TBC);
199
0
    newtop = luaF_close(L, newtop, CLOSEKTOP, 0);
200
0
  }
201
242M
  L->top.p = newtop;  /* correct top only after closing any upvalue */
202
242M
  lua_unlock(L);
203
242M
}
204
205
206
893k
LUA_API void lua_closeslot (lua_State *L, int idx) {
207
893k
  StkId level;
208
893k
  lua_lock(L);
209
893k
  level = index2stack(L, idx);
210
893k
  api_check(L, (L->ci->callstatus & CIST_TBC) && (L->tbclist.p == level),
211
893k
     "no variable to close at given level");
212
893k
  level = luaF_close(L, level, CLOSEKTOP, 0);
213
893k
  setnilvalue(s2v(level));
214
893k
  lua_unlock(L);
215
893k
}
216
217
218
/*
219
** Reverse the stack segment from 'from' to 'to'
220
** (auxiliary to 'lua_rotate')
221
** Note that we move(copy) only the value inside the stack.
222
** (We do not move additional fields that may exist.)
223
*/
224
64.0M
static void reverse (lua_State *L, StkId from, StkId to) {
225
84.4M
  for (; from < to; from++, to--) {
226
20.4M
    TValue temp;
227
20.4M
    setobj(L, &temp, s2v(from));
228
20.4M
    setobjs2s(L, from, to);
229
20.4M
    setobj2s(L, to, &temp);
230
20.4M
  }
231
64.0M
}
232
233
234
/*
235
** Let x = AB, where A is a prefix of length 'n'. Then,
236
** rotate x n == BA. But BA == (A^r . B^r)^r.
237
*/
238
21.3M
LUA_API void lua_rotate (lua_State *L, int idx, int n) {
239
21.3M
  StkId p, t, m;
240
21.3M
  lua_lock(L);
241
21.3M
  t = L->top.p - 1;  /* end of stack segment being rotated */
242
21.3M
  p = index2stack(L, idx);  /* start of segment */
243
21.3M
  api_check(L, L->tbclist.p < p, "moving a to-be-closed slot");
244
21.3M
  api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
245
21.3M
  m = (n >= 0 ? t - n : p - n - 1);  /* end of prefix */
246
21.3M
  reverse(L, p, m);  /* reverse the prefix with length 'n' */
247
21.3M
  reverse(L, m + 1, t);  /* reverse the suffix */
248
21.3M
  reverse(L, p, t);  /* reverse the entire segment */
249
21.3M
  lua_unlock(L);
250
21.3M
}
251
252
253
2.53M
LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
254
2.53M
  TValue *fr, *to;
255
2.53M
  lua_lock(L);
256
2.53M
  fr = index2value(L, fromidx);
257
2.53M
  to = index2value(L, toidx);
258
2.53M
  api_check(L, isvalid(L, to), "invalid index");
259
5.06M
  setobj(L, to, fr);
260
2.53M
  if (isupvalue(toidx))  /* function upvalue? */
261
0
    luaC_barrier(L, clCvalue(s2v(L->ci->func.p)), fr);
262
  /* LUA_REGISTRYINDEX does not need gc barrier
263
     (collector revisits it before finishing collection) */
264
2.53M
  lua_unlock(L);
265
2.53M
}
266
267
268
10.5M
LUA_API void lua_pushvalue (lua_State *L, int idx) {
269
10.5M
  lua_lock(L);
270
10.5M
  setobj2s(L, L->top.p, index2value(L, idx));
271
10.5M
  api_incr_top(L);
272
10.5M
  lua_unlock(L);
273
10.5M
}
274
275
276
277
/*
278
** access functions (stack -> C)
279
*/
280
281
282
804M
LUA_API int lua_type (lua_State *L, int idx) {
283
804M
  const TValue *o = index2value(L, idx);
284
804M
  return (isvalid(L, o) ? ttype(o) : LUA_TNONE);
285
804M
}
286
287
288
170M
LUA_API const char *lua_typename (lua_State *L, int t) {
289
170M
  UNUSED(L);
290
170M
  api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type");
291
170M
  return ttypename(t);
292
170M
}
293
294
295
62.0k
LUA_API int lua_iscfunction (lua_State *L, int idx) {
296
62.0k
  const TValue *o = index2value(L, idx);
297
62.0k
  return (ttislcf(o) || (ttisCclosure(o)));
298
62.0k
}
299
300
301
2.55M
LUA_API int lua_isinteger (lua_State *L, int idx) {
302
2.55M
  const TValue *o = index2value(L, idx);
303
2.55M
  return ttisinteger(o);
304
2.55M
}
305
306
307
221k
LUA_API int lua_isnumber (lua_State *L, int idx) {
308
221k
  lua_Number n;
309
221k
  const TValue *o = index2value(L, idx);
310
221k
  return tonumber(o, &n);
311
221k
}
312
313
314
22.6M
LUA_API int lua_isstring (lua_State *L, int idx) {
315
22.6M
  const TValue *o = index2value(L, idx);
316
22.6M
  return (ttisstring(o) || cvt2str(o));
317
22.6M
}
318
319
320
8
LUA_API int lua_isuserdata (lua_State *L, int idx) {
321
8
  const TValue *o = index2value(L, idx);
322
8
  return (ttisfulluserdata(o) || ttislightuserdata(o));
323
8
}
324
325
326
158M
LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
327
158M
  const TValue *o1 = index2value(L, index1);
328
158M
  const TValue *o2 = index2value(L, index2);
329
158M
  return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0;
330
158M
}
331
332
333
615k
LUA_API void lua_arith (lua_State *L, int op) {
334
615k
  lua_lock(L);
335
615k
  if (op != LUA_OPUNM && op != LUA_OPBNOT)
336
615k
    api_checkpop(L, 2);  /* all other operations expect two operands */
337
24.7k
  else {  /* for unary operations, add fake 2nd operand */
338
24.7k
    api_checkpop(L, 1);
339
49.4k
    setobjs2s(L, L->top.p, L->top.p - 1);
340
24.7k
    api_incr_top(L);
341
24.7k
  }
342
  /* first operand at top - 2, second at top - 1; result go to top - 2 */
343
615k
  luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2);
344
615k
  L->top.p--;  /* pop second operand */
345
615k
  lua_unlock(L);
346
615k
}
347
348
349
693k
LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
350
693k
  const TValue *o1;
351
693k
  const TValue *o2;
352
693k
  int i = 0;
353
693k
  lua_lock(L);  /* may call tag method */
354
693k
  o1 = index2value(L, index1);
355
693k
  o2 = index2value(L, index2);
356
693k
  if (isvalid(L, o1) && isvalid(L, o2)) {
357
693k
    switch (op) {
358
7
      case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
359
693k
      case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
360
27
      case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
361
0
      default: api_check(L, 0, "invalid option");
362
693k
    }
363
693k
  }
364
692k
  lua_unlock(L);
365
692k
  return i;
366
693k
}
367
368
369
42.1M
LUA_API unsigned (lua_numbertocstring) (lua_State *L, int idx, char *buff) {
370
42.1M
  const TValue *o = index2value(L, idx);
371
42.1M
  if (ttisnumber(o)) {
372
17.0M
    unsigned len = luaO_tostringbuff(o, buff);
373
17.0M
    buff[len++] = '\0';  /* add final zero */
374
17.0M
    return len;
375
17.0M
  }
376
25.1M
  else
377
25.1M
    return 0;
378
42.1M
}
379
380
381
108M
LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
382
108M
  size_t sz = luaO_str2num(s, s2v(L->top.p));
383
108M
  if (sz != 0)
384
1.45M
    api_incr_top(L);
385
108M
  return sz;
386
108M
}
387
388
389
3.34M
LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
390
3.34M
  lua_Number n = 0;
391
3.34M
  const TValue *o = index2value(L, idx);
392
3.34M
  int isnum = tonumber(o, &n);
393
3.34M
  if (pisnum)
394
3.32M
    *pisnum = isnum;
395
3.34M
  return n;
396
3.34M
}
397
398
399
7.75M
LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
400
7.75M
  lua_Integer res = 0;
401
7.75M
  const TValue *o = index2value(L, idx);
402
7.75M
  int isnum = tointeger(o, &res);
403
7.75M
  if (pisnum)
404
7.66M
    *pisnum = isnum;
405
7.75M
  return res;
406
7.75M
}
407
408
409
4.54M
LUA_API int lua_toboolean (lua_State *L, int idx) {
410
4.54M
  const TValue *o = index2value(L, idx);
411
4.54M
  return !l_isfalse(o);
412
4.54M
}
413
414
415
198M
LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
416
198M
  TValue *o;
417
198M
  lua_lock(L);
418
198M
  o = index2value(L, idx);
419
198M
  if (!ttisstring(o)) {
420
14.1M
    if (!cvt2str(o)) {  /* not convertible? */
421
10.8M
      if (len != NULL) *len = 0;
422
10.8M
      lua_unlock(L);
423
10.8M
      return NULL;
424
10.8M
    }
425
3.31M
    luaO_tostring(L, o);
426
3.31M
    luaC_checkGC(L);
427
3.31M
    o = index2value(L, idx);  /* previous call may reallocate the stack */
428
3.31M
  }
429
187M
  lua_unlock(L);
430
187M
  if (len != NULL)
431
347M
    return getlstr(tsvalue(o), *len);
432
13.9M
  else
433
27.9M
    return getstr(tsvalue(o));
434
187M
}
435
436
437
52.2k
LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
438
52.2k
  const TValue *o = index2value(L, idx);
439
52.2k
  switch (ttypetag(o)) {
440
34.3k
    case LUA_VSHRSTR: return cast(lua_Unsigned, tsvalue(o)->shrlen);
441
1.37k
    case LUA_VLNGSTR: return cast(lua_Unsigned, tsvalue(o)->u.lnglen);
442
0
    case LUA_VUSERDATA: return cast(lua_Unsigned, uvalue(o)->len);
443
16.5k
    case LUA_VTABLE: {
444
16.5k
      lua_Unsigned res;
445
16.5k
      lua_lock(L);
446
16.5k
      res = luaH_getn(L, hvalue(o));
447
16.5k
      lua_unlock(L);
448
16.5k
      return res;
449
16.5k
    }
450
11
    default: return 0;
451
52.2k
  }
452
52.2k
}
453
454
455
8
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
456
8
  const TValue *o = index2value(L, idx);
457
8
  if (ttislcf(o)) return fvalue(o);
458
7
  else if (ttisCclosure(o))
459
0
    return clCvalue(o)->f;
460
7
  else return NULL;  /* not a C function */
461
8
}
462
463
464
94.6M
l_sinline void *touserdata (const TValue *o) {
465
94.6M
  switch (ttype(o)) {
466
150M
    case LUA_TUSERDATA: return getudatamem(uvalue(o));
467
75.1M
    case LUA_TLIGHTUSERDATA: return pvalue(o);
468
88.4k
    default: return NULL;
469
94.6M
  }
470
94.6M
}
471
472
473
94.6M
LUA_API void *lua_touserdata (lua_State *L, int idx) {
474
94.6M
  const TValue *o = index2value(L, idx);
475
94.6M
  return touserdata(o);
476
94.6M
}
477
478
479
39.0k
LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
480
39.0k
  const TValue *o = index2value(L, idx);
481
39.0k
  return (!ttisthread(o)) ? NULL : thvalue(o);
482
39.0k
}
483
484
485
/*
486
** Returns a pointer to the internal representation of an object.
487
** Note that ISO C does not allow the conversion of a pointer to
488
** function to a 'void*', so the conversion here goes through
489
** a 'size_t'. (As the returned pointer is only informative, this
490
** conversion should not be a problem.)
491
*/
492
1.98M
LUA_API const void *lua_topointer (lua_State *L, int idx) {
493
1.98M
  const TValue *o = index2value(L, idx);
494
1.98M
  switch (ttypetag(o)) {
495
10.1k
    case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
496
0
    case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA:
497
0
      return touserdata(o);
498
1.97M
    default: {
499
1.97M
      if (iscollectable(o))
500
1.97M
        return gcvalue(o);
501
6.69k
      else
502
6.69k
        return NULL;
503
1.97M
    }
504
1.98M
  }
505
1.98M
}
506
507
508
509
/*
510
** push functions (C -> stack)
511
*/
512
513
514
140M
LUA_API void lua_pushnil (lua_State *L) {
515
140M
  lua_lock(L);
516
140M
  setnilvalue(s2v(L->top.p));
517
140M
  api_incr_top(L);
518
140M
  lua_unlock(L);
519
140M
}
520
521
522
1.55M
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
523
1.55M
  lua_lock(L);
524
1.55M
  setfltvalue(s2v(L->top.p), n);
525
1.55M
  api_incr_top(L);
526
1.55M
  lua_unlock(L);
527
1.55M
}
528
529
530
28.2M
LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
531
28.2M
  lua_lock(L);
532
28.2M
  setivalue(s2v(L->top.p), n);
533
28.2M
  api_incr_top(L);
534
28.2M
  lua_unlock(L);
535
28.2M
}
536
537
538
/*
539
** Pushes on the stack a string with given length. Avoid using 's' when
540
** 'len' == 0 (as 's' can be NULL in that case), due to later use of
541
** 'memcmp' and 'memcpy'.
542
*/
543
8.71M
LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
544
8.71M
  TString *ts;
545
8.71M
  lua_lock(L);
546
8.71M
  ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
547
8.71M
  setsvalue2s(L, L->top.p, ts);
548
8.71M
  api_incr_top(L);
549
8.71M
  luaC_checkGC(L);
550
8.71M
  lua_unlock(L);
551
8.71M
  return getstr(ts);
552
8.71M
}
553
554
555
LUA_API const char *lua_pushexternalstring (lua_State *L,
556
952k
          const char *s, size_t len, lua_Alloc falloc, void *ud) {
557
952k
  TString *ts;
558
952k
  lua_lock(L);
559
952k
  api_check(L, len <= MAX_SIZE, "string too large");
560
952k
  api_check(L, s[len] == '\0', "string not ending with zero");
561
952k
  ts = luaS_newextlstr (L, s, len, falloc, ud);
562
952k
  setsvalue2s(L, L->top.p, ts);
563
952k
  api_incr_top(L);
564
952k
  luaC_checkGC(L);
565
952k
  lua_unlock(L);
566
952k
  return getstr(ts);
567
952k
}
568
569
570
197M
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
571
197M
  lua_lock(L);
572
197M
  if (s == NULL)
573
197M
    setnilvalue(s2v(L->top.p));
574
196M
  else {
575
196M
    TString *ts;
576
196M
    ts = luaS_new(L, s);
577
196M
    setsvalue2s(L, L->top.p, ts);
578
196M
    s = getstr(ts);  /* internal copy's address */
579
196M
  }
580
197M
  api_incr_top(L);
581
197M
  luaC_checkGC(L);
582
197M
  lua_unlock(L);
583
197M
  return s;
584
197M
}
585
586
587
LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
588
1.49M
                                      va_list argp) {
589
1.49M
  const char *ret;
590
1.49M
  lua_lock(L);
591
1.49M
  ret = luaO_pushvfstring(L, fmt, argp);
592
1.49M
  luaC_checkGC(L);
593
1.49M
  lua_unlock(L);
594
1.49M
  return ret;
595
1.49M
}
596
597
598
7.41M
LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
599
7.41M
  const char *ret;
600
7.41M
  va_list argp;
601
7.41M
  lua_lock(L);
602
7.41M
  pushvfstring(L, argp, fmt, ret);
603
7.41M
  luaC_checkGC(L);
604
7.41M
  lua_unlock(L);
605
7.41M
  return ret;
606
7.41M
}
607
608
609
7.73M
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
610
7.73M
  lua_lock(L);
611
7.73M
  if (n == 0) {
612
7.00M
    setfvalue(s2v(L->top.p), fn);
613
7.00M
    api_incr_top(L);
614
7.00M
  }
615
732k
  else {
616
732k
    int i;
617
732k
    CClosure *cl;
618
732k
    api_checkpop(L, n);
619
732k
    api_check(L, n <= MAXUPVAL, "upvalue index too large");
620
732k
    cl = luaF_newCclosure(L, n);
621
732k
    cl->f = fn;
622
2.55M
    for (i = 0; i < n; i++) {
623
1.82M
      setobj2n(L, &cl->upvalue[i], s2v(L->top.p - n + i));
624
      /* does not need barrier because closure is white */
625
1.82M
      lua_assert(iswhite(cl));
626
1.82M
    }
627
732k
    L->top.p -= n;
628
732k
    setclCvalue(L, s2v(L->top.p), cl);
629
732k
    api_incr_top(L);
630
732k
    luaC_checkGC(L);
631
732k
  }
632
7.73M
  lua_unlock(L);
633
7.73M
}
634
635
636
11.3M
LUA_API void lua_pushboolean (lua_State *L, int b) {
637
11.3M
  lua_lock(L);
638
11.3M
  if (b)
639
11.3M
    setbtvalue(s2v(L->top.p));
640
5.05M
  else
641
11.3M
    setbfvalue(s2v(L->top.p));
642
11.3M
  api_incr_top(L);
643
11.3M
  lua_unlock(L);
644
11.3M
}
645
646
647
5.30M
LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
648
5.30M
  lua_lock(L);
649
5.30M
  setpvalue(s2v(L->top.p), p);
650
5.30M
  api_incr_top(L);
651
5.30M
  lua_unlock(L);
652
5.30M
}
653
654
655
4.50M
LUA_API int lua_pushthread (lua_State *L) {
656
4.50M
  lua_lock(L);
657
4.50M
  setthvalue(L, s2v(L->top.p), L);
658
4.50M
  api_incr_top(L);
659
4.50M
  lua_unlock(L);
660
4.50M
  return (mainthread(G(L)) == L);
661
4.50M
}
662
663
664
665
/*
666
** get functions (Lua -> stack)
667
*/
668
669
670
22.2M
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
671
22.2M
  lu_byte tag;
672
22.2M
  TString *str = luaS_new(L, k);
673
22.2M
  luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag);
674
22.2M
  if (!tagisempty(tag))
675
21.6M
    api_incr_top(L);
676
633k
  else {
677
633k
    setsvalue2s(L, L->top.p, str);
678
633k
    api_incr_top(L);
679
633k
    tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
680
633k
  }
681
22.2M
  lua_unlock(L);
682
22.2M
  return novariant(tag);
683
22.2M
}
684
685
686
/*
687
** The following function assumes that the registry cannot be a weak
688
** table; so, an emergency collection while using the global table
689
** cannot collect it.
690
*/
691
3.48M
static void getGlobalTable (lua_State *L, TValue *gt) {
692
6.96M
  Table *registry = hvalue(&G(L)->l_registry);
693
6.96M
  lu_byte tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
694
6.96M
  (void)tag;  /* avoid not-used warnings when checks are off */
695
6.96M
  api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist");
696
6.96M
}
697
698
699
2
LUA_API int lua_getglobal (lua_State *L, const char *name) {
700
2
  TValue gt;
701
2
  lua_lock(L);
702
2
  getGlobalTable(L, &gt);
703
2
  return auxgetstr(L, &gt, name);
704
2
}
705
706
707
5.15k
LUA_API int lua_gettable (lua_State *L, int idx) {
708
5.15k
  lu_byte tag;
709
5.15k
  TValue *t;
710
5.15k
  lua_lock(L);
711
5.15k
  api_checkpop(L, 1);
712
5.15k
  t = index2value(L, idx);
713
5.15k
  luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, tag);
714
5.15k
  if (tagisempty(tag))
715
3.42k
    tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
716
5.15k
  lua_unlock(L);
717
5.15k
  return novariant(tag);
718
5.15k
}
719
720
721
22.2M
LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
722
22.2M
  lua_lock(L);
723
22.2M
  return auxgetstr(L, index2value(L, idx), k);
724
22.2M
}
725
726
727
33.6M
LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
728
33.6M
  TValue *t;
729
33.6M
  lu_byte tag;
730
33.6M
  lua_lock(L);
731
33.6M
  t = index2value(L, idx);
732
33.6M
  luaV_fastgeti(t, n, s2v(L->top.p), tag);
733
33.6M
  if (tagisempty(tag)) {
734
11.1M
    TValue key;
735
11.1M
    setivalue(&key, n);
736
11.1M
    tag = luaV_finishget(L, t, &key, L->top.p, tag);
737
11.1M
  }
738
33.6M
  api_incr_top(L);
739
33.6M
  lua_unlock(L);
740
33.6M
  return novariant(tag);
741
33.6M
}
742
743
744
13.2M
static int finishrawget (lua_State *L, lu_byte tag) {
745
13.2M
  if (tagisempty(tag))  /* avoid copying empty items to the stack */
746
13.2M
    setnilvalue(s2v(L->top.p));
747
13.2M
  api_incr_top(L);
748
13.2M
  lua_unlock(L);
749
13.2M
  return novariant(tag);
750
13.2M
}
751
752
753
198M
l_sinline Table *gettable (lua_State *L, int idx) {
754
198M
  TValue *t = index2value(L, idx);
755
198M
  api_check(L, ttistable(t), "table expected");
756
198M
  return hvalue(t);
757
198M
}
758
759
760
12.6M
LUA_API int lua_rawget (lua_State *L, int idx) {
761
12.6M
  Table *t;
762
12.6M
  lu_byte tag;
763
12.6M
  lua_lock(L);
764
12.6M
  api_checkpop(L, 1);
765
12.6M
  t = gettable(L, idx);
766
12.6M
  tag = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1));
767
12.6M
  L->top.p--;  /* pop key */
768
12.6M
  return finishrawget(L, tag);
769
12.6M
}
770
771
772
580k
LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
773
580k
  Table *t;
774
580k
  lu_byte tag;
775
580k
  lua_lock(L);
776
580k
  t = gettable(L, idx);
777
580k
  luaH_fastgeti(t, n, s2v(L->top.p), tag);
778
580k
  return finishrawget(L, tag);
779
580k
}
780
781
782
10
LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
783
10
  Table *t;
784
10
  TValue k;
785
10
  lua_lock(L);
786
10
  t = gettable(L, idx);
787
10
  setpvalue(&k, cast_voidp(p));
788
10
  return finishrawget(L, luaH_get(t, &k, s2v(L->top.p)));
789
10
}
790
791
792
3.58M
LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
793
3.58M
  Table *t;
794
3.58M
  lua_lock(L);
795
3.58M
  t = luaH_new(L);
796
3.58M
  sethvalue2s(L, L->top.p, t);
797
3.58M
  api_incr_top(L);
798
3.58M
  if (narray > 0 || nrec > 0)
799
503k
    luaH_resize(L, t, cast_uint(narray), cast_uint(nrec));
800
3.58M
  luaC_checkGC(L);
801
3.58M
  lua_unlock(L);
802
3.58M
}
803
804
805
17.8M
LUA_API int lua_getmetatable (lua_State *L, int objindex) {
806
17.8M
  const TValue *obj;
807
17.8M
  Table *mt;
808
17.8M
  int res = 0;
809
17.8M
  lua_lock(L);
810
17.8M
  obj = index2value(L, objindex);
811
17.8M
  switch (ttype(obj)) {
812
5.65M
    case LUA_TTABLE:
813
5.65M
      mt = hvalue(obj)->metatable;
814
5.65M
      break;
815
3.52M
    case LUA_TUSERDATA:
816
3.52M
      mt = uvalue(obj)->metatable;
817
3.52M
      break;
818
8.62M
    default:
819
8.62M
      mt = G(L)->mt[ttype(obj)];
820
8.62M
      break;
821
17.8M
  }
822
17.8M
  if (mt != NULL) {
823
12.0M
    sethvalue2s(L, L->top.p, mt);
824
12.0M
    api_incr_top(L);
825
12.0M
    res = 1;
826
12.0M
  }
827
17.8M
  lua_unlock(L);
828
17.8M
  return res;
829
17.8M
}
830
831
832
14
LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
833
14
  TValue *o;
834
14
  int t;
835
14
  lua_lock(L);
836
14
  o = index2value(L, idx);
837
14
  api_check(L, ttisfulluserdata(o), "full userdata expected");
838
28
  if (n <= 0 || n > uvalue(o)->nuvalue) {
839
14
    setnilvalue(s2v(L->top.p));
840
14
    t = LUA_TNONE;
841
14
  }
842
0
  else {
843
0
    setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv);
844
0
    t = ttype(s2v(L->top.p));
845
0
  }
846
14
  api_incr_top(L);
847
14
  lua_unlock(L);
848
14
  return t;
849
14
}
850
851
852
/*
853
** set functions (stack -> Lua)
854
*/
855
856
/*
857
** t[k] = value at the top of the stack (where 'k' is a string)
858
*/
859
51.1M
static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
860
51.1M
  int hres;
861
51.1M
  TString *str = luaS_new(L, k);
862
51.1M
  api_checkpop(L, 1);
863
51.1M
  luaV_fastset(t, str, s2v(L->top.p - 1), hres, luaH_psetstr);
864
51.1M
  if (hres == HOK) {
865
36.4M
    luaV_finishfastset(L, t, s2v(L->top.p - 1));
866
36.4M
    L->top.p--;  /* pop value */
867
36.4M
  }
868
14.6M
  else {
869
14.6M
    setsvalue2s(L, L->top.p, str);  /* push 'str' (to make it a TValue) */
870
14.6M
    api_incr_top(L);
871
14.6M
    luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), hres);
872
14.6M
    L->top.p -= 2;  /* pop value and key */
873
14.6M
  }
874
51.1M
  lua_unlock(L);  /* lock done by caller */
875
51.1M
}
876
877
878
295k
LUA_API void lua_setglobal (lua_State *L, const char *name) {
879
295k
  TValue gt;
880
295k
  lua_lock(L);  /* unlock done in 'auxsetstr' */
881
295k
  getGlobalTable(L, &gt);
882
295k
  auxsetstr(L, &gt, name);
883
295k
}
884
885
886
8
LUA_API void lua_settable (lua_State *L, int idx) {
887
8
  TValue *t;
888
8
  int hres;
889
8
  lua_lock(L);
890
8
  api_checkpop(L, 2);
891
8
  t = index2value(L, idx);
892
8
  luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset);
893
8
  if (hres == HOK)
894
8
    luaV_finishfastset(L, t, s2v(L->top.p - 1));
895
1
  else
896
1
    luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres);
897
8
  L->top.p -= 2;  /* pop index and value */
898
8
  lua_unlock(L);
899
8
}
900
901
902
50.8M
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
903
50.8M
  lua_lock(L);  /* unlock done in 'auxsetstr' */
904
50.8M
  auxsetstr(L, index2value(L, idx), k);
905
50.8M
}
906
907
908
581k
LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
909
581k
  TValue *t;
910
581k
  int hres;
911
581k
  lua_lock(L);
912
581k
  api_checkpop(L, 1);
913
581k
  t = index2value(L, idx);
914
581k
  luaV_fastseti(t, n, s2v(L->top.p - 1), hres);
915
581k
  if (hres == HOK)
916
581k
    luaV_finishfastset(L, t, s2v(L->top.p - 1));
917
8.89k
  else {
918
8.89k
    TValue temp;
919
8.89k
    setivalue(&temp, n);
920
8.89k
    luaV_finishset(L, t, &temp, s2v(L->top.p - 1), hres);
921
8.89k
  }
922
581k
  L->top.p--;  /* pop value */
923
581k
  lua_unlock(L);
924
581k
}
925
926
927
2.08M
static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
928
2.08M
  Table *t;
929
2.08M
  lua_lock(L);
930
2.08M
  api_checkpop(L, n);
931
2.08M
  t = gettable(L, idx);
932
2.08M
  luaH_set(L, t, key, s2v(L->top.p - 1));
933
2.08M
  invalidateTMcache(t);
934
2.08M
  luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
935
2.08M
  L->top.p -= n;
936
2.08M
  lua_unlock(L);
937
2.08M
}
938
939
940
2.08M
LUA_API void lua_rawset (lua_State *L, int idx) {
941
2.08M
  aux_rawset(L, idx, s2v(L->top.p - 2), 2);
942
2.08M
}
943
944
945
0
LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
946
0
  TValue k;
947
0
  setpvalue(&k, cast_voidp(p));
948
0
  aux_rawset(L, idx, &k, 1);
949
0
}
950
951
952
126k
LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
953
126k
  Table *t;
954
126k
  lua_lock(L);
955
126k
  api_checkpop(L, 1);
956
126k
  t = gettable(L, idx);
957
126k
  luaH_setint(L, t, n, s2v(L->top.p - 1));
958
126k
  luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
959
126k
  L->top.p--;
960
126k
  lua_unlock(L);
961
126k
}
962
963
964
5.20M
LUA_API int lua_setmetatable (lua_State *L, int objindex) {
965
5.20M
  TValue *obj;
966
5.20M
  Table *mt;
967
5.20M
  lua_lock(L);
968
5.20M
  api_checkpop(L, 1);
969
5.20M
  obj = index2value(L, objindex);
970
5.20M
  if (ttisnil(s2v(L->top.p - 1)))
971
9.61k
    mt = NULL;
972
5.19M
  else {
973
5.19M
    api_check(L, ttistable(s2v(L->top.p - 1)), "table expected");
974
10.3M
    mt = hvalue(s2v(L->top.p - 1));
975
10.3M
  }
976
5.20M
  switch (ttype(obj)) {
977
2.85M
    case LUA_TTABLE: {
978
5.71M
      hvalue(obj)->metatable = mt;
979
5.71M
      if (mt) {
980
2.85M
        luaC_objbarrier(L, gcvalue(obj), mt);
981
2.85M
        luaC_checkfinalizer(L, gcvalue(obj), mt);
982
2.85M
      }
983
5.71M
      break;
984
5.71M
    }
985
2.85M
    case LUA_TUSERDATA: {
986
4.33M
      uvalue(obj)->metatable = mt;
987
4.33M
      if (mt) {
988
2.15M
        luaC_objbarrier(L, uvalue(obj), mt);
989
2.15M
        luaC_checkfinalizer(L, gcvalue(obj), mt);
990
2.15M
      }
991
4.33M
      break;
992
4.33M
    }
993
2.16M
    default: {
994
179k
      G(L)->mt[ttype(obj)] = mt;
995
179k
      break;
996
4.33M
    }
997
5.20M
  }
998
5.20M
  L->top.p--;
999
5.20M
  lua_unlock(L);
1000
5.20M
  return 1;
1001
5.20M
}
1002
1003
1004
52
LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
1005
52
  TValue *o;
1006
52
  int res;
1007
52
  lua_lock(L);
1008
52
  api_checkpop(L, 1);
1009
52
  o = index2value(L, idx);
1010
52
  api_check(L, ttisfulluserdata(o), "full userdata expected");
1011
104
  if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
1012
51
    res = 0;  /* 'n' not in [1, uvalue(o)->nuvalue] */
1013
1
  else {
1014
3
    setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1));
1015
1
    luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1));
1016
1
    res = 1;
1017
1
  }
1018
52
  L->top.p--;
1019
52
  lua_unlock(L);
1020
52
  return res;
1021
52
}
1022
1023
1024
/*
1025
** 'load' and 'call' functions (run Lua code)
1026
*/
1027
1028
1029
#define checkresults(L,na,nr) \
1030
11.9M
     (api_check(L, (nr) == LUA_MULTRET \
1031
11.9M
               || (L->ci->top.p - L->top.p >= (nr) - (na)), \
1032
11.9M
  "results from function overflow current stack size"), \
1033
11.9M
      api_check(L, LUA_MULTRET <= (nr) && (nr) <= MAXRESULTS,  \
1034
11.9M
                   "invalid number of results"))
1035
1036
1037
LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
1038
8.01M
                        lua_KContext ctx, lua_KFunction k) {
1039
8.01M
  StkId func;
1040
8.01M
  lua_lock(L);
1041
8.01M
  api_check(L, k == NULL || !isLua(L->ci),
1042
8.01M
    "cannot use continuations inside hooks");
1043
8.01M
  api_checkpop(L, nargs + 1);
1044
8.01M
  api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
1045
16.0M
  checkresults(L, nargs, nresults);
1046
16.0M
  func = L->top.p - (nargs+1);
1047
8.01M
  if (k != NULL && yieldable(L)) {  /* need to prepare continuation? */
1048
0
    L->ci->u.c.k = k;  /* save continuation */
1049
0
    L->ci->u.c.ctx = ctx;  /* save context */
1050
0
    luaD_call(L, func, nresults);  /* do the call */
1051
0
  }
1052
8.01M
  else  /* no continuation or no yieldable */
1053
8.01M
    luaD_callnoyield(L, func, nresults);  /* just do the call */
1054
8.01M
  adjustresults(L, nresults);
1055
8.01M
  lua_unlock(L);
1056
8.01M
}
1057
1058
1059
1060
/*
1061
** Execute a protected call.
1062
*/
1063
struct CallS {  /* data to 'f_call' */
1064
  StkId func;
1065
  int nresults;
1066
};
1067
1068
1069
3.94M
static void f_call (lua_State *L, void *ud) {
1070
3.94M
  struct CallS *c = cast(struct CallS *, ud);
1071
3.94M
  luaD_callnoyield(L, c->func, c->nresults);
1072
3.94M
}
1073
1074
1075
1076
LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
1077
3.94M
                        lua_KContext ctx, lua_KFunction k) {
1078
3.94M
  struct CallS c;
1079
3.94M
  TStatus status;
1080
3.94M
  ptrdiff_t func;
1081
3.94M
  lua_lock(L);
1082
3.94M
  api_check(L, k == NULL || !isLua(L->ci),
1083
3.94M
    "cannot use continuations inside hooks");
1084
3.94M
  api_checkpop(L, nargs + 1);
1085
3.94M
  api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
1086
7.89M
  checkresults(L, nargs, nresults);
1087
7.89M
  if (errfunc == 0)
1088
3.83M
    func = 0;
1089
114k
  else {
1090
114k
    StkId o = index2stack(L, errfunc);
1091
114k
    api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
1092
114k
    func = savestack(L, o);
1093
114k
  }
1094
3.94M
  c.func = L->top.p - (nargs+1);  /* function to be called */
1095
3.94M
  if (k == NULL || !yieldable(L)) {  /* no continuation or no yieldable? */
1096
3.94M
    c.nresults = nresults;  /* do a 'conventional' protected call */
1097
3.94M
    status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
1098
3.94M
  }
1099
2.27k
  else {  /* prepare continuation (call is already protected by 'resume') */
1100
2.27k
    CallInfo *ci = L->ci;
1101
2.27k
    ci->u.c.k = k;  /* save continuation */
1102
2.27k
    ci->u.c.ctx = ctx;  /* save context */
1103
    /* save information for error recovery */
1104
2.27k
    ci->u2.funcidx = cast_int(savestack(L, c.func));
1105
2.27k
    ci->u.c.old_errfunc = L->errfunc;
1106
2.27k
    L->errfunc = func;
1107
2.27k
    setoah(ci, L->allowhook);  /* save value of 'allowhook' */
1108
2.27k
    ci->callstatus |= CIST_YPCALL;  /* function can do error recovery */
1109
2.27k
    luaD_call(L, c.func, nresults);  /* do the call */
1110
2.27k
    ci->callstatus &= ~CIST_YPCALL;
1111
2.27k
    L->errfunc = ci->u.c.old_errfunc;
1112
2.27k
    status = LUA_OK;  /* if it is here, there were no errors */
1113
2.27k
  }
1114
3.94M
  adjustresults(L, nresults);
1115
3.94M
  lua_unlock(L);
1116
3.94M
  return APIstatus(status);
1117
3.94M
}
1118
1119
1120
LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
1121
9.17M
                      const char *chunkname, const char *mode) {
1122
9.17M
  ZIO z;
1123
9.17M
  TStatus status;
1124
9.17M
  lua_lock(L);
1125
9.17M
  if (!chunkname) chunkname = "?";
1126
9.17M
  luaZ_init(L, &z, reader, data);
1127
9.17M
  status = luaD_protectedparser(L, &z, chunkname, mode);
1128
9.17M
  if (status == LUA_OK) {  /* no errors? */
1129
6.44M
    LClosure *f = clLvalue(s2v(L->top.p - 1));  /* get new function */
1130
6.44M
    if (f->nupvalues >= 1) {  /* does it have an upvalue? */
1131
      /* get global table from registry */
1132
3.18M
      TValue gt;
1133
3.18M
      getGlobalTable(L, &gt);
1134
      /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
1135
3.18M
      setobj(L, f->upvals[0]->v.p, &gt);
1136
3.18M
      luaC_barrier(L, f->upvals[0], &gt);
1137
3.18M
    }
1138
6.44M
  }
1139
9.17M
  lua_unlock(L);
1140
9.17M
  return APIstatus(status);
1141
9.17M
}
1142
1143
1144
/*
1145
** Dump a Lua function, calling 'writer' to write its parts. Ensure
1146
** the stack returns with its original size.
1147
*/
1148
64.3k
LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
1149
64.3k
  int status;
1150
64.3k
  ptrdiff_t otop = savestack(L, L->top.p);  /* original top */
1151
64.3k
  TValue *f = s2v(L->top.p - 1);  /* function to be dumped */
1152
64.3k
  lua_lock(L);
1153
64.3k
  api_checkpop(L, 1);
1154
64.3k
  api_check(L, isLfunction(f), "Lua function expected");
1155
64.3k
  status = luaU_dump(L, clLvalue(f)->p, writer, data, strip);
1156
64.3k
  L->top.p = restorestack(L, otop);  /* restore top */
1157
64.3k
  lua_unlock(L);
1158
64.3k
  return status;
1159
64.3k
}
1160
1161
1162
16
LUA_API int lua_status (lua_State *L) {
1163
16
  return APIstatus(L->status);
1164
16
}
1165
1166
1167
/*
1168
** Garbage-collection function
1169
*/
1170
2.34M
LUA_API int lua_gc (lua_State *L, int what, ...) {
1171
2.34M
  va_list argp;
1172
2.34M
  int res = 0;
1173
2.34M
  global_State *g = G(L);
1174
2.34M
  if (g->gcstp & (GCSTPGC | GCSTPCLS))  /* internal stop? */
1175
0
    return -1;  /* all options are invalid when stopped */
1176
2.34M
  lua_lock(L);
1177
2.34M
  va_start(argp, what);
1178
2.34M
  switch (what) {
1179
8.02k
    case LUA_GCSTOP: {
1180
8.02k
      g->gcstp = GCSTPUSR;  /* stopped by the user */
1181
8.02k
      break;
1182
0
    }
1183
4.37k
    case LUA_GCRESTART: {
1184
4.37k
      luaE_setdebt(g, 0);
1185
4.37k
      g->gcstp = 0;  /* (other bits must be zero here) */
1186
4.37k
      break;
1187
0
    }
1188
235k
    case LUA_GCCOLLECT: {
1189
235k
      luaC_fullgc(L, 0);
1190
235k
      break;
1191
0
    }
1192
970
    case LUA_GCCOUNT: {
1193
      /* GC values are expressed in Kbytes: #bytes/2^10 */
1194
970
      res = cast_int(gettotalbytes(g) >> 10);
1195
970
      break;
1196
0
    }
1197
919
    case LUA_GCCOUNTB: {
1198
919
      res = cast_int(gettotalbytes(g) & 0x3ff);
1199
919
      break;
1200
0
    }
1201
902k
    case LUA_GCSTEP: {
1202
902k
      lu_byte oldstp = g->gcstp;
1203
902k
      l_mem n = cast(l_mem, va_arg(argp, size_t));
1204
902k
      int work = 0;  /* true if GC did some work */
1205
902k
      g->gcstp = 0;  /* allow GC to run (other bits must be zero here) */
1206
902k
      if (n <= 0)
1207
6.66k
        n = g->GCdebt;  /* force to run one basic step */
1208
902k
      luaE_setdebt(g, g->GCdebt - n);
1209
902k
      luaC_condGC(L, (void)0, work = 1);
1210
902k
      if (work && g->gcstate == GCSpause)  /* end of cycle? */
1211
29.6k
        res = 1;  /* signal it */
1212
902k
      g->gcstp = oldstp;  /* restore previous state */
1213
902k
      break;
1214
0
    }
1215
35.5k
    case LUA_GCISRUNNING: {
1216
35.5k
      res = gcrunning(g);
1217
35.5k
      break;
1218
0
    }
1219
1.14M
    case LUA_GCGEN: {
1220
1.14M
      res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
1221
1.14M
      luaC_changemode(L, KGC_GENMINOR);
1222
1.14M
      break;
1223
0
    }
1224
10.7k
    case LUA_GCINC: {
1225
10.7k
      res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
1226
10.7k
      luaC_changemode(L, KGC_INC);
1227
10.7k
      break;
1228
0
    }
1229
0
    case LUA_GCPARAM: {
1230
0
      int param = va_arg(argp, int);
1231
0
      int value = va_arg(argp, int);
1232
0
      api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter");
1233
0
      res = cast_int(luaO_applyparam(g->gcparams[param], 100));
1234
0
      if (value >= 0)
1235
0
        g->gcparams[param] = luaO_codeparam(cast_uint(value));
1236
0
      break;
1237
0
    }
1238
0
    default: res = -1;  /* invalid option */
1239
2.34M
  }
1240
2.34M
  va_end(argp);
1241
2.34M
  lua_unlock(L);
1242
2.34M
  return res;
1243
2.34M
}
1244
1245
1246
1247
/*
1248
** miscellaneous functions
1249
*/
1250
1251
1252
1.56M
LUA_API int lua_error (lua_State *L) {
1253
1.56M
  TValue *errobj;
1254
1.56M
  lua_lock(L);
1255
1.56M
  errobj = s2v(L->top.p - 1);
1256
1.56M
  api_checkpop(L, 1);
1257
  /* error object is the memory error message? */
1258
1.56M
  if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
1259
1.58k
    luaM_error(L);  /* raise a memory error */
1260
1.56M
  else
1261
1.56M
    luaG_errormsg(L);  /* raise a regular error */
1262
  /* code unreachable; will unlock when control actually leaves the kernel */
1263
0
  return 0;  /* to avoid warnings */
1264
1.56M
}
1265
1266
1267
183M
LUA_API int lua_next (lua_State *L, int idx) {
1268
183M
  Table *t;
1269
183M
  int more;
1270
183M
  lua_lock(L);
1271
183M
  api_checkpop(L, 1);
1272
183M
  t = gettable(L, idx);
1273
183M
  more = luaH_next(L, t, L->top.p - 1);
1274
183M
  if (more)
1275
171M
    api_incr_top(L);
1276
11.2M
  else  /* no more elements */
1277
11.2M
    L->top.p--;  /* pop key */
1278
183M
  lua_unlock(L);
1279
183M
  return more;
1280
183M
}
1281
1282
1283
898k
LUA_API void lua_toclose (lua_State *L, int idx) {
1284
898k
  StkId o;
1285
898k
  lua_lock(L);
1286
898k
  o = index2stack(L, idx);
1287
898k
  api_check(L, L->tbclist.p < o, "given index below or equal a marked one");
1288
898k
  luaF_newtbcupval(L, o);  /* create new to-be-closed upvalue */
1289
898k
  L->ci->callstatus |= CIST_TBC;  /* mark that function has TBC slots */
1290
898k
  lua_unlock(L);
1291
898k
}
1292
1293
1294
2.63M
LUA_API void lua_concat (lua_State *L, int n) {
1295
2.63M
  lua_lock(L);
1296
2.63M
  api_checknelems(L, n);
1297
2.63M
  if (n > 0) {
1298
2.63M
    luaV_concat(L, n);
1299
2.63M
    luaC_checkGC(L);
1300
2.63M
  }
1301
6
  else {  /* nothing to concatenate */
1302
6
    setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0));  /* push empty string */
1303
6
    api_incr_top(L);
1304
6
  }
1305
2.63M
  lua_unlock(L);
1306
2.63M
}
1307
1308
1309
130k
LUA_API void lua_len (lua_State *L, int idx) {
1310
130k
  TValue *t;
1311
130k
  lua_lock(L);
1312
130k
  t = index2value(L, idx);
1313
130k
  luaV_objlen(L, L->top.p, t);
1314
130k
  api_incr_top(L);
1315
130k
  lua_unlock(L);
1316
130k
}
1317
1318
1319
3.16M
LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
1320
3.16M
  lua_Alloc f;
1321
3.16M
  lua_lock(L);
1322
3.16M
  if (ud) *ud = G(L)->ud;
1323
3.16M
  f = G(L)->frealloc;
1324
3.16M
  lua_unlock(L);
1325
3.16M
  return f;
1326
3.16M
}
1327
1328
1329
0
LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
1330
0
  lua_lock(L);
1331
0
  G(L)->ud = ud;
1332
0
  G(L)->frealloc = f;
1333
0
  lua_unlock(L);
1334
0
}
1335
1336
1337
69.5k
void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
1338
69.5k
  lua_lock(L);
1339
69.5k
  G(L)->ud_warn = ud;
1340
69.5k
  G(L)->warnf = f;
1341
69.5k
  lua_unlock(L);
1342
69.5k
}
1343
1344
1345
40.2k
void lua_warning (lua_State *L, const char *msg, int tocont) {
1346
40.2k
  lua_lock(L);
1347
40.2k
  luaE_warning(L, msg, tocont);
1348
40.2k
  lua_unlock(L);
1349
40.2k
}
1350
1351
1352
1353
2.46M
LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
1354
2.46M
  Udata *u;
1355
2.46M
  lua_lock(L);
1356
2.46M
  api_check(L, 0 <= nuvalue && nuvalue < SHRT_MAX, "invalid value");
1357
2.46M
  u = luaS_newudata(L, size, cast(unsigned short, nuvalue));
1358
2.46M
  setuvalue(L, s2v(L->top.p), u);
1359
2.46M
  api_incr_top(L);
1360
2.46M
  luaC_checkGC(L);
1361
2.46M
  lua_unlock(L);
1362
2.46M
  return getudatamem(u);
1363
2.46M
}
1364
1365
1366
1367
static const char *aux_upvalue (TValue *fi, int n, TValue **val,
1368
20.0k
                                GCObject **owner) {
1369
20.0k
  switch (ttypetag(fi)) {
1370
0
    case LUA_VCCL: {  /* C closure */
1371
0
      CClosure *f = clCvalue(fi);
1372
0
      if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
1373
0
        return NULL;  /* 'n' not in [1, f->nupvalues] */
1374
0
      *val = &f->upvalue[n-1];
1375
0
      if (owner) *owner = obj2gco(f);
1376
0
      return "";
1377
0
    }
1378
19.9k
    case LUA_VLCL: {  /* Lua closure */
1379
39.9k
      LClosure *f = clLvalue(fi);
1380
39.9k
      TString *name;
1381
39.9k
      Proto *p = f->p;
1382
39.9k
      if (!(cast_uint(n) - 1u  < cast_uint(p->sizeupvalues)))
1383
3
        return NULL;  /* 'n' not in [1, p->sizeupvalues] */
1384
19.9k
      *val = f->upvals[n-1]->v.p;
1385
19.9k
      if (owner) *owner = obj2gco(f->upvals[n - 1]);
1386
19.9k
      name = p->upvalues[n-1].name;
1387
19.9k
      return (name == NULL) ? "(no name)" : getstr(name);
1388
19.9k
    }
1389
63
    default: return NULL;  /* not a closure */
1390
20.0k
  }
1391
20.0k
}
1392
1393
1394
26
LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1395
26
  const char *name;
1396
26
  TValue *val = NULL;  /* to avoid warnings */
1397
26
  lua_lock(L);
1398
26
  name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
1399
26
  if (name) {
1400
0
    setobj2s(L, L->top.p, val);
1401
0
    api_incr_top(L);
1402
0
  }
1403
26
  lua_unlock(L);
1404
26
  return name;
1405
26
}
1406
1407
1408
20.0k
LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
1409
20.0k
  const char *name;
1410
20.0k
  TValue *val = NULL;  /* to avoid warnings */
1411
20.0k
  GCObject *owner = NULL;  /* to avoid warnings */
1412
20.0k
  TValue *fi;
1413
20.0k
  lua_lock(L);
1414
20.0k
  fi = index2value(L, funcindex);
1415
20.0k
  api_checknelems(L, 1);
1416
20.0k
  name = aux_upvalue(fi, n, &val, &owner);
1417
20.0k
  if (name) {
1418
19.9k
    L->top.p--;
1419
19.9k
    setobj(L, val, s2v(L->top.p));
1420
19.9k
    luaC_barrier(L, owner, val);
1421
19.9k
  }
1422
20.0k
  lua_unlock(L);
1423
20.0k
  return name;
1424
20.0k
}
1425
1426
1427
0
static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
1428
0
  static const UpVal *const nullup = NULL;
1429
0
  LClosure *f;
1430
0
  TValue *fi = index2value(L, fidx);
1431
0
  api_check(L, ttisLclosure(fi), "Lua function expected");
1432
0
  f = clLvalue(fi);
1433
0
  if (pf) *pf = f;
1434
0
  if (1 <= n && n <= f->p->sizeupvalues)
1435
0
    return &f->upvals[n - 1];  /* get its upvalue pointer */
1436
0
  else
1437
0
    return (UpVal**)&nullup;
1438
0
}
1439
1440
1441
0
LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
1442
0
  TValue *fi = index2value(L, fidx);
1443
0
  switch (ttypetag(fi)) {
1444
0
    case LUA_VLCL: {  /* lua closure */
1445
0
      return *getupvalref(L, fidx, n, NULL);
1446
0
    }
1447
0
    case LUA_VCCL: {  /* C closure */
1448
0
      CClosure *f = clCvalue(fi);
1449
0
      if (1 <= n && n <= f->nupvalues)
1450
0
        return &f->upvalue[n - 1];
1451
      /* else */
1452
0
    }  /* FALLTHROUGH */
1453
0
    case LUA_VLCF:
1454
0
      return NULL;  /* light C functions have no upvalues */
1455
0
    default: {
1456
0
      api_check(L, 0, "function expected");
1457
0
      return NULL;
1458
0
    }
1459
0
  }
1460
0
}
1461
1462
1463
LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
1464
0
                                            int fidx2, int n2) {
1465
0
  LClosure *f1;
1466
0
  UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
1467
0
  UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
1468
0
  api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index");
1469
0
  *up1 = *up2;
1470
  luaC_objbarrier(L, f1, *up1);
1471
0
}
1472
1473