Coverage Report

Created: 2025-07-18 07:03

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