Coverage Report

Created: 2026-02-26 07:19

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