[lua] added %luacode feature, documentation & examples
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10312 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
39ad0f01f6
commit
e543cd9040
12 changed files with 279 additions and 25 deletions
18
Examples/lua/arrays/Makefile
Normal file
18
Examples/lua/arrays/Makefile
Normal file
|
|
@ -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
|
||||
25
Examples/lua/arrays/example.c
Normal file
25
Examples/lua/arrays/example.c
Normal file
|
|
@ -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 ( *(double*)a - *(double*)b );
|
||||
}
|
||||
|
||||
void sort_double(double* arr, int len)
|
||||
{
|
||||
qsort(arr, len, sizeof(double), compare_double);
|
||||
}
|
||||
42
Examples/lua/arrays/example.i
Normal file
42
Examples/lua/arrays/example.i
Normal file
|
|
@ -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);
|
||||
%}
|
||||
74
Examples/lua/arrays/runme.lua
Normal file
74
Examples/lua/arrays/runme.lua
Normal file
|
|
@ -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)
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue