简体   繁体   English

如何用lua c api在C语言中制作结构体

[英]How to make a structure in C language with lua c api

How to create the following C language structure using Lua c api?如何使用Lua c api创建以下C语言结构?

typedef struct _c{
    int d;
} obj_c;
typedef struct _b{
    obj_c c[4];
}obj_b;
typedef struct _a{
    obj_b b;
}obj_a;
obj_a a[4];

The above structure in lua a[1].bc[1].d = 1; lua中的上述结构a[1].bc[1].d=1; I try to use it together, but it doesn't work.我尝试一起使用它,但它不起作用。 Error Message: PANIC: unprotected error in call to Lua API (attempt to index a number value)错误消息:恐慌:调用 Lua API 时出现未受保护的错误(尝试索引一个数字值)

in lua a[1].bc = 1;在lua中 a[1].bc = 1; To use like this, I wrote the following code.为了像这样使用,我编写了以下代码。 This code works normally.此代码正常工作。

lua_createtable(L, 2, 0); // stack: {} : -1
{
    lua_pushnumber(L, 1); // stack: {}, 1 : -2
    {
        lua_newtable(L); // stack: {}, 1, {} : -3

        lua_createtable(L, 0, 1); // stack: {}, 1, {}, {} : -4
        lua_pushnumber(L, 49);
        lua_setfield(L, -2, "c");

        lua_setfield(L, -2, "b");
        lua_settable(L, -3);
    }
    lua_pushnumber(L, 2); // stack: {}, 2 : -2
    {
        lua_newtable(L); // stack: {}, 2, {} : -3

        lua_createtable(L, 0, 1); // stack: {}, 2, {}, {} : -4
        lua_pushstring(L, 50);
        lua_setfield(L, -2, "c");

        lua_setfield(L, -2, "b");
        lua_settable(L, -3);
    }
}
lua_pop(L, -2);
lua_setglobal(L, "a");

What do I do a[1].bc[1].d = 1;我该怎么做 a[1].bc[1].d = 1; Can it be made in the same form?可以做成同样的形式吗?

At first you use lua_pop not correctly, the main usage is to remove number of top elements from stack.起初你使用lua_pop不正确,主要用途是从堆栈中删除一些顶部元素。 In lua.h #define lua_pop(n) lua_settop(L, -(n)-1) , in your case it will be the same as lua_settop(L, 1) in your case it may be OK, but if there is something in stack (like arguments) it may lead to failure.lua.h #define lua_pop(n) lua_settop(L, -(n)-1) ,在你的情况下它与lua_settop(L, 1)在你的情况下可能是一样的,但如果有什么在堆栈中(如参数)它可能会导致失败。 In your code lua_pop is not needed at all as your stack on this line already filled table, so it must be:在您的代码中根本不需要lua_pop因为您在这一行上的堆栈已经填满了表,所以它必须是:

lua_createtable(L, 2, 0); // stack: {}
{
    lua_pushnumber(L, 1); // stack: {}, 1
    {
        lua_newtable(L); // stack: {}, 1, {}

        lua_createtable(L, 0, 1); // stack: {}, 1, {}, {}
        lua_pushnumber(L, 49);
        lua_setfield(L, -2, "c"); // stack: {}, 1, {}, {c=49}

        lua_setfield(L, -2, "b"); // stack: {}, 1, {b={c=49}}
        lua_settable(L, -3); // stack: {1 = {b={c=49}}}
    }
    lua_pushnumber(L, 2); // stack: {1 = {b={c=49}}}, 2
    {
        lua_newtable(L); // stack: {1 = {b={c=49}}}, 2, {}

        lua_createtable(L, 0, 1); // stack: {1={b={c=49}}}, 2, {}, {}
        lua_pushstring(L, 50);
        lua_setfield(L, -2, "c"); // stack: {1={b={c=49}}}, 2, {}, {c=50}

        lua_setfield(L, -2, "b"); // stack: {1={b={c=49}}}, 2, {b={c=50}}
        lua_settable(L, -3); // stack: {1={b={c=49}}, 2={b={c=50}}}
    }
}
lua_setglobal(L, "a"); // stack is empty, _G.a={ {b={c=49}}, {b={c=50}}}

If you want to make field c as array of tables, instead of lua_pushunmber(L, 49) and lua_pushnumber(L, 50) , replace with following code:如果要将字段c设为表数组,而不是lua_pushunmber(L, 49)lua_pushnumber(L, 50) ,请替换为以下代码:

lua_newtable(L);          // stack:...,{}
lua_pushnumber(L, 1);     // stack:...,{},1
lua_newtable(L);          // stack:...,{},1,{}
lua_pushnumber(L, 1);     // stack:...,{},1,{},1
lua_setfield(L, -2, "d"); // stack:...,{},1,{d=1}
lua_settable(L, -3);      //stack:...,{1={d=1}}

So in your stack instead of number will be filled table.所以在你的堆栈中而不是数字将被填充表。

To create empty structure like in C:要像在 C 中一样创建空结构:

/*typedef struct _c{
    int d;
} obj_c;
typedef struct _b{
    obj_c c[4];
}obj_b;
typedef struct _a{
    obj_b b;
}obj_a;
obj_a a[4];*/

int i,j;
lua_createtable(L, 4, 0); //obj_a[4]
for (i = 1; i <= 4; i++) { // adding 4 tables obj_a
  lua_createtable(L, 0, 1); //obj_a
  lua_createtable(L, 4, 0); //obj_c[4]
  for (j = 1; j <= 4; j++) { // adding 4 tables obj_c
    lua_createtable(L, 0, 1); //obj_c
    lua_pushinteger(L, 0); //default d value
    lua_setfield(L, -2, "d"); //{d=0}
    lua_seti(L, -2, j); //add table obj_c to array
  }
  lua_setfield(L, -2, "b"); // {b=obj_c[4]}
  lua_seti(L, -2, i); //add table obj_a to array
}
lua_setglobal(L, "a");

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

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