annotate lib/lua/lua-5.2.2/lapi.c @ 1996:f7e8c4d4a01b

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