Coverage Report

Created: 2025-08-28 06:30

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