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

Adding properties to ExpandoObject via Lua? #519

Open
rbwhitaker opened this issue Jan 15, 2024 · 3 comments
Open

Adding properties to ExpandoObject via Lua? #519

rbwhitaker opened this issue Jan 15, 2024 · 3 comments

Comments

@rbwhitaker
Copy link

I might just be misunderstanding something. My Lua is extremely rusty, and my experience with NLua is virtually zero.

This code doesn't have the behavior I'd expect:

using System.Dynamic;
using NLua;

Lua state = new ();
dynamic x = new ExpandoObject();
x.a = 5;
state["x"] = x;

// Run embedded Lua.
state.DoString("""
    print(x.a)
    x.a = 10
    print(x.a)
    """);

This makes a dynamic/ExpandoObject in C# and adds it as a (global?) variable. Lua is able to see x.a (the print(x.a) shows 5). But on the assignment line, with x.a = 10, I get the following error:

NLua.Exceptions.LuaScriptException: 'field or property 'a' does not exist'

It seems like it is available for reading but not writing.

This variation worked fine, which makes me think I'm not just making a Lua syntax error.

using NLua;

Lua state = new ();
state.DoString("""
    x = {}
    print(x.a)
    x.a = 10
    print(x.a)
    """);

This displays nil at first, and then 10, as I'd expect.

I can't tell if this is a bug or if I'm just missing something obvious. I'd expect that I could send an ExpandoObject over to Lua and have Lua be able to modify its values, not just see the current ones.

I'd love some advice or guidance from people more experienced with Lua and NLua.

@viniciusjarina
Copy link
Member

I am not sure what you trying to achieve. But NLua will try to use reflection. I don't think it will work with ExpandObject. But why not use a Lua object directly ?

@rbwhitaker
Copy link
Author

Because I need the interop. The object must also be useable in C#, and actually begins life on the C# side. ExpandoObject definitely feels easy on the C# side, and something that should just work on the Lua side, since Lua is a dynamically typed language. This paradigm works fine in IronPython, for what it's worth. So there isn't some platform limitation or anything.

@viniciusjarina
Copy link
Member

I see. I guess it would be doable if we can dynamically invoke properties/methods in the dynamic object. NLua will try to reflect over the CLR object directly, but in theory it could be fixed changing NLua to check if is a dynamic object.
As workaround I guess you could invoke a helper method and pass the name of the property and the value to C# and just set to the ExpandoObject using the the IDictionary interface.

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