Coverage Report

Created: 2025-08-28 06:25

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