import source from 1.3.40
[external/swig.git] / Examples / lua / import.lua
1 -- import
2 -- the lua 5.0 loading mechanism is rather poor & relies upon the loadlib() fn
3 -- the lua 5.1 loading mechanism is simplicity itself
4 -- for now we need a bridge which will use the correct verion
5
6 function import_5_0(module)
7         -- imports the file into the program
8         -- for a module 'example'
9         -- this must load 'example.dll' or 'example.so'
10         -- and look for the fn 'Example_Init()' (note the capitalisation)
11         if rawget(_G,module)~=nil then return end -- module appears to be loaded
12                 
13         -- capitialising the first letter
14         local c=string.upper(string.sub(module,1,1))
15         local fnname=c..string.sub(module,2).."_Init"
16         
17         local suffix,lib
18         -- note: as there seems to be no way in lua to determine the platform
19         -- we will try loading all possible names
20         -- providing one works, we can load
21         for _,suffix in pairs{".dll",".so"} do
22                 lib=loadlib(module..suffix,fnname)
23                 if lib then -- found
24                         break
25                 end
26         end
27         assert(lib,"error loading module:"..module)
28         
29         lib() -- execute the function: initalising the lib
30         local m=rawget(_G,module)       -- gets the module object
31         assert(m~=nil,"no module table found")
32 end
33
34 function import_5_1(module)
35         require(module)
36 end
37
38 if string.sub(_VERSION,1,7)=='Lua 5.0' then
39         import=import_5_0
40 else
41         import=import_5_1
42 end