Re: Binding modules loaded by "require"?
Jason McKesson <[email protected]>
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
On 12/17/2009 5:22 AM, Kristoffer Danielsson wrote: > Thanks for your reply. > > I've read the documentation, but I don't quite follow. > > If I load "C:\MyScript.lua" and do my luabind stuff: > > luabind::open(L); > bind "foo" etc... > > And then, inside this MyScript.lua, I want to get access to another > library: > > require 'extra.mylib' -- Loads C:\extra\mylib.lua... > > Now, inside mylib.lua I want to access my C++ function "foo". You > mention "preload", but where should I initialize it - and how? > > Thanks again! The documentation under "require" tells you exactly why "require 'extra.mylib'" causes "C:\extra\mylib.lua" to be loaded. This happens exactly and only because there is an entry in the "package.loaders" array that tells it to convert "extra.mylib" into a Lua filename "\extra\mylib.lua". The package system, when you initialized it, causes this to happen. Specifically, the documentation under "package.loaders" explains how it works: Each entry in this table is a /searcher function/. When looking for a module, |require| <http://www.lua.org/manual/5.1/manual.html#pdf-require> calls each of these searchers in ascending order, with the module name (the argument given to |require| <http://www.lua.org/manual/5.1/manual.html#pdf-require>) as its sole parameter. The function can return another function (the module /loader/) or a string explaining why it did not find that module (or *nil* if it has nothing to say). Lua initializes this table with four functions. Therefore, all "require" does is call functions in "package.loaders". So, if you want to provide a hook into the package system to be able to create your own C/C++ packages, you simply modify this array. Add a C/C++ function into the array that takes a string. This function will be called when a user uses "require", as long as none of the earlier loaders. The initial form of "package.loaders" looks like this: package.loaders[1] = function(moduleName) if(package.preload[moduleName] && type(package.preload[moduleName]) == "function") then return package.preload[moduleName] else return nil end end package.loaders[2] = function(moduleName) --[[ Search package.path for a Lua file with moduleName. ]] end package.loaders[3] = function(moduleName) --[[ Search package.cpath for a DLL/SO file with moduleName. ]] end package.loaders[4] = function(moduleName) --[[ Search package.cpath for a DLL/SO file with moduleName's root name. ]] end You can add/change one of these functions, or you can simply add an entry in "package.preload[moduleName]" for your module's name. ------------------------------------------------------------------------------ This SF.Net email is sponsored by the Verizon Developer Community Take advantage of Verizon's best-in-class app development support A streamlined, 14 day to market process makes app distribution fast and easy Join now and get one step closer to millions of Verizon customers http://p.sf.net/sfu/verizon-dev2dev _______________________________________________ luabind-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/luabind-user