Binding modules loaded by "require"?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Binding modules loaded by "require"?

Kristoffer Danielsson
If I load a module inside my luabind-controlled lua-script:
require "path/mylib"
 
...then I want to bind my C++ functions to "mylib.lua". How do I do that?
 
As it is now, the library does not know of my C++ classes/functions, which basically makes modularization worthless.
 
Thanks!


Yes! Windows 7 gör din dator bättre och enklare Yes! Windows 7 gör din dator bättre och enklare
------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Binding modules loaded by "require"?

Jason McKesson
On 12/16/2009 2:55 PM, Kristoffer Danielsson wrote:
If I load a module inside my luabind-controlled lua-script:
require "path/mylib"
 
...then I want to bind my C++ functions to "mylib.lua". How do I do that?
 
As it is now, the library does not know of my C++ classes/functions, which basically makes modularization worthless.
 
Thanks!
The Lua manual explains the functioning of require quite well. You can find what you're looking for here: http://www.lua.org/manual/5.1/manual.html#5.3

Basically, what you want is to register a loading function with Lua. Just use Luabind to register a function, then place this function in the "package.preload" table under the entry of whatever you want the module to be named. So if you want "path/mylib" to be the name of the module, you would put the loader function in "package.preload["path/mylib"]".

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Binding modules loaded by "require"?

Kristoffer Danielsson
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!
 

Date: Wed, 16 Dec 2009 20:16:20 -0800
From: [hidden email]
To: [hidden email]
Subject: Re: [luabind] Binding modules loaded by "require"?

On 12/16/2009 2:55 PM, Kristoffer Danielsson wrote:
If I load a module inside my luabind-controlled lua-script:
require "path/mylib"
 
...then I want to bind my C++ functions to "mylib.lua". How do I do that?
 
As it is now, the library does not know of my C++ classes/functions, which basically makes modularization worthless.
 
Thanks!
The Lua manual explains the functioning of require quite well. You can find what you're looking for here: http://www.lua.org/manual/5.1/manual.html#5.3

Basically, what you want is to register a loading function with Lua. Just use Luabind to register a function, then place this function in the "package.preload" table under the entry of whatever you want the module to be named. So if you want "path/mylib" to be the name of the module, you would put the loader function in "package.preload["path/mylib"]".


Ladda ner nya Internet Explorer 8! Ladda ner nya Internet Explorer 8!
------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Binding modules loaded by "require"?

Jason McKesson
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.



------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Binding modules loaded by "require"?

Kristoffer Danielsson
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: [hidden email]
To: [hidden email]
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.




Hitta kärleken! Klicka här Hitta en dator som passar dig!
------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user