aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/GuiLua/test_c.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/GuiLua/test_c.c')
-rw-r--r--src/GuiLua/test_c.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/GuiLua/test_c.c b/src/GuiLua/test_c.c
new file mode 100644
index 0000000..5328bda
--- /dev/null
+++ b/src/GuiLua/test_c.c
@@ -0,0 +1,88 @@
1/* Should be a Lua skang module, roughly the same as test.lua
2
3Seems to be several problems with linking in various OSes, here's some
4possibly helpful links -
5
6http://lua.2524044.n2.nabble.com/C-Lua-modules-not-compatible-with-every-Lua-interpreter-td7647522.html
7http://lua-users.org/wiki/LuaProxyDllFour
8http://stackoverflow.com/questions/11492194/how-do-you-create-a-lua-plugin-that-calls-the-c-lua-api?rq=1
9http://lua-users.org/lists/lua-l/2008-01/msg00671.html
10*/
11
12
13#include "GuiLua.h"
14
15
16static const char *ourName = "test_c";
17int skang, _M;
18
19static int cfunc (lua_State *L)
20{
21 double arg1 = luaL_checknumber(L, 1);
22 const char *arg2 = luaL_checkstring(L, 2);
23
24 printf("Inside %s.cfunc(%f, %s)\n", ourName, arg1, arg2);
25 return 0;
26}
27
28/* local test_c = require 'test_c'
29
30Lua's require() function will strip any stuff from the front of the name
31separated by a hyphen, so 'ClientHamr-GuiLua-test_c' -> 'test_c'. Then
32it will search through a path, and eventually find this test_c.so (or
33test_c.dll or whatever), then call luaopen_test_c(), which should return
34a table. The argument (only thing on the stack) for this function will
35be 'test_c'.
36
37Normally luaL_register() creates a table of functions, that is the table
38returned, but we want to do something different with skang.
39*/
40int luaopen_test_c(lua_State *L)
41{
42 // In theory, the only thing on the stack now is 'test_c' from the require() call.
43
44// pseudo-indices, special tables that can be accessed like the stack -
45// LUA_GLOBALSINDEX - thread environment, where globals are
46// LUA_ENVIRONINDEX - C function environment, in this case luaopen_test_c() is the C function
47// LUA_REGISTRYINDEX - C registry, global, for unique keys use the module name as a string, or a lightuserdata address to a C object in our module.
48// lua_upvalueindex(n) - C function upvalues
49
50// The only locals we care about are skang and _M.
51// All modules go into package.loaded[name] as well.
52// skang is essentially a global anyway.
53// _M we pass back as the result, and our functions get added to it by skang.thingasm()
54// Not entirely true, _M is a proxy table, getmetatable(_M).__values[cfunc] would be our function.
55
56// local skang = require 'skang'
57 lua_getglobal(L, "require");
58 lua_pushstring(L, SKANG);
59 lua_call(L, 1, 1);
60 lua_setfield(L, LUA_REGISTRYINDEX, SKANG);
61 lua_getfield(L, LUA_REGISTRYINDEX, SKANG);
62 skang = lua_gettop(L);
63
64// local _M = skang.moduleBegin('test_c', nil, 'Copyright 2014 David Seikel', '0.1', '2014-03-27 03:57:00', nil, false)
65 push_lua(L, "@ ( $ ~ $ $ $ ~ ! )", skang, MODULEBEGIN, ourName, "Copyright 2014 David Seikel", "0.1", "2014-03-27 03:57:00", 0, 1);
66 lua_setfield(L, LUA_REGISTRYINDEX, ourName);
67 lua_getfield(L, LUA_REGISTRYINDEX, ourName);
68 _M = lua_gettop(L);
69
70// This uses function{} style.
71// skang.thingasm{_M, 'cfooble,c', 'cfooble help text', 1, widget=\"'edit', 'The cfooble:', 1, 1, 10, 50\", required=true}
72 push_lua(L, "@ ( { = $ $ % $widget !required } )", skang, THINGASM, _M, "cfooble,c", "cfooble help text", 1, "'edit', 'The cfooble:', 1, 1, 10, 50", 1, 0);
73
74// skang.thing(_M, 'cbar', 'Help text', 'Default')
75 push_lua(L, "@ ( = $ $ $ )", skang, THINGASM, _M, "cbar", "Help text", "Default", 0);
76
77// skang.thingasm(_M, 'cfoo')
78 push_lua(L, "@ ( = $ )", skang, THINGASM, _M, "cfoo", 0);
79
80// skang.thingasm(_M, 'cfunc', 'cfunc does nothing really', cfunc, 'number,string')
81 push_lua(L, "@ ( = $ $ & $ )", skang, THINGASM, _M, "cfunc", "cfunc does nothing really", cfunc, "number,string", 0);
82
83// skang.moduleEnd(_M)
84 push_lua(L, "@ ( = )", skang, MODULEEND, _M, 0);
85
86 // Return _M, the table itself, not the index.
87 return 1;
88}