D to Javascript converter (a hacked up dmd)

Adam D. Ruppe destructionator at gmail.com
Fri Mar 2 09:20:20 PST 2012


Here's one of the nicer things to do:

http://arsdnet.net/dtojs/game.d
http://arsdnet.net/dtojs/game.html

we can do little browser games in D.

If you look at game.js:
http://arsdnet.net/dtojs/game.js

you can see it is similar in size to the original
D file (this is after running tools/gcfunctions and
tools/mangledown on it; before them, it was 21 KB).

I just quickly whipped this together, so it isn't
much of a game - you just move your thing around.
Use keys a,d, and w to control it.


Not much to it, and writing this in Javascript would
have been easy enough, but even for this trivial thing,
a few D nicieties came through:

1) I typo'd one of the method names. D caught it instantly.

2) The degrees thing is supposed to be a user defined literal..
    but it didn't actually work like that (it outputted functions
    that returned the literals!) I consider this a bug in the 
converter.

3) The click event handler is a little more succicent than the
    same in Javascript would be.

4) The switch. Two nice features of D here that JS lacks:

    a) "case 'A':" comes out as "case 65:". The key code is a
       number, which is not equal to 'A' in Javascript!

       A lot of sites have tables for this, but it is just ascii,
       so 'A' works well. In D.

    b) the comma on case is a bit shorter than the same in JS.
       D is better at this short, dynamic stuff than JS itself
       if you ask me - just look at the beauty of my server side
       dom.d compared to JS.




But, it also shows that D->JS isn't a magic bullet.


1) This won't work on IE8, since it doesn't have the canvas thing
    nor addEventListener. (The latter could be emulated in a 
library,
    though, with exactly the same syntax too.)

2) It runs smoothly on IE9, but sucks my butt on Firefox 9 on the
    same computer (my little laptop).

    It is still javascript when it runs, so speed boosts are 
limited
    by that; if the browser is slow, using D won't help much.

3) Obviously, it won't magically turn on people's javascript 
either.
    But, same language client+server means it is that much less
    work to make nice fallbacks (e.g. proper validation).

BTW speaking of validation, I'm pondering if scope(failure)
could make elegant code for that...



But, hey, I'm calling it a win. The suckage of browsers
is a reality regardless of the language.



More information about the Digitalmars-d-announce mailing list