D to Javascript converter (a hacked up dmd)

Adam D. Ruppe destructionator at gmail.com
Sat Mar 3 16:27:41 PST 2012


On Sunday, 4 March 2012 at 00:05:19 UTC, James Miller wrote:
> Hmm, does your DOM library compile with this?

No. I want do a port of some of the functions to it,
though.

(Even if I fix it to not use goto, so it compiles to JS,
I don't actually need a parser in the browser - it should
use native methods when possible. So, my strategy will
be to port convenience functions instead of the whole thing.)

For Javascript dom stuff, I'm generally happy enough
with the browser's api alone, but my little js file
ports some of my convenience functions:

addChild
insertAfter
prependChild
hasClass
toggleClass
removeClass
addClass
getParent
appendText
replaceText
removeFromTree


All as free functions in Javascript. Together with what
the browser already has, that covers everything I want,
well, except for dataset which is beautiful on the server,
but meh on the client since you have to use getAttribute
for compatibility. But, that's not a big deal.

I didn't port most of my form functions in dom.d because
I just don't do that kind of stuff client side anyway.

(Other stuff in my js lib are some functional programming
helpers and an event compatibility set.)



But, for the D->JS DOM additions, these functions are
pretty much what I want. My strategy I'm considering
is to make a separate document module, that replaces
the browser.document.

Using alias this magic, I'll make extension classes that
are compatible with the real thing, without any runtime
wrapping.

It'll be like a homemade UFCS:

final class BetterElement {
     JSElement __js_this;
     alias __js_this this;

     BetterElement addClass(string c) {
          className ~= " " ~ c;
     }
}

/* do the same for document */

auto be = document.querySelector("#foo > p");
be.addClass("test");

When this is compiled, it comes out as:

function addClass(c, d_this) {
    d_this.className += " " + c;
}

var be = document.querySelector("#foo > p");
addClass("test", be);




Which is about as minimal as you can get in JS (it's
about the same as I wrote by hand), while keeping
the beautiful syntax in D.




Anyway, bottom line is my goal is indeed to have a
compatible version of my dom library for D->JS too, but
it will have almost a completely separate implementation
to be more efficient in the browser environment.


More information about the Digitalmars-d-announce mailing list