Coverage Report

Created: 2025-11-24 06:29

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