aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-04-22 11:40:50 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-04-22 11:40:50 -0300
commitee4859b3e3db6c1a3223669d15538b3852ca4791 (patch)
treea9a9532c03f0a9bc58599e34457cb618aa5f11f2 /lapi.c
parentf388ee4a822b3d8027ed7c28aa21e9406e4a11eb (diff)
downloadlua-ee4859b3e3db6c1a3223669d15538b3852ca4791.tar.gz
lua-ee4859b3e3db6c1a3223669d15538b3852ca4791.tar.bz2
lua-ee4859b3e3db6c1a3223669d15538b3852ca4791.zip
new way to handle errors (temporary version)
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c92
1 files changed, 80 insertions, 12 deletions
diff --git a/lapi.c b/lapi.c
index 72f38c93..4eeada6c 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.183 2002/04/05 18:54:31 roberto Exp roberto $ 2** $Id: lapi.c,v 1.184 2002/04/16 17:08:28 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*/
@@ -19,6 +19,7 @@
19#include "lstring.h" 19#include "lstring.h"
20#include "ltable.h" 20#include "ltable.h"
21#include "ltm.h" 21#include "ltm.h"
22#include "lundump.h"
22#include "lvm.h" 23#include "lvm.h"
23 24
24 25
@@ -528,30 +529,97 @@ LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
528} 529}
529 530
530 531
531LUA_API int lua_dofile (lua_State *L, const char *filename) { 532LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
532 int status; 533 int status;
533 status = lua_loadfile(L, filename); 534 int errpos = lua_gettop(L) - nargs;
534 if (status == 0) /* parse OK? */ 535 lua_getglobal(L, "_ERRORMESSAGE");
535 status = lua_call(L, 0, LUA_MULTRET); /* call main */ 536 lua_insert(L, errpos); /* put below function and args */
537 status = lua_pcall(L, nargs, nresults, errpos);
538 lua_remove(L, errpos);
536 return status; 539 return status;
537} 540}
538 541
539 542
540LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, 543LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf) {
541 const char *name) {
542 int status; 544 int status;
543 status = lua_loadbuffer(L, buff, size, name); 545 const TObject *err;
544 if (status == 0) /* parse OK? */ 546 lua_lock(L);
545 status = lua_call(L, 0, LUA_MULTRET); /* call main */ 547 err = (errf == 0) ? &luaO_nilobject : luaA_index(L, errf);
548 status = luaD_pcall(L, nargs, nresults, err);
549 lua_unlock(L);
550 return status;
551}
552
553
554static int aux_do (lua_State *L, int status) {
555 if (status == 0) { /* parse OK? */
556 int err = lua_gettop(L);
557 lua_getglobal(L, "_ERRORMESSAGE");
558 lua_insert(L, err);
559 status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */
560 lua_remove(L, err); /* remove error function */
561 }
546 return status; 562 return status;
547} 563}
548 564
549 565
566LUA_API int lua_dofile (lua_State *L, const char *filename) {
567 return aux_do(L, lua_loadfile(L, filename));
568}
569
570
571LUA_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
550LUA_API int lua_dostring (lua_State *L, const char *str) { 577LUA_API int lua_dostring (lua_State *L, const char *str) {
551 return lua_dobuffer(L, str, strlen(str), str); 578 return lua_dobuffer(L, str, strlen(str), str);
552} 579}
553 580
554 581
582LUA_API int lua_loadfile (lua_State *L, const char *filename) {
583 ZIO z;
584 int status;
585 int bin; /* flag for file mode */
586 int nlevel; /* level on the stack of filename */
587 FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
588 if (f == NULL) return LUA_ERRFILE; /* unable to open file */
589 bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]);
590 if (bin && f != stdin) {
591 fclose(f);
592 f = fopen(filename, "rb"); /* reopen in binary mode */
593 if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
594 }
595 if (filename == NULL)
596 lua_pushstring(L, "=stdin");
597 else {
598 lua_pushliteral(L, "@");
599 lua_pushstring(L, filename);
600 lua_concat(L, 2);
601 }
602 nlevel = lua_gettop(L);
603 filename = lua_tostring(L, -1); /* filename = `@'..filename */
604 luaZ_Fopen(&z, f, filename);
605 status = luaD_protectedparser(L, &z, bin);
606 if (ferror(f)) status = LUA_ERRFILE;
607 lua_remove(L, nlevel); /* remove filename */
608 if (f != stdin)
609 fclose(f);
610 return status;
611}
612
613
614LUA_API int lua_loadbuffer (lua_State *L, const char *buff, size_t size,
615 const char *name) {
616 ZIO z;
617 if (!name) name = "?";
618 luaZ_mopen(&z, buff, size, name);
619 return luaD_protectedparser(L, &z, buff[0]==LUA_SIGNATURE[0]);
620}
621
622
555 623
556/* 624/*
557** Garbage-collection functions 625** Garbage-collection functions
@@ -595,7 +663,7 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
595 663
596LUA_API void lua_error (lua_State *L, const char *s) { 664LUA_API void lua_error (lua_State *L, const char *s) {
597 lua_lock(L); 665 lua_lock(L);
598 luaD_error(L, s); 666 luaD_runerror(L, s);
599 lua_unlock(L); 667 lua_unlock(L);
600} 668}
601 669
@@ -662,7 +730,7 @@ LUA_API void lua_concat (lua_State *L, int n) {
662 L->top -= (n-1); 730 L->top -= (n-1);
663 luaC_checkGC(L); 731 luaC_checkGC(L);
664 } 732 }
665 else if (n == 0) { /* push null string */ 733 else if (n == 0) { /* push empty string */
666 setsvalue(L->top, luaS_newlstr(L, NULL, 0)); 734 setsvalue(L->top, luaS_newlstr(L, NULL, 0));
667 api_incr_top(L); 735 api_incr_top(L);
668 } 736 }