Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/contrib/openzfs/module/lua/lapi.c
48383 views
1
// SPDX-License-Identifier: MIT
2
/*
3
** $Id: lapi.c,v 2.171.1.1 2013/04/12 18:48:47 roberto Exp $
4
** Lua API
5
** See Copyright Notice in lua.h
6
*/
7
8
9
#define lapi_c
10
#define LUA_CORE
11
12
#include <sys/lua/lua.h>
13
14
#include "lapi.h"
15
#include "ldebug.h"
16
#include "ldo.h"
17
#include "lfunc.h"
18
#include "lgc.h"
19
#include "lmem.h"
20
#include "lobject.h"
21
#include "lstate.h"
22
#include "lstring.h"
23
#include "ltable.h"
24
#include "ltm.h"
25
#include "lvm.h"
26
27
28
29
const char lua_ident[] =
30
"$LuaVersion: " LUA_COPYRIGHT " $"
31
"$LuaAuthors: " LUA_AUTHORS " $";
32
33
34
/* value at a non-valid index */
35
#define NONVALIDVALUE cast(TValue *, luaO_nilobject)
36
37
/* corresponding test */
38
#define isvalid(o) ((o) != luaO_nilobject)
39
40
/* test for pseudo index */
41
#define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
42
43
/* test for valid but not pseudo index */
44
#define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
45
46
#define api_checkvalidindex(L, o) api_check(L, isvalid(o), "invalid index")
47
48
#define api_checkstackindex(L, i, o) \
49
api_check(L, isstackindex(i, o), "index not in the stack")
50
51
52
static TValue *index2addr (lua_State *L, int idx) {
53
CallInfo *ci = L->ci;
54
if (idx > 0) {
55
TValue *o = ci->func + idx;
56
api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
57
if (o >= L->top) return NONVALIDVALUE;
58
else return o;
59
}
60
else if (!ispseudo(idx)) { /* negative index */
61
api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
62
return L->top + idx;
63
}
64
else if (idx == LUA_REGISTRYINDEX)
65
return &G(L)->l_registry;
66
else { /* upvalues */
67
idx = LUA_REGISTRYINDEX - idx;
68
api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
69
if (ttislcf(ci->func)) /* light C function? */
70
return NONVALIDVALUE; /* it has no upvalues */
71
else {
72
CClosure *func = clCvalue(ci->func);
73
return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
74
}
75
}
76
}
77
78
79
/*
80
** to be called by 'lua_checkstack' in protected mode, to grow stack
81
** capturing memory errors
82
*/
83
static void growstack (lua_State *L, void *ud) {
84
int size = *(int *)ud;
85
luaD_growstack(L, size);
86
}
87
88
89
LUA_API int lua_checkstack (lua_State *L, int size) {
90
int res;
91
CallInfo *ci = L->ci;
92
lua_lock(L);
93
if (L->stack_last - L->top > size) /* stack large enough? */
94
res = 1; /* yes; check is OK */
95
else { /* no; need to grow stack */
96
int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
97
if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */
98
res = 0; /* no */
99
else /* try to grow stack */
100
res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK);
101
}
102
if (res && ci->top < L->top + size)
103
ci->top = L->top + size; /* adjust frame top */
104
lua_unlock(L);
105
return res;
106
}
107
108
109
LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
110
int i;
111
if (from == to) return;
112
lua_lock(to);
113
api_checknelems(from, n);
114
api_check(from, G(from) == G(to), "moving among independent states");
115
api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
116
from->top -= n;
117
for (i = 0; i < n; i++) {
118
setobj2s(to, to->top++, from->top + i);
119
}
120
lua_unlock(to);
121
}
122
123
124
LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
125
lua_CFunction old;
126
lua_lock(L);
127
old = G(L)->panic;
128
G(L)->panic = panicf;
129
lua_unlock(L);
130
return old;
131
}
132
133
134
LUA_API const lua_Number *lua_version (lua_State *L) {
135
static const lua_Number version = LUA_VERSION_NUM;
136
if (L == NULL) return &version;
137
else return G(L)->version;
138
}
139
140
141
142
/*
143
** basic stack manipulation
144
*/
145
146
147
/*
148
** convert an acceptable stack index into an absolute index
149
*/
150
LUA_API int lua_absindex (lua_State *L, int idx) {
151
return (idx > 0 || ispseudo(idx))
152
? idx
153
: cast_int(L->top - L->ci->func + idx);
154
}
155
156
157
LUA_API int lua_gettop (lua_State *L) {
158
return cast_int(L->top - (L->ci->func + 1));
159
}
160
161
162
LUA_API void lua_settop (lua_State *L, int idx) {
163
StkId func = L->ci->func;
164
lua_lock(L);
165
if (idx >= 0) {
166
api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
167
while (L->top < (func + 1) + idx)
168
setnilvalue(L->top++);
169
L->top = (func + 1) + idx;
170
}
171
else {
172
api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
173
L->top += idx+1; /* `subtract' index (index is negative) */
174
}
175
lua_unlock(L);
176
}
177
178
179
LUA_API void lua_remove (lua_State *L, int idx) {
180
StkId p;
181
lua_lock(L);
182
p = index2addr(L, idx);
183
api_checkstackindex(L, idx, p);
184
while (++p < L->top) setobjs2s(L, p-1, p);
185
L->top--;
186
lua_unlock(L);
187
}
188
189
190
LUA_API void lua_insert (lua_State *L, int idx) {
191
StkId p;
192
StkId q;
193
lua_lock(L);
194
p = index2addr(L, idx);
195
api_checkstackindex(L, idx, p);
196
for (q = L->top; q > p; q--) /* use L->top as a temporary */
197
setobjs2s(L, q, q - 1);
198
setobjs2s(L, p, L->top);
199
lua_unlock(L);
200
}
201
202
203
static void moveto (lua_State *L, TValue *fr, int idx) {
204
TValue *to = index2addr(L, idx);
205
api_checkvalidindex(L, to);
206
setobj(L, to, fr);
207
if (idx < LUA_REGISTRYINDEX) /* function upvalue? */
208
luaC_barrier(L, clCvalue(L->ci->func), fr);
209
/* LUA_REGISTRYINDEX does not need gc barrier
210
(collector revisits it before finishing collection) */
211
}
212
213
214
LUA_API void lua_replace (lua_State *L, int idx) {
215
lua_lock(L);
216
api_checknelems(L, 1);
217
moveto(L, L->top - 1, idx);
218
L->top--;
219
lua_unlock(L);
220
}
221
222
223
LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
224
TValue *fr;
225
lua_lock(L);
226
fr = index2addr(L, fromidx);
227
moveto(L, fr, toidx);
228
lua_unlock(L);
229
}
230
231
232
LUA_API void lua_pushvalue (lua_State *L, int idx) {
233
lua_lock(L);
234
setobj2s(L, L->top, index2addr(L, idx));
235
api_incr_top(L);
236
lua_unlock(L);
237
}
238
239
240
241
/*
242
** access functions (stack -> C)
243
*/
244
245
246
LUA_API int lua_type (lua_State *L, int idx) {
247
StkId o = index2addr(L, idx);
248
return (isvalid(o) ? ttypenv(o) : LUA_TNONE);
249
}
250
251
252
LUA_API const char *lua_typename (lua_State *L, int t) {
253
UNUSED(L);
254
if (t > 8 || t < 0)
255
return "internal_type_error";
256
return ttypename(t);
257
}
258
259
260
LUA_API int lua_iscfunction (lua_State *L, int idx) {
261
StkId o = index2addr(L, idx);
262
return (ttislcf(o) || (ttisCclosure(o)));
263
}
264
265
266
LUA_API int lua_isnumber (lua_State *L, int idx) {
267
TValue n;
268
const TValue *o = index2addr(L, idx);
269
return tonumber(o, &n);
270
}
271
272
273
LUA_API int lua_isstring (lua_State *L, int idx) {
274
int t = lua_type(L, idx);
275
return (t == LUA_TSTRING || t == LUA_TNUMBER);
276
}
277
278
279
LUA_API int lua_isuserdata (lua_State *L, int idx) {
280
const TValue *o = index2addr(L, idx);
281
return (ttisuserdata(o) || ttislightuserdata(o));
282
}
283
284
285
LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
286
StkId o1 = index2addr(L, index1);
287
StkId o2 = index2addr(L, index2);
288
return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
289
}
290
291
292
LUA_API void lua_arith (lua_State *L, int op) {
293
StkId o1; /* 1st operand */
294
StkId o2; /* 2nd operand */
295
lua_lock(L);
296
if (op != LUA_OPUNM) /* all other operations expect two operands */
297
api_checknelems(L, 2);
298
else { /* for unary minus, add fake 2nd operand */
299
api_checknelems(L, 1);
300
setobjs2s(L, L->top, L->top - 1);
301
L->top++;
302
}
303
o1 = L->top - 2;
304
o2 = L->top - 1;
305
if (ttisnumber(o1) && ttisnumber(o2)) {
306
setnvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
307
}
308
else
309
luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
310
L->top--;
311
lua_unlock(L);
312
}
313
314
315
LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
316
StkId o1, o2;
317
int i = 0;
318
lua_lock(L); /* may call tag method */
319
o1 = index2addr(L, index1);
320
o2 = index2addr(L, index2);
321
if (isvalid(o1) && isvalid(o2)) {
322
switch (op) {
323
case LUA_OPEQ: i = equalobj(L, o1, o2); break;
324
case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
325
case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
326
default: api_check(L, 0, "invalid option");
327
}
328
}
329
lua_unlock(L);
330
return i;
331
}
332
333
334
LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) {
335
TValue n;
336
const TValue *o = index2addr(L, idx);
337
if (tonumber(o, &n)) {
338
if (isnum) *isnum = 1;
339
return nvalue(o);
340
}
341
else {
342
if (isnum) *isnum = 0;
343
return 0;
344
}
345
}
346
347
348
LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
349
TValue n;
350
const TValue *o = index2addr(L, idx);
351
if (tonumber(o, &n)) {
352
lua_Integer res;
353
lua_Number num = nvalue(o);
354
lua_number2integer(res, num);
355
if (isnum) *isnum = 1;
356
return res;
357
}
358
else {
359
if (isnum) *isnum = 0;
360
return 0;
361
}
362
}
363
364
365
LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) {
366
TValue n;
367
const TValue *o = index2addr(L, idx);
368
if (tonumber(o, &n)) {
369
lua_Unsigned res;
370
lua_Number num = nvalue(o);
371
lua_number2unsigned(res, num);
372
if (isnum) *isnum = 1;
373
return res;
374
}
375
else {
376
if (isnum) *isnum = 0;
377
return 0;
378
}
379
}
380
381
382
LUA_API int lua_toboolean (lua_State *L, int idx) {
383
const TValue *o = index2addr(L, idx);
384
return !l_isfalse(o);
385
}
386
387
388
LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
389
StkId o = index2addr(L, idx);
390
if (!ttisstring(o)) {
391
lua_lock(L); /* `luaV_tostring' may create a new string */
392
if (!luaV_tostring(L, o)) { /* conversion failed? */
393
if (len != NULL) *len = 0;
394
lua_unlock(L);
395
return NULL;
396
}
397
luaC_checkGC(L);
398
o = index2addr(L, idx); /* previous call may reallocate the stack */
399
lua_unlock(L);
400
}
401
if (len != NULL) *len = tsvalue(o)->len;
402
return svalue(o);
403
}
404
405
406
LUA_API size_t lua_rawlen (lua_State *L, int idx) {
407
StkId o = index2addr(L, idx);
408
switch (ttypenv(o)) {
409
case LUA_TSTRING: return tsvalue(o)->len;
410
case LUA_TUSERDATA: return uvalue(o)->len;
411
case LUA_TTABLE: return luaH_getn(hvalue(o));
412
default: return 0;
413
}
414
}
415
416
417
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
418
StkId o = index2addr(L, idx);
419
if (ttislcf(o)) return fvalue(o);
420
else if (ttisCclosure(o))
421
return clCvalue(o)->f;
422
else return NULL; /* not a C function */
423
}
424
425
426
LUA_API void *lua_touserdata (lua_State *L, int idx) {
427
StkId o = index2addr(L, idx);
428
switch (ttypenv(o)) {
429
case LUA_TUSERDATA: return ((void *)(rawuvalue(o) + 1));
430
case LUA_TLIGHTUSERDATA: return pvalue(o);
431
default: return NULL;
432
}
433
}
434
435
436
LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
437
StkId o = index2addr(L, idx);
438
return (!ttisthread(o)) ? NULL : thvalue(o);
439
}
440
441
442
LUA_API const void *lua_topointer (lua_State *L, int idx) {
443
StkId o = index2addr(L, idx);
444
switch (ttype(o)) {
445
case LUA_TTABLE: return hvalue(o);
446
case LUA_TLCL: return clLvalue(o);
447
case LUA_TCCL: return clCvalue(o);
448
case LUA_TLCF: return cast(void *, cast(uintptr_t, fvalue(o)));
449
case LUA_TTHREAD: return thvalue(o);
450
case LUA_TUSERDATA:
451
case LUA_TLIGHTUSERDATA:
452
return lua_touserdata(L, idx);
453
default: return NULL;
454
}
455
}
456
457
458
459
/*
460
** push functions (C -> stack)
461
*/
462
463
464
LUA_API void lua_pushnil (lua_State *L) {
465
lua_lock(L);
466
setnilvalue(L->top);
467
api_incr_top(L);
468
lua_unlock(L);
469
}
470
471
472
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
473
lua_lock(L);
474
setnvalue(L->top, n);
475
luai_checknum(L, L->top,
476
luaG_runerror(L, "C API - attempt to push a signaling NaN"));
477
api_incr_top(L);
478
lua_unlock(L);
479
}
480
481
482
LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
483
lua_lock(L);
484
setnvalue(L->top, cast_num(n));
485
api_incr_top(L);
486
lua_unlock(L);
487
}
488
489
490
LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
491
lua_Number n;
492
lua_lock(L);
493
n = lua_unsigned2number(u);
494
setnvalue(L->top, n);
495
api_incr_top(L);
496
lua_unlock(L);
497
}
498
499
500
LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
501
TString *ts;
502
lua_lock(L);
503
luaC_checkGC(L);
504
ts = luaS_newlstr(L, s, len);
505
setsvalue2s(L, L->top, ts);
506
api_incr_top(L);
507
lua_unlock(L);
508
return getstr(ts);
509
}
510
511
512
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
513
if (s == NULL) {
514
lua_pushnil(L);
515
return NULL;
516
}
517
else {
518
TString *ts;
519
lua_lock(L);
520
luaC_checkGC(L);
521
ts = luaS_new(L, s);
522
setsvalue2s(L, L->top, ts);
523
api_incr_top(L);
524
lua_unlock(L);
525
return getstr(ts);
526
}
527
}
528
529
530
LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
531
va_list argp) {
532
const char *ret;
533
lua_lock(L);
534
luaC_checkGC(L);
535
ret = luaO_pushvfstring(L, fmt, argp);
536
lua_unlock(L);
537
return ret;
538
}
539
540
541
LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
542
const char *ret;
543
va_list argp;
544
lua_lock(L);
545
luaC_checkGC(L);
546
va_start(argp, fmt);
547
ret = luaO_pushvfstring(L, fmt, argp);
548
va_end(argp);
549
lua_unlock(L);
550
return ret;
551
}
552
553
554
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
555
lua_lock(L);
556
if (n == 0) {
557
setfvalue(L->top, fn);
558
}
559
else {
560
Closure *cl;
561
api_checknelems(L, n);
562
api_check(L, n <= MAXUPVAL, "upvalue index too large");
563
luaC_checkGC(L);
564
cl = luaF_newCclosure(L, n);
565
cl->c.f = fn;
566
L->top -= n;
567
while (n--)
568
setobj2n(L, &cl->c.upvalue[n], L->top + n);
569
setclCvalue(L, L->top, cl);
570
}
571
api_incr_top(L);
572
lua_unlock(L);
573
}
574
575
576
LUA_API void lua_pushboolean (lua_State *L, int b) {
577
lua_lock(L);
578
setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
579
api_incr_top(L);
580
lua_unlock(L);
581
}
582
583
584
LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
585
lua_lock(L);
586
setpvalue(L->top, p);
587
api_incr_top(L);
588
lua_unlock(L);
589
}
590
591
592
LUA_API int lua_pushthread (lua_State *L) {
593
lua_lock(L);
594
setthvalue(L, L->top, L);
595
api_incr_top(L);
596
lua_unlock(L);
597
return (G(L)->mainthread == L);
598
}
599
600
601
602
/*
603
** get functions (Lua -> stack)
604
*/
605
606
607
LUA_API void lua_getglobal (lua_State *L, const char *var) {
608
Table *reg = hvalue(&G(L)->l_registry);
609
const TValue *gt; /* global table */
610
lua_lock(L);
611
gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
612
setsvalue2s(L, L->top++, luaS_new(L, var));
613
luaV_gettable(L, gt, L->top - 1, L->top - 1);
614
lua_unlock(L);
615
}
616
617
618
LUA_API void lua_gettable (lua_State *L, int idx) {
619
StkId t;
620
lua_lock(L);
621
t = index2addr(L, idx);
622
luaV_gettable(L, t, L->top - 1, L->top - 1);
623
lua_unlock(L);
624
}
625
626
627
LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
628
StkId t;
629
lua_lock(L);
630
t = index2addr(L, idx);
631
setsvalue2s(L, L->top, luaS_new(L, k));
632
api_incr_top(L);
633
luaV_gettable(L, t, L->top - 1, L->top - 1);
634
lua_unlock(L);
635
}
636
637
638
LUA_API void lua_rawget (lua_State *L, int idx) {
639
StkId t;
640
lua_lock(L);
641
t = index2addr(L, idx);
642
api_check(L, ttistable(t), "table expected");
643
setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
644
lua_unlock(L);
645
}
646
647
648
LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
649
StkId t;
650
lua_lock(L);
651
t = index2addr(L, idx);
652
api_check(L, ttistable(t), "table expected");
653
setobj2s(L, L->top, luaH_getint(hvalue(t), n));
654
api_incr_top(L);
655
lua_unlock(L);
656
}
657
658
659
LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) {
660
StkId t;
661
TValue k;
662
lua_lock(L);
663
t = index2addr(L, idx);
664
api_check(L, ttistable(t), "table expected");
665
setpvalue(&k, cast(void *, p));
666
setobj2s(L, L->top, luaH_get(hvalue(t), &k));
667
api_incr_top(L);
668
lua_unlock(L);
669
}
670
671
672
LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
673
Table *t;
674
lua_lock(L);
675
luaC_checkGC(L);
676
t = luaH_new(L);
677
sethvalue(L, L->top, t);
678
api_incr_top(L);
679
if (narray > 0 || nrec > 0)
680
luaH_resize(L, t, narray, nrec);
681
lua_unlock(L);
682
}
683
684
685
LUA_API int lua_getmetatable (lua_State *L, int objindex) {
686
const TValue *obj;
687
Table *mt = NULL;
688
int res;
689
lua_lock(L);
690
obj = index2addr(L, objindex);
691
switch (ttypenv(obj)) {
692
case LUA_TTABLE:
693
mt = hvalue(obj)->metatable;
694
break;
695
case LUA_TUSERDATA:
696
mt = uvalue(obj)->metatable;
697
break;
698
default:
699
mt = G(L)->mt[ttypenv(obj)];
700
break;
701
}
702
if (mt == NULL)
703
res = 0;
704
else {
705
sethvalue(L, L->top, mt);
706
api_incr_top(L);
707
res = 1;
708
}
709
lua_unlock(L);
710
return res;
711
}
712
713
714
LUA_API void lua_getuservalue (lua_State *L, int idx) {
715
StkId o;
716
lua_lock(L);
717
o = index2addr(L, idx);
718
api_check(L, ttisuserdata(o), "userdata expected");
719
if (uvalue(o)->env) {
720
sethvalue(L, L->top, uvalue(o)->env);
721
} else
722
setnilvalue(L->top);
723
api_incr_top(L);
724
lua_unlock(L);
725
}
726
727
728
/*
729
** set functions (stack -> Lua)
730
*/
731
732
733
LUA_API void lua_setglobal (lua_State *L, const char *var) {
734
Table *reg = hvalue(&G(L)->l_registry);
735
const TValue *gt; /* global table */
736
lua_lock(L);
737
api_checknelems(L, 1);
738
gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
739
setsvalue2s(L, L->top++, luaS_new(L, var));
740
luaV_settable(L, gt, L->top - 1, L->top - 2);
741
L->top -= 2; /* pop value and key */
742
lua_unlock(L);
743
}
744
745
746
LUA_API void lua_settable (lua_State *L, int idx) {
747
StkId t;
748
lua_lock(L);
749
api_checknelems(L, 2);
750
t = index2addr(L, idx);
751
luaV_settable(L, t, L->top - 2, L->top - 1);
752
L->top -= 2; /* pop index and value */
753
lua_unlock(L);
754
}
755
756
757
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
758
StkId t;
759
lua_lock(L);
760
api_checknelems(L, 1);
761
t = index2addr(L, idx);
762
setsvalue2s(L, L->top++, luaS_new(L, k));
763
luaV_settable(L, t, L->top - 1, L->top - 2);
764
L->top -= 2; /* pop value and key */
765
lua_unlock(L);
766
}
767
768
769
LUA_API void lua_rawset (lua_State *L, int idx) {
770
StkId t;
771
lua_lock(L);
772
api_checknelems(L, 2);
773
t = index2addr(L, idx);
774
api_check(L, ttistable(t), "table expected");
775
setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
776
invalidateTMcache(hvalue(t));
777
luaC_barrierback(L, gcvalue(t), L->top-1);
778
L->top -= 2;
779
lua_unlock(L);
780
}
781
782
783
LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
784
StkId t;
785
lua_lock(L);
786
api_checknelems(L, 1);
787
t = index2addr(L, idx);
788
api_check(L, ttistable(t), "table expected");
789
luaH_setint(L, hvalue(t), n, L->top - 1);
790
luaC_barrierback(L, gcvalue(t), L->top-1);
791
L->top--;
792
lua_unlock(L);
793
}
794
795
796
LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
797
StkId t;
798
TValue k;
799
lua_lock(L);
800
api_checknelems(L, 1);
801
t = index2addr(L, idx);
802
api_check(L, ttistable(t), "table expected");
803
setpvalue(&k, cast(void *, p));
804
setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
805
luaC_barrierback(L, gcvalue(t), L->top - 1);
806
L->top--;
807
lua_unlock(L);
808
}
809
810
811
LUA_API int lua_setmetatable (lua_State *L, int objindex) {
812
TValue *obj;
813
Table *mt;
814
lua_lock(L);
815
api_checknelems(L, 1);
816
obj = index2addr(L, objindex);
817
if (ttisnil(L->top - 1))
818
mt = NULL;
819
else {
820
api_check(L, ttistable(L->top - 1), "table expected");
821
mt = hvalue(L->top - 1);
822
}
823
switch (ttypenv(obj)) {
824
case LUA_TTABLE: {
825
hvalue(obj)->metatable = mt;
826
if (mt) {
827
luaC_objbarrierback(L, gcvalue(obj), mt);
828
luaC_checkfinalizer(L, gcvalue(obj), mt);
829
}
830
break;
831
}
832
case LUA_TUSERDATA: {
833
uvalue(obj)->metatable = mt;
834
if (mt) {
835
luaC_objbarrier(L, rawuvalue(obj), mt);
836
luaC_checkfinalizer(L, gcvalue(obj), mt);
837
}
838
break;
839
}
840
default: {
841
G(L)->mt[ttypenv(obj)] = mt;
842
break;
843
}
844
}
845
L->top--;
846
lua_unlock(L);
847
return 1;
848
}
849
850
851
LUA_API void lua_setuservalue (lua_State *L, int idx) {
852
StkId o;
853
lua_lock(L);
854
api_checknelems(L, 1);
855
o = index2addr(L, idx);
856
api_check(L, ttisuserdata(o), "userdata expected");
857
if (ttisnil(L->top - 1))
858
uvalue(o)->env = NULL;
859
else {
860
api_check(L, ttistable(L->top - 1), "table expected");
861
uvalue(o)->env = hvalue(L->top - 1);
862
luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
863
}
864
L->top--;
865
lua_unlock(L);
866
}
867
868
869
/*
870
** `load' and `call' functions (run Lua code)
871
*/
872
873
874
#define checkresults(L,na,nr) \
875
api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
876
"results from function overflow current stack size")
877
878
879
LUA_API int lua_getctx (lua_State *L, int *ctx) {
880
if (L->ci->callstatus & CIST_YIELDED) {
881
if (ctx) *ctx = L->ci->u.c.ctx;
882
return L->ci->u.c.status;
883
}
884
else return LUA_OK;
885
}
886
887
888
LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
889
lua_CFunction k) {
890
StkId func;
891
lua_lock(L);
892
api_check(L, k == NULL || !isLua(L->ci),
893
"cannot use continuations inside hooks");
894
api_checknelems(L, nargs+1);
895
api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
896
checkresults(L, nargs, nresults);
897
func = L->top - (nargs+1);
898
if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
899
L->ci->u.c.k = k; /* save continuation */
900
L->ci->u.c.ctx = ctx; /* save context */
901
luaD_call(L, func, nresults, 1); /* do the call */
902
}
903
else /* no continuation or no yieldable */
904
luaD_call(L, func, nresults, 0); /* just do the call */
905
adjustresults(L, nresults);
906
lua_unlock(L);
907
}
908
909
910
911
/*
912
** Execute a protected call.
913
*/
914
struct CallS { /* data to `f_call' */
915
StkId func;
916
int nresults;
917
};
918
919
920
static void f_call (lua_State *L, void *ud) {
921
struct CallS *c = cast(struct CallS *, ud);
922
luaD_call(L, c->func, c->nresults, 0);
923
}
924
925
926
927
LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
928
int ctx, lua_CFunction k) {
929
struct CallS c;
930
int status;
931
ptrdiff_t func;
932
lua_lock(L);
933
api_check(L, k == NULL || !isLua(L->ci),
934
"cannot use continuations inside hooks");
935
api_checknelems(L, nargs+1);
936
api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
937
checkresults(L, nargs, nresults);
938
if (errfunc == 0)
939
func = 0;
940
else {
941
StkId o = index2addr(L, errfunc);
942
api_checkstackindex(L, errfunc, o);
943
func = savestack(L, o);
944
}
945
c.func = L->top - (nargs+1); /* function to be called */
946
if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
947
c.nresults = nresults; /* do a 'conventional' protected call */
948
status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
949
}
950
else { /* prepare continuation (call is already protected by 'resume') */
951
CallInfo *ci = L->ci;
952
ci->u.c.k = k; /* save continuation */
953
ci->u.c.ctx = ctx; /* save context */
954
/* save information for error recovery */
955
ci->extra = savestack(L, c.func);
956
ci->u.c.old_allowhook = L->allowhook;
957
ci->u.c.old_errfunc = L->errfunc;
958
L->errfunc = func;
959
/* mark that function may do error recovery */
960
ci->callstatus |= CIST_YPCALL;
961
luaD_call(L, c.func, nresults, 1); /* do the call */
962
ci->callstatus &= ~CIST_YPCALL;
963
L->errfunc = ci->u.c.old_errfunc;
964
status = LUA_OK; /* if it is here, there were no errors */
965
}
966
adjustresults(L, nresults);
967
lua_unlock(L);
968
return status;
969
}
970
971
972
LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
973
const char *chunkname, const char *mode) {
974
ZIO z;
975
int status;
976
lua_lock(L);
977
if (!chunkname) chunkname = "?";
978
luaZ_init(L, &z, reader, data);
979
status = luaD_protectedparser(L, &z, chunkname, mode);
980
if (status == LUA_OK) { /* no errors? */
981
LClosure *f = clLvalue(L->top - 1); /* get newly created function */
982
if (f->nupvalues == 1) { /* does it have one upvalue? */
983
/* get global table from registry */
984
Table *reg = hvalue(&G(L)->l_registry);
985
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
986
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
987
setobj(L, f->upvals[0]->v, gt);
988
luaC_barrier(L, f->upvals[0], gt);
989
}
990
}
991
lua_unlock(L);
992
return status;
993
}
994
995
#if defined(LUA_USE_DUMP)
996
LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
997
int status;
998
TValue *o;
999
lua_lock(L);
1000
api_checknelems(L, 1);
1001
o = L->top - 1;
1002
if (isLfunction(o))
1003
status = luaU_dump(L, getproto(o), writer, data, 0);
1004
else
1005
status = 1;
1006
lua_unlock(L);
1007
return status;
1008
}
1009
#endif
1010
1011
LUA_API int lua_status (lua_State *L) {
1012
return L->status;
1013
}
1014
1015
1016
/*
1017
** Garbage-collection function
1018
*/
1019
1020
LUA_API int lua_gc (lua_State *L, int what, int data) {
1021
int res = 0;
1022
global_State *g;
1023
lua_lock(L);
1024
g = G(L);
1025
switch (what) {
1026
case LUA_GCSTOP: {
1027
g->gcrunning = 0;
1028
break;
1029
}
1030
case LUA_GCRESTART: {
1031
luaE_setdebt(g, 0);
1032
g->gcrunning = 1;
1033
break;
1034
}
1035
case LUA_GCCOLLECT: {
1036
luaC_fullgc(L, 0);
1037
break;
1038
}
1039
case LUA_GCCOUNT: {
1040
/* GC values are expressed in Kbytes: #bytes/2^10 */
1041
res = cast_int(gettotalbytes(g) >> 10);
1042
break;
1043
}
1044
case LUA_GCCOUNTB: {
1045
res = cast_int(gettotalbytes(g) & 0x3ff);
1046
break;
1047
}
1048
case LUA_GCSTEP: {
1049
if (g->gckind == KGC_GEN) { /* generational mode? */
1050
res = (g->GCestimate == 0); /* true if it will do major collection */
1051
luaC_forcestep(L); /* do a single step */
1052
}
1053
else {
1054
lu_mem debt = cast(lu_mem, data) * 1024 - GCSTEPSIZE;
1055
if (g->gcrunning)
1056
debt += g->GCdebt; /* include current debt */
1057
luaE_setdebt(g, debt);
1058
luaC_forcestep(L);
1059
if (g->gcstate == GCSpause) /* end of cycle? */
1060
res = 1; /* signal it */
1061
}
1062
break;
1063
}
1064
case LUA_GCSETPAUSE: {
1065
res = g->gcpause;
1066
g->gcpause = data;
1067
break;
1068
}
1069
case LUA_GCSETMAJORINC: {
1070
res = g->gcmajorinc;
1071
g->gcmajorinc = data;
1072
break;
1073
}
1074
case LUA_GCSETSTEPMUL: {
1075
res = g->gcstepmul;
1076
g->gcstepmul = data;
1077
break;
1078
}
1079
case LUA_GCISRUNNING: {
1080
res = g->gcrunning;
1081
break;
1082
}
1083
case LUA_GCGEN: { /* change collector to generational mode */
1084
luaC_changemode(L, KGC_GEN);
1085
break;
1086
}
1087
case LUA_GCINC: { /* change collector to incremental mode */
1088
luaC_changemode(L, KGC_NORMAL);
1089
break;
1090
}
1091
default: res = -1; /* invalid option */
1092
}
1093
lua_unlock(L);
1094
return res;
1095
}
1096
1097
1098
1099
/*
1100
** miscellaneous functions
1101
*/
1102
1103
1104
LUA_API int lua_error (lua_State *L) {
1105
lua_lock(L);
1106
api_checknelems(L, 1);
1107
luaG_errormsg(L);
1108
/* code unreachable; will unlock when control actually leaves the kernel */
1109
return 0; /* to avoid warnings */
1110
}
1111
1112
1113
LUA_API int lua_next (lua_State *L, int idx) {
1114
StkId t;
1115
int more;
1116
lua_lock(L);
1117
t = index2addr(L, idx);
1118
api_check(L, ttistable(t), "table expected");
1119
more = luaH_next(L, hvalue(t), L->top - 1);
1120
if (more) {
1121
api_incr_top(L);
1122
}
1123
else /* no more elements */
1124
L->top -= 1; /* remove key */
1125
lua_unlock(L);
1126
return more;
1127
}
1128
1129
1130
LUA_API void lua_concat (lua_State *L, int n) {
1131
lua_lock(L);
1132
api_checknelems(L, n);
1133
if (n >= 2) {
1134
luaC_checkGC(L);
1135
luaV_concat(L, n);
1136
}
1137
else if (n == 0) { /* push empty string */
1138
setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
1139
api_incr_top(L);
1140
}
1141
/* else n == 1; nothing to do */
1142
lua_unlock(L);
1143
}
1144
1145
1146
LUA_API void lua_len (lua_State *L, int idx) {
1147
StkId t;
1148
lua_lock(L);
1149
t = index2addr(L, idx);
1150
luaV_objlen(L, L->top, t);
1151
api_incr_top(L);
1152
lua_unlock(L);
1153
}
1154
1155
1156
LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
1157
lua_Alloc f;
1158
lua_lock(L);
1159
if (ud) *ud = G(L)->ud;
1160
f = G(L)->frealloc;
1161
lua_unlock(L);
1162
return f;
1163
}
1164
1165
1166
LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
1167
lua_lock(L);
1168
G(L)->ud = ud;
1169
G(L)->frealloc = f;
1170
lua_unlock(L);
1171
}
1172
1173
1174
LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
1175
Udata *u;
1176
lua_lock(L);
1177
luaC_checkGC(L);
1178
u = luaS_newudata(L, size, NULL);
1179
setuvalue(L, L->top, u);
1180
api_incr_top(L);
1181
lua_unlock(L);
1182
return u + 1;
1183
}
1184
1185
1186
1187
static const char *aux_upvalue (StkId fi, int n, TValue **val,
1188
GCObject **owner) {
1189
switch (ttype(fi)) {
1190
case LUA_TCCL: { /* C closure */
1191
CClosure *f = clCvalue(fi);
1192
if (!(1 <= n && n <= f->nupvalues)) return NULL;
1193
*val = &f->upvalue[n-1];
1194
if (owner) *owner = obj2gco(f);
1195
return "";
1196
}
1197
case LUA_TLCL: { /* Lua closure */
1198
LClosure *f = clLvalue(fi);
1199
TString *name;
1200
Proto *p = f->p;
1201
if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1202
*val = f->upvals[n-1]->v;
1203
if (owner) *owner = obj2gco(f->upvals[n - 1]);
1204
name = p->upvalues[n-1].name;
1205
return (name == NULL) ? "" : getstr(name);
1206
}
1207
default: return NULL; /* not a closure */
1208
}
1209
}
1210
1211
1212
LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1213
const char *name;
1214
TValue *val = NULL; /* to avoid warnings */
1215
lua_lock(L);
1216
name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL);
1217
if (name) {
1218
setobj2s(L, L->top, val);
1219
api_incr_top(L);
1220
}
1221
lua_unlock(L);
1222
return name;
1223
}
1224
1225
1226
LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
1227
const char *name;
1228
TValue *val = NULL; /* to avoid warnings */
1229
GCObject *owner = NULL; /* to avoid warnings */
1230
StkId fi;
1231
lua_lock(L);
1232
fi = index2addr(L, funcindex);
1233
api_checknelems(L, 1);
1234
name = aux_upvalue(fi, n, &val, &owner);
1235
if (name) {
1236
L->top--;
1237
setobj(L, val, L->top);
1238
luaC_barrier(L, owner, L->top);
1239
}
1240
lua_unlock(L);
1241
return name;
1242
}
1243
1244
1245
static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
1246
LClosure *f;
1247
StkId fi = index2addr(L, fidx);
1248
api_check(L, ttisLclosure(fi), "Lua function expected");
1249
f = clLvalue(fi);
1250
api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
1251
if (pf) *pf = f;
1252
return &f->upvals[n - 1]; /* get its upvalue pointer */
1253
}
1254
1255
1256
LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
1257
StkId fi = index2addr(L, fidx);
1258
switch (ttype(fi)) {
1259
case LUA_TLCL: { /* lua closure */
1260
return *getupvalref(L, fidx, n, NULL);
1261
}
1262
case LUA_TCCL: { /* C closure */
1263
CClosure *f = clCvalue(fi);
1264
api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
1265
return &f->upvalue[n - 1];
1266
}
1267
default: {
1268
api_check(L, 0, "closure expected");
1269
return NULL;
1270
}
1271
}
1272
}
1273
1274
1275
LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
1276
int fidx2, int n2) {
1277
LClosure *f1;
1278
UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
1279
UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
1280
*up1 = *up2;
1281
luaC_objbarrier(L, f1, *up2);
1282
}
1283
1284
EXPORT_SYMBOL(lua_absindex);
1285
EXPORT_SYMBOL(lua_atpanic);
1286
EXPORT_SYMBOL(lua_checkstack);
1287
EXPORT_SYMBOL(lua_close);
1288
EXPORT_SYMBOL(lua_createtable);
1289
EXPORT_SYMBOL(lua_error);
1290
EXPORT_SYMBOL(lua_getfield);
1291
EXPORT_SYMBOL(lua_gettable);
1292
EXPORT_SYMBOL(lua_gettop);
1293
EXPORT_SYMBOL(lua_isnumber);
1294
EXPORT_SYMBOL(lua_isstring);
1295
EXPORT_SYMBOL(lua_newstate);
1296
EXPORT_SYMBOL(lua_newuserdata);
1297
EXPORT_SYMBOL(lua_next);
1298
EXPORT_SYMBOL(lua_pcallk);
1299
EXPORT_SYMBOL(lua_pushboolean);
1300
EXPORT_SYMBOL(lua_pushcclosure);
1301
EXPORT_SYMBOL(lua_pushfstring);
1302
EXPORT_SYMBOL(lua_pushinteger);
1303
EXPORT_SYMBOL(lua_pushlightuserdata);
1304
EXPORT_SYMBOL(lua_pushnil);
1305
EXPORT_SYMBOL(lua_pushnumber);
1306
EXPORT_SYMBOL(lua_pushstring);
1307
EXPORT_SYMBOL(lua_pushvalue);
1308
EXPORT_SYMBOL(lua_pushvfstring);
1309
EXPORT_SYMBOL(lua_remove);
1310
EXPORT_SYMBOL(lua_replace);
1311
EXPORT_SYMBOL(lua_setfield);
1312
EXPORT_SYMBOL(lua_setglobal);
1313
EXPORT_SYMBOL(lua_sethook);
1314
EXPORT_SYMBOL(lua_setmetatable);
1315
EXPORT_SYMBOL(lua_settable);
1316
EXPORT_SYMBOL(lua_settop);
1317
EXPORT_SYMBOL(lua_toboolean);
1318
EXPORT_SYMBOL(lua_tointegerx);
1319
EXPORT_SYMBOL(lua_tolstring);
1320
EXPORT_SYMBOL(lua_tonumberx);
1321
EXPORT_SYMBOL(lua_touserdata);
1322
EXPORT_SYMBOL(lua_type);
1323
EXPORT_SYMBOL(lua_typename);
1324
1325