简体   繁体   English

Lua C:我将如何使用 Lua 源代码创建一个 Lua 解释器来执行给定的 Lua 代码块?

[英]Lua C: How would I use the Lua source code to create a Lua interpreter that will execute given blocks of Lua code?

I would like to have a detailed explanation.我想要一个详细的解释。

How would I use the Lua source code to create a Lua interpreter that will execute given blocks of Lua code?我将如何使用 Lua 源代码创建一个 Lua 解释器来执行给定的 Lua 代码块? The blocks of Lua code would be sent as a char . Lua 代码块将作为char发送。

you need a call to lua_load to compile the block of code, and then a call to lua_call to run it.你需要调用lua_load来编译代码块,然后调用lua_call来运行它。 For a really good example of how this is done, take a look at the example provided here: .有关如何完成此操作的非常好的示例,请查看此处提供示例:

The first argument to any Lua api function is always an interpreter state, which is the return value of lua_open()任何 Lua api 函数的第一个参数总是一个解释器状态,它是lua_open()的返回值

The example actually uses luaL_loadbuffer which wraps the call to lua_load to make compiling ac string a bit easier.该示例实际上使用luaL_loadbuffer包装对lua_load的调用,以使编译 ac 字符串更容易一些。 you can read how to use it in the chapter of the reference manual that covers the The Auxiliary Library .您可以在涵盖辅助库的参考手册的章节中阅读如何使用它。 This leaves a lua chunk at the top of the lua stack, which can then be invoked with lua_call , but the example uses lua_pcall , which provides a bit of error trapping.这会在 lua 堆栈的顶部留下一个 lua,然后可以使用lua_call调用lua_call ,但该示例使用lua_pcall ,它提供了一些错误捕获。 since the chunk you just compiled doesn't take any arguments (it's a chunk not a function) and doesn't have any return value you'd be interested in, and you want to see the error exactly as it was produced, all of the arguments besides the first (which is always the lua interpreter state) can be zeros.由于您刚刚编译的块不接受任何参数(它是一个块而不是函数)并且没有您感兴趣的任何返回值,并且您希望完全按照产生的错误查看错误,所有除了第一个(始终是 lua 解释器状态)之外的参数可以为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM