Coverage Report

Created: 2026-04-11 06:45

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