且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

修改Lua Chunk环境:Lua 5.2

更新时间:2023-02-21 09:36:00

允许块在不同环境中运行的最简单方法是使其明确并使其接受环境.将这一行添加到代码块的顶部即可达到以下目的:

The simplest way to allow a chunk to be run in different environments is to make this explicit and have it receive an environment. Adding this line at the top of the chunk achieves this:

_ENV=...

现在,您可以根据需要拨打chunk(env1)和更高版本的chunk(env2)了.

Now you can call chunk(env1) and later chunk(env2) at your pleasure.

在那里,没有debug具有高价值的魔法.

There, no debug magic with upvalues.

尽管可以清楚地知道您的块中是否包含该行,但是您可以在加载时通​​过编写一个合适的读取器函数来添加它,该函数首先发送该行,然后发送文件的内容.

Although it will be clear if your chunk contains that line, you can add it at load time, by writing a suitable reader function that first sends that line and then the contents of the file.