implement nexus_lua_eval
authorTero Marttila <terom@fixme.fi>
Sun, 12 Apr 2009 20:37:57 +0300
changeset 136 81dbeb5bc38e
parent 135 9159bd51525f
child 137 c607c357c486
implement nexus_lua_eval
src/lua_console.c
src/nexus_lua.c
src/nexus_lua.h
--- a/src/lua_console.c	Sun Apr 12 18:56:51 2009 +0300
+++ b/src/lua_console.c	Sun Apr 12 20:37:57 2009 +0300
@@ -9,38 +9,18 @@
 static void lua_console_on_line (const char *line, void *arg)
 {
     struct lua_console *lc = arg;
-    lua_State *L = lc->lua->st;
-    int ret;
+    struct error_info err;
 
     // ignore empty lines and EOF
     if (!line || !(*line))
         return;
     
-    // XXX: move to nexus_lua
-
-    // load the line as a lua function
-    if ((ret = luaL_loadstring(L, line)))
-        goto error;
-
-    // execute it
-    if ((ret = lua_pcall(L, 0, 0, 0)))
-        goto error;
-
-    // XXX: display results?
-    
-error:
-    if (ret) {
-        struct error_info err;
-        
-        // build the error_info
-        nexus_lua_error(L, ret, &err);
-
-        // log it
+    // eval it
+    if (nexus_lua_eval(lc->lua, line, &err))
         log_error("%s", error_msg(&err));
 
-        // pop it
-        lua_pop(L, -1);
-    }
+    // XXX: display return value?
+     
 }
 
 static struct console_callbacks _console_callbacks = {
--- a/src/nexus_lua.c	Sun Apr 12 18:56:51 2009 +0300
+++ b/src/nexus_lua.c	Sun Apr 12 20:37:57 2009 +0300
@@ -69,6 +69,39 @@
     free(lua);
 }
 
+err_t nexus_lua_eval (struct nexus_lua *lua, const char *chunk, struct error_info *err)
+{
+    int ret;
+
+    // load the line as a lua function
+    if ((ret = luaL_loadstring(lua->st, chunk)))
+        goto error;
+
+    // execute it
+    if ((ret = lua_pcall(lua->st, 0, 0, 0))) {
+        // pop off the bad chunk
+        lua_pop(lua->st, 1);
+
+        goto error;
+    }
+
+    // ok, pop off the chunk
+    lua_pop(lua->st, 1);
+    
+    // done
+    return SUCCESS;
+
+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);
+}
+
 err_t nexus_lua_error (lua_State *L, int ret, struct error_info *err)
 {
     const char *error = lua_tostring(L, -1);
@@ -83,3 +116,4 @@
         default:                RETURN_SET_ERROR_EXTRA(err, ERR_UNKNOWN, ret);
     };
 }
+
--- a/src/nexus_lua.h	Sun Apr 12 18:56:51 2009 +0300
+++ b/src/nexus_lua.h	Sun Apr 12 20:37:57 2009 +0300
@@ -34,11 +34,19 @@
 void nexus_lua_destroy (struct nexus_lua *lua);
 
 /**
+ * Parse and execute the given lua chunk in the nexus's lua context.
+ *
+ * This operation is equally valid for both textual and binary chunks, but this is intended for textual chunks, and
+ * hence accepts a NUL-terminated string.
+ */
+err_t nexus_lua_eval (struct nexus_lua *lua, const char *chunk, struct error_info *err);
+
+/**
  * Handle a Lua error by converting the given error code into a ERR_LUA_* code, inspecting the error object at
  * the top of the stack.
  *
  * Please note that the resulting error_info points into strings inside the lua stack - once you pop the error, the
- * error_info might not be valid anymore.
+ * error_info might not be valid anymore after the next GC cycle.
  */
 err_t nexus_lua_error (lua_State *L, int ret, struct error_info *err);