From 19d5b1ce3423f158ea857a1fe54ed7a03523c63e Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Thu, 27 Mar 2014 18:12:44 +1000 Subject: Make test_c a real Lua module, but just a test one doing it the usual way, not a skang module, yet. --- ClientHamr/GuiLua/build.sh | 54 ++++++++++++++++++++++ ClientHamr/GuiLua/test.lua | 4 +- ClientHamr/GuiLua/test_c.c | 113 +++++++++++++++++++++++---------------------- 3 files changed, 114 insertions(+), 57 deletions(-) create mode 100755 ClientHamr/GuiLua/build.sh (limited to 'ClientHamr/GuiLua') diff --git a/ClientHamr/GuiLua/build.sh b/ClientHamr/GuiLua/build.sh new file mode 100755 index 0000000..387b193 --- /dev/null +++ b/ClientHamr/GuiLua/build.sh @@ -0,0 +1,54 @@ +#! /bin/bash + +export LOCALDIR=`pwd` + +# No need for a make file, or dependencies, the entire thing takes only a few seconds to build. + +# This assumes you have EFL installed in one of two standard places. +if [ -d "/opt/EFL" ] +then + export E17DIR="/opt/EFL" +else + export E17DIR="/usr" +fi + +CFLAGS="-g -Wall -Wunreachable-code -I include -I $LOCALDIR" +CFLAGS="$CFLAGS -I ../../libraries" +#CFLAGS="$CFLAGS -I ../../libraries/LuaJIT-2.0.2/src" +CFLAGS="$CFLAGS $(pkg-config --cflags luajit)" +#CFLAGS="$CFLAGS -I /usr/include/lua5.1" +CFLAGS="$CFLAGS -I $E17DIR/include/eo-1" +CFLAGS="$CFLAGS -I $E17DIR/include/eina-1" +CFLAGS="$CFLAGS -I $E17DIR/include/eina-1/eina" +CFLAGS="$CFLAGS -I $E17DIR/include/eet-1" +CFLAGS="$CFLAGS -I $E17DIR/include/embryo-1" +CFLAGS="$CFLAGS -I $E17DIR/include/edje-1" +CFLAGS="$CFLAGS -I $E17DIR/include/evas-1" +CFLAGS="$CFLAGS -I $E17DIR/include/ecore-1" +CFLAGS="$CFLAGS -I $E17DIR/include/efl-1" +CFLAGS="$CFLAGS -I $E17DIR/include/ecore-con-1" +CFLAGS="$CFLAGS -I $E17DIR/include/ecore-evas-1" +CFLAGS="$CFLAGS -I $E17DIR/include/ecore-file-1" +CFLAGS="$CFLAGS -I $E17DIR/include" +CFLAGS="$CFLAGS -DPACKAGE_DATA_DIR=\"$LOCALDIR\" $CFLAGOPTS" + +#LDFLAGS="-L ../../libraries/LuaJIT-2.0.2/src -L lib -L /usr/lib -L /lib -L $E17DIR/lib" +#libs="-leo -lecore -levas -ledje -lembryo -leet -leina -lluajit -lpthread -lm" +LDFLAGS="$(pkg-config --libs-only-L luajit) -L lib -L /usr/lib -L /lib -L $E17DIR/lib" +libs="-leo -lecore -levas -ledje -lembryo -leet -leina $(pkg-config --libs-only-L luajit) -lpthread -lm" +#LDFLAGS="-L /usr/lib/lua/5.1 -L lib -L /usr/lib -L /lib -L $E17DIR/lib" +#libs="-lecore -levas -ledje -lembryo -leet -leina -llua5.1 -lpthread -lm" +# These need to be added to libs if linking staticaly, though some parts of EFL don't like that. +#-lecore_evas \ +#-lecore_file \ +#-ldl \ +#-lfontconfig \ +#-lfreetype \ +#-lexpat \ +#-lrt \ +#-lz + +echo "clean" +rm -f test_c.so test_c.o +echo "C modules" +gcc $CFLAGS -fPIC -shared -o test_c.so test_c.c diff --git a/ClientHamr/GuiLua/test.lua b/ClientHamr/GuiLua/test.lua index 58f8d19..b1ab691 100644 --- a/ClientHamr/GuiLua/test.lua +++ b/ClientHamr/GuiLua/test.lua @@ -35,7 +35,7 @@ skang.thing('foo') -- We can use inline functions if we don't need the function internally. skang.thing('ffunc', 'Help Text', function (arg1, arg2) - print('Inside test.ffunc ' .. arg1 .. ', ' .. arg2) + print('Inside test.ffunc(' .. arg1 .. ', ' .. arg2 .. ')') end, 'number,string') print('Ending soon') @@ -47,12 +47,14 @@ end -- Test it. local skang = require 'skang' local test = require 'test' +local test_c = require 'test_c' print('MODULE ' .. test._NAME .. ', ' .. test.COPYRIGHT .. ' Version: ' .. test.VERSION .. '\n' .. test.VERSION_DESC .. '\n') print('foo = ' .. test.foo .. ' ->> ' .. skang.things.foo.help) print('End ' .. test.bar .. ' ' .. test.VERSION .. ' ' .. skang.things.ffunc.help .. ' ->> ' .. skang.things.f.action) test.ffunc('one', 2) +test_c.ffunc(0, 'zero') --skang.things.ffunc('seven', 'aight') print('') diff --git a/ClientHamr/GuiLua/test_c.c b/ClientHamr/GuiLua/test_c.c index 8c3796d..e7ee388 100644 --- a/ClientHamr/GuiLua/test_c.c +++ b/ClientHamr/GuiLua/test_c.c @@ -1,78 +1,79 @@ -/* Should be a Lua module, roughly the same as test.lua +/* Should be a Lua skang module, roughly the same as test.lua */ +#include +#include +//#include -/* NOTES - -From http://www.inf.puc-rio.br/~roberto/pil2/chapter15.pdf +static int ffunc (lua_State *L) +{ + double arg1 = luaL_checknumber(L, 1); + const char *arg2 = luaL_checkstring(L, 2); -"Well-behaved C libraries should export one function called -luaopen_modname, which is the function that require tries to call after -linking the library. In Section 26.2 we will discuss how to write C -libraries." + printf("Inside test_c.ffunc(%f, %s)\n", arg1, arg2); + return 0; +} -The "modname" bit is replaced by the name of the module. Though if the -module name includes a hyphen, the "require" function strips out the -hyphen and the bit before it. -Though it seems that chapter 26 is not in the same place? +static const struct luaL_reg test_c [] = +{ + {"ffunc", ffunc}, + {NULL, NULL} +}; -http://www.lua.org/pil/26.2.html doesn't say much really, and is for -Lua 5.0 +/* local test_c = require 'test_c' +Lua's require() function will strip any stuff from the front of the name +separated by a hypen, so 'GuiLua-test_c' -> 'test_c'. Then it will +search through a path, and eventually find this test_c.so (or test_c.dll +or whatever), then call luaopen_test_c(), which should return a table. -An example - +Normally luaL_register() creates a table of functions, that is the table +returned, but we want to do something different with skang. -// build@ gcc -shared -I/home/sdonovan/lua/include -o mylib.so mylib.c -// includes for your code -#include -#include +*/ +int luaopen_test_c(lua_State *L) +{ +// This is a moving target, old ways get deperecated, new ways get added, +// would have to check version before doing any of these. +// luaL_openlib(L, "test_c", test_c, 0); // Lua 5.0 way. +// luaL_register (L, "test_c", test_c); // Lua 5.1 way. +// luaL_newlib() or luaL_setfuncs() // Lua 5.2 way. + // Creates a global table "test_c", does the package.loaded[test_c] thing. + lua_newtable(L); + luaL_register (L, NULL, test_c); // Lua 5.1 way. + // Puts the funcions in a table on top of the stack. -// includes for Lua -#include -#include -#include +/* BUT REALLY ... -// defining functions callable from Lua -static int l_createtable (lua_State *L) { - int narr = luaL_optint(L,1,0); // initial array slots, default 0 - int nrec = luaL_optint(L,2,0); // intialof hash slots, default 0 - lua_createtable(L,narr,nrec); - return 1; -} +We are in fact NOT putting any functions into the returned table. -static int l_solve (lua_State *L) { - double a = lua_tonumber(L,1); // coeff of x*x - double b = lua_tonumber(L,2); // coef of x - double c = lua_tonumber(L,3); // constant - double abc = b*b - 4*a*c; - if (abc < 0.0) { - lua_pushnil(L); - lua_pushstring(L,"imaginary roots!"); - return 2; - } else { - abc = sqrt(abc); - a = 2*a; - lua_pushnumber(L,(-b + abc)/a); - lua_pushnumber(L,(+b - abc)/a); - return 2; - } -} +skang.moduleBegin() returns the table we need to send back to Lua. + it saves getfenv(2) as the old environment, which should in theory be L + and setfenv(_M, 2) to set the tbale to be it's own environment + it does the package.loaded[test_c] thing for us + it returns the table it created, so we should just leave that on the stack as our result -static const luaL_reg mylib[] = { - {"createtable",l_createtable}, - {"solve",l_solve}, - {NULL,NULL} -}; +skang.thing() also uses getfenv(2) to grab the module's table -int luaopen_mylib(lua_State *L) -{ - luaL_register (L, "mylib", mylib); - return 1; -} +*/ +/* TODO - load skang, create things, etc. + +local skang = require "skang" +local _M = skang.moduleBegin("test_c", nil, "Copyright 2014 David Seikel", "0.1", "2014-03-27 03:57:00") + +skang.thing("fooble,f", "Help text goes here", 1, "number", "'edit', 'The fooble:', 1, 1, 10, 50", true) +skang.thing("bar", "Help text", "Default") +skang.thing("foo") +skang.thing("ffunc", "Help Text", ffunc, "number,string") + +skang.moduleEnd(_M) */ + return 1; +} -- cgit v1.1