From 012b4dd32d92aa58c48e6754c73c989bc1eda48c Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Thu, 17 Apr 2014 15:13:31 +1000 Subject: GuiLuaDo() now passes args to Lua in arg, then lets skang do most of the work. Plus clean ups from that. --- ClientHamr/GuiLua/GuiLua.c | 61 ++++++++++++++++++++++++++-------------------- ClientHamr/GuiLua/GuiLua.h | 1 - ClientHamr/GuiLua/build.sh | 1 + ClientHamr/GuiLua/test.lua | 1 - 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/ClientHamr/GuiLua/GuiLua.c b/ClientHamr/GuiLua/GuiLua.c index c441fcf..49c62b8 100644 --- a/ClientHamr/GuiLua/GuiLua.c +++ b/ClientHamr/GuiLua/GuiLua.c @@ -531,8 +531,6 @@ static int window(lua_State *L) ourGlobals = lua_touserdata(L, -1); lua_pop(L, 1); - loggingStartup(ourGlobals); - PI("GuiLua running as an application.\n"); if (pull_lua(L, 1, "%w %h $title", &w, &h, &title) > 0) PI("Setting window to %d %d %s", w, h, title); else @@ -601,6 +599,14 @@ int luaopen_widget(lua_State *L) { // In theory, the only thing on the stack now is 'widget' from the require() call. + // In theory this function only ever gets called once. + memset(&ourGlobals, 0, sizeof(globals)); + loggingStartup(&ourGlobals); + + // Shove ourGlobals into the registry. + lua_pushlightuserdata(L, &ourGlobals); + lua_setfield(L, LUA_REGISTRYINDEX, "ourGlobals"); + // pseudo-indices, special tables that can be accessed like the stack - // LUA_GLOBALSINDEX - thread environment, where globals are // LUA_ENVIRONINDEX - C function environment, in this case luaopen_test_c() is the C function @@ -618,14 +624,17 @@ int luaopen_widget(lua_State *L) lua_pushstring(L, "skang"); lua_call(L, 1, 1); skang = lua_gettop(L); + lua_setfield(L, LUA_REGISTRYINDEX, "skang"); lua_getfield(L, LUA_REGISTRYINDEX, "skang"); // local _M = skang.moduleBegin('widget', nil, 'Copyright 2014 David Seikel', '0.1', '2014-04-08 00:42:00', nil, false) push_lua(L, "@ ( $ ~ $ $ $ ~ ! )", skang, "moduleBegin", ourName, "Copyright 2014 David Seikel", "0.1", "2014-04-08 00:42:00", 0, 1); _M = lua_gettop(L); + // Save this module in the C registry. lua_setfield(L, LUA_REGISTRYINDEX, ourName); + lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Define our functions. push_lua(L, "@ ( @ $ $ & $ )", skang, "thingasm", LUA_REGISTRYINDEX, ourName, "window", "Opens our window.", window, "number,number,string", 0); @@ -642,41 +651,41 @@ int luaopen_widget(lua_State *L) void GuiLuaDo(int argc, char **argv) { + lua_State *L; // Our Lua state. lua_Number i; - memset(&ourGlobals, 0, sizeof(globals)); - ourGlobals.L = luaL_newstate(); - if (ourGlobals.L) + L = luaL_newstate(); + if (L) { - luaL_openlibs(ourGlobals.L); - // Shove ourGlobals into the registry. - lua_pushlightuserdata(ourGlobals.L, &ourGlobals); - lua_setfield(ourGlobals.L, LUA_REGISTRYINDEX, "ourGlobals"); + luaL_openlibs(L); - // luaopen_widget() expects a string argument, and returns a table. - // Though in this case, both get ignored anyway. - lua_pushstring(ourGlobals.L, "widget"); - lua_pop(ourGlobals.L, luaopen_widget(ourGlobals.L) + 1); - - // Pass all our command line arguments to skang. + // Pass all our command line arguments to Lua. i = 1; - push_lua(ourGlobals.L, "@ ( @", LUA_REGISTRYINDEX, "skang", 1, "scanArguments"); - lua_newtable(ourGlobals.L); + lua_newtable(L); while (--argc > 0 && *++argv != '\0') { - lua_pushnumber(ourGlobals.L, i++); - lua_pushstring(ourGlobals.L, *argv); - lua_settable(ourGlobals.L, -3); + lua_pushnumber(L, i++); + lua_pushstring(L, *argv); + lua_settable(L, -3); } - lua_call(ourGlobals.L, 1, 0); - push_lua(ourGlobals.L, "@ ( @ )", 1, "pullArguments", LUA_REGISTRYINDEX, "skang", 0); + lua_setfield(L, LUA_GLOBALSINDEX, "arg"); - // Run the main loop via a Lua call. - lua_pop(ourGlobals.L, loopWindow(ourGlobals.L)); - lua_pop(ourGlobals.L, closeWindow(ourGlobals.L)); - lua_close(ourGlobals.L); + lua_getglobal(L, "require"); + lua_pushstring(L, "skang"); + // When we do this, skang will process all the arguments passed to GuiLuaDo(). + // This likely includes a module load, which likely loads the widget module above. + lua_call(L, 1, 1); + skang = lua_gettop(L); + lua_setfield(L, LUA_REGISTRYINDEX, "skang"); + + + // Run the main loop via a Lua call. + // This does nothing if no module opened a window. + lua_pop(L, loopWindow(L)); + lua_pop(L, closeWindow(L)); + lua_close(L); } else fprintf(stderr, "Failed to start Lua!\n"); diff --git a/ClientHamr/GuiLua/GuiLua.h b/ClientHamr/GuiLua/GuiLua.h index dfc554a..1041224 100644 --- a/ClientHamr/GuiLua/GuiLua.h +++ b/ClientHamr/GuiLua/GuiLua.h @@ -46,7 +46,6 @@ typedef enum struct _globals { Evas_Object *win; // Our Elm window. - lua_State *L; // Our Lua state. int logDom; // Our logging domain. }; diff --git a/ClientHamr/GuiLua/build.sh b/ClientHamr/GuiLua/build.sh index 076c106..c39df5b 100755 --- a/ClientHamr/GuiLua/build.sh +++ b/ClientHamr/GuiLua/build.sh @@ -23,5 +23,6 @@ gcc $CFLAGS -fPIC -shared -o test_c.so test_c.c gcc $CFLAGS -fPIC -c GuiLua.c echo "C libraries" gcc $CFLAGS -shared -Wl,-soname,libGuiLua.so -o libGuiLua.so GuiLua.o +ln -fs libGuiLua.so widget.so echo "C apps" gcc $CFLAGS -Wl,-export-dynamic -o skang skang.c $LDFLAGS -lGuiLua $libs diff --git a/ClientHamr/GuiLua/test.lua b/ClientHamr/GuiLua/test.lua index d484cf2..fc5de1b 100644 --- a/ClientHamr/GuiLua/test.lua +++ b/ClientHamr/GuiLua/test.lua @@ -42,7 +42,6 @@ local copy = skang.copy(test, 'copy') print('End ' .. test.bar .. ' ' .. test.VERSION .. ' ' .. skang.get(test, 'ffunc', 'help') .. ' ->> ' .. skang.get(test, 'f', 'action')) -print(test.DEFAULT_SKANG) print('') print('foo = ' .. test.foo .. ' ->> ' .. skang.get(test, 'foo', 'help')) -- cgit v1.1