Coverage Report

Created: 2025-08-25 06:57

/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.81k
#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
2.02k
static TValue *index2value (lua_State *L, int idx) {
59
2.02k
  CallInfo *ci = L->ci;
60
2.02k
  if (idx > 0) {
61
217
    StkId o = ci->func.p + idx;
62
217
    api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index");
63
217
    if (o >= L->top.p) return &G(L)->nilvalue;
64
217
    else return s2v(o);
65
217
  }
66
1.81k
  else if (!ispseudo(idx)) {  /* negative index */
67
1.71k
    api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
68
1.71k
                 "invalid index");
69
1.71k
    return s2v(L->top.p + idx);
70
1.71k
  }
71
100
  else if (idx == LUA_REGISTRYINDEX)
72
100
    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
2.02k
}
87
88
89
90
/*
91
** Convert a valid actual index (not a pseudo-index) to its address.
92
*/
93
674
static StkId index2stack (lua_State *L, int idx) {
94
674
  CallInfo *ci = L->ci;
95
674
  if (idx > 0) {
96
357
    StkId o = ci->func.p + idx;
97
357
    api_check(L, o < L->top.p, "invalid index");
98
357
    return o;
99
357
  }
100
317
  else {    /* non-positive index */
101
317
    api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
102
317
                 "invalid index");
103
317
    api_check(L, !ispseudo(idx), "invalid index");
104
317
    return L->top.p + idx;
105
317
  }
106
674
}
107
108
109
50
LUA_API int lua_checkstack (lua_State *L, int n) {
110
50
  int res;
111
50
  CallInfo *ci;
112
50
  lua_lock(L);
113
50
  ci = L->ci;
114
50
  api_check(L, n >= 0, "negative 'n'");
115
50
  if (L->stack_last.p - L->top.p > n)  /* stack large enough? */
116
50
    res = 1;  /* yes; check is OK */
117
0
  else  /* need to grow stack */
118
0
    res = luaD_growstack(L, n, 0);
119
50
  if (res && ci->top.p < L->top.p + n)
120
0
    ci->top.p = L->top.p + n;  /* adjust frame top */
121
50
  lua_unlock(L);
122
50
  return res;
123
50
}
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
441
LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
143
441
  lua_CFunction old;
144
441
  lua_lock(L);
145
441
  old = G(L)->panic;
146
441
  G(L)->panic = panicf;
147
441
  lua_unlock(L);
148
441
  return old;
149
441
}
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
119
LUA_API int lua_gettop (lua_State *L) {
175
119
  return cast_int(L->top.p - (L->ci->func.p + 1));
176
119
}
177
178
179
1.06k
LUA_API void lua_settop (lua_State *L, int idx) {
180
1.06k
  CallInfo *ci;
181
1.06k
  StkId func, newtop;
182
1.06k
  ptrdiff_t diff;  /* difference for new top */
183
1.06k
  lua_lock(L);
184
1.06k
  ci = L->ci;
185
1.06k
  func = ci->func.p;
186
1.06k
  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
1.06k
  else {
193
1.06k
    api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top");
194
1.06k
    diff = idx + 1;  /* will "subtract" index (as it is negative) */
195
1.06k
  }
196
1.06k
  newtop = L->top.p + diff;
197
1.06k
  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
1.06k
  L->top.p = newtop;  /* correct top only after closing any upvalue */
202
1.06k
  lua_unlock(L);
203
1.06k
}
204
205
206
50
LUA_API void lua_closeslot (lua_State *L, int idx) {
207
50
  StkId level;
208
50
  lua_lock(L);
209
50
  level = index2stack(L, idx);
210
50
  api_check(L, (L->ci->callstatus & CIST_TBC) && (L->tbclist.p == level),
211
50
     "no variable to close at given level");
212
50
  level = luaF_close(L, level, CLOSEKTOP, 0);
213
50
  setnilvalue(s2v(level));
214
50
  lua_unlock(L);
215
50
}
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.36k
static void reverse (lua_State *L, StkId from, StkId to) {
225
1.71k
  for (; from < to; from++, to--) {
226
353
    TValue temp;
227
353
    setobj(L, &temp, s2v(from));
228
353
    setobjs2s(L, from, to);
229
353
    setobj2s(L, to, &temp);
230
353
  }
231
1.36k
}
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
455
LUA_API void lua_rotate (lua_State *L, int idx, int n) {
239
455
  StkId p, t, m;
240
455
  lua_lock(L);
241
455
  t = L->top.p - 1;  /* end of stack segment being rotated */
242
455
  p = index2stack(L, idx);  /* start of segment */
243
455
  api_check(L, L->tbclist.p < p, "moving a to-be-closed slot");
244
455
  api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
245
455
  m = (n >= 0 ? t - n : p - n - 1);  /* end of prefix */
246
455
  reverse(L, p, m);  /* reverse the prefix with length 'n' */
247
455
  reverse(L, m + 1, t);  /* reverse the suffix */
248
455
  reverse(L, p, t);  /* reverse the entire segment */
249
455
  lua_unlock(L);
250
455
}
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
50
LUA_API void lua_pushvalue (lua_State *L, int idx) {
269
50
  lua_lock(L);
270
50
  setobj2s(L, L->top.p, index2value(L, idx));
271
50
  api_incr_top(L);
272
50
  lua_unlock(L);
273
50
}
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
792
LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
416
792
  TValue *o;
417
792
  lua_lock(L);
418
792
  o = index2value(L, idx);
419
792
  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
792
  lua_unlock(L);
430
792
  if (len != NULL)
431
472
    return getlstr(tsvalue(o), *len);
432
556
  else
433
1.11k
    return getstr(tsvalue(o));
434
792
}
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: return luaH_getn(L, hvalue(o));
444
0
    default: return 0;
445
0
  }
446
0
}
447
448
449
0
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
450
0
  const TValue *o = index2value(L, idx);
451
0
  if (ttislcf(o)) return fvalue(o);
452
0
  else if (ttisCclosure(o))
453
0
    return clCvalue(o)->f;
454
0
  else return NULL;  /* not a C function */
455
0
}
456
457
458
887
l_sinline void *touserdata (const TValue *o) {
459
887
  switch (ttype(o)) {
460
1.00k
    case LUA_TUSERDATA: return getudatamem(uvalue(o));
461
500
    case LUA_TLIGHTUSERDATA: return pvalue(o);
462
0
    default: return NULL;
463
887
  }
464
887
}
465
466
467
887
LUA_API void *lua_touserdata (lua_State *L, int idx) {
468
887
  const TValue *o = index2value(L, idx);
469
887
  return touserdata(o);
470
887
}
471
472
473
0
LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
474
0
  const TValue *o = index2value(L, idx);
475
0
  return (!ttisthread(o)) ? NULL : thvalue(o);
476
0
}
477
478
479
/*
480
** Returns a pointer to the internal representation of an object.
481
** Note that ANSI C does not allow the conversion of a pointer to
482
** function to a 'void*', so the conversion here goes through
483
** a 'size_t'. (As the returned pointer is only informative, this
484
** conversion should not be a problem.)
485
*/
486
0
LUA_API const void *lua_topointer (lua_State *L, int idx) {
487
0
  const TValue *o = index2value(L, idx);
488
0
  switch (ttypetag(o)) {
489
0
    case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
490
0
    case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA:
491
0
      return touserdata(o);
492
0
    default: {
493
0
      if (iscollectable(o))
494
0
        return gcvalue(o);
495
0
      else
496
0
        return NULL;
497
0
    }
498
0
  }
499
0
}
500
501
502
503
/*
504
** push functions (C -> stack)
505
*/
506
507
508
0
LUA_API void lua_pushnil (lua_State *L) {
509
0
  lua_lock(L);
510
0
  setnilvalue(s2v(L->top.p));
511
0
  api_incr_top(L);
512
0
  lua_unlock(L);
513
0
}
514
515
516
0
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
517
0
  lua_lock(L);
518
0
  setfltvalue(s2v(L->top.p), n);
519
0
  api_incr_top(L);
520
0
  lua_unlock(L);
521
0
}
522
523
524
0
LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
525
0
  lua_lock(L);
526
0
  setivalue(s2v(L->top.p), n);
527
0
  api_incr_top(L);
528
0
  lua_unlock(L);
529
0
}
530
531
532
/*
533
** Pushes on the stack a string with given length. Avoid using 's' when
534
** 'len' == 0 (as 's' can be NULL in that case), due to later use of
535
** 'memcmp' and 'memcpy'.
536
*/
537
67
LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
538
67
  TString *ts;
539
67
  lua_lock(L);
540
67
  ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
541
67
  setsvalue2s(L, L->top.p, ts);
542
67
  api_incr_top(L);
543
67
  luaC_checkGC(L);
544
67
  lua_unlock(L);
545
67
  return getstr(ts);
546
67
}
547
548
549
LUA_API const char *lua_pushexternalstring (lua_State *L,
550
50
          const char *s, size_t len, lua_Alloc falloc, void *ud) {
551
50
  TString *ts;
552
50
  lua_lock(L);
553
50
  api_check(L, len <= MAX_SIZE, "string too large");
554
50
  api_check(L, s[len] == '\0', "string not ending with zero");
555
50
  ts = luaS_newextlstr (L, s, len, falloc, ud);
556
50
  setsvalue2s(L, L->top.p, ts);
557
50
  api_incr_top(L);
558
50
  luaC_checkGC(L);
559
50
  lua_unlock(L);
560
50
  return getstr(ts);
561
50
}
562
563
564
167
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
565
167
  lua_lock(L);
566
167
  if (s == NULL)
567
167
    setnilvalue(s2v(L->top.p));
568
167
  else {
569
167
    TString *ts;
570
167
    ts = luaS_new(L, s);
571
167
    setsvalue2s(L, L->top.p, ts);
572
167
    s = getstr(ts);  /* internal copy's address */
573
167
  }
574
167
  api_incr_top(L);
575
167
  luaC_checkGC(L);
576
167
  lua_unlock(L);
577
167
  return s;
578
167
}
579
580
581
LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
582
0
                                      va_list argp) {
583
0
  const char *ret;
584
0
  lua_lock(L);
585
0
  ret = luaO_pushvfstring(L, fmt, argp);
586
0
  luaC_checkGC(L);
587
0
  lua_unlock(L);
588
0
  return ret;
589
0
}
590
591
592
119
LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
593
119
  const char *ret;
594
119
  va_list argp;
595
119
  lua_lock(L);
596
119
  pushvfstring(L, argp, fmt, ret);
597
119
  luaC_checkGC(L);
598
119
  lua_unlock(L);
599
119
  return ret;
600
119
}
601
602
603
219
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
604
219
  lua_lock(L);
605
219
  if (n == 0) {
606
219
    setfvalue(s2v(L->top.p), fn);
607
219
    api_incr_top(L);
608
219
  }
609
0
  else {
610
0
    int i;
611
0
    CClosure *cl;
612
0
    api_checkpop(L, n);
613
0
    api_check(L, n <= MAXUPVAL, "upvalue index too large");
614
0
    cl = luaF_newCclosure(L, n);
615
0
    cl->f = fn;
616
0
    for (i = 0; i < n; i++) {
617
0
      setobj2n(L, &cl->upvalue[i], s2v(L->top.p - n + i));
618
      /* does not need barrier because closure is white */
619
0
      lua_assert(iswhite(cl));
620
0
    }
621
0
    L->top.p -= n;
622
0
    setclCvalue(L, s2v(L->top.p), cl);
623
0
    api_incr_top(L);
624
0
    luaC_checkGC(L);
625
0
  }
626
219
  lua_unlock(L);
627
219
}
628
629
630
0
LUA_API void lua_pushboolean (lua_State *L, int b) {
631
0
  lua_lock(L);
632
0
  if (b)
633
0
    setbtvalue(s2v(L->top.p));
634
0
  else
635
0
    setbfvalue(s2v(L->top.p));
636
0
  api_incr_top(L);
637
0
  lua_unlock(L);
638
0
}
639
640
641
117
LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
642
117
  lua_lock(L);
643
117
  setpvalue(s2v(L->top.p), p);
644
117
  api_incr_top(L);
645
117
  lua_unlock(L);
646
117
}
647
648
649
0
LUA_API int lua_pushthread (lua_State *L) {
650
0
  lua_lock(L);
651
0
  setthvalue(L, s2v(L->top.p), L);
652
0
  api_incr_top(L);
653
0
  lua_unlock(L);
654
0
  return (mainthread(G(L)) == L);
655
0
}
656
657
658
659
/*
660
** get functions (Lua -> stack)
661
*/
662
663
664
50
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
665
50
  lu_byte tag;
666
50
  TString *str = luaS_new(L, k);
667
50
  luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag);
668
50
  if (!tagisempty(tag))
669
0
    api_incr_top(L);
670
50
  else {
671
50
    setsvalue2s(L, L->top.p, str);
672
50
    api_incr_top(L);
673
50
    tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
674
50
  }
675
50
  lua_unlock(L);
676
50
  return novariant(tag);
677
50
}
678
679
680
/*
681
** The following function assumes that the registry cannot be a weak
682
** table; so, an emergency collection while using the global table
683
** cannot collect it.
684
*/
685
119
static void getGlobalTable (lua_State *L, TValue *gt) {
686
238
  Table *registry = hvalue(&G(L)->l_registry);
687
119
  lu_byte tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
688
238
  (void)tag;  /* avoid not-used warnings when checks are off */
689
238
  api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist");
690
238
}
691
692
693
0
LUA_API int lua_getglobal (lua_State *L, const char *name) {
694
0
  TValue gt;
695
0
  lua_lock(L);
696
0
  getGlobalTable(L, &gt);
697
0
  return auxgetstr(L, &gt, name);
698
0
}
699
700
701
0
LUA_API int lua_gettable (lua_State *L, int idx) {
702
0
  lu_byte tag;
703
0
  TValue *t;
704
0
  lua_lock(L);
705
0
  api_checkpop(L, 1);
706
0
  t = index2value(L, idx);
707
0
  luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, tag);
708
0
  if (tagisempty(tag))
709
0
    tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
710
0
  lua_unlock(L);
711
0
  return novariant(tag);
712
0
}
713
714
715
50
LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
716
50
  lua_lock(L);
717
50
  return auxgetstr(L, index2value(L, idx), k);
718
50
}
719
720
721
0
LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
722
0
  TValue *t;
723
0
  lu_byte tag;
724
0
  lua_lock(L);
725
0
  t = index2value(L, idx);
726
0
  luaV_fastgeti(t, n, s2v(L->top.p), tag);
727
0
  if (tagisempty(tag)) {
728
0
    TValue key;
729
0
    setivalue(&key, n);
730
0
    tag = luaV_finishget(L, t, &key, L->top.p, tag);
731
0
  }
732
0
  api_incr_top(L);
733
0
  lua_unlock(L);
734
0
  return novariant(tag);
735
0
}
736
737
738
0
static int finishrawget (lua_State *L, lu_byte tag) {
739
0
  if (tagisempty(tag))  /* avoid copying empty items to the stack */
740
0
    setnilvalue(s2v(L->top.p));
741
0
  api_incr_top(L);
742
0
  lua_unlock(L);
743
0
  return novariant(tag);
744
0
}
745
746
747
0
l_sinline Table *gettable (lua_State *L, int idx) {
748
0
  TValue *t = index2value(L, idx);
749
0
  api_check(L, ttistable(t), "table expected");
750
0
  return hvalue(t);
751
0
}
752
753
754
0
LUA_API int lua_rawget (lua_State *L, int idx) {
755
0
  Table *t;
756
0
  lu_byte tag;
757
0
  lua_lock(L);
758
0
  api_checkpop(L, 1);
759
0
  t = gettable(L, idx);
760
0
  tag = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1));
761
0
  L->top.p--;  /* pop key */
762
0
  return finishrawget(L, tag);
763
0
}
764
765
766
0
LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
767
0
  Table *t;
768
0
  lu_byte tag;
769
0
  lua_lock(L);
770
0
  t = gettable(L, idx);
771
0
  luaH_fastgeti(t, n, s2v(L->top.p), tag);
772
0
  return finishrawget(L, tag);
773
0
}
774
775
776
0
LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
777
0
  Table *t;
778
0
  TValue k;
779
0
  lua_lock(L);
780
0
  t = gettable(L, idx);
781
0
  setpvalue(&k, cast_voidp(p));
782
0
  return finishrawget(L, luaH_get(t, &k, s2v(L->top.p)));
783
0
}
784
785
786
50
LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
787
50
  Table *t;
788
50
  lua_lock(L);
789
50
  t = luaH_new(L);
790
50
  sethvalue2s(L, L->top.p, t);
791
50
  api_incr_top(L);
792
50
  if (narray > 0 || nrec > 0)
793
50
    luaH_resize(L, t, cast_uint(narray), cast_uint(nrec));
794
50
  luaC_checkGC(L);
795
50
  lua_unlock(L);
796
50
}
797
798
799
0
LUA_API int lua_getmetatable (lua_State *L, int objindex) {
800
0
  const TValue *obj;
801
0
  Table *mt;
802
0
  int res = 0;
803
0
  lua_lock(L);
804
0
  obj = index2value(L, objindex);
805
0
  switch (ttype(obj)) {
806
0
    case LUA_TTABLE:
807
0
      mt = hvalue(obj)->metatable;
808
0
      break;
809
0
    case LUA_TUSERDATA:
810
0
      mt = uvalue(obj)->metatable;
811
0
      break;
812
0
    default:
813
0
      mt = G(L)->mt[ttype(obj)];
814
0
      break;
815
0
  }
816
0
  if (mt != NULL) {
817
0
    sethvalue2s(L, L->top.p, mt);
818
0
    api_incr_top(L);
819
0
    res = 1;
820
0
  }
821
0
  lua_unlock(L);
822
0
  return res;
823
0
}
824
825
826
0
LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
827
0
  TValue *o;
828
0
  int t;
829
0
  lua_lock(L);
830
0
  o = index2value(L, idx);
831
0
  api_check(L, ttisfulluserdata(o), "full userdata expected");
832
0
  if (n <= 0 || n > uvalue(o)->nuvalue) {
833
0
    setnilvalue(s2v(L->top.p));
834
0
    t = LUA_TNONE;
835
0
  }
836
0
  else {
837
0
    setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv);
838
0
    t = ttype(s2v(L->top.p));
839
0
  }
840
0
  api_incr_top(L);
841
0
  lua_unlock(L);
842
0
  return t;
843
0
}
844
845
846
/*
847
** set functions (stack -> Lua)
848
*/
849
850
/*
851
** t[k] = value at the top of the stack (where 'k' is a string)
852
*/
853
200
static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
854
200
  int hres;
855
200
  TString *str = luaS_new(L, k);
856
200
  api_checkpop(L, 1);
857
200
  luaV_fastset(t, str, s2v(L->top.p - 1), hres, luaH_psetstr);
858
200
  if (hres == HOK) {
859
100
    luaV_finishfastset(L, t, s2v(L->top.p - 1));
860
100
    L->top.p--;  /* pop value */
861
100
  }
862
100
  else {
863
100
    setsvalue2s(L, L->top.p, str);  /* push 'str' (to make it a TValue) */
864
100
    api_incr_top(L);
865
100
    luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), hres);
866
100
    L->top.p -= 2;  /* pop value and key */
867
100
  }
868
200
  lua_unlock(L);  /* lock done by caller */
869
200
}
870
871
872
0
LUA_API void lua_setglobal (lua_State *L, const char *name) {
873
0
  TValue gt;
874
0
  lua_lock(L);  /* unlock done in 'auxsetstr' */
875
0
  getGlobalTable(L, &gt);
876
0
  auxsetstr(L, &gt, name);
877
0
}
878
879
880
0
LUA_API void lua_settable (lua_State *L, int idx) {
881
0
  TValue *t;
882
0
  int hres;
883
0
  lua_lock(L);
884
0
  api_checkpop(L, 2);
885
0
  t = index2value(L, idx);
886
0
  luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset);
887
0
  if (hres == HOK)
888
0
    luaV_finishfastset(L, t, s2v(L->top.p - 1));
889
0
  else
890
0
    luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres);
891
0
  L->top.p -= 2;  /* pop index and value */
892
0
  lua_unlock(L);
893
0
}
894
895
896
200
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
897
200
  lua_lock(L);  /* unlock done in 'auxsetstr' */
898
200
  auxsetstr(L, index2value(L, idx), k);
899
200
}
900
901
902
0
LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
903
0
  TValue *t;
904
0
  int hres;
905
0
  lua_lock(L);
906
0
  api_checkpop(L, 1);
907
0
  t = index2value(L, idx);
908
0
  luaV_fastseti(t, n, s2v(L->top.p - 1), hres);
909
0
  if (hres == HOK)
910
0
    luaV_finishfastset(L, t, s2v(L->top.p - 1));
911
0
  else {
912
0
    TValue temp;
913
0
    setivalue(&temp, n);
914
0
    luaV_finishset(L, t, &temp, s2v(L->top.p - 1), hres);
915
0
  }
916
0
  L->top.p--;  /* pop value */
917
0
  lua_unlock(L);
918
0
}
919
920
921
0
static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
922
0
  Table *t;
923
0
  lua_lock(L);
924
0
  api_checkpop(L, n);
925
0
  t = gettable(L, idx);
926
0
  luaH_set(L, t, key, s2v(L->top.p - 1));
927
0
  invalidateTMcache(t);
928
0
  luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
929
0
  L->top.p -= n;
930
0
  lua_unlock(L);
931
0
}
932
933
934
0
LUA_API void lua_rawset (lua_State *L, int idx) {
935
0
  aux_rawset(L, idx, s2v(L->top.p - 2), 2);
936
0
}
937
938
939
0
LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
940
0
  TValue k;
941
0
  setpvalue(&k, cast_voidp(p));
942
0
  aux_rawset(L, idx, &k, 1);
943
0
}
944
945
946
0
LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
947
0
  Table *t;
948
0
  lua_lock(L);
949
0
  api_checkpop(L, 1);
950
0
  t = gettable(L, idx);
951
0
  luaH_setint(L, t, n, s2v(L->top.p - 1));
952
0
  luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
953
0
  L->top.p--;
954
0
  lua_unlock(L);
955
0
}
956
957
958
50
LUA_API int lua_setmetatable (lua_State *L, int objindex) {
959
50
  TValue *obj;
960
50
  Table *mt;
961
50
  lua_lock(L);
962
50
  api_checkpop(L, 1);
963
50
  obj = index2value(L, objindex);
964
50
  if (ttisnil(s2v(L->top.p - 1)))
965
0
    mt = NULL;
966
50
  else {
967
50
    api_check(L, ttistable(s2v(L->top.p - 1)), "table expected");
968
100
    mt = hvalue(s2v(L->top.p - 1));
969
100
  }
970
50
  switch (ttype(obj)) {
971
0
    case LUA_TTABLE: {
972
0
      hvalue(obj)->metatable = mt;
973
0
      if (mt) {
974
0
        luaC_objbarrier(L, gcvalue(obj), mt);
975
0
        luaC_checkfinalizer(L, gcvalue(obj), mt);
976
0
      }
977
0
      break;
978
0
    }
979
50
    case LUA_TUSERDATA: {
980
100
      uvalue(obj)->metatable = mt;
981
50
      if (mt) {
982
50
        luaC_objbarrier(L, uvalue(obj), mt);
983
50
        luaC_checkfinalizer(L, gcvalue(obj), mt);
984
50
      }
985
50
      break;
986
100
    }
987
50
    default: {
988
0
      G(L)->mt[ttype(obj)] = mt;
989
0
      break;
990
100
    }
991
50
  }
992
50
  L->top.p--;
993
50
  lua_unlock(L);
994
50
  return 1;
995
50
}
996
997
998
0
LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
999
0
  TValue *o;
1000
0
  int res;
1001
0
  lua_lock(L);
1002
0
  api_checkpop(L, 1);
1003
0
  o = index2value(L, idx);
1004
0
  api_check(L, ttisfulluserdata(o), "full userdata expected");
1005
0
  if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
1006
0
    res = 0;  /* 'n' not in [1, uvalue(o)->nuvalue] */
1007
0
  else {
1008
0
    setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1));
1009
0
    luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1));
1010
0
    res = 1;
1011
0
  }
1012
0
  L->top.p--;
1013
0
  lua_unlock(L);
1014
0
  return res;
1015
0
}
1016
1017
1018
/*
1019
** 'load' and 'call' functions (run Lua code)
1020
*/
1021
1022
1023
#define checkresults(L,na,nr) \
1024
119
     (api_check(L, (nr) == LUA_MULTRET \
1025
119
               || (L->ci->top.p - L->top.p >= (nr) - (na)), \
1026
119
  "results from function overflow current stack size"), \
1027
119
      api_check(L, LUA_MULTRET <= (nr) && (nr) <= MAXRESULTS,  \
1028
119
                   "invalid number of results"))
1029
1030
1031
LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
1032
0
                        lua_KContext ctx, lua_KFunction k) {
1033
0
  StkId func;
1034
0
  lua_lock(L);
1035
0
  api_check(L, k == NULL || !isLua(L->ci),
1036
0
    "cannot use continuations inside hooks");
1037
0
  api_checkpop(L, nargs + 1);
1038
0
  api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
1039
0
  checkresults(L, nargs, nresults);
1040
0
  func = L->top.p - (nargs+1);
1041
0
  if (k != NULL && yieldable(L)) {  /* need to prepare continuation? */
1042
0
    L->ci->u.c.k = k;  /* save continuation */
1043
0
    L->ci->u.c.ctx = ctx;  /* save context */
1044
0
    luaD_call(L, func, nresults);  /* do the call */
1045
0
  }
1046
0
  else  /* no continuation or no yieldable */
1047
0
    luaD_callnoyield(L, func, nresults);  /* just do the call */
1048
0
  adjustresults(L, nresults);
1049
0
  lua_unlock(L);
1050
0
}
1051
1052
1053
1054
/*
1055
** Execute a protected call.
1056
*/
1057
struct CallS {  /* data to 'f_call' */
1058
  StkId func;
1059
  int nresults;
1060
};
1061
1062
1063
119
static void f_call (lua_State *L, void *ud) {
1064
119
  struct CallS *c = cast(struct CallS *, ud);
1065
119
  luaD_callnoyield(L, c->func, c->nresults);
1066
119
}
1067
1068
1069
1070
LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
1071
119
                        lua_KContext ctx, lua_KFunction k) {
1072
119
  struct CallS c;
1073
119
  TStatus status;
1074
119
  ptrdiff_t func;
1075
119
  lua_lock(L);
1076
119
  api_check(L, k == NULL || !isLua(L->ci),
1077
119
    "cannot use continuations inside hooks");
1078
119
  api_checkpop(L, nargs + 1);
1079
119
  api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
1080
238
  checkresults(L, nargs, nresults);
1081
119
  if (errfunc == 0)
1082
0
    func = 0;
1083
119
  else {
1084
119
    StkId o = index2stack(L, errfunc);
1085
119
    api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
1086
119
    func = savestack(L, o);
1087
119
  }
1088
119
  c.func = L->top.p - (nargs+1);  /* function to be called */
1089
119
  if (k == NULL || !yieldable(L)) {  /* no continuation or no yieldable? */
1090
119
    c.nresults = nresults;  /* do a 'conventional' protected call */
1091
119
    status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
1092
119
  }
1093
0
  else {  /* prepare continuation (call is already protected by 'resume') */
1094
0
    CallInfo *ci = L->ci;
1095
0
    ci->u.c.k = k;  /* save continuation */
1096
0
    ci->u.c.ctx = ctx;  /* save context */
1097
    /* save information for error recovery */
1098
0
    ci->u2.funcidx = cast_int(savestack(L, c.func));
1099
0
    ci->u.c.old_errfunc = L->errfunc;
1100
0
    L->errfunc = func;
1101
0
    setoah(ci, L->allowhook);  /* save value of 'allowhook' */
1102
0
    ci->callstatus |= CIST_YPCALL;  /* function can do error recovery */
1103
0
    luaD_call(L, c.func, nresults);  /* do the call */
1104
0
    ci->callstatus &= ~CIST_YPCALL;
1105
0
    L->errfunc = ci->u.c.old_errfunc;
1106
0
    status = LUA_OK;  /* if it is here, there were no errors */
1107
0
  }
1108
119
  adjustresults(L, nresults);
1109
119
  lua_unlock(L);
1110
119
  return APIstatus(status);
1111
119
}
1112
1113
1114
LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
1115
441
                      const char *chunkname, const char *mode) {
1116
441
  ZIO z;
1117
441
  TStatus status;
1118
441
  lua_lock(L);
1119
441
  if (!chunkname) chunkname = "?";
1120
441
  luaZ_init(L, &z, reader, data);
1121
441
  status = luaD_protectedparser(L, &z, chunkname, mode);
1122
441
  if (status == LUA_OK) {  /* no errors? */
1123
238
    LClosure *f = clLvalue(s2v(L->top.p - 1));  /* get new function */
1124
119
    if (f->nupvalues >= 1) {  /* does it have an upvalue? */
1125
      /* get global table from registry */
1126
119
      TValue gt;
1127
119
      getGlobalTable(L, &gt);
1128
      /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
1129
119
      setobj(L, f->upvals[0]->v.p, &gt);
1130
119
      luaC_barrier(L, f->upvals[0], &gt);
1131
119
    }
1132
238
  }
1133
441
  lua_unlock(L);
1134
441
  return APIstatus(status);
1135
441
}
1136
1137
1138
/*
1139
** Dump a Lua function, calling 'writer' to write its parts. Ensure
1140
** the stack returns with its original size.
1141
*/
1142
0
LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
1143
0
  int status;
1144
0
  ptrdiff_t otop = savestack(L, L->top.p);  /* original top */
1145
0
  TValue *f = s2v(L->top.p - 1);  /* function to be dumped */
1146
0
  lua_lock(L);
1147
0
  api_checkpop(L, 1);
1148
0
  api_check(L, isLfunction(f), "Lua function expected");
1149
0
  status = luaU_dump(L, clLvalue(f)->p, writer, data, strip);
1150
0
  L->top.p = restorestack(L, otop);  /* restore top */
1151
0
  lua_unlock(L);
1152
0
  return status;
1153
0
}
1154
1155
1156
0
LUA_API int lua_status (lua_State *L) {
1157
0
  return APIstatus(L->status);
1158
0
}
1159
1160
1161
/*
1162
** Garbage-collection function
1163
*/
1164
50
LUA_API int lua_gc (lua_State *L, int what, ...) {
1165
50
  va_list argp;
1166
50
  int res = 0;
1167
50
  global_State *g = G(L);
1168
50
  if (g->gcstp & (GCSTPGC | GCSTPCLS))  /* internal stop? */
1169
0
    return -1;  /* all options are invalid when stopped */
1170
50
  lua_lock(L);
1171
50
  va_start(argp, what);
1172
50
  switch (what) {
1173
0
    case LUA_GCSTOP: {
1174
0
      g->gcstp = GCSTPUSR;  /* stopped by the user */
1175
0
      break;
1176
0
    }
1177
0
    case LUA_GCRESTART: {
1178
0
      luaE_setdebt(g, 0);
1179
0
      g->gcstp = 0;  /* (other bits must be zero here) */
1180
0
      break;
1181
0
    }
1182
0
    case LUA_GCCOLLECT: {
1183
0
      luaC_fullgc(L, 0);
1184
0
      break;
1185
0
    }
1186
0
    case LUA_GCCOUNT: {
1187
      /* GC values are expressed in Kbytes: #bytes/2^10 */
1188
0
      res = cast_int(gettotalbytes(g) >> 10);
1189
0
      break;
1190
0
    }
1191
0
    case LUA_GCCOUNTB: {
1192
0
      res = cast_int(gettotalbytes(g) & 0x3ff);
1193
0
      break;
1194
0
    }
1195
50
    case LUA_GCSTEP: {
1196
50
      lu_byte oldstp = g->gcstp;
1197
50
      l_mem n = cast(l_mem, va_arg(argp, size_t));
1198
50
      int work = 0;  /* true if GC did some work */
1199
50
      g->gcstp = 0;  /* allow GC to run (other bits must be zero here) */
1200
50
      if (n <= 0)
1201
0
        n = g->GCdebt;  /* force to run one basic step */
1202
50
      luaE_setdebt(g, g->GCdebt - n);
1203
50
      luaC_condGC(L, (void)0, work = 1);
1204
50
      if (work && g->gcstate == GCSpause)  /* end of cycle? */
1205
45
        res = 1;  /* signal it */
1206
50
      g->gcstp = oldstp;  /* restore previous state */
1207
50
      break;
1208
0
    }
1209
0
    case LUA_GCISRUNNING: {
1210
0
      res = gcrunning(g);
1211
0
      break;
1212
0
    }
1213
0
    case LUA_GCGEN: {
1214
0
      res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
1215
0
      luaC_changemode(L, KGC_GENMINOR);
1216
0
      break;
1217
0
    }
1218
0
    case LUA_GCINC: {
1219
0
      res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
1220
0
      luaC_changemode(L, KGC_INC);
1221
0
      break;
1222
0
    }
1223
0
    case LUA_GCPARAM: {
1224
0
      int param = va_arg(argp, int);
1225
0
      int value = va_arg(argp, int);
1226
0
      api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter");
1227
0
      res = cast_int(luaO_applyparam(g->gcparams[param], 100));
1228
0
      if (value >= 0)
1229
0
        g->gcparams[param] = luaO_codeparam(cast_uint(value));
1230
0
      break;
1231
0
    }
1232
0
    default: res = -1;  /* invalid option */
1233
50
  }
1234
50
  va_end(argp);
1235
50
  lua_unlock(L);
1236
50
  return res;
1237
50
}
1238
1239
1240
1241
/*
1242
** miscellaneous functions
1243
*/
1244
1245
1246
0
LUA_API int lua_error (lua_State *L) {
1247
0
  TValue *errobj;
1248
0
  lua_lock(L);
1249
0
  errobj = s2v(L->top.p - 1);
1250
0
  api_checkpop(L, 1);
1251
  /* error object is the memory error message? */
1252
0
  if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
1253
0
    luaM_error(L);  /* raise a memory error */
1254
0
  else
1255
0
    luaG_errormsg(L);  /* raise a regular error */
1256
  /* code unreachable; will unlock when control actually leaves the kernel */
1257
0
  return 0;  /* to avoid warnings */
1258
0
}
1259
1260
1261
0
LUA_API int lua_next (lua_State *L, int idx) {
1262
0
  Table *t;
1263
0
  int more;
1264
0
  lua_lock(L);
1265
0
  api_checkpop(L, 1);
1266
0
  t = gettable(L, idx);
1267
0
  more = luaH_next(L, t, L->top.p - 1);
1268
0
  if (more)
1269
0
    api_incr_top(L);
1270
0
  else  /* no more elements */
1271
0
    L->top.p--;  /* pop key */
1272
0
  lua_unlock(L);
1273
0
  return more;
1274
0
}
1275
1276
1277
50
LUA_API void lua_toclose (lua_State *L, int idx) {
1278
50
  StkId o;
1279
50
  lua_lock(L);
1280
50
  o = index2stack(L, idx);
1281
50
  api_check(L, L->tbclist.p < o, "given index below or equal a marked one");
1282
50
  luaF_newtbcupval(L, o);  /* create new to-be-closed upvalue */
1283
50
  L->ci->callstatus |= CIST_TBC;  /* mark that function has TBC slots */
1284
50
  lua_unlock(L);
1285
50
}
1286
1287
1288
0
LUA_API void lua_concat (lua_State *L, int n) {
1289
0
  lua_lock(L);
1290
0
  api_checknelems(L, n);
1291
0
  if (n > 0) {
1292
0
    luaV_concat(L, n);
1293
0
    luaC_checkGC(L);
1294
0
  }
1295
0
  else {  /* nothing to concatenate */
1296
0
    setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0));  /* push empty string */
1297
0
    api_incr_top(L);
1298
0
  }
1299
0
  lua_unlock(L);
1300
0
}
1301
1302
1303
0
LUA_API void lua_len (lua_State *L, int idx) {
1304
0
  TValue *t;
1305
0
  lua_lock(L);
1306
0
  t = index2value(L, idx);
1307
0
  luaV_objlen(L, L->top.p, t);
1308
0
  api_incr_top(L);
1309
0
  lua_unlock(L);
1310
0
}
1311
1312
1313
200
LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
1314
200
  lua_Alloc f;
1315
200
  lua_lock(L);
1316
200
  if (ud) *ud = G(L)->ud;
1317
200
  f = G(L)->frealloc;
1318
200
  lua_unlock(L);
1319
200
  return f;
1320
200
}
1321
1322
1323
0
LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
1324
0
  lua_lock(L);
1325
0
  G(L)->ud = ud;
1326
0
  G(L)->frealloc = f;
1327
0
  lua_unlock(L);
1328
0
}
1329
1330
1331
441
void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
1332
441
  lua_lock(L);
1333
441
  G(L)->ud_warn = ud;
1334
441
  G(L)->warnf = f;
1335
441
  lua_unlock(L);
1336
441
}
1337
1338
1339
0
void lua_warning (lua_State *L, const char *msg, int tocont) {
1340
0
  lua_lock(L);
1341
0
  luaE_warning(L, msg, tocont);
1342
0
  lua_unlock(L);
1343
0
}
1344
1345
1346
1347
50
LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
1348
50
  Udata *u;
1349
50
  lua_lock(L);
1350
50
  api_check(L, 0 <= nuvalue && nuvalue < SHRT_MAX, "invalid value");
1351
50
  u = luaS_newudata(L, size, cast(unsigned short, nuvalue));
1352
50
  setuvalue(L, s2v(L->top.p), u);
1353
50
  api_incr_top(L);
1354
50
  luaC_checkGC(L);
1355
50
  lua_unlock(L);
1356
50
  return getudatamem(u);
1357
50
}
1358
1359
1360
1361
static const char *aux_upvalue (TValue *fi, int n, TValue **val,
1362
0
                                GCObject **owner) {
1363
0
  switch (ttypetag(fi)) {
1364
0
    case LUA_VCCL: {  /* C closure */
1365
0
      CClosure *f = clCvalue(fi);
1366
0
      if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
1367
0
        return NULL;  /* 'n' not in [1, f->nupvalues] */
1368
0
      *val = &f->upvalue[n-1];
1369
0
      if (owner) *owner = obj2gco(f);
1370
0
      return "";
1371
0
    }
1372
0
    case LUA_VLCL: {  /* Lua closure */
1373
0
      LClosure *f = clLvalue(fi);
1374
0
      TString *name;
1375
0
      Proto *p = f->p;
1376
0
      if (!(cast_uint(n) - 1u  < cast_uint(p->sizeupvalues)))
1377
0
        return NULL;  /* 'n' not in [1, p->sizeupvalues] */
1378
0
      *val = f->upvals[n-1]->v.p;
1379
0
      if (owner) *owner = obj2gco(f->upvals[n - 1]);
1380
0
      name = p->upvalues[n-1].name;
1381
0
      return (name == NULL) ? "(no name)" : getstr(name);
1382
0
    }
1383
0
    default: return NULL;  /* not a closure */
1384
0
  }
1385
0
}
1386
1387
1388
0
LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1389
0
  const char *name;
1390
0
  TValue *val = NULL;  /* to avoid warnings */
1391
0
  lua_lock(L);
1392
0
  name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
1393
0
  if (name) {
1394
0
    setobj2s(L, L->top.p, val);
1395
0
    api_incr_top(L);
1396
0
  }
1397
0
  lua_unlock(L);
1398
0
  return name;
1399
0
}
1400
1401
1402
0
LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
1403
0
  const char *name;
1404
0
  TValue *val = NULL;  /* to avoid warnings */
1405
0
  GCObject *owner = NULL;  /* to avoid warnings */
1406
0
  TValue *fi;
1407
0
  lua_lock(L);
1408
0
  fi = index2value(L, funcindex);
1409
0
  api_checknelems(L, 1);
1410
0
  name = aux_upvalue(fi, n, &val, &owner);
1411
0
  if (name) {
1412
0
    L->top.p--;
1413
0
    setobj(L, val, s2v(L->top.p));
1414
0
    luaC_barrier(L, owner, val);
1415
0
  }
1416
0
  lua_unlock(L);
1417
0
  return name;
1418
0
}
1419
1420
1421
0
static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
1422
0
  static const UpVal *const nullup = NULL;
1423
0
  LClosure *f;
1424
0
  TValue *fi = index2value(L, fidx);
1425
0
  api_check(L, ttisLclosure(fi), "Lua function expected");
1426
0
  f = clLvalue(fi);
1427
0
  if (pf) *pf = f;
1428
0
  if (1 <= n && n <= f->p->sizeupvalues)
1429
0
    return &f->upvals[n - 1];  /* get its upvalue pointer */
1430
0
  else
1431
0
    return (UpVal**)&nullup;
1432
0
}
1433
1434
1435
0
LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
1436
0
  TValue *fi = index2value(L, fidx);
1437
0
  switch (ttypetag(fi)) {
1438
0
    case LUA_VLCL: {  /* lua closure */
1439
0
      return *getupvalref(L, fidx, n, NULL);
1440
0
    }
1441
0
    case LUA_VCCL: {  /* C closure */
1442
0
      CClosure *f = clCvalue(fi);
1443
0
      if (1 <= n && n <= f->nupvalues)
1444
0
        return &f->upvalue[n - 1];
1445
      /* else */
1446
0
    }  /* FALLTHROUGH */
1447
0
    case LUA_VLCF:
1448
0
      return NULL;  /* light C functions have no upvalues */
1449
0
    default: {
1450
0
      api_check(L, 0, "function expected");
1451
0
      return NULL;
1452
0
    }
1453
0
  }
1454
0
}
1455
1456
1457
LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
1458
0
                                            int fidx2, int n2) {
1459
0
  LClosure *f1;
1460
0
  UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
1461
0
  UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
1462
0
  api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index");
1463
0
  *up1 = *up2;
1464
0
  luaC_objbarrier(L, f1, *up1);
1465
0
}
1466
1467