Thread overview
How can I use class and wasm?
6 days ago
Jack
6 days ago
Paul Backus
6 days ago
Jack
6 days ago
Adam D. Ruppe
6 days ago
can I make it work? the code (see below) result in link error:

>lld: error: wasm.o: undefined symbol: _D4wasm1C7__ClassZ
>lld: error: wasm.o: undefined symbol: _d_allocclass
>Error: linking with LLD failed

command line:

>ldc2 --d-debug -mtriple=wasm32-unknown-unknown-wasm -betterC wasm.d

ldc version:
> 1.24.0-beta1 (DMD v2.094.0, LLVM 11.0.0)

code:

>extern(C): // disable D mangling
>// seems to be the required entry point
>void _start() {}
>
>extern(C)
>int g()
>{
>    auto c = new C();
>    return c.f();
>}
>
>extern(C++)
>{
>    class C
>    {
>        int f() { return 42; }
>    }
>}
6 days ago
On Thursday, 15 October 2020 at 22:02:11 UTC, Jack wrote:
> can I make it work? the code (see below) result in link error:
>
>>lld: error: wasm.o: undefined symbol: _D4wasm1C7__ClassZ
>>lld: error: wasm.o: undefined symbol: _d_allocclass
>>Error: linking with LLD failed
>
> command line:
>
>>ldc2 --d-debug -mtriple=wasm32-unknown-unknown-wasm -betterC wasm.d
>
> ldc version:
>> 1.24.0-beta1 (DMD v2.094.0, LLVM 11.0.0)
>
> code:
>
>>extern(C): // disable D mangling
>>// seems to be the required entry point
>>void _start() {}
>>
>>extern(C)
>>int g()
>>{
>>    auto c = new C();

You can't use `new` in betterC.
6 days ago
On Friday, 16 October 2020 at 02:43:19 UTC, Paul Backus wrote:
> On Thursday, 15 October 2020 at 22:02:11 UTC, Jack wrote:
>> can I make it work? the code (see below) result in link error:
>>
>>>lld: error: wasm.o: undefined symbol: _D4wasm1C7__ClassZ
>>>lld: error: wasm.o: undefined symbol: _d_allocclass
>>>Error: linking with LLD failed
>>
>> command line:
>>
>>>ldc2 --d-debug -mtriple=wasm32-unknown-unknown-wasm -betterC wasm.d
>>
>> ldc version:
>>> 1.24.0-beta1 (DMD v2.094.0, LLVM 11.0.0)
>>
>> code:
>>
>>>extern(C): // disable D mangling
>>>// seems to be the required entry point
>>>void _start() {}
>>>
>>>extern(C)
>>>int g()
>>>{
>>>    auto c = new C();
>
> You can't use `new` in betterC.

that's right, my bad but even a custom allocator, using malloc()/free() wouldn't work either. How can I allocate memory for this class?
6 days ago
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

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.

Personally, I prefer to just not use betterC and make my own mini runtime:

http://dpldocs.info/this-week-in-d/Blog.Posted_2020_08_10.html

in particular

https://github.com/adamdruppe/webassembly/blob/master/arsd-webassembly/object.d#L74


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.