Proposed strategy to support WASM in D

Pierce Ng pierce at samadhiweb.com
Sat Nov 19 01:16:00 UTC 2022


On Friday, 18 November 2022 at 00:55:57 UTC, AnimusPEXUS wrote:
> 0.2: wasi support - this approach means what wasi platforms 
> provide interface for wasi application to interact with the 
> system

WASI is like a libc interface for Wasm and [WASI 
libc](https://github.com/WebAssembly/wasi-libc) is an 
implementation. A Wasm program calls WASI to access libc/OS-like 
functionality provided by the Wasm runtime, which could be the 
web browser or other runtimes.

Earlier this year, for the fantasy game console 
[TIC-80](https://github.com/nesbox/TIC-80), I implemented the 
[D-Wasm 
template](https://github.com/nesbox/TIC-80/tree/main/templates/d).

However, I'm stuck actually implementing games in D, (IHIRC) 
because  of a symbol clash between TIC-80's time() API function 
and WASI libc's time(). For comparison and contrast, with the Zig 
TIC-80 Wasm interface, Zig source code calls tic.time() for the 
TIC-80 time() function and thus no symbol conflict.

Here's the code snippet:

```
// D
void TIC() {
   cls(0);
   for (int x=0; x<240; x+=28) {
     for (int y=0; y<136; y+=28) {
       cx = 12*sinf(time()/30000*(x+y+1)); // <========== time() 
symbol conflict
       cy = 12*cosf(time()/30000*(x+y+1)); // <========== ditto
       Pir(x, y, 25, 25, x+cx, y+cy);
     }
   }
}

// Zig
export fn TIC() void {
     // some var initialization removed
     tic.cls(3);
     while (ax <= 240) {
       fx = @intToFloat(f32, ax);
       ay = 0;
       while (ay <= 136) {
         fy = @intToFloat(f32, ay);
         ca = 12*std.math.sin(tic.time()/30000*(fx+fy+1)); // 
tic.time()
         cb = 12*std.math.cos(tic.time()/30000*(fx+fy+1)); // no 
conflict
         Pir(fx, fy, 25, 25, fx+ca, fy+cb);
         ay += 28;
       }
       ax += 28;
     }
}
```

Without modifying TIC-80 source code, is there a way to do some 
'namespace' thingie for D like how Zig does it?

More generally, as a D newbie, the confusion for me was (still 
is): Should I be writing D programs (whether for TIC-80 or more 
mundane platforms like Linux) in D, or in BetterC?



More information about the Digitalmars-d mailing list