D to Javascript converter (a hacked up dmd)

Robert Clipsham robert at octarineparrot.com
Thu Mar 1 08:29:09 PST 2012


On 01/03/2012 05:20, Adam D. Ruppe wrote:
> I probably should have been working today.... but instead
> spent a good amount of time on this again.
>
> New zip:
>
> https://github.com/downloads/adamdruppe/dtojs/dtojs-0.3.zip
>
> (I haven't cleaned up the github fork yet)
>
> To give you an idea of what is working, check out
> my tests.d file:
>
> http://arsdnet.net/dtojs/tests.d
>
> This compiles (raw) into this:
> http://arsdnet.net/dtojs/tests.js
>
> BTW, when compiling, be sure to put test/object.d
> on your command line too, or the runtime functions
> won't be present in your .js file!
>
> 23 KB (down to 11 KB if you run the included mangledown tool
> on it. gcfunctions can often make a big difference, but not here,
> since it removes what you don't use. A test that doesn't use
> something is an incomplete test!)
>
>
> I think I broke std.algorithm again :(, but fixed a lot of
> other stuff. (it is the lambda generator in std.algorithm that
> is breaking. The JS has the function, but it is nested too deep
> and inaccessible to the actual algorithm.)
>
>
>
> But, a lot of stuff works now, and the generated code isn't
> half bad. I've started using more static type info to slightly
> optimize it too, but only some basics.
>
>
> One of the messiest things was try/catch. JS doesn't do
> types like D, so I implemented it with a series of
> if(dynamic_cast()), and ultimately rethrow if none matched.
>
> A bit ugly, but it seems to pass the tests!

This is really cool! It would be quite interesting to run the dmd 
testsuite through it and see how much passes. Same goes for phobos 
unittests (obviously both of these would fail for system stuff like 
inline asm...)

I'd also be interested in some benchmarks (eg std.algorithm.sort vs 
Array.sort), but that seems a bit pointless really. Might be interesting 
to throw in some optimisations when translating though (though you're 
doing some of these already!)

Interesting idea:

version(JavaScript) asm
{
     /* Well... it's not really asm, but it's
        as close as you could get with a javascript backend :p */
     document.write("Oh look, inline javascript D-:");
}

Perhaps it would be better to have a dedicated function for that though.

-- 
Robert
http://octarineparrot.com/


More information about the Digitalmars-d-announce mailing list