D to Javascript converter (a hacked up dmd)

Adam D. Ruppe destructionator at gmail.com
Thu Mar 1 11:23:18 PST 2012


On Thursday, 1 March 2012 at 18:04:35 UTC, Ary Manzana wrote:
> 6. Javascript objects have some built-in properties that are 
> different from D. So implementing those in D would make their 
> performance worse (but you can always hard-code those functions 
> into the compiler and translate them directly to their JS 
> equivalent).


It's not so bad. In my zip, src/test/jslang provides bindings
to these things, which generate no code to use.

In src/test/std, I implement some Phobos interfaces this way.
(Run with dmd -inline and they generate no extra code.)

module std.string;

import jslang.string;

string replace(string a, string b, string c) {
    auto s = cast(JSString) a; // cast just to appease D
    return s.replace(b, c); // uses the built in method
}


...come to think of it, I think that's wrong. replace() in JS
only does the first occurrence iirc. D expects them all. But,
we could do:


import jslang.string;
import jslang.regexp;

string replace(string a, string b, string c) {
    auto s = cast(JSString) a; // cast just to appease D
    return s.replace(new RegExp(b, "g"), c); // uses the built in 
method
}

or something like that.




But, none of this is hard coded in the compiler. You can
call javascript functions just like anything else, given
a prototype.

Object eval(...); // don't bother typing the arguments if you 
don't want to


More information about the Digitalmars-d-announce mailing list