coroutune return issue

Sep 29, 2014 at 10:23 AM
Hi NeoLithos,

The coroutune has a return result issue.
function foo(a)
    print('foo', a)
    return coroutine.yield(2 * a)
end

co = coroutine.create(function ( a, b )
    print('co-body', a, b)
    local r = foo(a + 1)
    print('co-body', r)
    local r, s = coroutine.yield(a + b, a - b)
    print('co-body', r, s)
    return b, 'end'
end)

print('main', coroutine.resume(co, 1, 10))
print('main', coroutine.resume(co, 'r'))
print('main', coroutine.resume(co, 'x', 'y'))
print('main', coroutine.resume(co, 'x', 'y'))
Expect result is:
co-body 1 10
foo 2
main true 4
co-body r
main true 11 -9
co-body x y
main true 10 end
main false cannot resume dead coroutine
But we get:
co-body 1 10
foo 2
main 4
co-body r
main 11 -9
co-body x y
main 10 end
main false cannot resume dead coroutine
    /// <summary></summary>
    /// <param name="ar"></param>
    /// <returns></returns>
    public LuaResult EndResume(IAsyncResult ar)
    {
      lock (this)
        try
        {
          if (this != ar)
            throw new LuaRuntimeException(Properties.Resources.rsCoroutineInvalidAR, 2, false);

          if (lockResume == null)
            throw new LuaRuntimeException(Properties.Resources.rsCoroutineNoBeginResume, 2, false);

          if (evYield != null)
            evYield.Wait();

          if (evResume == null)
          {
            evYield.Dispose();
            evYield = null;
            return new LuaResult("false", "cannot resume dead coroutine"); //here
          }
          else
            return new LuaResult(true, currentYield); //here
        }
        finally
        {
          Monitor.Exit(lockResume);
          lockResume = null;
        }
    } // proc EndResume
It should be change to :
    /// <summary></summary>
    /// <param name="ar"></param>
    /// <returns></returns>
    public LuaResult EndResume(IAsyncResult ar)
    {
      lock (this)
        try
        {
          if (this != ar)
            throw new LuaRuntimeException(Properties.Resources.rsCoroutineInvalidAR, 2, false);

          if (lockResume == null)
            throw new LuaRuntimeException(Properties.Resources.rsCoroutineNoBeginResume, 2, false);

          if (evYield != null)
            evYield.Wait();

          if (evResume == null)
          {
            evYield.Dispose();
            evYield = null;
            return new LuaResult(false, "cannot resume dead coroutine");
          }
          else
            return new LuaResult(true, (object)currentYield);
        }
        finally
        {
          Monitor.Exit(lockResume);
          lockResume = null;
        }
    } // proc EndResume
Please help to fix it.
Coordinator
Sep 29, 2014 at 5:31 PM
Hi,

I broke the constructor of LuaResult.

I changed it in 31945.

Do you need a compiled version? Or can I wait until friday?

Thank you for the good report.
NeoLithos
Marked as answer by tzhaoh on 10/16/2014 at 7:53 PM
Sep 30, 2014 at 1:02 AM
Thanks for your fixed so quickly.

No need compiled version. I'm migrating to Dotnet FW 3.5 using Dynamic Language Runtime. It seems no problem.