From 4fdde97639d977ab1b99e8923306ae0517a86d32 Mon Sep 17 00:00:00 2001 From: david Date: Thu, 5 Sep 2013 22:10:15 +0000 Subject: [PATCH] Factor out lua_call_traceback. This does a lua_pcall with the standard traceback error handler. --- ncat/ncat_lua.c | 18 +++++++++++++----- ncat/ncat_lua.h | 1 + 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/ncat/ncat_lua.c b/ncat/ncat_lua.c index d7816c08a..71dd73549 100644 --- a/ncat/ncat_lua.c +++ b/ncat/ncat_lua.c @@ -165,11 +165,7 @@ lua_State *lua_setup(const char *filename) void lua_run(lua_State *L) { - /* The chunk as read from lua_setup is on top of the stack. Put the - traceback function before it and run it. */ - lua_pushcfunction(L, traceback); - lua_insert(L, -2); - if (lua_pcall(L, 0, 0, -2) != LUA_OK && !lua_isnil(L, -1)) { + if (lua_call_traceback(L, 0, 0) != LUA_OK && !lua_isnil(L, -1)) { report(L, "Error running the Lua script"); } else { if (o.debug) @@ -178,3 +174,15 @@ void lua_run(lua_State *L) exit(EXIT_SUCCESS); } } + + +/* Returns the value of a lua_pcall of the chunk on top of the stack, with an + error handler that prints a traceback. */ +int lua_call_traceback(lua_State *L, int nargs, int nresults) +{ + /* The chunk to run is on top of the stack. Put the traceback function + before it and run it. */ + lua_pushcfunction(L, traceback); + lua_insert(L, -2); + return lua_pcall(L, nargs, nresults, -2); +} diff --git a/ncat/ncat_lua.h b/ncat/ncat_lua.h index 0cc038976..c74680de5 100644 --- a/ncat/ncat_lua.h +++ b/ncat/ncat_lua.h @@ -140,5 +140,6 @@ extern "C" { lua_State *lua_setup(const char *filename); void lua_run(lua_State *L); +int lua_call_traceback(lua_State *L, int nargs, int nresults); #endif