简体   繁体   English

是否可以使用ac指针访问Lua表元素?

[英]Is it possible to access Lua table elements using a c pointer?

I call a C function in Lua passing an array/table to it as argument: 我在Lua中调用一个C函数,将一个数组/表作为参数传递给它:

tools:setColors({255,255,0})

In the C function I get the size of: 在C函数中,我得到的大小:

if (lua_gettop(state) == 2 && lua_istable(state, -1))
{
    lua_len(state, -1);
    int count = lua_tointeger(state, -1);
    lua_pop(state, 1);
}

Instead of iterating over the table is it possible to get the C pointer to that array to use it later for memcpy ? 是否有可能获得指向该数组的C指针以便稍后将其用于memcpy Or maybe there is another way to copy data directly? 或者可能还有另一种直接复制数据的方法?

update: What I actually try to do, so maybe someone has a better solution... In my Lua script I do some calculation with the colors. 更新:我实际上尝试做什么,所以也许有人有更好的解决方案...在我的Lua脚本中,我用颜色做一些计算。 The RGB values of all colors are saved in the one big table (example above would mean one color). 所有颜色的RGB值都保存在一个大表中(上面的示例将表示一种颜色)。 This table is passed back to my C code with setColors call, where I normally would copy it using memcpy to a std::vector ( memcpy(_colors.data(), data, length ); At the moment I do the following: 这个表通过setColors调用传递给我的C代码,我通常会使用memcpy将它复制到std :: vector( memcpy(_colors.data(), data, length );目前我执行以下操作:

    // one argument with array of colors (triple per color)
    lua_len(state, -1);
    int count = lua_tointeger(state, -1);
    lua_pop(state, 1);

    for (int i=0; i < count / 3; i++)
    {
        ColorRgb color; // struct {uint8_t red, uint8_t green, uint8_t blue}
        lua_rawgeti(state, 2, 1 + i*3);
        color.red = luaL_checkinteger(state, -1);
        lua_pop(state, 1);

        lua_rawgeti(state, 2, 2 + i*3);
        color.green = luaL_checkinteger(state, -1);
        lua_pop(state, 1);

        lua_rawgeti(state, 2, 3 + i*3);
        color.blue = luaL_checkinteger(state, -1);
        lua_pop(state, 1);
        _colors[i] = color;
    }

seems for me a lot of code for a simple copy operation... PS I work with Lua 5.3 对我来说似乎有很多代码用于简单的复制操作...... PS我使用的是Lua 5.3

No, it is not possible to use a Lua table as a C array via a pointer. 不,不可能通过指针将Lua表用作C数组。

The only way to get and put values in a Lua table is by using the Lua C API. 在Lua表中获取和放置值的唯一方法是使用Lua C API。

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

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