C++ 调用c++;lua函数传递的参数更少

C++ 调用c++;lua函数传递的参数更少,c++,function,lua,C++,Function,Lua,因此,函数类似于: send_success(lua_State *L){ MailService *mls = static_cast<MailService *>(lua_touserdata(L, lua_upvalueindex(1))); Device *dev = static_cast<Device *>(lua_touserdata(L, lua_upvalueindex(2))); int numArgs = lua_getto

因此,函数类似于:

send_success(lua_State *L){

    MailService *mls = static_cast<MailService *>(lua_touserdata(L, lua_upvalueindex(1)));
    Device *dev = static_cast<Device *>(lua_touserdata(L, lua_upvalueindex(2)));
    int numArgs = lua_gettop(L);
    TRACE << "Number of arguments passed is = " << numArgs;

   /* here I do some operation to get the arguments.
    I am expecting total of 5 arguments on the stack. 
    3 arguments are passed from function call in lua 
    and 2 arguments are pushed as closure 

   */
    string one_param = lua_tostring(L, 3, NULL)
    string two_param = lua_tostring(L, 4, NULL)
    string other_param = lua_tostring(L, 5, NULL)



}
从lua那里打电话,我会的

obj:sendSuccess("one param","second param","third param")
但是当我检查参数的数量时。它应该给出5个参数。相反,只传递4个参数。 我做了一些测试,看看我传递的两个对象是否正确传递了使用过的数据。它们被正确地通过了

这里唯一缺少的是,缺少一个从lua端传递的参数

我还试着只推一个物体,结果它正常工作了。因此,我不确定我是否在某个地方弄乱了参数编号


请说出您的意见

您作为闭包的一部分创建的用户数据对象不会作为参数传递给函数,而是放置在状态的另一个位置


这意味着用于获取带有
lua\u tostring
的参数的偏移量是错误的。

作为闭包的一部分创建的用户数据对象没有作为参数传递给函数,而是放置在状态的另一个位置

这意味着用于获取带有
lua\u tostring
的参数的偏移量是错误的。

确定。所以问题是

lua\u pushclosure
将用户数据保存在
lua\u堆栈上的单独空间中。在该堆栈中,偏移量1和2表示第一个和第二个对象

lua_pushlightuserdata(theLua, (void*) mls);
lua_pushlightuserdata(theLua, (void*) this);
lua_pushcclosure(theLua, lua_send_success,2);
但在那之后,我要去第三个第三个,假设我已经进入了第二个位置。但这是错误的。正确的做法是考虑<代码> PutsCuule<代码>在堆栈上只占用一个空间,而不管推送<代码> ListueReDATAs/COD>多少次,其余的PARAM可以从第二个偏移量开始访问。所以下面的代码对我很有用:

  string one_param = lua_tostring(L, 2, NULL)
  string two_param = lua_tostring(L, 3, NULL)
  string other_param = lua_tostring(L, 4, NULL)
嗯。所以问题是

lua\u pushclosure
将用户数据保存在
lua\u堆栈上的单独空间中。在该堆栈中,偏移量1和2表示第一个和第二个对象

lua_pushlightuserdata(theLua, (void*) mls);
lua_pushlightuserdata(theLua, (void*) this);
lua_pushcclosure(theLua, lua_send_success,2);
但在那之后,我要去第三个第三个,假设我已经进入了第二个位置。但这是错误的。正确的做法是考虑<代码> PutsCuule<代码>在堆栈上只占用一个空间,而不管推送<代码> ListueReDATAs/COD>多少次,其余的PARAM可以从第二个偏移量开始访问。所以下面的代码对我很有用:

  string one_param = lua_tostring(L, 2, NULL)
  string two_param = lua_tostring(L, 3, NULL)
  string other_param = lua_tostring(L, 4, NULL)

你能举个例子说明我应该如何得到实际的参数,以我的例子为背景吗?你能举个例子说明我应该如何得到实际的参数,以我的例子为背景吗?