Luabind and coroutines

I am having trouble understanding how to use coroutines with luabind correctly. There is a template function:

template<class Ret> 
Ret resume_function(object const& obj, ...)

Where ( Ret) is supposed to contain the values ​​passed to yieldLua.

My current moments of confusion:

  • What happens if the function returns rather than calls yield? Does the value resume_functionreturn the return value of the function?
  • How should you use this function if you do not know in advance which (or how many) parameters will be passed to yield? For example, if there are several possible assignment functions that a function can call.
  • What is the type Retif multiple values ​​are passed in yield?

I'm just mistaken, how does it all work? I imagine something like that. On the Lua side:

local img = loadImage("foo.png")

loadImage ++, , lua_yield, luabind::resume_function img .

"foo.png" yield ? , yield, yield? ? , , - .

+3
1

(Ret) , Lua.

Luabind , , coroutine.yield.

, , ? resume_function ?

, .

, , ( ) ? , , .

; . , , , .

Ret, ?

. . , .

: Lua . , Luabind, , , Lua , . , Luabind . , , /, , , Ret.

loadImage ++, , lua_yield, luabind:: resume_function img .

Luabind, lua_yield . Luabind - , , , . :

module(L)
[
    def("do_thing_that_takes_time", &do_thing_that_takes_time, yield)
];

, ++, . Luabind, Lua, , , .

, , Lua coroutines - , . ; , coroutine.resume .

, Lua C/++; Lua ( lua_State).

, , , , Lua ++, , Lua , , .

, Lua script , ++. loadImage coroutine; , ++. Lua script , , , .

, , - , Lua script , . Lua script , ++ , . - :

function loadImageAsCoroutine(imageFilename)
    local cppThread = cpp.loadImage(imageFilename);

    local function threadFunc(cppThread)
        if(cppThread:isFinished()) then
            local data = cppThread:GetImage();
            return data;
        else
            coroutine.yield();
        end
    end

    local thread = coroutine.create(threadFunc);

    local errors, data = assert(coroutine.resume(thread, cppThread));

    if(coroutine.status(thread) == "dead") then
        return data;
    else
        return thread;
    end
end

coroutine . ; "thread", ++ . .

, , coroutine.resume ( luabind:: resume_function - ). . nil, ++ , nil .

+2
source

All Articles