Thread overview
How can I use WebAssembly Reference Types with LDC?
Sep 14
TM
Sep 14
kinke
Sep 14
TM
Nov 22
Sergey
September 14

In C/C++, you can access wasm's externref and funcref using:

char __attribute__((address_space(10)))* //externref
char __attribute__((address_space(20)))* //funcref

Does D provide similar functionality?

September 14

On Saturday, 14 September 2024 at 08:28:02 UTC, TM wrote:

>

In C/C++, you can access wasm's externref and funcref using:

char __attribute__((address_space(10)))* //externref
char __attribute__((address_space(20)))* //funcref

Does D provide similar functionality?

Nope, there's currently no way to customize the address-space of pointers. dcompute has 2-3 magic structs for expressing such pointers.

Maybe this could by solved via some magic

// magic struct requiring compiler support
struct __as_pointer(T, uint addressSpace) { T* ptr; alias this = ptr; }

alias ExternRef(T) = __as_pointer!(T, 10);
alias FuncRef(T) = __as_pointer!(T, 20);
September 14

On Saturday, 14 September 2024 at 12:08:13 UTC, kinke wrote:

>

On Saturday, 14 September 2024 at 08:28:02 UTC, TM wrote:

>

In C/C++, you can access wasm's externref and funcref using:

char __attribute__((address_space(10)))* //externref
char __attribute__((address_space(20)))* //funcref

Does D provide similar functionality?

Nope, there's currently no way to customize the address-space of pointers. dcompute has 2-3 magic structs for expressing such pointers.

Maybe this could by solved via some magic

// magic struct requiring compiler support
struct __as_pointer(T, uint addressSpace) { T* ptr; alias this = ptr; }

alias ExternRef(T) = __as_pointer!(T, 10);
alias FuncRef(T) = __as_pointer!(T, 20);

Thank you for your response. Unfortunately, I was unable to generate wasm's externref using the method you suggested. It appears that dcompute cannot generate code targeting wasm.

Sadly, it seems difficult to achieve this with the capabilities of the dlang.

September 15
On 15/09/2024 5:05 AM, TM wrote:
> On Saturday, 14 September 2024 at 12:08:13 UTC, kinke wrote:
>> On Saturday, 14 September 2024 at 08:28:02 UTC, TM wrote:
>>> In C/C++, you can access wasm's externref and funcref using:
>>> ```C++
>>> char __attribute__((address_space(10)))* //externref
>>> char __attribute__((address_space(20)))* //funcref
>>> ```
>>> Does D provide similar functionality?
>>
>> Nope, there's currently no way to customize the address-space of pointers. dcompute has 2-3 magic structs for expressing such pointers.
>>
>> Maybe this could by solved via some magic
>> ```D
>> // magic struct requiring compiler support
>> struct __as_pointer(T, uint addressSpace) { T* ptr; alias this = ptr; }
>>
>> alias ExternRef(T) = __as_pointer!(T, 10);
>> alias FuncRef(T) = __as_pointer!(T, 20);
>> ```
> 
> Thank you for your response. Unfortunately, I was unable to generate wasm's externref using the method you suggested. It appears that dcompute cannot generate code targeting wasm.
> 
> Sadly, it seems difficult to achieve this with the capabilities of the dlang.

Indeed, what Martin is suggesting is that a compiler improvement is required.

There is no way to do what you want currently.
November 22

On Friday, 22 November 2024 at 02:05:13 UTC, ErichMurazik wrote:

>

Some programs on my computer crashed when accessing your link.

@moderator @spam

January 30

On Saturday, 14 September 2024 at 12:08:13 UTC, kinke wrote:

>

Nope, there's currently no way to customize the address-space of pointers. dcompute has 2-3 magic structs for expressing such pointers.

Maybe this could by solved via some magic

// magic struct requiring compiler support
struct __as_pointer(T, uint addressSpace) { T* ptr; alias this = ptr; }

alias ExternRef(T) = __as_pointer!(T, 10);
alias FuncRef(T) = __as_pointer!(T, 20);

I'm currently in the process of implementing such feature, here are my ideas:

  • Amend the magic pragma system, so one could get pragma(LDCspectypes, "wasm.externref") struct ExternRef {}, but that currently does not really work with the current system.
  • Amend the magic attribute system, so one could get @WASM_externref struct ExternRef {}, but also the attribute system is not equiped with it, even if a @WASM_funcref would be the most ideal for implementing funcref.
  • Amend the type resolving system, so if it detects a struct __wasm_externref_t{}, it overrides the output, but currently I don't know how that works (maybe it should be the easiest?).
  • Amend DMD to have __wasm_externref_t as an "exotic type" that is only valid on WASM targets, but even vectors don't have their own dedicated types.