LUAÇ问题 [英] Lua C question

查看:116
本文介绍了LUAÇ问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑2:

**,我想有一个详细的解释。我是一个NOOB C语言。

**I would like to have a detailed explanation. I'm a NOOB to the C language.

我将如何使用Lua的源$ C ​​$ C创建一个Lua间preTER将执行给定的Lua code块? Lua的code的块将被送到一个字符

How would I use the Lua source code to create a Lua interpreter that will execute given blocks of Lua code? The blocks of Lua code would be sent as a char.

推荐答案

您需要 lua_load 调用编译code块,然后调用到 lua_call 来运行它。对于如何做到这一点一个很好的例子,来看看这里所提供的例如:

you need a call to lua_load to compile the block of code, and then a call to lua_call to run it. For a really good example of how this is done, take a look at the example provided here:.

的第一个参数的Lua任何API函数始终是一个跨preTER状态,这是 lua_open()

The first argument to any Lua api function is always an interpreter state, which is the return value of lua_open()

这个例子实际上使用 luaL_loadbuffer 它包装调用 lua_load 来使编译C字符串更容易一点。你可以了解如何参考手册,涵盖了的辅助的章节中使用它图书馆。这使得一个Lua的的在LUA堆栈的顶部,然后可以用 lua_call 调用,但示例使用 lua_pcall ,它提供了一个误码诱捕。因为你刚刚编译块不带任何参数(这是一个块不是一个函数),并没有你感兴趣的任何返回值,并且要准确地看到错误,因为它是生产的,所有的除了第一个参数(这始终是LUA间preTER状态)可以为零。

The example actually uses luaL_loadbuffer which wraps the call to lua_load to make compiling a c string a bit easier. 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. 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Ç问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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