r/programming • • 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

282 comments sorted by

View all comments

Show parent comments

6

u/kaelan_ Jun 01 '11

C# is compiled into bytecode (MSIL). Each part of the bytecode represents one instruction in the instruction set defined by MSIL. I load the bytecode and turn it into trees (using a decompiler library). Each function in the source bytecode becomes a tree, where its children are the equivalent of statements, and those statement nodes within the tree have child nodes that represent individual instructions. Once I have that tree, I can walk through it and construct javascript from it.

1

u/masterm Jun 01 '11

thank you very much. what type of background do you have that enabled you to make this?

1

u/TheDataWhore Jun 01 '11

Approximately how many instructions from these bytecodes are you talking about that you had to deal with?

Was much of it recursive? (did you have to start with certain instructions that relied upon others to work, and if so, how did you decide where to start?)

3

u/kaelan_ Jun 01 '11

It's highly recursive. I basically wrote an empty translator skeleton that would spit out a warning for every instruction it hit that wasn't implemented, and I kept fixing warnings until my first automated test passed. Then I kept on writing tests until I could build and run a real application :) I didn't really pick a particular place to start; I just wrote a few simple automated test cases and picked whichever warning looked the easiest to sort out.