Coverage Report

Created: 2025-12-11 06:33

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