lua-users home
lua-l archive

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


On Tue, May 12, 2015 at 7:16 PM, Jonathan Goble <jcgoble3@gmail.com> wrote:
> On Tue, May 12, 2015 at 9:42 PM, Josh Haberman <jhaberman@gmail.com> wrote:
>> Since package.loadlib bypasses require(), it seems like this approach
>> will involve re-implementing the require() logic? Stuff like checking
>> package.preload, splitting LUA_CPATH on ";", looking in each
>> directory, etc?
>
> Yes, you'd have to check package.preload, but if not found there,
> `package.searchpath(libname, package.cpath)` ought to return the
> filename for passing to package.loadlib.

I tried this out and it seems to work!

One caveat: require() will load the module RTLD_LOCAL. On OS X at
least, it appears that once you load a module as RTLD_LOCAL,
subsequent loads as RTLD_GLOBAL don't appear to have any effect.

So ultimately my a.lua looks like this:

-- Ensure the library is loaded as RTLD_GLOBAL.
package.loadlib(package.searchpath("a_cext", package.cpath), "*")

-- Let require load the module in the normal way.
require "a_cext"

Turning these two statements around makes it not work anymore! This
also means I probably need to do a bit more work to give a nicer error
message if the first load fails (for example, if LUA_CPATH was set
incorrectly).