从脚本调用 C++ 函数时 Lua 崩溃

简而言之,我调用了 Lua 脚本函数 aaaaaa 调用了 C++ 函数 testfunc,在 testfunc 要调用脚本函数 bbb 时就崩溃了。

这是 Lua 代码:

foo = 100
function aaa()
    testfunc(foo * 2)
end
function bbb(...)
    for val in ... do
        print(val)
    end
end

以下是 C++ testfunc,它使用 3 个参数调用了 bbb

int testfunc( lua_State* lua )
{
    auto arg1 = lua_tointeger( lua, 1 );
    juce::Logger::writeToLog( "testfunc called with argument " + juce::String( arg1 ) );

    int bbb_type = lua_getglobal( lua, "bbb" );
    if ( bbb_type != LUA_TFUNCTION )
    {
        lua_pushstring( lua, "bbb is not function" );
        lua_error( lua );
    }

    lua_pushinteger( lua, 100 );
    lua_pushinteger( lua, 200 );
    lua_pushstring( lua, "deep dark fantasy" );
    lua_call( lua, 3, 0 );

    return 0;
}

在崩溃的堆栈跟踪中,似乎在 luaD_precall 中它不认为传入的是一个对象,switch 分支走到了最后一个 default 部分,试图从 __call metamethod 中查找可调用对象。然而,在 testfunc 中,函数对象的类型已经被检查过了。

点赞