Paul Bleisch | 1 Oct 2000 07:46

"import"?


I would like to reproduce behavior similar to Python's 
"import".  Basically, I'd like to import a lua file
and have everything declared/defined in that file to
appear in the global table named whatever the module
is named.

Specifically, I would like to do something like this
in lua code:

-- somemodule.lua
--
v = 1.0;
t = { a=0, b=1 };

-- somefile.lua
--
import("somemodule");

print "v = " .. somemodule.v;

Any ideas?

Paul

Erik Hougaard | 1 Oct 2000 11:23

Re: "import"?

----- Original Message ----- 
> I would like to reproduce behavior similar to Python's 
> "import".  Basically, I'd like to import a lua file
> and have everything declared/defined in that file to
> appear in the global table named whatever the module
> is named.
> 
> Specifically, I would like to do something like this
> in lua code:
> 
> -- somemodule.lua
> --
> v = 1.0;
> t = { a=0, b=1 };
> 
> -- somefile.lua
> --
> import("somemodule");
> 
> print "v = " .. somemodule.v;
> 

dofile("somemodule.lua")

BTW: You dont need the ; at the end of lines :-)

/Erik

Michael T. Richter | 1 Oct 2000 13:01

Re: "import"?

>> I would like to reproduce behavior similar to Python's
>> "import".  Basically, I'd like to import a lua file
>> and have everything declared/defined in that file to
>> appear in the global table named whatever the module
>> is named.

>> Specifically, I would like to do something like this
>> in lua code:

>> -- somemodule.lua
>> --
>> v = 1.0;
>> t = { a=0, b=1 };

>> -- somefile.lua
>> --
>> import("somemodule");

>> print "v = " .. somemodule.v;

> dofile("somemodule.lua")

That doesn't do what he requested.  This takes everything in
"somemodule.lua" and puts it into the global namespace of the current
module.  He wants to wrap everything in "somemodule.lua" in a table named
"somemodule" to avoid collisions with variables already named.

Roberto Ierusalimschy | 1 Oct 2000 16:15
Picon
Picon

Re: "import"?

Maybe something like

  local newenv = {}
  local oldenv = globals()
  local oldtm = gettagmethod(tag(nil), 'getglobal)
  settagmethod(tag(nil), 'getglobal', function (varname)
    return %oldenv[varname]
  end)  -- alow new environment to use all variables from old one
  globals(newenv)   -- set new environment
  dofile(filename)
  globals(oldenv)   -- restore environment
  settagmethod(tag(nil), 'getglobal', oldtm)
  return newenv

-- Roberto

Michael T. Richter | 1 Oct 2000 17:23

Re: "import"?

>   local newenv = {}
>   local oldenv = globals()
>   local oldtm = gettagmethod(tag(nil), 'getglobal)
>   settagmethod(tag(nil), 'getglobal', function (varname)
>     return %oldenv[varname]
>   end)  -- alow new environment to use all variables from old one
>   globals(newenv)   -- set new environment
>   dofile(filename)
>   globals(oldenv)   -- restore environment
>   settagmethod(tag(nil), 'getglobal', oldtm)
>   return newenv

That's brilliant, Roberto!

Every time I think "now here's something that Lua probably can't do well" I
seem to be proven wrong.  With a couple of very minor tweaks the code you've
written can be turned into a full-fledged Lua module system.

Paul Bleisch | 1 Oct 2000 19:17

RE: "import"?


This appears to require 4.0, no?  I should have put the extra
requirement that I need this in 3.2.

Paul

> -----Original Message-----
> From: Roberto Ierusalimschy [mailto:roberto <at> inf.puc-rio.br]
> Sent: Sunday, October 01, 2000 9:15 AM
> To: Multiple recipients of list
> Subject: Re: "import"? 
> 
> 
> Maybe something like
> 
>   local newenv = {}
>   local oldenv = globals()
>   local oldtm = gettagmethod(tag(nil), 'getglobal)
>   settagmethod(tag(nil), 'getglobal', function (varname)
>     return %oldenv[varname]
>   end)  -- alow new environment to use all variables from old one
>   globals(newenv)   -- set new environment
>   dofile(filename)
>   globals(oldenv)   -- restore environment
>   settagmethod(tag(nil), 'getglobal', oldtm)
>   return newenv
> 
> -- Roberto
> 

(Continue reading)

Mauro Vezzosi | 2 Oct 2000 09:35
Picon
Favicon

lua.c userinit() call in 4.0b

Hi

In Lua 4.0b src/lua/readme and lua.c is written that the userinit() 
function must be defined as void (*)(void), but it should be void (*)
(lua_State *).
In lua.c main() the function call USERINIT() should be USERINIT(L), 
therefore also lua.c userinit() function will change.

- In src/lua/luac.htm is missing the explanation of the option -s 
(strip debug information).
- In doc/manual.htm "8 - Lua Stand-alone" is missing the explanation of 
the option -s (set stack size).
- In doc/manual.htm in the end of "5.10 - Manipulating Tables in Lua" 
is written "creates a new, empty table *and and* pushes it onto the 
stack." :-)

Bye
Mauro

Roberto Ierusalimschy | 2 Oct 2000 14:49
Picon
Picon

Re: "import"?

> This appears to require 4.0, no?

Yes.

>  I should have put the extra requirement that I need this in 3.2.

You can do something similar, but instead of changing the table of globals,
you will have to copy all globals to a new table, and then erase all globals.
It will be slower, but I think it works:

  -- save these functions, as they will be erased from global space
  local foreach, foreachvar, setglobal = foreach, foreachvar, setglobal
  local oldstate = {}
  -- save current globals
  foreachvar(function(n,v) %oldstate[n] = v end)
  foreachvar(function(n) %setglobal(n, nil) end)   -- erase all globals

  ...  -- set tag method and dofile as before

  local newstate = {}
  -- save new globals in this state
  foreachvar(function(n,v) %oldstate[n] = v end)
  foreachvar(function(n) %setglobal(n, nil) end)   -- erase new globals
  -- restore old globals
  foreach(oldstate, function(n,v) %setglobal(n,v) end)

-- Roberto

Picon

Re: "import"?

>>  I should have put the extra requirement that I need this in 3.2.
>
>You can do something similar, but instead of changing the table of globals,
>you will have to copy all globals to a new table, and then erase all globals.

Or you can set up a "setglobal" tag method to redirect global definitions to
a table. Something like

 function import(f)
  local t={}
  local g=settagmethod(tag(nil),"setglobal",function (n,o,v) %t[n]=v end)
  dofile(f..".lua")
  settagmethod(tag(nil),"setglobal",g)
  return t
 end

If your module use (=read) global variables defined in the module, then you
must also set up a "getglobal" tag method to read first from t and then fro
the real globals (with rawgetglobal).
--lhf

Picon

Re: "import"?

>Every time I think "now here's something that Lua probably can't do well" I
>seem to be proven wrong.

That's the power of extensible semantics via meta-mechanisms, a fundamental
concept in the design of Lua: it addresses "difficult" tasks while allowing
the programmer to code her own solution.
--lhf


Gmane