diff options
Diffstat (limited to '')
-rw-r--r-- | ClientHamr/GuiLua/test_c.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/ClientHamr/GuiLua/test_c.c b/ClientHamr/GuiLua/test_c.c index d72067e..8c3796d 100644 --- a/ClientHamr/GuiLua/test_c.c +++ b/ClientHamr/GuiLua/test_c.c | |||
@@ -22,4 +22,57 @@ Though it seems that chapter 26 is not in the same place? | |||
22 | http://www.lua.org/pil/26.2.html doesn't say much really, and is for | 22 | http://www.lua.org/pil/26.2.html doesn't say much really, and is for |
23 | Lua 5.0 | 23 | Lua 5.0 |
24 | 24 | ||
25 | |||
26 | |||
27 | An example - | ||
28 | |||
29 | // build@ gcc -shared -I/home/sdonovan/lua/include -o mylib.so mylib.c | ||
30 | // includes for your code | ||
31 | #include <string.h> | ||
32 | #include <math.h> | ||
33 | |||
34 | // includes for Lua | ||
35 | #include <lua.h> | ||
36 | #include <lauxlib.h> | ||
37 | #include <lualib.h> | ||
38 | |||
39 | // defining functions callable from Lua | ||
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 | |||
47 | static int l_solve (lua_State *L) { | ||
48 | double a = lua_tonumber(L,1); // coeff of x*x | ||
49 | double b = lua_tonumber(L,2); // coef of x | ||
50 | double c = lua_tonumber(L,3); // constant | ||
51 | double abc = b*b - 4*a*c; | ||
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 | |||
65 | static const luaL_reg mylib[] = { | ||
66 | {"createtable",l_createtable}, | ||
67 | {"solve",l_solve}, | ||
68 | {NULL,NULL} | ||
69 | }; | ||
70 | |||
71 | int luaopen_mylib(lua_State *L) | ||
72 | { | ||
73 | luaL_register (L, "mylib", mylib); | ||
74 | return 1; | ||
75 | } | ||
76 | |||
77 | |||
25 | */ | 78 | */ |