aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lapi.c58
-rw-r--r--ldo.c31
-rw-r--r--ldo.h5
-rw-r--r--lua.c49
-rw-r--r--lua.h3
5 files changed, 105 insertions, 41 deletions
diff --git a/lapi.c b/lapi.c
index 7577ce9f..7e663f05 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.223 2002/11/25 11:16:48 roberto Exp roberto $ 2** $Id: lapi.c,v 1.224 2002/11/25 17:50:14 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*/
@@ -649,12 +649,66 @@ LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
649} 649}
650 650
651 651
652
653/*
654** Execute a protected call.
655*/
656struct CallS { /* data to `f_call' */
657 StkId func;
658 int nresults;
659};
660
661
662static void f_call (lua_State *L, void *ud) {
663 struct CallS *c = cast(struct CallS *, ud);
664 luaD_call(L, c->func, c->nresults);
665}
666
667
668
652LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) { 669LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
670 struct CallS c;
653 int status; 671 int status;
654 ptrdiff_t func; 672 ptrdiff_t func;
655 lua_lock(L); 673 lua_lock(L);
656 func = (errfunc == 0) ? 0 : savestack(L, luaA_index(L, errfunc)); 674 func = (errfunc == 0) ? 0 : savestack(L, luaA_index(L, errfunc));
657 status = luaD_pcall(L, nargs, nresults, func); 675 c.func = L->top - (nargs+1); /* function to be called */
676 c.nresults = nresults;
677 status = luaD_pcall(L, &f_call, &c, savestack(L, c.func), func);
678 lua_unlock(L);
679 return status;
680}
681
682
683/*
684** Execute a protected C call.
685*/
686struct CCallS { /* data to `f_Ccall' */
687 lua_CFunction func;
688 void *ud;
689};
690
691
692static void f_Ccall (lua_State *L, void *ud) {
693 struct CCallS *c = cast(struct CCallS *, ud);
694 Closure *cl;
695 cl = luaF_newCclosure(L, 0);
696 cl->c.f = c->func;
697 setclvalue(L->top, cl); /* push function */
698 incr_top(L);
699 setpvalue(L->top, c->ud); /* push only argument */
700 incr_top(L);
701 luaD_call(L, L->top - 2, 0);
702}
703
704
705LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
706 struct CCallS c;
707 int status;
708 lua_lock(L);
709 c.func = func;
710 c.ud = ud;
711 status = luaD_pcall(L, &f_Ccall, &c, savestack(L, L->top), 0);
658 lua_unlock(L); 712 lua_unlock(L);
659 return status; 713 return status;
660} 714}
diff --git a/ldo.c b/ldo.c
index cf2948c6..15afc6f1 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 1.208 2002/11/22 17:16:52 roberto Exp roberto $ 2** $Id: ldo.c,v 1.209 2002/11/22 18:01:46 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -378,35 +378,17 @@ LUA_API int lua_yield (lua_State *L, int nresults) {
378} 378}
379 379
380 380
381/* 381int luaD_pcall (lua_State *L, Pfunc func, void *u,
382** Execute a protected call. 382 ptrdiff_t old_top, ptrdiff_t ef) {
383*/
384struct CallS { /* data to `f_call' */
385 StkId func;
386 int nresults;
387};
388
389
390static void f_call (lua_State *L, void *ud) {
391 struct CallS *c = cast(struct CallS *, ud);
392 luaD_call(L, c->func, c->nresults);
393}
394
395
396int luaD_pcall (lua_State *L, int nargs, int nresults, ptrdiff_t errfunc) {
397 struct CallS c;
398 int status; 383 int status;
399 unsigned short oldnCcalls = L->nCcalls; 384 unsigned short oldnCcalls = L->nCcalls;
400 ptrdiff_t old_top = savestack(L, L->top);
401 ptrdiff_t old_ci = saveci(L, L->ci); 385 ptrdiff_t old_ci = saveci(L, L->ci);
402 lu_byte old_allowhooks = L->allowhook; 386 lu_byte old_allowhooks = L->allowhook;
403 ptrdiff_t old_errfunc = L->errfunc; 387 ptrdiff_t old_errfunc = L->errfunc;
404 L->errfunc = errfunc; 388 L->errfunc = ef;
405 c.func = L->top - (nargs+1); /* function to be called */ 389 status = luaD_rawrunprotected(L, func, u);
406 c.nresults = nresults;
407 status = luaD_rawrunprotected(L, &f_call, &c);
408 if (status != 0) { /* an error occurred? */ 390 if (status != 0) { /* an error occurred? */
409 StkId oldtop = restorestack(L, old_top) - (nargs+1); 391 StkId oldtop = restorestack(L, old_top);
410 luaF_close(L, oldtop); /* close eventual pending closures */ 392 luaF_close(L, oldtop); /* close eventual pending closures */
411 seterrorobj(L, status, oldtop); 393 seterrorobj(L, status, oldtop);
412 L->nCcalls = oldnCcalls; 394 L->nCcalls = oldnCcalls;
@@ -420,6 +402,7 @@ int luaD_pcall (lua_State *L, int nargs, int nresults, ptrdiff_t errfunc) {
420} 402}
421 403
422 404
405
423/* 406/*
424** Execute a protected parser. 407** Execute a protected parser.
425*/ 408*/
diff --git a/ldo.h b/ldo.h
index aa568869..929ee8ed 100644
--- a/ldo.h
+++ b/ldo.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.h,v 1.54 2002/11/21 17:19:11 roberto Exp roberto $ 2** $Id: ldo.h,v 1.55 2002/11/21 17:41:25 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -46,7 +46,8 @@ int luaD_protectedparser (lua_State *L, ZIO *z, int bin);
46void luaD_callhook (lua_State *L, int event, int line); 46void luaD_callhook (lua_State *L, int event, int line);
47StkId luaD_precall (lua_State *L, StkId func); 47StkId luaD_precall (lua_State *L, StkId func);
48void luaD_call (lua_State *L, StkId func, int nResults); 48void luaD_call (lua_State *L, StkId func, int nResults);
49int luaD_pcall (lua_State *L, int nargs, int nresults, ptrdiff_t errfunc); 49int luaD_pcall (lua_State *L, Pfunc func, void *u,
50 ptrdiff_t oldtop, ptrdiff_t ef);
50void luaD_poscall (lua_State *L, int wanted, StkId firstResult); 51void luaD_poscall (lua_State *L, int wanted, StkId firstResult);
51void luaD_reallocCI (lua_State *L, int newsize); 52void luaD_reallocCI (lua_State *L, int newsize);
52void luaD_reallocstack (lua_State *L, int newsize); 53void luaD_reallocstack (lua_State *L, int newsize);
diff --git a/lua.c b/lua.c
index 3dc0069d..a257417b 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.110 2002/11/25 17:47:13 roberto Exp roberto $ 2** $Id: lua.c,v 1.111 2002/12/04 15:38:25 roberto Exp roberto $
3** Lua stand-alone interpreter 3** Lua stand-alone interpreter
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -388,19 +388,44 @@ static int handle_luainit (void) {
388} 388}
389 389
390 390
391int main (int argc, char *argv[]) { 391struct Smain {
392 int argc;
393 char **argv;
394 int status;
395};
396
397
398static int pmain (lua_State *l) {
399 struct Smain *s = (struct Smain *)lua_touserdata(l, 1);
392 int status; 400 int status;
393 int interactive = 0; 401 int interactive = 0;
394 (void)argc; /* to avoid warnings */ 402 progname = s->argv[0];
395 progname = argv[0]; 403 L = l;
396 L = lua_open(); /* create state */ 404 lua_userinit(l); /* open libraries */
397 lua_atpanic(L, l_panic);
398 lua_userinit(L); /* open libraries */
399 status = handle_luainit(); 405 status = handle_luainit();
400 if (status != 0) return status; 406 if (status == 0) {
401 status = handle_argv(argv, &interactive); 407 status = handle_argv(s->argv, &interactive);
402 if (status == 0 && interactive) manual_input(); 408 if (status == 0 && interactive) manual_input();
403 lua_close(L); 409 }
404 return status; 410 s->status = status;
411 return 0;
412}
413
414
415int main (int argc, char *argv[]) {
416 int status;
417 struct Smain s;
418 lua_State *l = lua_open(); /* create state */
419 if (l == NULL) {
420 l_message(argv[0], "cannot create state: not enough memory");
421 return EXIT_FAILURE;
422 }
423 s.argc = argc;
424 s.argv = argv;
425 lua_atpanic(l, l_panic);
426 status = lua_cpcall(l, &pmain, &s);
427 report(status);
428 lua_close(l);
429 return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
405} 430}
406 431
diff --git a/lua.h b/lua.h
index ea61f0c3..d8fb9047 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.167 2002/11/25 17:50:14 roberto Exp roberto $ 2** $Id: lua.h,v 1.168 2002/11/26 12:53:29 roberto Exp roberto $
3** Lua - An Extensible Extension Language 3** Lua - An Extensible Extension Language
4** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil 4** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
5** http://www.lua.org mailto:info@lua.org 5** http://www.lua.org mailto:info@lua.org
@@ -186,6 +186,7 @@ LUA_API int lua_setglobals (lua_State *L, int idx);
186*/ 186*/
187LUA_API void lua_call (lua_State *L, int nargs, int nresults); 187LUA_API void lua_call (lua_State *L, int nargs, int nresults);
188LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc); 188LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc);
189LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud);
189LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *dt, 190LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *dt,
190 const char *chunkname); 191 const char *chunkname);
191 192