aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ClientHamr/GuiLua/test_c.c
diff options
context:
space:
mode:
authorDavid Walter Seikel2014-03-27 16:17:53 +1000
committerDavid Walter Seikel2014-03-27 16:17:53 +1000
commit9e64b723462544dbc4d64a9179a71fce124018b6 (patch)
tree8589f1fbf4a00e712c8980dee60ca8036293eea0 /ClientHamr/GuiLua/test_c.c
parentFound an old PIL chapter 26. (diff)
downloadSledjHamr-9e64b723462544dbc4d64a9179a71fce124018b6.zip
SledjHamr-9e64b723462544dbc4d64a9179a71fce124018b6.tar.gz
SledjHamr-9e64b723462544dbc4d64a9179a71fce124018b6.tar.bz2
SledjHamr-9e64b723462544dbc4d64a9179a71fce124018b6.tar.xz
Add an example of a C Lua module in the test_c comments.
Diffstat (limited to 'ClientHamr/GuiLua/test_c.c')
-rw-r--r--ClientHamr/GuiLua/test_c.c53
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?
22http://www.lua.org/pil/26.2.html doesn't say much really, and is for 22http://www.lua.org/pil/26.2.html doesn't say much really, and is for
23Lua 5.0 23Lua 5.0
24 24
25
26
27An 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
40static 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
47static 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
65static const luaL_reg mylib[] = {
66 {"createtable",l_createtable},
67 {"solve",l_solve},
68 {NULL,NULL}
69};
70
71int luaopen_mylib(lua_State *L)
72{
73 luaL_register (L, "mylib", mylib);
74 return 1;
75}
76
77
25*/ 78*/