lua_tothread

本文探讨了在Lua 5.1.4版本中使用协程(coroutine)和钩子函数(testHook)时遇到的问题。当从C调用Lua_State并设置行钩子(line hook)时,程序会在lvm.c的第71行崩溃。通过提供复现问题的代码示例,作者希望找到问题的原因及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://arcemu.org/forums/index.php?showtopic=19327
 
 
    Hello.

Call to lua function from c using yielded lua_State works fine. But  same call with line hook set crashes in lvm.c line 71.
    Lua version 5.1.4

    Code to reproduce:

#include <stdio.h>

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

void testHook(lua_State *L, lua_Debug *ar)
{
    static int c = 0;
    c++;
}

int main(void)
{
    lua_State* l = lua_open();
    luaopen_base(l);

lua_sethook(l, testHook, LUA_MASKLINE, 0); //works fine when  this line is commented
    const char* script =   "function func()    \
                                print('func1')    \
                            end    \
                            function threadFunc()    \
                                print('before yield')    \
                                coroutine.yield()    \
                                print('after yield')    \
                            end    \
                            thread = coroutine.create(threadFunc)    \
                            coroutine.resume(thread)    \
                            ";
    int rez = luaL_dostring(l, script);
    if (rez) {
        return 1;
    }

    lua_getglobal(l, "thread");
    lua_State* thread = lua_tothread(l, -1);

lua_getglobal(thread, "func"); //will work too if "l" state is  used....
    if (lua_pcall(thread, 0, 0, 0)) {
        return 1;
    }

    lua_resume(thread, 0);

    return 0;
}

I believe that it should either work in both cases (wonder how?...)  or report error even if no hook set...
--
WBR, Anatoly Ahmedov
### Lua 中 `lua_pcallk` 与 `lua_pcall` 的区别 在 Lua 的 C API 中,`lua_pcallk` 和 `lua_pcall` 都用于保护模式下调用函数,防止被调用的 Lua 函数中发生错误。然而,两者之间存在显著差异。 #### `lua_pcall` - `lua_pcall` 是一个较早的函数,用于以保护模式运行一个 Lua 函数。 - 它接受以下参数: - 栈中函数的位置。 - 参数的数量。 - 返回值的数量。 - 错误处理函数的位置(如果有的话)。 - 调用完成后,控制权立即返回给调用者[^2]。 #### `lua_pcallk` - `lua_pcallk` 是 `lua_pcall` 的扩展版本,支持异步操作。 - 它额外提供了一个回调函数参数和一个用户数据参数。 - 当被调用的函数完成时,Lua 可以通过回调函数通知调用者,这使得它适合于异步或协程场景。 - 其签名允许开发者指定一个“continuation function”,以便在函数执行完毕后继续处理逻辑[^3]。 ### Lua 5.4.6 中 `lua_pcall` 的移除原因 在 Lua 5.4 版本中,`lua_pcall` 被标记为废弃,并最终在 Lua 5.4.6 中完全移除。这是因为 `lua_pcallk` 提供了更通用的功能,可以完全替代 `lua_pcall` 的作用[^4]。具体来说: - `lua_pcall` 的功能可以通过将 `lua_pcallk` 的回调函数设置为 `NULL` 来实现。 - 移除 `lua_pcall` 简化了 C API 的设计,减少了冗余函数,同时鼓励开发者使用更现代化的接口。 以下是 `lua_pcallk` 的典型用法示例,展示如何模拟 `lua_pcall` 的行为: ```c int lua_pcall_alternative(lua_State *L, int nargs, int nresults, int errfunc) { return lua_pcallk(L, nargs, nresults, errfunc, 0, NULL); } ``` ### 示例代码:使用 `lua_pcallk` 进行保护模式调用 以下是一个使用 `lua_pcallk` 的简单示例,展示了如何在保护模式下调用 Lua 函数并处理可能的错误。 ```c #include "lua.h" #include "lauxlib.h" #include "lualib.h" static int continuation_function(lua_State *L) { // 处理异步结果 printf("Continuation function called\n"); return 0; } int main() { lua_State *L = luaL_newstate(); luaL_openlibs(L); // 创建一个简单的 Lua 函数 luaL_dostring(L, "function f(x) return x * 2 end"); // 准备调用参数 lua_getglobal(L, "f"); lua_pushinteger(L, 10); // 使用 lua_pcallk 调用函数 int status = lua_pcallk(L, 1, 1, 0, 0, continuation_function); if (status != LUA_OK) { fprintf(stderr, "Error: %s\n", lua_tostring(L, -1)); lua_pop(L, 1); // 弹出错误消息 } else { printf("Result: %d\n", (int)lua_tointeger(L, -1)); } lua_close(L); return 0; } ``` ### 结论 `lua_pcallk` 是 `lua_pcall` 的增强版本,提供了对异步操作的支持。随着 Lua 5.4 的发展,`lua_pcall` 因其功能可被 `lua_pcallk` 完全覆盖而被移除,从而简化了 C API 的设计。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值