Coverage Report

Created: 2025-08-25 07:03

/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.17G
#define isvalid(L, o) ((o) != &G(L)->nilvalue)
45
46
47
/* test for pseudo index */
48
737M
#define ispseudo(i)   ((i) <= LUA_REGISTRYINDEX)
49
50
/* test for upvalue */
51
3.29M
#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.72G
static TValue *index2value (lua_State *L, int idx) {
59
1.72G
  CallInfo *ci = L->ci;
60
1.72G
  if (idx > 0) {
61
984M
    StkId o = ci->func.p + idx;
62
984M
    api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index");
63
984M
    if (o >= L->top.p) return &G(L)->nilvalue;
64
843M
    else return s2v(o);
65
984M
  }
66
737M
  else if (!ispseudo(idx)) {  /* negative index */
67
706M
    api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
68
706M
                 "invalid index");
69
706M
    return s2v(L->top.p + idx);
70
706M
  }
71
31.1M
  else if (idx == LUA_REGISTRYINDEX)
72
30.0M
    return &G(L)->l_registry;
73
1.11M
  else {  /* upvalues */
74
1.11M
    idx = LUA_REGISTRYINDEX - idx;
75
1.11M
    api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
76
1.11M
    if (ttisCclosure(s2v(ci->func.p))) {  /* C closure? */
77
1.11M
      CClosure *func = clCvalue(s2v(ci->func.p));
78
1.11M
      return (idx <= func->nupvalues) ? &func->upvalue[idx-1]
79
1.11M
                                      : &G(L)->nilvalue;
80
1.11M
    }
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.11M
  }
86
1.72G
}
87
88
89
90
/*
91
** Convert a valid actual index (not a pseudo-index) to its address.
92
*/
93
24.6M
static StkId index2stack (lua_State *L, int idx) {
94
24.6M
  CallInfo *ci = L->ci;
95
24.6M
  if (idx > 0) {
96
4.96M
    StkId o = ci->func.p + idx;
97
4.96M
    api_check(L, o < L->top.p, "invalid index");
98
4.96M
    return o;
99
4.96M
  }
100
19.6M
  else {    /* non-positive index */
101
19.6M
    api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
102
19.6M
                 "invalid index");
103
19.6M
    api_check(L, !ispseudo(idx), "invalid index");
104
19.6M
    return L->top.p + idx;
105
19.6M
  }
106
24.6M
}
107
108
109
7.84M
LUA_API int lua_checkstack (lua_State *L, int n) {
110
7.84M
  int res;
111
7.84M
  CallInfo *ci;
112
7.84M
  lua_lock(L);
113
7.84M
  ci = L->ci;
114
7.84M
  api_check(L, n >= 0, "negative 'n'");
115
7.84M
  if (L->stack_last.p - L->top.p > n)  /* stack large enough? */
116
7.82M
    res = 1;  /* yes; check is OK */
117
17.9k
  else  /* need to grow stack */
118
17.9k
    res = luaD_growstack(L, n, 0);
119
7.84M
  if (res && ci->top.p < L->top.p + n)
120
164k
    ci->top.p = L->top.p + n;  /* adjust frame top */
121
7.84M
  lua_unlock(L);
122
7.84M
  return res;
123
7.84M
}
124
125
126
753k
LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
127
753k
  int i;
128
753k
  if (from == to) return;
129
45.6k
  lua_lock(to);
130
45.6k
  api_checkpop(from, n);
131
45.6k
  api_check(from, G(from) == G(to), "moving among independent states");
132
45.6k
  api_check(from, to->ci->top.p - to->top.p >= n, "stack overflow");
133
45.6k
  from->top.p -= n;
134
139k
  for (i = 0; i < n; i++) {
135
94.0k
    setobjs2s(to, to->top.p, from->top.p + i);
136
94.0k
    to->top.p++;  /* stack already checked by previous 'api_check' */
137
94.0k
  }
138
45.6k
  lua_unlock(to);
139
45.6k
}
140
141
142
59.7k
LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
143
59.7k
  lua_CFunction old;
144
59.7k
  lua_lock(L);
145
59.7k
  old = G(L)->panic;
146
59.7k
  G(L)->panic = panicf;
147
59.7k
  lua_unlock(L);
148
59.7k
  return old;
149
59.7k
}
150
151
152
241k
LUA_API lua_Number lua_version (lua_State *L) {
153
241k
  UNUSED(L);
154
241k
  return LUA_VERSION_NUM;
155
241k
}
156
157
158
159
/*
160
** basic stack manipulation
161
*/
162
163
164
/*
165
** convert an acceptable stack index into an absolute index
166
*/
167
8.30M
LUA_API int lua_absindex (lua_State *L, int idx) {
168
8.30M
  return (idx > 0 || ispseudo(idx))
169
8.30M
         ? idx
170
8.30M
         : cast_int(L->top.p - L->ci->func.p) + idx;
171
8.30M
}
172
173
174
26.0M
LUA_API int lua_gettop (lua_State *L) {
175
26.0M
  return cast_int(L->top.p - (L->ci->func.p + 1));
176
26.0M
}
177
178
179
216M
LUA_API void lua_settop (lua_State *L, int idx) {
180
216M
  CallInfo *ci;
181
216M
  StkId func, newtop;
182
216M
  ptrdiff_t diff;  /* difference for new top */
183
216M
  lua_lock(L);
184
216M
  ci = L->ci;
185
216M
  func = ci->func.p;
186
216M
  if (idx >= 0) {
187
12.1M
    api_check(L, idx <= ci->top.p - (func + 1), "new top too large");
188
12.1M
    diff = ((func + 1) + idx) - L->top.p;
189
27.5M
    for (; diff > 0; diff--)
190
15.4M
      setnilvalue(s2v(L->top.p++));  /* clear new slots */
191
12.1M
  }
192
204M
  else {
193
204M
    api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top");
194
204M
    diff = idx + 1;  /* will "subtract" index (as it is negative) */
195
204M
  }
196
216M
  newtop = L->top.p + diff;
197
216M
  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
216M
  L->top.p = newtop;  /* correct top only after closing any upvalue */
202
216M
  lua_unlock(L);
203
216M
}
204
205
206
905k
LUA_API void lua_closeslot (lua_State *L, int idx) {
207
905k
  StkId level;
208
905k
  lua_lock(L);
209
905k
  level = index2stack(L, idx);
210
905k
  api_check(L, (L->ci->callstatus & CIST_TBC) && (L->tbclist.p == level),
211
905k
     "no variable to close at given level");
212
905k
  level = luaF_close(L, level, CLOSEKTOP, 0);
213
905k
  setnilvalue(s2v(level));
214
905k
  lua_unlock(L);
215
905k
}
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
68.1M
static void reverse (lua_State *L, StkId from, StkId to) {
225
90.2M
  for (; from < to; from++, to--) {
226
22.1M
    TValue temp;
227
22.1M
    setobj(L, &temp, s2v(from));
228
22.1M
    setobjs2s(L, from, to);
229
22.1M
    setobj2s(L, to, &temp);
230
22.1M
  }
231
68.1M
}
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
22.7M
LUA_API void lua_rotate (lua_State *L, int idx, int n) {
239
22.7M
  StkId p, t, m;
240
22.7M
  lua_lock(L);
241
22.7M
  t = L->top.p - 1;  /* end of stack segment being rotated */
242
22.7M
  p = index2stack(L, idx);  /* start of segment */
243
22.7M
  api_check(L, L->tbclist.p < p, "moving a to-be-closed slot");
244
22.7M
  api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
245
22.7M
  m = (n >= 0 ? t - n : p - n - 1);  /* end of prefix */
246
22.7M
  reverse(L, p, m);  /* reverse the prefix with length 'n' */
247
22.7M
  reverse(L, m + 1, t);  /* reverse the suffix */
248
22.7M
  reverse(L, p, t);  /* reverse the entire segment */
249
22.7M
  lua_unlock(L);
250
22.7M
}
251
252
253
3.29M
LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
254
3.29M
  TValue *fr, *to;
255
3.29M
  lua_lock(L);
256
3.29M
  fr = index2value(L, fromidx);
257
3.29M
  to = index2value(L, toidx);
258
3.29M
  api_check(L, isvalid(L, to), "invalid index");
259
6.59M
  setobj(L, to, fr);
260
3.29M
  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
3.29M
  lua_unlock(L);
265
3.29M
}
266
267
268
10.3M
LUA_API void lua_pushvalue (lua_State *L, int idx) {
269
10.3M
  lua_lock(L);
270
10.3M
  setobj2s(L, L->top.p, index2value(L, idx));
271
10.3M
  api_incr_top(L);
272
10.3M
  lua_unlock(L);
273
10.3M
}
274
275
276
277
/*
278
** access functions (stack -> C)
279
*/
280
281
282
700M
LUA_API int lua_type (lua_State *L, int idx) {
283
700M
  const TValue *o = index2value(L, idx);
284
700M
  return (isvalid(L, o) ? ttype(o) : LUA_TNONE);
285
700M
}
286
287
288
148M
LUA_API const char *lua_typename (lua_State *L, int t) {
289
148M
  UNUSED(L);
290
148M
  api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type");
291
148M
  return ttypename(t);
292
148M
}
293
294
295
10.1k
LUA_API int lua_iscfunction (lua_State *L, int idx) {
296
10.1k
  const TValue *o = index2value(L, idx);
297
10.1k
  return (ttislcf(o) || (ttisCclosure(o)));
298
10.1k
}
299
300
301
52.3k
LUA_API int lua_isinteger (lua_State *L, int idx) {
302
52.3k
  const TValue *o = index2value(L, idx);
303
52.3k
  return ttisinteger(o);
304
52.3k
}
305
306
307
222k
LUA_API int lua_isnumber (lua_State *L, int idx) {
308
222k
  lua_Number n;
309
222k
  const TValue *o = index2value(L, idx);
310
222k
  return tonumber(o, &n);
311
222k
}
312
313
314
16.4M
LUA_API int lua_isstring (lua_State *L, int idx) {
315
16.4M
  const TValue *o = index2value(L, idx);
316
16.4M
  return (ttisstring(o) || cvt2str(o));
317
16.4M
}
318
319
320
24
LUA_API int lua_isuserdata (lua_State *L, int idx) {
321
24
  const TValue *o = index2value(L, idx);
322
24
  return (ttisfulluserdata(o) || ttislightuserdata(o));
323
24
}
324
325
326
156M
LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
327
156M
  const TValue *o1 = index2value(L, index1);
328
156M
  const TValue *o2 = index2value(L, index2);
329
156M
  return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0;
330
156M
}
331
332
333
541k
LUA_API void lua_arith (lua_State *L, int op) {
334
541k
  lua_lock(L);
335
541k
  if (op != LUA_OPUNM && op != LUA_OPBNOT)
336
541k
    api_checkpop(L, 2);  /* all other operations expect two operands */
337
46.2k
  else {  /* for unary operations, add fake 2nd operand */
338
46.2k
    api_checkpop(L, 1);
339
92.4k
    setobjs2s(L, L->top.p, L->top.p - 1);
340
46.2k
    api_incr_top(L);
341
46.2k
  }
342
  /* first operand at top - 2, second at top - 1; result go to top - 2 */
343
541k
  luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2);
344
541k
  L->top.p--;  /* pop second operand */
345
541k
  lua_unlock(L);
346
541k
}
347
348
349
956k
LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
350
956k
  const TValue *o1;
351
956k
  const TValue *o2;
352
956k
  int i = 0;
353
956k
  lua_lock(L);  /* may call tag method */
354
956k
  o1 = index2value(L, index1);
355
956k
  o2 = index2value(L, index2);
356
956k
  if (isvalid(L, o1) && isvalid(L, o2)) {
357
956k
    switch (op) {
358
9
      case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
359
956k
      case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
360
26
      case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
361
0
      default: api_check(L, 0, "invalid option");
362
956k
    }
363
956k
  }
364
956k
  lua_unlock(L);
365
956k
  return i;
366
956k
}
367
368
369
56.7M
LUA_API unsigned (lua_numbertocstring) (lua_State *L, int idx, char *buff) {
370
56.7M
  const TValue *o = index2value(L, idx);
371
56.7M
  if (ttisnumber(o)) {
372
22.9M
    unsigned len = luaO_tostringbuff(o, buff);
373
22.9M
    buff[len++] = '\0';  /* add final zero */
374
22.9M
    return len;
375
22.9M
  }
376
33.8M
  else
377
33.8M
    return 0;
378
56.7M
}
379
380
381
93.6M
LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
382
93.6M
  size_t sz = luaO_str2num(s, s2v(L->top.p));
383
93.6M
  if (sz != 0)
384
1.36M
    api_incr_top(L);
385
93.6M
  return sz;
386
93.6M
}
387
388
389
849k
LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
390
849k
  lua_Number n = 0;
391
849k
  const TValue *o = index2value(L, idx);
392
849k
  int isnum = tonumber(o, &n);
393
849k
  if (pisnum)
394
845k
    *pisnum = isnum;
395
849k
  return n;
396
849k
}
397
398
399
14.2M
LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
400
14.2M
  lua_Integer res = 0;
401
14.2M
  const TValue *o = index2value(L, idx);
402
14.2M
  int isnum = tointeger(o, &res);
403
14.2M
  if (pisnum)
404
14.0M
    *pisnum = isnum;
405
14.2M
  return res;
406
14.2M
}
407
408
409
4.45M
LUA_API int lua_toboolean (lua_State *L, int idx) {
410
4.45M
  const TValue *o = index2value(L, idx);
411
4.45M
  return !l_isfalse(o);
412
4.45M
}
413
414
415
187M
LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
416
187M
  TValue *o;
417
187M
  lua_lock(L);
418
187M
  o = index2value(L, idx);
419
187M
  if (!ttisstring(o)) {
420
10.3M
    if (!cvt2str(o)) {  /* not convertible? */
421
7.33M
      if (len != NULL) *len = 0;
422
7.33M
      lua_unlock(L);
423
7.33M
      return NULL;
424
7.33M
    }
425
3.06M
    luaO_tostring(L, o);
426
3.06M
    luaC_checkGC(L);
427
3.06M
    o = index2value(L, idx);  /* previous call may reallocate the stack */
428
3.06M
  }
429
180M
  lua_unlock(L);
430
180M
  if (len != NULL)
431
331M
    return getlstr(tsvalue(o), *len);
432
14.2M
  else
433
28.5M
    return getstr(tsvalue(o));
434
180M
}
435
436
437
127k
LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
438
127k
  const TValue *o = index2value(L, idx);
439
127k
  switch (ttypetag(o)) {
440
107k
    case LUA_VSHRSTR: return cast(lua_Unsigned, tsvalue(o)->shrlen);
441
720
    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.5k
    case LUA_VTABLE: return luaH_getn(L, hvalue(o));
444
3
    default: return 0;
445
127k
  }
446
127k
}
447
448
449
26
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
450
26
  const TValue *o = index2value(L, idx);
451
26
  if (ttislcf(o)) return fvalue(o);
452
24
  else if (ttisCclosure(o))
453
0
    return clCvalue(o)->f;
454
24
  else return NULL;  /* not a C function */
455
26
}
456
457
458
84.3M
l_sinline void *touserdata (const TValue *o) {
459
84.3M
  switch (ttype(o)) {
460
135M
    case LUA_TUSERDATA: return getudatamem(uvalue(o));
461
67.9M
    case LUA_TLIGHTUSERDATA: return pvalue(o);
462
121k
    default: return NULL;
463
84.3M
  }
464
84.3M
}
465
466
467
84.3M
LUA_API void *lua_touserdata (lua_State *L, int idx) {
468
84.3M
  const TValue *o = index2value(L, idx);
469
84.3M
  return touserdata(o);
470
84.3M
}
471
472
473
16.8k
LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
474
16.8k
  const TValue *o = index2value(L, idx);
475
16.8k
  return (!ttisthread(o)) ? NULL : thvalue(o);
476
16.8k
}
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
1.27M
LUA_API const void *lua_topointer (lua_State *L, int idx) {
487
1.27M
  const TValue *o = index2value(L, idx);
488
1.27M
  switch (ttypetag(o)) {
489
14.2k
    case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
490
0
    case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA:
491
0
      return touserdata(o);
492
1.25M
    default: {
493
1.25M
      if (iscollectable(o))
494
1.25M
        return gcvalue(o);
495
367
      else
496
367
        return NULL;
497
1.25M
    }
498
1.27M
  }
499
1.27M
}
500
501
502
503
/*
504
** push functions (C -> stack)
505
*/
506
507
508
118M
LUA_API void lua_pushnil (lua_State *L) {
509
118M
  lua_lock(L);
510
118M
  setnilvalue(s2v(L->top.p));
511
118M
  api_incr_top(L);
512
118M
  lua_unlock(L);
513
118M
}
514
515
516
80.3k
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
517
80.3k
  lua_lock(L);
518
80.3k
  setfltvalue(s2v(L->top.p), n);
519
80.3k
  api_incr_top(L);
520
80.3k
  lua_unlock(L);
521
80.3k
}
522
523
524
33.3M
LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
525
33.3M
  lua_lock(L);
526
33.3M
  setivalue(s2v(L->top.p), n);
527
33.3M
  api_incr_top(L);
528
33.3M
  lua_unlock(L);
529
33.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
12.2M
LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
538
12.2M
  TString *ts;
539
12.2M
  lua_lock(L);
540
12.2M
  ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
541
12.2M
  setsvalue2s(L, L->top.p, ts);
542
12.2M
  api_incr_top(L);
543
12.2M
  luaC_checkGC(L);
544
12.2M
  lua_unlock(L);
545
12.2M
  return getstr(ts);
546
12.2M
}
547
548
549
LUA_API const char *lua_pushexternalstring (lua_State *L,
550
958k
          const char *s, size_t len, lua_Alloc falloc, void *ud) {
551
958k
  TString *ts;
552
958k
  lua_lock(L);
553
958k
  api_check(L, len <= MAX_SIZE, "string too large");
554
958k
  api_check(L, s[len] == '\0', "string not ending with zero");
555
958k
  ts = luaS_newextlstr (L, s, len, falloc, ud);
556
958k
  setsvalue2s(L, L->top.p, ts);
557
958k
  api_incr_top(L);
558
958k
  luaC_checkGC(L);
559
958k
  lua_unlock(L);
560
958k
  return getstr(ts);
561
958k
}
562
563
564
177M
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
565
177M
  lua_lock(L);
566
177M
  if (s == NULL)
567
177M
    setnilvalue(s2v(L->top.p));
568
176M
  else {
569
176M
    TString *ts;
570
176M
    ts = luaS_new(L, s);
571
176M
    setsvalue2s(L, L->top.p, ts);
572
176M
    s = getstr(ts);  /* internal copy's address */
573
176M
  }
574
177M
  api_incr_top(L);
575
177M
  luaC_checkGC(L);
576
177M
  lua_unlock(L);
577
177M
  return s;
578
177M
}
579
580
581
LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
582
1.87M
                                      va_list argp) {
583
1.87M
  const char *ret;
584
1.87M
  lua_lock(L);
585
1.87M
  ret = luaO_pushvfstring(L, fmt, argp);
586
1.87M
  luaC_checkGC(L);
587
1.87M
  lua_unlock(L);
588
1.87M
  return ret;
589
1.87M
}
590
591
592
6.97M
LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
593
6.97M
  const char *ret;
594
6.97M
  va_list argp;
595
6.97M
  lua_lock(L);
596
6.97M
  pushvfstring(L, argp, fmt, ret);
597
6.97M
  luaC_checkGC(L);
598
6.97M
  lua_unlock(L);
599
6.97M
  return ret;
600
6.97M
}
601
602
603
4.98M
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
604
4.98M
  lua_lock(L);
605
4.98M
  if (n == 0) {
606
4.11M
    setfvalue(s2v(L->top.p), fn);
607
4.11M
    api_incr_top(L);
608
4.11M
  }
609
869k
  else {
610
869k
    int i;
611
869k
    CClosure *cl;
612
869k
    api_checkpop(L, n);
613
869k
    api_check(L, n <= MAXUPVAL, "upvalue index too large");
614
869k
    cl = luaF_newCclosure(L, n);
615
869k
    cl->f = fn;
616
3.12M
    for (i = 0; i < n; i++) {
617
2.25M
      setobj2n(L, &cl->upvalue[i], s2v(L->top.p - n + i));
618
      /* does not need barrier because closure is white */
619
2.25M
      lua_assert(iswhite(cl));
620
2.25M
    }
621
869k
    L->top.p -= n;
622
869k
    setclCvalue(L, s2v(L->top.p), cl);
623
869k
    api_incr_top(L);
624
869k
    luaC_checkGC(L);
625
869k
  }
626
4.98M
  lua_unlock(L);
627
4.98M
}
628
629
630
13.5M
LUA_API void lua_pushboolean (lua_State *L, int b) {
631
13.5M
  lua_lock(L);
632
13.5M
  if (b)
633
13.5M
    setbtvalue(s2v(L->top.p));
634
5.56M
  else
635
13.5M
    setbfvalue(s2v(L->top.p));
636
13.5M
  api_incr_top(L);
637
13.5M
  lua_unlock(L);
638
13.5M
}
639
640
641
5.66M
LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
642
5.66M
  lua_lock(L);
643
5.66M
  setpvalue(s2v(L->top.p), p);
644
5.66M
  api_incr_top(L);
645
5.66M
  lua_unlock(L);
646
5.66M
}
647
648
649
5.40M
LUA_API int lua_pushthread (lua_State *L) {
650
5.40M
  lua_lock(L);
651
5.40M
  setthvalue(L, s2v(L->top.p), L);
652
5.40M
  api_incr_top(L);
653
5.40M
  lua_unlock(L);
654
5.40M
  return (mainthread(G(L)) == L);
655
5.40M
}
656
657
658
659
/*
660
** get functions (Lua -> stack)
661
*/
662
663
664
29.8M
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
665
29.8M
  lu_byte tag;
666
29.8M
  TString *str = luaS_new(L, k);
667
29.8M
  luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag);
668
29.8M
  if (!tagisempty(tag))
669
29.3M
    api_incr_top(L);
670
552k
  else {
671
552k
    setsvalue2s(L, L->top.p, str);
672
552k
    api_incr_top(L);
673
552k
    tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
674
552k
  }
675
29.8M
  lua_unlock(L);
676
29.8M
  return novariant(tag);
677
29.8M
}
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
4.12M
static void getGlobalTable (lua_State *L, TValue *gt) {
686
8.24M
  Table *registry = hvalue(&G(L)->l_registry);
687
4.12M
  lu_byte tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
688
8.24M
  (void)tag;  /* avoid not-used warnings when checks are off */
689
8.24M
  api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist");
690
8.24M
}
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.44k
LUA_API int lua_gettable (lua_State *L, int idx) {
702
3.44k
  lu_byte tag;
703
3.44k
  TValue *t;
704
3.44k
  lua_lock(L);
705
3.44k
  api_checkpop(L, 1);
706
3.44k
  t = index2value(L, idx);
707
3.44k
  luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, tag);
708
3.44k
  if (tagisempty(tag))
709
2.51k
    tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
710
3.44k
  lua_unlock(L);
711
3.44k
  return novariant(tag);
712
3.44k
}
713
714
715
29.8M
LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
716
29.8M
  lua_lock(L);
717
29.8M
  return auxgetstr(L, index2value(L, idx), k);
718
29.8M
}
719
720
721
30.5M
LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
722
30.5M
  TValue *t;
723
30.5M
  lu_byte tag;
724
30.5M
  lua_lock(L);
725
30.5M
  t = index2value(L, idx);
726
30.5M
  luaV_fastgeti(t, n, s2v(L->top.p), tag);
727
30.5M
  if (tagisempty(tag)) {
728
14.5M
    TValue key;
729
14.5M
    setivalue(&key, n);
730
14.5M
    tag = luaV_finishget(L, t, &key, L->top.p, tag);
731
14.5M
  }
732
30.5M
  api_incr_top(L);
733
30.5M
  lua_unlock(L);
734
30.5M
  return novariant(tag);
735
30.5M
}
736
737
738
11.9M
static int finishrawget (lua_State *L, lu_byte tag) {
739
11.9M
  if (tagisempty(tag))  /* avoid copying empty items to the stack */
740
11.9M
    setnilvalue(s2v(L->top.p));
741
11.9M
  api_incr_top(L);
742
11.9M
  lua_unlock(L);
743
11.9M
  return novariant(tag);
744
11.9M
}
745
746
747
180M
l_sinline Table *gettable (lua_State *L, int idx) {
748
180M
  TValue *t = index2value(L, idx);
749
180M
  api_check(L, ttistable(t), "table expected");
750
180M
  return hvalue(t);
751
180M
}
752
753
754
11.4M
LUA_API int lua_rawget (lua_State *L, int idx) {
755
11.4M
  Table *t;
756
11.4M
  lu_byte tag;
757
11.4M
  lua_lock(L);
758
11.4M
  api_checkpop(L, 1);
759
11.4M
  t = gettable(L, idx);
760
11.4M
  tag = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1));
761
11.4M
  L->top.p--;  /* pop key */
762
11.4M
  return finishrawget(L, tag);
763
11.4M
}
764
765
766
468k
LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
767
468k
  Table *t;
768
468k
  lu_byte tag;
769
468k
  lua_lock(L);
770
468k
  t = gettable(L, idx);
771
468k
  luaH_fastgeti(t, n, s2v(L->top.p), tag);
772
468k
  return finishrawget(L, tag);
773
468k
}
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
3.94M
LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
787
3.94M
  Table *t;
788
3.94M
  lua_lock(L);
789
3.94M
  t = luaH_new(L);
790
3.94M
  sethvalue2s(L, L->top.p, t);
791
3.94M
  api_incr_top(L);
792
3.94M
  if (narray > 0 || nrec > 0)
793
438k
    luaH_resize(L, t, cast_uint(narray), cast_uint(nrec));
794
3.94M
  luaC_checkGC(L);
795
3.94M
  lua_unlock(L);
796
3.94M
}
797
798
799
14.4M
LUA_API int lua_getmetatable (lua_State *L, int objindex) {
800
14.4M
  const TValue *obj;
801
14.4M
  Table *mt;
802
14.4M
  int res = 0;
803
14.4M
  lua_lock(L);
804
14.4M
  obj = index2value(L, objindex);
805
14.4M
  switch (ttype(obj)) {
806
2.08M
    case LUA_TTABLE:
807
2.08M
      mt = hvalue(obj)->metatable;
808
0
      break;
809
5.96M
    case LUA_TUSERDATA:
810
5.96M
      mt = uvalue(obj)->metatable;
811
0
      break;
812
6.39M
    default:
813
6.39M
      mt = G(L)->mt[ttype(obj)];
814
6.39M
      break;
815
14.4M
  }
816
14.4M
  if (mt != NULL) {
817
12.6M
    sethvalue2s(L, L->top.p, mt);
818
12.6M
    api_incr_top(L);
819
12.6M
    res = 1;
820
12.6M
  }
821
14.4M
  lua_unlock(L);
822
14.4M
  return res;
823
14.4M
}
824
825
826
23
LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
827
23
  TValue *o;
828
23
  int t;
829
23
  lua_lock(L);
830
23
  o = index2value(L, idx);
831
23
  api_check(L, ttisfulluserdata(o), "full userdata expected");
832
46
  if (n <= 0 || n > uvalue(o)->nuvalue) {
833
23
    setnilvalue(s2v(L->top.p));
834
23
    t = LUA_TNONE;
835
23
  }
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
23
  api_incr_top(L);
841
23
  lua_unlock(L);
842
23
  return t;
843
23
}
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
58.9M
static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
854
58.9M
  int hres;
855
58.9M
  TString *str = luaS_new(L, k);
856
58.9M
  api_checkpop(L, 1);
857
58.9M
  luaV_fastset(t, str, s2v(L->top.p - 1), hres, luaH_psetstr);
858
58.9M
  if (hres == HOK) {
859
41.9M
    luaV_finishfastset(L, t, s2v(L->top.p - 1));
860
41.9M
    L->top.p--;  /* pop value */
861
41.9M
  }
862
17.0M
  else {
863
17.0M
    setsvalue2s(L, L->top.p, str);  /* push 'str' (to make it a TValue) */
864
17.0M
    api_incr_top(L);
865
17.0M
    luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), hres);
866
17.0M
    L->top.p -= 2;  /* pop value and key */
867
17.0M
  }
868
58.9M
  lua_unlock(L);  /* lock done by caller */
869
58.9M
}
870
871
872
268k
LUA_API void lua_setglobal (lua_State *L, const char *name) {
873
268k
  TValue gt;
874
268k
  lua_lock(L);  /* unlock done in 'auxsetstr' */
875
268k
  getGlobalTable(L, &gt);
876
268k
  auxsetstr(L, &gt, name);
877
268k
}
878
879
880
9
LUA_API void lua_settable (lua_State *L, int idx) {
881
9
  TValue *t;
882
9
  int hres;
883
9
  lua_lock(L);
884
9
  api_checkpop(L, 2);
885
9
  t = index2value(L, idx);
886
9
  luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset);
887
9
  if (hres == HOK)
888
9
    luaV_finishfastset(L, t, s2v(L->top.p - 1));
889
1
  else
890
1
    luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres);
891
9
  L->top.p -= 2;  /* pop index and value */
892
9
  lua_unlock(L);
893
9
}
894
895
896
58.7M
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
897
58.7M
  lua_lock(L);  /* unlock done in 'auxsetstr' */
898
58.7M
  auxsetstr(L, index2value(L, idx), k);
899
58.7M
}
900
901
902
684k
LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
903
684k
  TValue *t;
904
684k
  int hres;
905
684k
  lua_lock(L);
906
684k
  api_checkpop(L, 1);
907
684k
  t = index2value(L, idx);
908
684k
  luaV_fastseti(t, n, s2v(L->top.p - 1), hres);
909
684k
  if (hres == HOK)
910
684k
    luaV_finishfastset(L, t, s2v(L->top.p - 1));
911
12.3k
  else {
912
12.3k
    TValue temp;
913
12.3k
    setivalue(&temp, n);
914
12.3k
    luaV_finishset(L, t, &temp, s2v(L->top.p - 1), hres);
915
12.3k
  }
916
684k
  L->top.p--;  /* pop value */
917
684k
  lua_unlock(L);
918
684k
}
919
920
921
927k
static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
922
927k
  Table *t;
923
927k
  lua_lock(L);
924
927k
  api_checkpop(L, n);
925
927k
  t = gettable(L, idx);
926
927k
  luaH_set(L, t, key, s2v(L->top.p - 1));
927
927k
  invalidateTMcache(t);
928
927k
  luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
929
927k
  L->top.p -= n;
930
927k
  lua_unlock(L);
931
927k
}
932
933
934
927k
LUA_API void lua_rawset (lua_State *L, int idx) {
935
927k
  aux_rawset(L, idx, s2v(L->top.p - 2), 2);
936
927k
}
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
115k
LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
947
115k
  Table *t;
948
115k
  lua_lock(L);
949
115k
  api_checkpop(L, 1);
950
115k
  t = gettable(L, idx);
951
115k
  luaH_setint(L, t, n, s2v(L->top.p - 1));
952
115k
  luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
953
115k
  L->top.p--;
954
115k
  lua_unlock(L);
955
115k
}
956
957
958
4.90M
LUA_API int lua_setmetatable (lua_State *L, int objindex) {
959
4.90M
  TValue *obj;
960
4.90M
  Table *mt;
961
4.90M
  lua_lock(L);
962
4.90M
  api_checkpop(L, 1);
963
4.90M
  obj = index2value(L, objindex);
964
4.90M
  if (ttisnil(s2v(L->top.p - 1)))
965
8.92k
    mt = NULL;
966
4.89M
  else {
967
4.89M
    api_check(L, ttistable(s2v(L->top.p - 1)), "table expected");
968
9.78M
    mt = hvalue(s2v(L->top.p - 1));
969
9.78M
  }
970
4.90M
  switch (ttype(obj)) {
971
1.68M
    case LUA_TTABLE: {
972
3.37M
      hvalue(obj)->metatable = mt;
973
1.68M
      if (mt) {
974
1.68M
        luaC_objbarrier(L, gcvalue(obj), mt);
975
1.68M
        luaC_checkfinalizer(L, gcvalue(obj), mt);
976
1.68M
      }
977
1.68M
      break;
978
3.37M
    }
979
3.03M
    case LUA_TUSERDATA: {
980
6.07M
      uvalue(obj)->metatable = mt;
981
3.03M
      if (mt) {
982
3.02M
        luaC_objbarrier(L, uvalue(obj), mt);
983
3.02M
        luaC_checkfinalizer(L, gcvalue(obj), mt);
984
3.02M
      }
985
3.03M
      break;
986
6.07M
    }
987
3.03M
    default: {
988
175k
      G(L)->mt[ttype(obj)] = mt;
989
175k
      break;
990
6.07M
    }
991
4.90M
  }
992
4.90M
  L->top.p--;
993
4.90M
  lua_unlock(L);
994
4.90M
  return 1;
995
4.90M
}
996
997
998
44
LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
999
44
  TValue *o;
1000
44
  int res;
1001
44
  lua_lock(L);
1002
44
  api_checkpop(L, 1);
1003
44
  o = index2value(L, idx);
1004
44
  api_check(L, ttisfulluserdata(o), "full userdata expected");
1005
88
  if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
1006
43
    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
44
  L->top.p--;
1013
44
  lua_unlock(L);
1014
44
  return res;
1015
44
}
1016
1017
1018
/*
1019
** 'load' and 'call' functions (run Lua code)
1020
*/
1021
1022
1023
#define checkresults(L,na,nr) \
1024
14.6M
     (api_check(L, (nr) == LUA_MULTRET \
1025
14.6M
               || (L->ci->top.p - L->top.p >= (nr) - (na)), \
1026
14.6M
  "results from function overflow current stack size"), \
1027
14.6M
      api_check(L, LUA_MULTRET <= (nr) && (nr) <= MAXRESULTS,  \
1028
14.6M
                   "invalid number of results"))
1029
1030
1031
LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
1032
10.3M
                        lua_KContext ctx, lua_KFunction k) {
1033
10.3M
  StkId func;
1034
10.3M
  lua_lock(L);
1035
10.3M
  api_check(L, k == NULL || !isLua(L->ci),
1036
10.3M
    "cannot use continuations inside hooks");
1037
10.3M
  api_checkpop(L, nargs + 1);
1038
10.3M
  api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
1039
20.7M
  checkresults(L, nargs, nresults);
1040
10.3M
  func = L->top.p - (nargs+1);
1041
10.3M
  if (k != NULL && yieldable(L)) {  /* need to prepare continuation? */
1042
2
    L->ci->u.c.k = k;  /* save continuation */
1043
2
    L->ci->u.c.ctx = ctx;  /* save context */
1044
2
    luaD_call(L, func, nresults);  /* do the call */
1045
2
  }
1046
10.3M
  else  /* no continuation or no yieldable */
1047
10.3M
    luaD_callnoyield(L, func, nresults);  /* just do the call */
1048
10.3M
  adjustresults(L, nresults);
1049
10.3M
  lua_unlock(L);
1050
10.3M
}
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
4.29M
static void f_call (lua_State *L, void *ud) {
1064
4.29M
  struct CallS *c = cast(struct CallS *, ud);
1065
4.29M
  luaD_callnoyield(L, c->func, c->nresults);
1066
4.29M
}
1067
1068
1069
1070
LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
1071
4.29M
                        lua_KContext ctx, lua_KFunction k) {
1072
4.29M
  struct CallS c;
1073
4.29M
  TStatus status;
1074
4.29M
  ptrdiff_t func;
1075
4.29M
  lua_lock(L);
1076
4.29M
  api_check(L, k == NULL || !isLua(L->ci),
1077
4.29M
    "cannot use continuations inside hooks");
1078
4.29M
  api_checkpop(L, nargs + 1);
1079
4.29M
  api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
1080
8.59M
  checkresults(L, nargs, nresults);
1081
4.29M
  if (errfunc == 0)
1082
4.19M
    func = 0;
1083
104k
  else {
1084
104k
    StkId o = index2stack(L, errfunc);
1085
104k
    api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
1086
104k
    func = savestack(L, o);
1087
104k
  }
1088
4.29M
  c.func = L->top.p - (nargs+1);  /* function to be called */
1089
4.29M
  if (k == NULL || !yieldable(L)) {  /* no continuation or no yieldable? */
1090
4.29M
    c.nresults = nresults;  /* do a 'conventional' protected call */
1091
4.29M
    status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
1092
4.29M
  }
1093
57
  else {  /* prepare continuation (call is already protected by 'resume') */
1094
57
    CallInfo *ci = L->ci;
1095
57
    ci->u.c.k = k;  /* save continuation */
1096
57
    ci->u.c.ctx = ctx;  /* save context */
1097
    /* save information for error recovery */
1098
57
    ci->u2.funcidx = cast_int(savestack(L, c.func));
1099
57
    ci->u.c.old_errfunc = L->errfunc;
1100
57
    L->errfunc = func;
1101
57
    setoah(ci, L->allowhook);  /* save value of 'allowhook' */
1102
57
    ci->callstatus |= CIST_YPCALL;  /* function can do error recovery */
1103
57
    luaD_call(L, c.func, nresults);  /* do the call */
1104
57
    ci->callstatus &= ~CIST_YPCALL;
1105
57
    L->errfunc = ci->u.c.old_errfunc;
1106
57
    status = LUA_OK;  /* if it is here, there were no errors */
1107
57
  }
1108
4.29M
  adjustresults(L, nresults);
1109
4.29M
  lua_unlock(L);
1110
4.29M
  return APIstatus(status);
1111
4.29M
}
1112
1113
1114
LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
1115
10.0M
                      const char *chunkname, const char *mode) {
1116
10.0M
  ZIO z;
1117
10.0M
  TStatus status;
1118
10.0M
  lua_lock(L);
1119
10.0M
  if (!chunkname) chunkname = "?";
1120
10.0M
  luaZ_init(L, &z, reader, data);
1121
10.0M
  status = luaD_protectedparser(L, &z, chunkname, mode);
1122
10.0M
  if (status == LUA_OK) {  /* no errors? */
1123
7.70M
    LClosure *f = clLvalue(s2v(L->top.p - 1));  /* get new function */
1124
3.85M
    if (f->nupvalues >= 1) {  /* does it have an upvalue? */
1125
      /* get global table from registry */
1126
3.85M
      TValue gt;
1127
3.85M
      getGlobalTable(L, &gt);
1128
      /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
1129
3.85M
      setobj(L, f->upvals[0]->v.p, &gt);
1130
3.85M
      luaC_barrier(L, f->upvals[0], &gt);
1131
3.85M
    }
1132
7.70M
  }
1133
10.0M
  lua_unlock(L);
1134
10.0M
  return APIstatus(status);
1135
10.0M
}
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
12.4k
LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
1143
12.4k
  int status;
1144
12.4k
  ptrdiff_t otop = savestack(L, L->top.p);  /* original top */
1145
12.4k
  TValue *f = s2v(L->top.p - 1);  /* function to be dumped */
1146
12.4k
  lua_lock(L);
1147
12.4k
  api_checkpop(L, 1);
1148
12.4k
  api_check(L, isLfunction(f), "Lua function expected");
1149
12.4k
  status = luaU_dump(L, clLvalue(f)->p, writer, data, strip);
1150
12.4k
  L->top.p = restorestack(L, otop);  /* restore top */
1151
12.4k
  lua_unlock(L);
1152
12.4k
  return status;
1153
12.4k
}
1154
1155
1156
1.00k
LUA_API int lua_status (lua_State *L) {
1157
1.00k
  return APIstatus(L->status);
1158
1.00k
}
1159
1160
1161
/*
1162
** Garbage-collection function
1163
*/
1164
2.88M
LUA_API int lua_gc (lua_State *L, int what, ...) {
1165
2.88M
  va_list argp;
1166
2.88M
  int res = 0;
1167
2.88M
  global_State *g = G(L);
1168
2.88M
  if (g->gcstp & (GCSTPGC | GCSTPCLS))  /* internal stop? */
1169
0
    return -1;  /* all options are invalid when stopped */
1170
2.88M
  lua_lock(L);
1171
2.88M
  va_start(argp, what);
1172
2.88M
  switch (what) {
1173
314k
    case LUA_GCSTOP: {
1174
314k
      g->gcstp = GCSTPUSR;  /* stopped by the user */
1175
314k
      break;
1176
0
    }
1177
3.16k
    case LUA_GCRESTART: {
1178
3.16k
      luaE_setdebt(g, 0);
1179
3.16k
      g->gcstp = 0;  /* (other bits must be zero here) */
1180
3.16k
      break;
1181
0
    }
1182
191k
    case LUA_GCCOLLECT: {
1183
191k
      luaC_fullgc(L, 0);
1184
191k
      break;
1185
0
    }
1186
880
    case LUA_GCCOUNT: {
1187
      /* GC values are expressed in Kbytes: #bytes/2^10 */
1188
880
      res = cast_int(gettotalbytes(g) >> 10);
1189
880
      break;
1190
0
    }
1191
815
    case LUA_GCCOUNTB: {
1192
815
      res = cast_int(gettotalbytes(g) & 0x3ff);
1193
815
      break;
1194
0
    }
1195
912k
    case LUA_GCSTEP: {
1196
912k
      lu_byte oldstp = g->gcstp;
1197
912k
      l_mem n = cast(l_mem, va_arg(argp, size_t));
1198
912k
      int work = 0;  /* true if GC did some work */
1199
912k
      g->gcstp = 0;  /* allow GC to run (other bits must be zero here) */
1200
912k
      if (n <= 0)
1201
6.49k
        n = g->GCdebt;  /* force to run one basic step */
1202
912k
      luaE_setdebt(g, g->GCdebt - n);
1203
912k
      luaC_condGC(L, (void)0, work = 1);
1204
912k
      if (work && g->gcstate == GCSpause)  /* end of cycle? */
1205
21.2k
        res = 1;  /* signal it */
1206
912k
      g->gcstp = oldstp;  /* restore previous state */
1207
912k
      break;
1208
0
    }
1209
335
    case LUA_GCISRUNNING: {
1210
335
      res = gcrunning(g);
1211
335
      break;
1212
0
    }
1213
1.45M
    case LUA_GCGEN: {
1214
1.45M
      res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
1215
1.45M
      luaC_changemode(L, KGC_GENMINOR);
1216
1.45M
      break;
1217
0
    }
1218
7.29k
    case LUA_GCINC: {
1219
7.29k
      res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
1220
7.29k
      luaC_changemode(L, KGC_INC);
1221
7.29k
      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
2.88M
  }
1234
2.88M
  va_end(argp);
1235
2.88M
  lua_unlock(L);
1236
2.88M
  return res;
1237
2.88M
}
1238
1239
1240
1241
/*
1242
** miscellaneous functions
1243
*/
1244
1245
1246
1.93M
LUA_API int lua_error (lua_State *L) {
1247
1.93M
  TValue *errobj;
1248
1.93M
  lua_lock(L);
1249
1.93M
  errobj = s2v(L->top.p - 1);
1250
1.93M
  api_checkpop(L, 1);
1251
  /* error object is the memory error message? */
1252
1.93M
  if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
1253
3.88k
    luaM_error(L);  /* raise a memory error */
1254
1.93M
  else
1255
1.93M
    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
1.93M
}
1259
1260
1261
167M
LUA_API int lua_next (lua_State *L, int idx) {
1262
167M
  Table *t;
1263
167M
  int more;
1264
167M
  lua_lock(L);
1265
167M
  api_checkpop(L, 1);
1266
167M
  t = gettable(L, idx);
1267
167M
  more = luaH_next(L, t, L->top.p - 1);
1268
167M
  if (more)
1269
159M
    api_incr_top(L);
1270
8.14M
  else  /* no more elements */
1271
8.14M
    L->top.p--;  /* pop key */
1272
167M
  lua_unlock(L);
1273
167M
  return more;
1274
167M
}
1275
1276
1277
918k
LUA_API void lua_toclose (lua_State *L, int idx) {
1278
918k
  StkId o;
1279
918k
  lua_lock(L);
1280
918k
  o = index2stack(L, idx);
1281
918k
  api_check(L, L->tbclist.p < o, "given index below or equal a marked one");
1282
918k
  luaF_newtbcupval(L, o);  /* create new to-be-closed upvalue */
1283
918k
  L->ci->callstatus |= CIST_TBC;  /* mark that function has TBC slots */
1284
918k
  lua_unlock(L);
1285
918k
}
1286
1287
1288
3.34M
LUA_API void lua_concat (lua_State *L, int n) {
1289
3.34M
  lua_lock(L);
1290
3.34M
  api_checknelems(L, n);
1291
3.34M
  if (n > 0) {
1292
3.34M
    luaV_concat(L, n);
1293
3.34M
    luaC_checkGC(L);
1294
3.34M
  }
1295
4
  else {  /* nothing to concatenate */
1296
4
    setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0));  /* push empty string */
1297
4
    api_incr_top(L);
1298
4
  }
1299
3.34M
  lua_unlock(L);
1300
3.34M
}
1301
1302
1303
127k
LUA_API void lua_len (lua_State *L, int idx) {
1304
127k
  TValue *t;
1305
127k
  lua_lock(L);
1306
127k
  t = index2value(L, idx);
1307
127k
  luaV_objlen(L, L->top.p, t);
1308
127k
  api_incr_top(L);
1309
127k
  lua_unlock(L);
1310
127k
}
1311
1312
1313
3.02M
LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
1314
3.02M
  lua_Alloc f;
1315
3.02M
  lua_lock(L);
1316
3.02M
  if (ud) *ud = G(L)->ud;
1317
3.02M
  f = G(L)->frealloc;
1318
3.02M
  lua_unlock(L);
1319
3.02M
  return f;
1320
3.02M
}
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
72.4k
void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
1332
72.4k
  lua_lock(L);
1333
72.4k
  G(L)->ud_warn = ud;
1334
72.4k
  G(L)->warnf = f;
1335
72.4k
  lua_unlock(L);
1336
72.4k
}
1337
1338
1339
77.4k
void lua_warning (lua_State *L, const char *msg, int tocont) {
1340
77.4k
  lua_lock(L);
1341
77.4k
  luaE_warning(L, msg, tocont);
1342
77.4k
  lua_unlock(L);
1343
77.4k
}
1344
1345
1346
1347
3.31M
LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
1348
3.31M
  Udata *u;
1349
3.31M
  lua_lock(L);
1350
3.31M
  api_check(L, 0 <= nuvalue && nuvalue < SHRT_MAX, "invalid value");
1351
3.31M
  u = luaS_newudata(L, size, cast(unsigned short, nuvalue));
1352
3.31M
  setuvalue(L, s2v(L->top.p), u);
1353
3.31M
  api_incr_top(L);
1354
3.31M
  luaC_checkGC(L);
1355
3.31M
  lua_unlock(L);
1356
3.31M
  return getudatamem(u);
1357
3.31M
}
1358
1359
1360
1361
static const char *aux_upvalue (TValue *fi, int n, TValue **val,
1362
110k
                                GCObject **owner) {
1363
110k
  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
110k
    case LUA_VLCL: {  /* Lua closure */
1373
221k
      LClosure *f = clLvalue(fi);
1374
0
      TString *name;
1375
221k
      Proto *p = f->p;
1376
221k
      if (!(cast_uint(n) - 1u  < cast_uint(p->sizeupvalues)))
1377
0
        return NULL;  /* 'n' not in [1, p->sizeupvalues] */
1378
110k
      *val = f->upvals[n-1]->v.p;
1379
110k
      if (owner) *owner = obj2gco(f->upvals[n - 1]);
1380
110k
      name = p->upvalues[n-1].name;
1381
110k
      return (name == NULL) ? "(no name)" : getstr(name);
1382
110k
    }
1383
55
    default: return NULL;  /* not a closure */
1384
110k
  }
1385
110k
}
1386
1387
1388
26
LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1389
26
  const char *name;
1390
26
  TValue *val = NULL;  /* to avoid warnings */
1391
26
  lua_lock(L);
1392
26
  name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
1393
26
  if (name) {
1394
0
    setobj2s(L, L->top.p, val);
1395
0
    api_incr_top(L);
1396
0
  }
1397
26
  lua_unlock(L);
1398
26
  return name;
1399
26
}
1400
1401
1402
110k
LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
1403
110k
  const char *name;
1404
110k
  TValue *val = NULL;  /* to avoid warnings */
1405
110k
  GCObject *owner = NULL;  /* to avoid warnings */
1406
110k
  TValue *fi;
1407
110k
  lua_lock(L);
1408
110k
  fi = index2value(L, funcindex);
1409
110k
  api_checknelems(L, 1);
1410
110k
  name = aux_upvalue(fi, n, &val, &owner);
1411
110k
  if (name) {
1412
110k
    L->top.p--;
1413
110k
    setobj(L, val, s2v(L->top.p));
1414
110k
    luaC_barrier(L, owner, val);
1415
110k
  }
1416
110k
  lua_unlock(L);
1417
110k
  return name;
1418
110k
}
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