如何在运行lua代码之前通过lua C API设置一个lua代码块的环境表? [英] How do I set, via the lua C API, the environment table for a chunk of lua code prior to running it?

查看:259
本文介绍了如何在运行lua代码之前通过lua C API设置一个lua代码块的环境表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的游戏引擎的界面使用标记语言和Lua构建,类似于HTML和javascript。因此,可视元素将具有用于UI事件的处理程序,诸如鼠标移动或点击,并且每当运行处理程序时,引擎将检查其是否被编译,并且如果不是则将通过 luaL_loadstring 。可以通过元素重复或赋值( this.onclick = that.onclick )共享手柄。



如何我在运行它之前设置了一段lua代码的环境?想法是使元素和事件特定的数据可用于块,并且还链接到父UI元素的环境。 Lua 5.2更改已删除 lua_setfenv ,所以我不知道如何完成这个。 lua_load 允许指定环境,但似乎只用于加载代码而不是运行它。

解决方案

a href =http://www.lua.org/manual/5.2/manual.html#2.2 =nofollow>参考手册:



< blockquote>

您可以使用load(或loadfile)加载具有不同环境的块。 (在C中,您必须加载块,然后更改其第一个上值的值。)


设置值使用< a href =http://www.lua.org/manual/5.2/manual.html#lua_setupvalue =nofollow> lua_setupvalue 。所以,首先加载你的代码,然后推新环境,调用 lua_setupvalue ,就像你之前调用 lua_setfenv

  luaL_loadfile(L,file.lua); / *加载和编译处理程序* / 
lua_getglobal(L,my_environment); / * push环境到栈* /
lua_setupvalue(L,-2,1); / * pop环境并分配给upvalue#1 * /
/ *需要任何其他设置* /
lua_pcall(L,...); / * call handler * /

此外,从你的问题的结尾:


函数 lua_load 允许指定一个环境,但似乎只用于加载代码而不是运行它。


实际上并非如此; load (Lua函数)可以指定一个环境,但 lua_load p>

此外,虽然它只用于加载代码,而不是运行它,这与 luaL_loadstring - 的确, luaL_loadstring 只是一个包装器。 lua_load 是一个较低级别的API函数,可用于实现自定义加载器(例如,通过网络加载代码,或从压缩文件加载代码)。因此,如果您在使用 lua_pcall lua_load运行它之前已经习惯使用 luaL_loadstring 应该看起来很熟悉。


The interface for my game engine is built using a markup language and Lua, similar to HTML and 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. Handers can be shared either by element duplication or assignment (this.onclick = that.onclick).

How do I set the environment of a chunk of lua code before running it? 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. Lua 5.2 changes removed lua_setfenv, so I am not sure how to accomplish this. The function lua_load allows specifying an environment, but seems to only be used for loading code and not running it.

解决方案

From the reference manual:

You can use load (or loadfile) to load a chunk with a different environment. (In C, you have to load the chunk and then change the value of its first upvalue.)

Setting upvalues is done with 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:

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.

This is not actually the case; load (the Lua function) lets you specify an environment, but lua_load (the C function) does not.

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. 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). So if you're already comfortable loading code with luaL_loadstring before running it with lua_pcall, lua_load should look familiar.

这篇关于如何在运行lua代码之前通过lua C API设置一个lua代码块的环境表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆