[英]How do I set, via the lua C API, the environment table for a chunk of lua code prior to running it?
The interface for my game engine is built using a markup language and Lua, similar to HTML and javascript. 我的游戏引擎界面是使用标记语言和Lua(类似于HTML和javascript)构建的。 As such, visual elements will have handlers for UI events such as a mouse move or click, and each time a handler is to be run, the engine will check if it is compiled yet and if not will compile it via luaL_loadstring
. 因此,可视元素将具有UI事件的处理程序,例如鼠标移动或单击,并且每次运行处理程序时,引擎将检查它是否已编译,如果不是,则将通过luaL_loadstring
编译它。 Handers can be shared either by element duplication or assignment ( this.onclick = that.onclick
). 可以通过元素复制或赋值来共享this.onclick = that.onclick
( this.onclick = that.onclick
)。
How do I set the environment of a chunk of lua code before running it? 在运行之前,如何设置大量lua代码的环境? The idea is to make element- and event-specific data available to the chunk, and also to link to the environment of the parent UI element. 我们的想法是将元素和特定于事件的数据提供给块,并且还链接到父UI元素的环境。 Lua 5.2 changes removed lua_setfenv
, so I am not sure how to accomplish this. Lua 5.2的更改删除了lua_setfenv
,所以我不确定如何完成此操作。 The function lua_load
allows specifying an environment, but seems to only be used for loading code and not running it. 函数lua_load
允许指定一个环境,但似乎只用于加载代码而不运行它。
From the reference manual : 从参考手册 :
You can use load (or loadfile) to load a chunk with a different environment. 您可以使用load(或loadfile)来加载具有不同环境的块。 (In C, you have to load the chunk and then change the value of its first upvalue.) (在C中,您必须加载块,然后更改其第一个upvalue的值。)
Setting upvalues is done with lua_setupvalue
. 设置upvalues与做lua_setupvalue
。 So, load your code first, then push the new environment and call lua_setupvalue
the same way you would have called lua_setfenv
before: 因此,首先加载您的代码,然后推送新环境并以与之前调用lua_setupvalue
相同的方式调用lua_setfenv
:
luaL_loadfile(L, "file.lua"); /* load and compile handler */
lua_getglobal(L, "my_environment"); /* push environment onto stack */
lua_setupvalue(L, -2, 1); /* pop environment and assign to upvalue#1 */
/* any other setup needed */
lua_pcall(L, ...); /* call handler */
Also, from the end of your question: 另外,从您的问题的结尾:
The function
lua_load
allows specifying an environment, but seems to only be used for loading code and not running it. 函数lua_load
允许指定一个环境,但似乎只用于加载代码而不运行它。
This is not actually the case; 事实并非如此; load
(the Lua function) lets you specify an environment, but lua_load
(the C function) does not. load
(Lua函数)允许您指定环境,而lua_load
(C函数)则不能。
Also, while it is "only used for loading code, and not running it", this is the same as luaL_loadstring
- indeed, luaL_loadstring
is just a wrapper around it. 另外,虽然它“仅用于加载代码,而不用于运行代码”,但这与luaL_loadstring
相同-实际上, luaL_loadstring
只是它的包装。 lua_load
is a lower-level API function that can be used to implement custom loaders (for example, loading code over the network, or from a compressed file). lua_load
是一个较低级别的API函数,可用于实现自定义加载器(例如,通过网络或压缩文件加载代码)。 So if you're already comfortable loading code with luaL_loadstring
before running it with lua_pcall
, lua_load
should look familiar. 因此,如果在使用lua_pcall
运行代码之前已经习惯使用luaL_loadstring
加载代码,则lua_load
应该看起来很熟悉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.