lua-users home
lua-l archive

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


Ben Kelly <ranavin@gmail.com> wrote:
>
> Personally, what I'll miss most is DSL creation. The ability to swap
> around the caller's environment silently makes it easy to create
> "keyword-like" functions that hugely simplify the structure of
> configuration and game-behaviour files. Having to tack _ENV in front of
> each of those is not only very ugly, it confuses the modders.
>
> Yeah, I can use debug.setupvalue(), but now I'm using the debug library
> in release code, which last I checked was considered extremely
> inappropriate under any circumstances.

The way to do this is to wrap the script like this

	script, script_setenv = load(
		[[ return function (...) ]]
		.. source ..
		[[ end, function (env) _ENV = env end ]]
		)()

Then your special keyword-like functions can call the relevant
script_setenv function instead of setfenv. You can fake up a replacement
setfenv like this

	script_env_setter[script] = script_setenv

	function setscriptenv(script,env)
		script_env_setters[script](env)
	end

Tony.
-- 
f.anthony.n.finch  <dot@dotat.at>  http://dotat.at/
Shannon: Southwest 5 to 7, increasing gale 8 at times. Mainly rough.
Occasional rain. Moderate or good, occasionally poor.