Skip to content

Try read LuaValue#269

Merged
akeit0 merged 2 commits into
nuskey8:mainfrom
ashtonmeuser:try-read-lua-value
Mar 20, 2026
Merged

Try read LuaValue#269
akeit0 merged 2 commits into
nuskey8:mainfrom
ashtonmeuser:try-read-lua-value

Conversation

@ashtonmeuser
Copy link
Copy Markdown
Contributor

Return the original ref when calling LuaValue.TryRead<LuaValue>. This is useful for generics where T may be LuaValue.

For example, the consider the following readonly LuaTable that reads from a backing IReadOnlyDictionary<TKey, TValue>. This works as is in all cases except when the key or value type is LuaValue itself.

public static LuaTable ReadOnlyDict<TKey, TValue>(IReadOnlyDictionary<TKey, TValue> backing)
{
    var proxy = new LuaTable();
    var metatable = new LuaTable();

    metatable[Metamethods.Index] = new LuaFunction("__index", (context, ct) =>
    {
        var keyValue = context.GetArgument(1);
        if (!keyValue.TryRead<TKey>(out var key) || !backing.TryGetValue(key, out var value))
            return new(context.Return(LuaValue.Nil));

        return new(context.Return(LuaValue.FromObject(value)));
    });

    metatable[Metamethods.NewIndex] = new LuaFunction("__newindex", (context, ct) =>
        throw new LuaRuntimeException(context.State, $"readonly table: cannot assign key '{context.GetArgument(1)}'"));

    metatable[Metamethods.Metatable] = "readonly";
    proxy.Metatable = metatable;

    return proxy;
}

P.S. I intend to make a PR with some of the collection wrappers I've created like the one above.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates LuaValue.TryRead<T> so that requesting T == LuaValue succeeds and returns the original LuaValue instance, enabling generic code paths where T may itself be LuaValue.

Changes:

  • Add a fast-path in LuaValue.TryRead<T> for T == LuaValue that returns this without conversion.
  • Add tests covering TryRead<LuaValue> (including Nil), Read<LuaValue>, and reference preservation for TryRead<LuaTable>.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Lua/LuaValue.cs Adds T == LuaValue special-case in TryRead<T> to return the original value.
tests/Lua.Tests/LuaValueTests.cs Adds unit tests validating the new TryRead/Read behavior and table identity behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@akeit0 akeit0 merged commit f86229e into nuskey8:main Mar 20, 2026
4 checks passed
@akeit0
Copy link
Copy Markdown
Collaborator

akeit0 commented Mar 20, 2026

Merged! Thanks!

@ashtonmeuser ashtonmeuser deleted the try-read-lua-value branch March 30, 2026 17:02
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

Successfully merging this pull request may close these issues.

3 participants