Coverage Report

Created: 2026-01-17 06:38

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