lua-users home
lua-l archive

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


Tomas Guisasola Gorham <tomas@tecgraf.puc-rio.br> wrote:
> 	I mean:
> 
> == A.lua ==
> module"A"
> g = 10
> function f (x) return x+g end
> 
> == B.lua ==
> local A = require"A"
> module"B"
> g = 20
> import_f_from_A
> 
> 	What will be the result of B.f(2) ?

Did you try?

Simulating a Lua VM I would say that B.f() calls A.f() which has a
hard-coded reference to its "local" global g. It doesn't know the first
thing about B's environment and its g (that's assuming your opaque
import_f_from_A call hasn't inadvertently mixed up environments).

So this call returns 12. If it doesn't either Lua or I am broken.

> > > 	But `require' only passes its first argument to the script :-(
> > 
> > Yeah, that's one of my pet peeves as well. It would be a really great
> > addition if ll_require() would push further arguments before it calls
> > the loaded module. ll_module() could then simply access these as further
> > parameters. This should even work with C libraries.
> > 
> > Alas, doing this would break most existing module code.
> 	Why?

I can't see how the current module system would cleanly refer to more
than one parameter, given that module()'s list of optional parameters
(after the name) is a list of functions to be applied over the module.

Modules are strange hybrids: module() is a function call in its own
right but a module is also, as it were, on the receiving end of a call.
Try this:

-- in a.lua
local g=_G
print(...)
module(...)
g.print(_M._NAME)

Next copy a.lua to b.lua and then run this file:

-- test.lua
require("a")
require("b")

This feature allows a certain degree of hacking.

(Small aside: I've just hacked the sources to try multiple parameters
for require(). It's possible to change ll_require() such that it pushes
more parameters and ll_module() such that a module accepts them in (...)
without crashing lua. However, I don't know enough about the inner
workings of those functions (and their support functions) to be sure
what goes on. Time permitting, I will investigate.)

-- 
cheers  thomasl

web : http://thomaslauer.com/start