Ported js13k game underrun to D targeting webassembly

Sebastiaan Koppe mail at skoppe.eu
Mon Nov 19 13:52:28 UTC 2018


On Monday, 19 November 2018 at 11:13:52 UTC, Dukc wrote:
> I had a look at your code, and just now I realized that spasm 
> can really call JavaScript without any glue from JavaScript 
> side.
I am not sure where you got that impression from, but I am afraid 
you'll still need js glue code.

> This is huge! Now I can start to think about expanding my usage 
> of D at the web page I'm developing, potentially replacing 
> parts of my application made with Bridge.NET.
The only thing I did with spasm was to use a subset of the D 
language to define a declarative language for creating html 
components, and I used lots of introspection to optimise the 
rendering of them.

Everything else comes from the wasm backend from llvm and it's 
support in ldc.

I really appreciate you getting excited about it but look, Spasm 
is still in an early stage. The code is rough, the documentation 
poor. And it has plenty missing that needs to be fixed before you 
should consider writing anything serious with it. The two most 
important being bindings to all web apis and memory deallocation.

Having said that, of course I want you to use it :)

> I know you did announce about Spasm earlier and I should have 
> thanked you already. My fault: I realized that you made 
> something that enables one to generate WebAsm, but didn't 
> realize it enabled calling JS classes directly. Should have 
> looked closer.
The way calling js classes from D (or any language out there) 
works, is to insert the js object in a big associative array at 
the js side with an int as the key. The int (handle) is passed to 
D. Anytime D wants to call a function or retrieve a property of 
that class it calls some glue code with the handle as first 
parameter. In the javascript glue code the handle is used to 
lookup the object and the corresponding function is called.

Since javascript allows you to call by string (e.g. 
`object["foobar"]()`), you can minimize glue code at the expense 
of decoding utf-8 strings.

> 1. Manually compile your code and parts of Druntime/Phobos 
> targetting 32-bit X86, link them manually with llvm-link using 
> --only-needed and only then compile to JavaScript (Haven't 
> tried with WebAsm, but theoretically should compile the same 
> way). This way you don't need to manually stub what you don't 
> use -well, when the linkers have same ideas about what you're 
> calling, which's not always the case.
That might work, but for the end-user it really needs to be as 
simple as `dub build`.

> 2: Use Vladimir's DScripten. I haven't tried, as I was limited 
> by not being aware of anything like spasm to call Web API, but 
> probably easier than my method above and brings larger portions 
> of Phobos into user's reach. Also IIRC Vladimir managed to get 
> allocators working.
Yep. The prototype version of spasm ran on top of 
dscripten-tools. I decided to move to native llvm wasm 
compilation instead, since I don't particularly like the 
emscripten toolchain.

> Even with my relatively meager method, pipeline-style 
> programming with std.algorithm and std.range over (often 
> static) arrays is doable and practical in my experience.
As long as std.algorithms/ranges don't allocate, they work.


More information about the Digitalmars-d-announce mailing list