Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CPS compiler for coroutine implementation

I used to work on IronLua in my spare time. Lexing and parsing is currently done. I kind of stopped working on it out of frustration since implementing Lua coroutines in .NET without resorting to dirty threading hacks is not easy. This is tied to how I compile Lua functions, and it's a problem I need to solve early while designing the compiler.

I've been researching coroutine implementations, and it turns out that my initial feelings about continuations were correct.

Since coroutine creation, yield and other operations are not language keywords, but functions in the "coroutine" table, I cannot statically switch to CPS-style compilation as the coroutine table might have been overwritten by a previous script. While I understand that scripts overwriting the coroutine table are a rare occurrence, I'd like to be on the safe side and approach the issue as cleanly as possible.

My plan is to use continuation-passing style for every expression, no matter if we're in a coroutine or not. Everything would be followed by a continuation.

Besides the obvious difficulty of writing a compiler in the first place, and adding CPS transform on top of it, I'm troubled by this design decision and its performance implications.

I'm looking for advice about Lua coroutine implementation in .NET.

Thanks for your time.

like image 330
Raine Avatar asked Nov 14 '22 21:11

Raine


1 Answers

I'm not familiar with the details of lua co-routines. But I think the only way you can get co-routine support on a similar level to what lua itself supports requires that all local variables are allocated on the heap in some form.

Your problem isn't only that the coroutine functions might be replaced, but that any lua function you call might yield.

You should also look into the async CTP which implements very similar behavior in C#. The main difference I see is that you need to make all methods async, while the C# feature is opt-in.

If your .net integration is good(and I expect it to be good if build on the DLR) then I wouldn't worry to much about performance. It's easy to write the performance critical parts in C# and use lua for glue code.

like image 136
CodesInChaos Avatar answered Dec 17 '22 04:12

CodesInChaos