add liblua wrapper and build-scripts
authorRobin Gareus <robin@gareus.org>
Sat, 13 Feb 2016 21:31:17 +0000 (22:31 +0100)
committerRobin Gareus <robin@gareus.org>
Mon, 22 Feb 2016 21:06:47 +0000 (22:06 +0100)
libs/lua/lua.cc [new file with mode: 0644]
libs/lua/lua/lua.h [new file with mode: 0644]
libs/lua/lua/luastate.h [new file with mode: 0644]
libs/lua/luastate.cc [new file with mode: 0644]
libs/lua/wscript [new file with mode: 0644]
wscript

diff --git a/libs/lua/lua.cc b/libs/lua/lua.cc
new file mode 100644 (file)
index 0000000..cc6593a
--- /dev/null
@@ -0,0 +1,96 @@
+#if _MSC_VER
+#pragma push_macro("_CRT_SECURE_NO_WARNINGS")
+#ifndef _CRT_SECURE_NO_WARNINGS
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#endif
+
+#ifdef PLATFORM_WINDOWS
+# define LUA_USE_WINDOWS
+#elif defined __APPLE__
+# define LUA_USE_MACOSX
+#else
+# define LUA_USE_LINUX
+#endif
+
+extern "C"
+{
+
+#define lobject_c
+#define lvm_c
+#define LUA_CORE
+#define LUA_LIB
+#include "lua-5.3.2/luaconf.h"
+#undef lobject_c
+#undef lvm_c
+#undef LUA_CORE
+#undef LUA_LIB
+
+// override luaconf.h symbol export
+#undef LUA_API
+#undef LUALIB_API
+#undef LUAMOD_API
+#define LUA_API                extern "C"
+#define LUALIB_API     LUA_API
+#define LUAMOD_API     LUALIB_API
+
+// disable support for extenal libs
+#undef LUA_DL_DLL
+#undef LUA_USE_DLOPEN
+
+// enable bit lib
+#define LUA_COMPAT_BITLIB
+
+#if _MSC_VER
+#pragma warning (push)
+#pragma warning (disable: 4244) /* Possible loss of data */
+#pragma warning (disable: 4702) /* Unreachable code */
+#endif
+
+#include "lua-5.3.2/ltable.c"
+
+#include "lua-5.3.2/lauxlib.c"
+#include "lua-5.3.2/lbaselib.c"
+
+#include "lua-5.3.2/lbitlib.c"
+#include "lua-5.3.2/lcorolib.c"
+#include "lua-5.3.2/ldblib.c"
+#include "lua-5.3.2/linit.c"
+#include "lua-5.3.2/liolib.c"
+#include "lua-5.3.2/lmathlib.c"
+#include "lua-5.3.2/loslib.c"
+#include "lua-5.3.2/lstrlib.c"
+#include "lua-5.3.2/ltablib.c"
+
+#include "lua-5.3.2/lapi.c"
+#include "lua-5.3.2/lcode.c"
+#include "lua-5.3.2/lctype.c"
+#include "lua-5.3.2/ldebug.c"
+#include "lua-5.3.2/ldo.c"
+#include "lua-5.3.2/ldump.c"
+#include "lua-5.3.2/lfunc.c"
+#include "lua-5.3.2/lgc.c"
+#include "lua-5.3.2/llex.c"
+#include "lua-5.3.2/lmem.c"
+#include "lua-5.3.2/lobject.c"
+#include "lua-5.3.2/lopcodes.c"
+#include "lua-5.3.2/lparser.c"
+#include "lua-5.3.2/lstate.c"
+#include "lua-5.3.2/lstring.c"
+#include "lua-5.3.2/ltm.c"
+#include "lua-5.3.2/lundump.c"
+#include "lua-5.3.2/lutf8lib.c"
+#include "lua-5.3.2/lvm.c"
+#include "lua-5.3.2/lzio.c"
+
+#include "lua-5.3.2/loadlib.c"
+
+#if _MSC_VER
+#pragma warning (pop)
+#endif
+
+}
+
+#if _MSC_VER
+#pragma pop_macro("_CRT_SECURE_NO_WARNINGS")
+#endif
diff --git a/libs/lua/lua/lua.h b/libs/lua/lua/lua.h
new file mode 100644 (file)
index 0000000..ad25233
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef LUA_LIBRARY_H
+#define LUA_LIBRARY_H
+
+#include "lua-5.3.2/lua.hpp"
+
+#endif
diff --git a/libs/lua/lua/luastate.h b/libs/lua/lua/luastate.h
new file mode 100644 (file)
index 0000000..60fa005
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#ifndef LUA_STATE_H
+#define LUA_STATE_H
+
+#include <string>
+#include <sigc++/sigc++.h>
+
+#include "lua/lua.h"
+
+class LuaState {
+public:
+       LuaState();
+       LuaState(lua_State *ls);
+       ~LuaState();
+
+       int do_command (std::string);
+       int do_file (std::string);
+       void collect_garbage ();
+
+       sigc::signal<void,std::string> Print;
+
+       lua_State* getState () { return L; }
+
+protected:
+       lua_State* L;
+
+private:
+       void init ();
+  static int _print (lua_State *L);
+       void print (std::string text);
+
+};
+
+#endif
diff --git a/libs/lua/luastate.cc b/libs/lua/luastate.cc
new file mode 100644 (file)
index 0000000..546b02a
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include <assert.h>
+#include "lua/luastate.h"
+
+// from lauxlib.c
+static int panic (lua_State *L) {
+       lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
+                       lua_tostring(L, -1));
+       return 0;  /* return to Lua to abort */
+}
+
+LuaState::LuaState()
+       : L (luaL_newstate ())
+{
+       assert (L);
+       init ();
+}
+
+LuaState::LuaState(lua_State *ls)
+       : L (ls)
+{
+       assert (L);
+       init ();
+}
+
+LuaState::~LuaState() {
+       lua_close (L);
+}
+
+void
+LuaState::init() {
+       lua_atpanic (L, &panic);
+       luaL_openlibs (L);
+       lua_pushlightuserdata (L, this);
+       lua_pushcclosure (L, &LuaState::_print, 1);
+       lua_setglobal (L, "print");
+}
+
+int
+LuaState::do_command (std::string cmd) {
+       int result = luaL_dostring (L, cmd.c_str());
+       if (result != 0) {
+               print ("Error: " + std::string (lua_tostring (L, -1)));
+       }
+       return result;
+}
+
+int
+LuaState::do_file (std::string fn) {
+       int result = luaL_dofile (L, fn.c_str());
+       if (result != 0) {
+               print ("Error: " + std::string (lua_tostring (L, -1)));
+       }
+       return result;
+}
+
+void
+LuaState::collect_garbage () {
+       lua_gc (L, LUA_GCCOLLECT, 0);
+}
+
+void
+LuaState::print (std::string text) {
+       Print (text); /* EMIT SIGNAL */
+}
+
+int
+LuaState::_print (lua_State *L) {
+       LuaState* const luaState = static_cast <LuaState*> (lua_touserdata (L, lua_upvalueindex (1)));
+       std::string text;
+       int n = lua_gettop(L);  /* number of arguments */
+       int i;
+       lua_getglobal(L, "tostring");
+       for (i=1; i<=n; i++) {
+               const char *s;
+               size_t l;
+               lua_pushvalue(L, -1);  /* function to be called */
+               lua_pushvalue(L, i);   /* value to print */
+               lua_call(L, 1, 1);
+               s = lua_tolstring(L, -1, &l);  /* get result */
+               if (s == NULL)
+                       return luaL_error(L, "'tostring' must return a string to 'print'");
+               if (i > 1) text += " ";
+               text += std::string (s, l);
+               lua_pop(L, 1);  /* pop result */
+       }
+       luaState->print (text);
+       return 0;
+}
diff --git a/libs/lua/wscript b/libs/lua/wscript
new file mode 100644 (file)
index 0000000..6598e5a
--- /dev/null
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+from waflib.extras import autowaf as autowaf
+from waflib import TaskGen
+import os
+import sys
+
+# Variables for 'waf dist'
+APPNAME = 'liblua'
+VERSION = "3.5.1"
+I18N_PACKAGE = 'liblua'
+
+# Mandatory variables
+top = '.'
+out = 'build'
+
+def options(opt):
+    autowaf.set_options(opt)
+
+def configure(conf):
+    conf.load('compiler_c')
+    autowaf.configure(conf)
+
+def build(bld):
+    obj=bld.stlib (source = ['lua.cc', 'luastate.cc'],
+               cflags = [ '-fPIC' ],
+               cxxflags = [ '-fPIC' ],
+               includes = ['.'],
+               export_includes = ['.'],
+               target = 'liblua',
+               uselib = [ 'SIGCPP', 'DL' ]
+               )
+    autowaf.ensure_visible_symbols (obj, True)
+
+def shutdown():
+    autowaf.shutdown()
diff --git a/wscript b/wscript
index c0c70377c2049c3cf55fe6ce5c233353ade8071f..69ea9b3220e37eb2673cf68d20c340df5ce3c858 100644 (file)
--- a/wscript
+++ b/wscript
@@ -203,6 +203,7 @@ children = [
         'libs/qm-dsp',
         'libs/vamp-plugins',
         'libs/libltc',
+        'libs/lua',
         'libs/ptformat',
         # core ardour libraries
         'libs/pbd',