Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it likely ever to be possible to compile from D 2.0 to Javascript?

Long-term C++ programmer (that's me) exploring other languages and is wondering whether it is worth learning much about D 2.0. (That is the clean slate re-write of D.) So far, I like what I see - many pragmatic, wise choices. Now, I want to learn a little more and want to know whether it is likely ever to be possible to do the following:

  1. Write some code in D 2.0. (Maybe it is a well-defined subset of D 2.0.)
  2. Compile the code from 1.
  3. Receive Javascript source code from the compiler.

In summary, I would like to have a D 2.0 compiler with a backend capable of generating Javascript.

So my question has two parts:

  1. Does such a compiler exist already?
  2. If such a compiler does not exist, and if I chose to implement one, where would be the best place to start?

For context, the reason that I would like the above is so that I could implement a bunch of business logic in D 2.0, and then use that business logic in a pure Javascript (that is no Flash/Silverlight/Java) Web Application.

Update

I have had three answers, two of which are on-topic, and one that is not and is therefore ignored.

@FeepingCreature reckons that it will never happen because "it's too incongruous with D's targeted environment". I am interested to know what that means, and have asked.

@Nekuromento , in contrast, points out that it is already theoretically possible using LDC, followed by the LLVM Javascript backend. I checked out both links. LDC support for D 2.0 looks like a work in progress. But I would guess it will get there at some point. The LLVM Javascript is also not optimised or minified, but it looks from the demos that they work. So I think that is enough to be useful at the very least.

Update 2

OK - this seems to be the best solution to date.

  1. Invoke LDC. (@Nekuromento) LDC combines the Digital Mars front-end with an LLVM backend.
  2. Pipe the output from 1. through the LLVM Javscript backend. (@Nekuromento)
  3. Pipe the output from 2. though Closure (@BCS)

I will choose a best answer fairly soon, but I would like some more-informed opinion really.

Update 3

Some more information on why I want to do this.

Here is a concrete example of one project that I wish to implement. It will be a geometric modelling library, probably written by just me, taking several years to write, and ending up at about 100,000 lines of code. I would like it to contain as close to zero bugs as possible, and, therefore, I do not wish to implement it in Javascript. I would like it to run on the server, on the desktop, and in the browser too. (By in "in the browser" I mean without the assistance of any plugin such as Flash, Silverlight or Java.) The truth is that code in the browser running on Javascript is here to stay for a while. Projects will increasingly need a strategy to deal with that.

Update 4

I was hoping for some more answers, but didn't get them. Ah well, never mind. Thanks for all the feedback, including @dsimicha 's stimulating comments. (I hope it is OK to disagree technically sometimes, while at the same time keeping things civil.) I have accepted @Nekuromento 's answer.

Update 5 2018-07-18

Seven years on, LDC v1.11 supports compiling and linking to WebAssembly.3

like image 529
Paul Delhanty Avatar asked Mar 27 '11 10:03

Paul Delhanty


1 Answers

Such a compiler currently does not exist, but there is some work on javascript LLVM-backend.

As there is LDC. It uses DMD front-end and LLVM, so theoretically one can add javascript output to it.

like image 148
Nekuromento Avatar answered Oct 21 '22 09:10

Nekuromento