From: Robin Gareus Date: Sat, 13 Feb 2016 21:31:17 +0000 (+0100) Subject: add liblua wrapper and build-scripts X-Git-Tag: 5.0-pre0~1487 X-Git-Url: https://main.carlh.net/gitweb/?p=ardour.git;a=commitdiff_plain;h=5b40e073e9c973479c3d286a007c57e1e0fa3d0f add liblua wrapper and build-scripts --- diff --git a/libs/lua/lua.cc b/libs/lua/lua.cc new file mode 100644 index 0000000000..cc6593a13d --- /dev/null +++ b/libs/lua/lua.cc @@ -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 index 0000000000..ad25233691 --- /dev/null +++ b/libs/lua/lua/lua.h @@ -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 index 0000000000..60fa0052dd --- /dev/null +++ b/libs/lua/lua/luastate.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2016 Robin Gareus + * + * 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 +#include + +#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 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 index 0000000000..546b02a006 --- /dev/null +++ b/libs/lua/luastate.cc @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2016 Robin Gareus + * + * 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 +#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 (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 index 0000000000..6598e5a230 --- /dev/null +++ b/libs/lua/wscript @@ -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 c0c70377c2..69ea9b3220 100644 --- 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',