diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-11-22 11:12:07 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-11-22 11:12:07 -0200 |
commit | 29ede6aa13144ff7b69c57a87be1ee93f57ae896 (patch) | |
tree | adcfb5dcff7db55481cd675349e23dec0e63c939 /lbuiltin.c | |
parent | 951897c09319ae5474a4b86bb7d615136577caa0 (diff) | |
download | lua-29ede6aa13144ff7b69c57a87be1ee93f57ae896.tar.gz lua-29ede6aa13144ff7b69c57a87be1ee93f57ae896.tar.bz2 lua-29ede6aa13144ff7b69c57a87be1ee93f57ae896.zip |
first implementation of multiple states (reentrant code).
Diffstat (limited to 'lbuiltin.c')
-rw-r--r-- | lbuiltin.c | 591 |
1 files changed, 295 insertions, 296 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbuiltin.c,v 1.72 1999/11/11 17:02:40 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.73 1999/11/16 12:50:48 roberto Exp roberto $ |
3 | ** Built-in functions | 3 | ** Built-in functions |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -10,6 +10,8 @@ | |||
10 | #include <stdlib.h> | 10 | #include <stdlib.h> |
11 | #include <string.h> | 11 | #include <string.h> |
12 | 12 | ||
13 | #define LUA_REENTRANT | ||
14 | |||
13 | #include "lapi.h" | 15 | #include "lapi.h" |
14 | #include "lauxlib.h" | 16 | #include "lauxlib.h" |
15 | #include "lbuiltin.h" | 17 | #include "lbuiltin.h" |
@@ -34,7 +36,7 @@ | |||
34 | */ | 36 | */ |
35 | 37 | ||
36 | 38 | ||
37 | static void pushtagstring (TaggedString *s) { | 39 | static void pushtagstring (lua_State *L, TaggedString *s) { |
38 | ttype(L->stack.top) = LUA_T_STRING; | 40 | ttype(L->stack.top) = LUA_T_STRING; |
39 | tsvalue(L->stack.top) = s; | 41 | tsvalue(L->stack.top) = s; |
40 | incr_top; | 42 | incr_top; |
@@ -46,29 +48,29 @@ static real getsize (const Hash *h) { | |||
46 | int i = h->size; | 48 | int i = h->size; |
47 | Node *n = h->node; | 49 | Node *n = h->node; |
48 | while (i--) { | 50 | while (i--) { |
49 | if (ttype(key(n)) == LUA_T_NUMBER && | 51 | if (ttype(key(L, n)) == LUA_T_NUMBER && |
50 | ttype(val(n)) != LUA_T_NIL && | 52 | ttype(val(L, n)) != LUA_T_NIL && |
51 | nvalue(key(n)) > max) | 53 | nvalue(key(L, n)) > max) |
52 | max = nvalue(key(n)); | 54 | max = nvalue(key(L, n)); |
53 | n++; | 55 | n++; |
54 | } | 56 | } |
55 | return max; | 57 | return max; |
56 | } | 58 | } |
57 | 59 | ||
58 | 60 | ||
59 | static real getnarg (const Hash *a) { | 61 | static real getnarg (lua_State *L, const Hash *a) { |
60 | TObject index; | 62 | TObject index; |
61 | const TObject *value; | 63 | const TObject *value; |
62 | /* value = table.n */ | 64 | /* value = table.n */ |
63 | ttype(&index) = LUA_T_STRING; | 65 | ttype(&index) = LUA_T_STRING; |
64 | tsvalue(&index) = luaS_new("n"); | 66 | tsvalue(&index) = luaS_new(L, "n"); |
65 | value = luaH_get(a, &index); | 67 | value = luaH_get(L, a, &index); |
66 | return (ttype(value) == LUA_T_NUMBER) ? nvalue(value) : getsize(a); | 68 | return (ttype(value) == LUA_T_NUMBER) ? nvalue(value) : getsize(a); |
67 | } | 69 | } |
68 | 70 | ||
69 | 71 | ||
70 | static Hash *gettable (int arg) { | 72 | static Hash *gettable (lua_State *L, int arg) { |
71 | return avalue(luaA_Address(luaL_tablearg(arg))); | 73 | return avalue(luaA_Address(L, luaL_tablearg(L, arg))); |
72 | } | 74 | } |
73 | 75 | ||
74 | /* }====================================================== */ | 76 | /* }====================================================== */ |
@@ -85,8 +87,8 @@ static Hash *gettable (int arg) { | |||
85 | ** If your system does not support "stderr", redefine this function, or | 87 | ** If your system does not support "stderr", redefine this function, or |
86 | ** redefine _ERRORMESSAGE so that it won't need _ALERT. | 88 | ** redefine _ERRORMESSAGE so that it won't need _ALERT. |
87 | */ | 89 | */ |
88 | static void luaB_alert (void) { | 90 | static void luaB_alert (lua_State *L) { |
89 | fputs(luaL_check_string(1), stderr); | 91 | fputs(luaL_check_string(L, 1), stderr); |
90 | } | 92 | } |
91 | 93 | ||
92 | 94 | ||
@@ -94,13 +96,13 @@ static void luaB_alert (void) { | |||
94 | ** Standard implementation of _ERRORMESSAGE. | 96 | ** Standard implementation of _ERRORMESSAGE. |
95 | ** The library "iolib" redefines _ERRORMESSAGE for better error information. | 97 | ** The library "iolib" redefines _ERRORMESSAGE for better error information. |
96 | */ | 98 | */ |
97 | static void error_message (void) { | 99 | static void error_message (lua_State *L) { |
98 | lua_Object al = lua_rawgetglobal("_ALERT"); | 100 | lua_Object al = lua_rawgetglobal(L, "_ALERT"); |
99 | if (lua_isfunction(al)) { /* avoid error loop if _ALERT is not defined */ | 101 | if (lua_isfunction(L, al)) { /* avoid error loop if _ALERT is not defined */ |
100 | char buff[600]; | 102 | char buff[600]; |
101 | sprintf(buff, "lua error: %.500s\n", luaL_check_string(1)); | 103 | sprintf(buff, "lua error: %.500s\n", luaL_check_string(L, 1)); |
102 | lua_pushstring(buff); | 104 | lua_pushstring(L, buff); |
103 | lua_callfunction(al); | 105 | lua_callfunction(L, al); |
104 | } | 106 | } |
105 | } | 107 | } |
106 | 108 | ||
@@ -115,142 +117,142 @@ static void error_message (void) { | |||
115 | #define MAXPRINT 40 /* arbitrary limit */ | 117 | #define MAXPRINT 40 /* arbitrary limit */ |
116 | #endif | 118 | #endif |
117 | 119 | ||
118 | static void luaB_print (void) { | 120 | static void luaB_print (lua_State *L) { |
119 | lua_Object args[MAXPRINT]; | 121 | lua_Object args[MAXPRINT]; |
120 | lua_Object obj; | 122 | lua_Object obj; |
121 | int n = 0; | 123 | int n = 0; |
122 | int i; | 124 | int i; |
123 | while ((obj = lua_getparam(n+1)) != LUA_NOOBJECT) { | 125 | while ((obj = lua_getparam(L, n+1)) != LUA_NOOBJECT) { |
124 | luaL_arg_check(n < MAXPRINT, n+1, "too many arguments"); | 126 | luaL_arg_check(L, n < MAXPRINT, n+1, "too many arguments"); |
125 | args[n++] = obj; | 127 | args[n++] = obj; |
126 | } | 128 | } |
127 | for (i=0; i<n; i++) { | 129 | for (i=0; i<n; i++) { |
128 | lua_pushobject(args[i]); | 130 | lua_pushobject(L, args[i]); |
129 | if (lua_call("tostring")) | 131 | if (lua_call(L, "tostring")) |
130 | lua_error("error in `tostring' called by `print'"); | 132 | lua_error(L, "error in `tostring' called by `print'"); |
131 | obj = lua_getresult(1); | 133 | obj = lua_getresult(L, 1); |
132 | if (!lua_isstring(obj)) | 134 | if (!lua_isstring(L, obj)) |
133 | lua_error("`tostring' must return a string to `print'"); | 135 | lua_error(L, "`tostring' must return a string to `print'"); |
134 | if (i>0) fputs("\t", stdout); | 136 | if (i>0) fputs("\t", stdout); |
135 | fputs(lua_getstring(obj), stdout); | 137 | fputs(lua_getstring(L, obj), stdout); |
136 | } | 138 | } |
137 | fputs("\n", stdout); | 139 | fputs("\n", stdout); |
138 | } | 140 | } |
139 | 141 | ||
140 | 142 | ||
141 | static void luaB_tonumber (void) { | 143 | static void luaB_tonumber (lua_State *L) { |
142 | int base = luaL_opt_int(2, 10); | 144 | int base = luaL_opt_int(L, 2, 10); |
143 | if (base == 10) { /* standard conversion */ | 145 | if (base == 10) { /* standard conversion */ |
144 | lua_Object o = lua_getparam(1); | 146 | lua_Object o = lua_getparam(L, 1); |
145 | if (lua_isnumber(o)) lua_pushnumber(lua_getnumber(o)); | 147 | if (lua_isnumber(L, o)) lua_pushnumber(L, lua_getnumber(L, o)); |
146 | else lua_pushnil(); /* not a number */ | 148 | else lua_pushnil(L); /* not a number */ |
147 | } | 149 | } |
148 | else { | 150 | else { |
149 | const char *s1 = luaL_check_string(1); | 151 | const char *s1 = luaL_check_string(L, 1); |
150 | char *s2; | 152 | char *s2; |
151 | real n; | 153 | real n; |
152 | luaL_arg_check(0 <= base && base <= 36, 2, "base out of range"); | 154 | luaL_arg_check(L, 0 <= base && base <= 36, 2, "base out of range"); |
153 | n = strtoul(s1, &s2, base); | 155 | n = strtoul(s1, &s2, base); |
154 | if (s1 == s2) return; /* no valid digits: return nil */ | 156 | if (s1 == s2) return; /* no valid digits: return nil */ |
155 | while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */ | 157 | while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */ |
156 | if (*s2) return; /* invalid trailing character: return nil */ | 158 | if (*s2) return; /* invalid trailing character: return nil */ |
157 | lua_pushnumber(n); | 159 | lua_pushnumber(L, n); |
158 | } | 160 | } |
159 | } | 161 | } |
160 | 162 | ||
161 | 163 | ||
162 | static void luaB_error (void) { | 164 | static void luaB_error (lua_State *L) { |
163 | lua_error(lua_getstring(lua_getparam(1))); | 165 | lua_error(L, lua_getstring(L, lua_getparam(L, 1))); |
164 | } | 166 | } |
165 | 167 | ||
166 | static void luaB_setglobal (void) { | 168 | static void luaB_setglobal (lua_State *L) { |
167 | const char *n = luaL_check_string(1); | 169 | const char *n = luaL_check_string(L, 1); |
168 | lua_Object value = luaL_nonnullarg(2); | 170 | lua_Object value = luaL_nonnullarg(L, 2); |
169 | lua_pushobject(value); | 171 | lua_pushobject(L, value); |
170 | lua_setglobal(n); | 172 | lua_setglobal(L, n); |
171 | lua_pushobject(value); /* return given value */ | 173 | lua_pushobject(L, value); /* return given value */ |
172 | } | 174 | } |
173 | 175 | ||
174 | static void luaB_rawsetglobal (void) { | 176 | static void luaB_rawsetglobal (lua_State *L) { |
175 | const char *n = luaL_check_string(1); | 177 | const char *n = luaL_check_string(L, 1); |
176 | lua_Object value = luaL_nonnullarg(2); | 178 | lua_Object value = luaL_nonnullarg(L, 2); |
177 | lua_pushobject(value); | 179 | lua_pushobject(L, value); |
178 | lua_rawsetglobal(n); | 180 | lua_rawsetglobal(L, n); |
179 | lua_pushobject(value); /* return given value */ | 181 | lua_pushobject(L, value); /* return given value */ |
180 | } | 182 | } |
181 | 183 | ||
182 | static void luaB_getglobal (void) { | 184 | static void luaB_getglobal (lua_State *L) { |
183 | lua_pushobject(lua_getglobal(luaL_check_string(1))); | 185 | lua_pushobject(L, lua_getglobal(L, luaL_check_string(L, 1))); |
184 | } | 186 | } |
185 | 187 | ||
186 | static void luaB_rawgetglobal (void) { | 188 | static void luaB_rawgetglobal (lua_State *L) { |
187 | lua_pushobject(lua_rawgetglobal(luaL_check_string(1))); | 189 | lua_pushobject(L, lua_rawgetglobal(L, luaL_check_string(L, 1))); |
188 | } | 190 | } |
189 | 191 | ||
190 | static void luaB_luatag (void) { | 192 | static void luaB_luatag (lua_State *L) { |
191 | lua_pushnumber(lua_tag(lua_getparam(1))); | 193 | lua_pushnumber(L, lua_tag(L, lua_getparam(L, 1))); |
192 | } | 194 | } |
193 | 195 | ||
194 | static void luaB_settag (void) { | 196 | static void luaB_settag (lua_State *L) { |
195 | lua_Object o = luaL_tablearg(1); | 197 | lua_Object o = luaL_tablearg(L, 1); |
196 | lua_pushobject(o); | 198 | lua_pushobject(L, o); |
197 | lua_settag(luaL_check_int(2)); | 199 | lua_settag(L, luaL_check_int(L, 2)); |
198 | lua_pushobject(o); /* return first argument */ | 200 | lua_pushobject(L, o); /* return first argument */ |
199 | } | 201 | } |
200 | 202 | ||
201 | static void luaB_newtag (void) { | 203 | static void luaB_newtag (lua_State *L) { |
202 | lua_pushnumber(lua_newtag()); | 204 | lua_pushnumber(L, lua_newtag(L)); |
203 | } | 205 | } |
204 | 206 | ||
205 | static void luaB_copytagmethods (void) { | 207 | static void luaB_copytagmethods (lua_State *L) { |
206 | lua_pushnumber(lua_copytagmethods(luaL_check_int(1), | 208 | lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1), |
207 | luaL_check_int(2))); | 209 | luaL_check_int(L, 2))); |
208 | } | 210 | } |
209 | 211 | ||
210 | static void luaB_rawgettable (void) { | 212 | static void luaB_rawgettable (lua_State *L) { |
211 | lua_pushobject(luaL_nonnullarg(1)); | 213 | lua_pushobject(L, luaL_nonnullarg(L, 1)); |
212 | lua_pushobject(luaL_nonnullarg(2)); | 214 | lua_pushobject(L, luaL_nonnullarg(L, 2)); |
213 | lua_pushobject(lua_rawgettable()); | 215 | lua_pushobject(L, lua_rawgettable(L)); |
214 | } | 216 | } |
215 | 217 | ||
216 | static void luaB_rawsettable (void) { | 218 | static void luaB_rawsettable (lua_State *L) { |
217 | lua_pushobject(luaL_nonnullarg(1)); | 219 | lua_pushobject(L, luaL_nonnullarg(L, 1)); |
218 | lua_pushobject(luaL_nonnullarg(2)); | 220 | lua_pushobject(L, luaL_nonnullarg(L, 2)); |
219 | lua_pushobject(luaL_nonnullarg(3)); | 221 | lua_pushobject(L, luaL_nonnullarg(L, 3)); |
220 | lua_rawsettable(); | 222 | lua_rawsettable(L); |
221 | } | 223 | } |
222 | 224 | ||
223 | static void luaB_settagmethod (void) { | 225 | static void luaB_settagmethod (lua_State *L) { |
224 | int tag = luaL_check_int(1); | 226 | int tag = luaL_check_int(L, 1); |
225 | const char *event = luaL_check_string(2); | 227 | const char *event = luaL_check_string(L, 2); |
226 | lua_Object nf = luaL_nonnullarg(3); | 228 | lua_Object nf = luaL_nonnullarg(L, 3); |
227 | #ifndef LUA_COMPAT_GC | 229 | #ifndef LUA_COMPAT_GC |
228 | if (strcmp(event, "gc") == 0 && tag != LUA_T_NIL) | 230 | if (strcmp(event, "gc") == 0 && tag != LUA_T_NIL) |
229 | lua_error("cannot set this tag method from Lua"); | 231 | lua_error(L, "cannot set this tag method from Lua"); |
230 | #endif | 232 | #endif |
231 | lua_pushobject(nf); | 233 | lua_pushobject(L, nf); |
232 | lua_pushobject(lua_settagmethod(tag, event)); | 234 | lua_pushobject(L, lua_settagmethod(L, tag, event)); |
233 | } | 235 | } |
234 | 236 | ||
235 | static void luaB_gettagmethod (void) { | 237 | static void luaB_gettagmethod (lua_State *L) { |
236 | lua_pushobject(lua_gettagmethod(luaL_check_int(1), luaL_check_string(2))); | 238 | lua_pushobject(L, lua_gettagmethod(L, luaL_check_int(L, 1), luaL_check_string(L, 2))); |
237 | } | 239 | } |
238 | 240 | ||
239 | static void luaB_seterrormethod (void) { | 241 | static void luaB_seterrormethod (lua_State *L) { |
240 | lua_Object nf = luaL_functionarg(1); | 242 | lua_Object nf = luaL_functionarg(L, 1); |
241 | lua_pushobject(nf); | 243 | lua_pushobject(L, nf); |
242 | lua_pushobject(lua_seterrormethod()); | 244 | lua_pushobject(L, lua_seterrormethod(L)); |
243 | } | 245 | } |
244 | 246 | ||
245 | static void luaB_collectgarbage (void) { | 247 | static void luaB_collectgarbage (lua_State *L) { |
246 | lua_pushnumber(lua_collectgarbage(luaL_opt_int(1, 0))); | 248 | lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0))); |
247 | } | 249 | } |
248 | 250 | ||
249 | 251 | ||
250 | static void luaB_type (void) { | 252 | static void luaB_type (lua_State *L) { |
251 | lua_Object o = luaL_nonnullarg(1); | 253 | lua_Object o = luaL_nonnullarg(L, 1); |
252 | lua_pushstring(lua_type(o)); | 254 | lua_pushstring(L, lua_type(L, o)); |
253 | lua_pushnumber(lua_tag(o)); | 255 | lua_pushnumber(L, lua_tag(L, o)); |
254 | } | 256 | } |
255 | 257 | ||
256 | /* }====================================================== */ | 258 | /* }====================================================== */ |
@@ -264,62 +266,62 @@ static void luaB_type (void) { | |||
264 | */ | 266 | */ |
265 | 267 | ||
266 | 268 | ||
267 | static void passresults (void) { | 269 | static void passresults (lua_State *L) { |
268 | L->Cstack.base = L->Cstack.lua2C; /* position of first result */ | 270 | L->Cstack.base = L->Cstack.lua2C; /* position of first result */ |
269 | if (L->Cstack.num == 0) | 271 | if (L->Cstack.num == 0) |
270 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ | 272 | lua_pushuserdata(L, NULL); /* at least one result to signal no errors */ |
271 | } | 273 | } |
272 | 274 | ||
273 | static void luaB_dostring (void) { | 275 | static void luaB_dostring (lua_State *L) { |
274 | long l; | 276 | long l; |
275 | const char *s = luaL_check_lstr(1, &l); | 277 | const char *s = luaL_check_lstr(L, 1, &l); |
276 | if (*s == ID_CHUNK) | 278 | if (*s == ID_CHUNK) |
277 | lua_error("`dostring' cannot run pre-compiled code"); | 279 | lua_error(L, "`dostring' cannot run pre-compiled code"); |
278 | if (lua_dobuffer(s, l, luaL_opt_string(2, s)) == 0) | 280 | if (lua_dobuffer(L, s, l, luaL_opt_string(L, 2, s)) == 0) |
279 | passresults(); | 281 | passresults(L); |
280 | /* else return no value */ | 282 | /* else return no value */ |
281 | } | 283 | } |
282 | 284 | ||
283 | 285 | ||
284 | static void luaB_dofile (void) { | 286 | static void luaB_dofile (lua_State *L) { |
285 | const char *fname = luaL_opt_string(1, NULL); | 287 | const char *fname = luaL_opt_string(L, 1, NULL); |
286 | if (lua_dofile(fname) == 0) | 288 | if (lua_dofile(L, fname) == 0) |
287 | passresults(); | 289 | passresults(L); |
288 | /* else return no value */ | 290 | /* else return no value */ |
289 | } | 291 | } |
290 | 292 | ||
291 | 293 | ||
292 | static void luaB_call (void) { | 294 | static void luaB_call (lua_State *L) { |
293 | lua_Object f = luaL_nonnullarg(1); | 295 | lua_Object f = luaL_nonnullarg(L, 1); |
294 | const Hash *arg = gettable(2); | 296 | const Hash *arg = gettable(L, 2); |
295 | const char *options = luaL_opt_string(3, ""); | 297 | const char *options = luaL_opt_string(L, 3, ""); |
296 | lua_Object err = lua_getparam(4); | 298 | lua_Object err = lua_getparam(L, 4); |
297 | int narg = (int)getnarg(arg); | 299 | int narg = (int)getnarg(L, arg); |
298 | int i, status; | 300 | int i, status; |
299 | if (err != LUA_NOOBJECT) { /* set new error method */ | 301 | if (err != LUA_NOOBJECT) { /* set new error method */ |
300 | lua_pushobject(err); | 302 | lua_pushobject(L, err); |
301 | err = lua_seterrormethod(); | 303 | err = lua_seterrormethod(L); |
302 | } | 304 | } |
303 | /* push arg[1...n] */ | 305 | /* push arg[1...n] */ |
304 | luaD_checkstack(narg); | 306 | luaD_checkstack(L, narg); |
305 | for (i=0; i<narg; i++) | 307 | for (i=0; i<narg; i++) |
306 | *(L->stack.top++) = *luaH_getint(arg, i+1); | 308 | *(L->stack.top++) = *luaH_getint(L, arg, i+1); |
307 | status = lua_callfunction(f); | 309 | status = lua_callfunction(L, f); |
308 | if (err != LUA_NOOBJECT) { /* restore old error method */ | 310 | if (err != LUA_NOOBJECT) { /* restore old error method */ |
309 | lua_pushobject(err); | 311 | lua_pushobject(L, err); |
310 | lua_seterrormethod(); | 312 | lua_seterrormethod(L); |
311 | } | 313 | } |
312 | if (status != 0) { /* error in call? */ | 314 | if (status != 0) { /* error in call? */ |
313 | if (strchr(options, 'x')) { | 315 | if (strchr(options, 'x')) { |
314 | lua_pushnil(); | 316 | lua_pushnil(L); |
315 | return; /* return nil to signal the error */ | 317 | return; /* return nil to signal the error */ |
316 | } | 318 | } |
317 | else | 319 | else |
318 | lua_error(NULL); | 320 | lua_error(L, NULL); |
319 | } | 321 | } |
320 | else { /* no errors */ | 322 | else { /* no errors */ |
321 | if (strchr(options, 'p')) { /* pack results? */ | 323 | if (strchr(options, 'p')) { /* pack results? */ |
322 | luaV_pack(L->Cstack.lua2C, L->Cstack.num, L->stack.top); | 324 | luaV_pack(L, L->Cstack.lua2C, L->Cstack.num, L->stack.top); |
323 | incr_top; | 325 | incr_top; |
324 | } | 326 | } |
325 | else | 327 | else |
@@ -328,45 +330,45 @@ static void luaB_call (void) { | |||
328 | } | 330 | } |
329 | 331 | ||
330 | 332 | ||
331 | static void luaB_nextvar (void) { | 333 | static void luaB_nextvar (lua_State *L) { |
332 | const TObject *o = luaA_Address(luaL_nonnullarg(1)); | 334 | const TObject *o = luaA_Address(L, luaL_nonnullarg(L, 1)); |
333 | TaggedString *g; | 335 | TaggedString *g; |
334 | if (ttype(o) == LUA_T_NIL) | 336 | if (ttype(o) == LUA_T_NIL) |
335 | g = NULL; | 337 | g = NULL; |
336 | else { | 338 | else { |
337 | luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected"); | 339 | luaL_arg_check(L, ttype(o) == LUA_T_STRING, 1, "variable name expected"); |
338 | g = tsvalue(o); | 340 | g = tsvalue(o); |
339 | } | 341 | } |
340 | if (!luaA_nextvar(g)) | 342 | if (!luaA_nextvar(L, g)) |
341 | lua_pushnil(); | 343 | lua_pushnil(L); |
342 | } | 344 | } |
343 | 345 | ||
344 | 346 | ||
345 | static void luaB_next (void) { | 347 | static void luaB_next (lua_State *L) { |
346 | const Hash *a = gettable(1); | 348 | const Hash *a = gettable(L, 1); |
347 | const TObject *k = luaA_Address(luaL_nonnullarg(2)); | 349 | const TObject *k = luaA_Address(L, luaL_nonnullarg(L, 2)); |
348 | int i; /* will get first element after `i' */ | 350 | int i; /* will get first element after `i' */ |
349 | if (ttype(k) == LUA_T_NIL) | 351 | if (ttype(k) == LUA_T_NIL) |
350 | i = 0; /* get first */ | 352 | i = 0; /* get first */ |
351 | else { | 353 | else { |
352 | i = luaH_pos(a, k)+1; | 354 | i = luaH_pos(L, a, k)+1; |
353 | luaL_arg_check(i != 0, 2, "key not found"); | 355 | luaL_arg_check(L, i != 0, 2, "key not found"); |
354 | } | 356 | } |
355 | if (luaA_next(a, i) == 0) | 357 | if (luaA_next(L, a, i) == 0) |
356 | lua_pushnil(); | 358 | lua_pushnil(L); |
357 | } | 359 | } |
358 | 360 | ||
359 | 361 | ||
360 | static void luaB_tostring (void) { | 362 | static void luaB_tostring (lua_State *L) { |
361 | lua_Object obj = lua_getparam(1); | 363 | lua_Object obj = lua_getparam(L, 1); |
362 | const TObject *o = luaA_Address(obj); | 364 | const TObject *o = luaA_Address(L, obj); |
363 | char buff[64]; | 365 | char buff[64]; |
364 | switch (ttype(o)) { | 366 | switch (ttype(o)) { |
365 | case LUA_T_NUMBER: | 367 | case LUA_T_NUMBER: |
366 | lua_pushstring(lua_getstring(obj)); | 368 | lua_pushstring(L, lua_getstring(L, obj)); |
367 | return; | 369 | return; |
368 | case LUA_T_STRING: | 370 | case LUA_T_STRING: |
369 | lua_pushobject(obj); | 371 | lua_pushobject(L, obj); |
370 | return; | 372 | return; |
371 | case LUA_T_ARRAY: | 373 | case LUA_T_ARRAY: |
372 | sprintf(buff, "table: %p", o->value.a); | 374 | sprintf(buff, "table: %p", o->value.a); |
@@ -384,12 +386,12 @@ static void luaB_tostring (void) { | |||
384 | sprintf(buff, "userdata: %p", o->value.ts->u.d.value); | 386 | sprintf(buff, "userdata: %p", o->value.ts->u.d.value); |
385 | break; | 387 | break; |
386 | case LUA_T_NIL: | 388 | case LUA_T_NIL: |
387 | lua_pushstring("nil"); | 389 | lua_pushstring(L, "nil"); |
388 | return; | 390 | return; |
389 | default: | 391 | default: |
390 | LUA_INTERNALERROR("invalid type"); | 392 | LUA_INTERNALERROR(L, "invalid type"); |
391 | } | 393 | } |
392 | lua_pushstring(buff); | 394 | lua_pushstring(L, buff); |
393 | } | 395 | } |
394 | 396 | ||
395 | /* }====================================================== */ | 397 | /* }====================================================== */ |
@@ -406,28 +408,28 @@ static void luaB_tostring (void) { | |||
406 | ** ======================================================= | 408 | ** ======================================================= |
407 | */ | 409 | */ |
408 | 410 | ||
409 | static void luaB_assert (void) { | 411 | static void luaB_assert (lua_State *L) { |
410 | lua_Object p = lua_getparam(1); | 412 | lua_Object p = lua_getparam(L, 1); |
411 | if (p == LUA_NOOBJECT || lua_isnil(p)) | 413 | if (p == LUA_NOOBJECT || lua_isnil(L, p)) |
412 | luaL_verror("assertion failed! %.90s", luaL_opt_string(2, "")); | 414 | luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, "")); |
413 | } | 415 | } |
414 | 416 | ||
415 | 417 | ||
416 | static void luaB_foreachi (void) { | 418 | static void luaB_foreachi (lua_State *L) { |
417 | const Hash *t = gettable(1); | 419 | const Hash *t = gettable(L, 1); |
418 | int i; | 420 | int i; |
419 | int n = (int)getnarg(t); | 421 | int n = (int)getnarg(L, t); |
420 | TObject f; | 422 | TObject f; |
421 | /* 'f' cannot be a pointer to TObject, because it is on the stack, and the | 423 | /* 'f' cannot be a pointer to TObject, because it is on the stack, and the |
422 | stack may be reallocated by the call. Moreover, some C compilers do not | 424 | stack may be reallocated by the call. Moreover, some C compilers do not |
423 | initialize structs, so we must do the assignment after the declaration */ | 425 | initialize structs, so we must do the assignment after the declaration */ |
424 | f = *luaA_Address(luaL_functionarg(2)); | 426 | f = *luaA_Address(L, luaL_functionarg(L, 2)); |
425 | luaD_checkstack(3); /* for f, key, and val */ | 427 | luaD_checkstack(L, 3); /* for f, key, and val */ |
426 | for (i=1; i<=n; i++) { | 428 | for (i=1; i<=n; i++) { |
427 | *(L->stack.top++) = f; | 429 | *(L->stack.top++) = f; |
428 | ttype(L->stack.top) = LUA_T_NUMBER; nvalue(L->stack.top++) = i; | 430 | ttype(L->stack.top) = LUA_T_NUMBER; nvalue(L->stack.top++) = i; |
429 | *(L->stack.top++) = *luaH_getint(t, i); | 431 | *(L->stack.top++) = *luaH_getint(L, t, i); |
430 | luaD_calln(2, 1); | 432 | luaD_calln(L, 2, 1); |
431 | if (ttype(L->stack.top-1) != LUA_T_NIL) | 433 | if (ttype(L->stack.top-1) != LUA_T_NIL) |
432 | return; | 434 | return; |
433 | L->stack.top--; | 435 | L->stack.top--; |
@@ -435,19 +437,19 @@ static void luaB_foreachi (void) { | |||
435 | } | 437 | } |
436 | 438 | ||
437 | 439 | ||
438 | static void luaB_foreach (void) { | 440 | static void luaB_foreach (lua_State *L) { |
439 | const Hash *a = gettable(1); | 441 | const Hash *a = gettable(L, 1); |
440 | int i; | 442 | int i; |
441 | TObject f; /* see comment in 'foreachi' */ | 443 | TObject f; /* see comment in 'foreachi' */ |
442 | f = *luaA_Address(luaL_functionarg(2)); | 444 | f = *luaA_Address(L, luaL_functionarg(L, 2)); |
443 | luaD_checkstack(3); /* for f, key, and val */ | 445 | luaD_checkstack(L, 3); /* for f, key, and val */ |
444 | for (i=0; i<a->size; i++) { | 446 | for (i=0; i<a->size; i++) { |
445 | const Node *nd = &(a->node[i]); | 447 | const Node *nd = &(a->node[i]); |
446 | if (ttype(val(nd)) != LUA_T_NIL) { | 448 | if (ttype(val(L, nd)) != LUA_T_NIL) { |
447 | *(L->stack.top++) = f; | 449 | *(L->stack.top++) = f; |
448 | *(L->stack.top++) = *key(nd); | 450 | *(L->stack.top++) = *key(L, nd); |
449 | *(L->stack.top++) = *val(nd); | 451 | *(L->stack.top++) = *val(L, nd); |
450 | luaD_calln(2, 1); | 452 | luaD_calln(L, 2, 1); |
451 | if (ttype(L->stack.top-1) != LUA_T_NIL) | 453 | if (ttype(L->stack.top-1) != LUA_T_NIL) |
452 | return; | 454 | return; |
453 | L->stack.top--; /* remove result */ | 455 | L->stack.top--; /* remove result */ |
@@ -456,18 +458,18 @@ static void luaB_foreach (void) { | |||
456 | } | 458 | } |
457 | 459 | ||
458 | 460 | ||
459 | static void luaB_foreachvar (void) { | 461 | static void luaB_foreachvar (lua_State *L) { |
460 | GlobalVar *gv; | 462 | GlobalVar *gv; |
461 | TObject f; /* see comment in 'foreachi' */ | 463 | TObject f; /* see comment in 'foreachi' */ |
462 | f = *luaA_Address(luaL_functionarg(1)); | 464 | f = *luaA_Address(L, luaL_functionarg(L, 1)); |
463 | luaD_checkstack(4); /* for extra var name, f, var name, and globalval */ | 465 | luaD_checkstack(L, 4); /* for extra var name, f, var name, and globalval */ |
464 | for (gv = L->rootglobal; gv; gv = gv->next) { | 466 | for (gv = L->rootglobal; gv; gv = gv->next) { |
465 | if (gv->value.ttype != LUA_T_NIL) { | 467 | if (gv->value.ttype != LUA_T_NIL) { |
466 | pushtagstring(gv->name); /* keep (extra) name on stack to avoid GC */ | 468 | pushtagstring(L, gv->name); /* keep (extra) name on stack to avoid GC */ |
467 | *(L->stack.top++) = f; | 469 | *(L->stack.top++) = f; |
468 | pushtagstring(gv->name); | 470 | pushtagstring(L, gv->name); |
469 | *(L->stack.top++) = gv->value; | 471 | *(L->stack.top++) = gv->value; |
470 | luaD_calln(2, 1); | 472 | luaD_calln(L, 2, 1); |
471 | if (ttype(L->stack.top-1) != LUA_T_NIL) { | 473 | if (ttype(L->stack.top-1) != LUA_T_NIL) { |
472 | L->stack.top--; | 474 | L->stack.top--; |
473 | *(L->stack.top-1) = *L->stack.top; /* remove extra name */ | 475 | *(L->stack.top-1) = *L->stack.top; /* remove extra name */ |
@@ -479,39 +481,39 @@ static void luaB_foreachvar (void) { | |||
479 | } | 481 | } |
480 | 482 | ||
481 | 483 | ||
482 | static void luaB_getn (void) { | 484 | static void luaB_getn (lua_State *L) { |
483 | lua_pushnumber(getnarg(gettable(1))); | 485 | lua_pushnumber(L, getnarg(L, gettable(L, 1))); |
484 | } | 486 | } |
485 | 487 | ||
486 | 488 | ||
487 | static void luaB_tinsert (void) { | 489 | static void luaB_tinsert (lua_State *L) { |
488 | Hash *a = gettable(1); | 490 | Hash *a = gettable(L, 1); |
489 | lua_Object v = lua_getparam(3); | 491 | lua_Object v = lua_getparam(L, 3); |
490 | int n = (int)getnarg(a); | 492 | int n = (int)getnarg(L, a); |
491 | int pos; | 493 | int pos; |
492 | if (v != LUA_NOOBJECT) | 494 | if (v != LUA_NOOBJECT) |
493 | pos = luaL_check_int(2); | 495 | pos = luaL_check_int(L, 2); |
494 | else { /* called with only 2 arguments */ | 496 | else { /* called with only 2 arguments */ |
495 | v = luaL_nonnullarg(2); | 497 | v = luaL_nonnullarg(L, 2); |
496 | pos = n+1; | 498 | pos = n+1; |
497 | } | 499 | } |
498 | luaV_setn(a, n+1); /* a.n = n+1 */ | 500 | luaV_setn(L, a, n+1); /* a.n = n+1 */ |
499 | for ( ;n>=pos; n--) | 501 | for ( ;n>=pos; n--) |
500 | luaH_move(a, n, n+1); /* a[n+1] = a[n] */ | 502 | luaH_move(L, a, n, n+1); /* a[n+1] = a[n] */ |
501 | luaH_setint(a, pos, luaA_Address(v)); /* a[pos] = v */ | 503 | luaH_setint(L, a, pos, luaA_Address(L, v)); /* a[pos] = v */ |
502 | } | 504 | } |
503 | 505 | ||
504 | 506 | ||
505 | static void luaB_tremove (void) { | 507 | static void luaB_tremove (lua_State *L) { |
506 | Hash *a = gettable(1); | 508 | Hash *a = gettable(L, 1); |
507 | int n = (int)getnarg(a); | 509 | int n = (int)getnarg(L, a); |
508 | int pos = luaL_opt_int(2, n); | 510 | int pos = luaL_opt_int(L, 2, n); |
509 | if (n <= 0) return; /* table is "empty" */ | 511 | if (n <= 0) return; /* table is "empty" */ |
510 | luaA_pushobject(luaH_getint(a, pos)); /* result = a[pos] */ | 512 | luaA_pushobject(L, luaH_getint(L, a, pos)); /* result = a[pos] */ |
511 | for ( ;pos<n; pos++) | 513 | for ( ;pos<n; pos++) |
512 | luaH_move(a, pos+1, pos); /* a[pos] = a[pos+1] */ | 514 | luaH_move(L, a, pos+1, pos); /* a[pos] = a[pos+1] */ |
513 | luaV_setn(a, n-1); /* a.n = n-1 */ | 515 | luaV_setn(L, a, n-1); /* a.n = n-1 */ |
514 | luaH_setint(a, n, &luaO_nilobject); /* a[n] = nil */ | 516 | luaH_setint(L, a, n, &luaO_nilobject); /* a[n] = nil */ |
515 | } | 517 | } |
516 | 518 | ||
517 | 519 | ||
@@ -520,63 +522,63 @@ static void luaB_tremove (void) { | |||
520 | ** Quicksort | 522 | ** Quicksort |
521 | */ | 523 | */ |
522 | 524 | ||
523 | static void swap (Hash *a, int i, int j) { | 525 | static void swap (lua_State *L, Hash *a, int i, int j) { |
524 | TObject temp; | 526 | TObject temp; |
525 | temp = *luaH_getint(a, i); | 527 | temp = *luaH_getint(L, a, i); |
526 | luaH_move(a, j, i); | 528 | luaH_move(L, a, j, i); |
527 | luaH_setint(a, j, &temp); | 529 | luaH_setint(L, a, j, &temp); |
528 | } | 530 | } |
529 | 531 | ||
530 | static int sort_comp (lua_Object f, const TObject *a, const TObject *b) { | 532 | static int sort_comp (lua_State *L, lua_Object f, const TObject *a, const TObject *b) { |
531 | /* notice: the caller (auxsort) must check stack space */ | 533 | /* notice: the caller (auxsort) must check stack space */ |
532 | if (f != LUA_NOOBJECT) { | 534 | if (f != LUA_NOOBJECT) { |
533 | *(L->stack.top) = *luaA_Address(f); | 535 | *(L->stack.top) = *luaA_Address(L, f); |
534 | *(L->stack.top+1) = *a; | 536 | *(L->stack.top+1) = *a; |
535 | *(L->stack.top+2) = *b; | 537 | *(L->stack.top+2) = *b; |
536 | L->stack.top += 3; | 538 | L->stack.top += 3; |
537 | luaD_calln(2, 1); | 539 | luaD_calln(L, 2, 1); |
538 | } | 540 | } |
539 | else { /* a < b? */ | 541 | else { /* a < b? */ |
540 | *(L->stack.top) = *a; | 542 | *(L->stack.top) = *a; |
541 | *(L->stack.top+1) = *b; | 543 | *(L->stack.top+1) = *b; |
542 | L->stack.top += 2; | 544 | L->stack.top += 2; |
543 | luaV_comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT); | 545 | luaV_comparison(L, LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT); |
544 | } | 546 | } |
545 | return ttype(--(L->stack.top)) != LUA_T_NIL; | 547 | return ttype(--(L->stack.top)) != LUA_T_NIL; |
546 | } | 548 | } |
547 | 549 | ||
548 | static void auxsort (Hash *a, int l, int u, lua_Object f) { | 550 | static void auxsort (lua_State *L, Hash *a, int l, int u, lua_Object f) { |
549 | StkId P = L->stack.top - L->stack.stack; /* temporary place for pivot */ | 551 | StkId P = L->stack.top - L->stack.stack; /* temporary place for pivot */ |
550 | L->stack.top++; | 552 | L->stack.top++; |
551 | ttype(L->stack.stack+P) = LUA_T_NIL; | 553 | ttype(L->stack.stack+P) = LUA_T_NIL; |
552 | while (l < u) { /* for tail recursion */ | 554 | while (l < u) { /* for tail recursion */ |
553 | int i, j; | 555 | int i, j; |
554 | /* sort elements a[l], a[(l+u)/2] and a[u] */ | 556 | /* sort elements a[l], a[(l+u)/2] and a[u] */ |
555 | if (sort_comp(f, luaH_getint(a, u), luaH_getint(a, l))) /* a[u]<a[l]? */ | 557 | if (sort_comp(L, f, luaH_getint(L, a, u), luaH_getint(L, a, l))) /* a[u]<a[l]? */ |
556 | swap(a, l, u); | 558 | swap(L, a, l, u); |
557 | if (u-l == 1) break; /* only 2 elements */ | 559 | if (u-l == 1) break; /* only 2 elements */ |
558 | i = (l+u)/2; | 560 | i = (l+u)/2; |
559 | *(L->stack.stack+P) = *luaH_getint(a, i); /* P = a[i] */ | 561 | *(L->stack.stack+P) = *luaH_getint(L, a, i); /* P = a[i] */ |
560 | if (sort_comp(f, L->stack.stack+P, luaH_getint(a, l))) /* a[i]<a[l]? */ | 562 | if (sort_comp(L, f, L->stack.stack+P, luaH_getint(L, a, l))) /* a[i]<a[l]? */ |
561 | swap(a, l, i); | 563 | swap(L, a, l, i); |
562 | else if (sort_comp(f, luaH_getint(a, u), L->stack.stack+P)) /* a[u]<a[i]? */ | 564 | else if (sort_comp(L, f, luaH_getint(L, a, u), L->stack.stack+P)) /* a[u]<a[i]? */ |
563 | swap(a, i, u); | 565 | swap(L, a, i, u); |
564 | if (u-l == 2) break; /* only 3 elements */ | 566 | if (u-l == 2) break; /* only 3 elements */ |
565 | *(L->stack.stack+P) = *luaH_getint(a, i); /* save pivot on stack (for GC) */ | 567 | *(L->stack.stack+P) = *luaH_getint(L, a, i); /* save pivot on stack (GC) */ |
566 | swap(a, i, u-1); /* put median element as pivot (a[u-1]) */ | 568 | swap(L, a, i, u-1); /* put median element as pivot (a[u-1]) */ |
567 | /* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */ | 569 | /* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */ |
568 | i = l; j = u-1; | 570 | i = l; j = u-1; |
569 | for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ | 571 | for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ |
570 | /* repeat i++ until a[i] >= P */ | 572 | /* repeat i++ until a[i] >= P */ |
571 | while (sort_comp(f, luaH_getint(a, ++i), L->stack.stack+P)) | 573 | while (sort_comp(L, f, luaH_getint(L, a, ++i), L->stack.stack+P)) |
572 | if (i>u) lua_error("invalid order function for sorting"); | 574 | if (i>u) lua_error(L, "invalid order function for sorting"); |
573 | /* repeat j-- until a[j] <= P */ | 575 | /* repeat j-- until a[j] <= P */ |
574 | while (sort_comp(f, (L->stack.stack+P), luaH_getint(a, --j))) | 576 | while (sort_comp(L, f, (L->stack.stack+P), luaH_getint(L, a, --j))) |
575 | if (j<l) lua_error("invalid order function for sorting"); | 577 | if (j<l) lua_error(L, "invalid order function for sorting"); |
576 | if (j<i) break; | 578 | if (j<i) break; |
577 | swap(a, i, j); | 579 | swap(L, a, i, j); |
578 | } | 580 | } |
579 | swap(a, u-1, i); /* swap pivot (a[u-1]) with a[i] */ | 581 | swap(L, a, u-1, i); /* swap pivot (a[u-1]) with a[i] */ |
580 | /* a[l..i-1] <= a[i] == P <= a[i+1..u] */ | 582 | /* a[l..i-1] <= a[i] == P <= a[i+1..u] */ |
581 | /* adjust so that smaller "half" is in [j..i] and larger one in [l..u] */ | 583 | /* adjust so that smaller "half" is in [j..i] and larger one in [l..u] */ |
582 | if (i-l < u-i) { | 584 | if (i-l < u-i) { |
@@ -585,21 +587,21 @@ static void auxsort (Hash *a, int l, int u, lua_Object f) { | |||
585 | else { | 587 | else { |
586 | j=i+1; i=u; u=j-2; | 588 | j=i+1; i=u; u=j-2; |
587 | } | 589 | } |
588 | auxsort(a, j, i, f); /* call recursively the smaller one */ | 590 | auxsort(L, a, j, i, f); /* call recursively the smaller one */ |
589 | } /* repeat the routine for the larger one */ | 591 | } /* repeat the routine for the larger one */ |
590 | L->stack.top--; /* remove pivot from stack */ | 592 | L->stack.top--; /* remove pivot from stack */ |
591 | } | 593 | } |
592 | 594 | ||
593 | static void luaB_sort (void) { | 595 | static void luaB_sort (lua_State *L) { |
594 | lua_Object t = lua_getparam(1); | 596 | lua_Object t = lua_getparam(L, 1); |
595 | Hash *a = gettable(1); | 597 | Hash *a = gettable(L, 1); |
596 | int n = (int)getnarg(a); | 598 | int n = (int)getnarg(L, a); |
597 | lua_Object func = lua_getparam(2); | 599 | lua_Object func = lua_getparam(L, 2); |
598 | luaL_arg_check(func == LUA_NOOBJECT || lua_isfunction(func), 2, | 600 | luaL_arg_check(L, func == LUA_NOOBJECT || lua_isfunction(L, func), 2, |
599 | "function expected"); | 601 | "function expected"); |
600 | luaD_checkstack(4); /* for Pivot, f, a, b (sort_comp) */ | 602 | luaD_checkstack(L, 4); /* for Pivot, f, a, b (sort_comp) */ |
601 | auxsort(a, 1, n, func); | 603 | auxsort(L, a, 1, n, func); |
602 | lua_pushobject(t); | 604 | lua_pushobject(L, t); |
603 | } | 605 | } |
604 | 606 | ||
605 | /* }====================================================== */ | 607 | /* }====================================================== */ |
@@ -617,138 +619,138 @@ static void luaB_sort (void) { | |||
617 | ** ======================================================= | 619 | ** ======================================================= |
618 | */ | 620 | */ |
619 | 621 | ||
620 | static void mem_query (void) { | 622 | static void mem_query (lua_State *L) { |
621 | lua_pushnumber(totalmem); | 623 | lua_pushnumber(L, totalmem); |
622 | lua_pushnumber(numblocks); | 624 | lua_pushnumber(L, numblocks); |
623 | } | 625 | } |
624 | 626 | ||
625 | 627 | ||
626 | static void hash_query (void) { | 628 | static void hash_query (lua_State *L) { |
627 | const TObject *o = luaA_Address(luaL_nonnullarg(1)); | 629 | const TObject *o = luaA_Address(L, luaL_nonnullarg(L, 1)); |
628 | if (lua_getparam(2) == LUA_NOOBJECT) { | 630 | if (lua_getparam(L, 2) == LUA_NOOBJECT) { |
629 | luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "string expected"); | 631 | luaL_arg_check(L, ttype(o) == LUA_T_STRING, 1, "string expected"); |
630 | lua_pushnumber(tsvalue(o)->hash); | 632 | lua_pushnumber(L, tsvalue(o)->hash); |
631 | } | 633 | } |
632 | else { | 634 | else { |
633 | const Hash *t = avalue(luaA_Address(luaL_tablearg(2))); | 635 | const Hash *t = avalue(luaA_Address(L, luaL_tablearg(L, 2))); |
634 | lua_pushnumber(luaH_mainposition(t, o) - t->node); | 636 | lua_pushnumber(L, luaH_mainposition(L, t, o) - t->node); |
635 | } | 637 | } |
636 | } | 638 | } |
637 | 639 | ||
638 | 640 | ||
639 | static void table_query (void) { | 641 | static void table_query (lua_State *L) { |
640 | const Hash *t = avalue(luaA_Address(luaL_tablearg(1))); | 642 | const Hash *t = avalue(luaA_Address(L, luaL_tablearg(L, 1))); |
641 | int i = luaL_opt_int(2, -1); | 643 | int i = luaL_opt_int(L, 2, -1); |
642 | if (i == -1) { | 644 | if (i == -1) { |
643 | lua_pushnumber(t->size); | 645 | lua_pushnumber(L, t->size); |
644 | lua_pushnumber(t->firstfree - t->node); | 646 | lua_pushnumber(L, t->firstfree - t->node); |
645 | } | 647 | } |
646 | else if (i < t->size) { | 648 | else if (i < t->size) { |
647 | luaA_pushobject(&t->node[i].key); | 649 | luaA_pushobject(L, &t->node[i].key); |
648 | luaA_pushobject(&t->node[i].val); | 650 | luaA_pushobject(L, &t->node[i].val); |
649 | if (t->node[i].next) | 651 | if (t->node[i].next) |
650 | lua_pushnumber(t->node[i].next - t->node); | 652 | lua_pushnumber(L, t->node[i].next - t->node); |
651 | } | 653 | } |
652 | } | 654 | } |
653 | 655 | ||
654 | 656 | ||
655 | static void query_strings (void) { | 657 | static void query_strings (lua_State *L) { |
656 | int h = luaL_check_int(1) - 1; | 658 | int h = luaL_check_int(L, 1) - 1; |
657 | int s = luaL_opt_int(2, 0) - 1; | 659 | int s = luaL_opt_int(L, 2, 0) - 1; |
658 | if (s==-1) { | 660 | if (s==-1) { |
659 | if (h < NUM_HASHS) { | 661 | if (h < NUM_HASHS) { |
660 | lua_pushnumber(L->string_root[h].nuse); | 662 | lua_pushnumber(L, L->string_root[h].nuse); |
661 | lua_pushnumber(L->string_root[h].size); | 663 | lua_pushnumber(L, L->string_root[h].size); |
662 | } | 664 | } |
663 | } | 665 | } |
664 | else { | 666 | else { |
665 | TaggedString *ts = L->string_root[h].hash[s]; | 667 | TaggedString *ts = L->string_root[h].hash[s]; |
666 | for (ts = L->string_root[h].hash[s]; ts; ts = ts->nexthash) { | 668 | for (ts = L->string_root[h].hash[s]; ts; ts = ts->nexthash) { |
667 | if (ts->constindex == -1) lua_pushstring("<USERDATA>"); | 669 | if (ts->constindex == -1) lua_pushstring(L, "<USERDATA>"); |
668 | else lua_pushstring(ts->str); | 670 | else lua_pushstring(L, ts->str); |
669 | } | 671 | } |
670 | } | 672 | } |
671 | } | 673 | } |
672 | 674 | ||
673 | 675 | ||
674 | static void extra_services (void) { | 676 | static void extra_services (lua_State *L) { |
675 | const char *service = luaL_check_string(1); | 677 | const char *service = luaL_check_string(L, 1); |
676 | switch (*service) { | 678 | switch (*service) { |
677 | case 'U': /* create a userdata with a given value/tag */ | 679 | case 'U': /* create a userdata with a given value/tag */ |
678 | lua_pushusertag((void *)luaL_check_int(2), luaL_check_int(3)); | 680 | lua_pushusertag(L, (void *)luaL_check_int(L, 2), luaL_check_int(L, 3)); |
679 | break; | 681 | break; |
680 | 682 | ||
681 | case 'u': /* return the value of a userdata */ | 683 | case 'u': /* return the value of a userdata */ |
682 | lua_pushnumber((int)lua_getuserdata(lua_getparam(2))); | 684 | lua_pushnumber(L, (int)lua_getuserdata(L, lua_getparam(L, 2))); |
683 | break; | 685 | break; |
684 | 686 | ||
685 | case 't': /* set `gc' tag method */ | 687 | case 't': /* set `gc' tag method */ |
686 | lua_pushobject(lua_getparam(3)); | 688 | lua_pushobject(L, lua_getparam(L, 3)); |
687 | lua_settagmethod(luaL_check_int(2), "gc"); | 689 | lua_settagmethod(L, luaL_check_int(L, 2), "gc"); |
688 | break; | 690 | break; |
689 | 691 | ||
690 | default: luaL_argerror(1, "invalid service"); | 692 | default: luaL_argerror(L, 1, "invalid service"); |
691 | } | 693 | } |
692 | } | 694 | } |
693 | 695 | ||
694 | 696 | ||
695 | static void testC (void) { | 697 | static void testC (lua_State *L) { |
696 | #define getnum(s) ((*s++) - '0') | 698 | #define getnum(L, s) ((*s++) - '0') |
697 | #define getname(s) (nome[0] = *s++, nome) | 699 | #define getname(L, s) (nome[0] = *s++, nome) |
698 | 700 | ||
699 | lua_Object reg[10]; | 701 | lua_Object reg[10]; |
700 | char nome[2]; | 702 | char nome[2]; |
701 | const char *s = luaL_check_string(1); | 703 | const char *s = luaL_check_string(L, 1); |
702 | nome[1] = 0; | 704 | nome[1] = 0; |
703 | for (;;) { | 705 | for (;;) { |
704 | switch (*s++) { | 706 | switch (*s++) { |
705 | case '0': case '1': case '2': case '3': case '4': | 707 | case '0': case '1': case '2': case '3': case '4': |
706 | case '5': case '6': case '7': case '8': case '9': | 708 | case '5': case '6': case '7': case '8': case '9': |
707 | lua_pushnumber(*(s-1) - '0'); | 709 | lua_pushnumber(L, *(s-1) - '0'); |
708 | break; | 710 | break; |
709 | 711 | ||
710 | case 'c': reg[getnum(s)] = lua_createtable(); break; | 712 | case 'c': reg[getnum(L, s)] = lua_createtable(L); break; |
711 | case 'C': { lua_CFunction f = lua_getcfunction(lua_getglobal(getname(s))); | 713 | case 'C': { lua_CFunction f = lua_getcfunction(L, lua_getglobal(L, getname(L, s))); |
712 | lua_pushcclosure(f, getnum(s)); | 714 | lua_pushcclosure(L, f, getnum(L, s)); |
713 | break; | 715 | break; |
714 | } | 716 | } |
715 | case 'P': reg[getnum(s)] = lua_pop(); break; | 717 | case 'P': reg[getnum(L, s)] = lua_pop(L); break; |
716 | case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; } | 718 | case 'g': { int n=getnum(L, s); reg[n]=lua_getglobal(L, getname(L, s)); break; } |
717 | case 'G': { int n = getnum(s); | 719 | case 'G': { int n = getnum(L, s); |
718 | reg[n] = lua_rawgetglobal(getname(s)); | 720 | reg[n] = lua_rawgetglobal(L, getname(L, s)); |
719 | break; | 721 | break; |
720 | } | 722 | } |
721 | case 'l': lua_pushnumber(lua_ref(1)); reg[getnum(s)] = lua_pop(); break; | 723 | case 'l': lua_pushnumber(L, lua_ref(L, 1)); reg[getnum(L, s)] = lua_pop(L); break; |
722 | case 'L': lua_pushnumber(lua_ref(0)); reg[getnum(s)] = lua_pop(); break; | 724 | case 'L': lua_pushnumber(L, lua_ref(L, 0)); reg[getnum(L, s)] = lua_pop(L); break; |
723 | case 'r': { int n=getnum(s); | 725 | case 'r': { int n=getnum(L, s); |
724 | reg[n]=lua_getref((int)lua_getnumber(reg[getnum(s)])); | 726 | reg[n]=lua_getref(L, (int)lua_getnumber(L, reg[getnum(L, s)])); |
725 | break; | 727 | break; |
726 | } | 728 | } |
727 | case 'u': lua_unref((int)lua_getnumber(reg[getnum(s)])); | 729 | case 'u': lua_unref(L, (int)lua_getnumber(L, reg[getnum(L, s)])); |
728 | break; | 730 | break; |
729 | case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; } | 731 | case 'p': { int n = getnum(L, s); reg[n] = lua_getparam(L, getnum(L, s)); break; } |
730 | case '=': lua_setglobal(getname(s)); break; | 732 | case '=': lua_setglobal(L, getname(L, s)); break; |
731 | case 's': lua_pushstring(getname(s)); break; | 733 | case 's': lua_pushstring(L, getname(L, s)); break; |
732 | case 'o': lua_pushobject(reg[getnum(s)]); break; | 734 | case 'o': lua_pushobject(L, reg[getnum(L, s)]); break; |
733 | case 'f': lua_call(getname(s)); break; | 735 | case 'f': lua_call(L, getname(L, s)); break; |
734 | case 'i': reg[getnum(s)] = lua_gettable(); break; | 736 | case 'i': reg[getnum(L, s)] = lua_gettable(L); break; |
735 | case 'I': reg[getnum(s)] = lua_rawgettable(); break; | 737 | case 'I': reg[getnum(L, s)] = lua_rawgettable(L); break; |
736 | case 't': lua_settable(); break; | 738 | case 't': lua_settable(L); break; |
737 | case 'T': lua_rawsettable(); break; | 739 | case 'T': lua_rawsettable(L); break; |
738 | case 'N' : lua_pushstring(lua_nextvar(lua_getstring(reg[getnum(s)]))); | 740 | case 'N' : lua_pushstring(L, lua_nextvar(L, lua_getstring(L, reg[getnum(L, s)]))); |
739 | break; | 741 | break; |
740 | case 'n' : { int n=getnum(s); | 742 | case 'n' : { int n=getnum(L, s); |
741 | n=lua_next(reg[n], (int)lua_getnumber(reg[getnum(s)])); | 743 | n=lua_next(L, reg[n], (int)lua_getnumber(L, reg[getnum(L, s)])); |
742 | lua_pushnumber(n); break; | 744 | lua_pushnumber(L, n); break; |
743 | } | 745 | } |
744 | case 'q' : { int n1=getnum(s); int n2=getnum(s); | 746 | case 'q' : { int n1=getnum(L, s); int n2=getnum(L, s); |
745 | lua_pushnumber(lua_equalobj(reg[n1], reg[n2])); | 747 | lua_pushnumber(L, lua_equalobj(L, reg[n1], reg[n2])); |
746 | break; | 748 | break; |
747 | } | 749 | } |
748 | default: luaL_verror("unknown command in `testC': %c", *(s-1)); | 750 | default: luaL_verror(L, "unknown command in `testC': %c", *(s-1)); |
749 | } | 751 | } |
750 | if (*s == 0) return; | 752 | if (*s == 0) return; |
751 | if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'"); | 753 | if (*s++ != ' ') lua_error(L, "missing ` ' between commands in `testC'"); |
752 | } | 754 | } |
753 | } | 755 | } |
754 | 756 | ||
@@ -804,15 +806,12 @@ static const struct luaL_reg builtin_funcs[] = { | |||
804 | }; | 806 | }; |
805 | 807 | ||
806 | 808 | ||
807 | #define INTFUNCSIZE (sizeof(builtin_funcs)/sizeof(builtin_funcs[0])) | 809 | void luaB_predefine (lua_State *L) { |
808 | |||
809 | |||
810 | void luaB_predefine (void) { | ||
811 | /* pre-register mem error messages, to avoid loop when error arises */ | 810 | /* pre-register mem error messages, to avoid loop when error arises */ |
812 | luaS_newfixedstring(tableEM); | 811 | luaS_newfixedstring(L, tableEM); |
813 | luaS_newfixedstring(memEM); | 812 | luaS_newfixedstring(L, memEM); |
814 | luaL_openlib(builtin_funcs, (sizeof(builtin_funcs)/sizeof(builtin_funcs[0]))); | 813 | luaL_openlib(L, builtin_funcs, (sizeof(builtin_funcs)/sizeof(builtin_funcs[0]))); |
815 | lua_pushstring(LUA_VERSION); | 814 | lua_pushstring(L, LUA_VERSION); |
816 | lua_setglobal("_VERSION"); | 815 | lua_setglobal(L, "_VERSION"); |
817 | } | 816 | } |
818 | 817 | ||