diff options
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 118 |
1 files changed, 75 insertions, 43 deletions
@@ -1,10 +1,12 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.184 2002/04/16 17:08:28 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.185 2002/04/22 14:40:50 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
6 | 6 | ||
7 | 7 | ||
8 | #include <errno.h> | ||
9 | #include <stdio.h> | ||
8 | #include <string.h> | 10 | #include <string.h> |
9 | 11 | ||
10 | #include "lua.h" | 12 | #include "lua.h" |
@@ -516,7 +518,7 @@ LUA_API void lua_setmetatable (lua_State *L, int objindex) { | |||
516 | 518 | ||
517 | 519 | ||
518 | /* | 520 | /* |
519 | ** `do' functions (run Lua code) | 521 | ** `load' and `call' functions (run Lua code) |
520 | */ | 522 | */ |
521 | 523 | ||
522 | LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) { | 524 | LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) { |
@@ -529,17 +531,6 @@ LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) { | |||
529 | } | 531 | } |
530 | 532 | ||
531 | 533 | ||
532 | LUA_API int lua_call (lua_State *L, int nargs, int nresults) { | ||
533 | int status; | ||
534 | int errpos = lua_gettop(L) - nargs; | ||
535 | lua_getglobal(L, "_ERRORMESSAGE"); | ||
536 | lua_insert(L, errpos); /* put below function and args */ | ||
537 | status = lua_pcall(L, nargs, nresults, errpos); | ||
538 | lua_remove(L, errpos); | ||
539 | return status; | ||
540 | } | ||
541 | |||
542 | |||
543 | LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf) { | 534 | LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf) { |
544 | int status; | 535 | int status; |
545 | const TObject *err; | 536 | const TObject *err; |
@@ -551,31 +542,11 @@ LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf) { | |||
551 | } | 542 | } |
552 | 543 | ||
553 | 544 | ||
554 | static int aux_do (lua_State *L, int status) { | 545 | static int errfile (lua_State *L, const char *filename) { |
555 | if (status == 0) { /* parse OK? */ | 546 | char buff[150]; |
556 | int err = lua_gettop(L); | 547 | sprintf(buff, "cannot read file `%.80s' (%.40s)", filename, strerror(errno)); |
557 | lua_getglobal(L, "_ERRORMESSAGE"); | 548 | lua_pushstring(L, buff); |
558 | lua_insert(L, err); | 549 | return LUA_ERRFILE; |
559 | status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */ | ||
560 | lua_remove(L, err); /* remove error function */ | ||
561 | } | ||
562 | return status; | ||
563 | } | ||
564 | |||
565 | |||
566 | LUA_API int lua_dofile (lua_State *L, const char *filename) { | ||
567 | return aux_do(L, lua_loadfile(L, filename)); | ||
568 | } | ||
569 | |||
570 | |||
571 | LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, | ||
572 | const char *name) { | ||
573 | return aux_do(L, lua_loadbuffer(L, buff, size, name)); | ||
574 | } | ||
575 | |||
576 | |||
577 | LUA_API int lua_dostring (lua_State *L, const char *str) { | ||
578 | return lua_dobuffer(L, str, strlen(str), str); | ||
579 | } | 550 | } |
580 | 551 | ||
581 | 552 | ||
@@ -585,12 +556,12 @@ LUA_API int lua_loadfile (lua_State *L, const char *filename) { | |||
585 | int bin; /* flag for file mode */ | 556 | int bin; /* flag for file mode */ |
586 | int nlevel; /* level on the stack of filename */ | 557 | int nlevel; /* level on the stack of filename */ |
587 | FILE *f = (filename == NULL) ? stdin : fopen(filename, "r"); | 558 | FILE *f = (filename == NULL) ? stdin : fopen(filename, "r"); |
588 | if (f == NULL) return LUA_ERRFILE; /* unable to open file */ | 559 | if (f == NULL) return errfile(L, filename); /* unable to open file */ |
589 | bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]); | 560 | bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]); |
590 | if (bin && f != stdin) { | 561 | if (bin && f != stdin) { |
591 | fclose(f); | 562 | fclose(f); |
592 | f = fopen(filename, "rb"); /* reopen in binary mode */ | 563 | f = fopen(filename, "rb"); /* reopen in binary mode */ |
593 | if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */ | 564 | if (f == NULL) return errfile(L, filename); /* unable to reopen file */ |
594 | } | 565 | } |
595 | if (filename == NULL) | 566 | if (filename == NULL) |
596 | lua_pushstring(L, "=stdin"); | 567 | lua_pushstring(L, "=stdin"); |
@@ -603,7 +574,8 @@ LUA_API int lua_loadfile (lua_State *L, const char *filename) { | |||
603 | filename = lua_tostring(L, -1); /* filename = `@'..filename */ | 574 | filename = lua_tostring(L, -1); /* filename = `@'..filename */ |
604 | luaZ_Fopen(&z, f, filename); | 575 | luaZ_Fopen(&z, f, filename); |
605 | status = luaD_protectedparser(L, &z, bin); | 576 | status = luaD_protectedparser(L, &z, bin); |
606 | if (ferror(f)) status = LUA_ERRFILE; | 577 | if (ferror(f)) |
578 | return errfile(L, filename); | ||
607 | lua_remove(L, nlevel); /* remove filename */ | 579 | lua_remove(L, nlevel); /* remove filename */ |
608 | if (f != stdin) | 580 | if (f != stdin) |
609 | fclose(f); | 581 | fclose(f); |
@@ -661,9 +633,10 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) { | |||
661 | */ | 633 | */ |
662 | 634 | ||
663 | 635 | ||
664 | LUA_API void lua_error (lua_State *L, const char *s) { | 636 | LUA_API void lua_errorobj (lua_State *L) { |
665 | lua_lock(L); | 637 | lua_lock(L); |
666 | luaD_runerror(L, s); | 638 | api_checknelems(L, 1); |
639 | luaD_errorobj(L, L->top - 1, LUA_ERRRUN); | ||
667 | lua_unlock(L); | 640 | lua_unlock(L); |
668 | } | 641 | } |
669 | 642 | ||
@@ -767,3 +740,62 @@ LUA_API int lua_pushupvalues (lua_State *L) { | |||
767 | } | 740 | } |
768 | 741 | ||
769 | 742 | ||
743 | |||
744 | /* | ||
745 | ** {====================================================== | ||
746 | ** compatibility code | ||
747 | ** ======================================================= | ||
748 | */ | ||
749 | |||
750 | |||
751 | static void callalert (lua_State *L, int status) { | ||
752 | if (status != 0) { | ||
753 | int top = lua_gettop(L); | ||
754 | lua_getglobal(L, "_ALERT"); | ||
755 | lua_insert(L, -2); | ||
756 | lua_pcall(L, 1, 0, 0); | ||
757 | lua_settop(L, top-1); | ||
758 | } | ||
759 | } | ||
760 | |||
761 | |||
762 | LUA_API int lua_call (lua_State *L, int nargs, int nresults) { | ||
763 | int status; | ||
764 | int errpos = lua_gettop(L) - nargs; | ||
765 | lua_getglobal(L, "_ERRORMESSAGE"); | ||
766 | lua_insert(L, errpos); /* put below function and args */ | ||
767 | status = lua_pcall(L, nargs, nresults, errpos); | ||
768 | lua_remove(L, errpos); | ||
769 | callalert(L, status); | ||
770 | return status; | ||
771 | } | ||
772 | |||
773 | static int aux_do (lua_State *L, int status) { | ||
774 | if (status == 0) { /* parse OK? */ | ||
775 | int err = lua_gettop(L); | ||
776 | lua_getglobal(L, "_ERRORMESSAGE"); | ||
777 | lua_insert(L, err); | ||
778 | status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */ | ||
779 | lua_remove(L, err); /* remove error function */ | ||
780 | } | ||
781 | callalert(L, status); | ||
782 | return status; | ||
783 | } | ||
784 | |||
785 | |||
786 | LUA_API int lua_dofile (lua_State *L, const char *filename) { | ||
787 | return aux_do(L, lua_loadfile(L, filename)); | ||
788 | } | ||
789 | |||
790 | |||
791 | LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, | ||
792 | const char *name) { | ||
793 | return aux_do(L, lua_loadbuffer(L, buff, size, name)); | ||
794 | } | ||
795 | |||
796 | |||
797 | LUA_API int lua_dostring (lua_State *L, const char *str) { | ||
798 | return lua_dobuffer(L, str, strlen(str), str); | ||
799 | } | ||
800 | |||
801 | /* }====================================================== */ | ||