fix nexus_lua_eval error handling
authorTero Marttila <terom@fixme.fi>
Sun, 19 Apr 2009 23:53:37 +0300
changeset 144 51f96539f9f3
parent 143 1edab39c88a8
child 145 a5582e1a83da
fix nexus_lua_eval error handling
src/nexus_lua.c
--- a/src/nexus_lua.c	Sun Apr 19 06:20:59 2009 +0300
+++ b/src/nexus_lua.c	Sun Apr 19 23:53:37 2009 +0300
@@ -72,33 +72,35 @@
 err_t nexus_lua_eval (struct nexus_lua *lua, const char *chunk, struct error_info *err)
 {
     int ret;
+    bool loaded = false;
+
+    RESET_ERROR(err);
 
     // load the line as a lua function
     if ((ret = luaL_loadstring(lua->st, chunk)))
         goto error;
+    
+    loaded = true;
 
     // execute it
-    if ((ret = lua_pcall(lua->st, 0, 0, 0))) {
-        // pop off the bad chunk
+    if ((ret = lua_pcall(lua->st, 0, 0, 0)))
+        goto error;
+
+error:
+    if (ret) {
+        // build the error_info
+        nexus_lua_error(lua->st, ret, err);
+
+        // pop it
+        // XXX: err points at GC:able memory
         lua_pop(lua->st, 1);
-
-        goto error;
     }
 
-    // ok, pop off the chunk
-    lua_pop(lua->st, 1);
-    
-    // done
-    return SUCCESS;
+    if (loaded) {
+        // pop off the chunk
+        lua_pop(lua->st, 1);
+    }
 
-error:
-    // build the error_info
-    nexus_lua_error(lua->st, ret, err);
-
-    // pop it
-    lua_pop(lua->st, -1);
-
-    // XXX: err points at GC:able memory
     return ERROR_CODE(err);
 }