lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


D Burgess wrote:
I guess if require passed as an argument to the Lua module the directory where it was found, then the Lua stub could easily load the .dll from the same directory.

Well you are in luck.

Try,

table.foreach(debug.getregistry(),print)

you will see stuff like

LOADLIB: .\mypackage.dll userdata: 0032BC88

This is the path where the package was found.

But this is done only when loading C modules. And I can't have the C module load the Lua implementation, because the default loading order is Lua->C - if I had MyLib.lua and MyLib.dll in the same directory, MyLib.lua would be loaded first :(

Is there a simple solution to this problem?
Add you own loader to package.loaders or change the order
of the loaders.

If I write a library intended for common use, I wouldn't want to mess with the default loading order.

It's a pity that I had to examine Lua's source to see that the filename of the loaded Lua module is stored as debug info of the module function... somehow I didn't think about debug.getinfo
This is what I do now in the beginning of MyLib.lua:

local function loadCmodule( modname, clibext, openFunc )
	local path = debug.getinfo( 2, "S" ).source
	modname = modname or string.gmatch( path,"([^\\/]+)\.lua$")()
	assert( modname, "Could not extract module name from path" )
	path = string.sub( path, 2, - #(modname .. ".lua") - 1 )
	clibext = clibext or ".dll"
	openFunc = openFunc or "luaopen_" .. modname
	modname = modname .. clibext
	local mod = package.loadlib( path .. modname, openFunc )
	mod = mod or package.loadlib( path .. "Release\\" .. modname, openFunc )
	mod = mod or package.loadlib( path .. "Debug\\" .. modname, openFunc )
	return assert( mod, "C module " .. modname .. " not found" )()
end

local cmod = loadCmodule()

... add Lua interface here

The above function loadCmodule() (or some improved version of it) seems like a good candidate for a common utils library.

I'd be interested to see how other libraries which have a Lua stub on top of a C module deal with this issue.

Regards,
Ivan