Coverage Report

Created: 2025-10-10 06:41

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