Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LuaCall / While loop where control variable is never changed. #296

Open
blakepell opened this issue Mar 5, 2021 · 1 comment
Open

LuaCall / While loop where control variable is never changed. #296

blakepell opened this issue Mar 5, 2021 · 1 comment

Comments

@blakepell
Copy link

blakepell commented Mar 5, 2021

I haven't had a problem with this but the last while check looks buggy. The while loop at the end has a check where the control variable is never changed. Presumably, if it gets there and copied < nresults then it's going to create an endless loop on the L.Push(DynValue.Nil); line.

If it's trying to fill the additional spots with L.Push(DynValue.Nil) does copied need to be incremented in that loop?

protected static void LuaCall(LuaState L, lua_Integer nargs, lua_Integer nresults = LUA_MULTRET)
{
    DynValue[] args = L.GetTopArray(nargs);

    L.Discard(nargs);

    DynValue func = L.Pop();

    DynValue ret = L.ExecutionContext.Call(func, args);

    if (nresults != 0)
    {
        if (nresults == -1)
        {
            nresults = (ret.Type == DataType.Tuple) ? ret.Tuple.Length : 1;
        }

        DynValue[] vals = (ret.Type == DataType.Tuple) ? ret.Tuple : new DynValue[1] { ret };

        int copied = 0;

        for (int i = 0; i < vals.Length && copied < nresults; i++, copied++)
        {
            L.Push(vals[i]);
        }

        while (copied < nresults)
        {
            L.Push(DynValue.Nil);
        }
    }
}

Should the last while loop be:

while (copied < nresults)
{
    L.Push(DynValue.Nil);
    copied++;
}
Joy-less added a commit to Joy-less/moonsharp that referenced this issue Aug 15, 2023
Joy-less added a commit to Joy-less/moonsharp that referenced this issue Aug 15, 2023
@Joy-less
Copy link

Fix in #326

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants