Re: Binding modules loaded by "require"?

Kristoffer Danielsson <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
Hi,

 

Thanks for your clarification. It makes perfect sense now.

 

I ended up creating a "lua sandbox", where I explicitly set entries in the package.loaders array. Regarding the "namespace" of loaded libraries, it turned out to be as easy as this:

 

module(..., package.seeall)

 

Your solution requires a bit more work, but perhaps it's better (or more elegant?). I don't know.

 

Thanks again!

 


Date: Sat, 19 Dec 2009 13:57:23 -0800
From: [email protected]
To: [email protected]
Subject: Re: [luabind] Binding modules loaded by "require"?

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 calls each of these searchers in ascending order, with the module name (the argument given to 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.


 		 	   		  
_________________________________________________________________
Nya Windows 7 gör allt lite enklare. Hitta en dator som passar dig!
http://windows.microsoft.com/shop

------------------------------------------------------------------------------
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.