Support for Lua added - patch from Mark Gossage

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7365 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2005-08-15 20:58:56 +00:00
commit 3941e19141
27 changed files with 2266 additions and 0 deletions

View file

@ -0,0 +1,62 @@
#######################################################################
# $Header$
# Makefile for lua test-suite
#######################################################################
LANGUAGE = lua
LUA = @LUABIN@
SCRIPTSUFFIX = _runme.lua
srcdir = @srcdir@
top_srcdir = @top_srcdir@
top_builddir = @top_builddir@
# sorry, currently very few test cases work/have been written
#CPP_TEST_CASES += \
# cnum
#C_TEST_CASES += \
# file_test \
# nondynamic
include $(srcdir)/../common.mk
# Overridden variables here
LIBS = -L.
# Rules for the different types of tests
%.cpptest:
$(setup) \
($(swig_and_compile_cpp); ) &&\
$(run_testcase)
%.ctest:
$(setup) \
($(swig_and_compile_c); ) &&\
$(run_testcase)
%.multicpptest:
$(setup) \
($(swig_and_compile_multi_cpp); ) &&\
$(run_testcase)
# Runs the testcase. A testcase is only run if
# a file is found which has _runme.lua appended after the testcase name.
run_testcase = \
if [ -f $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) ]; then ( \
$(LUA) $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX);) \
fi;
# Clean: (does nothing, we dont generate extra lua code)
%.clean:
clean:
$(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile lua_clean
cvsignore:
@echo '*wrap* *.so *.dll *.exp *.lib'
@echo Makefile
@for i in ${CPP_TEST_CASES} ${C_TEST_CASES}; do echo $$i.lua; done
@for i in ${CPP_TEST_CASES} ${C_TEST_CASES}; do if grep -q $${i}_runme.lua CVS/Entries ; then echo $${i}_runme.lua; fi; done

View file

@ -0,0 +1,17 @@
require("import") -- the import fn
import("abstract_access") -- import code
-- catch "undefined" global variables
setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
-- trying to instantiate pure virual classes
-- should fail
assert(pcall(abstract_access.A)==false)
assert(pcall(abstract_access.B)==false)
assert(pcall(abstract_access.C)==false)
-- instantiate object
d=abstract_access.D()
--call fn
assert(d:do_x()==1)

View file

@ -0,0 +1,20 @@
require("import") -- the import fn
import("enums",false) -- import lib
-- catch "undefined" global variables
setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
-- check values
assert(enums.CSP_ITERATION_FWD==0)
assert(enums.CSP_ITERATION_BWD==11)
assert(enums.ABCDE==0)
assert(enums.FGHJI==1)
assert(enums.boo==0)
assert(enums.hoo==5)
assert(enums.globalinstance1==0)
assert(enums.globalinstance2==1)
assert(enums.globalinstance3==30)
assert(enums.AnonEnum1==0)
assert(enums.AnonEnum2==100)
-- no point in checking fns, C will allow any value

View file

@ -0,0 +1,29 @@
-- demo of lua swig capacilities (operator overloading)
require("import") -- the import fn
import("exception_order",true) -- import lib into global
-- catching undefined variables
setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
a = A()
function try1()
a:foo()
end
ok,ex=pcall(try1)
assert(ok==false and swig_type(ex)==swig_type(E1()))
function try2()
a:bar()
end
ok,ex=pcall(try2)
assert(ok==false and swig_type(ex)==swig_type(E2()))
function try3()
a:foobar()
end
ok,ex=pcall(try3)
assert(ok==false and type(ex)=="string")
-- the SWIG_exception is just an error string

View file

@ -0,0 +1,25 @@
-- import
function import(module,intoglobal)
-- imports the file into the program
-- for a module example
-- this must load 'example.dll'
-- and look for the fn 'Example_Init()' (note the capitalisation)
local libname=module..".dll" -- windows
--libname=module..".so" -- unix
-- capitialising the first letter
local c=string.upper(string.sub(module,1,1))
local fnname=c..string.sub(module,2).."_Init"
assert(loadlib(libname,fnname),"error loading module:"..module)()
-- moving to global namespace
if intoglobal then
--m=raw_get(_G,module) -- gets the module object
local m=_G[module] -- gets the module object
assert(m~=nil,"no module table found")
local k,v
for k,v in m do _G[k]=v end
end
end

View file

@ -0,0 +1,16 @@
require("import") -- the import fn
import("newobject1",true) -- import code into global
foo1 = Foo_makeFoo() -- lua doesnt yet support static fns properly
assert(Foo_fooCount() == 1) -- lua doesnt yet support static fns properly
foo2 = foo1:makeMore()
assert(Foo_fooCount() == 2)
foo1 = nil
collectgarbage()
assert(Foo_fooCount() == 1)
foo2 = nil
collectgarbage()
assert(Foo_fooCount() == 0)

View file

@ -0,0 +1,16 @@
require("import") -- the import fn
import("newobject2",true) -- import code into global
foo1 = makeFoo() -- lua doesnt yet support static fns properly
assert(fooCount() == 1) -- lua doesnt yet support static fns properly
foo2 = makeFoo()
assert(fooCount() == 2)
foo1 = nil
collectgarbage()
assert(fooCount() == 1)
foo2 = nil
collectgarbage()
assert(fooCount() == 0)

View file

@ -0,0 +1,142 @@
-- demo of lua swig capacilities (operator overloading)
require("import") -- the import fn
import("operator_overload",true) -- import lib into global
-- catching undefined variables
setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
-- test routine:
a=Op()
b=Op(5)
c=Op(b) -- copy construct
d=Op(2)
-- test equality
assert(a~=b)
assert(b==c)
assert(a~=d)
-- test <
assert(a<b)
assert(a<=b)
assert(b<=c)
assert(b>=c)
assert(b>d)
assert(b>=d)
-- lua does not support += operators: skiping
-- test +
f=Op(1)
g=Op(1)
assert(f+g==Op(2))
assert(f-g==Op(0))
assert(f*g==Op(1))
assert(f/g==Op(1))
--assert(f%g==Op(0)) -- lua does not support %
-- test unary operators
--assert((not a)==true) -- lua does not allow overloading for not operator
--assert((not b)==false) -- lua does not allow overloading for not operator
assert(-a==a)
assert(-b==Op(-5))
-- test []
h=Op(3)
assert(h[0]==3)
assert(h[1]==0)
h[0]=2 -- set
assert(h[0]==2)
h[1]=2 -- ignored
assert(h[0]==2)
assert(h[1]==0)
-- test ()
i=Op(3)
assert(i()==3)
assert(i(1)==4)
assert(i(1,2)==6)
-- plus add some code to check the __str__ fn
assert(tostring(Op(1))=="Op(1)")
assert(tostring(Op(-3))=="Op(-3)")
--[[
/* Sample test code in C++
#include <assert.h>
#include <stdio.h>
int main(int argc,char** argv)
{
// test routine:
Op a;
Op b=5;
Op c=b; // copy construct
Op d=2;
// test equality
assert(a!=b);
assert(b==c);
assert(a!=d);
// test <
assert(a<b);
assert(a<=b);
assert(b<=c);
assert(b>=c);
assert(b>d);
assert(b>=d);
// test +=
Op e=3;
e+=d;
assert(e==b);
e-=c;
assert(e==a);
e=Op(1);
e*=b;
assert(e==c);
e/=d;
assert(e==d);
e%=c;
assert(e==d);
// test +
Op f(1),g(1);
assert(f+g==Op(2));
assert(f-g==Op(0));
assert(f*g==Op(1));
assert(f/g==Op(1));
assert(f%g==Op(0));
// test unary operators
assert(!a==true);
assert(!b==false);
assert(-a==a);
assert(-b==Op(-5));
// test []
Op h=3;
assert(h[0]==3);
assert(h[1]==0);
h[0]=2; // set
assert(h[0]==2);
h[1]=2; // ignored
assert(h[0]==2);
assert(h[1]==0);
// test ()
Op i=3;
assert(i()==3);
assert(i(1)==4);
assert(i(1,2)==6);
// plus add some code to check the __str__ fn
//assert(str(Op(1))=="Op(1)");
//assert(str(Op(-3))=="Op(-3)");
printf("ok\n");
}
*/
]]

View file

@ -0,0 +1,31 @@
require("import") -- the import fn
import("primitive_ref",true) -- import code into global namespace
assert(ref_int(3)==3)
assert(ref_uint(3) == 3)
assert(ref_short(3) == 3)
assert(ref_ushort(3) == 3)
assert(ref_long(3) == 3)
assert(ref_ulong(3) == 3)
assert(ref_schar(3) == 3)
assert(ref_uchar(3) == 3)
assert(ref_float(3.5) == 3.5)
assert(ref_double(3.5) == 3.5)
assert(ref_bool(true) == true)
assert(ref_char('x') == 'x')
assert(ref_over(0) == 0)
a=A(12)
assert(ref_over(a)==12)

View file

@ -0,0 +1,9 @@
require("import") -- the import fn
import("ret_by_value") -- import code
-- catch "undefined" global variables
setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
a = ret_by_value.get_test()
assert(a.myInt == 100)
assert(a.myShort == 200)