How can I use class and wasm?
Jack
jckj33 at gmail.com
Mon Oct 26 21:38:39 UTC 2020
On Friday, 16 October 2020 at 03:42:22 UTC, Adam D. Ruppe wrote:
> On Friday, 16 October 2020 at 03:04:25 UTC, Jack wrote:
>> How can I allocate memory for this class?
>
> It is possible but not easy without druntime.
>
> If you are using -betterC, you can use extern(C++) classes with
> extern(D) members. The compiler will let you declare that. But
> then you need to allocate it. `__traits(classInstanceSize,
> Whatever)` will tell you the size to malloc, but you also need
> to copy an initializer over before you call the constructor.
>
> I have a technique here that works on dmd...
>
> http://dpldocs.info/this-week-in-d/Blog.Posted_2020_07_27.html#zero-runtime-classes
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
> but ldc is more strict about the type definition and I don't
> know the magic it expects there... like it should be doable but
> idk how so this might not be of much use.
Which type definition are you refering to? you mean the class
members/memory layout of a class?
> Personally, I prefer to just not use betterC and make my own
> mini runtime:
I was thinking the same, this seems the way to go.
> http://dpldocs.info/this-week-in-d/Blog.Posted_2020_08_10.html
>
> in particular
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? assuming there's such documentation
somewhere, if you did into the compiler to get those, let me 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:
memorySize: function() { return memory.buffer.byteLength; },
?
in the same way that ldc defines __headp_base and __data_end,
doesn't it define some variables where the implementation of
memorySize() and growMemory() could be done in D? also, where can
I find this Sebastiaan's project that you mentioned in the
article? it's a druntime like yours?
> https://github.com/adamdruppe/webassembly/blob/master/arsd-webassembly/object.d#L74
this is great, good enough to put me in the direction on how to
do that. Also, I found C++ has this EMSCRIPTEN_BINDINGS() from
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? I don't know about the C++'s or
compiler's dependences that would prevent it from working with D.
In fact, that seems quite a hacky. Do you think we are better off
write our own small druntime? if I got an working nice one, I'll
put on github for those who would like to use this.
[1]:
https://stackoverflow.com/questions/15865923/interaction-with-c-classes-in-emscripten
Thanks for your answer, that was really helpful.
>
> But that's also not easy, lots of unfinished cases in my thing,
> but I did manage to make it work... for my specific case.
More information about the Digitalmars-d-learn
mailing list