Lua improvements - Mark Gossage patch #1295168
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7472 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
d0381c8141
commit
76d58c55cb
9 changed files with 72 additions and 57 deletions
|
|
@ -869,7 +869,7 @@ csharp_clean:
|
||||||
|
|
||||||
# lua flags
|
# lua flags
|
||||||
LUA_INCLUDE= @LUAINCLUDE@
|
LUA_INCLUDE= @LUAINCLUDE@
|
||||||
LUA_LIB = @LUALIB@ -llua -llualib
|
LUA_LIB = @LUALIB@ @LUALIBRARY@
|
||||||
|
|
||||||
# Extra specific dynamic linking options
|
# Extra specific dynamic linking options
|
||||||
LUA_DLNK = @LUADYNAMICLINKING@
|
LUA_DLNK = @LUADYNAMICLINKING@
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
require("import") -- the import fn
|
require("import") -- the import fn
|
||||||
import("enums",false) -- import lib
|
import("enums") -- import lib
|
||||||
|
|
||||||
-- catch "undefined" global variables
|
-- catch "undefined" global variables
|
||||||
setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
|
setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,25 @@
|
||||||
-- demo of lua swig capacilities (operator overloading)
|
-- demo of lua swig capacilities (operator overloading)
|
||||||
require("import") -- the import fn
|
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
|
-- catching undefined variables
|
||||||
setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
|
setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
|
||||||
|
|
||||||
a = A()
|
a = eo.A()
|
||||||
|
|
||||||
function try1()
|
function try1()
|
||||||
a:foo()
|
a:foo()
|
||||||
end
|
end
|
||||||
|
|
||||||
ok,ex=pcall(try1)
|
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()
|
function try2()
|
||||||
a:bar()
|
a:bar()
|
||||||
end
|
end
|
||||||
ok,ex=pcall(try2)
|
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()
|
function try3()
|
||||||
a:foobar()
|
a:foobar()
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,42 @@
|
||||||
-- import
|
-- 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
|
-- imports the file into the program
|
||||||
-- for a module example
|
-- for a module 'example'
|
||||||
-- this must load 'example.dll' or 'example.so'
|
-- this must load 'example.dll' or 'example.so'
|
||||||
-- and look for the fn 'Example_Init()' (note the capitalisation)
|
-- 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
|
-- capitialising the first letter
|
||||||
local c=string.upper(string.sub(module,1,1))
|
local c=string.upper(string.sub(module,1,1))
|
||||||
local fnname=c..string.sub(module,2).."_Init"
|
local fnname=c..string.sub(module,2).."_Init"
|
||||||
|
|
||||||
-- attempt to load the module using Unix and Windows dll/shared object
|
local suffix,lib
|
||||||
local loadlibrary = loadlib(module..".so",fnname)
|
-- note: as there seems to be no way in lua to determine the platform
|
||||||
if loadlibrary == nil then
|
-- we will try loading all possible names
|
||||||
loadlibrary = loadlib(module..".dll",fnname)
|
-- providing one works, we can load
|
||||||
end
|
for _,suffix in pairs{".dll",".so"} do
|
||||||
assert(loadlibrary~=nil,"could not load module")
|
lib=loadlib(module..suffix,fnname)
|
||||||
loadlibrary()
|
if lib then -- found
|
||||||
|
break
|
||||||
-- moving to global namespace
|
end
|
||||||
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
|
||||||
|
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
|
end
|
||||||
|
|
@ -1,16 +1,16 @@
|
||||||
require("import") -- the import fn
|
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
|
foo1 = newobject1.Foo_makeFoo() -- lua doesnt yet support static fns properly
|
||||||
assert(Foo_fooCount() == 1) -- lua doesnt yet support static fns properly
|
assert(newobject1.Foo_fooCount() == 1) -- lua doesnt yet support static fns properly
|
||||||
|
|
||||||
foo2 = foo1:makeMore()
|
foo2 = foo1:makeMore()
|
||||||
assert(Foo_fooCount() == 2)
|
assert(newobject1.Foo_fooCount() == 2)
|
||||||
|
|
||||||
foo1 = nil
|
foo1 = nil
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
assert(Foo_fooCount() == 1)
|
assert(newobject1.Foo_fooCount() == 1)
|
||||||
|
|
||||||
foo2 = nil
|
foo2 = nil
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
assert(Foo_fooCount() == 0)
|
assert(newobject1.Foo_fooCount() == 0)
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,16 @@
|
||||||
require("import") -- the import fn
|
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
|
foo1 = newobject2.makeFoo() -- lua doesnt yet support static fns properly
|
||||||
assert(fooCount() == 1) -- lua doesnt yet support static fns properly
|
assert(newobject2.fooCount() == 1) -- lua doesnt yet support static fns properly
|
||||||
|
|
||||||
foo2 = makeFoo()
|
foo2 = newobject2.makeFoo()
|
||||||
assert(fooCount() == 2)
|
assert(newobject2.fooCount() == 2)
|
||||||
|
|
||||||
foo1 = nil
|
foo1 = nil
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
assert(fooCount() == 1)
|
assert(newobject2.fooCount() == 1)
|
||||||
|
|
||||||
foo2 = nil
|
foo2 = nil
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
assert(fooCount() == 0)
|
assert(newobject2.fooCount() == 0)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
-- demo of lua swig capacilities (operator overloading)
|
-- demo of lua swig capacilities (operator overloading)
|
||||||
require("import") -- the import fn
|
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
|
-- catching undefined variables
|
||||||
setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
|
setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,32 @@
|
||||||
require("import") -- the import fn
|
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)
|
a=pr.A(12)
|
||||||
assert(ref_over(a)==12)
|
assert(pr.ref_over(a)==12)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,6 @@
|
||||||
require("import") -- the import fn
|
require("import") -- the import fn
|
||||||
import("ret_by_value") -- import code
|
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()
|
a = ret_by_value.get_test()
|
||||||
assert(a.myInt == 100)
|
assert(a.myInt == 100)
|
||||||
assert(a.myShort == 200)
|
assert(a.myShort == 200)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue