如何从Lua函数获取多个返回的表? [英] How to get multiple returned tables from Lua function?

查看:453
本文介绍了如何从Lua函数获取多个返回的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将多个浮点数组从C ++发送到Lua函数作为参数,然后从该函数返回多个表,以便可以再次将它们用作C ++中的浮点数组.

I'm trying to send multiple float arrays from C++ to Lua function as arguments and then return multiple tables from the function so I can use them again as float arrays in C++.

所以我的Lua函数将如下所示.

So my Lua function will look like this.

function perform(arg1, arg2)
    local ret1, ret2 = {}, {}
    for i=1, #arg1 do
        ret1[i] = arg1[i] * 0.2;
        ret2[i] = arg2[i] * 0.3;
    end
    return ret1, ret2
end

这就是我在C ++中向Lua函数发送和从中返回多个表的方式.

And this is how I send and return multiple tables to/from Lua function in C++.

lua_getglobal(L, "perform");

for (int i=0; i<numArgs; ++i) {

    lua_newtable(L);
    float *in = reinterpret_cast<float*>(w[i]);

    for (int j=0; j<64; ++j) {

        lua_pushinteger(L, j+1);
        lua_pushnumber(L, in[j]);
        lua_settable(L, -3);
    }
}
lua_call(L, numArgs, numRets);

for (int i=0; i<numRets; ++i) {

    float *out = reinterpret_cast<float*>(w[numArgs+i]);

    for (int j=0; j<64; ++j) {

        lua_pushinteger(L, j+1);
        lua_gettable(L, -2);
        out[j] = lua_tonumber(L, -1);
        lua_pop(L, 1);
    }
    //how to detect next returned table?
}

但是,如果我尝试使用代码,则返回的数组具有相同的值.

But if I try the code, the returned arrays have same values.

我认为这是因为我没有正确获取返回的表.

I think it's because I'm not correctly getting the returned tables.

有人可以教我如何正确获取多个返回的表吗?

Could anybody please teach me how to get multiple returned tables correctly?

P.S:我还想知道我的代码是否可以优化以获得更好的性能.

P.S: I would also like to know if my code can be optimized for better performance.

传递并返回一个包含多个子表的表会更快(更高效)吗?如果是这样,希望有人能教我该怎么做.

推荐答案

我不知道您要在这里做什么,但是从函数返回的第二张表很容易在堆栈上访问.您只需对堆栈索引执行一些算术运算即可到达正确的位置.

I have no idea what you are trying to do here, but the second table returned from the function is easily accessible on the stack. You just have to perform some arithmetic on the stack index to get to the correct position.

那些reinterpret_casts在我看来非常可疑.您很有可能做错了事.

Those reinterpret_casts look extremely fishy to me. You are quite likely to be doing something wrong.

#include <iostream>
#include <vector>

#include <lua.hpp>

int main(int argc, char *argv[]) {
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " <script.lua>\n";
        return 1;
    }

    luaL_dofile(L, argv[1]);

    // Mock data
    int numArgs = 2;
    int numRets = 2;
    std::vector<float> w1(64, 1.0f);
    std::vector<float> w2(64, 1.0f);
    std::vector<float> w3(64, 1.0f);
    std::vector<float> w4(64, 1.0f);
    std::vector<float *> w = {w1.data(), w2.data(), w3.data(), w4.data()};

    lua_getglobal(L, "perform");

    for (int i = 0; i < numArgs; ++i) {

        lua_newtable(L);
        float *in = reinterpret_cast<float *>(w[i]);

        for (int j = 0; j < 64; ++j) {
            lua_pushinteger(L, j + 1);
            lua_pushnumber(L, in[j]);
            lua_settable(L, -3);
        }
    }

    lua_call(L, numArgs, numRets);

    for (int i = 0; i < numRets; ++i) {

        float *out = reinterpret_cast<float *>(w[numArgs + i]);

        for (int j = 0; j < 64; ++j) {
            lua_pushinteger(L, j + 1);
            lua_gettable(L, -2 - i); // Just some stack index arithmetic
            out[j] = lua_tonumber(L, -1);
            lua_pop(L, 1);
        }
    }

    lua_close(L);
}

这篇关于如何从Lua函数获取多个返回的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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