From 0200c27e335d3b838dac70e28b7b4853986277a6 Mon Sep 17 00:00:00 2001 From: Christian Helmuth Date: Sun, 6 May 2012 21:56:37 +0200 Subject: [PATCH] Lua: Port of Lua runtime and simple example The Lua runtime library is built in two variants: ANSI C and C++. The C++ provides all Lua API function with C++ linkage and uses C++ exceptions instead of setjmp/longjmp for protected execution of Lua chunks. The ported version of Lua is 5.1.5. --- .gitignore | 1 + libports/lib/import/import-lua.mk | 1 + libports/lib/import/import-luacxx.mk | 1 + libports/lib/mk/lua.inc | 18 ++++++ libports/lib/mk/lua.mk | 7 +++ libports/lib/mk/luacxx.mk | 11 ++++ libports/ports/lua.mk | 34 +++++++++++ libports/run/moon.run | 33 ++++++++++ libports/src/test/libports/lua/target.mk | 5 ++ libports/src/test/libports/luacxx/target.mk | 5 ++ libports/src/test/moon/main.cc | 67 +++++++++++++++++++++ libports/src/test/moon/target.mk | 3 + tool/autopilot.list | 1 + 13 files changed, 187 insertions(+) create mode 100644 libports/lib/import/import-lua.mk create mode 100644 libports/lib/import/import-luacxx.mk create mode 100644 libports/lib/mk/lua.inc create mode 100644 libports/lib/mk/lua.mk create mode 100644 libports/lib/mk/luacxx.mk create mode 100644 libports/ports/lua.mk create mode 100644 libports/run/moon.run create mode 100644 libports/src/test/libports/lua/target.mk create mode 100644 libports/src/test/libports/luacxx/target.mk create mode 100644 libports/src/test/moon/main.cc create mode 100644 libports/src/test/moon/target.mk diff --git a/.gitignore b/.gitignore index 5965122e42..399c80651a 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ /libports/include/libc-i386/ /libports/include/libc/ /libports/include/libpng +/libports/include/lua /libports/include/lwip/lwip /libports/include/lwip/netif /libports/include/mupdf diff --git a/libports/lib/import/import-lua.mk b/libports/lib/import/import-lua.mk new file mode 100644 index 0000000000..d7d6418477 --- /dev/null +++ b/libports/lib/import/import-lua.mk @@ -0,0 +1 @@ +REP_INC_DIR += include/lua diff --git a/libports/lib/import/import-luacxx.mk b/libports/lib/import/import-luacxx.mk new file mode 100644 index 0000000000..d7d6418477 --- /dev/null +++ b/libports/lib/import/import-luacxx.mk @@ -0,0 +1 @@ +REP_INC_DIR += include/lua diff --git a/libports/lib/mk/lua.inc b/libports/lib/mk/lua.inc new file mode 100644 index 0000000000..1da151495e --- /dev/null +++ b/libports/lib/mk/lua.inc @@ -0,0 +1,18 @@ +LUA = lua-5.1.5 +LUA_DIR = $(REP_DIR)/contrib/$(LUA)/src +LIBS += libc libm +INC_DIR += $(LUA_DIR) +CC_DEF = -DLUA_ANSI -DLUA_USE_MKSTEMP + +# core objects +LUA_CORE_C = lapi.c lcode.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c \ + lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c ltm.c \ + lundump.c lvm.c lzio.c + +# library objects, e.g. string handling +LUA_LIB_C = lauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c loslib.c ltablib.c \ + lstrlib.c loadlib.c linit.c + +vpath %.c $(LUA_DIR) + +# vi: set ft=make : diff --git a/libports/lib/mk/lua.mk b/libports/lib/mk/lua.mk new file mode 100644 index 0000000000..2c276a542a --- /dev/null +++ b/libports/lib/mk/lua.mk @@ -0,0 +1,7 @@ +# +# Lua library (ANSI C variant) +# + +include $(REP_DIR)/lib/mk/lua.inc + +SRC_C = $(LUA_CORE_C) $(LUA_LIB_C) diff --git a/libports/lib/mk/luacxx.mk b/libports/lib/mk/luacxx.mk new file mode 100644 index 0000000000..cdc5902f25 --- /dev/null +++ b/libports/lib/mk/luacxx.mk @@ -0,0 +1,11 @@ +# +# Lua library (C++ variant) +# + +include $(REP_DIR)/lib/mk/lua.inc + +SRC_C = $(LUA_CORE_C) $(LUA_LIB_C) + +# force compilation with C++ compiler +CUSTOM_CC = $(CXX) +CC_WARN += -Wno-sign-compare diff --git a/libports/ports/lua.mk b/libports/ports/lua.mk new file mode 100644 index 0000000000..83fb544061 --- /dev/null +++ b/libports/ports/lua.mk @@ -0,0 +1,34 @@ +LUA = lua-5.1.5 +LUA_TGZ = $(LUA).tar.gz +LUA_URL = http://www.lua.org/ftp/$(LUA_TGZ) + +# +# Interface to top-level prepare Makefile +# +PORTS += $(LUA) + +LUA_INC_DIR = include/lua + +prepare-lua: $(CONTRIB_DIR)/$(LUA) $(LUA_INC_DIR) + +$(CONTRIB_DIR)/$(LUA): clean-lua + +# +# Port-specific local rules +# +$(DOWNLOAD_DIR)/$(LUA_TGZ): + $(VERBOSE)wget -c -P $(DOWNLOAD_DIR) $(LUA_URL) && touch $@ + +$(CONTRIB_DIR)/$(LUA): $(DOWNLOAD_DIR)/$(LUA_TGZ) + $(VERBOSE)tar xfz $< -C $(CONTRIB_DIR) && touch $@ + +LUA_INCLUDES = lua.h lauxlib.h luaconf.h lualib.h + +$(LUA_INC_DIR): + $(VERBOSE)mkdir -p $@ + $(VERBOSE)for i in $(LUA_INCLUDES); do \ + ln -sf ../../$(CONTRIB_DIR)/$(LUA)/src/$$i $@; done + +clean-lua: + $(VERBOSE)rm -rf $(LUA_INC_DIR) + $(VERBOSE)rm -rf $(CONTRIB_DIR)/$(LUA) diff --git a/libports/run/moon.run b/libports/run/moon.run new file mode 100644 index 0000000000..18bbfc3492 --- /dev/null +++ b/libports/run/moon.run @@ -0,0 +1,33 @@ +# +# Lua C++ library test +# + +build "core init test/moon" + +create_boot_directory + +install_config { + + + + + + + + + + + + + +} + +build_boot_image { + core init test-moon + ld.lib.so libc.lib.so libc_log.lib.so libm.lib.so +} + +append qemu_args " -nographic -m 64 " + +run_genode_until forever + diff --git a/libports/src/test/libports/lua/target.mk b/libports/src/test/libports/lua/target.mk new file mode 100644 index 0000000000..b913c31027 --- /dev/null +++ b/libports/src/test/libports/lua/target.mk @@ -0,0 +1,5 @@ +TARGET = test-lua +LIBS = cxx env lua libc libm +SRC_CC = main.cc + +vpath main.cc $(PRG_DIR)/.. diff --git a/libports/src/test/libports/luacxx/target.mk b/libports/src/test/libports/luacxx/target.mk new file mode 100644 index 0000000000..2abaa04564 --- /dev/null +++ b/libports/src/test/libports/luacxx/target.mk @@ -0,0 +1,5 @@ +TARGET = test-luacxx +LIBS = cxx env luacxx libc libm +SRC_CC = main.cc + +vpath main.cc $(PRG_DIR)/.. diff --git a/libports/src/test/moon/main.cc b/libports/src/test/moon/main.cc new file mode 100644 index 0000000000..eed6fde0ab --- /dev/null +++ b/libports/src/test/moon/main.cc @@ -0,0 +1,67 @@ +/* + * \brief Lua C++ library test + * \author Christian Helmuth + * \date 2012-05-06 + */ + +/* + * Copyright (C) 2012 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU General Public License version 2. + */ + +/* Genode includes */ +#include + +/* Lua includes */ +#include +#include +#include + + +static int log(lua_State *L) +{ + int n = lua_gettop(L); + + for (int i = 1; i <= n; ++i) { + if (lua_isstring(L, i)) + PLOG("%s", lua_tostring(L, i)); + else if (lua_isnil(L, i)) + PLOG("%s", "nil"); + else if (lua_isboolean(L, i)) + PLOG("%s", lua_toboolean(L, i) ? "true" : "false"); + else + PLOG("%s: %p", luaL_typename(L, i), lua_topointer(L, i)); + } + return 0; +} + + +static char const *exec_string = + "i = 10000000000000000 + 1\n" + "log(\"your result is: \"..i)\n" + "a = { }\n" + "log(a)\n" + "log(type(a))\n" + "a.foo = \"foo\"\n" + "a.bar = \"bar\"\n" + "log(a.foo .. \" \" .. a.bar)\n" + ; + + +int main() +{ + lua_State *L = lua_open(); + + /* initialize libs */ + luaopen_base(L); + + /* register simple log function */ + lua_register(L, "log", log); + + if (luaL_dostring(L, exec_string) != 0) + PLOG("%s\n", lua_tostring(L, -1)); + + lua_close(L); +} diff --git a/libports/src/test/moon/target.mk b/libports/src/test/moon/target.mk new file mode 100644 index 0000000000..867e43e66d --- /dev/null +++ b/libports/src/test/moon/target.mk @@ -0,0 +1,3 @@ +TARGET = test-moon +LIBS = cxx env luacxx libc libc_log libm +SRC_CC = main.cc diff --git a/tool/autopilot.list b/tool/autopilot.list index 806b889fae..f3653a93a4 100644 --- a/tool/autopilot.list +++ b/tool/autopilot.list @@ -13,3 +13,4 @@ l4linux lx_hybrid_ctors lx_hybrid_exception lx_hybrid_pthread_ipc +moon