summaryrefslogtreecommitdiff
path: root/Examples/lua/arrays
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/arrays
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/arrays')
-rw-r--r--Examples/lua/arrays/Makefile18
-rw-r--r--Examples/lua/arrays/example.c25
-rw-r--r--Examples/lua/arrays/example.i42
-rw-r--r--Examples/lua/arrays/runme.lua74
4 files changed, 159 insertions, 0 deletions
diff --git a/Examples/lua/arrays/Makefile b/Examples/lua/arrays/Makefile
new file mode 100644
index 0000000..bb9cf0b
--- /dev/null
+++ b/Examples/lua/arrays/Makefile
@@ -0,0 +1,18 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS = example.c
+TARGET = example
+INTERFACE = example.i
+
+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/arrays/example.c b/Examples/lua/arrays/example.c
new file mode 100644
index 0000000..56c7b19
--- /dev/null
+++ b/Examples/lua/arrays/example.c
@@ -0,0 +1,25 @@
+/* File : example.c */
+
+#include <stdlib.h>
+
+/* we are using the qsort function, which needs a helper function to sort */
+int compare_int(const void * a, const void * b)
+{
+ return ( *(int*)a - *(int*)b );
+}
+
+void sort_int(int* arr, int len)
+{
+ qsort(arr, len, sizeof(int), compare_int);
+}
+
+// ditto doubles
+int compare_double(const void * a, const void * b)
+{
+ return (int)( *(double*)a - *(double*)b );
+}
+
+void sort_double(double* arr, int len)
+{
+ qsort(arr, len, sizeof(double), compare_double);
+}
diff --git a/Examples/lua/arrays/example.i b/Examples/lua/arrays/example.i
new file mode 100644
index 0000000..197a5a2
--- /dev/null
+++ b/Examples/lua/arrays/example.i
@@ -0,0 +1,42 @@
+/* File : example.i */
+%module example
+
+/* in this file there are two sorting functions
+and three different ways to wrap them.
+
+See the lua code for how they are called
+*/
+
+%include <carrays.i> // array helpers
+
+// this declares a batch of function for manipulating C integer arrays
+%array_functions(int,int)
+
+// this adds some lua code directly into the module
+// warning: you need the example. prefix if you want it added into the module
+// addmittedly this code is a bit tedious, but its a one off effort
+%luacode {
+function example.sort_int2(t)
+ local len=table.maxn(t) -- the len
+ local arr=example.new_int(len)
+ for i=1,len do
+ example.int_setitem(arr,i-1,t[i]) -- note: C index is one less then lua indea
+ end
+ example.sort_int(arr,len) -- call the fn
+ -- copy back
+ for i=1,len do
+ t[i]=example.int_getitem(arr,i-1) -- note: C index is one less then lua indea
+ end
+ example.delete_int(arr) -- must delete it
+end
+}
+
+// this way uses the SWIG-Lua typemaps to do the conversion for us
+// the %apply command states to apply this wherever the argument signature matches
+%include <typemaps.i>
+%apply (double *INOUT,int) {(double* arr,int len)};
+
+%inline %{
+extern void sort_int(int* arr, int len);
+extern void sort_double(double* arr, int len);
+%} \ No newline at end of file
diff --git a/Examples/lua/arrays/runme.lua b/Examples/lua/arrays/runme.lua
new file mode 100644
index 0000000..b0f5cfc
--- /dev/null
+++ b/Examples/lua/arrays/runme.lua
@@ -0,0 +1,74 @@
+---- 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 helper to print a Lua table
+function print_table(t)
+ print(table.concat(t,","))
+end
+
+-- a helper to print a C array
+function print_array(arr,len)
+ for i=0,len-1 do
+ io.write(example.int_getitem(arr,i),",")
+ end
+ io.write("\n")
+end
+
+math.randomseed(0) -- init random
+
+
+--[[ version 1: passing a C array to the code
+lets test call sort_int()
+this requires a C array, so is the hardest to use]]
+ARRAY_SIZE=10
+arr=example.new_int(ARRAY_SIZE)
+for i=0,ARRAY_SIZE-1 do
+ example.int_setitem(arr,i,math.random(1000))
+end
+print "unsorted"
+print_array(arr,ARRAY_SIZE)
+example.sort_int(arr,ARRAY_SIZE)
+print "sorted"
+print_array(arr,ARRAY_SIZE)
+example.delete_int(arr) -- must delete it
+print ""
+
+--[[ version 2: using %luacode to write a helper
+a simpler way is to use a %luacode
+which is a lua function added into the module
+this can do the conversion for us
+so we can just add a lua table directly
+(what we do is move the lua code into the module instead)
+]]
+t={}
+for i=1,ARRAY_SIZE do
+ t[i]=math.random(1000)
+end
+print "unsorted"
+print_table(t)
+example.sort_int2(t) -- calls lua helper which then calls C
+print "sorted"
+print_table(t)
+print ""
+
+--[[ version 3: use a typemap
+this is the best way
+it uses the SWIG-Lua typemaps to do the work
+one item of note: the typemap creates a copy, rather than edit-in-place]]
+t={}
+for i=1,ARRAY_SIZE do
+ t[i]=math.random(1000)/10
+end
+print "unsorted"
+print_table(t)
+t=example.sort_double(t) -- replace t with the result
+print "sorted"
+print_table(t)
+