diff options
-rw-r--r-- | lbuiltin.c | 151 |
1 files changed, 63 insertions, 88 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbuiltin.c,v 1.36 1998/12/03 15:45:15 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.37 1998/12/15 14:59:59 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 | */ |
@@ -27,8 +27,7 @@ | |||
27 | 27 | ||
28 | 28 | ||
29 | 29 | ||
30 | static void pushstring (TaggedString *s) | 30 | static void pushstring (TaggedString *s) { |
31 | { | ||
32 | TObject o; | 31 | TObject o; |
33 | o.ttype = LUA_T_STRING; | 32 | o.ttype = LUA_T_STRING; |
34 | o.value.ts = s; | 33 | o.value.ts = s; |
@@ -60,12 +59,12 @@ static int getnarg (lua_Object table) { | |||
60 | } | 59 | } |
61 | 60 | ||
62 | 61 | ||
63 | static void getn (void) { | 62 | static void luaB_getn (void) { |
64 | lua_pushnumber(getnarg(luaL_tablearg(1))); | 63 | lua_pushnumber(getnarg(luaL_tablearg(1))); |
65 | } | 64 | } |
66 | 65 | ||
67 | 66 | ||
68 | static void nextvar (void) { | 67 | static void luaB_nextvar (void) { |
69 | TObject *o = luaA_Address(luaL_nonnullarg(1)); | 68 | TObject *o = luaA_Address(luaL_nonnullarg(1)); |
70 | TaggedString *g; | 69 | TaggedString *g; |
71 | if (ttype(o) == LUA_T_NIL) | 70 | if (ttype(o) == LUA_T_NIL) |
@@ -87,7 +86,7 @@ static void nextvar (void) { | |||
87 | } | 86 | } |
88 | 87 | ||
89 | 88 | ||
90 | static void foreachvar (void) { | 89 | static void luaB_foreachvar (void) { |
91 | TObject f = *luaA_Address(luaL_functionarg(1)); | 90 | TObject f = *luaA_Address(luaL_functionarg(1)); |
92 | GCnode *g; | 91 | GCnode *g; |
93 | StkId name = L->Cstack.base++; /* place to keep var name (to avoid GC) */ | 92 | StkId name = L->Cstack.base++; /* place to keep var name (to avoid GC) */ |
@@ -111,7 +110,7 @@ static void foreachvar (void) { | |||
111 | } | 110 | } |
112 | 111 | ||
113 | 112 | ||
114 | static void next (void) { | 113 | static void luaB_next (void) { |
115 | Node *n = luaH_next(luaA_Address(luaL_tablearg(1)), | 114 | Node *n = luaH_next(luaA_Address(luaL_tablearg(1)), |
116 | luaA_Address(luaL_nonnullarg(2))); | 115 | luaA_Address(luaL_nonnullarg(2))); |
117 | if (n) { | 116 | if (n) { |
@@ -122,7 +121,7 @@ static void next (void) { | |||
122 | } | 121 | } |
123 | 122 | ||
124 | 123 | ||
125 | static void foreach (void) { | 124 | static void luaB_foreach (void) { |
126 | TObject t = *luaA_Address(luaL_tablearg(1)); | 125 | TObject t = *luaA_Address(luaL_tablearg(1)); |
127 | TObject f = *luaA_Address(luaL_functionarg(2)); | 126 | TObject f = *luaA_Address(luaL_functionarg(2)); |
128 | int i; | 127 | int i; |
@@ -142,7 +141,7 @@ static void foreach (void) { | |||
142 | } | 141 | } |
143 | 142 | ||
144 | 143 | ||
145 | static void foreachi (void) { | 144 | static void luaB_foreachi (void) { |
146 | lua_Object ot = luaL_tablearg(1); | 145 | lua_Object ot = luaL_tablearg(1); |
147 | Hash *t = avalue(luaA_Address(ot)); | 146 | Hash *t = avalue(luaA_Address(ot)); |
148 | TObject f = *luaA_Address(luaL_functionarg(2)); | 147 | TObject f = *luaA_Address(luaL_functionarg(2)); |
@@ -162,8 +161,7 @@ static void foreachi (void) { | |||
162 | } | 161 | } |
163 | 162 | ||
164 | 163 | ||
165 | static void internaldostring (void) | 164 | static void luaB_dostring (void) { |
166 | { | ||
167 | long l; | 165 | long l; |
168 | char *s = luaL_check_lstr(1, &l); | 166 | char *s = luaL_check_lstr(1, &l); |
169 | if (*s == ID_CHUNK) | 167 | if (*s == ID_CHUNK) |
@@ -174,8 +172,7 @@ static void internaldostring (void) | |||
174 | } | 172 | } |
175 | 173 | ||
176 | 174 | ||
177 | static void internaldofile (void) | 175 | static void luaB_dofile (void) { |
178 | { | ||
179 | char *fname = luaL_opt_string(1, NULL); | 176 | char *fname = luaL_opt_string(1, NULL); |
180 | if (lua_dofile(fname) == 0) | 177 | if (lua_dofile(fname) == 0) |
181 | if (luaA_passresults() == 0) | 178 | if (luaA_passresults() == 0) |
@@ -183,7 +180,7 @@ static void internaldofile (void) | |||
183 | } | 180 | } |
184 | 181 | ||
185 | 182 | ||
186 | static void to_string (void) { | 183 | static void luaB_tostring (void) { |
187 | lua_Object obj = lua_getparam(1); | 184 | lua_Object obj = lua_getparam(1); |
188 | TObject *o = luaA_Address(obj); | 185 | TObject *o = luaA_Address(obj); |
189 | char buff[32]; | 186 | char buff[32]; |
@@ -224,7 +221,7 @@ static void to_string (void) { | |||
224 | } | 221 | } |
225 | 222 | ||
226 | 223 | ||
227 | static void luaI_print (void) { | 224 | static void luaB_print (void) { |
228 | TaggedString *ts = luaS_new("tostring"); | 225 | TaggedString *ts = luaS_new("tostring"); |
229 | lua_Object obj; | 226 | lua_Object obj; |
230 | int i = 1; | 227 | int i = 1; |
@@ -255,16 +252,14 @@ static void error_message (void) { | |||
255 | } | 252 | } |
256 | 253 | ||
257 | 254 | ||
258 | static void luaI_type (void) | 255 | static void luaB_type (void) { |
259 | { | ||
260 | lua_Object o = luaL_nonnullarg(1); | 256 | lua_Object o = luaL_nonnullarg(1); |
261 | lua_pushstring(luaO_typename(luaA_Address(o))); | 257 | lua_pushstring(luaO_typename(luaA_Address(o))); |
262 | lua_pushnumber(lua_tag(o)); | 258 | lua_pushnumber(lua_tag(o)); |
263 | } | 259 | } |
264 | 260 | ||
265 | 261 | ||
266 | static void luaB_tonumber (void) | 262 | static void luaB_tonumber (void) { |
267 | { | ||
268 | int base = luaL_opt_number(2, 10); | 263 | int base = luaL_opt_number(2, 10); |
269 | if (base == 10) { /* standard conversion */ | 264 | if (base == 10) { /* standard conversion */ |
270 | lua_Object o = lua_getparam(1); | 265 | lua_Object o = lua_getparam(1); |
@@ -283,22 +278,19 @@ static void luaB_tonumber (void) | |||
283 | } | 278 | } |
284 | 279 | ||
285 | 280 | ||
286 | static void luaI_error (void) | 281 | static void luaB_error (void) { |
287 | { | ||
288 | lua_error(lua_getstring(lua_getparam(1))); | 282 | lua_error(lua_getstring(lua_getparam(1))); |
289 | } | 283 | } |
290 | 284 | ||
291 | 285 | ||
292 | static void luaI_assert (void) | 286 | static void luaB_assert (void) { |
293 | { | ||
294 | lua_Object p = lua_getparam(1); | 287 | lua_Object p = lua_getparam(1); |
295 | if (p == LUA_NOOBJECT || lua_isnil(p)) | 288 | if (p == LUA_NOOBJECT || lua_isnil(p)) |
296 | luaL_verror("assertion failed! %.100s", luaL_opt_string(2, "")); | 289 | luaL_verror("assertion failed! %.100s", luaL_opt_string(2, "")); |
297 | } | 290 | } |
298 | 291 | ||
299 | 292 | ||
300 | static void setglobal (void) | 293 | static void luaB_setglobal (void) { |
301 | { | ||
302 | char *n = luaL_check_string(1); | 294 | char *n = luaL_check_string(1); |
303 | lua_Object value = luaL_nonnullarg(2); | 295 | lua_Object value = luaL_nonnullarg(2); |
304 | lua_pushobject(value); | 296 | lua_pushobject(value); |
@@ -306,8 +298,7 @@ static void setglobal (void) | |||
306 | lua_pushobject(value); /* return given value */ | 298 | lua_pushobject(value); /* return given value */ |
307 | } | 299 | } |
308 | 300 | ||
309 | static void rawsetglobal (void) | 301 | static void luaB_rawsetglobal (void) { |
310 | { | ||
311 | char *n = luaL_check_string(1); | 302 | char *n = luaL_check_string(1); |
312 | lua_Object value = luaL_nonnullarg(2); | 303 | lua_Object value = luaL_nonnullarg(2); |
313 | lua_pushobject(value); | 304 | lua_pushobject(value); |
@@ -315,23 +306,20 @@ static void rawsetglobal (void) | |||
315 | lua_pushobject(value); /* return given value */ | 306 | lua_pushobject(value); /* return given value */ |
316 | } | 307 | } |
317 | 308 | ||
318 | static void getglobal (void) | 309 | static void luaB_getglobal (void) { |
319 | { | ||
320 | lua_pushobject(lua_getglobal(luaL_check_string(1))); | 310 | lua_pushobject(lua_getglobal(luaL_check_string(1))); |
321 | } | 311 | } |
322 | 312 | ||
323 | static void rawgetglobal (void) | 313 | static void luaB_rawgetglobal (void) { |
324 | { | ||
325 | lua_pushobject(lua_rawgetglobal(luaL_check_string(1))); | 314 | lua_pushobject(lua_rawgetglobal(luaL_check_string(1))); |
326 | } | 315 | } |
327 | 316 | ||
328 | static void luatag (void) | 317 | static void luaB_luatag (void) { |
329 | { | ||
330 | lua_pushnumber(lua_tag(lua_getparam(1))); | 318 | lua_pushnumber(lua_tag(lua_getparam(1))); |
331 | } | 319 | } |
332 | 320 | ||
333 | 321 | ||
334 | static void luaI_call (void) { | 322 | static void luaB_call (void) { |
335 | lua_Object f = luaL_nonnullarg(1); | 323 | lua_Object f = luaL_nonnullarg(1); |
336 | lua_Object arg = luaL_tablearg(2); | 324 | lua_Object arg = luaL_tablearg(2); |
337 | char *options = luaL_opt_string(3, ""); | 325 | char *options = luaL_opt_string(3, ""); |
@@ -368,8 +356,7 @@ static void luaI_call (void) { | |||
368 | } | 356 | } |
369 | 357 | ||
370 | 358 | ||
371 | static void settag (void) | 359 | static void luaB_settag (void) { |
372 | { | ||
373 | lua_Object o = luaL_tablearg(1); | 360 | lua_Object o = luaL_tablearg(1); |
374 | lua_pushobject(o); | 361 | lua_pushobject(o); |
375 | lua_settag(luaL_check_number(2)); | 362 | lua_settag(luaL_check_number(2)); |
@@ -377,29 +364,25 @@ static void settag (void) | |||
377 | } | 364 | } |
378 | 365 | ||
379 | 366 | ||
380 | static void newtag (void) | 367 | static void luaB_newtag (void) { |
381 | { | ||
382 | lua_pushnumber(lua_newtag()); | 368 | lua_pushnumber(lua_newtag()); |
383 | } | 369 | } |
384 | 370 | ||
385 | 371 | ||
386 | static void copytagmethods (void) | 372 | static void luaB_copytagmethods (void) { |
387 | { | ||
388 | lua_pushnumber(lua_copytagmethods(luaL_check_number(1), | 373 | lua_pushnumber(lua_copytagmethods(luaL_check_number(1), |
389 | luaL_check_number(2))); | 374 | luaL_check_number(2))); |
390 | } | 375 | } |
391 | 376 | ||
392 | 377 | ||
393 | static void rawgettable (void) | 378 | static void luaB_rawgettable (void) { |
394 | { | ||
395 | lua_pushobject(luaL_nonnullarg(1)); | 379 | lua_pushobject(luaL_nonnullarg(1)); |
396 | lua_pushobject(luaL_nonnullarg(2)); | 380 | lua_pushobject(luaL_nonnullarg(2)); |
397 | lua_pushobject(lua_rawgettable()); | 381 | lua_pushobject(lua_rawgettable()); |
398 | } | 382 | } |
399 | 383 | ||
400 | 384 | ||
401 | static void rawsettable (void) | 385 | static void luaB_rawsettable (void) { |
402 | { | ||
403 | lua_pushobject(luaL_nonnullarg(1)); | 386 | lua_pushobject(luaL_nonnullarg(1)); |
404 | lua_pushobject(luaL_nonnullarg(2)); | 387 | lua_pushobject(luaL_nonnullarg(2)); |
405 | lua_pushobject(luaL_nonnullarg(3)); | 388 | lua_pushobject(luaL_nonnullarg(3)); |
@@ -407,8 +390,7 @@ static void rawsettable (void) | |||
407 | } | 390 | } |
408 | 391 | ||
409 | 392 | ||
410 | static void settagmethod (void) | 393 | static void luaB_settagmethod (void) { |
411 | { | ||
412 | lua_Object nf = luaL_nonnullarg(3); | 394 | lua_Object nf = luaL_nonnullarg(3); |
413 | lua_pushobject(nf); | 395 | lua_pushobject(nf); |
414 | lua_pushobject(lua_settagmethod((int)luaL_check_number(1), | 396 | lua_pushobject(lua_settagmethod((int)luaL_check_number(1), |
@@ -416,23 +398,20 @@ static void settagmethod (void) | |||
416 | } | 398 | } |
417 | 399 | ||
418 | 400 | ||
419 | static void gettagmethod (void) | 401 | static void luaB_gettagmethod (void) { |
420 | { | ||
421 | lua_pushobject(lua_gettagmethod((int)luaL_check_number(1), | 402 | lua_pushobject(lua_gettagmethod((int)luaL_check_number(1), |
422 | luaL_check_string(2))); | 403 | luaL_check_string(2))); |
423 | } | 404 | } |
424 | 405 | ||
425 | 406 | ||
426 | static void seterrormethod (void) | 407 | static void luaB_seterrormethod (void) { |
427 | { | ||
428 | lua_Object nf = luaL_functionarg(1); | 408 | lua_Object nf = luaL_functionarg(1); |
429 | lua_pushobject(nf); | 409 | lua_pushobject(nf); |
430 | lua_pushobject(lua_seterrormethod()); | 410 | lua_pushobject(lua_seterrormethod()); |
431 | } | 411 | } |
432 | 412 | ||
433 | 413 | ||
434 | static void luaI_collectgarbage (void) | 414 | static void luaB_collectgarbage (void) { |
435 | { | ||
436 | lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0))); | 415 | lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0))); |
437 | } | 416 | } |
438 | 417 | ||
@@ -519,15 +498,13 @@ static void luaB_sort (void) { | |||
519 | */ | 498 | */ |
520 | #ifdef DEBUG | 499 | #ifdef DEBUG |
521 | 500 | ||
522 | static void mem_query (void) | 501 | static void mem_query (void) { |
523 | { | ||
524 | lua_pushnumber(totalmem); | 502 | lua_pushnumber(totalmem); |
525 | lua_pushnumber(numblocks); | 503 | lua_pushnumber(numblocks); |
526 | } | 504 | } |
527 | 505 | ||
528 | 506 | ||
529 | static void countlist (void) | 507 | static void countlist (void) { |
530 | { | ||
531 | char *s = luaL_check_string(1); | 508 | char *s = luaL_check_string(1); |
532 | GCnode *l = (s[0]=='t') ? L->roottable.next : (s[0]=='c') ? L->rootcl.next : | 509 | GCnode *l = (s[0]=='t') ? L->roottable.next : (s[0]=='c') ? L->rootcl.next : |
533 | (s[0]=='p') ? L->rootproto.next : L->rootglobal.next; | 510 | (s[0]=='p') ? L->rootproto.next : L->rootglobal.next; |
@@ -540,8 +517,7 @@ static void countlist (void) | |||
540 | } | 517 | } |
541 | 518 | ||
542 | 519 | ||
543 | static void testC (void) | 520 | static void testC (void) { |
544 | { | ||
545 | #define getnum(s) ((*s++) - '0') | 521 | #define getnum(s) ((*s++) - '0') |
546 | #define getname(s) (nome[0] = *s++, nome) | 522 | #define getname(s) (nome[0] = *s++, nome) |
547 | 523 | ||
@@ -603,37 +579,37 @@ static struct luaL_reg int_funcs[] = { | |||
603 | {"totalmem", mem_query}, | 579 | {"totalmem", mem_query}, |
604 | {"count", countlist}, | 580 | {"count", countlist}, |
605 | #endif | 581 | #endif |
606 | {"assert", luaI_assert}, | 582 | {"assert", luaB_assert}, |
607 | {"call", luaI_call}, | 583 | {"call", luaB_call}, |
608 | {"collectgarbage", luaI_collectgarbage}, | 584 | {"collectgarbage", luaB_collectgarbage}, |
609 | {"dofile", internaldofile}, | 585 | {"dofile", luaB_dofile}, |
610 | {"copytagmethods", copytagmethods}, | 586 | {"copytagmethods", luaB_copytagmethods}, |
611 | {"dostring", internaldostring}, | 587 | {"dostring", luaB_dostring}, |
612 | {"error", luaI_error}, | 588 | {"error", luaB_error}, |
613 | {"_ERRORMESSAGE", error_message}, | 589 | {"_ERRORMESSAGE", error_message}, |
614 | {"foreach", foreach}, | 590 | {"foreach", luaB_foreach}, |
615 | {"foreachi", foreachi}, | 591 | {"foreachi", luaB_foreachi}, |
616 | {"foreachvar", foreachvar}, | 592 | {"foreachvar", luaB_foreachvar}, |
617 | {"getn", getn}, | 593 | {"getn", luaB_getn}, |
618 | {"getglobal", getglobal}, | 594 | {"getglobal", luaB_getglobal}, |
619 | {"newtag", newtag}, | 595 | {"newtag", luaB_newtag}, |
620 | {"next", next}, | 596 | {"next", luaB_next}, |
621 | {"nextvar", nextvar}, | 597 | {"nextvar", luaB_nextvar}, |
622 | {"print", luaI_print}, | 598 | {"print", luaB_print}, |
623 | {"rawgetglobal", rawgetglobal}, | 599 | {"rawgetglobal", luaB_rawgetglobal}, |
624 | {"rawgettable", rawgettable}, | 600 | {"rawgettable", luaB_rawgettable}, |
625 | {"rawsetglobal", rawsetglobal}, | 601 | {"rawsetglobal", luaB_rawsetglobal}, |
626 | {"rawsettable", rawsettable}, | 602 | {"rawsettable", luaB_rawsettable}, |
627 | {"seterrormethod", seterrormethod}, | 603 | {"seterrormethod", luaB_seterrormethod}, |
628 | {"setglobal", setglobal}, | 604 | {"setglobal", luaB_setglobal}, |
629 | {"settagmethod", settagmethod}, | 605 | {"settagmethod", luaB_settagmethod}, |
630 | {"gettagmethod", gettagmethod}, | 606 | {"gettagmethod", luaB_gettagmethod}, |
631 | {"settag", settag}, | 607 | {"settag", luaB_settag}, |
632 | {"sort", luaB_sort}, | 608 | {"sort", luaB_sort}, |
633 | {"tonumber", luaB_tonumber}, | 609 | {"tonumber", luaB_tonumber}, |
634 | {"tostring", to_string}, | 610 | {"tostring", luaB_tostring}, |
635 | {"tag", luatag}, | 611 | {"tag", luaB_luatag}, |
636 | {"type", luaI_type}, | 612 | {"type", luaB_type}, |
637 | {"_ALERT", luaB_message} | 613 | {"_ALERT", luaB_message} |
638 | }; | 614 | }; |
639 | 615 | ||
@@ -641,8 +617,7 @@ static struct luaL_reg int_funcs[] = { | |||
641 | #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0])) | 617 | #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0])) |
642 | 618 | ||
643 | 619 | ||
644 | void luaB_predefine (void) | 620 | void luaB_predefine (void) { |
645 | { | ||
646 | /* pre-register mem error messages, to avoid loop when error arises */ | 621 | /* pre-register mem error messages, to avoid loop when error arises */ |
647 | luaS_newfixedstring(tableEM); | 622 | luaS_newfixedstring(tableEM); |
648 | luaS_newfixedstring(memEM); | 623 | luaS_newfixedstring(memEM); |