D to Javascript converter (a hacked up dmd)
Adam D. Ruppe
destructionator at gmail.com
Sat Mar 3 08:49:21 PST 2012
On Saturday, 3 March 2012 at 16:29:25 UTC, Adam D. Ruppe wrote:
> There's no such thing as proper scoping in Javascript.
Let me expand a bit. consider this D:
if(1) {
int a = 3;
}
In JS, right now, that'd output:
if(1) {
var mangled_a = 3;
}
which, to the interpreter, is:
var mangled_a;
if(1) {
mangled_a = 3;
}
thanks to declaration hoisting.
If you want it to be scoped literally like D,
it'd look more like this:
if(1) {
function() {
var a = 3;
}();
}
I really doubt that'd help performance. Another option
is to reuse declarations:
if(1) {
int a = 0;
}
string a = "";
could become:
var a;
if(1) {
a = 0;
}
a = ""; // we know the old a is out of scope, so we'll reuse it
but I don't see a big benefit there either. Now, the JS
compiler will definitely see a as a dynamically typed var,
and may not be able to do the same optimizations it could
if it had a consistent type.
When you consider the bug-pronedness of me doing this instead
of relying on dmd's well debugged mangling process to do it
all for me, I really think it is a net loss, despite the
slightly more readable names.
Oh another thing: global variables in D are module scoped.
The mangledness again handles that for me. Just another
bug opportunity.
Perhaps a smarter helper tool could make pretty names, but
I don't want to put it in the compiler.
More information about the Digitalmars-d-announce
mailing list