Lua improvements - Mark Gossage patch #1295168

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@7472 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2005-09-20 20:05:06 +00:00
commit 655f282468
9 changed files with 72 additions and 57 deletions

View file

@ -869,7 +869,7 @@ csharp_clean:
# lua flags
LUA_INCLUDE= @LUAINCLUDE@
LUA_LIB = @LUALIB@ -llua -llualib
LUA_LIB = @LUALIB@ @LUALIBRARY@
# Extra specific dynamic linking options
LUA_DLNK = @LUADYNAMICLINKING@

View file

@ -1,5 +1,5 @@
require("import") -- the import fn
import("enums",false) -- import lib
import("enums") -- import lib
-- catch "undefined" global variables
setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})

View file

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

View file

@ -1,28 +1,42 @@
-- import
function import(module,intoglobal)
-- the lua 5.0 loading mechanism is rather poor & relies upon the loadlib() fn
-- the lua 5.1 loading mechanism is simplicity itself
-- for now we need a bridge which will use the correct verion
function import_5_0(module)
-- imports the file into the program
-- for a module example
-- for a module 'example'
-- this must load 'example.dll' or 'example.so'
-- and look for the fn 'Example_Init()' (note the capitalisation)
if rawget(_G,module)~=nil then return end -- module appears to be loaded
-- capitialising the first letter
local c=string.upper(string.sub(module,1,1))
local fnname=c..string.sub(module,2).."_Init"
-- attempt to load the module using Unix and Windows dll/shared object
local loadlibrary = loadlib(module..".so",fnname)
if loadlibrary == nil then
loadlibrary = loadlib(module..".dll",fnname)
end
assert(loadlibrary~=nil,"could not load module")
loadlibrary()
-- 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
local suffix,lib
-- note: as there seems to be no way in lua to determine the platform
-- we will try loading all possible names
-- providing one works, we can load
for _,suffix in pairs{".dll",".so"} do
lib=loadlib(module..suffix,fnname)
if lib then -- found
break
end
end
assert(lib,"error loading module:"..module)
lib() -- execute the function: initalising the lib
local m=rawget(_G,module) -- gets the module object
assert(m~=nil,"no module table found")
end
function import_5_1(module)
require(module)
end
if string.sub(_VERSION,1,7)=='Lua 5.0' then
import=import_5_0
else
import=import_5_1
end

View file

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

View file

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

View file

@ -1,6 +1,8 @@
-- demo of lua swig capacilities (operator overloading)
require("import") -- the import fn
import("operator_overload",true) -- import lib into global
import("operator_overload") -- import lib
for k,v in pairs(operator_overload) do _G[k]=v end -- move to global
-- catching undefined variables
setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})

View file

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

View file

@ -1,9 +1,6 @@
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)