diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-03 14:46:34 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-03 14:46:34 -0300 |
| commit | 35a22ed1ab15fabb9404d0184165baa1d52394f1 (patch) | |
| tree | 2dc281a4897de0eac364fa15695d11db030cd079 /lapi.c | |
| parent | ff91b355f451c7b7fc286d8ca9bc8613096f497d (diff) | |
| download | lua-35a22ed1ab15fabb9404d0184165baa1d52394f1.tar.gz lua-35a22ed1ab15fabb9404d0184165baa1d52394f1.tar.bz2 lua-35a22ed1ab15fabb9404d0184165baa1d52394f1.zip | |
lua_load* replaced by a simple lua_load
Diffstat (limited to 'lapi.c')
| -rw-r--r-- | lapi.c | 122 |
1 files changed, 12 insertions, 110 deletions
| @@ -1,11 +1,10 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 1.192 2002/05/16 18:39:46 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.193 2002/05/27 20:35:40 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 <stdio.h> | ||
| 9 | #include <string.h> | 8 | #include <string.h> |
| 10 | 9 | ||
| 11 | #include "lua.h" | 10 | #include "lua.h" |
| @@ -31,11 +30,6 @@ const char lua_ident[] = | |||
| 31 | "$URL: www.lua.org $\n"; | 30 | "$URL: www.lua.org $\n"; |
| 32 | 31 | ||
| 33 | 32 | ||
| 34 | #ifndef lua_filerror | ||
| 35 | #include <errno.h> | ||
| 36 | #define lua_fileerror (strerror(errno)) | ||
| 37 | #endif | ||
| 38 | |||
| 39 | 33 | ||
| 40 | #ifndef api_check | 34 | #ifndef api_check |
| 41 | #define api_check(L, o) ((void)1) | 35 | #define api_check(L, o) ((void)1) |
| @@ -368,9 +362,11 @@ LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, | |||
| 368 | LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { | 362 | LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { |
| 369 | const char *ret; | 363 | const char *ret; |
| 370 | va_list argp; | 364 | va_list argp; |
| 365 | lua_lock(L); | ||
| 371 | va_start(argp, fmt); | 366 | va_start(argp, fmt); |
| 372 | ret = lua_pushvfstring(L, fmt, argp); | 367 | ret = luaO_pushvfstring(L, fmt, argp); |
| 373 | va_end(argp); | 368 | va_end(argp); |
| 369 | lua_unlock(L); | ||
| 374 | return ret; | 370 | return ret; |
| 375 | } | 371 | } |
| 376 | 372 | ||
| @@ -567,54 +563,19 @@ LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf) { | |||
| 567 | } | 563 | } |
| 568 | 564 | ||
| 569 | 565 | ||
| 570 | static int errfile (lua_State *L, const char *filename) { | 566 | LUA_API int lua_load (lua_State *L, lua_Getblock getblock, void *ud, |
| 571 | if (filename == NULL) filename = "stdin"; | 567 | int binary, const char *chunkname) { |
| 572 | lua_pushfstring(L, "cannot read %s: %s", filename, lua_fileerror); | ||
| 573 | return LUA_ERRFILE; | ||
| 574 | } | ||
| 575 | |||
| 576 | |||
| 577 | LUA_API int lua_loadfile (lua_State *L, const char *filename) { | ||
| 578 | ZIO z; | 568 | ZIO z; |
| 579 | int fnindex; | ||
| 580 | int status; | 569 | int status; |
| 581 | int bin; /* flag for file mode */ | 570 | lua_lock(L); |
| 582 | FILE *f = (filename == NULL) ? stdin : fopen(filename, "r"); | 571 | if (!chunkname) chunkname = "?"; |
| 583 | if (f == NULL) return errfile(L, filename); /* unable to open file */ | 572 | luaZ_init(&z, getblock, ud, chunkname); |
| 584 | bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]); | 573 | status = luaD_protectedparser(L, &z, binary); |
| 585 | if (bin && f != stdin) { | 574 | lua_unlock(L); |
| 586 | fclose(f); | ||
| 587 | f = fopen(filename, "rb"); /* reopen in binary mode */ | ||
| 588 | if (f == NULL) return errfile(L, filename); /* unable to reopen file */ | ||
| 589 | } | ||
| 590 | if (filename == NULL) | ||
| 591 | lua_pushliteral(L, "=stdin"); | ||
| 592 | else | ||
| 593 | lua_pushfstring(L, "@%s", filename); | ||
| 594 | fnindex = lua_gettop(L); /* stack index of file name */ | ||
| 595 | luaZ_Fopen(&z, f, lua_tostring(L, fnindex)); | ||
| 596 | status = luaD_protectedparser(L, &z, bin); | ||
| 597 | lua_remove(L, fnindex); | ||
| 598 | if (ferror(f)) { | ||
| 599 | if (status == 0) lua_pop(L, 1); /* remove chunk */ | ||
| 600 | return errfile(L, filename); | ||
| 601 | } | ||
| 602 | if (f != stdin) | ||
| 603 | fclose(f); | ||
| 604 | return status; | 575 | return status; |
| 605 | } | 576 | } |
| 606 | 577 | ||
| 607 | 578 | ||
| 608 | LUA_API int lua_loadbuffer (lua_State *L, const char *buff, size_t size, | ||
| 609 | const char *name) { | ||
| 610 | ZIO z; | ||
| 611 | if (!name) name = "?"; | ||
| 612 | luaZ_mopen(&z, buff, size, name); | ||
| 613 | return luaD_protectedparser(L, &z, buff[0]==LUA_SIGNATURE[0]); | ||
| 614 | } | ||
| 615 | |||
| 616 | |||
| 617 | |||
| 618 | /* | 579 | /* |
| 619 | ** Garbage-collection functions | 580 | ** Garbage-collection functions |
| 620 | */ | 581 | */ |
| @@ -722,7 +683,7 @@ LUA_API void lua_concat (lua_State *L, int n) { | |||
| 722 | lua_lock(L); | 683 | lua_lock(L); |
| 723 | api_checknelems(L, n); | 684 | api_checknelems(L, n); |
| 724 | if (n >= 2) { | 685 | if (n >= 2) { |
| 725 | luaV_strconc(L, n, L->top - L->ci->base - 1); | 686 | luaV_concat(L, n, L->top - L->ci->base - 1); |
| 726 | L->top -= (n-1); | 687 | L->top -= (n-1); |
| 727 | luaC_checkGC(L); | 688 | luaC_checkGC(L); |
| 728 | } | 689 | } |
| @@ -763,62 +724,3 @@ LUA_API int lua_pushupvalues (lua_State *L) { | |||
| 763 | } | 724 | } |
| 764 | 725 | ||
| 765 | 726 | ||
| 766 | |||
| 767 | /* | ||
| 768 | ** {====================================================== | ||
| 769 | ** compatibility code | ||
| 770 | ** ======================================================= | ||
| 771 | */ | ||
| 772 | |||
| 773 | |||
| 774 | static void callalert (lua_State *L, int status) { | ||
| 775 | if (status != 0) { | ||
| 776 | int top = lua_gettop(L); | ||
| 777 | lua_getglobal(L, "_ALERT"); | ||
| 778 | lua_insert(L, -2); | ||
| 779 | lua_pcall(L, 1, 0, 0); | ||
| 780 | lua_settop(L, top-1); | ||
| 781 | } | ||
| 782 | } | ||
| 783 | |||
| 784 | |||
| 785 | LUA_API int lua_call (lua_State *L, int nargs, int nresults) { | ||
| 786 | int status; | ||
| 787 | int errpos = lua_gettop(L) - nargs; | ||
| 788 | lua_getglobal(L, "_ERRORMESSAGE"); | ||
| 789 | lua_insert(L, errpos); /* put below function and args */ | ||
| 790 | status = lua_pcall(L, nargs, nresults, errpos); | ||
| 791 | lua_remove(L, errpos); | ||
| 792 | callalert(L, status); | ||
| 793 | return status; | ||
| 794 | } | ||
| 795 | |||
| 796 | static int aux_do (lua_State *L, int status) { | ||
| 797 | if (status == 0) { /* parse OK? */ | ||
| 798 | int err = lua_gettop(L); | ||
| 799 | lua_getglobal(L, "_ERRORMESSAGE"); | ||
| 800 | lua_insert(L, err); | ||
| 801 | status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */ | ||
| 802 | lua_remove(L, err); /* remove error function */ | ||
| 803 | } | ||
| 804 | callalert(L, status); | ||
| 805 | return status; | ||
| 806 | } | ||
| 807 | |||
| 808 | |||
| 809 | LUA_API int lua_dofile (lua_State *L, const char *filename) { | ||
| 810 | return aux_do(L, lua_loadfile(L, filename)); | ||
| 811 | } | ||
| 812 | |||
| 813 | |||
| 814 | LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, | ||
| 815 | const char *name) { | ||
| 816 | return aux_do(L, lua_loadbuffer(L, buff, size, name)); | ||
| 817 | } | ||
| 818 | |||
| 819 | |||
| 820 | LUA_API int lua_dostring (lua_State *L, const char *str) { | ||
| 821 | return lua_dobuffer(L, str, strlen(str), str); | ||
| 822 | } | ||
| 823 | |||
| 824 | /* }====================================================== */ | ||
