diff options
Diffstat (limited to 'lbuiltin.c')
-rw-r--r-- | lbuiltin.c | 330 |
1 files changed, 48 insertions, 282 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbuiltin.c,v 1.82 1999/12/06 11:42:18 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.83 1999/12/07 12:05:34 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 | */ |
@@ -29,6 +29,17 @@ | |||
29 | 29 | ||
30 | 30 | ||
31 | 31 | ||
32 | #ifdef DEBUG | ||
33 | /* | ||
34 | ** function defined in ltests.c, to open some internal-test functions | ||
35 | */ | ||
36 | void luaB_opentests (lua_State *L); | ||
37 | #else | ||
38 | #define luaB_opentests(L) /* do nothing */ | ||
39 | #endif | ||
40 | |||
41 | |||
42 | |||
32 | /* | 43 | /* |
33 | ** {====================================================== | 44 | ** {====================================================== |
34 | ** Auxiliary functions | 45 | ** Auxiliary functions |
@@ -87,7 +98,7 @@ static Hash *gettable (lua_State *L, int arg) { | |||
87 | ** If your system does not support "stderr", redefine this function, or | 98 | ** If your system does not support "stderr", redefine this function, or |
88 | ** redefine _ERRORMESSAGE so that it won't need _ALERT. | 99 | ** redefine _ERRORMESSAGE so that it won't need _ALERT. |
89 | */ | 100 | */ |
90 | static void luaB_alert (lua_State *L) { | 101 | void luaB_alert (lua_State *L) { |
91 | fputs(luaL_check_string(L, 1), stderr); | 102 | fputs(luaL_check_string(L, 1), stderr); |
92 | } | 103 | } |
93 | 104 | ||
@@ -96,7 +107,7 @@ static void luaB_alert (lua_State *L) { | |||
96 | ** Standard implementation of _ERRORMESSAGE. | 107 | ** Standard implementation of _ERRORMESSAGE. |
97 | ** The library "iolib" redefines _ERRORMESSAGE for better error information. | 108 | ** The library "iolib" redefines _ERRORMESSAGE for better error information. |
98 | */ | 109 | */ |
99 | static void error_message (lua_State *L) { | 110 | void luaB_ERRORMESSAGE (lua_State *L) { |
100 | lua_Object al = lua_rawgetglobal(L, "_ALERT"); | 111 | lua_Object al = lua_rawgetglobal(L, "_ALERT"); |
101 | if (lua_isfunction(L, al)) { /* avoid error loop if _ALERT is not defined */ | 112 | if (lua_isfunction(L, al)) { /* avoid error loop if _ALERT is not defined */ |
102 | char buff[600]; | 113 | char buff[600]; |
@@ -117,7 +128,7 @@ static void error_message (lua_State *L) { | |||
117 | #define MAXPRINT 40 /* arbitrary limit */ | 128 | #define MAXPRINT 40 /* arbitrary limit */ |
118 | #endif | 129 | #endif |
119 | 130 | ||
120 | static void luaB_print (lua_State *L) { | 131 | void luaB_print (lua_State *L) { |
121 | lua_Object args[MAXPRINT]; | 132 | lua_Object args[MAXPRINT]; |
122 | lua_Object obj; | 133 | lua_Object obj; |
123 | int n = 0; | 134 | int n = 0; |
@@ -140,7 +151,7 @@ static void luaB_print (lua_State *L) { | |||
140 | } | 151 | } |
141 | 152 | ||
142 | 153 | ||
143 | static void luaB_tonumber (lua_State *L) { | 154 | void luaB_tonumber (lua_State *L) { |
144 | int base = luaL_opt_int(L, 2, 10); | 155 | int base = luaL_opt_int(L, 2, 10); |
145 | if (base == 10) { /* standard conversion */ | 156 | if (base == 10) { /* standard conversion */ |
146 | lua_Object o = lua_getparam(L, 1); | 157 | lua_Object o = lua_getparam(L, 1); |
@@ -161,66 +172,66 @@ static void luaB_tonumber (lua_State *L) { | |||
161 | } | 172 | } |
162 | 173 | ||
163 | 174 | ||
164 | static void luaB_error (lua_State *L) { | 175 | void luaB_error (lua_State *L) { |
165 | lua_error(L, lua_getstring(L, lua_getparam(L, 1))); | 176 | lua_error(L, lua_getstring(L, lua_getparam(L, 1))); |
166 | } | 177 | } |
167 | 178 | ||
168 | static void luaB_setglobal (lua_State *L) { | 179 | void luaB_setglobal (lua_State *L) { |
169 | const char *n = luaL_check_string(L, 1); | 180 | const char *n = luaL_check_string(L, 1); |
170 | lua_Object value = luaL_nonnullarg(L, 2); | 181 | lua_Object value = luaL_nonnullarg(L, 2); |
171 | lua_pushobject(L, value); | 182 | lua_pushobject(L, value); |
172 | lua_setglobal(L, n); | 183 | lua_setglobal(L, n); |
173 | } | 184 | } |
174 | 185 | ||
175 | static void luaB_rawsetglobal (lua_State *L) { | 186 | void luaB_rawsetglobal (lua_State *L) { |
176 | const char *n = luaL_check_string(L, 1); | 187 | const char *n = luaL_check_string(L, 1); |
177 | lua_Object value = luaL_nonnullarg(L, 2); | 188 | lua_Object value = luaL_nonnullarg(L, 2); |
178 | lua_pushobject(L, value); | 189 | lua_pushobject(L, value); |
179 | lua_rawsetglobal(L, n); | 190 | lua_rawsetglobal(L, n); |
180 | } | 191 | } |
181 | 192 | ||
182 | static void luaB_getglobal (lua_State *L) { | 193 | void luaB_getglobal (lua_State *L) { |
183 | lua_pushobject(L, lua_getglobal(L, luaL_check_string(L, 1))); | 194 | lua_pushobject(L, lua_getglobal(L, luaL_check_string(L, 1))); |
184 | } | 195 | } |
185 | 196 | ||
186 | static void luaB_rawgetglobal (lua_State *L) { | 197 | void luaB_rawgetglobal (lua_State *L) { |
187 | lua_pushobject(L, lua_rawgetglobal(L, luaL_check_string(L, 1))); | 198 | lua_pushobject(L, lua_rawgetglobal(L, luaL_check_string(L, 1))); |
188 | } | 199 | } |
189 | 200 | ||
190 | static void luaB_tag (lua_State *L) { | 201 | void luaB_tag (lua_State *L) { |
191 | lua_pushnumber(L, lua_tag(L, lua_getparam(L, 1))); | 202 | lua_pushnumber(L, lua_tag(L, lua_getparam(L, 1))); |
192 | } | 203 | } |
193 | 204 | ||
194 | static void luaB_settag (lua_State *L) { | 205 | void luaB_settag (lua_State *L) { |
195 | lua_Object o = luaL_tablearg(L, 1); | 206 | lua_Object o = luaL_tablearg(L, 1); |
196 | lua_pushobject(L, o); | 207 | lua_pushobject(L, o); |
197 | lua_settag(L, luaL_check_int(L, 2)); | 208 | lua_settag(L, luaL_check_int(L, 2)); |
198 | lua_pushobject(L, o); /* return first argument */ | 209 | lua_pushobject(L, o); /* return first argument */ |
199 | } | 210 | } |
200 | 211 | ||
201 | static void luaB_newtag (lua_State *L) { | 212 | void luaB_newtag (lua_State *L) { |
202 | lua_pushnumber(L, lua_newtag(L)); | 213 | lua_pushnumber(L, lua_newtag(L)); |
203 | } | 214 | } |
204 | 215 | ||
205 | static void luaB_copytagmethods (lua_State *L) { | 216 | void luaB_copytagmethods (lua_State *L) { |
206 | lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1), | 217 | lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1), |
207 | luaL_check_int(L, 2))); | 218 | luaL_check_int(L, 2))); |
208 | } | 219 | } |
209 | 220 | ||
210 | static void luaB_rawgettable (lua_State *L) { | 221 | void luaB_rawgettable (lua_State *L) { |
211 | lua_pushobject(L, luaL_nonnullarg(L, 1)); | 222 | lua_pushobject(L, luaL_nonnullarg(L, 1)); |
212 | lua_pushobject(L, luaL_nonnullarg(L, 2)); | 223 | lua_pushobject(L, luaL_nonnullarg(L, 2)); |
213 | lua_pushobject(L, lua_rawgettable(L)); | 224 | lua_pushobject(L, lua_rawgettable(L)); |
214 | } | 225 | } |
215 | 226 | ||
216 | static void luaB_rawsettable (lua_State *L) { | 227 | void luaB_rawsettable (lua_State *L) { |
217 | lua_pushobject(L, luaL_nonnullarg(L, 1)); | 228 | lua_pushobject(L, luaL_nonnullarg(L, 1)); |
218 | lua_pushobject(L, luaL_nonnullarg(L, 2)); | 229 | lua_pushobject(L, luaL_nonnullarg(L, 2)); |
219 | lua_pushobject(L, luaL_nonnullarg(L, 3)); | 230 | lua_pushobject(L, luaL_nonnullarg(L, 3)); |
220 | lua_rawsettable(L); | 231 | lua_rawsettable(L); |
221 | } | 232 | } |
222 | 233 | ||
223 | static void luaB_settagmethod (lua_State *L) { | 234 | void luaB_settagmethod (lua_State *L) { |
224 | int tag = luaL_check_int(L, 1); | 235 | int tag = luaL_check_int(L, 1); |
225 | const char *event = luaL_check_string(L, 2); | 236 | const char *event = luaL_check_string(L, 2); |
226 | lua_Object nf = luaL_nonnullarg(L, 3); | 237 | lua_Object nf = luaL_nonnullarg(L, 3); |
@@ -232,23 +243,23 @@ static void luaB_settagmethod (lua_State *L) { | |||
232 | lua_pushobject(L, lua_settagmethod(L, tag, event)); | 243 | lua_pushobject(L, lua_settagmethod(L, tag, event)); |
233 | } | 244 | } |
234 | 245 | ||
235 | static void luaB_gettagmethod (lua_State *L) { | 246 | void luaB_gettagmethod (lua_State *L) { |
236 | lua_pushobject(L, lua_gettagmethod(L, luaL_check_int(L, 1), | 247 | lua_pushobject(L, lua_gettagmethod(L, luaL_check_int(L, 1), |
237 | luaL_check_string(L, 2))); | 248 | luaL_check_string(L, 2))); |
238 | } | 249 | } |
239 | 250 | ||
240 | static void luaB_seterrormethod (lua_State *L) { | 251 | void luaB_seterrormethod (lua_State *L) { |
241 | lua_Object nf = luaL_functionarg(L, 1); | 252 | lua_Object nf = luaL_functionarg(L, 1); |
242 | lua_pushobject(L, nf); | 253 | lua_pushobject(L, nf); |
243 | lua_pushobject(L, lua_seterrormethod(L)); | 254 | lua_pushobject(L, lua_seterrormethod(L)); |
244 | } | 255 | } |
245 | 256 | ||
246 | static void luaB_collectgarbage (lua_State *L) { | 257 | void luaB_collectgarbage (lua_State *L) { |
247 | lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0))); | 258 | lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0))); |
248 | } | 259 | } |
249 | 260 | ||
250 | 261 | ||
251 | static void luaB_type (lua_State *L) { | 262 | void luaB_type (lua_State *L) { |
252 | lua_Object o = luaL_nonnullarg(L, 1); | 263 | lua_Object o = luaL_nonnullarg(L, 1); |
253 | lua_pushstring(L, lua_type(L, o)); | 264 | lua_pushstring(L, lua_type(L, o)); |
254 | } | 265 | } |
@@ -270,7 +281,7 @@ static void passresults (lua_State *L) { | |||
270 | lua_pushuserdata(L, NULL); /* at least one result to signal no errors */ | 281 | lua_pushuserdata(L, NULL); /* at least one result to signal no errors */ |
271 | } | 282 | } |
272 | 283 | ||
273 | static void luaB_dostring (lua_State *L) { | 284 | void luaB_dostring (lua_State *L) { |
274 | long l; | 285 | long l; |
275 | const char *s = luaL_check_lstr(L, 1, &l); | 286 | const char *s = luaL_check_lstr(L, 1, &l); |
276 | if (*s == ID_CHUNK) | 287 | if (*s == ID_CHUNK) |
@@ -281,7 +292,7 @@ static void luaB_dostring (lua_State *L) { | |||
281 | } | 292 | } |
282 | 293 | ||
283 | 294 | ||
284 | static void luaB_dofile (lua_State *L) { | 295 | void luaB_dofile (lua_State *L) { |
285 | const char *fname = luaL_opt_string(L, 1, NULL); | 296 | const char *fname = luaL_opt_string(L, 1, NULL); |
286 | if (lua_dofile(L, fname) == 0) | 297 | if (lua_dofile(L, fname) == 0) |
287 | passresults(L); | 298 | passresults(L); |
@@ -289,7 +300,7 @@ static void luaB_dofile (lua_State *L) { | |||
289 | } | 300 | } |
290 | 301 | ||
291 | 302 | ||
292 | static void luaB_call (lua_State *L) { | 303 | void luaB_call (lua_State *L) { |
293 | lua_Object f = luaL_nonnullarg(L, 1); | 304 | lua_Object f = luaL_nonnullarg(L, 1); |
294 | const Hash *arg = gettable(L, 2); | 305 | const Hash *arg = gettable(L, 2); |
295 | const char *options = luaL_opt_string(L, 3, ""); | 306 | const char *options = luaL_opt_string(L, 3, ""); |
@@ -328,7 +339,7 @@ static void luaB_call (lua_State *L) { | |||
328 | } | 339 | } |
329 | 340 | ||
330 | 341 | ||
331 | static void luaB_nextvar (lua_State *L) { | 342 | void luaB_nextvar (lua_State *L) { |
332 | lua_Object o = luaL_nonnullarg(L, 1); | 343 | lua_Object o = luaL_nonnullarg(L, 1); |
333 | TaggedString *g; | 344 | TaggedString *g; |
334 | if (ttype(o) == LUA_T_NIL) | 345 | if (ttype(o) == LUA_T_NIL) |
@@ -342,7 +353,7 @@ static void luaB_nextvar (lua_State *L) { | |||
342 | } | 353 | } |
343 | 354 | ||
344 | 355 | ||
345 | static void luaB_next (lua_State *L) { | 356 | void luaB_next (lua_State *L) { |
346 | const Hash *a = gettable(L, 1); | 357 | const Hash *a = gettable(L, 1); |
347 | lua_Object k = luaL_nonnullarg(L, 2); | 358 | lua_Object k = luaL_nonnullarg(L, 2); |
348 | int i; /* will get first element after `i' */ | 359 | int i; /* will get first element after `i' */ |
@@ -357,7 +368,7 @@ static void luaB_next (lua_State *L) { | |||
357 | } | 368 | } |
358 | 369 | ||
359 | 370 | ||
360 | static void luaB_tostring (lua_State *L) { | 371 | void luaB_tostring (lua_State *L) { |
361 | lua_Object o = lua_getparam(L, 1); | 372 | lua_Object o = lua_getparam(L, 1); |
362 | char buff[64]; | 373 | char buff[64]; |
363 | switch (ttype(o)) { | 374 | switch (ttype(o)) { |
@@ -405,14 +416,14 @@ static void luaB_tostring (lua_State *L) { | |||
405 | ** ======================================================= | 416 | ** ======================================================= |
406 | */ | 417 | */ |
407 | 418 | ||
408 | static void luaB_assert (lua_State *L) { | 419 | void luaB_assert (lua_State *L) { |
409 | lua_Object p = lua_getparam(L, 1); | 420 | lua_Object p = lua_getparam(L, 1); |
410 | if (p == LUA_NOOBJECT || lua_isnil(L, p)) | 421 | if (p == LUA_NOOBJECT || lua_isnil(L, p)) |
411 | luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, "")); | 422 | luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, "")); |
412 | } | 423 | } |
413 | 424 | ||
414 | 425 | ||
415 | static void luaB_foreachi (lua_State *L) { | 426 | void luaB_foreachi (lua_State *L) { |
416 | const Hash *t = gettable(L, 1); | 427 | const Hash *t = gettable(L, 1); |
417 | int n = (int)getnarg(L, t); | 428 | int n = (int)getnarg(L, t); |
418 | int i; | 429 | int i; |
@@ -430,7 +441,7 @@ static void luaB_foreachi (lua_State *L) { | |||
430 | } | 441 | } |
431 | 442 | ||
432 | 443 | ||
433 | static void luaB_foreach (lua_State *L) { | 444 | void luaB_foreach (lua_State *L) { |
434 | const Hash *a = gettable(L, 1); | 445 | const Hash *a = gettable(L, 1); |
435 | lua_Object f = luaL_functionarg(L, 2); | 446 | lua_Object f = luaL_functionarg(L, 2); |
436 | int i; | 447 | int i; |
@@ -450,7 +461,7 @@ static void luaB_foreach (lua_State *L) { | |||
450 | } | 461 | } |
451 | 462 | ||
452 | 463 | ||
453 | static void luaB_foreachvar (lua_State *L) { | 464 | void luaB_foreachvar (lua_State *L) { |
454 | lua_Object f = luaL_functionarg(L, 1); | 465 | lua_Object f = luaL_functionarg(L, 1); |
455 | GlobalVar *gv; | 466 | GlobalVar *gv; |
456 | luaD_checkstack(L, 4); /* for extra var name, f, var name, and globalval */ | 467 | luaD_checkstack(L, 4); /* for extra var name, f, var name, and globalval */ |
@@ -472,12 +483,12 @@ static void luaB_foreachvar (lua_State *L) { | |||
472 | } | 483 | } |
473 | 484 | ||
474 | 485 | ||
475 | static void luaB_getn (lua_State *L) { | 486 | void luaB_getn (lua_State *L) { |
476 | lua_pushnumber(L, getnarg(L, gettable(L, 1))); | 487 | lua_pushnumber(L, getnarg(L, gettable(L, 1))); |
477 | } | 488 | } |
478 | 489 | ||
479 | 490 | ||
480 | static void luaB_tinsert (lua_State *L) { | 491 | void luaB_tinsert (lua_State *L) { |
481 | Hash *a = gettable(L, 1); | 492 | Hash *a = gettable(L, 1); |
482 | lua_Object v = lua_getparam(L, 3); | 493 | lua_Object v = lua_getparam(L, 3); |
483 | int n = (int)getnarg(L, a); | 494 | int n = (int)getnarg(L, a); |
@@ -495,7 +506,7 @@ static void luaB_tinsert (lua_State *L) { | |||
495 | } | 506 | } |
496 | 507 | ||
497 | 508 | ||
498 | static void luaB_tremove (lua_State *L) { | 509 | void luaB_tremove (lua_State *L) { |
499 | Hash *a = gettable(L, 1); | 510 | Hash *a = gettable(L, 1); |
500 | int n = (int)getnarg(L, a); | 511 | int n = (int)getnarg(L, a); |
501 | int pos = luaL_opt_int(L, 2, n); | 512 | int pos = luaL_opt_int(L, 2, n); |
@@ -583,7 +594,7 @@ static void auxsort (lua_State *L, Hash *a, int l, int u, lua_Object f) { | |||
583 | L->top--; /* remove pivot from stack */ | 594 | L->top--; /* remove pivot from stack */ |
584 | } | 595 | } |
585 | 596 | ||
586 | static void luaB_sort (lua_State *L) { | 597 | void luaB_sort (lua_State *L) { |
587 | lua_Object t = lua_getparam(L, 1); | 598 | lua_Object t = lua_getparam(L, 1); |
588 | Hash *a = gettable(L, 1); | 599 | Hash *a = gettable(L, 1); |
589 | int n = (int)getnarg(L, a); | 600 | int n = (int)getnarg(L, a); |
@@ -601,255 +612,9 @@ static void luaB_sort (lua_State *L) { | |||
601 | /* }====================================================== */ | 612 | /* }====================================================== */ |
602 | 613 | ||
603 | 614 | ||
604 | |||
605 | #ifdef DEBUG | ||
606 | /* | ||
607 | ** {====================================================== | ||
608 | ** some DEBUG functions | ||
609 | ** (for internal debugging of the Lua implementation) | ||
610 | ** ======================================================= | ||
611 | */ | ||
612 | |||
613 | static void mem_query (lua_State *L) { | ||
614 | lua_pushnumber(L, totalmem); | ||
615 | lua_pushnumber(L, numblocks); | ||
616 | } | ||
617 | |||
618 | |||
619 | static void hash_query (lua_State *L) { | ||
620 | lua_Object o = luaL_nonnullarg(L, 1); | ||
621 | if (lua_getparam(L, 2) == LUA_NOOBJECT) { | ||
622 | luaL_arg_check(L, ttype(o) == LUA_T_STRING, 1, "string expected"); | ||
623 | lua_pushnumber(L, tsvalue(o)->hash); | ||
624 | } | ||
625 | else { | ||
626 | const Hash *t = avalue(luaL_tablearg(L, 2)); | ||
627 | lua_pushnumber(L, luaH_mainposition(t, o) - t->node); | ||
628 | } | ||
629 | } | ||
630 | |||
631 | |||
632 | static void table_query (lua_State *L) { | ||
633 | const Hash *t = avalue(luaL_tablearg(L, 1)); | ||
634 | int i = luaL_opt_int(L, 2, -1); | ||
635 | if (i == -1) { | ||
636 | lua_pushnumber(L, t->size); | ||
637 | lua_pushnumber(L, t->firstfree - t->node); | ||
638 | } | ||
639 | else if (i < t->size) { | ||
640 | luaA_pushobject(L, &t->node[i].key); | ||
641 | luaA_pushobject(L, &t->node[i].val); | ||
642 | if (t->node[i].next) | ||
643 | lua_pushnumber(L, t->node[i].next - t->node); | ||
644 | } | ||
645 | } | ||
646 | |||
647 | |||
648 | static void query_strings (lua_State *L) { | ||
649 | int h = luaL_check_int(L, 1) - 1; | ||
650 | int s = luaL_opt_int(L, 2, 0) - 1; | ||
651 | if (s==-1) { | ||
652 | if (h < NUM_HASHS) { | ||
653 | lua_pushnumber(L, L->string_root[h].nuse); | ||
654 | lua_pushnumber(L, L->string_root[h].size); | ||
655 | } | ||
656 | } | ||
657 | else { | ||
658 | TaggedString *ts = L->string_root[h].hash[s]; | ||
659 | for (ts = L->string_root[h].hash[s]; ts; ts = ts->nexthash) { | ||
660 | if (ts->constindex == -1) lua_pushstring(L, "<USERDATA>"); | ||
661 | else lua_pushstring(L, ts->str); | ||
662 | } | ||
663 | } | ||
664 | } | ||
665 | |||
666 | |||
667 | static const char *delimits = " \t\n,;"; | ||
668 | |||
669 | static void skip (const char **pc) { | ||
670 | while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++; | ||
671 | } | ||
672 | |||
673 | static int getnum (const char **pc) { | ||
674 | int res = 0; | ||
675 | skip(pc); | ||
676 | while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0'; | ||
677 | return res; | ||
678 | } | ||
679 | |||
680 | static int getreg (lua_State *L, const char **pc) { | ||
681 | skip(pc); | ||
682 | if (*(*pc)++ != 'r') lua_error(L, "`testC' expecting a register"); | ||
683 | return getnum(pc); | ||
684 | } | ||
685 | |||
686 | static const char *getname (const char **pc) { | ||
687 | static char buff[30]; | ||
688 | int i = 0; | ||
689 | skip(pc); | ||
690 | while (**pc != '\0' && !strchr(delimits, **pc)) | ||
691 | buff[i++] = *(*pc)++; | ||
692 | buff[i] = '\0'; | ||
693 | return buff; | ||
694 | } | ||
695 | |||
696 | |||
697 | #define EQ(s1) (strcmp(s1, inst) == 0) | ||
698 | |||
699 | static void testC (lua_State *L) { | ||
700 | lua_Object reg[10]; | ||
701 | const char *pc = luaL_check_string(L, 1); | ||
702 | for (;;) { | ||
703 | const char *inst = getname(&pc); | ||
704 | if EQ("") return; | ||
705 | else if EQ("pushnum") { | ||
706 | lua_pushnumber(L, getnum(&pc)); | ||
707 | } | ||
708 | else if EQ("createtable") { | ||
709 | reg[getreg(L, &pc)] = lua_createtable(L); | ||
710 | } | ||
711 | else if EQ("closure") { | ||
712 | lua_CFunction f = lua_getcfunction(L, lua_getglobal(L, getname(&pc))); | ||
713 | lua_pushcclosure(L, f, getnum(&pc)); | ||
714 | } | ||
715 | else if EQ("pop") { | ||
716 | reg[getreg(L, &pc)] = lua_pop(L); | ||
717 | } | ||
718 | else if EQ("getglobal") { | ||
719 | int n = getreg(L, &pc); | ||
720 | reg[n] = lua_getglobal(L, getname(&pc)); | ||
721 | } | ||
722 | else if EQ("rawgetglobal") { | ||
723 | int n = getreg(L, &pc); | ||
724 | reg[n] = lua_rawgetglobal(L, getname(&pc)); | ||
725 | } | ||
726 | else if EQ("ref") { | ||
727 | lua_pushnumber(L, lua_ref(L, 0)); | ||
728 | reg[getreg(L, &pc)] = lua_pop(L); | ||
729 | } | ||
730 | else if EQ("reflock") { | ||
731 | lua_pushnumber(L, lua_ref(L, 1)); | ||
732 | reg[getreg(L, &pc)] = lua_pop(L); | ||
733 | } | ||
734 | else if EQ("getref") { | ||
735 | int n = getreg(L, &pc); | ||
736 | reg[n] = lua_getref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)])); | ||
737 | } | ||
738 | else if EQ("unref") { | ||
739 | lua_unref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)])); | ||
740 | } | ||
741 | else if EQ("getparam") { | ||
742 | int n = getreg(L, &pc); | ||
743 | reg[n] = lua_getparam(L, getnum(&pc)+1); /* skips the commmand itself */ | ||
744 | } | ||
745 | else if EQ("getresult") { | ||
746 | int n = getreg(L, &pc); | ||
747 | reg[n] = lua_getparam(L, getnum(&pc)); | ||
748 | } | ||
749 | else if EQ("setglobal") { | ||
750 | lua_setglobal(L, getname(&pc)); | ||
751 | } | ||
752 | else if EQ("rawsetglobal") { | ||
753 | lua_rawsetglobal(L, getname(&pc)); | ||
754 | } | ||
755 | else if EQ("pushstring") { | ||
756 | lua_pushstring(L, getname(&pc)); | ||
757 | } | ||
758 | else if EQ("pushreg") { | ||
759 | lua_pushobject(L, reg[getreg(L, &pc)]); | ||
760 | } | ||
761 | else if EQ("call") { | ||
762 | lua_call(L, getname(&pc)); | ||
763 | } | ||
764 | else if EQ("gettable") { | ||
765 | reg[getreg(L, &pc)] = lua_gettable(L); | ||
766 | } | ||
767 | else if EQ("rawgettable") { | ||
768 | reg[getreg(L, &pc)] = lua_rawgettable(L); | ||
769 | } | ||
770 | else if EQ("settable") { | ||
771 | lua_settable(L); | ||
772 | } | ||
773 | else if EQ("rawsettable") { | ||
774 | lua_rawsettable(L); | ||
775 | } | ||
776 | else if EQ("nextvar") { | ||
777 | lua_pushstring(L, lua_nextvar(L, lua_getstring(L, reg[getreg(L, &pc)]))); | ||
778 | } | ||
779 | else if EQ("next") { | ||
780 | int n = getreg(L, &pc); | ||
781 | n = lua_next(L, reg[n], (int)lua_getnumber(L, reg[getreg(L, &pc)])); | ||
782 | lua_pushnumber(L, n); | ||
783 | } | ||
784 | else if EQ("equal") { | ||
785 | int n1 = getreg(L, &pc); | ||
786 | int n2 = getreg(L, &pc); | ||
787 | lua_pushnumber(L, lua_equal(L, reg[n1], reg[n2])); | ||
788 | } | ||
789 | else if EQ("pushusertag") { | ||
790 | int val = getreg(L, &pc); | ||
791 | int tag = getreg(L, &pc); | ||
792 | lua_pushusertag(L, (void *)(int)lua_getnumber(L, reg[val]), | ||
793 | (int)lua_getnumber(L, reg[tag])); | ||
794 | } | ||
795 | else if EQ("udataval") { | ||
796 | int n = getreg(L, &pc); | ||
797 | lua_pushnumber(L, (int)lua_getuserdata(L, reg[getreg(L, &pc)])); | ||
798 | reg[n] = lua_pop(L); | ||
799 | } | ||
800 | else if EQ("settagmethod") { | ||
801 | int n = getreg(L, &pc); | ||
802 | lua_settagmethod(L, (int)lua_getnumber(L, reg[n]), getname(&pc)); | ||
803 | } | ||
804 | else if EQ("beginblock") { | ||
805 | lua_beginblock(L); | ||
806 | } | ||
807 | else if EQ("endblock") { | ||
808 | lua_endblock(L); | ||
809 | } | ||
810 | else if EQ("newstate") { | ||
811 | int stacksize = getnum(&pc); | ||
812 | lua_State *L1 = lua_newstate("stack", stacksize, | ||
813 | "builtin", getnum(&pc), NULL); | ||
814 | lua_pushuserdata(L, L1); | ||
815 | } | ||
816 | else if EQ("closestate") { | ||
817 | lua_close(lua_getuserdata(L, reg[getreg(L, &pc)])); | ||
818 | } | ||
819 | else if EQ("doremote") { | ||
820 | lua_Object ol1 = reg[getreg(L, &pc)]; | ||
821 | lua_Object str = reg[getreg(L, &pc)]; | ||
822 | lua_State *L1; | ||
823 | lua_Object temp; | ||
824 | int i; | ||
825 | if (!lua_isuserdata(L, ol1) || !lua_isstring(L, str)) | ||
826 | lua_error(L, "bad arguments for `doremote'"); | ||
827 | L1 = lua_getuserdata(L, ol1); | ||
828 | lua_dostring(L1, lua_getstring(L, str)); | ||
829 | i = 1; | ||
830 | while ((temp = lua_getresult(L1, i++)) != LUA_NOOBJECT) | ||
831 | lua_pushstring(L, lua_getstring(L1, temp)); | ||
832 | } | ||
833 | else luaL_verror(L, "unknown command in `testC': %.20s", inst); | ||
834 | } | ||
835 | } | ||
836 | |||
837 | |||
838 | /* }====================================================== */ | ||
839 | #endif | ||
840 | |||
841 | |||
842 | |||
843 | static const struct luaL_reg builtin_funcs[] = { | 615 | static const struct luaL_reg builtin_funcs[] = { |
844 | #ifdef DEBUG | ||
845 | {"hash", hash_query}, | ||
846 | {"querystr", query_strings}, | ||
847 | {"querytab", table_query}, | ||
848 | {"testC", testC}, | ||
849 | {"totalmem", mem_query}, | ||
850 | #endif | ||
851 | {"_ALERT", luaB_alert}, | 616 | {"_ALERT", luaB_alert}, |
852 | {"_ERRORMESSAGE", error_message}, | 617 | {"_ERRORMESSAGE", luaB_ERRORMESSAGE}, |
853 | {"call", luaB_call}, | 618 | {"call", luaB_call}, |
854 | {"collectgarbage", luaB_collectgarbage}, | 619 | {"collectgarbage", luaB_collectgarbage}, |
855 | {"copytagmethods", luaB_copytagmethods}, | 620 | {"copytagmethods", luaB_copytagmethods}, |
@@ -891,6 +656,7 @@ void luaB_predefine (lua_State *L) { | |||
891 | luaS_newfixedstring(L, tableEM); | 656 | luaS_newfixedstring(L, tableEM); |
892 | luaS_newfixedstring(L, memEM); | 657 | luaS_newfixedstring(L, memEM); |
893 | luaL_openl(L, builtin_funcs); | 658 | luaL_openl(L, builtin_funcs); |
659 | luaB_opentests(L); /* internal test functions (when DEBUG is on) */ | ||
894 | lua_pushstring(L, LUA_VERSION); | 660 | lua_pushstring(L, LUA_VERSION); |
895 | lua_setglobal(L, "_VERSION"); | 661 | lua_setglobal(L, "_VERSION"); |
896 | } | 662 | } |