diff options
Diffstat (limited to '')
-rw-r--r-- | ClientHamr/GuiLua/test_c.c | 113 |
1 files changed, 57 insertions, 56 deletions
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 @@ | |||
1 | /* Should be a Lua module, roughly the same as test.lua | 1 | /* Should be a Lua skang module, roughly the same as test.lua |
2 | 2 | ||
3 | */ | 3 | */ |
4 | 4 | ||
5 | 5 | ||
6 | #include <lua.h> | ||
7 | #include <lauxlib.h> | ||
8 | //#include <lualib.h> | ||
6 | 9 | ||
7 | /* NOTES - | ||
8 | 10 | ||
9 | From http://www.inf.puc-rio.br/~roberto/pil2/chapter15.pdf | 11 | static int ffunc (lua_State *L) |
12 | { | ||
13 | double arg1 = luaL_checknumber(L, 1); | ||
14 | const char *arg2 = luaL_checkstring(L, 2); | ||
10 | 15 | ||
11 | "Well-behaved C libraries should export one function called | 16 | printf("Inside test_c.ffunc(%f, %s)\n", arg1, arg2); |
12 | luaopen_modname, which is the function that require tries to call after | 17 | return 0; |
13 | linking the library. In Section 26.2 we will discuss how to write C | 18 | } |
14 | libraries." | ||
15 | 19 | ||
16 | The "modname" bit is replaced by the name of the module. Though if the | ||
17 | module name includes a hyphen, the "require" function strips out the | ||
18 | hyphen and the bit before it. | ||
19 | 20 | ||
20 | Though it seems that chapter 26 is not in the same place? | 21 | static const struct luaL_reg test_c [] = |
22 | { | ||
23 | {"ffunc", ffunc}, | ||
24 | {NULL, NULL} | ||
25 | }; | ||
21 | 26 | ||
22 | http://www.lua.org/pil/26.2.html doesn't say much really, and is for | ||
23 | Lua 5.0 | ||
24 | 27 | ||
28 | /* local test_c = require 'test_c' | ||
25 | 29 | ||
30 | Lua's require() function will strip any stuff from the front of the name | ||
31 | separated by a hypen, so 'GuiLua-test_c' -> 'test_c'. Then it will | ||
32 | search through a path, and eventually find this test_c.so (or test_c.dll | ||
33 | or whatever), then call luaopen_test_c(), which should return a table. | ||
26 | 34 | ||
27 | An example - | 35 | Normally luaL_register() creates a table of functions, that is the table |
36 | returned, but we want to do something different with skang. | ||
28 | 37 | ||
29 | // build@ gcc -shared -I/home/sdonovan/lua/include -o mylib.so mylib.c | 38 | */ |
30 | // includes for your code | 39 | int luaopen_test_c(lua_State *L) |
31 | #include <string.h> | 40 | { |
32 | #include <math.h> | 41 | // This is a moving target, old ways get deperecated, new ways get added, |
42 | // would have to check version before doing any of these. | ||
43 | // luaL_openlib(L, "test_c", test_c, 0); // Lua 5.0 way. | ||
44 | // luaL_register (L, "test_c", test_c); // Lua 5.1 way. | ||
45 | // luaL_newlib() or luaL_setfuncs() // Lua 5.2 way. | ||
46 | // Creates a global table "test_c", does the package.loaded[test_c] thing. | ||
47 | lua_newtable(L); | ||
48 | luaL_register (L, NULL, test_c); // Lua 5.1 way. | ||
49 | // Puts the funcions in a table on top of the stack. | ||
33 | 50 | ||
34 | // includes for Lua | 51 | /* BUT REALLY ... |
35 | #include <lua.h> | ||
36 | #include <lauxlib.h> | ||
37 | #include <lualib.h> | ||
38 | 52 | ||
39 | // defining functions callable from Lua | 53 | We are in fact NOT putting any functions into the returned table. |
40 | static int l_createtable (lua_State *L) { | ||
41 | int narr = luaL_optint(L,1,0); // initial array slots, default 0 | ||
42 | int nrec = luaL_optint(L,2,0); // intialof hash slots, default 0 | ||
43 | lua_createtable(L,narr,nrec); | ||
44 | return 1; | ||
45 | } | ||
46 | 54 | ||
47 | static int l_solve (lua_State *L) { | 55 | skang.moduleBegin() returns the table we need to send back to Lua. |
48 | double a = lua_tonumber(L,1); // coeff of x*x | 56 | it saves getfenv(2) as the old environment, which should in theory be L |
49 | double b = lua_tonumber(L,2); // coef of x | 57 | and setfenv(_M, 2) to set the tbale to be it's own environment |
50 | double c = lua_tonumber(L,3); // constant | 58 | it does the package.loaded[test_c] thing for us |
51 | double abc = b*b - 4*a*c; | 59 | it returns the table it created, so we should just leave that on the stack as our result |
52 | if (abc < 0.0) { | ||
53 | lua_pushnil(L); | ||
54 | lua_pushstring(L,"imaginary roots!"); | ||
55 | return 2; | ||
56 | } else { | ||
57 | abc = sqrt(abc); | ||
58 | a = 2*a; | ||
59 | lua_pushnumber(L,(-b + abc)/a); | ||
60 | lua_pushnumber(L,(+b - abc)/a); | ||
61 | return 2; | ||
62 | } | ||
63 | } | ||
64 | 60 | ||
65 | static const luaL_reg mylib[] = { | 61 | skang.thing() also uses getfenv(2) to grab the module's table |
66 | {"createtable",l_createtable}, | ||
67 | {"solve",l_solve}, | ||
68 | {NULL,NULL} | ||
69 | }; | ||
70 | 62 | ||
71 | int luaopen_mylib(lua_State *L) | 63 | */ |
72 | { | ||
73 | luaL_register (L, "mylib", mylib); | ||
74 | return 1; | ||
75 | } | ||
76 | 64 | ||
65 | /* TODO - load skang, create things, etc. | ||
66 | |||
67 | local skang = require "skang" | ||
68 | local _M = skang.moduleBegin("test_c", nil, "Copyright 2014 David Seikel", "0.1", "2014-03-27 03:57:00") | ||
69 | |||
70 | skang.thing("fooble,f", "Help text goes here", 1, "number", "'edit', 'The fooble:', 1, 1, 10, 50", true) | ||
71 | skang.thing("bar", "Help text", "Default") | ||
72 | skang.thing("foo") | ||
73 | skang.thing("ffunc", "Help Text", ffunc, "number,string") | ||
74 | |||
75 | skang.moduleEnd(_M) | ||
77 | 76 | ||
78 | */ | 77 | */ |
78 | return 1; | ||
79 | } | ||