summaryrefslogtreecommitdiff
path: root/Examples/lua/funcptr3
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@baserock.org>2009-08-18 20:56:02 +0000
committerLorry <lorry@roadtrain.codethink.co.uk>2012-09-25 16:59:08 +0000
commit9f8a09ed743cedd9547bf0661d518647966ab114 (patch)
tree9c7803d3b27a8ec22e91792ac7f7932efa128b20 /Examples/lua/funcptr3
downloadswig-tarball-master.tar.gz
Imported from /srv/lorry/lorry-area/swig-tarball/swig-1.3.40.tar.gz.HEADswig-1.3.40master
Diffstat (limited to 'Examples/lua/funcptr3')
-rw-r--r--Examples/lua/funcptr3/Makefile19
-rw-r--r--Examples/lua/funcptr3/example.c19
-rw-r--r--Examples/lua/funcptr3/example.h9
-rw-r--r--Examples/lua/funcptr3/example.i69
-rw-r--r--Examples/lua/funcptr3/runme.lua54
5 files changed, 170 insertions, 0 deletions
diff --git a/Examples/lua/funcptr3/Makefile b/Examples/lua/funcptr3/Makefile
new file mode 100644
index 0000000..ac0fff4
--- /dev/null
+++ b/Examples/lua/funcptr3/Makefile
@@ -0,0 +1,19 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS = example.c
+TARGET = example
+INTERFACE = example.i
+LIBS =
+
+all::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' lua
+
+static::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='mylua' INTERFACE='$(INTERFACE)' lua_static
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile lua_clean
+
+check: all
diff --git a/Examples/lua/funcptr3/example.c b/Examples/lua/funcptr3/example.c
new file mode 100644
index 0000000..5c4a3da
--- /dev/null
+++ b/Examples/lua/funcptr3/example.c
@@ -0,0 +1,19 @@
+/* File : example.c */
+
+int do_op(int a, int b, int (*op)(int,int)) {
+ return (*op)(a,b);
+}
+
+int add(int a, int b) {
+ return a+b;
+}
+
+int sub(int a, int b) {
+ return a-b;
+}
+
+int mul(int a, int b) {
+ return a*b;
+}
+
+int (*funcvar)(int,int) = add;
diff --git a/Examples/lua/funcptr3/example.h b/Examples/lua/funcptr3/example.h
new file mode 100644
index 0000000..9936e24
--- /dev/null
+++ b/Examples/lua/funcptr3/example.h
@@ -0,0 +1,9 @@
+/* file: example.h */
+
+extern int do_op(int,int, int (*op)(int,int));
+extern int add(int,int);
+extern int sub(int,int);
+extern int mul(int,int);
+
+extern int (*funcvar)(int,int);
+
diff --git a/Examples/lua/funcptr3/example.i b/Examples/lua/funcptr3/example.i
new file mode 100644
index 0000000..319b227
--- /dev/null
+++ b/Examples/lua/funcptr3/example.i
@@ -0,0 +1,69 @@
+/* File : example.i */
+/*
+This demonstrates how to pass a lua function, into some C code and then call it.
+
+There are two examples, the first is as a parameter, the second as a global variable.
+
+*/
+%module example
+%{
+#include "example.h"
+%}
+/* the extra wrappers for lua functions, see SWIG/Lib/lua/lua_fnptr.i for more details */
+%include "lua_fnptr.i"
+
+/* these are a bunch of C functions which we want to be able to call from lua */
+extern int add(int,int);
+extern int sub(int,int);
+extern int mul(int,int);
+
+/* this function takes a lua function as a parameter and calls it.
+As this is takes a lua fn it needs lua code
+*/
+%inline %{
+
+int callback(int a, int b, SWIGLUA_FN fn)
+{
+ SWIGLUA_FN_GET(fn);
+ lua_pushnumber(fn.L,a);
+ lua_pushnumber(fn.L,b);
+ lua_call(fn.L,2,1); /* 2 in, 1 out */
+ return (int)luaL_checknumber(fn.L,-1);
+}
+%}
+
+/******************
+Second code uses a stored reference.
+*******************/
+
+%inline %{
+/* note: this is not so good to just have it as a raw ref
+ people could set anything to this
+ a better solution would to be to have a fn which wants a SWIGLUA_FN, then
+ checks the type & converts to a SWIGLUA_REF.
+*/
+SWIGLUA_REF the_func={0,0};
+
+void call_the_func(int a)
+{
+ int i;
+ if (the_func.L==0){
+ printf("the_func is zero\n");
+ return;
+ }
+ swiglua_ref_get(&the_func);
+ if (!lua_isfunction(the_func.L,-1))
+ {
+ printf("the_func is not a function\n");
+ return;
+ }
+ lua_pop(the_func.L,1); /* tidy stack */
+ for(i=0;i<a;i++)
+ {
+ swiglua_ref_get(&the_func);
+ lua_pushnumber(the_func.L,i);
+ lua_call(the_func.L,1,0); /* 1 in, 0 out */
+ }
+}
+
+%}
diff --git a/Examples/lua/funcptr3/runme.lua b/Examples/lua/funcptr3/runme.lua
new file mode 100644
index 0000000..b78c504
--- /dev/null
+++ b/Examples/lua/funcptr3/runme.lua
@@ -0,0 +1,54 @@
+---- importing ----
+if string.sub(_VERSION,1,7)=='Lua 5.0' then
+ -- lua5.0 doesnt have a nice way to do this
+ lib=loadlib('example.dll','luaopen_example') or loadlib('example.so','luaopen_example')
+ assert(lib)()
+else
+ -- lua 5.1 does
+ require('example')
+end
+
+a = 37
+b = 42
+
+-- Now call our C function
+
+print("Trying some C functions")
+print(" a =", a)
+print(" b =", b)
+print(" add(a,b) =", example.add(a,b))
+print(" sub(a,b) =", example.sub(a,b))
+print(" mul(a,b) =", example.mul(a,b))
+
+print("Calling them using the my_func()")
+print(" add(a,b) =", example.callback(a,b,example.add))
+print(" sub(a,b) =", example.callback(a,b,example.sub))
+print(" mul(a,b) =", example.callback(a,b,example.mul))
+
+print("Now let us write our own function")
+function foo(a,b) return 101 end
+print(" foo(a,b) =", example.callback(a,b,foo))
+
+print("Now let us try something that will fail")
+local ok,c=pcall(example.callback,a,b,print)
+if ok==false then
+ print("this failed as expected, error:",c)
+else
+ print("oops, that worked! result:",c)
+end
+
+
+-- part2 stored function
+print("trying a stored fn")
+print("the_func=",example.the_func)
+print("setting to print")
+example.the_func=print
+print("the_func=",example.the_func)
+print("call_the_func(5)")
+example.call_the_func(5)
+
+function bar(i) print("bar",i) end
+print("setting to bar")
+example.the_func=bar
+print("call_the_func(5)")
+example.call_the_func(5)