Coverage Report

Created: 2024-04-23 06:32

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