www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - NaCl/Emscripten

reply Manu via Digitalmars-d <digitalmars-d puremagic.com> writes:
I'm looking at another potential opportunity to get D into the office,
but the target's for this particular project are NaCL and/or
Emscripten.

I was gonna start hacking around to see what the limitations are with
Emscripten on D code tonight. Has anyone done any serious
investigation here?

NaCl is a more useful target, but I think that will rely on a special
build of LDC... has there been discussion about that before? Can any
of the LDC guys chime in on the possibility?
Jan 09 2015
next sibling parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Friday, 9 January 2015 at 09:28:22 UTC, Manu via Digitalmars-d 
wrote:
 I was gonna start hacking around to see what the limitations 
 are with
 Emscripten on D code tonight. Has anyone done any serious
 investigation here?

 NaCl is a more useful target, but I think that will rely on a 
 special
 build of LDC... has there been discussion about that before?
I have read the standard on ASM.js and some on NaCl. I think you will run into trouble with the GC on both NaCl and ASM.js. If you don't target ASM.js, then you probably could a completely new backend for D that use the standard javascript collector and still get some of the benefits. Remember that only Firefox support ASM.js.
Jan 09 2015
parent reply Manu via Digitalmars-d <digitalmars-d puremagic.com> writes:
On 9 January 2015 at 19:51, via Digitalmars-d
<digitalmars-d puremagic.com> wrote:
 On Friday, 9 January 2015 at 09:28:22 UTC, Manu via Digitalmars-d wrote:
 I was gonna start hacking around to see what the limitations are with
 Emscripten on D code tonight. Has anyone done any serious
 investigation here?

 NaCl is a more useful target, but I think that will rely on a special
 build of LDC... has there been discussion about that before?
I have read the standard on ASM.js and some on NaCl. I think you will run into trouble with the GC on both NaCl and ASM.js. If you don't target ASM.js, then you probably could a completely new backend for D that use the standard javascript collector and still get some of the benefits. Remember that only Firefox support ASM.js.
I can probably get by with nogc. There's no threading in asm.js either, so that might be a spanner. Maybe -betterc would be usable out of the box...
Jan 09 2015
next sibling parent "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Friday, 9 January 2015 at 10:10:36 UTC, Manu via Digitalmars-d 
wrote:
 I can probably get by with  nogc. There's no threading in asm.js
 either, so that might be a spanner.
You have worker threads, so you would have to use message passing, but I believe you can transfer "byte heaps" by reference between threads in a unique_ptr style (so only one thread can access it at the same time): https://developer.mozilla.org/en-US/docs/Web/API/Transferable Maybe it is possible to get the GC to work too, but it sounds like a lot of work for nothing IMO.
Jan 09 2015
prev sibling parent "Tobias Pankrath" <tobias pankrath.net> writes:
 I can probably get by with  nogc. There's no threading in asm.js
 either, so that might be a spanner. Maybe -betterc would be 
 usable out
 of the box...
You could compile your D code to LLVM IR using ldc2 --output-ll and feed that to emcc to see how far you get.
Jan 09 2015
prev sibling next sibling parent "deadalnix" <deadalnix gmail.com> writes:
On Friday, 9 January 2015 at 09:28:22 UTC, Manu via Digitalmars-d 
wrote:
 I'm looking at another potential opportunity to get D into the 
 office,
 but the target's for this particular project are NaCL and/or
 Emscripten.

 I was gonna start hacking around to see what the limitations 
 are with
 Emscripten on D code tonight. Has anyone done any serious
 investigation here?

 NaCl is a more useful target, but I think that will rely on a 
 special
 build of LDC... has there been discussion about that before? 
 Can any
 of the LDC guys chime in on the possibility?
Both will need a special build of LDC, and runtime support. I know people made that work in the past, but I wouldn't expect the support to be great.
Jan 09 2015
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2015-01-09 10:28, Manu via Digitalmars-d wrote:
 I'm looking at another potential opportunity to get D into the office,
 but the target's for this particular project are NaCL and/or
 Emscripten.

 I was gonna start hacking around to see what the limitations are with
 Emscripten on D code tonight. Has anyone done any serious
 investigation here?

 NaCl is a more useful target, but I think that will rely on a special
 build of LDC... has there been discussion about that before? Can any
 of the LDC guys chime in on the possibility?
Don't know if there's any interest but Adam D. Ruppe has hacked DMD to output JavaScript. You should be able to find it somewhere on the newsgroup. -- /Jacob Carlborg
Jan 09 2015
parent reply "Mengu" <mengukagan gmail.com> writes:
On Friday, 9 January 2015 at 12:46:41 UTC, Jacob Carlborg wrote:
 On 2015-01-09 10:28, Manu via Digitalmars-d wrote:
 I'm looking at another potential opportunity to get D into the 
 office,
 but the target's for this particular project are NaCL and/or
 Emscripten.

 I was gonna start hacking around to see what the limitations 
 are with
 Emscripten on D code tonight. Has anyone done any serious
 investigation here?

 NaCl is a more useful target, but I think that will rely on a 
 special
 build of LDC... has there been discussion about that before? 
 Can any
 of the LDC guys chime in on the possibility?
Don't know if there's any interest but Adam D. Ruppe has hacked DMD to output JavaScript. You should be able to find it somewhere on the newsgroup.
guess you're talking about dtojs: https://github.com/adamdruppe/dtojs.
Jan 09 2015
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 9 January 2015 at 15:27:08 UTC, Mengu wrote:
 Don't know if there's any interest but Adam D. Ruppe has 
 hacked DMD to output JavaScript. You should be able to find it 
 somewhere on the newsgroup.
guess you're talking about dtojs: https://github.com/adamdruppe/dtojs.
I haven't updated that for a long time though, it probably won't work at all anymore and it was never really production ready. I wouldn't try it on a professional job.
Jan 09 2015
prev sibling next sibling parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
There are also other compilers from C++ to Javascript, Mandreel 
and Cheerp.

Cheerp claims to support the builtin Javascript garbage collector:

«Dynamic memory management. C++ objects are translated directly 
to JS objects, without the proxy of an emulated, flat memory 
space. Allow your applications to exploit the JavaScript VM 
garbage collector and co-exist with fair, on-demand memory 
allocation.»

http://www.leaningtech.com/cheerp/blog/

So if Cheerp is ready for production (I don't know if it is), it 
might be a better fit for D.
Jan 10 2015
parent reply Manu via Digitalmars-d <digitalmars-d puremagic.com> writes:
On 11 January 2015 at 01:31, via Digitalmars-d
<digitalmars-d puremagic.com> wrote:
 There are also other compilers from C++ to Javascript, Mandreel and Cheerp.

 Cheerp claims to support the builtin Javascript garbage collector:

 «Dynamic memory management. C++ objects are translated directly to JS
 objects, without the proxy of an emulated, flat memory space. Allow your
 applications to exploit the JavaScript VM garbage collector and co-exist
 with fair, on-demand memory allocation.»

 http://www.leaningtech.com/cheerp/blog/

 So if Cheerp is ready for production (I don't know if it is), it might be a
 better fit for D.
The thing about cheerp vs emscripten, is that while cheerp produces code that is more like javascript, emscripten produces asm.js, which is lightning fast by comparison. If there's actual work being done, then emscripten is the choice, if it's just as a substitute for writing js code, because it's a nightmare, then cheerp is probably better.
Jan 10 2015
parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Sunday, 11 January 2015 at 01:05:59 UTC, Manu via 
Digitalmars-d wrote:
 The thing about cheerp vs emscripten, is that while cheerp 
 produces
 code that is more like javascript, emscripten produces asm.js, 
 which
 is lightning fast by comparison.
 If there's actual work being done, then emscripten is the 
 choice, if
 it's just as a substitute for writing js code, because it's a
 nightmare, then cheerp is probably better.
You are probably right about throughput which I would expect to be better with Emscripten than Cheerp, although keep in mind that only Firefox converts asm.js directly to asm without the regular JIT. IE and Chrome uses the regular JIT AFAIK. With asm.js you are also stuck with a fixed size heap and if you manage to implement GC on asm.js, you get the freeze... Then you have interop with JS. Not sure if you can interact with Worker-threads within asm.js? I had not heard of Cheerp until today, but it looks interesting to me.
Jan 10 2015
parent reply "Janus" <ysangkok gmail.com> writes:
On Sunday, 11 January 2015 at 01:33:25 UTC, Ola Fosheim Grøstad 
wrote:
 On Sunday, 11 January 2015 at 01:05:59 UTC, Manu via 
 Digitalmars-d wrote:
 With asm.js you are also stuck with a fixed size heap
The heap can grow now, see https://github.com/kripken/emscripten/blob/master/ChangeLog.markdown#v1251-1012014 On Friday, 9 January 2015 at 09:51:36 UTC, Ola Fosheim Grøstad wrote:
 Remember that only Firefox support ASM.js.
Chrome runs it pretty fast though, sometimes faster than Firefox.
Jan 13 2015
parent "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Wednesday, 14 January 2015 at 03:11:41 UTC, Janus wrote:
 On Sunday, 11 January 2015 at 01:33:25 UTC, Ola Fosheim Grøstad 
 wrote:
 On Sunday, 11 January 2015 at 01:05:59 UTC, Manu via 
 Digitalmars-d wrote:
 With asm.js you are also stuck with a fixed size heap
The heap can grow now, see https://github.com/kripken/emscripten/blob/master/ChangeLog.markdown#v1251-1012014
Thanks, I've read the source code now. It involves creating a new array, then copying the old array into the new one. And it probably needs the experimental Ecmascript 7 ArrayBuffer.transfer() function to be fast. So clearly something that only can run when waiting for something (like between levels in a game).
 Chrome runs it pretty fast though, sometimes faster than 
 Firefox.
Yes, not if the code is ASM.js, but still good even then.
Jan 14 2015
prev sibling parent Martin Nowak <code+news.digitalmars dawg.eu> writes:
On 01/09/2015 10:28 AM, Manu via Digitalmars-d wrote:
 I'm looking at another potential opportunity to get D into the office,
 but the target's for this particular project are NaCL and/or
 Emscripten.

 I was gonna start hacking around to see what the limitations are with
 Emscripten on D code tonight. Has anyone done any serious
 investigation here?

 NaCl is a more useful target, but I think that will rely on a special
 build of LDC... has there been discussion about that before? Can any
 of the LDC guys chime in on the possibility?
It would require some druntime work for the libc differences (newlib vs. glibc). That's similar to the ongoing Android/X86 work https://github.com/D-Programming-Language/druntime/pull/1069.
Jan 10 2015