View mode: basic / threaded / horizontal-split · Log in · Help
March 02, 2012
Re: D to Javascript converter (a hacked up dmd)
Am Thu, 01 Mar 2012 21:13:41 +0100
schrieb Bystroushaak <bystrousak@kitakitsune.org>:

> I've played with gdc, but it's pretty complicated. I have few ADM 5120
> routers with debwrt and it would be nice to be able compile D2 code
> there, but so far, it was just fail.
> 

ADM 5120 is MIPS, right? Are you trying to build a cross compiler or a
compiler running on the MIPS itself? Making a cross-compiler is never
easy. Assuming you already have a C cross-compiler, you have to patch
the build process to include D at some point. You could also try
https://bitbucket.org/goshawk/gdc/wiki/crosstool-ng
but then you need a compatible 'sysroot' (a folder including the basic
libraries & headers as on your target system, must include libc &
some kernel headers, afaik) for your router.

Another issue: druntime/phobos might not compile (as long as
debwrt uses glibc it could work though), so configure with
--disable-phobos to disable both druntime and phobos. You'll only get
the compiler this way.

And MIPS is probably also affected by GDC issue 120, so you have to
configure like this:
DFLAGS="-fno-section-anchors" ./configure [configure arguments here]
March 02, 2012
Re: D to Javascript converter (a hacked up dmd)
On 02/03/2012 02:28, Adam D. Ruppe wrote:

> 3) The JS language doesn't have block scoping like D.

Have you considered faking scopes in JS using anonymous functions?

----
var foo;

(function(a, b){
	var foo; // not the same as the foo outside the func!
	// do stuff
}(x, y)); // invoke immediately with needed arguments from outer scope

----

by passing all the parameters to the anon func you get a speed increase 
in the lookup on most JS platforms.

I don't think they are very pretty, but it is sometimes a useful technique.

A...
March 02, 2012
Re: D to Javascript converter (a hacked up dmd)
"Jacob Carlborg" <doob@me.com> wrote in message 
news:jipus9$v81$1@digitalmars.com...
> On 2012-03-02 03:28, Adam D. Ruppe wrote:
>> On Thursday, 1 March 2012 at 08:01:07 UTC, Daniel Murphy wrote:
>>> Web programming... might actually become non-horrible?
>>
>> As far as I can tell, we have variable lifetime! (JS's garbage
>> collector is still a question, and some of the variables are
>> global - dmd loves the (var a, b) syntax, which is invalid so
>> I made var a in global scope. It is not set to null when done.)
>
> What do you mean with "dmd loves the (var a, b) syntax, which is invalid 
> so"?
>

CommaExp's containing Declarations
March 02, 2012
Re: D to Javascript converter (a hacked up dmd)
On 2012-03-02 10:57, Daniel Murphy wrote:
> "Jacob Carlborg"<doob@me.com>  wrote in message
> news:jipus9$v81$1@digitalmars.com...
>> On 2012-03-02 03:28, Adam D. Ruppe wrote:
>>> On Thursday, 1 March 2012 at 08:01:07 UTC, Daniel Murphy wrote:
>>>> Web programming... might actually become non-horrible?
>>>
>>> As far as I can tell, we have variable lifetime! (JS's garbage
>>> collector is still a question, and some of the variables are
>>> global - dmd loves the (var a, b) syntax, which is invalid so
>>> I made var a in global scope. It is not set to null when done.)
>>
>> What do you mean with "dmd loves the (var a, b) syntax, which is invalid
>> so"?
>>
>
> CommaExp's containing Declarations

Is he saying "var a, b, c;" is invalid JavaScript code?

-- 
/Jacob Carlborg
March 02, 2012
Re: D to Javascript converter (a hacked up dmd)
"Jacob Carlborg" <doob@me.com> wrote in message 
news:jiq8kb$1ht0$1@digitalmars.com...
>>
>> CommaExp's containing Declarations
>
> Is he saying "var a, b, c;" is invalid JavaScript code?
>
> -- 
> /Jacob Carlborg

It's invalid if it's a comma expression.

eg.

auto x = (auto a = 3, a);

Which dmd creates _everywhere_.
March 02, 2012
Re: D to Javascript converter (a hacked up dmd)
On Friday, 2 March 2012 at 05:58:03 UTC, Nick Sabalausky wrote:
> You make it sound as if there's another kind of JS.

Horribly inefficient relative to the environment :P

I just don't want 100 kb of crap to download just to
write a hello world.

A lot of people prefer to use gzipped sizes, but I don't
because the browser still has to parse the whole thing,
and that takes time after the download, even if you don't
actually run any of it.

(On some browsers, it parses it again on every page load!)


This generated D code gzips very, very well, but I still
want to keep it small in general for general speed.

The fastest code is no code at all. Less to parse, less
to run.
March 02, 2012
Re: D to Javascript converter (a hacked up dmd)
> I think once you get past the horror that is building gcc for your target
> platform, the big issue remaining is that druntime needs to be ported.

There is actually gdc package in debwrt, but it is D1 :(

> You'll hit the same issue with a D->C translator, so it might be better just
> to work on the actual runtime.

Well, I don't consider myself as enough skilled programmer to work at gdc.
March 02, 2012
Re: D to Javascript converter (a hacked up dmd)
On Friday, 2 March 2012 at 09:33:44 UTC, Alix Pexton wrote:
> Have you considered faking scopes in JS using anonymous 
> functions?

Yeah, though right now I'm trusting the D compiler to
keep it straight, and it is doing a pretty good job
while being really simple to code.

What happens is all the local variables are mangled
with respect to their full scope too (which leads to
ridiculously long, but unique names).

The D scope simply outputs its own code:

D:
 { // create a scope
    int a = 10;
 }
  int a = 20; // the old a is out of scope, so we can do this

Javascript:
    var _D_inner_a = 10; // not a real var name, just demo
    var _D_outer_a = 20; // new var


The inner variable is still in scope, so I doubt Javascript
garbage collects it, but thanks to the rules of D, it is
never used again.

But, if you use a struct with a destructor (or scope(exit)),
it generates this:

D:
  {
    MyStruct a;
  }
   MyStruct a;

Javascript:

   try {
     var _D_inner_A = new MyStruct();
   }
   finally {
        MyStruct_destructor(_D_inner_A);
   }
   try {
      var _D_outer_A = new MyStruct();
   }
   finally {
       MyStruct_destructor(_D_outer_A);
   }



And, in this case, Javascript's bizarre scoping
actually simplifies things.


In Javascript, when you write "var x = y;" in the
middle of a function, the way it really works is
it adds a "var x;" to the top of the function,
and leaves "x = y;" in hte middle.


You write:
function test() {
    if(something_here()) {
       var a = 10;
    }
    a++;
}

Javascript acts as if you wrote:

function test() {
   var a;
   if(something_here()) {
         a = 10;
   }
   a++;
}


With our try/finally (that dmd generates, mostly; I didn't
have to do much to make this work), the var declartion
in the try being hoisted up top means we can be sure it
works correctly in the finally, too.




But, with regard to the D scoping, dmd mangles the names
to make sure you don't reference anything that's out of
scope accidentally by giving them unique names.




This hoisting behavior is the proper solution to the
CommaExp declarations mentioned previously, where
dmd likes to write: foo( (var a = 10, a) ) and the
sort.


Currently, I put the var a; in global scope, leaving
just (a=10,a) in the expression. This is legal Javascript,
and should be ok due to unique names, but it pollutes the globals.


If I add a way to instead put those var declarations in
function scope, it is cleaner - no globals - and the same
thing Javascript itself would do anyway!


I'll have to redo the string buffering to make that though,
so it will take some code to do it. Dumping it in the global
was simple to implement with what was already there.

> I don't think they are very pretty, but it is sometimes a 
> useful technique.

heh, same with mangled names :)
March 02, 2012
Re: D to Javascript converter (a hacked up dmd)
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.
March 02, 2012
Re: D to Javascript converter (a hacked up dmd)
> ADM 5120 is MIPS, right? Are you trying to build a cross compiler or a
> compiler running on the MIPS itself? 

Compiler running on the MIPS.

> And MIPS is probably also affected by GDC issue 120, so you have to
> configure like this:
> DFLAGS="-fno-section-anchors" ./configure [configure arguments here]

Thanks.
4 5 6 7 8 9 10 11 12
Top | Discussion index | About this forum | D home