Coverage Report

Created: 2023-08-27 06:20

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