Guidance needed on C# and Lua integration

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Guidance needed on C# and Lua integration

Andrew Starks
We are working on a project where Lua is the middle-wear that binds to
other languages. We do quite a bit of our application programming in
C# and our first binding to it.

Our current workflow is Lua 5.3 -> Managed C++ -> C#, but as my
colleague put it today, "We are interested in binding our library to
C#, not all of Lua to C#." Therefore, we are looking around.

It looks to me as though nLua[1] (formerly LuaInterface) is the
leading way to use Lua and C#. Many of the examples show Lua in the
scripting role, but it appears that it may work well in both
directions.

Some considerations:

1: We need Lua to be able to load C and Lua libraries with require.
2: We also need to be able to call delegates (and most likely other
objects) from Lua, but mostly C# is calling into Lua objects (I'm
using penlight's objects).
3: We use Lua 5.3
4: C#/Windows is not the only target language/platform.

Does anyone have experience with this or something similar? I'm
curious if anyone has tried using the "unsafe" keyword and just bound
Lua to C#, without an extensive wrapper. I'm also curious about any
alternatives.

I didn't see any 5.3 issues open on github, so I assume that it is not
on their map. I don't want to loose 64bit integers, but I am also
unsure of where the greatest uncertainty / pain lies, so I don't know
anything about whether forking the project to work with 5.3 is better
or dropping 5.3 is better... That question is better question for the
authors of the project, however. I mention it here, in case experience
has already had its say with someone here.

A JSON very-high-level binding is still being considered, but:
Pros:
 - The binding would be dead simple.
 - Other languages (especially Javascript) would use this, as well.
 - This is pretty much done, but using it in C# is not *that* easy. It
feels a lot like writing the whole library again.
Cons:
 - we think that this will lead to too much maintenance.
 - we'd have to re-implement the Lua object model that we've already
made in C#. (We may end up having to do that with nLua, too)
 - Increased probability of low performance, or at least limited
future options.
 - A proper integration would increase the possibility of extending
our library with C# delegates (uber-function pointers).

-Andrew

[1] https://github.com/NLua

Reply | Threaded
Open this post in threaded view
|

Re: Guidance needed on C# and Lua integration

Josh Simmons
On Tue, Apr 21, 2015 at 10:40 AM, Andrew Starks <[hidden email]> wrote:

> 4: C#/Windows is not the only target language/platform.

I'm pretty sure you still need to use P/Invoke if you want to do
native interop on Mono.

>
> Does anyone have experience with this or something similar? I'm
> curious if anyone has tried using the "unsafe" keyword and just bound
> Lua to C#, without an extensive wrapper. I'm also curious about any
> alternatives.
>

I'm not sure what you mean by "just" but once you have something like this
https://github.com/jsimmons/LuaSharp/blob/master/LuaWrap/LuaLib.cs (5.1)
you can do whatever you like and have it run on all platforms. More
automated things obviously require more magical stuff on the part of
the C# library.

Reply | Threaded
Open this post in threaded view
|

Re: Guidance needed on C# and Lua integration

Andrew Starks
On Mon, Apr 20, 2015 at 8:04 PM, Josh Simmons <[hidden email]> wrote:
> On Tue, Apr 21, 2015 at 10:40 AM, Andrew Starks <[hidden email]> wrote:
>
>> 4: C#/Windows is not the only target language/platform.
>
> I'm pretty sure you still need to use P/Invoke if you want to do
> native interop on Mono.

>>
>> Does anyone have experience with this or something similar? I'm
>> curious if anyone has tried using the "unsafe" keyword and just bound
>> Lua to C#, without an extensive wrapper. I'm also curious about any
>> alternatives.
>>
>
> I'm not sure what you mean by "just" but once you have something like this
> https://github.com/jsimmons/LuaSharp/blob/master/LuaWrap/LuaLib.cs (5.1)
> you can do whatever you like and have it run on all platforms. More
> automated things obviously require more magical stuff on the part of
> the C# library.
>

I guess I was approaching this differently than I would have
approached C++, but I shouldn't have. I don't program in C# and I'm
not writing the binding, so that's probably why I see it as "other".

>From looking at your repo (thank you!), it seems like it may be
similar to the choice that you face in C++:
1: basically use the stack as is, but wrap it in a simple "lua" object
2: using someone's library in an attempt to make using code in the
other language easier

I normally don't like the second approach, but i feel like it might be
pragmatic, in this case. I'm fuzzy about how to deal with C#'s garbage
collection, but that is almost certainly due to me knowing next to
nothing about C#. I have done enough reading and asking/listening to
know about P/invoke.

"just" referred to "just use the C API as-is, as much as possible."

Thank you!

-Andrew

Reply | Threaded
Open this post in threaded view
|

Re: Guidance needed on C# and Lua integration

Josh Simmons
On Tue, Apr 21, 2015 at 2:40 PM, Andrew Starks <[hidden email]> wrote:

>
> I guess I was approaching this differently than I would have
> approached C++, but I shouldn't have. I don't program in C# and I'm
> not writing the binding, so that's probably why I see it as "other".
>
> >From looking at your repo (thank you!), it seems like it may be
> similar to the choice that you face in C++:
> 1: basically use the stack as is, but wrap it in a simple "lua" object
> 2: using someone's library in an attempt to make using code in the
> other language easier
>

Essentially yes. In C# things are a bit more interesting because
without templates you're forced to generate the marshalling code at
runtime (short of using something like SWIG) which can cause issues.

It's also worth noting there's a decent middle ground if you just
abstract the stack operations behind something more language friendly
and still allow low level Lua API access for anything more involved.

(https://github.com/jsimmons/lcpp#example-codes An API along these lines)

Ultimately it depends on how you see your barrier APIs looking as to
what level of magic is appropriate.

> I normally don't like the second approach, but i feel like it might be
> pragmatic, in this case. I'm fuzzy about how to deal with C#'s garbage
> collection, but that is almost certainly due to me knowing next to
> nothing about C#. I have done enough reading and asking/listening to
> know about P/invoke.

Dealing with C#'s garbage collection is not too difficult if you just
keep a reference to a given object whilst the opposite side of the
interop layer also maintains one. Lua objects in C# can be represented
by a state pointer and lua_Ref. C# objects in Lua can be represented
by a generic userdata and a container of objects in C#.

You can obviously get more involved than that but that's the simplest
way to do things generically.


Cheers,
Josh.

Reply | Threaded
Open this post in threaded view
|

Re: Guidance needed on C# and Lua integration

steve donovan
In reply to this post by Josh Simmons
On Tue, Apr 21, 2015 at 3:04 AM, Josh Simmons <[hidden email]> wrote:
> I'm not sure what you mean by "just" but once you have something like this
> https://github.com/jsimmons/LuaSharp/blob/master/LuaWrap/LuaLib.cs (5.1)
> you can do whatever you like and have it run on all platforms. More
> automated things obviously require more magical stuff on the part of
> the C# library.

I always feel guilty when C#/Lua comes up, because I maintain MonoLuainterface.

The rather cool aspect of the 'LuaInterface' part is that reflection
makes the C# binding almost trivial.

There's no reason why external C shared libraries can't be pulled into
the mix using P/Invoke - a lot more straightforward than the JNI
needed for Java interop.

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: Guidance needed on C# and Lua integration

Matthias Kluwe
In reply to this post by Andrew Starks

Am 21.04.2015 02:41 schrieb "Andrew Starks" <[hidden email]>:
>
> We are working on a project where Lua is the middle-wear that binds to
> other languages.

I like that typo. The other layers could be called under-wear and suit...

Regards,
Matthias

Reply | Threaded
Open this post in threaded view
|

Re: Guidance needed on C# and Lua integration

Andrew Starks


On Tuesday, April 21, 2015, Matthias Kluwe <[hidden email]> wrote:

Am 21.04.2015 02:41 schrieb "Andrew Starks" <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;andrew.starks@trms.com&#39;);" target="_blank">andrew.starks@...>:
>
> We are working on a project where Lua is the middle-wear that binds to
> other languages.

I like that typo. The other layers could be called under-wear and suit...

Regards,
Matthias


Much creativity stems from my dyslexia. :)

-Andrew

(Dyslexia is not primarily "wrong word" but it is an indirect consequence. Or at least I'm going to blame it on that. :) )