lua-users home
lua-l archive

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


Lol.  You're on exactly the same train of thought that I had.  If you
see my original post, you will see that to try and get around that I
wrote this function:

function Require(pack)
        require(pack)
        local t = {}
        local mt = { __index = function(t,k) return
package.loaded[pack][k] end }
        setmetatable(t, mt)
        return t
End

Therefore whenever I wanted to require a package, I did:

utils = Require("utils") 

This would then always directly reference the table package.loaded.utils

However, this didn't work as I expected in some cases.  The only reason
that I could think of is that each state had their own version of
package, which is why I sent my original email, just to verify that I
wasn't going mad.  (Which I may well be)

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Nick Gammon
Sent: 09 August 2007 23:21
To: Lua list
Subject: Re: "Require" in a sandboxed environment


On 02/08/2007, at 5:48 PM, Tom Miles wrote:

> code from environment 1:
>
> utils = require "Utilities"
>
> utils.g_var = 1
> print(utils.g_var)
>
> -----------------------------------------------------
>
> code from environment 2:
>
> utils = require "Utilities"
>
> print(utils.g_var)

> In the lua code example given, env 1 and env 2 would both inherit a 
> common host environment, which itself is a child of the main state.
> i.e.
>
> main->host->env 1
>                      ->env 2


OK, here is another guess. :)

env1 and env2 both have their own states with a metatable entry for
__index for Lua globals. However under that setup "utils" is a local
variable to both env1 and env2. That is, they both have their own
"utils" variable. Thus you are not sharing "utils", and an attempt to
print(utils.g_var) will not reference the other "utils".

What *may* work is to do this at the global state (that is, not in a
sub-environment):

utils = require "Utilities"

This establishes utils at global scope.

- Nick
 








DISCLAIMER 
This email is sent by The Creative Assembly Limited company No. 03425917, registered in England &Wales registered office 27 Great West Road, Middlesex, TW8 9BW, England. The contents of this e-mail and any attachments are confidential to the intended recipient and may also be legally privileged. Unless you are the named addressee (or authorised to receive for the addressee) of this email you may not copy, disclose or distribute it to anyone else. If you have received this email in error, please notify us immediately by e-mail on postmaster@creative-assembly.co.uk and then delete the email and any copies. The Creative Assembly Limited have made all reasonable efforts to ensure that this e-mail and any attached documents or software are free from software viruses, but it is the recipient's responsibility to confirm this.