]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - module/lua/lapi.c
Vendor import of openzfs master @ 184df27eef0abdc7ab2105b21257f753834b936b
[FreeBSD/FreeBSD.git] / module / lua / lapi.c
1 /* BEGIN CSTYLED */
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   return ttypename(t);
255 }
256
257
258 LUA_API int lua_iscfunction (lua_State *L, int idx) {
259   StkId o = index2addr(L, idx);
260   return (ttislcf(o) || (ttisCclosure(o)));
261 }
262
263
264 LUA_API int lua_isnumber (lua_State *L, int idx) {
265   TValue n;
266   const TValue *o = index2addr(L, idx);
267   return tonumber(o, &n);
268 }
269
270
271 LUA_API int lua_isstring (lua_State *L, int idx) {
272   int t = lua_type(L, idx);
273   return (t == LUA_TSTRING || t == LUA_TNUMBER);
274 }
275
276
277 LUA_API int lua_isuserdata (lua_State *L, int idx) {
278   const TValue *o = index2addr(L, idx);
279   return (ttisuserdata(o) || ttislightuserdata(o));
280 }
281
282
283 LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
284   StkId o1 = index2addr(L, index1);
285   StkId o2 = index2addr(L, index2);
286   return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
287 }
288
289
290 LUA_API void lua_arith (lua_State *L, int op) {
291   StkId o1;  /* 1st operand */
292   StkId o2;  /* 2nd operand */
293   lua_lock(L);
294   if (op != LUA_OPUNM) /* all other operations expect two operands */
295     api_checknelems(L, 2);
296   else {  /* for unary minus, add fake 2nd operand */
297     api_checknelems(L, 1);
298     setobjs2s(L, L->top, L->top - 1);
299     L->top++;
300   }
301   o1 = L->top - 2;
302   o2 = L->top - 1;
303   if (ttisnumber(o1) && ttisnumber(o2)) {
304     setnvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
305   }
306   else
307     luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
308   L->top--;
309   lua_unlock(L);
310 }
311
312
313 LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
314   StkId o1, o2;
315   int i = 0;
316   lua_lock(L);  /* may call tag method */
317   o1 = index2addr(L, index1);
318   o2 = index2addr(L, index2);
319   if (isvalid(o1) && isvalid(o2)) {
320     switch (op) {
321       case LUA_OPEQ: i = equalobj(L, o1, o2); break;
322       case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
323       case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
324       default: api_check(L, 0, "invalid option");
325     }
326   }
327   lua_unlock(L);
328   return i;
329 }
330
331
332 LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) {
333   TValue n;
334   const TValue *o = index2addr(L, idx);
335   if (tonumber(o, &n)) {
336     if (isnum) *isnum = 1;
337     return nvalue(o);
338   }
339   else {
340     if (isnum) *isnum = 0;
341     return 0;
342   }
343 }
344
345
346 LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
347   TValue n;
348   const TValue *o = index2addr(L, idx);
349   if (tonumber(o, &n)) {
350     lua_Integer res;
351     lua_Number num = nvalue(o);
352     lua_number2integer(res, num);
353     if (isnum) *isnum = 1;
354     return res;
355   }
356   else {
357     if (isnum) *isnum = 0;
358     return 0;
359   }
360 }
361
362
363 LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) {
364   TValue n;
365   const TValue *o = index2addr(L, idx);
366   if (tonumber(o, &n)) {
367     lua_Unsigned res;
368     lua_Number num = nvalue(o);
369     lua_number2unsigned(res, num);
370     if (isnum) *isnum = 1;
371     return res;
372   }
373   else {
374     if (isnum) *isnum = 0;
375     return 0;
376   }
377 }
378
379
380 LUA_API int lua_toboolean (lua_State *L, int idx) {
381   const TValue *o = index2addr(L, idx);
382   return !l_isfalse(o);
383 }
384
385
386 LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
387   StkId o = index2addr(L, idx);
388   if (!ttisstring(o)) {
389     lua_lock(L);  /* `luaV_tostring' may create a new string */
390     if (!luaV_tostring(L, o)) {  /* conversion failed? */
391       if (len != NULL) *len = 0;
392       lua_unlock(L);
393       return NULL;
394     }
395     luaC_checkGC(L);
396     o = index2addr(L, idx);  /* previous call may reallocate the stack */
397     lua_unlock(L);
398   }
399   if (len != NULL) *len = tsvalue(o)->len;
400   return svalue(o);
401 }
402
403
404 LUA_API size_t lua_rawlen (lua_State *L, int idx) {
405   StkId o = index2addr(L, idx);
406   switch (ttypenv(o)) {
407     case LUA_TSTRING: return tsvalue(o)->len;
408     case LUA_TUSERDATA: return uvalue(o)->len;
409     case LUA_TTABLE: return luaH_getn(hvalue(o));
410     default: return 0;
411   }
412 }
413
414
415 LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
416   StkId o = index2addr(L, idx);
417   if (ttislcf(o)) return fvalue(o);
418   else if (ttisCclosure(o))
419     return clCvalue(o)->f;
420   else return NULL;  /* not a C function */
421 }
422
423
424 LUA_API void *lua_touserdata (lua_State *L, int idx) {
425   StkId o = index2addr(L, idx);
426   switch (ttypenv(o)) {
427     case LUA_TUSERDATA: return ((void *)(rawuvalue(o) + 1));
428     case LUA_TLIGHTUSERDATA: return pvalue(o);
429     default: return NULL;
430   }
431 }
432
433
434 LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
435   StkId o = index2addr(L, idx);
436   return (!ttisthread(o)) ? NULL : thvalue(o);
437 }
438
439
440 LUA_API const void *lua_topointer (lua_State *L, int idx) {
441   StkId o = index2addr(L, idx);
442   switch (ttype(o)) {
443     case LUA_TTABLE: return hvalue(o);
444     case LUA_TLCL: return clLvalue(o);
445     case LUA_TCCL: return clCvalue(o);
446     case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
447     case LUA_TTHREAD: return thvalue(o);
448     case LUA_TUSERDATA:
449     case LUA_TLIGHTUSERDATA:
450       return lua_touserdata(L, idx);
451     default: return NULL;
452   }
453 }
454
455
456
457 /*
458 ** push functions (C -> stack)
459 */
460
461
462 LUA_API void lua_pushnil (lua_State *L) {
463   lua_lock(L);
464   setnilvalue(L->top);
465   api_incr_top(L);
466   lua_unlock(L);
467 }
468
469
470 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
471   lua_lock(L);
472   setnvalue(L->top, n);
473   luai_checknum(L, L->top,
474     luaG_runerror(L, "C API - attempt to push a signaling NaN"));
475   api_incr_top(L);
476   lua_unlock(L);
477 }
478
479
480 LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
481   lua_lock(L);
482   setnvalue(L->top, cast_num(n));
483   api_incr_top(L);
484   lua_unlock(L);
485 }
486
487
488 LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
489   lua_Number n;
490   lua_lock(L);
491   n = lua_unsigned2number(u);
492   setnvalue(L->top, n);
493   api_incr_top(L);
494   lua_unlock(L);
495 }
496
497
498 LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
499   TString *ts;
500   lua_lock(L);
501   luaC_checkGC(L);
502   ts = luaS_newlstr(L, s, len);
503   setsvalue2s(L, L->top, ts);
504   api_incr_top(L);
505   lua_unlock(L);
506   return getstr(ts);
507 }
508
509
510 LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
511   if (s == NULL) {
512     lua_pushnil(L);
513     return NULL;
514   }
515   else {
516     TString *ts;
517     lua_lock(L);
518     luaC_checkGC(L);
519     ts = luaS_new(L, s);
520     setsvalue2s(L, L->top, ts);
521     api_incr_top(L);
522     lua_unlock(L);
523     return getstr(ts);
524   }
525 }
526
527
528 LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
529                                       va_list argp) {
530   const char *ret;
531   lua_lock(L);
532   luaC_checkGC(L);
533   ret = luaO_pushvfstring(L, fmt, argp);
534   lua_unlock(L);
535   return ret;
536 }
537
538
539 LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
540   const char *ret;
541   va_list argp;
542   lua_lock(L);
543   luaC_checkGC(L);
544   va_start(argp, fmt);
545   ret = luaO_pushvfstring(L, fmt, argp);
546   va_end(argp);
547   lua_unlock(L);
548   return ret;
549 }
550
551
552 LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
553   lua_lock(L);
554   if (n == 0) {
555     setfvalue(L->top, fn);
556   }
557   else {
558     Closure *cl;
559     api_checknelems(L, n);
560     api_check(L, n <= MAXUPVAL, "upvalue index too large");
561     luaC_checkGC(L);
562     cl = luaF_newCclosure(L, n);
563     cl->c.f = fn;
564     L->top -= n;
565     while (n--)
566       setobj2n(L, &cl->c.upvalue[n], L->top + n);
567     setclCvalue(L, L->top, cl);
568   }
569   api_incr_top(L);
570   lua_unlock(L);
571 }
572
573
574 LUA_API void lua_pushboolean (lua_State *L, int b) {
575   lua_lock(L);
576   setbvalue(L->top, (b != 0));  /* ensure that true is 1 */
577   api_incr_top(L);
578   lua_unlock(L);
579 }
580
581
582 LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
583   lua_lock(L);
584   setpvalue(L->top, p);
585   api_incr_top(L);
586   lua_unlock(L);
587 }
588
589
590 LUA_API int lua_pushthread (lua_State *L) {
591   lua_lock(L);
592   setthvalue(L, L->top, L);
593   api_incr_top(L);
594   lua_unlock(L);
595   return (G(L)->mainthread == L);
596 }
597
598
599
600 /*
601 ** get functions (Lua -> stack)
602 */
603
604
605 LUA_API void lua_getglobal (lua_State *L, const char *var) {
606   Table *reg = hvalue(&G(L)->l_registry);
607   const TValue *gt;  /* global table */
608   lua_lock(L);
609   gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
610   setsvalue2s(L, L->top++, luaS_new(L, var));
611   luaV_gettable(L, gt, L->top - 1, L->top - 1);
612   lua_unlock(L);
613 }
614
615
616 LUA_API void lua_gettable (lua_State *L, int idx) {
617   StkId t;
618   lua_lock(L);
619   t = index2addr(L, idx);
620   luaV_gettable(L, t, L->top - 1, L->top - 1);
621   lua_unlock(L);
622 }
623
624
625 LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
626   StkId t;
627   lua_lock(L);
628   t = index2addr(L, idx);
629   setsvalue2s(L, L->top, luaS_new(L, k));
630   api_incr_top(L);
631   luaV_gettable(L, t, L->top - 1, L->top - 1);
632   lua_unlock(L);
633 }
634
635
636 LUA_API void lua_rawget (lua_State *L, int idx) {
637   StkId t;
638   lua_lock(L);
639   t = index2addr(L, idx);
640   api_check(L, ttistable(t), "table expected");
641   setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
642   lua_unlock(L);
643 }
644
645
646 LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
647   StkId t;
648   lua_lock(L);
649   t = index2addr(L, idx);
650   api_check(L, ttistable(t), "table expected");
651   setobj2s(L, L->top, luaH_getint(hvalue(t), n));
652   api_incr_top(L);
653   lua_unlock(L);
654 }
655
656
657 LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) {
658   StkId t;
659   TValue k;
660   lua_lock(L);
661   t = index2addr(L, idx);
662   api_check(L, ttistable(t), "table expected");
663   setpvalue(&k, cast(void *, p));
664   setobj2s(L, L->top, luaH_get(hvalue(t), &k));
665   api_incr_top(L);
666   lua_unlock(L);
667 }
668
669
670 LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
671   Table *t;
672   lua_lock(L);
673   luaC_checkGC(L);
674   t = luaH_new(L);
675   sethvalue(L, L->top, t);
676   api_incr_top(L);
677   if (narray > 0 || nrec > 0)
678     luaH_resize(L, t, narray, nrec);
679   lua_unlock(L);
680 }
681
682
683 LUA_API int lua_getmetatable (lua_State *L, int objindex) {
684   const TValue *obj;
685   Table *mt = NULL;
686   int res;
687   lua_lock(L);
688   obj = index2addr(L, objindex);
689   switch (ttypenv(obj)) {
690     case LUA_TTABLE:
691       mt = hvalue(obj)->metatable;
692       break;
693     case LUA_TUSERDATA:
694       mt = uvalue(obj)->metatable;
695       break;
696     default:
697       mt = G(L)->mt[ttypenv(obj)];
698       break;
699   }
700   if (mt == NULL)
701     res = 0;
702   else {
703     sethvalue(L, L->top, mt);
704     api_incr_top(L);
705     res = 1;
706   }
707   lua_unlock(L);
708   return res;
709 }
710
711
712 LUA_API void lua_getuservalue (lua_State *L, int idx) {
713   StkId o;
714   lua_lock(L);
715   o = index2addr(L, idx);
716   api_check(L, ttisuserdata(o), "userdata expected");
717   if (uvalue(o)->env) {
718     sethvalue(L, L->top, uvalue(o)->env);
719   } else
720     setnilvalue(L->top);
721   api_incr_top(L);
722   lua_unlock(L);
723 }
724
725
726 /*
727 ** set functions (stack -> Lua)
728 */
729
730
731 LUA_API void lua_setglobal (lua_State *L, const char *var) {
732   Table *reg = hvalue(&G(L)->l_registry);
733   const TValue *gt;  /* global table */
734   lua_lock(L);
735   api_checknelems(L, 1);
736   gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
737   setsvalue2s(L, L->top++, luaS_new(L, var));
738   luaV_settable(L, gt, L->top - 1, L->top - 2);
739   L->top -= 2;  /* pop value and key */
740   lua_unlock(L);
741 }
742
743
744 LUA_API void lua_settable (lua_State *L, int idx) {
745   StkId t;
746   lua_lock(L);
747   api_checknelems(L, 2);
748   t = index2addr(L, idx);
749   luaV_settable(L, t, L->top - 2, L->top - 1);
750   L->top -= 2;  /* pop index and value */
751   lua_unlock(L);
752 }
753
754
755 LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
756   StkId t;
757   lua_lock(L);
758   api_checknelems(L, 1);
759   t = index2addr(L, idx);
760   setsvalue2s(L, L->top++, luaS_new(L, k));
761   luaV_settable(L, t, L->top - 1, L->top - 2);
762   L->top -= 2;  /* pop value and key */
763   lua_unlock(L);
764 }
765
766
767 LUA_API void lua_rawset (lua_State *L, int idx) {
768   StkId t;
769   lua_lock(L);
770   api_checknelems(L, 2);
771   t = index2addr(L, idx);
772   api_check(L, ttistable(t), "table expected");
773   setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
774   invalidateTMcache(hvalue(t));
775   luaC_barrierback(L, gcvalue(t), L->top-1);
776   L->top -= 2;
777   lua_unlock(L);
778 }
779
780
781 LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
782   StkId t;
783   lua_lock(L);
784   api_checknelems(L, 1);
785   t = index2addr(L, idx);
786   api_check(L, ttistable(t), "table expected");
787   luaH_setint(L, hvalue(t), n, L->top - 1);
788   luaC_barrierback(L, gcvalue(t), L->top-1);
789   L->top--;
790   lua_unlock(L);
791 }
792
793
794 LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
795   StkId t;
796   TValue k;
797   lua_lock(L);
798   api_checknelems(L, 1);
799   t = index2addr(L, idx);
800   api_check(L, ttistable(t), "table expected");
801   setpvalue(&k, cast(void *, p));
802   setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
803   luaC_barrierback(L, gcvalue(t), L->top - 1);
804   L->top--;
805   lua_unlock(L);
806 }
807
808
809 LUA_API int lua_setmetatable (lua_State *L, int objindex) {
810   TValue *obj;
811   Table *mt;
812   lua_lock(L);
813   api_checknelems(L, 1);
814   obj = index2addr(L, objindex);
815   if (ttisnil(L->top - 1))
816     mt = NULL;
817   else {
818     api_check(L, ttistable(L->top - 1), "table expected");
819     mt = hvalue(L->top - 1);
820   }
821   switch (ttypenv(obj)) {
822     case LUA_TTABLE: {
823       hvalue(obj)->metatable = mt;
824       if (mt) {
825         luaC_objbarrierback(L, gcvalue(obj), mt);
826         luaC_checkfinalizer(L, gcvalue(obj), mt);
827       }
828       break;
829     }
830     case LUA_TUSERDATA: {
831       uvalue(obj)->metatable = mt;
832       if (mt) {
833         luaC_objbarrier(L, rawuvalue(obj), mt);
834         luaC_checkfinalizer(L, gcvalue(obj), mt);
835       }
836       break;
837     }
838     default: {
839       G(L)->mt[ttypenv(obj)] = mt;
840       break;
841     }
842   }
843   L->top--;
844   lua_unlock(L);
845   return 1;
846 }
847
848
849 LUA_API void lua_setuservalue (lua_State *L, int idx) {
850   StkId o;
851   lua_lock(L);
852   api_checknelems(L, 1);
853   o = index2addr(L, idx);
854   api_check(L, ttisuserdata(o), "userdata expected");
855   if (ttisnil(L->top - 1))
856     uvalue(o)->env = NULL;
857   else {
858     api_check(L, ttistable(L->top - 1), "table expected");
859     uvalue(o)->env = hvalue(L->top - 1);
860     luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
861   }
862   L->top--;
863   lua_unlock(L);
864 }
865
866
867 /*
868 ** `load' and `call' functions (run Lua code)
869 */
870
871
872 #define checkresults(L,na,nr) \
873      api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
874         "results from function overflow current stack size")
875
876
877 LUA_API int lua_getctx (lua_State *L, int *ctx) {
878   if (L->ci->callstatus & CIST_YIELDED) {
879     if (ctx) *ctx = L->ci->u.c.ctx;
880     return L->ci->u.c.status;
881   }
882   else return LUA_OK;
883 }
884
885
886 LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
887                         lua_CFunction k) {
888   StkId func;
889   lua_lock(L);
890   api_check(L, k == NULL || !isLua(L->ci),
891     "cannot use continuations inside hooks");
892   api_checknelems(L, nargs+1);
893   api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
894   checkresults(L, nargs, nresults);
895   func = L->top - (nargs+1);
896   if (k != NULL && L->nny == 0) {  /* need to prepare continuation? */
897     L->ci->u.c.k = k;  /* save continuation */
898     L->ci->u.c.ctx = ctx;  /* save context */
899     luaD_call(L, func, nresults, 1);  /* do the call */
900   }
901   else  /* no continuation or no yieldable */
902     luaD_call(L, func, nresults, 0);  /* just do the call */
903   adjustresults(L, nresults);
904   lua_unlock(L);
905 }
906
907
908
909 /*
910 ** Execute a protected call.
911 */
912 struct CallS {  /* data to `f_call' */
913   StkId func;
914   int nresults;
915 };
916
917
918 static void f_call (lua_State *L, void *ud) {
919   struct CallS *c = cast(struct CallS *, ud);
920   luaD_call(L, c->func, c->nresults, 0);
921 }
922
923
924
925 LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
926                         int ctx, lua_CFunction k) {
927   struct CallS c;
928   int status;
929   ptrdiff_t func;
930   lua_lock(L);
931   api_check(L, k == NULL || !isLua(L->ci),
932     "cannot use continuations inside hooks");
933   api_checknelems(L, nargs+1);
934   api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
935   checkresults(L, nargs, nresults);
936   if (errfunc == 0)
937     func = 0;
938   else {
939     StkId o = index2addr(L, errfunc);
940     api_checkstackindex(L, errfunc, o);
941     func = savestack(L, o);
942   }
943   c.func = L->top - (nargs+1);  /* function to be called */
944   if (k == NULL || L->nny > 0) {  /* no continuation or no yieldable? */
945     c.nresults = nresults;  /* do a 'conventional' protected call */
946     status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
947   }
948   else {  /* prepare continuation (call is already protected by 'resume') */
949     CallInfo *ci = L->ci;
950     ci->u.c.k = k;  /* save continuation */
951     ci->u.c.ctx = ctx;  /* save context */
952     /* save information for error recovery */
953     ci->extra = savestack(L, c.func);
954     ci->u.c.old_allowhook = L->allowhook;
955     ci->u.c.old_errfunc = L->errfunc;
956     L->errfunc = func;
957     /* mark that function may do error recovery */
958     ci->callstatus |= CIST_YPCALL;
959     luaD_call(L, c.func, nresults, 1);  /* do the call */
960     ci->callstatus &= ~CIST_YPCALL;
961     L->errfunc = ci->u.c.old_errfunc;
962     status = LUA_OK;  /* if it is here, there were no errors */
963   }
964   adjustresults(L, nresults);
965   lua_unlock(L);
966   return status;
967 }
968
969
970 LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
971                       const char *chunkname, const char *mode) {
972   ZIO z;
973   int status;
974   lua_lock(L);
975   if (!chunkname) chunkname = "?";
976   luaZ_init(L, &z, reader, data);
977   status = luaD_protectedparser(L, &z, chunkname, mode);
978   if (status == LUA_OK) {  /* no errors? */
979     LClosure *f = clLvalue(L->top - 1);  /* get newly created function */
980     if (f->nupvalues == 1) {  /* does it have one upvalue? */
981       /* get global table from registry */
982       Table *reg = hvalue(&G(L)->l_registry);
983       const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
984       /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
985       setobj(L, f->upvals[0]->v, gt);
986       luaC_barrier(L, f->upvals[0], gt);
987     }
988   }
989   lua_unlock(L);
990   return status;
991 }
992
993 #if defined(LUA_USE_DUMP)
994 LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
995   int status;
996   TValue *o;
997   lua_lock(L);
998   api_checknelems(L, 1);
999   o = L->top - 1;
1000   if (isLfunction(o))
1001     status = luaU_dump(L, getproto(o), writer, data, 0);
1002   else
1003     status = 1;
1004   lua_unlock(L);
1005   return status;
1006 }
1007 #endif
1008
1009 LUA_API int lua_status (lua_State *L) {
1010   return L->status;
1011 }
1012
1013
1014 /*
1015 ** Garbage-collection function
1016 */
1017
1018 LUA_API int lua_gc (lua_State *L, int what, int data) {
1019   int res = 0;
1020   global_State *g;
1021   lua_lock(L);
1022   g = G(L);
1023   switch (what) {
1024     case LUA_GCSTOP: {
1025       g->gcrunning = 0;
1026       break;
1027     }
1028     case LUA_GCRESTART: {
1029       luaE_setdebt(g, 0);
1030       g->gcrunning = 1;
1031       break;
1032     }
1033     case LUA_GCCOLLECT: {
1034       luaC_fullgc(L, 0);
1035       break;
1036     }
1037     case LUA_GCCOUNT: {
1038       /* GC values are expressed in Kbytes: #bytes/2^10 */
1039       res = cast_int(gettotalbytes(g) >> 10);
1040       break;
1041     }
1042     case LUA_GCCOUNTB: {
1043       res = cast_int(gettotalbytes(g) & 0x3ff);
1044       break;
1045     }
1046     case LUA_GCSTEP: {
1047       if (g->gckind == KGC_GEN) {  /* generational mode? */
1048         res = (g->GCestimate == 0);  /* true if it will do major collection */
1049         luaC_forcestep(L);  /* do a single step */
1050       }
1051       else {
1052        lu_mem debt = cast(lu_mem, data) * 1024 - GCSTEPSIZE;
1053        if (g->gcrunning)
1054          debt += g->GCdebt;  /* include current debt */
1055        luaE_setdebt(g, debt);
1056        luaC_forcestep(L);
1057        if (g->gcstate == GCSpause)  /* end of cycle? */
1058          res = 1;  /* signal it */
1059       }
1060       break;
1061     }
1062     case LUA_GCSETPAUSE: {
1063       res = g->gcpause;
1064       g->gcpause = data;
1065       break;
1066     }
1067     case LUA_GCSETMAJORINC: {
1068       res = g->gcmajorinc;
1069       g->gcmajorinc = data;
1070       break;
1071     }
1072     case LUA_GCSETSTEPMUL: {
1073       res = g->gcstepmul;
1074       g->gcstepmul = data;
1075       break;
1076     }
1077     case LUA_GCISRUNNING: {
1078       res = g->gcrunning;
1079       break;
1080     }
1081     case LUA_GCGEN: {  /* change collector to generational mode */
1082       luaC_changemode(L, KGC_GEN);
1083       break;
1084     }
1085     case LUA_GCINC: {  /* change collector to incremental mode */
1086       luaC_changemode(L, KGC_NORMAL);
1087       break;
1088     }
1089     default: res = -1;  /* invalid option */
1090   }
1091   lua_unlock(L);
1092   return res;
1093 }
1094
1095
1096
1097 /*
1098 ** miscellaneous functions
1099 */
1100
1101
1102 LUA_API int lua_error (lua_State *L) {
1103   lua_lock(L);
1104   api_checknelems(L, 1);
1105   luaG_errormsg(L);
1106   /* code unreachable; will unlock when control actually leaves the kernel */
1107   return 0;  /* to avoid warnings */
1108 }
1109
1110
1111 LUA_API int lua_next (lua_State *L, int idx) {
1112   StkId t;
1113   int more;
1114   lua_lock(L);
1115   t = index2addr(L, idx);
1116   api_check(L, ttistable(t), "table expected");
1117   more = luaH_next(L, hvalue(t), L->top - 1);
1118   if (more) {
1119     api_incr_top(L);
1120   }
1121   else  /* no more elements */
1122     L->top -= 1;  /* remove key */
1123   lua_unlock(L);
1124   return more;
1125 }
1126
1127
1128 LUA_API void lua_concat (lua_State *L, int n) {
1129   lua_lock(L);
1130   api_checknelems(L, n);
1131   if (n >= 2) {
1132     luaC_checkGC(L);
1133     luaV_concat(L, n);
1134   }
1135   else if (n == 0) {  /* push empty string */
1136     setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
1137     api_incr_top(L);
1138   }
1139   /* else n == 1; nothing to do */
1140   lua_unlock(L);
1141 }
1142
1143
1144 LUA_API void lua_len (lua_State *L, int idx) {
1145   StkId t;
1146   lua_lock(L);
1147   t = index2addr(L, idx);
1148   luaV_objlen(L, L->top, t);
1149   api_incr_top(L);
1150   lua_unlock(L);
1151 }
1152
1153
1154 LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
1155   lua_Alloc f;
1156   lua_lock(L);
1157   if (ud) *ud = G(L)->ud;
1158   f = G(L)->frealloc;
1159   lua_unlock(L);
1160   return f;
1161 }
1162
1163
1164 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
1165   lua_lock(L);
1166   G(L)->ud = ud;
1167   G(L)->frealloc = f;
1168   lua_unlock(L);
1169 }
1170
1171
1172 LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
1173   Udata *u;
1174   lua_lock(L);
1175   luaC_checkGC(L);
1176   u = luaS_newudata(L, size, NULL);
1177   setuvalue(L, L->top, u);
1178   api_incr_top(L);
1179   lua_unlock(L);
1180   return u + 1;
1181 }
1182
1183
1184
1185 static const char *aux_upvalue (StkId fi, int n, TValue **val,
1186                                 GCObject **owner) {
1187   switch (ttype(fi)) {
1188     case LUA_TCCL: {  /* C closure */
1189       CClosure *f = clCvalue(fi);
1190       if (!(1 <= n && n <= f->nupvalues)) return NULL;
1191       *val = &f->upvalue[n-1];
1192       if (owner) *owner = obj2gco(f);
1193       return "";
1194     }
1195     case LUA_TLCL: {  /* Lua closure */
1196       LClosure *f = clLvalue(fi);
1197       TString *name;
1198       Proto *p = f->p;
1199       if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1200       *val = f->upvals[n-1]->v;
1201       if (owner) *owner = obj2gco(f->upvals[n - 1]);
1202       name = p->upvalues[n-1].name;
1203       return (name == NULL) ? "" : getstr(name);
1204     }
1205     default: return NULL;  /* not a closure */
1206   }
1207 }
1208
1209
1210 LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1211   const char *name;
1212   TValue *val = NULL;  /* to avoid warnings */
1213   lua_lock(L);
1214   name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL);
1215   if (name) {
1216     setobj2s(L, L->top, val);
1217     api_incr_top(L);
1218   }
1219   lua_unlock(L);
1220   return name;
1221 }
1222
1223
1224 LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
1225   const char *name;
1226   TValue *val = NULL;  /* to avoid warnings */
1227   GCObject *owner = NULL;  /* to avoid warnings */
1228   StkId fi;
1229   lua_lock(L);
1230   fi = index2addr(L, funcindex);
1231   api_checknelems(L, 1);
1232   name = aux_upvalue(fi, n, &val, &owner);
1233   if (name) {
1234     L->top--;
1235     setobj(L, val, L->top);
1236     luaC_barrier(L, owner, L->top);
1237   }
1238   lua_unlock(L);
1239   return name;
1240 }
1241
1242
1243 static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
1244   LClosure *f;
1245   StkId fi = index2addr(L, fidx);
1246   api_check(L, ttisLclosure(fi), "Lua function expected");
1247   f = clLvalue(fi);
1248   api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
1249   if (pf) *pf = f;
1250   return &f->upvals[n - 1];  /* get its upvalue pointer */
1251 }
1252
1253
1254 LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
1255   StkId fi = index2addr(L, fidx);
1256   switch (ttype(fi)) {
1257     case LUA_TLCL: {  /* lua closure */
1258       return *getupvalref(L, fidx, n, NULL);
1259     }
1260     case LUA_TCCL: {  /* C closure */
1261       CClosure *f = clCvalue(fi);
1262       api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
1263       return &f->upvalue[n - 1];
1264     }
1265     default: {
1266       api_check(L, 0, "closure expected");
1267       return NULL;
1268     }
1269   }
1270 }
1271
1272
1273 LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
1274                                             int fidx2, int n2) {
1275   LClosure *f1;
1276   UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
1277   UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
1278   *up1 = *up2;
1279   luaC_objbarrier(L, f1, *up2);
1280 }
1281
1282 #if defined(_KERNEL)
1283
1284 static int __init
1285 lua_init(void)
1286 {
1287         return (0);
1288 }
1289
1290 static void __exit
1291 lua_fini(void)
1292 {
1293 }
1294
1295 module_init(lua_init);
1296 module_exit(lua_fini);
1297
1298 #endif
1299 /* END CSTYLED */
1300
1301 ZFS_MODULE_DESCRIPTION("Lua Interpreter for ZFS");
1302 ZFS_MODULE_AUTHOR("Lua.org");
1303 ZFS_MODULE_LICENSE("MIT");
1304 ZFS_MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);
1305
1306 EXPORT_SYMBOL(lua_absindex);
1307 EXPORT_SYMBOL(lua_atpanic);
1308 EXPORT_SYMBOL(lua_checkstack);
1309 EXPORT_SYMBOL(lua_close);
1310 EXPORT_SYMBOL(lua_createtable);
1311 EXPORT_SYMBOL(lua_error);
1312 EXPORT_SYMBOL(lua_getfield);
1313 EXPORT_SYMBOL(lua_gettable);
1314 EXPORT_SYMBOL(lua_gettop);
1315 EXPORT_SYMBOL(lua_isnumber);
1316 EXPORT_SYMBOL(lua_isstring);
1317 EXPORT_SYMBOL(lua_newstate);
1318 EXPORT_SYMBOL(lua_newuserdata);
1319 EXPORT_SYMBOL(lua_next);
1320 EXPORT_SYMBOL(lua_pcallk);
1321 EXPORT_SYMBOL(lua_pushboolean);
1322 EXPORT_SYMBOL(lua_pushcclosure);
1323 EXPORT_SYMBOL(lua_pushfstring);
1324 EXPORT_SYMBOL(lua_pushinteger);
1325 EXPORT_SYMBOL(lua_pushlightuserdata);
1326 EXPORT_SYMBOL(lua_pushnil);
1327 EXPORT_SYMBOL(lua_pushnumber);
1328 EXPORT_SYMBOL(lua_pushstring);
1329 EXPORT_SYMBOL(lua_pushvalue);
1330 EXPORT_SYMBOL(lua_pushvfstring);
1331 EXPORT_SYMBOL(lua_remove);
1332 EXPORT_SYMBOL(lua_replace);
1333 EXPORT_SYMBOL(lua_setfield);
1334 EXPORT_SYMBOL(lua_setglobal);
1335 EXPORT_SYMBOL(lua_sethook);
1336 EXPORT_SYMBOL(lua_setmetatable);
1337 EXPORT_SYMBOL(lua_settable);
1338 EXPORT_SYMBOL(lua_settop);
1339 EXPORT_SYMBOL(lua_toboolean);
1340 EXPORT_SYMBOL(lua_tointegerx);
1341 EXPORT_SYMBOL(lua_tolstring);
1342 EXPORT_SYMBOL(lua_tonumberx);
1343 EXPORT_SYMBOL(lua_touserdata);
1344 EXPORT_SYMBOL(lua_type);
1345 EXPORT_SYMBOL(lua_typename);