xLua icon indicating copy to clipboard operation
xLua copied to clipboard

util.lua.txt中coroutine_call方法缺少返回值

Open PatchouliTC opened this issue 4 years ago • 1 comments

coroutine_call原始代码
local function coroutine_call(func)
    return function(...)
        local co = coroutine.create(func)
        assert(coroutine.resume(co, ...))
    end
end
在async_test样例内例子中listener作为void返回值可以正常运行,但如果存在以下情况:

1.C#需要调用lua方法并需要其返回某些结果

public Func<int,int,int> LuaCalc;

2.该lua方法需要异步回调C#方法并等待返回其他信息

local function calc(a,b)
    c=CS.Simple.StaticCaller.LuaGetintInfo_From_NetWork()
    return a+b+c
end

那么此时按照样例使用则需要定义为 obj.SetCalcMethod(util.coroutine_call(calc)) 之后再C#端调用LuaCalc方法可以正常执行,但是将无法获取返回值[xlua将会返回对应返回值类型的默认值]

个人暂时解决方法是手动加上了相关的返回值处理

coroutine_call改后代码
local function coroutine_call(func)
    return function(...)
        local co = coroutine.create(func)
        local rets={assert(coroutine.resume(co, ...))}

        if not rets or rets[1]==false then
            error("error happened while coroutine a function")
        end

        rets=table.remove(rets)
        if type(rets)=="table" then
            return unpack(rets)
        end
        return rets
    end
end

希望官方能对其进行修正

PatchouliTC avatar Mar 13 '21 16:03 PatchouliTC

你这写得有点问题吧? rets是全局的? 后面的逻辑也比较啰嗦,直接return rets[2]就可以了吧?

chexiongsheng avatar Mar 15 '21 02:03 chexiongsheng