Coverage Report

Created: 2025-07-11 06:33

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