diff options
Diffstat (limited to 'lbuiltin.c')
-rw-r--r-- | lbuiltin.c | 172 |
1 files changed, 83 insertions, 89 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbuiltin.c,v 1.52 1999/02/22 14:17:24 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.53 1999/02/22 19:13:12 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 | */ |
@@ -300,9 +300,76 @@ static void luaB_call (void) { | |||
300 | } | 300 | } |
301 | } | 301 | } |
302 | 302 | ||
303 | |||
304 | static void luaB_nextvar (void) { | ||
305 | TObject *o = luaA_Address(luaL_nonnullarg(1)); | ||
306 | TaggedString *g; | ||
307 | if (ttype(o) == LUA_T_NIL) | ||
308 | g = NULL; | ||
309 | else { | ||
310 | luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected"); | ||
311 | g = tsvalue(o); | ||
312 | } | ||
313 | if (!luaA_nextvar(g)) | ||
314 | lua_pushnil(); | ||
315 | } | ||
316 | |||
317 | |||
318 | static void luaB_next (void) { | ||
319 | Hash *a = gethash(1); | ||
320 | TObject *k = luaA_Address(luaL_nonnullarg(2)); | ||
321 | int i = (ttype(k) == LUA_T_NIL) ? 0 : luaH_pos(a, k)+1; | ||
322 | if (luaA_next(a, i) == 0) | ||
323 | lua_pushnil(); | ||
324 | } | ||
325 | |||
326 | |||
327 | static void luaB_tostring (void) { | ||
328 | lua_Object obj = lua_getparam(1); | ||
329 | TObject *o = luaA_Address(obj); | ||
330 | char buff[64]; | ||
331 | switch (ttype(o)) { | ||
332 | case LUA_T_NUMBER: | ||
333 | lua_pushstring(lua_getstring(obj)); | ||
334 | return; | ||
335 | case LUA_T_STRING: | ||
336 | lua_pushobject(obj); | ||
337 | return; | ||
338 | case LUA_T_ARRAY: | ||
339 | sprintf(buff, "table: %p", (void *)o->value.a); | ||
340 | break; | ||
341 | case LUA_T_CLOSURE: | ||
342 | sprintf(buff, "function: %p", (void *)o->value.cl); | ||
343 | break; | ||
344 | case LUA_T_PROTO: | ||
345 | sprintf(buff, "function: %p", (void *)o->value.tf); | ||
346 | break; | ||
347 | case LUA_T_CPROTO: | ||
348 | sprintf(buff, "function: %p", (void *)o->value.f); | ||
349 | break; | ||
350 | case LUA_T_USERDATA: | ||
351 | sprintf(buff, "userdata: %p", o->value.ts->u.d.v); | ||
352 | break; | ||
353 | case LUA_T_NIL: | ||
354 | lua_pushstring("nil"); | ||
355 | return; | ||
356 | default: | ||
357 | LUA_INTERNALERROR("invalid type"); | ||
358 | } | ||
359 | lua_pushstring(buff); | ||
360 | } | ||
361 | |||
362 | |||
363 | static void luaB_type (void) { | ||
364 | lua_Object o = luaL_nonnullarg(1); | ||
365 | lua_pushstring(luaO_typename(luaA_Address(o))); | ||
366 | lua_pushnumber(lua_tag(o)); | ||
367 | } | ||
368 | |||
303 | /* }====================================================== */ | 369 | /* }====================================================== */ |
304 | 370 | ||
305 | 371 | ||
372 | |||
306 | /* | 373 | /* |
307 | ** {====================================================== | 374 | ** {====================================================== |
308 | ** "Extra" functions | 375 | ** "Extra" functions |
@@ -352,7 +419,7 @@ static void luaB_foreach (void) { | |||
352 | luaD_calln(2, 1); | 419 | luaD_calln(2, 1); |
353 | if (ttype(L->stack.top-1) != LUA_T_NIL) | 420 | if (ttype(L->stack.top-1) != LUA_T_NIL) |
354 | return; | 421 | return; |
355 | L->stack.top--; | 422 | L->stack.top--; /* remove result */ |
356 | } | 423 | } |
357 | } | 424 | } |
358 | } | 425 | } |
@@ -361,22 +428,21 @@ static void luaB_foreach (void) { | |||
361 | static void luaB_foreachvar (void) { | 428 | static void luaB_foreachvar (void) { |
362 | TObject *f = luaA_Address(luaL_functionarg(1)); | 429 | TObject *f = luaA_Address(luaL_functionarg(1)); |
363 | GCnode *g; | 430 | GCnode *g; |
364 | StkId name = L->Cstack.base++; /* place to keep var name (to avoid GC) */ | 431 | luaD_checkstack(4); /* for extra var name, f, var name, and globalval */ |
365 | luaD_checkstack(4); /* for var name, f, s, and globalvar */ | ||
366 | ttype(L->stack.stack+name) = LUA_T_NIL; | ||
367 | L->stack.top++; /* top == base */ | ||
368 | for (g = L->rootglobal.next; g; g = g->next) { | 432 | for (g = L->rootglobal.next; g; g = g->next) { |
369 | TaggedString *s = (TaggedString *)g; | 433 | TaggedString *s = (TaggedString *)g; |
370 | if (s->u.s.globalval.ttype != LUA_T_NIL) { | 434 | if (s->u.s.globalval.ttype != LUA_T_NIL) { |
371 | ttype(L->stack.stack+name) = LUA_T_STRING; | 435 | pushtagstring(s); /* keep (extra) s on stack to avoid GC */ |
372 | tsvalue(L->stack.stack+name) = s; /* keep s on stack to avoid GC */ | ||
373 | *(L->stack.top++) = *f; | 436 | *(L->stack.top++) = *f; |
374 | pushtagstring(s); | 437 | pushtagstring(s); |
375 | *(L->stack.top++) = s->u.s.globalval; | 438 | *(L->stack.top++) = s->u.s.globalval; |
376 | luaD_calln(2, 1); | 439 | luaD_calln(2, 1); |
377 | if (ttype(L->stack.top-1) != LUA_T_NIL) | 440 | if (ttype(L->stack.top-1) != LUA_T_NIL) { |
441 | L->stack.top--; | ||
442 | *(L->stack.top-1) = *L->stack.top; /* remove extra s */ | ||
378 | return; | 443 | return; |
379 | L->stack.top--; | 444 | } |
445 | L->stack.top-=2; /* remove result and extra s */ | ||
380 | } | 446 | } |
381 | } | 447 | } |
382 | } | 448 | } |
@@ -505,85 +571,7 @@ static void luaB_sort (void) { | |||
505 | 571 | ||
506 | 572 | ||
507 | /* | 573 | /* |
508 | ** {====================================================== | 574 | ** ====================================================== */ |
509 | ** Internal Functions. | ||
510 | ** These functions need access to internal structures | ||
511 | ** to be implemented. | ||
512 | ** ======================================================= | ||
513 | */ | ||
514 | |||
515 | |||
516 | static void luaB_nextvar (void) { | ||
517 | TObject *o = luaA_Address(luaL_nonnullarg(1)); | ||
518 | TaggedString *g; | ||
519 | if (ttype(o) == LUA_T_NIL) | ||
520 | g = NULL; | ||
521 | else { | ||
522 | luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected"); | ||
523 | g = tsvalue(o); | ||
524 | } | ||
525 | g = luaA_nextvar(g); | ||
526 | if (g) { | ||
527 | pushtagstring(g); | ||
528 | luaA_pushobject(&g->u.s.globalval); | ||
529 | } | ||
530 | else lua_pushnil(); /* no more globals */ | ||
531 | } | ||
532 | |||
533 | |||
534 | static void luaB_next (void) { | ||
535 | Node *n = luaH_next(gethash(1), luaA_Address(luaL_nonnullarg(2))); | ||
536 | if (n) { | ||
537 | luaA_pushobject(&n->ref); | ||
538 | luaA_pushobject(&n->val); | ||
539 | } | ||
540 | else lua_pushnil(); | ||
541 | } | ||
542 | |||
543 | |||
544 | static void luaB_tostring (void) { | ||
545 | lua_Object obj = lua_getparam(1); | ||
546 | TObject *o = luaA_Address(obj); | ||
547 | char buff[64]; | ||
548 | switch (ttype(o)) { | ||
549 | case LUA_T_NUMBER: | ||
550 | lua_pushstring(lua_getstring(obj)); | ||
551 | return; | ||
552 | case LUA_T_STRING: | ||
553 | lua_pushobject(obj); | ||
554 | return; | ||
555 | case LUA_T_ARRAY: | ||
556 | sprintf(buff, "table: %p", (void *)o->value.a); | ||
557 | break; | ||
558 | case LUA_T_CLOSURE: | ||
559 | sprintf(buff, "function: %p", (void *)o->value.cl); | ||
560 | break; | ||
561 | case LUA_T_PROTO: | ||
562 | sprintf(buff, "function: %p", (void *)o->value.tf); | ||
563 | break; | ||
564 | case LUA_T_CPROTO: | ||
565 | sprintf(buff, "function: %p", (void *)o->value.f); | ||
566 | break; | ||
567 | case LUA_T_USERDATA: | ||
568 | sprintf(buff, "userdata: %p", o->value.ts->u.d.v); | ||
569 | break; | ||
570 | case LUA_T_NIL: | ||
571 | lua_pushstring("nil"); | ||
572 | return; | ||
573 | default: | ||
574 | LUA_INTERNALERROR("invalid type"); | ||
575 | } | ||
576 | lua_pushstring(buff); | ||
577 | } | ||
578 | |||
579 | |||
580 | static void luaB_type (void) { | ||
581 | lua_Object o = luaL_nonnullarg(1); | ||
582 | lua_pushstring(luaO_typename(luaA_Address(o))); | ||
583 | lua_pushnumber(lua_tag(o)); | ||
584 | } | ||
585 | |||
586 | /* }====================================================== */ | ||
587 | 575 | ||
588 | 576 | ||
589 | 577 | ||
@@ -658,6 +646,12 @@ static void testC (void) { | |||
658 | case 'I': reg[getnum(s)] = lua_rawgettable(); break; | 646 | case 'I': reg[getnum(s)] = lua_rawgettable(); break; |
659 | case 't': lua_settable(); break; | 647 | case 't': lua_settable(); break; |
660 | case 'T': lua_rawsettable(); break; | 648 | case 'T': lua_rawsettable(); break; |
649 | case 'N' : lua_pushstring(lua_nextvar(lua_getstring(reg[getnum(s)]))); | ||
650 | break; | ||
651 | case 'n' : { int n=getnum(s); | ||
652 | n=lua_next(reg[n], lua_getnumber(reg[getnum(s)])); | ||
653 | lua_pushnumber(n); break; | ||
654 | } | ||
661 | default: luaL_verror("unknown command in `testC': %c", *(s-1)); | 655 | default: luaL_verror("unknown command in `testC': %c", *(s-1)); |
662 | } | 656 | } |
663 | if (*s == 0) return; | 657 | if (*s == 0) return; |