diff options
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 374 |
1 files changed, 187 insertions, 187 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.55 1999/11/04 17:22:26 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.56 1999/11/11 17:02:40 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -7,6 +7,8 @@ | |||
7 | 7 | ||
8 | #include <string.h> | 8 | #include <string.h> |
9 | 9 | ||
10 | #define LUA_REENTRANT | ||
11 | |||
10 | #include "lapi.h" | 12 | #include "lapi.h" |
11 | #include "lauxlib.h" | 13 | #include "lauxlib.h" |
12 | #include "ldo.h" | 14 | #include "ldo.h" |
@@ -29,8 +31,8 @@ const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n" | |||
29 | 31 | ||
30 | 32 | ||
31 | 33 | ||
32 | TObject *luaA_Address (lua_Object o) { | 34 | TObject *luaA_Address (lua_State *L, lua_Object o) { |
33 | return (o != LUA_NOOBJECT) ? Address(o) : NULL; | 35 | return (o != LUA_NOOBJECT) ? Address(L, o) : NULL; |
34 | } | 36 | } |
35 | 37 | ||
36 | 38 | ||
@@ -60,27 +62,27 @@ static const TObject *luaA_protovalue (const TObject *o) { | |||
60 | } | 62 | } |
61 | 63 | ||
62 | 64 | ||
63 | static void checkCparams (int nParams) { | 65 | static void checkCparams (lua_State *L, int nParams) { |
64 | if (L->stack.top-L->stack.stack < L->Cstack.base+nParams) | 66 | if (L->stack.top-L->stack.stack < L->Cstack.base+nParams) |
65 | lua_error("API error - wrong number of arguments in C2lua stack"); | 67 | lua_error(L, "API error - wrong number of arguments in C2lua stack"); |
66 | } | 68 | } |
67 | 69 | ||
68 | 70 | ||
69 | static lua_Object put_luaObject (const TObject *o) { | 71 | static lua_Object put_luaObject (lua_State *L, const TObject *o) { |
70 | luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); | 72 | luaD_openstack(L, (L->stack.top-L->stack.stack)-L->Cstack.base); |
71 | L->stack.stack[L->Cstack.base++] = *o; | 73 | L->stack.stack[L->Cstack.base++] = *o; |
72 | return L->Cstack.base; /* this is +1 real position (see Ref) */ | 74 | return L->Cstack.base; /* this is +1 real position (see Ref) */ |
73 | } | 75 | } |
74 | 76 | ||
75 | 77 | ||
76 | lua_Object luaA_putObjectOnTop (void) { | 78 | lua_Object luaA_putObjectOnTop (lua_State *L) { |
77 | luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); | 79 | luaD_openstack(L, (L->stack.top-L->stack.stack)-L->Cstack.base); |
78 | L->stack.stack[L->Cstack.base++] = *(--L->stack.top); | 80 | L->stack.stack[L->Cstack.base++] = *(--L->stack.top); |
79 | return L->Cstack.base; /* this is +1 real position (see Ref) */ | 81 | return L->Cstack.base; /* this is +1 real position (see Ref) */ |
80 | } | 82 | } |
81 | 83 | ||
82 | 84 | ||
83 | static void top2LC (int n) { | 85 | static void top2LC (lua_State *L, int n) { |
84 | /* Put the 'n' elements on the top as the Lua2C contents */ | 86 | /* Put the 'n' elements on the top as the Lua2C contents */ |
85 | L->Cstack.base = (L->stack.top-L->stack.stack); /* new base */ | 87 | L->Cstack.base = (L->stack.top-L->stack.stack); /* new base */ |
86 | L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */ | 88 | L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */ |
@@ -88,9 +90,9 @@ static void top2LC (int n) { | |||
88 | } | 90 | } |
89 | 91 | ||
90 | 92 | ||
91 | lua_Object lua_pop (void) { | 93 | lua_Object lua_pop (lua_State *L) { |
92 | checkCparams(1); | 94 | checkCparams(L, 1); |
93 | return luaA_putObjectOnTop(); | 95 | return luaA_putObjectOnTop(L); |
94 | } | 96 | } |
95 | 97 | ||
96 | 98 | ||
@@ -98,248 +100,248 @@ lua_Object lua_pop (void) { | |||
98 | ** Get a parameter, returning the object handle or LUA_NOOBJECT on error. | 100 | ** Get a parameter, returning the object handle or LUA_NOOBJECT on error. |
99 | ** 'number' must be 1 to get the first parameter. | 101 | ** 'number' must be 1 to get the first parameter. |
100 | */ | 102 | */ |
101 | lua_Object lua_lua2C (int number) { | 103 | lua_Object lua_lua2C (lua_State *L, int number) { |
102 | if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT; | 104 | if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT; |
103 | /* Ref(L->stack.stack+(L->Cstack.lua2C+number-1)) == | 105 | /* Ref(L, L->stack.stack+(L->Cstack.lua2C+number-1)) == |
104 | L->stack.stack+(L->Cstack.lua2C+number-1)-L->stack.stack+1 == */ | 106 | L->stack.stack+(L->Cstack.lua2C+number-1)-L->stack.stack+1 == */ |
105 | return L->Cstack.lua2C+number; | 107 | return L->Cstack.lua2C+number; |
106 | } | 108 | } |
107 | 109 | ||
108 | 110 | ||
109 | int lua_callfunction (lua_Object function) { | 111 | int lua_callfunction (lua_State *L, lua_Object function) { |
110 | if (function == LUA_NOOBJECT) | 112 | if (function == LUA_NOOBJECT) |
111 | return 1; | 113 | return 1; |
112 | else { | 114 | else { |
113 | luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); | 115 | luaD_openstack(L, (L->stack.top-L->stack.stack)-L->Cstack.base); |
114 | set_normalized(L->stack.stack+L->Cstack.base, Address(function)); | 116 | set_normalized(L->stack.stack+L->Cstack.base, Address(L, function)); |
115 | return luaD_protectedrun(); | 117 | return luaD_protectedrun(L); |
116 | } | 118 | } |
117 | } | 119 | } |
118 | 120 | ||
119 | 121 | ||
120 | lua_Object lua_gettagmethod (int tag, const char *event) { | 122 | lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event) { |
121 | return put_luaObject(luaT_gettagmethod(tag, event)); | 123 | return put_luaObject(L, luaT_gettagmethod(L, tag, event)); |
122 | } | 124 | } |
123 | 125 | ||
124 | 126 | ||
125 | lua_Object lua_settagmethod (int tag, const char *event) { | 127 | lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) { |
126 | checkCparams(1); | 128 | checkCparams(L, 1); |
127 | luaT_settagmethod(tag, event, L->stack.top-1); | 129 | luaT_settagmethod(L, tag, event, L->stack.top-1); |
128 | return luaA_putObjectOnTop(); | 130 | return luaA_putObjectOnTop(L); |
129 | } | 131 | } |
130 | 132 | ||
131 | 133 | ||
132 | lua_Object lua_seterrormethod (void) { | 134 | lua_Object lua_seterrormethod (lua_State *L) { |
133 | lua_Object temp; | 135 | lua_Object temp; |
134 | checkCparams(1); | 136 | checkCparams(L, 1); |
135 | temp = lua_getglobal("_ERRORMESSAGE"); | 137 | temp = lua_getglobal(L, "_ERRORMESSAGE"); |
136 | lua_setglobal("_ERRORMESSAGE"); | 138 | lua_setglobal(L, "_ERRORMESSAGE"); |
137 | return temp; | 139 | return temp; |
138 | } | 140 | } |
139 | 141 | ||
140 | 142 | ||
141 | lua_Object lua_gettable (void) { | 143 | lua_Object lua_gettable (lua_State *L) { |
142 | checkCparams(2); | 144 | checkCparams(L, 2); |
143 | luaV_gettable(); | 145 | luaV_gettable(L); |
144 | return luaA_putObjectOnTop(); | 146 | return luaA_putObjectOnTop(L); |
145 | } | 147 | } |
146 | 148 | ||
147 | 149 | ||
148 | lua_Object lua_rawgettable (void) { | 150 | lua_Object lua_rawgettable (lua_State *L) { |
149 | checkCparams(2); | 151 | checkCparams(L, 2); |
150 | if (ttype(L->stack.top-2) != LUA_T_ARRAY) | 152 | if (ttype(L->stack.top-2) != LUA_T_ARRAY) |
151 | lua_error("indexed expression not a table in rawgettable"); | 153 | lua_error(L, "indexed expression not a table in rawgettable"); |
152 | *(L->stack.top-2) = *luaH_get(avalue(L->stack.top-2), L->stack.top-1); | 154 | *(L->stack.top-2) = *luaH_get(L, avalue(L->stack.top-2), L->stack.top-1); |
153 | --L->stack.top; | 155 | --L->stack.top; |
154 | return luaA_putObjectOnTop(); | 156 | return luaA_putObjectOnTop(L); |
155 | } | 157 | } |
156 | 158 | ||
157 | 159 | ||
158 | void lua_settable (void) { | 160 | void lua_settable (lua_State *L) { |
159 | checkCparams(3); | 161 | checkCparams(L, 3); |
160 | luaV_settable(L->stack.top-3); | 162 | luaV_settable(L, L->stack.top-3); |
161 | L->stack.top -= 2; /* pop table and index */ | 163 | L->stack.top -= 2; /* pop table and index */ |
162 | } | 164 | } |
163 | 165 | ||
164 | 166 | ||
165 | void lua_rawsettable (void) { | 167 | void lua_rawsettable (lua_State *L) { |
166 | checkCparams(3); | 168 | checkCparams(L, 3); |
167 | luaV_rawsettable(L->stack.top-3); | 169 | luaV_rawsettable(L, L->stack.top-3); |
168 | } | 170 | } |
169 | 171 | ||
170 | 172 | ||
171 | lua_Object lua_createtable (void) { | 173 | lua_Object lua_createtable (lua_State *L) { |
172 | TObject o; | 174 | TObject o; |
173 | luaC_checkGC(); | 175 | luaC_checkGC(L); |
174 | avalue(&o) = luaH_new(0); | 176 | avalue(&o) = luaH_new(L, 0); |
175 | ttype(&o) = LUA_T_ARRAY; | 177 | ttype(&o) = LUA_T_ARRAY; |
176 | return put_luaObject(&o); | 178 | return put_luaObject(L, &o); |
177 | } | 179 | } |
178 | 180 | ||
179 | 181 | ||
180 | lua_Object lua_getglobal (const char *name) { | 182 | lua_Object lua_getglobal (lua_State *L, const char *name) { |
181 | luaD_checkstack(2); /* may need that to call T.M. */ | 183 | luaD_checkstack(L, 2); /* may need that to call T.M. */ |
182 | luaV_getglobal(luaS_assertglobalbyname(name)); | 184 | luaV_getglobal(L, luaS_assertglobalbyname(L, name)); |
183 | return luaA_putObjectOnTop(); | 185 | return luaA_putObjectOnTop(L); |
184 | } | 186 | } |
185 | 187 | ||
186 | 188 | ||
187 | lua_Object lua_rawgetglobal (const char *name) { | 189 | lua_Object lua_rawgetglobal (lua_State *L, const char *name) { |
188 | GlobalVar *gv = luaS_assertglobalbyname(name); | 190 | GlobalVar *gv = luaS_assertglobalbyname(L, name); |
189 | return put_luaObject(&gv->value); | 191 | return put_luaObject(L, &gv->value); |
190 | } | 192 | } |
191 | 193 | ||
192 | 194 | ||
193 | void lua_setglobal (const char *name) { | 195 | void lua_setglobal (lua_State *L, const char *name) { |
194 | checkCparams(1); | 196 | checkCparams(L, 1); |
195 | luaD_checkstack(2); /* may need that to call T.M. */ | 197 | luaD_checkstack(L, 2); /* may need that to call T.M. */ |
196 | luaV_setglobal(luaS_assertglobalbyname(name)); | 198 | luaV_setglobal(L, luaS_assertglobalbyname(L, name)); |
197 | } | 199 | } |
198 | 200 | ||
199 | 201 | ||
200 | void lua_rawsetglobal (const char *name) { | 202 | void lua_rawsetglobal (lua_State *L, const char *name) { |
201 | GlobalVar *gv = luaS_assertglobalbyname(name); | 203 | GlobalVar *gv = luaS_assertglobalbyname(L, name); |
202 | checkCparams(1); | 204 | checkCparams(L, 1); |
203 | gv->value = *(--L->stack.top); | 205 | gv->value = *(--L->stack.top); |
204 | } | 206 | } |
205 | 207 | ||
206 | 208 | ||
207 | const char *lua_type (lua_Object o) { | 209 | const char *lua_type (lua_State *L, lua_Object o) { |
208 | return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(Address(o)); | 210 | return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(L, Address(L, o)); |
209 | } | 211 | } |
210 | 212 | ||
211 | int lua_isnil (lua_Object o) { | 213 | int lua_isnil (lua_State *L, lua_Object o) { |
212 | return (o != LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL); | 214 | return (o != LUA_NOOBJECT) && (ttype(Address(L, o)) == LUA_T_NIL); |
213 | } | 215 | } |
214 | 216 | ||
215 | int lua_istable (lua_Object o) { | 217 | int lua_istable (lua_State *L, lua_Object o) { |
216 | return (o != LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY); | 218 | return (o != LUA_NOOBJECT) && (ttype(Address(L, o)) == LUA_T_ARRAY); |
217 | } | 219 | } |
218 | 220 | ||
219 | int lua_isuserdata (lua_Object o) { | 221 | int lua_isuserdata (lua_State *L, lua_Object o) { |
220 | return (o != LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA); | 222 | return (o != LUA_NOOBJECT) && (ttype(Address(L, o)) == LUA_T_USERDATA); |
221 | } | 223 | } |
222 | 224 | ||
223 | int lua_iscfunction (lua_Object o) { | 225 | int lua_iscfunction (lua_State *L, lua_Object o) { |
224 | return (lua_tag(o) == LUA_T_CPROTO); | 226 | return (lua_tag(L, o) == LUA_T_CPROTO); |
225 | } | 227 | } |
226 | 228 | ||
227 | int lua_isnumber (lua_Object o) { | 229 | int lua_isnumber (lua_State *L, lua_Object o) { |
228 | return (o != LUA_NOOBJECT) && (tonumber(Address(o)) == 0); | 230 | return (o != LUA_NOOBJECT) && (tonumber(Address(L, o)) == 0); |
229 | } | 231 | } |
230 | 232 | ||
231 | int lua_isstring (lua_Object o) { | 233 | int lua_isstring (lua_State *L, lua_Object o) { |
232 | int t = lua_tag(o); | 234 | int t = lua_tag(L, o); |
233 | return (t == LUA_T_STRING) || (t == LUA_T_NUMBER); | 235 | return (t == LUA_T_STRING) || (t == LUA_T_NUMBER); |
234 | } | 236 | } |
235 | 237 | ||
236 | int lua_isfunction (lua_Object o) { | 238 | int lua_isfunction (lua_State *L, lua_Object o) { |
237 | int t = lua_tag(o); | 239 | int t = lua_tag(L, o); |
238 | return (t == LUA_T_PROTO) || (t == LUA_T_CPROTO); | 240 | return (t == LUA_T_PROTO) || (t == LUA_T_CPROTO); |
239 | } | 241 | } |
240 | 242 | ||
241 | int lua_equalobj (lua_Object o1, lua_Object o2) { | 243 | int lua_equalobj (lua_State *L, lua_Object o1, lua_Object o2) { |
242 | if (o1 == LUA_NOOBJECT || o2 == LUA_NOOBJECT) return 0; | 244 | if (o1 == LUA_NOOBJECT || o2 == LUA_NOOBJECT) return 0; |
243 | else return luaO_equalObj(Address(o1), Address(o2)); | 245 | else return luaO_equalObj(Address(L, o1), Address(L, o2)); |
244 | } | 246 | } |
245 | 247 | ||
246 | 248 | ||
247 | double lua_getnumber (lua_Object object) { | 249 | double lua_getnumber (lua_State *L, lua_Object object) { |
248 | if (object == LUA_NOOBJECT) return 0.0; | 250 | if (object == LUA_NOOBJECT) return 0.0; |
249 | if (tonumber(Address(object))) return 0.0; | 251 | if (tonumber(Address(L, object))) return 0.0; |
250 | else return (nvalue(Address(object))); | 252 | else return (nvalue(Address(L, object))); |
251 | } | 253 | } |
252 | 254 | ||
253 | const char *lua_getstring (lua_Object object) { | 255 | const char *lua_getstring (lua_State *L, lua_Object object) { |
254 | luaC_checkGC(); /* "tostring" may create a new string */ | 256 | luaC_checkGC(L); /* `tostring' may create a new string */ |
255 | if (object == LUA_NOOBJECT || tostring(Address(object))) | 257 | if (object == LUA_NOOBJECT || tostring(L, Address(L, object))) |
256 | return NULL; | 258 | return NULL; |
257 | else return (svalue(Address(object))); | 259 | else return (svalue(Address(L, object))); |
258 | } | 260 | } |
259 | 261 | ||
260 | long lua_strlen (lua_Object object) { | 262 | long lua_strlen (lua_State *L, lua_Object object) { |
261 | luaC_checkGC(); /* "tostring" may create a new string */ | 263 | luaC_checkGC(L); /* `tostring' may create a new string */ |
262 | if (object == LUA_NOOBJECT || tostring(Address(object))) | 264 | if (object == LUA_NOOBJECT || tostring(L, Address(L, object))) |
263 | return 0L; | 265 | return 0L; |
264 | else return (tsvalue(Address(object))->u.s.len); | 266 | else return (tsvalue(Address(L, object))->u.s.len); |
265 | } | 267 | } |
266 | 268 | ||
267 | void *lua_getuserdata (lua_Object object) { | 269 | void *lua_getuserdata (lua_State *L, lua_Object object) { |
268 | if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA) | 270 | if (object == LUA_NOOBJECT || ttype(Address(L, object)) != LUA_T_USERDATA) |
269 | return NULL; | 271 | return NULL; |
270 | else return tsvalue(Address(object))->u.d.value; | 272 | else return tsvalue(Address(L, object))->u.d.value; |
271 | } | 273 | } |
272 | 274 | ||
273 | lua_CFunction lua_getcfunction (lua_Object object) { | 275 | lua_CFunction lua_getcfunction (lua_State *L, lua_Object object) { |
274 | if (!lua_iscfunction(object)) | 276 | if (!lua_iscfunction(L, object)) |
275 | return NULL; | 277 | return NULL; |
276 | else return fvalue(luaA_protovalue(Address(object))); | 278 | else return fvalue(luaA_protovalue(Address(L, object))); |
277 | } | 279 | } |
278 | 280 | ||
279 | 281 | ||
280 | void lua_pushnil (void) { | 282 | void lua_pushnil (lua_State *L) { |
281 | ttype(L->stack.top) = LUA_T_NIL; | 283 | ttype(L->stack.top) = LUA_T_NIL; |
282 | incr_top; | 284 | incr_top; |
283 | } | 285 | } |
284 | 286 | ||
285 | void lua_pushnumber (double n) { | 287 | void lua_pushnumber (lua_State *L, double n) { |
286 | ttype(L->stack.top) = LUA_T_NUMBER; | 288 | ttype(L->stack.top) = LUA_T_NUMBER; |
287 | nvalue(L->stack.top) = n; | 289 | nvalue(L->stack.top) = n; |
288 | incr_top; | 290 | incr_top; |
289 | } | 291 | } |
290 | 292 | ||
291 | void lua_pushlstring (const char *s, long len) { | 293 | void lua_pushlstring (lua_State *L, const char *s, long len) { |
292 | tsvalue(L->stack.top) = luaS_newlstr(s, len); | 294 | tsvalue(L->stack.top) = luaS_newlstr(L, s, len); |
293 | ttype(L->stack.top) = LUA_T_STRING; | 295 | ttype(L->stack.top) = LUA_T_STRING; |
294 | incr_top; | 296 | incr_top; |
295 | luaC_checkGC(); | 297 | luaC_checkGC(L); |
296 | } | 298 | } |
297 | 299 | ||
298 | void lua_pushstring (const char *s) { | 300 | void lua_pushstring (lua_State *L, const char *s) { |
299 | if (s == NULL) | 301 | if (s == NULL) |
300 | lua_pushnil(); | 302 | lua_pushnil(L); |
301 | else | 303 | else |
302 | lua_pushlstring(s, strlen(s)); | 304 | lua_pushlstring(L, s, strlen(s)); |
303 | } | 305 | } |
304 | 306 | ||
305 | void lua_pushcclosure (lua_CFunction fn, int n) { | 307 | void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { |
306 | if (fn == NULL) | 308 | if (fn == NULL) |
307 | lua_error("API error - attempt to push a NULL Cfunction"); | 309 | lua_error(L, "API error - attempt to push a NULL Cfunction"); |
308 | checkCparams(n); | 310 | checkCparams(L, n); |
309 | ttype(L->stack.top) = LUA_T_CPROTO; | 311 | ttype(L->stack.top) = LUA_T_CPROTO; |
310 | fvalue(L->stack.top) = fn; | 312 | fvalue(L->stack.top) = fn; |
311 | incr_top; | 313 | incr_top; |
312 | luaV_closure(n); | 314 | luaV_closure(L, n); |
313 | luaC_checkGC(); | 315 | luaC_checkGC(L); |
314 | } | 316 | } |
315 | 317 | ||
316 | void lua_pushusertag (void *u, int tag) { | 318 | void lua_pushusertag (lua_State *L, void *u, int tag) { |
317 | if (tag < 0 && tag != LUA_ANYTAG) | 319 | if (tag < 0 && tag != LUA_ANYTAG) |
318 | luaT_realtag(tag); /* error if tag is not valid */ | 320 | luaT_realtag(L, tag); /* error if tag is not valid */ |
319 | tsvalue(L->stack.top) = luaS_createudata(u, tag); | 321 | tsvalue(L->stack.top) = luaS_createudata(L, u, tag); |
320 | ttype(L->stack.top) = LUA_T_USERDATA; | 322 | ttype(L->stack.top) = LUA_T_USERDATA; |
321 | incr_top; | 323 | incr_top; |
322 | luaC_checkGC(); | 324 | luaC_checkGC(L); |
323 | } | 325 | } |
324 | 326 | ||
325 | void luaA_pushobject (const TObject *o) { | 327 | void luaA_pushobject (lua_State *L, const TObject *o) { |
326 | *L->stack.top = *o; | 328 | *L->stack.top = *o; |
327 | incr_top; | 329 | incr_top; |
328 | } | 330 | } |
329 | 331 | ||
330 | void lua_pushobject (lua_Object o) { | 332 | void lua_pushobject (lua_State *L, lua_Object o) { |
331 | if (o == LUA_NOOBJECT) | 333 | if (o == LUA_NOOBJECT) |
332 | lua_error("API error - attempt to push a NOOBJECT"); | 334 | lua_error(L, "API error - attempt to push a NOOBJECT"); |
333 | set_normalized(L->stack.top, Address(o)); | 335 | set_normalized(L->stack.top, Address(L, o)); |
334 | incr_top; | 336 | incr_top; |
335 | } | 337 | } |
336 | 338 | ||
337 | 339 | ||
338 | int lua_tag (lua_Object lo) { | 340 | int lua_tag (lua_State *L, lua_Object lo) { |
339 | if (lo == LUA_NOOBJECT) | 341 | if (lo == LUA_NOOBJECT) |
340 | return LUA_T_NIL; | 342 | return LUA_T_NIL; |
341 | else { | 343 | else { |
342 | const TObject *o = Address(lo); | 344 | const TObject *o = Address(L, lo); |
343 | int t; | 345 | int t; |
344 | switch (t = ttype(o)) { | 346 | switch (t = ttype(o)) { |
345 | case LUA_T_USERDATA: | 347 | case LUA_T_USERDATA: |
@@ -354,7 +356,7 @@ int lua_tag (lua_Object lo) { | |||
354 | return o->value.cl->consts[0].ttype; | 356 | return o->value.cl->consts[0].ttype; |
355 | #ifdef DEBUG | 357 | #ifdef DEBUG |
356 | case LUA_T_LINE: | 358 | case LUA_T_LINE: |
357 | LUA_INTERNALERROR("invalid type"); | 359 | LUA_INTERNALERROR(L, "invalid type"); |
358 | #endif | 360 | #endif |
359 | default: | 361 | default: |
360 | return t; | 362 | return t; |
@@ -363,9 +365,9 @@ int lua_tag (lua_Object lo) { | |||
363 | } | 365 | } |
364 | 366 | ||
365 | 367 | ||
366 | void lua_settag (int tag) { | 368 | void lua_settag (lua_State *L, int tag) { |
367 | checkCparams(1); | 369 | checkCparams(L, 1); |
368 | luaT_realtag(tag); | 370 | luaT_realtag(L, tag); |
369 | switch (ttype(L->stack.top-1)) { | 371 | switch (ttype(L->stack.top-1)) { |
370 | case LUA_T_ARRAY: | 372 | case LUA_T_ARRAY: |
371 | (L->stack.top-1)->value.a->htag = tag; | 373 | (L->stack.top-1)->value.a->htag = tag; |
@@ -374,20 +376,20 @@ void lua_settag (int tag) { | |||
374 | (L->stack.top-1)->value.ts->u.d.tag = tag; | 376 | (L->stack.top-1)->value.ts->u.d.tag = tag; |
375 | break; | 377 | break; |
376 | default: | 378 | default: |
377 | luaL_verror("cannot change the tag of a %.20s", | 379 | luaL_verror(L, "cannot change the tag of a %.20s", |
378 | luaO_typename(L->stack.top-1)); | 380 | luaO_typename(L, L->stack.top-1)); |
379 | } | 381 | } |
380 | L->stack.top--; | 382 | L->stack.top--; |
381 | } | 383 | } |
382 | 384 | ||
383 | 385 | ||
384 | GlobalVar *luaA_nextvar (TaggedString *ts) { | 386 | GlobalVar *luaA_nextvar (lua_State *L, TaggedString *ts) { |
385 | GlobalVar *gv; | 387 | GlobalVar *gv; |
386 | if (ts == NULL) | 388 | if (ts == NULL) |
387 | gv = L->rootglobal; /* first variable */ | 389 | gv = L->rootglobal; /* first variable */ |
388 | else { | 390 | else { |
389 | /* check whether name is in global var list */ | 391 | /* check whether name is in global var list */ |
390 | luaL_arg_check(ts->u.s.gv, 1, "variable name expected"); | 392 | luaL_arg_check(L, ts->u.s.gv, 1, "variable name expected"); |
391 | gv = ts->u.s.gv->next; /* get next */ | 393 | gv = ts->u.s.gv->next; /* get next */ |
392 | } | 394 | } |
393 | while (gv && gv->value.ttype == LUA_T_NIL) /* skip globals with nil */ | 395 | while (gv && gv->value.ttype == LUA_T_NIL) /* skip globals with nil */ |
@@ -395,33 +397,33 @@ GlobalVar *luaA_nextvar (TaggedString *ts) { | |||
395 | if (gv) { | 397 | if (gv) { |
396 | ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = gv->name; | 398 | ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = gv->name; |
397 | incr_top; | 399 | incr_top; |
398 | luaA_pushobject(&gv->value); | 400 | luaA_pushobject(L, &gv->value); |
399 | } | 401 | } |
400 | return gv; | 402 | return gv; |
401 | } | 403 | } |
402 | 404 | ||
403 | 405 | ||
404 | const char *lua_nextvar (const char *varname) { | 406 | const char *lua_nextvar (lua_State *L, const char *varname) { |
405 | TaggedString *ts = (varname == NULL) ? NULL : luaS_new(varname); | 407 | TaggedString *ts = (varname == NULL) ? NULL : luaS_new(L, varname); |
406 | GlobalVar *gv = luaA_nextvar(ts); | 408 | GlobalVar *gv = luaA_nextvar(L, ts); |
407 | if (gv) { | 409 | if (gv) { |
408 | top2LC(2); | 410 | top2LC(L, 2); |
409 | return gv->name->str; | 411 | return gv->name->str; |
410 | } | 412 | } |
411 | else { | 413 | else { |
412 | top2LC(0); | 414 | top2LC(L, 0); |
413 | return NULL; | 415 | return NULL; |
414 | } | 416 | } |
415 | } | 417 | } |
416 | 418 | ||
417 | 419 | ||
418 | int luaA_next (const Hash *t, int i) { | 420 | int luaA_next (lua_State *L, const Hash *t, int i) { |
419 | int tsize = t->size; | 421 | int tsize = t->size; |
420 | for (; i<tsize; i++) { | 422 | for (; i<tsize; i++) { |
421 | Node *n = node(t, i); | 423 | Node *n = node(L, t, i); |
422 | if (ttype(val(n)) != LUA_T_NIL) { | 424 | if (ttype(val(L, n)) != LUA_T_NIL) { |
423 | luaA_pushobject(key(n)); | 425 | luaA_pushobject(L, key(L, n)); |
424 | luaA_pushobject(val(n)); | 426 | luaA_pushobject(L, val(L, n)); |
425 | return i+1; /* index to be used next time */ | 427 | return i+1; /* index to be used next time */ |
426 | } | 428 | } |
427 | } | 429 | } |
@@ -429,12 +431,12 @@ int luaA_next (const Hash *t, int i) { | |||
429 | } | 431 | } |
430 | 432 | ||
431 | 433 | ||
432 | int lua_next (lua_Object o, int i) { | 434 | int lua_next (lua_State *L, lua_Object o, int i) { |
433 | const TObject *t = Address(o); | 435 | const TObject *t = Address(L, o); |
434 | if (ttype(t) != LUA_T_ARRAY) | 436 | if (ttype(t) != LUA_T_ARRAY) |
435 | lua_error("API error - object is not a table in `lua_next'"); | 437 | lua_error(L, "API error - object is not a table in `lua_next'"); |
436 | i = luaA_next(avalue(t), i); | 438 | i = luaA_next(L, avalue(t), i); |
437 | top2LC((i==0) ? 0 : 2); | 439 | top2LC(L, (i==0) ? 0 : 2); |
438 | return i; | 440 | return i; |
439 | } | 441 | } |
440 | 442 | ||
@@ -446,25 +448,21 @@ int lua_next (lua_Object o, int i) { | |||
446 | ** ======================================================= | 448 | ** ======================================================= |
447 | */ | 449 | */ |
448 | 450 | ||
449 | lua_State *lua_setstate (lua_State *st) { | ||
450 | lua_State *old = lua_state; | ||
451 | lua_state = st; | ||
452 | return old; | ||
453 | } | ||
454 | 451 | ||
455 | lua_LHFunction lua_setlinehook (lua_LHFunction func) { | 452 | |
453 | lua_LHFunction lua_setlinehook (lua_State *L, lua_LHFunction func) { | ||
456 | lua_LHFunction old = L->linehook; | 454 | lua_LHFunction old = L->linehook; |
457 | L->linehook = func; | 455 | L->linehook = func; |
458 | return old; | 456 | return old; |
459 | } | 457 | } |
460 | 458 | ||
461 | lua_CHFunction lua_setcallhook (lua_CHFunction func) { | 459 | lua_CHFunction lua_setcallhook (lua_State *L, lua_CHFunction func) { |
462 | lua_CHFunction old = L->callhook; | 460 | lua_CHFunction old = L->callhook; |
463 | L->callhook = func; | 461 | L->callhook = func; |
464 | return old; | 462 | return old; |
465 | } | 463 | } |
466 | 464 | ||
467 | int lua_setdebug (int debug) { | 465 | int lua_setdebug (lua_State *L, int debug) { |
468 | int old = L->debug; | 466 | int old = L->debug; |
469 | L->debug = debug; | 467 | L->debug = debug; |
470 | return old; | 468 | return old; |
@@ -480,44 +478,44 @@ int lua_setdebug (int debug) { | |||
480 | */ | 478 | */ |
481 | 479 | ||
482 | 480 | ||
483 | lua_Function lua_stackedfunction (int level) { | 481 | lua_Function lua_stackedfunction (lua_State *L, int level) { |
484 | StkId i; | 482 | StkId i; |
485 | for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--) { | 483 | for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--) { |
486 | int t = L->stack.stack[i].ttype; | 484 | int t = L->stack.stack[i].ttype; |
487 | if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK) | 485 | if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK) |
488 | if (level-- == 0) | 486 | if (level-- == 0) |
489 | return Ref(L->stack.stack+i); | 487 | return Ref(L, L->stack.stack+i); |
490 | } | 488 | } |
491 | return LUA_NOOBJECT; | 489 | return LUA_NOOBJECT; |
492 | } | 490 | } |
493 | 491 | ||
494 | 492 | ||
495 | int lua_nups (lua_Function func) { | 493 | int lua_nups (lua_State *L, lua_Function func) { |
496 | const TObject *o = luaA_Address(func); | 494 | const TObject *o = luaA_Address(L, func); |
497 | return (!o || normalized_type(o) != LUA_T_CLOSURE) ? 0 : o->value.cl->nelems; | 495 | return (!o || normalized_type(o) != LUA_T_CLOSURE) ? 0 : o->value.cl->nelems; |
498 | } | 496 | } |
499 | 497 | ||
500 | 498 | ||
501 | int lua_currentline (lua_Function func) { | 499 | int lua_currentline (lua_State *L, lua_Function func) { |
502 | const TObject *f = Address(func); | 500 | const TObject *f = Address(L, func); |
503 | return (f+1 < L->stack.top && (f+1)->ttype == LUA_T_LINE) ? | 501 | return (f+1 < L->stack.top && (f+1)->ttype == LUA_T_LINE) ? |
504 | (f+1)->value.i : -1; | 502 | (f+1)->value.i : -1; |
505 | } | 503 | } |
506 | 504 | ||
507 | 505 | ||
508 | lua_Object lua_getlocal (lua_Function func, int local_number, | 506 | lua_Object lua_getlocal (lua_State *L, lua_Function func, int local_number, |
509 | const char **name) { | 507 | const char **name) { |
510 | /* check whether func is a Lua function */ | 508 | /* check whether func is a Lua function */ |
511 | if (lua_tag(func) != LUA_T_PROTO) | 509 | if (lua_tag(L, func) != LUA_T_PROTO) |
512 | return LUA_NOOBJECT; | 510 | return LUA_NOOBJECT; |
513 | else { | 511 | else { |
514 | TObject *f = Address(func); | 512 | TObject *f = Address(L, func); |
515 | TProtoFunc *fp = luaA_protovalue(f)->value.tf; | 513 | TProtoFunc *fp = luaA_protovalue(f)->value.tf; |
516 | *name = luaF_getlocalname(fp, local_number, lua_currentline(func)); | 514 | *name = luaF_getlocalname(fp, local_number, lua_currentline(L, func)); |
517 | if (*name) { | 515 | if (*name) { |
518 | /* if "*name", there must be a LUA_T_LINE */ | 516 | /* if "*name", there must be a LUA_T_LINE */ |
519 | /* therefore, f+2 points to function base */ | 517 | /* therefore, f+2 points to function base */ |
520 | return put_luaObject((f+2)+(local_number-1)); | 518 | return put_luaObject(L, (f+2)+(local_number-1)); |
521 | } | 519 | } |
522 | else | 520 | else |
523 | return LUA_NOOBJECT; | 521 | return LUA_NOOBJECT; |
@@ -525,15 +523,16 @@ lua_Object lua_getlocal (lua_Function func, int local_number, | |||
525 | } | 523 | } |
526 | 524 | ||
527 | 525 | ||
528 | int lua_setlocal (lua_Function func, int local_number) { | 526 | int lua_setlocal (lua_State *L, lua_Function func, int local_number) { |
529 | /* check whether func is a Lua function */ | 527 | /* check whether func is a Lua function */ |
530 | if (lua_tag(func) != LUA_T_PROTO) | 528 | if (lua_tag(L, func) != LUA_T_PROTO) |
531 | return 0; | 529 | return 0; |
532 | else { | 530 | else { |
533 | TObject *f = Address(func); | 531 | TObject *f = Address(L, func); |
534 | TProtoFunc *fp = luaA_protovalue(f)->value.tf; | 532 | TProtoFunc *fp = luaA_protovalue(f)->value.tf; |
535 | const char *name = luaF_getlocalname(fp, local_number, lua_currentline(func)); | 533 | const char *name = luaF_getlocalname(fp, local_number, |
536 | checkCparams(1); | 534 | lua_currentline(L, func)); |
535 | checkCparams(L, 1); | ||
537 | --L->stack.top; | 536 | --L->stack.top; |
538 | if (name) { | 537 | if (name) { |
539 | /* if "name", there must be a LUA_T_LINE */ | 538 | /* if "name", there must be a LUA_T_LINE */ |
@@ -547,11 +546,12 @@ int lua_setlocal (lua_Function func, int local_number) { | |||
547 | } | 546 | } |
548 | 547 | ||
549 | 548 | ||
550 | void lua_funcinfo (lua_Object func, const char **source, int *linedefined) { | 549 | void lua_funcinfo (lua_State *L, lua_Object func, |
551 | if (!lua_isfunction(func)) | 550 | const char **source, int *linedefined) { |
552 | lua_error("API error - `funcinfo' called with a non-function value"); | 551 | if (!lua_isfunction(L, func)) |
552 | lua_error(L, "API error - `funcinfo' called with a non-function value"); | ||
553 | else { | 553 | else { |
554 | const TObject *f = luaA_protovalue(Address(func)); | 554 | const TObject *f = luaA_protovalue(Address(L, func)); |
555 | if (normalized_type(f) == LUA_T_PROTO) { | 555 | if (normalized_type(f) == LUA_T_PROTO) { |
556 | *source = tfvalue(f)->source->str; | 556 | *source = tfvalue(f)->source->str; |
557 | *linedefined = tfvalue(f)->lineDefined; | 557 | *linedefined = tfvalue(f)->lineDefined; |
@@ -564,23 +564,23 @@ void lua_funcinfo (lua_Object func, const char **source, int *linedefined) { | |||
564 | } | 564 | } |
565 | 565 | ||
566 | 566 | ||
567 | static int checkfunc (TObject *o) { | 567 | static int checkfunc (lua_State *L, TObject *o) { |
568 | return luaO_equalObj(o, L->stack.top); | 568 | return luaO_equalObj(o, L->stack.top); |
569 | } | 569 | } |
570 | 570 | ||
571 | 571 | ||
572 | const char *lua_getobjname (lua_Object o, const char **name) { | 572 | const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) { |
573 | /* try to find a name for given function */ | 573 | /* try to find a name for given function */ |
574 | GlobalVar *g; | 574 | GlobalVar *g; |
575 | set_normalized(L->stack.top, Address(o)); /* to be accessed by "checkfunc" */ | 575 | set_normalized(L->stack.top, Address(L, o)); /* to be used by `checkfunc' */ |
576 | for (g=L->rootglobal; g; g=g->next) { | 576 | for (g=L->rootglobal; g; g=g->next) { |
577 | if (checkfunc(&g->value)) { | 577 | if (checkfunc(L, &g->value)) { |
578 | *name = g->name->str; | 578 | *name = g->name->str; |
579 | return "global"; | 579 | return "global"; |
580 | } | 580 | } |
581 | } | 581 | } |
582 | /* not found: try tag methods */ | 582 | /* not found: try tag methods */ |
583 | if ((*name = luaT_travtagmethods(checkfunc)) != NULL) | 583 | if ((*name = luaT_travtagmethods(L, checkfunc)) != NULL) |
584 | return "tag-method"; | 584 | return "tag-method"; |
585 | else return ""; /* not found at all */ | 585 | else return ""; /* not found at all */ |
586 | } | 586 | } |
@@ -600,34 +600,34 @@ const char *lua_getobjname (lua_Object o, const char **name) { | |||
600 | #endif | 600 | #endif |
601 | 601 | ||
602 | 602 | ||
603 | void lua_beginblock (void) { | 603 | void lua_beginblock (lua_State *L) { |
604 | luaM_growvector(L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack, | 604 | luaM_growvector(L, L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack, |
605 | "too many nested blocks", MAX_C_BLOCKS); | 605 | "too many nested blocks", MAX_C_BLOCKS); |
606 | L->Cblocks[L->numCblocks] = L->Cstack; | 606 | L->Cblocks[L->numCblocks] = L->Cstack; |
607 | L->numCblocks++; | 607 | L->numCblocks++; |
608 | } | 608 | } |
609 | 609 | ||
610 | void lua_endblock (void) { | 610 | void lua_endblock (lua_State *L) { |
611 | --L->numCblocks; | 611 | --L->numCblocks; |
612 | L->Cstack = L->Cblocks[L->numCblocks]; | 612 | L->Cstack = L->Cblocks[L->numCblocks]; |
613 | luaD_adjusttop(L->Cstack.base); | 613 | luaD_adjusttop(L, L->Cstack.base); |
614 | } | 614 | } |
615 | 615 | ||
616 | 616 | ||
617 | 617 | ||
618 | int lua_ref (int lock) { | 618 | int lua_ref (lua_State *L, int lock) { |
619 | int ref; | 619 | int ref; |
620 | checkCparams(1); | 620 | checkCparams(L, 1); |
621 | ref = luaR_ref(L->stack.top-1, lock); | 621 | ref = luaR_ref(L, L->stack.top-1, lock); |
622 | L->stack.top--; | 622 | L->stack.top--; |
623 | return ref; | 623 | return ref; |
624 | } | 624 | } |
625 | 625 | ||
626 | 626 | ||
627 | 627 | ||
628 | lua_Object lua_getref (int ref) { | 628 | lua_Object lua_getref (lua_State *L, int ref) { |
629 | const TObject *o = luaR_getref(ref); | 629 | const TObject *o = luaR_getref(L, ref); |
630 | return (o ? put_luaObject(o) : LUA_NOOBJECT); | 630 | return (o ? put_luaObject(L, o) : LUA_NOOBJECT); |
631 | } | 631 | } |
632 | 632 | ||
633 | /* }====================================================== */ | 633 | /* }====================================================== */ |