These allow using dscripten to compile D code to JavaScript (asm.js) with typical D workflow (dmd and rdmd).
- Run dscripten's
fetch_toolchainscript to download, patch and build LDC and Emscripten. - Build the tools in this directory (e.g.
dmd dmd-dscripten.d && dmd rdmd-dscripten.d). - (Optional) Set up your environment (see the Configuration section below).
There are two ways to use this toolchain:
- Invoke the tools directly (e.g.
path/to/rdmd-dscripten --compiler=path/to/dmd-dscripten --build-only worker.d) - Prepend the
bindirectory to yourPATH, so that it overrides the standarddmdandrdmdbinaries.
Because these tools attempt to implement the same command-line interface as dmd/rdmd, the second method can be used with any programs (e.g. build tools, error highlighting in editors) without needing to configure them. For example, Dub can be used to build programs in its --rdmd mode.
In addition to the usual switches and .d files, dmd-dscripten also understands how to handle .c, .llvm and .bc files on its command line, and will appropriately compile or otherwise include them into the compilation. See the test suite for examples.
The tools read a few environment variables:
DSCRIPTEN_RDMD- path to realrdmdbinary (default:/usr/bin/rdmd)DSCRIPTEN_TOOLCHAINS- path to toolchains build byfetch_toolchain(default:/tmp/toolchains)LLVMJS- path to Dscripten's LDC (default:$DSCRIPTEN_TOOLCHAINS/llvm-js)EMSCRIPTEN- path to Dscripten's Emscripten (default:$DSCRIPTEN_TOOLCHAINS/emscripten)DSCRIPTEN_TOOLS- path to the root of this repository (default:thisExePath.dirName- the directory the tool's executable is in)
<!doctype html>
<html>
<title>Dscripten WebWorker test</title>
<meta charset="UTF-8">
<script>
var w = new Worker('worker.js');
w.onmessage = function(event){
var text = (new TextDecoder("utf-8")).decode(event.data.data);
document.getElementById("result").innerHTML += text + '<br>';
};
w.postMessage({'funcName':'myFunc', 'data':'', 'callbackId':123});
</script>
<body>
<div id="result"></div>
</body>
</html>module worker;
import core.stdc.stdio;
import core.stdc.string;
import dscripten.standard;
import dscripten.emscripten;
import ldc.attributes;
@assumeUsed
extern(C)
void myFunc(char* data, int size)
{
foreach (i; 0..10)
{
char[32] buf;
sprintf(buf.ptr, "Working... %d", i);
workerRespondProvisionally(buf.ptr[0..strlen(buf.ptr)]);
}
workerRespond("Done!");
}Compile with:
$ rdmd-dscripten --compiler=dmd-dscripten --build-only worker.dThis will create worker.js.
For more examples, see the testsuite.