Coverage Report

Created: 2025-10-27 06:39

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