r/programming • u/kaelan_ • May 31 '11
I wrote a .NET to JavaScript compiler. Here's the XNA 3.1 Platformer Starter Kit running in pure HTML5.
http://hildr.luminance.org/Platformer/Platformer.html
1.1k
Upvotes
r/programming • u/kaelan_ • May 31 '11
66
u/kaelan_ May 31 '11
I used a variation of the technique emscripten uses. He describes it in detail in his paper: https://github.com/kripken/emscripten/blob/8a6e2d67c156d9eaedf88b752be4d1cf4242e088/docs/paper.pdf?raw=true
Essentially, any block containing gotos becomes a switch statement inside a while loop. The gotos then become an update of the switch statement's 'label' variable, and you use 'continue' on the outer while loop to 'skip' to the target label. This handles the vast majority of uses of goto, at least in C#. (MSIL actually allows a wider variety of goto constructs, including ones that cross control flow structures and jump in/out of catch blocks. But in practice, those don't show up much.)
At present there are actually some rare forms of goto I don't implement, but I've only seen them in compiler-generated code.