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.
This commit is contained in:
Christian Helmuth 2012-05-06 21:56:37 +02:00
parent bd3c53be31
commit 0200c27e33
13 changed files with 187 additions and 0 deletions

1
.gitignore vendored
View File

@ -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

View File

@ -0,0 +1 @@
REP_INC_DIR += include/lua

View File

@ -0,0 +1 @@
REP_INC_DIR += include/lua

18
libports/lib/mk/lua.inc Normal file
View File

@ -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 :

7
libports/lib/mk/lua.mk Normal file
View File

@ -0,0 +1,7 @@
#
# Lua library (ANSI C variant)
#
include $(REP_DIR)/lib/mk/lua.inc
SRC_C = $(LUA_CORE_C) $(LUA_LIB_C)

11
libports/lib/mk/luacxx.mk Normal file
View File

@ -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

34
libports/ports/lua.mk Normal file
View File

@ -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)

33
libports/run/moon.run Normal file
View File

@ -0,0 +1,33 @@
#
# Lua C++ library test
#
build "core init test/moon"
create_boot_directory
install_config {
<config>
<parent-provides>
<service name="ROM"/>
<service name="RM"/>
<service name="LOG"/>
</parent-provides>
<default-route>
<any-service> <parent/> <any-child/> </any-service>
</default-route>
<start name="test-moon">
<resource name="RAM" quantum="1100K"/>
</start>
</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

View File

@ -0,0 +1,5 @@
TARGET = test-lua
LIBS = cxx env lua libc libm
SRC_CC = main.cc
vpath main.cc $(PRG_DIR)/..

View File

@ -0,0 +1,5 @@
TARGET = test-luacxx
LIBS = cxx env luacxx libc libm
SRC_CC = main.cc
vpath main.cc $(PRG_DIR)/..

View File

@ -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 <base/printf.h>
/* Lua includes */
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
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);
}

View File

@ -0,0 +1,3 @@
TARGET = test-moon
LIBS = cxx env luacxx libc libc_log libm
SRC_CC = main.cc

View File

@ -13,3 +13,4 @@ l4linux
lx_hybrid_ctors
lx_hybrid_exception
lx_hybrid_pthread_ipc
moon