lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



 Hello all,
This is my first attempt at writing coroutine enabled code, so probably its my mis-reading of docs.
  I'm using LuaPlus distro & visual studio 2003 compiler.
I have a problem with transferring parameters to the coroutine function when I create it from the C++ side. The documentation says that lua_xmove (and LuaState->XMove()) functions get n parameters from one state and put them in other state. But If I use it, the target state is corrupted - the memory is filled with 0xdeadbeef. If I use a cycle to transfer every value to the top of the source stack and xmove it to target, everythings is OK.
Any ideas?

Note that my C++ function receives its own arguments from Lua side and tries to give the rest to the lua function
which will be called in the created coroutine.

The following code does not work:

// this is calculation how many arguments must transfer to Lua function - it is OK
int num_function_args = num_args-NUM_ARGS_TO_MASTER_FUNCTION;
LuaState *iter_state = iter_thread.GetState();
iter_state->CheckStack(num_function_args);
state->XMove(iter_state, num_function_args);

// the following will crash with Access Violation, because the memory of TObject is filled with 0xdeadbeef
__int64 blah_top = (iter_state->Stack( -1)).GetNumber();


The following code works:

int num_function_args = num_args-NUM_ARGS_TO_MASTER_FUNCTION;
LuaState *iter_state = iter_thread.GetState();
iter_state->CheckStack(num_function_args);
for (int i = -num_function_args; i < 0; ++i)
{
  state->PushValue(i);
  state->XMove(iter_thread.GetState(),1); // move a value to iter_thread
  state->Pop();
}

Also, when later I call CoResume:
if (iter_state->CoResume(num_function_args)!=0)
{
  state->PushNil();
  iter_state->XMove(state,1);
  return 2;
}

it triggers the following check which I also does not understand.
  else if (!(L->ci->state & CI_YIELD))  /* not inside a yield? */
    return resume_error(L, "cannot resume non-suspended coroutine");


I really didn't understand what's wrong with my code.
Any help or just a hint will be very welcome.
Best regards,
Todor Totev