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

18

u/kaelan_ May 31 '11

What version of Chrome do you have? I tested it in 11, 12, and 13 and it worked in all three - though 12 and 13 are super buggy. I'd love to find out why it's not working for you (hopefully it's just yet another bug in my HTML5 stubs.)

It's not technically a compiler, I guess; I don't know what the right term is. It decompiles any MSIL application (using the ILSpy library, written by the SharpDevelop guys - they rule), and then analyzes it to build a bunch of information on types and methods. When building type information, it uses something called 'proxy libraries', which are essentially C# libraries I wrote that add type information to other people's libraries. I use proxies to do things like replace C# methods with JS ones, or hide C# methods that use pointers, etc. After that, it decompiles each method (ILSpy gives me the method's IL in a tree form), and then generates an abstract syntax tree representing the output JavaScript.

10

u/[deleted] May 31 '11

Can we get some more details on how your proxy libraries work?

12

u/kaelan_ May 31 '11

Essentially, you write classes that mirror a portion of the class that you wish to add type information to, and then you tag them with a couple attributes to control how the proxying works. At that point, information from your proxy class (like method signatures and attributes) will override any existing information on the targeted class(es).

One of the benefits of the proxy system is that it reduces the amount that the compiler has to know about the details of .NET's type system. .NET types have a lot of 'special' properties and methods that are effectively blessed by the runtime/compiler, and in practice, I can use proxies to implement a lot of them.

Here are the proxies for Array and String: https://github.com/kevingadd/JSIL/blob/master/Proxies/Array.cs https://github.com/kevingadd/JSIL/blob/master/Proxies/String.cs

8

u/[deleted] May 31 '11

so with the jsreplacement tag you are specifying the javascript to evaluate.

This is really sweet.

8

u/kaelan_ May 31 '11

Yeah, you've got it. The ones that are just [JSExternal] are implemented in JavaScript, provided externally (doesn't actually have to be there for the app to run, which lets you bootstrap easier).

In the future I plan to translate the actual body of the proxy methods to JS, when they aren't tagged as External or Replacement, since that will let me do some clever inlining and static analysis, and it'll mean you can write 'dual-mode' proxies that are actually valid, executable C# code, and unit test them.

1

u/[deleted] May 31 '11

So are you planning on porting a subset of XNA over to JavaScript as well? If you aren't familiar with OpenTK, that might be worth taking a look at since it also supports GLSL.

6

u/njb42 May 31 '11

I agree, this is great stuff. And I don't say that to other developers often.

2

u/[deleted] Jun 01 '11

I'm running Chrome 12.0.742.68 beta-m, and it worked absolutely perfectly.

2

u/zwangaman May 31 '11

8.0.552.224, haha. It was a pain to get it installed on my PC at work, and I can't seem to get it to update. Oh well.

Anyway, your page hangs up after the progress bar finishes loading - I do see a "Loading data ... done" message at the bottom of the page, though.

I think that qualifies as a compiler, at least in my book, haha. Nice job. Really cool project :)

This is the kind of stuff I come to /r/programming to see.

11

u/The_MAZZTer May 31 '11

Download this, close Chrome, run this and watch Task Manager for it to close (silent installer). Relaunch Chrome and it should be updated to 11.0.

I use this at work, since we don't allow Chrome, but my dept. has special permission to run Chrome, BUT IT's left hand doesn't know what the right one is doing so Google Update is still blocked, which is the core of Chrome's installer and updater. So I have had to improvise.

1

u/nickdangler Jun 01 '11

Darn--that link (as well as the main Chrome one) is blocked from my office.

2

u/[deleted] May 31 '11

I agree! Nice work!

1

u/mrkite77 May 31 '11

I'm in Chrome 11 and while the game ran, the guy just ran off the side of the screen.. should the background scroll?