How can I use class and wasm?

Adam D. Ruppe destructionator at gmail.com
Wed Oct 28 16:35:15 UTC 2020


On Monday, 26 October 2020 at 21:38:39 UTC, Jack wrote:
> I did consider do something like this but the class would only 
> live in the function's lifetime, right? that wouldn't work if I 
> need to pass the class around

Right, you'd have to manage the memory somehow. One possibility 
is to malloc it too, but still manually done unless you use some 
kind of GC (and I think I do want to make a minimal gc for my 
little wasm thing but it will be a bit till I get around to it)


> Which type definition are you refering to? you mean the class 
> members/memory layout of a class?

The definition of the __init symbol. ldc needs it to be... 
something... whereas dmd doesn't care if it is just a void*.


> This is great! I'll try write a small druntime too for my case. 
> Do you have links/resources where things like __heap_base, 
> __data_end are defined?

Yeah, that's a llvm thing. I saw it here:

https://dassur.ma/things/c-to-webassembly/

like idk if there's something more authoritative but that blog 
set me started and it works for me so worth something.

A lot of this stuff is actually llvm meaning you can search for C 
/ clang info and apply it to D as well.

> know where you can it too. I didn't understand the interation 
> of webassembly-core.js with D. for example, you have this:
>
> // placeholder to be filled in by the loader
> var memory;
>
> How did you put the compiler/linker to set memory's location 
> properly so you can do things like:

That's actually set in my own javascript just I put that in the 
html file instead of the js file (I gotta refactor this a bit 
more).

See here:

https://github.com/adamdruppe/webassembly/blob/master/server/webassembly-skeleton.html#L24


The "obj.instance.exports.memory" is a web api thing (I think, 
tbh I found much of the docs to be unclear or incomplete). 
Mentioned here:

https://developer.mozilla.org/en-US/docs/WebAssembly/Loading_and_running

and again it worked for me so I ran with it. But I preassigned it 
to the global after instantiating just so it is available in more 
places more easily.

> doesn't it define some variables where the implementation of 
> memorySize() and growMemory() could be done in D?

It might, I don't know, I just barely got this hack to work.

> also, where can I find this Sebastiaan's project that you 
> mentioned in the article? it's a druntime like yours?

His is a port of the real thing.

see:
https://gist.github.com/skoppe/7617ceba6afd67b2e20c6be4f922725d

and:
https://github.com/skoppe/druntime/tree/wasm

He got like 80% done then has just been busy irl so not finished 
yet.

> EMSCRIPTEN_BINDINGS()

I don't know anything about emscripten. I looked at it years ago 
and figured it was too bloated to even seriously consider.

> emscripten/bind.h header, see[1] for example. I haven't tried 
> yet. But maybe marking the class as extern(C) that would work 
> with D's class too, right?

That won't work, extern(C) makes no sense on classes since C 
doesn't have classes. extern(C++) of course sometimes does make 
sense - you can make them with in D's -betterC too.

But exporting a D class to javascript is going to be ... 
basically a no go given webasm's limitations. I guess you could 
make it work but it would be messy.




More information about the Digitalmars-d-learn mailing list