Coverage Report

Created: 2023-09-15 06:13

/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
399
#define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue)
47
48
49
/* test for pseudo index */
50
11.1k
#define ispseudo(i)   ((i) <= LUA_REGISTRYINDEX)
51
52
/* test for upvalue */
53
10
#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
11.8k
static TValue *index2value (lua_State *L, int idx) {
61
11.8k
  CallInfo *ci = L->ci;
62
11.8k
  if (idx > 0) {
63
754
    StkId o = ci->func.p + idx;
64
754
    api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index");
65
754
    if (o >= L->top.p) return &G(L)->nilvalue;
66
754
    else return s2v(o);
67
754
  }
68
11.1k
  else if (!ispseudo(idx)) {  /* negative index */
69
11.1k
    api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
70
11.1k
                 "invalid index");
71
11.1k
    return s2v(L->top.p + idx);
72
11.1k
  }
73
0
  else if (idx == LUA_REGISTRYINDEX)
74
0
    return &G(L)->l_registry;
75
0
  else {  /* upvalues */
76
0
    idx = LUA_REGISTRYINDEX - idx;
77
0
    api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
78
0
    if (ttisCclosure(s2v(ci->func.p))) {  /* C closure? */
79
0
      CClosure *func = clCvalue(s2v(ci->func.p));
80
0
      return (idx <= func->nupvalues) ? &func->upvalue[idx-1]
81
0
                                      : &G(L)->nilvalue;
82
0
    }
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
0
  }
88
11.8k
}
89
90
91
92
/*
93
** Convert a valid actual index (not a pseudo-index) to its address.
94
*/
95
17
l_sinline StkId index2stack (lua_State *L, int idx) {
96
17
  CallInfo *ci = L->ci;
97
17
  if (idx > 0) {
98
17
    StkId o = ci->func.p + idx;
99
17
    api_check(L, o < L->top.p, "invalid index");
100
17
    return o;
101
17
  }
102
0
  else {    /* non-positive index */
103
0
    api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
104
0
                 "invalid index");
105
0
    api_check(L, !ispseudo(idx), "invalid index");
106
0
    return L->top.p + idx;
107
0
  }
108
17
}
109
110
111
87
LUA_API int lua_checkstack (lua_State *L, int n) {
112
87
  int res;
113
87
  CallInfo *ci;
114
87
  lua_lock(L);
115
87
  ci = L->ci;
116
87
  api_check(L, n >= 0, "negative 'n'");
117
87
  if (L->stack_last.p - L->top.p > n)  /* stack large enough? */
118
53
    res = 1;  /* yes; check is OK */
119
34
  else  /* need to grow stack */
120
34
    res = luaD_growstack(L, n, 0);
121
87
  if (res && ci->top.p < L->top.p + n)
122
39
    ci->top.p = L->top.p + n;  /* adjust frame top */
123
87
  lua_unlock(L);
124
87
  return res;
125
87
}
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
1.10k
LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
145
1.10k
  lua_CFunction old;
146
1.10k
  lua_lock(L);
147
1.10k
  old = G(L)->panic;
148
1.10k
  G(L)->panic = panicf;
149
1.10k
  lua_unlock(L);
150
1.10k
  return old;
151
1.10k
}
152
153
154
0
LUA_API lua_Number lua_version (lua_State *L) {
155
0
  UNUSED(L);
156
0
  return LUA_VERSION_NUM;
157
0
}
158
159
160
161
/*
162
** basic stack manipulation
163
*/
164
165
166
/*
167
** convert an acceptable stack index into an absolute index
168
*/
169
0
LUA_API int lua_absindex (lua_State *L, int idx) {
170
0
  return (idx > 0 || ispseudo(idx))
171
0
         ? idx
172
0
         : cast_int(L->top.p - L->ci->func.p) + idx;
173
0
}
174
175
176
10.9k
LUA_API int lua_gettop (lua_State *L) {
177
10.9k
  return cast_int(L->top.p - (L->ci->func.p + 1));
178
10.9k
}
179
180
181
1.15k
LUA_API void lua_settop (lua_State *L, int idx) {
182
1.15k
  CallInfo *ci;
183
1.15k
  StkId func, newtop;
184
1.15k
  ptrdiff_t diff;  /* difference for new top */
185
1.15k
  lua_lock(L);
186
1.15k
  ci = L->ci;
187
1.15k
  func = ci->func.p;
188
1.15k
  if (idx >= 0) {
189
1.11k
    api_check(L, idx <= ci->top.p - (func + 1), "new top too large");
190
1.11k
    diff = ((func + 1) + idx) - L->top.p;
191
1.11k
    for (; diff > 0; diff--)
192
1.11k
      setnilvalue(s2v(L->top.p++));  /* clear new slots */
193
1.11k
  }
194
32
  else {
195
32
    api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top");
196
32
    diff = idx + 1;  /* will "subtract" index (as it is negative) */
197
32
  }
198
1.15k
  api_check(L, L->tbclist.p < L->top.p, "previous pop of an unclosed slot");
199
1.15k
  newtop = L->top.p + diff;
200
1.15k
  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
1.15k
  L->top.p = newtop;  /* correct top only after closing any upvalue */
205
1.15k
  lua_unlock(L);
206
1.15k
}
207
208
209
0
LUA_API void lua_closeslot (lua_State *L, int idx) {
210
0
  StkId level;
211
0
  lua_lock(L);
212
0
  level = index2stack(L, idx);
213
0
  api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist.p == level,
214
0
     "no variable to close at given level");
215
0
  level = luaF_close(L, level, CLOSEKTOP, 0);
216
0
  setnilvalue(s2v(level));
217
0
  lua_unlock(L);
218
0
}
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
51
l_sinline void reverse (lua_State *L, StkId from, StkId to) {
228
61
  for (; from < to; from++, to--) {
229
10
    TValue temp;
230
10
    setobj(L, &temp, s2v(from));
231
10
    setobjs2s(L, from, to);
232
10
    setobj2s(L, to, &temp);
233
10
  }
234
51
}
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
17
LUA_API void lua_rotate (lua_State *L, int idx, int n) {
242
17
  StkId p, t, m;
243
17
  lua_lock(L);
244
17
  t = L->top.p - 1;  /* end of stack segment being rotated */
245
17
  p = index2stack(L, idx);  /* start of segment */
246
17
  api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
247
17
  m = (n >= 0 ? t - n : p - n - 1);  /* end of prefix */
248
17
  reverse(L, p, m);  /* reverse the prefix with length 'n' */
249
17
  reverse(L, m + 1, t);  /* reverse the suffix */
250
17
  reverse(L, p, t);  /* reverse the entire segment */
251
17
  lua_unlock(L);
252
17
}
253
254
255
10
LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
256
10
  TValue *fr, *to;
257
10
  lua_lock(L);
258
10
  fr = index2value(L, fromidx);
259
10
  to = index2value(L, toidx);
260
10
  api_check(L, isvalid(L, to), "invalid index");
261
10
  setobj(L, to, fr);
262
10
  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
10
  lua_unlock(L);
267
10
}
268
269
270
21
LUA_API void lua_pushvalue (lua_State *L, int idx) {
271
21
  lua_lock(L);
272
21
  setobj2s(L, L->top.p, index2value(L, idx));
273
21
  api_incr_top(L);
274
21
  lua_unlock(L);
275
21
}
276
277
278
279
/*
280
** access functions (stack -> C)
281
*/
282
283
284
399
LUA_API int lua_type (lua_State *L, int idx) {
285
399
  const TValue *o = index2value(L, idx);
286
399
  return (isvalid(L, o) ? ttype(o) : LUA_TNONE);
287
399
}
288
289
290
4
LUA_API const char *lua_typename (lua_State *L, int t) {
291
4
  UNUSED(L);
292
4
  api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type");
293
4
  return ttypename(t);
294
4
}
295
296
297
8
LUA_API int lua_iscfunction (lua_State *L, int idx) {
298
8
  const TValue *o = index2value(L, idx);
299
8
  return (ttislcf(o) || (ttisCclosure(o)));
300
8
}
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
22
LUA_API int lua_isnumber (lua_State *L, int idx) {
310
22
  lua_Number n;
311
22
  const TValue *o = index2value(L, idx);
312
22
  return tonumber(o, &n);
313
22
}
314
315
316
12
LUA_API int lua_isstring (lua_State *L, int idx) {
317
12
  const TValue *o = index2value(L, idx);
318
12
  return (ttisstring(o) || cvt2str(o));
319
12
}
320
321
322
21
LUA_API int lua_isuserdata (lua_State *L, int idx) {
323
21
  const TValue *o = index2value(L, idx);
324
21
  return (ttisfulluserdata(o) || ttislightuserdata(o));
325
21
}
326
327
328
0
LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
329
0
  const TValue *o1 = index2value(L, index1);
330
0
  const TValue *o2 = index2value(L, index2);
331
0
  return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0;
332
0
}
333
334
335
0
LUA_API void lua_arith (lua_State *L, int op) {
336
0
  lua_lock(L);
337
0
  if (op != LUA_OPUNM && op != LUA_OPBNOT)
338
0
    api_checknelems(L, 2);  /* all other operations expect two operands */
339
0
  else {  /* for unary operations, add fake 2nd operand */
340
0
    api_checknelems(L, 1);
341
0
    setobjs2s(L, L->top.p, L->top.p - 1);
342
0
    api_incr_top(L);
343
0
  }
344
  /* first operand at top - 2, second at top - 1; result go to top - 2 */
345
0
  luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2);
346
0
  L->top.p--;  /* remove second operand */
347
0
  lua_unlock(L);
348
0
}
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
0
LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
372
0
  size_t sz = luaO_str2num(s, s2v(L->top.p));
373
0
  if (sz != 0)
374
0
    api_incr_top(L);
375
0
  return sz;
376
0
}
377
378
379
32
LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
380
32
  lua_Number n = 0;
381
32
  const TValue *o = index2value(L, idx);
382
32
  int isnum = tonumber(o, &n);
383
32
  if (pisnum)
384
0
    *pisnum = isnum;
385
32
  return n;
386
32
}
387
388
389
21
LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
390
21
  lua_Integer res = 0;
391
21
  const TValue *o = index2value(L, idx);
392
21
  int isnum = tointeger(o, &res);
393
21
  if (pisnum)
394
0
    *pisnum = isnum;
395
21
  return res;
396
21
}
397
398
399
22
LUA_API int lua_toboolean (lua_State *L, int idx) {
400
22
  const TValue *o = index2value(L, idx);
401
22
  return !l_isfalse(o);
402
22
}
403
404
405
32
LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
406
32
  TValue *o;
407
32
  lua_lock(L);
408
32
  o = index2value(L, idx);
409
32
  if (!ttisstring(o)) {
410
31
    if (!cvt2str(o)) {  /* not convertible? */
411
17
      if (len != NULL) *len = 0;
412
17
      lua_unlock(L);
413
17
      return NULL;
414
17
    }
415
14
    luaO_tostring(L, o);
416
14
    luaC_checkGC(L);
417
14
    o = index2value(L, idx);  /* previous call may reallocate the stack */
418
14
  }
419
15
  if (len != NULL)
420
0
    *len = tsslen(tsvalue(o));
421
15
  lua_unlock(L);
422
15
  return getstr(tsvalue(o));
423
32
}
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
12
l_sinline void *touserdata (const TValue *o) {
448
12
  switch (ttype(o)) {
449
0
    case LUA_TUSERDATA: return getudatamem(uvalue(o));
450
0
    case LUA_TLIGHTUSERDATA: return pvalue(o);
451
12
    default: return NULL;
452
12
  }
453
12
}
454
455
456
12
LUA_API void *lua_touserdata (lua_State *L, int idx) {
457
12
  const TValue *o = index2value(L, idx);
458
12
  return touserdata(o);
459
12
}
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
0
LUA_API const void *lua_topointer (lua_State *L, int idx) {
476
0
  const TValue *o = index2value(L, idx);
477
0
  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
0
    default: {
482
0
      if (iscollectable(o))
483
0
        return gcvalue(o);
484
0
      else
485
0
        return NULL;
486
0
    }
487
0
  }
488
0
}
489
490
491
492
/*
493
** push functions (C -> stack)
494
*/
495
496
497
401
LUA_API void lua_pushnil (lua_State *L) {
498
401
  lua_lock(L);
499
401
  setnilvalue(s2v(L->top.p));
500
401
  api_incr_top(L);
501
401
  lua_unlock(L);
502
401
}
503
504
505
11.4k
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
506
11.4k
  lua_lock(L);
507
11.4k
  setfltvalue(s2v(L->top.p), n);
508
11.4k
  api_incr_top(L);
509
11.4k
  lua_unlock(L);
510
11.4k
}
511
512
513
92
LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
514
92
  lua_lock(L);
515
92
  setivalue(s2v(L->top.p), n);
516
92
  api_incr_top(L);
517
92
  lua_unlock(L);
518
92
}
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
138
LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
527
138
  TString *ts;
528
138
  lua_lock(L);
529
138
  ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
530
138
  setsvalue2s(L, L->top.p, ts);
531
138
  api_incr_top(L);
532
138
  luaC_checkGC(L);
533
138
  lua_unlock(L);
534
138
  return getstr(ts);
535
138
}
536
537
538
177
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
539
177
  lua_lock(L);
540
177
  if (s == NULL)
541
177
    setnilvalue(s2v(L->top.p));
542
177
  else {
543
177
    TString *ts;
544
177
    ts = luaS_new(L, s);
545
177
    setsvalue2s(L, L->top.p, ts);
546
177
    s = getstr(ts);  /* internal copy's address */
547
177
  }
548
177
  api_incr_top(L);
549
177
  luaC_checkGC(L);
550
177
  lua_unlock(L);
551
177
  return s;
552
177
}
553
554
555
LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
556
0
                                      va_list argp) {
557
0
  const char *ret;
558
0
  lua_lock(L);
559
0
  ret = luaO_pushvfstring(L, fmt, argp);
560
0
  luaC_checkGC(L);
561
0
  lua_unlock(L);
562
0
  return ret;
563
0
}
564
565
566
0
LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
567
0
  const char *ret;
568
0
  va_list argp;
569
0
  lua_lock(L);
570
0
  va_start(argp, fmt);
571
0
  ret = luaO_pushvfstring(L, fmt, argp);
572
0
  va_end(argp);
573
0
  luaC_checkGC(L);
574
0
  lua_unlock(L);
575
0
  return ret;
576
0
}
577
578
579
40
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
580
40
  lua_lock(L);
581
40
  if (n == 0) {
582
2
    setfvalue(s2v(L->top.p), fn);
583
2
    api_incr_top(L);
584
2
  }
585
38
  else {
586
38
    CClosure *cl;
587
38
    api_checknelems(L, n);
588
38
    api_check(L, n <= MAXUPVAL, "upvalue index too large");
589
38
    cl = luaF_newCclosure(L, n);
590
38
    cl->f = fn;
591
38
    L->top.p -= n;
592
205
    while (n--) {
593
167
      setobj2n(L, &cl->upvalue[n], s2v(L->top.p + n));
594
      /* does not need barrier because closure is white */
595
167
      lua_assert(iswhite(cl));
596
167
    }
597
38
    setclCvalue(L, s2v(L->top.p), cl);
598
38
    api_incr_top(L);
599
38
    luaC_checkGC(L);
600
38
  }
601
40
  lua_unlock(L);
602
40
}
603
604
605
76
LUA_API void lua_pushboolean (lua_State *L, int b) {
606
76
  lua_lock(L);
607
76
  if (b)
608
76
    setbtvalue(s2v(L->top.p));
609
17
  else
610
76
    setbfvalue(s2v(L->top.p));
611
76
  api_incr_top(L);
612
76
  lua_unlock(L);
613
76
}
614
615
616
0
LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
617
0
  lua_lock(L);
618
0
  setpvalue(s2v(L->top.p), p);
619
0
  api_incr_top(L);
620
0
  lua_unlock(L);
621
0
}
622
623
624
2
LUA_API int lua_pushthread (lua_State *L) {
625
2
  lua_lock(L);
626
2
  setthvalue(L, s2v(L->top.p), L);
627
2
  api_incr_top(L);
628
2
  lua_unlock(L);
629
2
  return (G(L)->mainthread == L);
630
2
}
631
632
633
634
/*
635
** get functions (Lua -> stack)
636
*/
637
638
639
2
l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) {
640
2
  const TValue *slot;
641
2
  TString *str = luaS_new(L, k);
642
2
  if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
643
0
    setobj2s(L, L->top.p, slot);
644
0
    api_incr_top(L);
645
0
  }
646
2
  else {
647
2
    setsvalue2s(L, L->top.p, str);
648
2
    api_incr_top(L);
649
2
    luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
650
2
  }
651
2
  lua_unlock(L);
652
2
  return ttype(s2v(L->top.p - 1));
653
2
}
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
6
  (&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1])
664
665
666
2
LUA_API int lua_getglobal (lua_State *L, const char *name) {
667
2
  const TValue *G;
668
2
  lua_lock(L);
669
2
  G = getGtable(L);
670
2
  return auxgetstr(L, G, name);
671
2
}
672
673
674
0
LUA_API int lua_gettable (lua_State *L, int idx) {
675
0
  const TValue *slot;
676
0
  TValue *t;
677
0
  lua_lock(L);
678
0
  t = index2value(L, idx);
679
0
  if (luaV_fastget(L, t, s2v(L->top.p - 1), slot, luaH_get)) {
680
0
    setobj2s(L, L->top.p - 1, slot);
681
0
  }
682
0
  else
683
0
    luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
684
0
  lua_unlock(L);
685
0
  return ttype(s2v(L->top.p - 1));
686
0
}
687
688
689
0
LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
690
0
  lua_lock(L);
691
0
  return auxgetstr(L, index2value(L, idx), k);
692
0
}
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
56
l_sinline int finishrawget (lua_State *L, const TValue *val) {
715
56
  if (isempty(val))  /* avoid copying empty items to the stack */
716
56
    setnilvalue(s2v(L->top.p));
717
0
  else
718
56
    setobj2s(L, L->top.p, val);
719
56
  api_incr_top(L);
720
56
  lua_unlock(L);
721
56
  return ttype(s2v(L->top.p - 1));
722
56
}
723
724
725
11.1k
static Table *gettable (lua_State *L, int idx) {
726
11.1k
  TValue *t = index2value(L, idx);
727
11.1k
  api_check(L, ttistable(t), "table expected");
728
11.1k
  return hvalue(t);
729
11.1k
}
730
731
732
25
LUA_API int lua_rawget (lua_State *L, int idx) {
733
25
  Table *t;
734
25
  const TValue *val;
735
25
  lua_lock(L);
736
25
  api_checknelems(L, 1);
737
25
  t = gettable(L, idx);
738
25
  val = luaH_get(t, s2v(L->top.p - 1));
739
25
  L->top.p--;  /* remove key */
740
25
  return finishrawget(L, val);
741
25
}
742
743
744
31
LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
745
31
  Table *t;
746
31
  lua_lock(L);
747
31
  t = gettable(L, idx);
748
31
  return finishrawget(L, luaH_getint(t, n));
749
31
}
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
978
LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
763
978
  Table *t;
764
978
  lua_lock(L);
765
978
  t = luaH_new(L);
766
978
  sethvalue2s(L, L->top.p, t);
767
978
  api_incr_top(L);
768
978
  if (narray > 0 || nrec > 0)
769
82
    luaH_resize(L, t, narray, nrec);
770
978
  luaC_checkGC(L);
771
978
  lua_unlock(L);
772
978
}
773
774
775
17
LUA_API int lua_getmetatable (lua_State *L, int objindex) {
776
17
  const TValue *obj;
777
17
  Table *mt;
778
17
  int res = 0;
779
17
  lua_lock(L);
780
17
  obj = index2value(L, objindex);
781
17
  switch (ttype(obj)) {
782
5
    case LUA_TTABLE:
783
5
      mt = hvalue(obj)->metatable;
784
5
      break;
785
0
    case LUA_TUSERDATA:
786
0
      mt = uvalue(obj)->metatable;
787
0
      break;
788
12
    default:
789
12
      mt = G(L)->mt[ttype(obj)];
790
12
      break;
791
17
  }
792
17
  if (mt != NULL) {
793
0
    sethvalue2s(L, L->top.p, mt);
794
0
    api_incr_top(L);
795
0
    res = 1;
796
0
  }
797
17
  lua_unlock(L);
798
17
  return res;
799
17
}
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
4
static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
830
4
  const TValue *slot;
831
4
  TString *str = luaS_new(L, k);
832
4
  api_checknelems(L, 1);
833
4
  if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
834
0
    luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
835
0
    L->top.p--;  /* pop value */
836
0
  }
837
4
  else {
838
4
    setsvalue2s(L, L->top.p, str);  /* push 'str' (to make it a TValue) */
839
4
    api_incr_top(L);
840
4
    luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot);
841
4
    L->top.p -= 2;  /* pop value and key */
842
4
  }
843
4
  lua_unlock(L);  /* lock done by caller */
844
4
}
845
846
847
4
LUA_API void lua_setglobal (lua_State *L, const char *name) {
848
4
  const TValue *G;
849
4
  lua_lock(L);  /* unlock done in 'auxsetstr' */
850
4
  G = getGtable(L);
851
4
  auxsetstr(L, G, name);
852
4
}
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
0
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
872
0
  lua_lock(L);  /* unlock done in 'auxsetstr' */
873
0
  auxsetstr(L, index2value(L, idx), k);
874
0
}
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
39
static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
897
39
  Table *t;
898
39
  lua_lock(L);
899
39
  api_checknelems(L, n);
900
39
  t = gettable(L, idx);
901
39
  luaH_set(L, t, key, s2v(L->top.p - 1));
902
39
  invalidateTMcache(t);
903
39
  luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
904
39
  L->top.p -= n;
905
39
  lua_unlock(L);
906
39
}
907
908
909
39
LUA_API void lua_rawset (lua_State *L, int idx) {
910
39
  aux_rawset(L, idx, s2v(L->top.p - 2), 2);
911
39
}
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
11.1k
LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
922
11.1k
  Table *t;
923
11.1k
  lua_lock(L);
924
11.1k
  api_checknelems(L, 1);
925
11.1k
  t = gettable(L, idx);
926
11.1k
  luaH_setint(L, t, n, s2v(L->top.p - 1));
927
11.1k
  luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
928
11.1k
  L->top.p--;
929
11.1k
  lua_unlock(L);
930
11.1k
}
931
932
933
0
LUA_API int lua_setmetatable (lua_State *L, int objindex) {
934
0
  TValue *obj;
935
0
  Table *mt;
936
0
  lua_lock(L);
937
0
  api_checknelems(L, 1);
938
0
  obj = index2value(L, objindex);
939
0
  if (ttisnil(s2v(L->top.p - 1)))
940
0
    mt = NULL;
941
0
  else {
942
0
    api_check(L, ttistable(s2v(L->top.p - 1)), "table expected");
943
0
    mt = hvalue(s2v(L->top.p - 1));
944
0
  }
945
0
  switch (ttype(obj)) {
946
0
    case LUA_TTABLE: {
947
0
      hvalue(obj)->metatable = mt;
948
0
      if (mt) {
949
0
        luaC_objbarrier(L, gcvalue(obj), mt);
950
0
        luaC_checkfinalizer(L, gcvalue(obj), mt);
951
0
      }
952
0
      break;
953
0
    }
954
0
    case LUA_TUSERDATA: {
955
0
      uvalue(obj)->metatable = mt;
956
0
      if (mt) {
957
0
        luaC_objbarrier(L, uvalue(obj), mt);
958
0
        luaC_checkfinalizer(L, gcvalue(obj), mt);
959
0
      }
960
0
      break;
961
0
    }
962
0
    default: {
963
0
      G(L)->mt[ttype(obj)] = mt;
964
0
      break;
965
0
    }
966
0
  }
967
0
  L->top.p--;
968
0
  lua_unlock(L);
969
0
  return 1;
970
0
}
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
0
     api_check(L, (nr) == LUA_MULTRET \
1000
0
               || (L->ci->top.p - L->top.p >= (nr) - (na)), \
1001
0
  "results from function overflow current stack size")
1002
1003
1004
LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
1005
0
                        lua_KContext ctx, lua_KFunction k) {
1006
0
  StkId func;
1007
0
  lua_lock(L);
1008
0
  api_check(L, k == NULL || !isLua(L->ci),
1009
0
    "cannot use continuations inside hooks");
1010
0
  api_checknelems(L, nargs+1);
1011
0
  api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
1012
0
  checkresults(L, nargs, nresults);
1013
0
  func = L->top.p - (nargs+1);
1014
0
  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
0
  else  /* no continuation or no yieldable */
1020
0
    luaD_callnoyield(L, func, nresults);  /* just do the call */
1021
0
  adjustresults(L, nresults);
1022
0
  lua_unlock(L);
1023
0
}
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
0
static void f_call (lua_State *L, void *ud) {
1037
0
  struct CallS *c = cast(struct CallS *, ud);
1038
0
  luaD_callnoyield(L, c->func, c->nresults);
1039
0
}
1040
1041
1042
1043
LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
1044
0
                        lua_KContext ctx, lua_KFunction k) {
1045
0
  struct CallS c;
1046
0
  int status;
1047
0
  ptrdiff_t func;
1048
0
  lua_lock(L);
1049
0
  api_check(L, k == NULL || !isLua(L->ci),
1050
0
    "cannot use continuations inside hooks");
1051
0
  api_checknelems(L, nargs+1);
1052
0
  api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
1053
0
  checkresults(L, nargs, nresults);
1054
0
  if (errfunc == 0)
1055
0
    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
0
  c.func = L->top.p - (nargs+1);  /* function to be called */
1062
0
  if (k == NULL || !yieldable(L)) {  /* no continuation or no yieldable? */
1063
0
    c.nresults = nresults;  /* do a 'conventional' protected call */
1064
0
    status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
1065
0
  }
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
0
  adjustresults(L, nresults);
1082
0
  lua_unlock(L);
1083
0
  return status;
1084
0
}
1085
1086
1087
LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
1088
0
                      const char *chunkname, const char *mode) {
1089
0
  ZIO z;
1090
0
  int status;
1091
0
  lua_lock(L);
1092
0
  if (!chunkname) chunkname = "?";
1093
0
  luaZ_init(L, &z, reader, data);
1094
0
  status = luaD_protectedparser(L, &z, chunkname, mode);
1095
0
  if (status == LUA_OK) {  /* no errors? */
1096
0
    LClosure *f = clLvalue(s2v(L->top.p - 1));  /* get new function */
1097
0
    if (f->nupvalues >= 1) {  /* does it have an upvalue? */
1098
      /* get global table from registry */
1099
0
      const TValue *gt = getGtable(L);
1100
      /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
1101
0
      setobj(L, f->upvals[0]->v.p, gt);
1102
0
      luaC_barrier(L, f->upvals[0], gt);
1103
0
    }
1104
0
  }
1105
0
  lua_unlock(L);
1106
0
  return status;
1107
0
}
1108
1109
1110
0
LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
1111
0
  int status;
1112
0
  TValue *o;
1113
0
  lua_lock(L);
1114
0
  api_checknelems(L, 1);
1115
0
  o = s2v(L->top.p - 1);
1116
0
  if (isLfunction(o))
1117
0
    status = luaU_dump(L, getproto(o), writer, data, strip);
1118
0
  else
1119
0
    status = 1;
1120
0
  lua_unlock(L);
1121
0
  return status;
1122
0
}
1123
1124
1125
1
LUA_API int lua_status (lua_State *L) {
1126
1
  return L->status;
1127
1
}
1128
1129
1130
/*
1131
** Garbage-collection function
1132
*/
1133
1.02k
LUA_API int lua_gc (lua_State *L, int what, ...) {
1134
1.02k
  va_list argp;
1135
1.02k
  int res = 0;
1136
1.02k
  global_State *g = G(L);
1137
1.02k
  if (g->gcstp & GCSTPGC)  /* internal stop? */
1138
0
    return -1;  /* all options are invalid when stopped */
1139
1.02k
  lua_lock(L);
1140
1.02k
  va_start(argp, what);
1141
1.02k
  switch (what) {
1142
133
    case LUA_GCSTOP: {
1143
133
      g->gcstp = GCSTPUSR;  /* stopped by the user */
1144
133
      break;
1145
0
    }
1146
108
    case LUA_GCRESTART: {
1147
108
      luaE_setdebt(g, 0);
1148
108
      g->gcstp = 0;  /* (GCSTPGC must be already zero here) */
1149
108
      break;
1150
0
    }
1151
318
    case LUA_GCCOLLECT: {
1152
318
      luaC_fullgc(L, 0);
1153
318
      break;
1154
0
    }
1155
93
    case LUA_GCCOUNT: {
1156
      /* GC values are expressed in Kbytes: #bytes/2^10 */
1157
93
      res = cast_int(gettotalbytes(g) >> 10);
1158
93
      break;
1159
0
    }
1160
77
    case LUA_GCCOUNTB: {
1161
77
      res = cast_int(gettotalbytes(g) & 0x3ff);
1162
77
      break;
1163
0
    }
1164
93
    case LUA_GCSTEP: {
1165
93
      int data = va_arg(argp, int);
1166
93
      l_mem debt = 1;  /* =1 to signal that it did an actual step */
1167
93
      lu_byte oldstp = g->gcstp;
1168
93
      g->gcstp = 0;  /* allow GC to run (GCSTPGC must be zero here) */
1169
93
      if (data == 0) {
1170
93
        luaE_setdebt(g, 0);  /* do a basic step */
1171
93
        luaC_step(L);
1172
93
      }
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
93
      g->gcstp = oldstp;  /* restore previous state */
1179
93
      if (debt > 0 && g->gcstate == GCSpause)  /* end of cycle? */
1180
93
        res = 1;  /* signal it */
1181
93
      break;
1182
0
    }
1183
93
    case LUA_GCSETPAUSE: {
1184
93
      int data = va_arg(argp, int);
1185
93
      res = getgcparam(g->gcpause);
1186
93
      setgcparam(g->gcpause, data);
1187
93
      break;
1188
0
    }
1189
113
    case LUA_GCSETSTEPMUL: {
1190
113
      int data = va_arg(argp, int);
1191
113
      res = getgcparam(g->gcstepmul);
1192
113
      setgcparam(g->gcstepmul, data);
1193
113
      break;
1194
0
    }
1195
0
    case LUA_GCISRUNNING: {
1196
0
      res = gcrunning(g);
1197
0
      break;
1198
0
    }
1199
0
    case LUA_GCGEN: {
1200
0
      int minormul = va_arg(argp, int);
1201
0
      int majormul = va_arg(argp, int);
1202
0
      res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC;
1203
0
      if (minormul != 0)
1204
0
        g->genminormul = minormul;
1205
0
      if (majormul != 0)
1206
0
        setgcparam(g->genmajormul, majormul);
1207
0
      luaC_changemode(L, KGC_GEN);
1208
0
      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
1.02k
  }
1226
1.02k
  va_end(argp);
1227
1.02k
  lua_unlock(L);
1228
1.02k
  return res;
1229
1.02k
}
1230
1231
1232
1233
/*
1234
** miscellaneous functions
1235
*/
1236
1237
1238
0
LUA_API int lua_error (lua_State *L) {
1239
0
  TValue *errobj;
1240
0
  lua_lock(L);
1241
0
  errobj = s2v(L->top.p - 1);
1242
0
  api_checknelems(L, 1);
1243
  /* error object is the memory error message? */
1244
0
  if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
1245
0
    luaM_error(L);  /* raise a memory error */
1246
0
  else
1247
0
    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
0
}
1251
1252
1253
3
LUA_API int lua_next (lua_State *L, int idx) {
1254
3
  Table *t;
1255
3
  int more;
1256
3
  lua_lock(L);
1257
3
  api_checknelems(L, 1);
1258
3
  t = gettable(L, idx);
1259
3
  more = luaH_next(L, t, L->top.p - 1);
1260
3
  if (more) {
1261
0
    api_incr_top(L);
1262
0
  }
1263
3
  else  /* no more elements */
1264
3
    L->top.p -= 1;  /* remove key */
1265
3
  lua_unlock(L);
1266
3
  return more;
1267
3
}
1268
1269
1270
0
LUA_API void lua_toclose (lua_State *L, int idx) {
1271
0
  int nresults;
1272
0
  StkId o;
1273
0
  lua_lock(L);
1274
0
  o = index2stack(L, idx);
1275
0
  nresults = L->ci->nresults;
1276
0
  api_check(L, L->tbclist.p < o, "given index below or equal a marked one");
1277
0
  luaF_newtbcupval(L, o);  /* create new to-be-closed upvalue */
1278
0
  if (!hastocloseCfunc(nresults))  /* function not marked yet? */
1279
0
    L->ci->nresults = codeNresults(nresults);  /* mark it */
1280
0
  lua_assert(hastocloseCfunc(L->ci->nresults));
1281
0
  lua_unlock(L);
1282
0
}
1283
1284
1285
34
LUA_API void lua_concat (lua_State *L, int n) {
1286
34
  lua_lock(L);
1287
34
  api_checknelems(L, n);
1288
34
  if (n > 0)
1289
34
    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
34
  luaC_checkGC(L);
1295
34
  lua_unlock(L);
1296
34
}
1297
1298
1299
0
LUA_API void lua_len (lua_State *L, int idx) {
1300
0
  TValue *t;
1301
0
  lua_lock(L);
1302
0
  t = index2value(L, idx);
1303
0
  luaV_objlen(L, L->top.p, t);
1304
0
  api_incr_top(L);
1305
0
  lua_unlock(L);
1306
0
}
1307
1308
1309
0
LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
1310
0
  lua_Alloc f;
1311
0
  lua_lock(L);
1312
0
  if (ud) *ud = G(L)->ud;
1313
0
  f = G(L)->frealloc;
1314
0
  lua_unlock(L);
1315
0
  return f;
1316
0
}
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
1.10k
void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
1328
1.10k
  lua_lock(L);
1329
1.10k
  G(L)->ud_warn = ud;
1330
1.10k
  G(L)->warnf = f;
1331
1.10k
  lua_unlock(L);
1332
1.10k
}
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
0
LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
1344
0
  Udata *u;
1345
0
  lua_lock(L);
1346
0
  api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value");
1347
0
  u = luaS_newudata(L, size, nuvalue);
1348
0
  setuvalue(L, s2v(L->top.p), u);
1349
0
  api_incr_top(L);
1350
0
  luaC_checkGC(L);
1351
0
  lua_unlock(L);
1352
0
  return getudatamem(u);
1353
0
}
1354
1355
1356
1357
static const char *aux_upvalue (TValue *fi, int n, TValue **val,
1358
47
                                GCObject **owner) {
1359
47
  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
47
    default: return NULL;  /* not a closure */
1380
47
  }
1381
47
}
1382
1383
1384
25
LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1385
25
  const char *name;
1386
25
  TValue *val = NULL;  /* to avoid warnings */
1387
25
  lua_lock(L);
1388
25
  name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
1389
25
  if (name) {
1390
0
    setobj2s(L, L->top.p, val);
1391
0
    api_incr_top(L);
1392
0
  }
1393
25
  lua_unlock(L);
1394
25
  return name;
1395
25
}
1396
1397
1398
22
LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
1399
22
  const char *name;
1400
22
  TValue *val = NULL;  /* to avoid warnings */
1401
22
  GCObject *owner = NULL;  /* to avoid warnings */
1402
22
  TValue *fi;
1403
22
  lua_lock(L);
1404
22
  fi = index2value(L, funcindex);
1405
22
  api_checknelems(L, 1);
1406
22
  name = aux_upvalue(fi, n, &val, &owner);
1407
22
  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
22
  lua_unlock(L);
1413
22
  return name;
1414
22
}
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