lua-bindings: added open_lualib() for on-demand loading of Lua libs.
authorKrisztian Litkey <krisztian.litkey@intel.com>
Mon, 7 Jan 2013 16:09:29 +0000 (18:09 +0200)
committerKrisztian Litkey <krisztian.litkey@intel.com>
Mon, 7 Jan 2013 16:09:29 +0000 (18:09 +0200)
Use open_lualib to load standard Lua libraries as needed. You can call
it with an argument list consisting of the names of the libraries you
want to load (IOW to luaopen_*()). Eg.
  murphy.get():open_lualib('math', 'string', 'io')
will try to luaopen_math(), luaopen_string(), and luaopen_io().
The available libraries are 'math', 'string', 'io', 'os', 'table',
'debug', 'package', and 'base'.

src/Makefile.am
src/core/lua-bindings/lua-lua.c [new file with mode: 0644]

index 37f5530..462382f 100644 (file)
@@ -221,6 +221,7 @@ pkgconfig_DATA  += core/murphy-core.pc
 
 LUA_BINDINGS_SOURCES =                                 \
                core/lua-bindings/lua-murphy.c  \
+               core/lua-bindings/lua-lua.c     \
                core/lua-bindings/lua-plugin.c  \
                core/lua-bindings/lua-log.c     \
                core/lua-bindings/lua-console.c
diff --git a/src/core/lua-bindings/lua-lua.c b/src/core/lua-bindings/lua-lua.c
new file mode 100644 (file)
index 0000000..63a7c32
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *   * Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimer.
+ *   * Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *   * Neither the name of Intel Corporation nor the names of its contributors
+ *     may be used to endorse or promote products derived from this software
+ *     without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <lualib.h>
+#include <lauxlib.h>
+
+#include <murphy/common/mm.h>
+#include <murphy/common/log.h>
+#include <murphy/core/plugin.h>
+#include <murphy/core/lua-bindings/murphy.h>
+
+
+static int open_lualib(lua_State *L)
+{
+    struct {
+        const char  *name;
+        int        (*loader)(lua_State *L);
+    } *lib, libs[] = {
+        { "math"   , luaopen_math    },
+        { "string" , luaopen_string  },
+        { "io"     , luaopen_io      },
+        { "os"     , luaopen_os      },
+        { "table"  , luaopen_table   },
+        { "debug"  , luaopen_debug   },
+        { "package", luaopen_package },
+        { "base"   , luaopen_base    },
+        { NULL     , NULL }
+    };
+    const char *name;
+    int         i, n;
+
+    n = lua_gettop(L);
+
+    if (lua_isuserdata(L, 1)) {
+        lua_remove(L, 1);                /* remove self if any */
+        n--;
+    }
+
+    if (n < 1)
+        return luaL_error(L, "%s called without any arguments", __FUNCTION__);
+
+    for (i = 1; i <= n; i++) {
+        luaL_checktype(L, 1, LUA_TSTRING);
+
+        name = lua_tostring(L, i);
+
+        for (lib = libs; lib->name != NULL; lib++)
+            if (!strcmp(lib->name, name))
+                break;
+
+        if (lib->loader != NULL) {
+            mrp_debug("loading Lua lib '%s' with %p...", name, lib->loader);
+            lib->loader(L);
+        }
+        else
+            return luaL_error(L, "failed to load unknown Lua lib '%s'", name);
+    }
+
+    lua_settop(L, 0);
+    return 0;
+}
+
+
+MURPHY_REGISTER_LUA_BINDINGS(murphy, { "open_lualib", open_lualib });