D and WebAssembly (Wasm)

Dukc ajieskola at gmail.com
Mon Sep 13 18:38:38 UTC 2021


On Monday, 13 September 2021 at 16:19:31 UTC, Kenneth Dallmann 
wrote:
>    Rumor has it that D can compile to Wasm, but only in the 
> "BetterC"
> subset.

True. Or alternatively, you probably can do it by simply not 
using the unported DRuntime parts, and defining a few stub 
functions for those runtime calls that remain in the binary 
anyway.

>    LLVM can compile to Wasm, but unfortunately it may not 
> support
> systems IO calls because of the lack of operating system access.

You're supposed to create the system hooks for WASM yourself, or 
use a library for doing that. 
https://code.dlang.org/packages/spasm should be your first stop 
when using D. I think WASM is designed so you can run code 
unprivileged code in it, as opposed to asm.js which always has 
full access to it's environment.

> Emscripten is a compiler that can transform C into something 
> that
> can run on Wasm without holes in the Standard Library.  It does
> this by simulating an operating system, which adds overhead to 
> the
> runtime, but allows a full range of C to be used, to my 
> understanding.

You can compile D to asm.js using the Emscripten backend, and 
then call the C API. No need for a C transpilation. However, I 
prefer WASM myself. It does not require a massive framework like 
Emscripten, and also it felt less buggy. And you can use Spasm 
with it.


More information about the Digitalmars-d mailing list