- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图从 C++ 调用 lua 函数,但我不断收到错误消息“错误:尝试调用 nil 值”。
lua 函数创建一个 C++ 对象,然后调用其方法之一,胶水代码已使用 tolua++ 生成。当调用 lua 函数时,我传入了一个 lua_State 指针,因为 C++ 类需要一个用于它的构造函数,lua 函数将其交给它。
但据我所知,它永远不会走那么远,它根本不运行脚本。至少错误没有引用脚本中的任何行号。
这是调用该函数的 C++ 代码:
int main()
{
lua_State *lState;
lState = luaL_newstate(); //new lua state
tolua_TestClass_open (lState); //open libs for TestClass
int iStatus = luaL_loadfile( lState, "lua1.lua" ); //load script
if (iStatus)
{
std::cout << "Error: " << lua_tostring( lState, -1 );
return 1;
}
iStatus = lua_pcall( lState, 0, 0, 0); //initialise the lua script
if( iStatus )
{
std::cout << "Error: " << lua_tostring( lState, -1 );
return 1;
}
lua_getglobal( lState, "lua1Function" ); //starting the function call
lua_pushlightuserdata (lState, lState); //lState is also being passed in as a parameter
iStatus = lua_pcall( lState, 1, 0, 0 ); //calling on this lua state with 1 argument expecting 0 outputs
if( iStatus ) //error checking
{
std::cout << "Error: " << lua_tostring( lState, -1 );
return 1;
}
return 1;
}
这是lua脚本:
function lua1Function(lstate)
tclass = TestClass:new();
tclass:method1();
tclass:method3();
end
我很确定这不是忘记做那么简单的事情:
tclass = TestClass:new(lstate);
因为胶水代码似乎表明我不需要这样做,这里:
/* method: new of class TestClass */
#ifndef TOLUA_DISABLE_tolua_TestClass_TestClass_new00
static int tolua_TestClass_TestClass_new00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertable(tolua_S,1,"TestClass",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
lua_State* tolua_var_1 = tolua_S; //seems to know I want the lua_State by default,
//usually it will pop a usertype or luanumber or whatever off the stack,
//depending on the parameter
{
TestClass* tolua_ret = (TestClass*) Mtolua_new((TestClass)(tolua_var_1));
tolua_pushusertype(tolua_S,(void*)tolua_ret,"TestClass");
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'new'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
产生的错误消息似乎证实了我的理论:“函数‘new’中的错误。参数 #2 是‘userdata’;预期‘[no object]’。”即它不期望/不需要我传递那个 lua_State 指针
我很茫然,我很难找到 lua 问题的解决方案,因为教程/文档在涉及 tolua++ 的基础上似乎很薄,但现在更改绑定(bind)库为时已晚。
任何帮助将不胜感激,我希望我提供的内容足以诊断问题。
编辑:这是我的 TestClass.cpp 代码(您可以忽略方法 1、2 和 3,因为它们似乎没有被调用,因为这个错误):
#include "TestClass.h"
#include <iostream>
TestClass::TestClass(lua_State *L)
{
num = NULL;
lState = L;
}
int TestClass::method1()
{
int iStatus = luaL_loadfile( lState, "lua2.lua" );
if (iStatus)
{
std::cout << "Error: " << lua_tostring( lState, -1 );
return 1;
}
iStatus = lua_pcall( lState, 0, 0, 0); //this might be to initialise the lua script
if( iStatus )
{
std::cout << "Error: " << lua_tostring( lState, -1 );
return 1;
}
///////////call lua function, passing on self pointer onto the stack////////////////
lua_getglobal( lState, "lua2Function" );
tolua_pushusertype(lState, this, "TestClass");
iStatus = lua_pcall( lState, 1, 1, 0 );
if( iStatus ) //error checking
{
std::cout << "Error: " << lua_tostring( lState, -1 );
return 1;
}
///////////lua function returns an int, return it////////////
num = lua_tointeger( lState, -1 );
return 0;
}
int TestClass::method2(int i)
{
i += 2;
return i;
}
void TestClass::method3()
{
std::cout << (char)num << std::endl;
}
/*
** Lua binding: TestClass
** Generated automatically by tolua++-1.0.92 on 04/05/11 17:59:24.
*/
#ifndef __cplusplus
#include "stdlib.h"
#endif
#include "string.h"
#include "tolua++.h"
/* function to release collected object via destructor */
#ifdef __cplusplus
static int tolua_collect_TestClass (lua_State* tolua_S)
{
TestClass* self = (TestClass*) tolua_tousertype(tolua_S,1,0);
Mtolua_delete(self);
return 0;
}
#endif
/* function to register type */
static void tolua_reg_types (lua_State* tolua_S)
{
tolua_usertype(tolua_S,"TestClass");
}
/* method: new of class TestClass */
#ifndef TOLUA_DISABLE_tolua_TestClass_TestClass_new00
static int tolua_TestClass_TestClass_new00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertable(tolua_S,1,"TestClass",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
lua_State* tolua_var_1 = tolua_S;
{
TestClass* tolua_ret = (TestClass*) Mtolua_new((TestClass)(tolua_var_1));
tolua_pushusertype(tolua_S,(void*)tolua_ret,"TestClass");
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'new'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: new_local of class TestClass */
#ifndef TOLUA_DISABLE_tolua_TestClass_TestClass_new00_local
static int tolua_TestClass_TestClass_new00_local(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertable(tolua_S,1,"TestClass",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
lua_State* tolua_var_1 = tolua_S;
{
TestClass* tolua_ret = (TestClass*) Mtolua_new((TestClass)(tolua_var_1));
tolua_pushusertype(tolua_S,(void*)tolua_ret,"TestClass");
tolua_register_gc(tolua_S,lua_gettop(tolua_S));
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'new'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: method1 of class TestClass */
#ifndef TOLUA_DISABLE_tolua_TestClass_TestClass_method100
static int tolua_TestClass_TestClass_method100(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"TestClass",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
TestClass* self = (TestClass*) tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'method1'", NULL);
#endif
{
int tolua_ret = (int) self->method1();
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'method1'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: method2 of class TestClass */
#ifndef TOLUA_DISABLE_tolua_TestClass_TestClass_method200
static int tolua_TestClass_TestClass_method200(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"TestClass",0,&tolua_err) ||
!tolua_isnumber(tolua_S,2,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
TestClass* self = (TestClass*) tolua_tousertype(tolua_S,1,0);
int tolua_var_2 = ((int) tolua_tonumber(tolua_S,2,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'method2'", NULL);
#endif
{
int tolua_ret = (int) self->method2(tolua_var_2);
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'method2'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: method3 of class TestClass */
#ifndef TOLUA_DISABLE_tolua_TestClass_TestClass_method300
static int tolua_TestClass_TestClass_method300(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"TestClass",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
TestClass* self = (TestClass*) tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'method3'", NULL);
#endif
{
self->method3();
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'method3'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* Open function */
TOLUA_API int tolua_TestClass_open (lua_State* tolua_S)
{
tolua_open(tolua_S);
tolua_reg_types(tolua_S);
tolua_module(tolua_S,NULL,0);
tolua_beginmodule(tolua_S,NULL);
#ifdef __cplusplus
tolua_cclass(tolua_S,"TestClass","TestClass","",tolua_collect_TestClass);
#else
tolua_cclass(tolua_S,"TestClass","TestClass","",NULL);
#endif
tolua_beginmodule(tolua_S,"TestClass");
tolua_function(tolua_S,"new",tolua_TestClass_TestClass_new00);
tolua_function(tolua_S,"new_local",tolua_TestClass_TestClass_new00_local);
tolua_function(tolua_S,".call",tolua_TestClass_TestClass_new00_local);
tolua_function(tolua_S,"method1",tolua_TestClass_TestClass_method100);
tolua_function(tolua_S,"method2",tolua_TestClass_TestClass_method200);
tolua_function(tolua_S,"method3",tolua_TestClass_TestClass_method300);
tolua_endmodule(tolua_S);
tolua_endmodule(tolua_S);
return 1;
}
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
TOLUA_API int luaopen_TestClass (lua_State* tolua_S) {
return tolua_TestClass_open(tolua_S);
};
#endif
最佳答案
Lua 状态永远不会由 Lua 代码显式处理——它是隐式的。任何由 Lua 调用的 C++ 函数都不需要显式地传递状态——它作为第一个参数获取它,它总是由 Lua 传递,而不管其他参数,因为没有 lua_State*
就不可能以任何方式与 Lua 交互.这样做的唯一原因是如果您有某种元状态,或者如果您正在使用 Lua 的协作协同例程做事。
全局函数看起来很简单,不太可能是错误的来源。您需要打印 TestClass 的内容以验证它是否具有预期的内容,如果没有,那么这是一个特定于绑定(bind)库的问题,您将不得不深入研究它们的内部结构,因为在我看来该代码最有可能出现问题是 TestClass 表没有您期望的内容。
关于c++ - 尝试调用 lua 脚本时出现 "Error: attempt to call a nil value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5559741/
reqwest v0.9 将 serde v1.0 作为依赖项,因此实现 converting serde_json errors into reqwest error . 在我的代码中,我使用 se
我有这个代码: let file = FileStorage { // ... }; file.write("Test", bytes.as_ref()) .map_err(|e| Mu
我只是尝试用angular-cli创建一个新项目,然后运行服务器,但是它停止并显示一条有趣的消息:Error: No errors。 我以这种方式更新了(希望有帮助):npm uninstall -g
我从我的 javascript 发送交易 Metamask 打开传输对话框 我确定 i get an error message in metamask (inpage.js:1 MetaMask -
这个问题在这里已经有了答案: How do you define custom `Error` types in Rust? (3 个答案) How to get a reference to a
我想知道两者之间有什么大的区别 if let error = error{} vs if error != nil?或者只是人们的不同之处,比如他们如何用代码表达自己? 例如,如果我使用这段代码: u
当我尝试发送超过 50KB 的图像时,我在 Blazor 服务器应用程序上收到以下错误消息 Error: Connection disconnected with error 'Error: Serv
我有一个error-page指令,它将所有异常重定向到错误显示页面 我的web.xml: [...] java.lang.Exception /vi
我有这样的对象: address: { "phone" : 888, "value" : 12 } 在 WHERE 中我需要通过 address.value 查找对象,但是在 SQL 中有函数
每次我尝试编译我的代码时,我都会遇到大量错误。这不是我的代码的问题,因为它在另一台计算机上工作得很好。我尝试重新安装和修复,但这没有帮助。这是整个错误消息: 1>------ Build starte
在我的代码的类部分,如果我写一个错误,则在不应该的情况下,将有几行报告为错误。我将'| error'放在可以从错误中恢复的良好/安全位置,但是我认为它没有使用它。也许它试图在某个地方恢复中间表情? 有
我遇到了 csv 输入文件整体读取故障的问题,我可以通过在 read_csv 函数中添加 "error_bad_lines=False" 来删除这些问题来解决这个问题。 但是我需要报告这些造成问题的文
在 Spring 中,验证后我们在 controller 中得到一个 BindingResult 对象。 很简单,如果我收到验证错误,我想重新显示我的表单,并在每个受影响的字段上方显示错误消息。 因此
我不知道出了什么问题,因为我用 Java 编程了大约一年,从来没有遇到过这个错误。在一分钟前在 Eclipse 中编译和运行工作,现在我得到这个错误: #A fatal error has been
SELECT to_char(messages. TIME, 'YYYY/MM/DD') AS FullDate, to_char(messages. TIME, 'MM/DD
我收到这些错误: AnonymousPath\Anonymized.vb : error BC30037: Character is not valid. AnonymousPath\Anonymiz
我刚刚安装了 gridengine 并在执行 qstat 时出现错误: error: commlib error: got select error (Connection refused) erro
嗨,我正在学习 PHP,我从 CRUD 系统开始,我在 Windows 上安装了 WAMP 服务器,当我运行它时,我收到以下错误消息。 SCREAM: Error suppression ignore
我刚刚开始一个新项目,我正在学习核心数据教程,可以找到:https://www.youtube.com/watch?v=zZJpsszfTHM 我似乎无法弄清楚为什么会抛出此错误。我有一个名为“Exp
当我使用 Jenkins 运行新构建时,出现以下错误: "FilePathY\XXX.cpp : fatal error C1853: 'FilePathZ\XXX.pch' precompiled
我是一名优秀的程序员,十分优秀!