lua-users home
lua-l archive

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


吕游 wrote:
>  if i call luaL_error, the coroutine will stop and then the outer
> lua_resume will return the error string which pass from luaL_error.
> my questing is: how to reuse the lua_state?
> i've tried to clear the stack,push a new chunk and then call
> lua_resume,but it said:"cannot resume non-suspended coroutine".

Unless there's some cunning way around it, you'll need to run the
coroutine's code inside a pcall. When you resume it, the pcall will
catch the error rather than causing the whole coroutine to terminate.

(errors/pcall are Lua's exceptions/try/catch mechanism:

function coroutine_really_main()
  call_some_native_function_that_may_error()
end

function coroutine_main()
  if pcall(coroutine_really_main) then
    success()
  else
    cleanup_after_error()
    return coroutine_main() -- restart the coroutine
  end
end

-- 
David Given
dg@cowlark.com