lua-users home
lua-l archive

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

It was thus said that the Great Dibyendu Majumdar once stated:
> Hi,
> I was wondering if anyone would be able to help me add some Lua 5.1
> compatibility features to Lua 5.3 in my Ravi-Distro package.
> I include Lua 5.3.5 in the distro with these enhancements:
> * The LuaJIT bit library is included
> * All available 5.1 and 5.2 compatibility options are enabled,
> These compatibility options enable Lua 5.3 to run LuaJIT's dynasm tool
> for instance.
> However I would like to improve backwards compatibility with Lua 5.1
> even further; in particular would like to add support for fenv(). But
> I have never used Lua 5.1, and despite good intentions, haven't been
> able to work on this aspect. I would very much appreciate any help.

  It may be possible.  setfenv() is used to set an environment table to
either a function or userdata.  If it's a function, this table become the
global environment for the function; for a userdata, it's similar to
setuservalue().  Given that Lua 5.3, section 3.3.2 states:

	As such, chunks can define local variables, receive arguments, and
	return values. Moreover, such anonymous function is compiled as in
	the scope of an external local variable called _ENV (see §2.2). The
	resulting function always has _ENV as its only upvalue, even if it
	does not use that variable.

  So any code loaded via load() (or similar) will hvae _ENV as the first
upvalue, you can use setupvalue() to switch out the _ENV value currently
set.  Arbitrary functions might be a bit harder to locate the proper _ENV to
swap (if not outright impossible in some cases).

  setfenv() at least, the Lua function, not the C function, can also modify
a function in the call stack.  Per the description:

	Sets the environment to be used by the given function. f can be a
	Lua function or a number that specifies the function at that stack
	level: Level 1 is the function calling setfenv. setfenv returns the
	given function.

	As a special case, when f is 0 setfenv changes the environment of
	the running thread. In this case, setfenv returns no values.

  -spc (As always, the devil is in the details ... )