简单 Lua API 示例

lua-users home
wiki

这是一个简单的入门示例,介绍了如何从 C 语言中与 Lua 进行交互。C 程序加载 Lua 脚本文件,设置一些 Lua 变量,运行 Lua 脚本,并读取返回值。以下示例适用于 Lua 5.3。

首先,创建以下 Lua 脚本文件并将其保存为 "script.lua"。此脚本将把名为 "foo" 的表的內容(将由 C 程序创建)转储到屏幕上,并返回此表中所有元素的总和。

-- script.lua
-- Receives a table, returns the sum of its components.
io.write("The table the script received has:\n");
x = 0
for i = 1, #foo do
  print(i, foo[i])
  x = x + foo[i]
end
io.write("Returning data back to C\n");
return x

为了从 C 语言中访问它,我们可以编写一个简单的程序来构建该表,将其传递给脚本,并获取返回值。创建一个名为 "test.c" 的 C 程序,如下所示

/*
 * test.c
 * Example of a C program that interfaces with Lua.
 * Based on Lua 5.0 code by Pedro Martelletto in November, 2003.
 * Updated to Lua 5.1. David Manura, January 2007.
 */

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>


int
main(void)
{
    int status, result, i;
    double sum;
    lua_State *L;

    /*
     * All Lua contexts are held in this structure. We work with it almost
     * all the time.
     */
    L = luaL_newstate();

    luaL_openlibs(L); /* Load Lua libraries */

    /* Load the file containing the script we are going to run */
    status = luaL_loadfile(L, "script.lua");
    if (status) {
        /* If something went wrong, error message is at the top of */
        /* the stack */
        fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1));
        exit(1);
    }

    /*
     * Ok, now here we go: We pass data to the lua script on the stack.
     * That is, we first have to prepare Lua's virtual stack the way we
     * want the script to receive it, then ask Lua to run it.
     */
    lua_newtable(L);    /* We will pass a table */

    /*
     * To put values into the table, we first push the index, then the
     * value, and then call lua_rawset() with the index of the table in the
     * stack. Let's see why it's -3: In Lua, the value -1 always refers to
     * the top of the stack. When you create the table with lua_newtable(),
     * the table gets pushed into the top of the stack. When you push the
     * index and then the cell value, the stack looks like:
     *
     * <- [stack bottom] -- table, index, value [top]
     *
     * So the -1 will refer to the cell value, thus -3 is used to refer to
     * the table itself. Note that lua_rawset() pops the two last elements
     * of the stack, so that after it has been called, the table is at the
     * top of the stack.
     */
    for (i = 1; i <= 5; i++) {
        lua_pushnumber(L, i);   /* Push the table index */
        lua_pushnumber(L, i*2); /* Push the cell value */
        lua_rawset(L, -3);      /* Stores the pair in the table */
    }

    /* By what name is the script going to reference our table? */
    lua_setglobal(L, "foo");

    /* Ask Lua to run our little script */
    result = lua_pcall(L, 0, LUA_MULTRET, 0);
    if (result) {
        fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1));
        exit(1);
    }

    /* Get the returned value at the top of the stack (index -1) */
    sum = lua_tonumber(L, -1);

    printf("Script returned: %.0f\n", sum);

    lua_pop(L, 1);  /* Take the returned value out of the stack */
    lua_close(L);   /* Cya, Lua */

    return 0;
}

现在是编译它的时间了。请记住,您需要链接到 -llua(Lua)以及可能需要的 -lm(数学库)。在我的系统上,我编写了这个简单的测试,"cc -o test test.c -I/usr/local/include -L/usr/local/lib -llua -lm" 生成了预期的二进制文件。在另一个系统(OpenSuse? 11.4,gcc4.7)上,还需要 -ldl 来解决对 "dlopen" 等的未定义引用。

最后,运行测试,您应该得到类似以下的结果

$ ./test                                           
The table the script received has:
1       2
2       4
3       6
4       8
5       10
Returning data back to C
Script returned: 30

这应该演示了基本 API 的工作原理以及如何在 C 和 Lua 之间传递值。

更多信息,请参见 [Lua API 演示]。它是一个为 Lua 解释器设计的模块,允许您使用堆栈设置一个假的 Lua 状态,调用 Lua API 函数,并观察它们对堆栈的影响。


最近更改 · 偏好设置
编辑 · 历史记录
最后编辑于 2018 年 3 月 2 日上午 7:09 GMT (差异)