]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/lua/lib/contrib/src/lapi.c
update
[l4.git] / l4 / pkg / lua / lib / contrib / src / lapi.c
1 /*
2 ** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $
3 ** Lua API
4 ** See Copyright Notice in lua.h
5 */
6
7
8 #include <assert.h>
9 #include <math.h>
10 #include <stdarg.h>
11 #include <string.h>
12
13 #define lapi_c
14 #define LUA_CORE
15
16 #include "lua.h"
17
18 #include "lapi.h"
19 #include "ldebug.h"
20 #include "ldo.h"
21 #include "lfunc.h"
22 #include "lgc.h"
23 #include "lmem.h"
24 #include "lobject.h"
25 #include "lstate.h"
26 #include "lstring.h"
27 #include "ltable.h"
28 #include "ltm.h"
29 #include "lundump.h"
30 #include "lvm.h"
31 #include "lnum.h"
32
33
34 const char lua_ident[] =
35   "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n"
36   "$Authors: " LUA_AUTHORS " $\n"
37   "$URL: www.lua.org $\n";
38
39
40
41 #define api_checknelems(L, n)   api_check(L, (n) <= (L->top - L->base))
42
43 #define api_checkvalidindex(L, i)       api_check(L, (i) != luaO_nilobject)
44
45 #define api_incr_top(L)   {api_check(L, L->top < L->ci->top); L->top++;}
46
47
48
49 static TValue *index2adr (lua_State *L, int idx) {
50   if (idx > 0) {
51     TValue *o = L->base + (idx - 1);
52     api_check(L, idx <= L->ci->top - L->base);
53     if (o >= L->top) return cast(TValue *, luaO_nilobject);
54     else return o;
55   }
56   else if (idx > LUA_REGISTRYINDEX) {
57     api_check(L, idx != 0 && -idx <= L->top - L->base);
58     return L->top + idx;
59   }
60   else switch (idx) {  /* pseudo-indices */
61     case LUA_REGISTRYINDEX: return registry(L);
62     case LUA_ENVIRONINDEX: {
63       Closure *func = curr_func(L);
64       sethvalue(L, &L->env, func->c.env);
65       return &L->env;
66     }
67     case LUA_GLOBALSINDEX: return gt(L);
68     default: {
69       Closure *func = curr_func(L);
70       idx = LUA_GLOBALSINDEX - idx;
71       return (idx <= func->c.nupvalues)
72                 ? &func->c.upvalue[idx-1]
73                 : cast(TValue *, luaO_nilobject);
74     }
75   }
76 }
77
78
79 static Table *getcurrenv (lua_State *L) {
80   if (L->ci == L->base_ci)  /* no enclosing function? */
81     return hvalue(gt(L));  /* use global table as environment */
82   else {
83     Closure *func = curr_func(L);
84     return func->c.env;
85   }
86 }
87
88
89 void luaA_pushobject (lua_State *L, const TValue *o) {
90   setobj2s(L, L->top, o);
91   api_incr_top(L);
92 }
93
94
95 LUA_API int lua_checkstack (lua_State *L, int size) {
96   int res = 1;
97   lua_lock(L);
98   if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK)
99     res = 0;  /* stack overflow */
100   else if (size > 0) {
101     luaD_checkstack(L, size);
102     if (L->ci->top < L->top + size)
103       L->ci->top = L->top + size;
104   }
105   lua_unlock(L);
106   return res;
107 }
108
109
110 LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
111   int i;
112   if (from == to) return;
113   lua_lock(to);
114   api_checknelems(from, n);
115   api_check(from, G(from) == G(to));
116   api_check(from, to->ci->top - to->top >= n);
117   from->top -= n;
118   for (i = 0; i < n; i++) {
119     setobj2s(to, to->top++, from->top + i);
120   }
121   lua_unlock(to);
122 }
123
124
125 LUA_API void lua_setlevel (lua_State *from, lua_State *to) {
126   to->nCcalls = from->nCcalls;
127 }
128
129
130 LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
131   lua_CFunction old;
132   lua_lock(L);
133   old = G(L)->panic;
134   G(L)->panic = panicf;
135   lua_unlock(L);
136   return old;
137 }
138
139
140 LUA_API lua_State *lua_newthread (lua_State *L) {
141   lua_State *L1;
142   lua_lock(L);
143   luaC_checkGC(L);
144   L1 = luaE_newthread(L);
145   setthvalue(L, L->top, L1);
146   api_incr_top(L);
147   lua_unlock(L);
148   luai_userstatethread(L, L1);
149   return L1;
150 }
151
152
153
154 /*
155 ** basic stack manipulation
156 */
157
158
159 LUA_API int lua_gettop (lua_State *L) {
160   return cast_int(L->top - L->base);
161 }
162
163
164 LUA_API void lua_settop (lua_State *L, int idx) {
165   lua_lock(L);
166   if (idx >= 0) {
167     api_check(L, idx <= L->stack_last - L->base);
168     while (L->top < L->base + idx)
169       setnilvalue(L->top++);
170     L->top = L->base + idx;
171   }
172   else {
173     api_check(L, -(idx+1) <= (L->top - L->base));
174     L->top += idx+1;  /* `subtract' index (index is negative) */
175   }
176   lua_unlock(L);
177 }
178
179
180 LUA_API void lua_remove (lua_State *L, int idx) {
181   StkId p;
182   lua_lock(L);
183   p = index2adr(L, idx);
184   api_checkvalidindex(L, p);
185   while (++p < L->top) setobjs2s(L, p-1, p);
186   L->top--;
187   lua_unlock(L);
188 }
189
190
191 LUA_API void lua_insert (lua_State *L, int idx) {
192   StkId p;
193   StkId q;
194   lua_lock(L);
195   p = index2adr(L, idx);
196   api_checkvalidindex(L, p);
197   for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
198   setobjs2s(L, p, L->top);
199   lua_unlock(L);
200 }
201
202
203 LUA_API void lua_replace (lua_State *L, int idx) {
204   StkId o;
205   lua_lock(L);
206   /* explicit test for incompatible code */
207   if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci)
208     luaG_runerror(L, "no calling environment");
209   api_checknelems(L, 1);
210   o = index2adr(L, idx);
211   api_checkvalidindex(L, o);
212   if (idx == LUA_ENVIRONINDEX) {
213     Closure *func = curr_func(L);
214     api_check(L, ttistable(L->top - 1)); 
215     func->c.env = hvalue(L->top - 1);
216     luaC_barrier(L, func, L->top - 1);
217   }
218   else {
219     setobj(L, o, L->top - 1);
220     if (idx < LUA_GLOBALSINDEX)  /* function upvalue? */
221       luaC_barrier(L, curr_func(L), L->top - 1);
222   }
223   L->top--;
224   lua_unlock(L);
225 }
226
227
228 LUA_API void lua_pushvalue (lua_State *L, int idx) {
229   lua_lock(L);
230   setobj2s(L, L->top, index2adr(L, idx));
231   api_incr_top(L);
232   lua_unlock(L);
233 }
234
235
236
237 /*
238 ** access functions (stack -> C)
239 */
240
241
242 LUA_API int lua_type (lua_State *L, int idx) {
243   StkId o = index2adr(L, idx);
244   return (o == luaO_nilobject) ? LUA_TNONE : ttype_ext(o);
245 }
246
247
248 LUA_API const char *lua_typename (lua_State *L, int t) {
249   UNUSED(L);
250 #ifdef LUA_TINT
251   lua_assert( t!= LUA_TINT );
252 #endif
253   return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
254 }
255
256
257 LUA_API int lua_iscfunction (lua_State *L, int idx) {
258   StkId o = index2adr(L, idx);
259   return iscfunction(o);
260 }
261
262
263 LUA_API int lua_isnumber (lua_State *L, int idx) {
264   TValue n;
265   const TValue *o = index2adr(L, idx);
266   return tonumber(o, &n);
267 }
268
269
270 LUA_API int lua_isinteger (lua_State *L, int idx) {
271   TValue tmp;
272   lua_Integer dum;
273   const TValue *o = index2adr(L, idx);
274 #ifdef LUA_TINT
275   return tonumber(o,&tmp) && (ttisint(o) || tt_integer_valued(o,&dum));
276 #else
277   return tonumber(o,&tmp) && tt_integer_valued(o,&dum);
278 #endif
279 }
280
281
282 LUA_API int lua_isstring (lua_State *L, int idx) {
283   int t = lua_type(L, idx);
284   return (t == LUA_TSTRING || t == LUA_TNUMBER);
285 }
286
287
288 LUA_API int lua_isuserdata (lua_State *L, int idx) {
289   const TValue *o = index2adr(L, idx);
290   return (ttisuserdata(o) || ttislightuserdata(o));
291 }
292
293
294 LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
295   StkId o1 = index2adr(L, index1);
296   StkId o2 = index2adr(L, index2);
297   return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
298          : luaO_rawequalObj(o1, o2);
299 }
300
301
302 LUA_API int lua_equal (lua_State *L, int index1, int index2) {
303   StkId o1, o2;
304   int i;
305   lua_lock(L);  /* may call tag method */
306   o1 = index2adr(L, index1);
307   o2 = index2adr(L, index2);
308   i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2);
309   lua_unlock(L);
310   return i;
311 }
312
313
314 LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
315   StkId o1, o2;
316   int i;
317   lua_lock(L);  /* may call tag method */
318   o1 = index2adr(L, index1);
319   o2 = index2adr(L, index2);
320   i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
321        : luaV_lessthan(L, o1, o2);
322   lua_unlock(L);
323   return i;
324 }
325
326
327 LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
328   TValue n;
329   const TValue *o = index2adr(L, idx);
330   if (tonumber(o, &n)) {
331 #ifdef LNUM_COMPLEX
332     if (nvalue_img(o) != 0)
333       luaG_runerror(L, "expecting a real number");
334 #endif
335     return nvalue(o);
336   }
337   return 0;
338 }
339
340
341 LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
342   TValue n;
343     /* Lua 5.1 documented behaviour is to return nonzero for non-integer:
344      *
345      * "If the number is not an integer, it is truncated in some non-specified way." 
346      */
347 #ifdef LUA_COMPAT_TOINTEGER
348   /* Lua 5.1 compatible */
349   const TValue *o = index2adr(L, idx);
350   if (tonumber(o, &n)) {
351     lua_Integer i;
352     lua_Number d;
353 # ifdef LUA_TINT
354     if (ttisint(o)) return ivalue(o);
355 # endif
356 # ifdef LNUM_COMPLEX
357     if (nvalue_img_fast(o) != 0)
358       luaG_runerror(L, "expecting a real number");
359 # endif
360     d= nvalue_fast(o);
361     lua_number2integer(i, d);
362     return i;
363   }
364 #else
365   /* New suggestion */
366   const TValue *o = index2adr(L, idx);
367   if (tonumber(o, &n)) {
368     lua_Integer i;
369     if (ttisint(o)) return ivalue(o);
370     if (tt_integer_valued(o,&i)) return i;
371   }
372 #endif
373   return 0;
374 }
375
376
377 #ifdef LNUM_COMPLEX
378 LUA_API lua_Complex lua_tocomplex (lua_State *L, int idx) {
379   TValue tmp;
380   const TValue *o = index2adr(L, idx);
381   if (tonumber(o, &tmp))
382     return nvalue_complex(o);
383   return 0;
384 }
385 #endif
386
387
388 LUA_API int lua_toboolean (lua_State *L, int idx) {
389   const TValue *o = index2adr(L, idx);
390   return !l_isfalse(o);
391 }
392
393
394 LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
395   StkId o = index2adr(L, idx);
396   if (!ttisstring(o)) {
397     lua_lock(L);  /* `luaV_tostring' may create a new string */
398     if (!luaV_tostring(L, o)) {  /* conversion failed? */
399       if (len != NULL) *len = 0;
400       lua_unlock(L);
401       return NULL;
402     }
403     luaC_checkGC(L);
404     o = index2adr(L, idx);  /* previous call may reallocate the stack */
405     lua_unlock(L);
406   }
407   if (len != NULL) *len = tsvalue(o)->len;
408   return svalue(o);
409 }
410
411
412 LUA_API size_t lua_objlen (lua_State *L, int idx) {
413   StkId o = index2adr(L, idx);
414   switch (ttype(o)) {
415     case LUA_TSTRING: return tsvalue(o)->len;
416     case LUA_TUSERDATA: return uvalue(o)->len;
417     case LUA_TTABLE: return luaH_getn(hvalue(o));
418 #ifdef LUA_TINT
419     case LUA_TINT:
420 #endif
421     case LUA_TNUMBER: {
422       size_t l;
423       lua_lock(L);  /* `luaV_tostring' may create a new string */
424       l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);
425       lua_unlock(L);
426       return l;
427     }
428     default: return 0;
429   }
430 }
431
432
433 LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
434   StkId o = index2adr(L, idx);
435   return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
436 }
437
438
439 LUA_API void *lua_touserdata (lua_State *L, int idx) {
440   StkId o = index2adr(L, idx);
441   switch (ttype(o)) {
442     case LUA_TUSERDATA: return (rawuvalue(o) + 1);
443     case LUA_TLIGHTUSERDATA: return pvalue(o);
444     default: return NULL;
445   }
446 }
447
448
449 LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
450   StkId o = index2adr(L, idx);
451   return (!ttisthread(o)) ? NULL : thvalue(o);
452 }
453
454
455 LUA_API const void *lua_topointer (lua_State *L, int idx) {
456   StkId o = index2adr(L, idx);
457   switch (ttype(o)) {
458     case LUA_TTABLE: return hvalue(o);
459     case LUA_TFUNCTION: return clvalue(o);
460     case LUA_TTHREAD: return thvalue(o);
461     case LUA_TUSERDATA:
462     case LUA_TLIGHTUSERDATA:
463       return lua_touserdata(L, idx);
464     default: return NULL;
465   }
466 }
467
468
469
470 /*
471 ** push functions (C -> stack)
472 */
473
474
475 LUA_API void lua_pushnil (lua_State *L) {
476   lua_lock(L);
477   setnilvalue(L->top);
478   api_incr_top(L);
479   lua_unlock(L);
480 }
481
482
483 /* 'lua_pushnumber()' may lose accuracy on integers, 'lua_pushinteger' will not.
484  */
485 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
486   lua_lock(L);
487   setnvalue(L->top, n);
488   api_incr_top(L);
489   lua_unlock(L);
490 }
491
492
493 LUA_API void lua_pushinteger (lua_State *L, lua_Integer i) {
494   lua_lock(L);
495   setivalue(L->top, i);
496   api_incr_top(L);
497   lua_unlock(L);
498 }
499
500
501 #ifdef LNUM_COMPLEX
502 LUA_API void lua_pushcomplex (lua_State *L, lua_Complex v) {
503   lua_lock(L);
504   setnvalue_complex( L->top, v );
505   api_incr_top(L);
506   lua_unlock(L);
507 }
508 #endif
509
510
511 LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
512   lua_lock(L);
513   luaC_checkGC(L);
514   setsvalue2s(L, L->top, luaS_newlstr(L, s, len));
515   api_incr_top(L);
516   lua_unlock(L);
517 }
518
519
520 LUA_API void lua_pushstring (lua_State *L, const char *s) {
521   if (s == NULL)
522     lua_pushnil(L);
523   else
524     lua_pushlstring(L, s, strlen(s));
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   Closure *cl;
554   lua_lock(L);
555   luaC_checkGC(L);
556   api_checknelems(L, n);
557   cl = luaF_newCclosure(L, n, getcurrenv(L));
558   cl->c.f = fn;
559   L->top -= n;
560   while (n--)
561     setobj2n(L, &cl->c.upvalue[n], L->top+n);
562   setclvalue(L, L->top, cl);
563   lua_assert(iswhite(obj2gco(cl)));
564   api_incr_top(L);
565   lua_unlock(L);
566 }
567
568
569 LUA_API void lua_pushboolean (lua_State *L, int b) {
570   lua_lock(L);
571   setbvalue(L->top, (b != 0));  /* ensure that true is 1 */
572   api_incr_top(L);
573   lua_unlock(L);
574 }
575
576
577 LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
578   lua_lock(L);
579   setpvalue(L->top, p);
580   api_incr_top(L);
581   lua_unlock(L);
582 }
583
584
585 LUA_API int lua_pushthread (lua_State *L) {
586   lua_lock(L);
587   setthvalue(L, L->top, L);
588   api_incr_top(L);
589   lua_unlock(L);
590   return (G(L)->mainthread == L);
591 }
592
593
594
595 /*
596 ** get functions (Lua -> stack)
597 */
598
599
600 LUA_API void lua_gettable (lua_State *L, int idx) {
601   StkId t;
602   lua_lock(L);
603   t = index2adr(L, idx);
604   api_checkvalidindex(L, t);
605   luaV_gettable(L, t, L->top - 1, L->top - 1);
606   lua_unlock(L);
607 }
608
609
610 LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
611   StkId t;
612   TValue key;
613   lua_lock(L);
614   t = index2adr(L, idx);
615   api_checkvalidindex(L, t);
616   setsvalue(L, &key, luaS_new(L, k));
617   luaV_gettable(L, t, &key, L->top);
618   api_incr_top(L);
619   lua_unlock(L);
620 }
621
622
623 LUA_API void lua_rawget (lua_State *L, int idx) {
624   StkId t;
625   lua_lock(L);
626   t = index2adr(L, idx);
627   api_check(L, ttistable(t));
628   setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
629   lua_unlock(L);
630 }
631
632
633 LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
634   StkId o;
635   lua_lock(L);
636   o = index2adr(L, idx);
637   api_check(L, ttistable(o));
638   setobj2s(L, L->top, luaH_getint(hvalue(o), n));
639   api_incr_top(L);
640   lua_unlock(L);
641 }
642
643
644 LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
645   lua_lock(L);
646   luaC_checkGC(L);
647   sethvalue(L, L->top, luaH_new(L, narray, nrec));
648   api_incr_top(L);
649   lua_unlock(L);
650 }
651
652
653 LUA_API int lua_getmetatable (lua_State *L, int objindex) {
654   const TValue *obj;
655   Table *mt = NULL;
656   int res;
657   lua_lock(L);
658   obj = index2adr(L, objindex);
659   switch (ttype(obj)) {
660     case LUA_TTABLE:
661       mt = hvalue(obj)->metatable;
662       break;
663     case LUA_TUSERDATA:
664       mt = uvalue(obj)->metatable;
665       break;
666 #ifdef LUA_TINT
667     case LUA_TINT:
668       mt = G(L)->mt[LUA_TNUMBER];
669       break;
670 #endif
671     default:
672       mt = G(L)->mt[ttype(obj)];
673       break;
674   }
675   if (mt == NULL)
676     res = 0;
677   else {
678     sethvalue(L, L->top, mt);
679     api_incr_top(L);
680     res = 1;
681   }
682   lua_unlock(L);
683   return res;
684 }
685
686
687 LUA_API void lua_getfenv (lua_State *L, int idx) {
688   StkId o;
689   lua_lock(L);
690   o = index2adr(L, idx);
691   api_checkvalidindex(L, o);
692   switch (ttype(o)) {
693     case LUA_TFUNCTION:
694       sethvalue(L, L->top, clvalue(o)->c.env);
695       break;
696     case LUA_TUSERDATA:
697       sethvalue(L, L->top, uvalue(o)->env);
698       break;
699     case LUA_TTHREAD:
700       setobj2s(L, L->top,  gt(thvalue(o)));
701       break;
702     default:
703       setnilvalue(L->top);
704       break;
705   }
706   api_incr_top(L);
707   lua_unlock(L);
708 }
709
710
711 /*
712 ** set functions (stack -> Lua)
713 */
714
715
716 LUA_API void lua_settable (lua_State *L, int idx) {
717   StkId t;
718   lua_lock(L);
719   api_checknelems(L, 2);
720   t = index2adr(L, idx);
721   api_checkvalidindex(L, t);
722   luaV_settable(L, t, L->top - 2, L->top - 1);
723   L->top -= 2;  /* pop index and value */
724   lua_unlock(L);
725 }
726
727
728 LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
729   StkId t;
730   TValue key;
731   lua_lock(L);
732   api_checknelems(L, 1);
733   t = index2adr(L, idx);
734   api_checkvalidindex(L, t);
735   setsvalue(L, &key, luaS_new(L, k));
736   luaV_settable(L, t, &key, L->top - 1);
737   L->top--;  /* pop value */
738   lua_unlock(L);
739 }
740
741
742 LUA_API void lua_rawset (lua_State *L, int idx) {
743   StkId t;
744   lua_lock(L);
745   api_checknelems(L, 2);
746   t = index2adr(L, idx);
747   api_check(L, ttistable(t));
748   setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
749   luaC_barriert(L, hvalue(t), L->top-1);
750   L->top -= 2;
751   lua_unlock(L);
752 }
753
754
755 LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
756   StkId o;
757   lua_lock(L);
758   api_checknelems(L, 1);
759   o = index2adr(L, idx);
760   api_check(L, ttistable(o));
761   setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1);
762   luaC_barriert(L, hvalue(o), L->top-1);
763   L->top--;
764   lua_unlock(L);
765 }
766
767
768 LUA_API int lua_setmetatable (lua_State *L, int objindex) {
769   TValue *obj;
770   Table *mt;
771   lua_lock(L);
772   api_checknelems(L, 1);
773   obj = index2adr(L, objindex);
774   api_checkvalidindex(L, obj);
775   if (ttisnil(L->top - 1))
776     mt = NULL;
777   else {
778     api_check(L, ttistable(L->top - 1));
779     mt = hvalue(L->top - 1);
780   }
781   switch (ttype(obj)) {
782     case LUA_TTABLE: {
783       hvalue(obj)->metatable = mt;
784       if (mt)
785         luaC_objbarriert(L, hvalue(obj), mt);
786       break;
787     }
788     case LUA_TUSERDATA: {
789       uvalue(obj)->metatable = mt;
790       if (mt)
791         luaC_objbarrier(L, rawuvalue(obj), mt);
792       break;
793     }
794     default: {
795       G(L)->mt[ttype_ext(obj)] = mt;
796       break;
797     }
798   }
799   L->top--;
800   lua_unlock(L);
801   return 1;
802 }
803
804
805 LUA_API int lua_setfenv (lua_State *L, int idx) {
806   StkId o;
807   int res = 1;
808   lua_lock(L);
809   api_checknelems(L, 1);
810   o = index2adr(L, idx);
811   api_checkvalidindex(L, o);
812   api_check(L, ttistable(L->top - 1));
813   switch (ttype(o)) {
814     case LUA_TFUNCTION:
815       clvalue(o)->c.env = hvalue(L->top - 1);
816       break;
817     case LUA_TUSERDATA:
818       uvalue(o)->env = hvalue(L->top - 1);
819       break;
820     case LUA_TTHREAD:
821       sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
822       break;
823     default:
824       res = 0;
825       break;
826   }
827   if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
828   L->top--;
829   lua_unlock(L);
830   return res;
831 }
832
833
834 /*
835 ** `load' and `call' functions (run Lua code)
836 */
837
838
839 #define adjustresults(L,nres) \
840     { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
841
842
843 #define checkresults(L,na,nr) \
844      api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
845         
846
847 LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
848   StkId func;
849   lua_lock(L);
850   api_checknelems(L, nargs+1);
851   checkresults(L, nargs, nresults);
852   func = L->top - (nargs+1);
853   luaD_call(L, func, nresults);
854   adjustresults(L, nresults);
855   lua_unlock(L);
856 }
857
858
859
860 /*
861 ** Execute a protected call.
862 */
863 struct CallS {  /* data to `f_call' */
864   StkId func;
865   int nresults;
866 };
867
868
869 static void f_call (lua_State *L, void *ud) {
870   struct CallS *c = cast(struct CallS *, ud);
871   luaD_call(L, c->func, c->nresults);
872 }
873
874
875
876 LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
877   struct CallS c;
878   int status;
879   ptrdiff_t func;
880   lua_lock(L);
881   api_checknelems(L, nargs+1);
882   checkresults(L, nargs, nresults);
883   if (errfunc == 0)
884     func = 0;
885   else {
886     StkId o = index2adr(L, errfunc);
887     api_checkvalidindex(L, o);
888     func = savestack(L, o);
889   }
890   c.func = L->top - (nargs+1);  /* function to be called */
891   c.nresults = nresults;
892   status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
893   adjustresults(L, nresults);
894   lua_unlock(L);
895   return status;
896 }
897
898
899 /*
900 ** Execute a protected C call.
901 */
902 struct CCallS {  /* data to `f_Ccall' */
903   lua_CFunction func;
904   void *ud;
905 };
906
907
908 static void f_Ccall (lua_State *L, void *ud) {
909   struct CCallS *c = cast(struct CCallS *, ud);
910   Closure *cl;
911   cl = luaF_newCclosure(L, 0, getcurrenv(L));
912   cl->c.f = c->func;
913   setclvalue(L, L->top, cl);  /* push function */
914   api_incr_top(L);
915   setpvalue(L->top, c->ud);  /* push only argument */
916   api_incr_top(L);
917   luaD_call(L, L->top - 2, 0);
918 }
919
920
921 LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
922   struct CCallS c;
923   int status;
924   lua_lock(L);
925   c.func = func;
926   c.ud = ud;
927   status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
928   lua_unlock(L);
929   return status;
930 }
931
932
933 LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
934                       const char *chunkname) {
935   ZIO z;
936   int status;
937   lua_lock(L);
938   if (!chunkname) chunkname = "?";
939   luaZ_init(L, &z, reader, data);
940   status = luaD_protectedparser(L, &z, chunkname);
941   lua_unlock(L);
942   return status;
943 }
944
945
946 LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
947   int status;
948   TValue *o;
949   lua_lock(L);
950   api_checknelems(L, 1);
951   o = L->top - 1;
952   if (isLfunction(o))
953     status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
954   else
955     status = 1;
956   lua_unlock(L);
957   return status;
958 }
959
960
961 LUA_API int  lua_status (lua_State *L) {
962   return L->status;
963 }
964
965
966 /*
967 ** Garbage-collection function
968 */
969
970 LUA_API int lua_gc (lua_State *L, int what, int data) {
971   int res = 0;
972   global_State *g;
973   lua_lock(L);
974   g = G(L);
975   switch (what) {
976     case LUA_GCSTOP: {
977       g->GCthreshold = MAX_LUMEM;
978       break;
979     }
980     case LUA_GCRESTART: {
981       g->GCthreshold = g->totalbytes;
982       break;
983     }
984     case LUA_GCCOLLECT: {
985       luaC_fullgc(L);
986       break;
987     }
988     case LUA_GCCOUNT: {
989       /* GC values are expressed in Kbytes: #bytes/2^10 */
990       res = cast_int(g->totalbytes >> 10);
991       break;
992     }
993     case LUA_GCCOUNTB: {
994       res = cast_int(g->totalbytes & 0x3ff);
995       break;
996     }
997     case LUA_GCSTEP: {
998       lu_mem a = (cast(lu_mem, data) << 10);
999       if (a <= g->totalbytes)
1000         g->GCthreshold = g->totalbytes - a;
1001       else
1002         g->GCthreshold = 0;
1003       while (g->GCthreshold <= g->totalbytes) {
1004         luaC_step(L);
1005         if (g->gcstate == GCSpause) {  /* end of cycle? */
1006           res = 1;  /* signal it */
1007           break;
1008         }
1009       }
1010       break;
1011     }
1012     case LUA_GCSETPAUSE: {
1013       res = g->gcpause;
1014       g->gcpause = data;
1015       break;
1016     }
1017     case LUA_GCSETSTEPMUL: {
1018       res = g->gcstepmul;
1019       g->gcstepmul = data;
1020       break;
1021     }
1022     default: res = -1;  /* invalid option */
1023   }
1024   lua_unlock(L);
1025   return res;
1026 }
1027
1028
1029
1030 /*
1031 ** miscellaneous functions
1032 */
1033
1034
1035 LUA_API int lua_error (lua_State *L) {
1036   lua_lock(L);
1037   api_checknelems(L, 1);
1038   luaG_errormsg(L);
1039   lua_unlock(L);
1040   return 0;  /* to avoid warnings */
1041 }
1042
1043
1044 LUA_API int lua_next (lua_State *L, int idx) {
1045   StkId t;
1046   int more;
1047   lua_lock(L);
1048   t = index2adr(L, idx);
1049   api_check(L, ttistable(t));
1050   more = luaH_next(L, hvalue(t), L->top - 1);
1051   if (more) {
1052     api_incr_top(L);
1053   }
1054   else  /* no more elements */
1055     L->top -= 1;  /* remove key */
1056   lua_unlock(L);
1057   return more;
1058 }
1059
1060
1061 LUA_API void lua_concat (lua_State *L, int n) {
1062   lua_lock(L);
1063   api_checknelems(L, n);
1064   if (n >= 2) {
1065     luaC_checkGC(L);
1066     luaV_concat(L, n, cast_int(L->top - L->base) - 1);
1067     L->top -= (n-1);
1068   }
1069   else if (n == 0) {  /* push empty string */
1070     setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
1071     api_incr_top(L);
1072   }
1073   /* else n == 1; nothing to do */
1074   lua_unlock(L);
1075 }
1076
1077
1078 LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
1079   lua_Alloc f;
1080   lua_lock(L);
1081   if (ud) *ud = G(L)->ud;
1082   f = G(L)->frealloc;
1083   lua_unlock(L);
1084   return f;
1085 }
1086
1087
1088 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
1089   lua_lock(L);
1090   G(L)->ud = ud;
1091   G(L)->frealloc = f;
1092   lua_unlock(L);
1093 }
1094
1095
1096 LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
1097   Udata *u;
1098   lua_lock(L);
1099   luaC_checkGC(L);
1100   u = luaS_newudata(L, size, getcurrenv(L));
1101   setuvalue(L, L->top, u);
1102   api_incr_top(L);
1103   lua_unlock(L);
1104   return u + 1;
1105 }
1106
1107
1108
1109
1110 static const char *aux_upvalue (StkId fi, int n, TValue **val) {
1111   Closure *f;
1112   if (!ttisfunction(fi)) return NULL;
1113   f = clvalue(fi);
1114   if (f->c.isC) {
1115     if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
1116     *val = &f->c.upvalue[n-1];
1117     return "";
1118   }
1119   else {
1120     Proto *p = f->l.p;
1121     if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1122     *val = f->l.upvals[n-1]->v;
1123     return getstr(p->upvalues[n-1]);
1124   }
1125 }
1126
1127
1128 LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1129   const char *name;
1130   TValue *val;
1131   lua_lock(L);
1132   name = aux_upvalue(index2adr(L, funcindex), n, &val);
1133   if (name) {
1134     setobj2s(L, L->top, val);
1135     api_incr_top(L);
1136   }
1137   lua_unlock(L);
1138   return name;
1139 }
1140
1141
1142 LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
1143   const char *name;
1144   TValue *val;
1145   StkId fi;
1146   lua_lock(L);
1147   fi = index2adr(L, funcindex);
1148   api_checknelems(L, 1);
1149   name = aux_upvalue(fi, n, &val);
1150   if (name) {
1151     L->top--;
1152     setobj(L, val, L->top);
1153     luaC_barrier(L, clvalue(fi), L->top);
1154   }
1155   lua_unlock(L);
1156   return name;
1157 }
1158
1159
1160 /* Help function for 'luaB_tonumber()', avoids multiple str->number
1161  * conversions for Lua "tonumber()".
1162  *
1163  * Also pushes floating point numbers with integer value as integer, which
1164  * can be used by 'tonumber()' in scripts to bring values back to integer
1165  * realm.
1166  *
1167  * Note: The 'back to integer realm' is _not_ to affect string conversions:
1168  * 'tonumber("4294967295.1")' should give a floating point value, although
1169  * the value would be 4294967296 (and storable in int64 realm).
1170  */
1171 #ifdef LUA_TINT
1172 int lua_pushvalue_as_number (lua_State *L, int idx)
1173 {
1174   const TValue *o = index2adr(L, idx);
1175   TValue tmp;
1176   lua_Integer i;
1177   if (ttisnumber(o)) {
1178     if ( (!ttisint(o)) && tt_integer_valued(o,&i)) {
1179       lua_pushinteger( L, i );
1180       return 1;
1181     }
1182   } else if (!tonumber(o, &tmp)) {
1183     return 0;
1184   }
1185   if (ttisint(o)) lua_pushinteger( L, ivalue(o) );
1186   else lua_pushnumber( L, nvalue_fast(o) );
1187   return 1;
1188 }
1189 #endif