Proposed strategy to support WASM in D

AnimusPEXUS animuspexus at protonmail.com
Fri Nov 18 00:55:57 UTC 2022


0. probably two different approaches to supporting wasm is needed 
simultatiously:
0.1: [wasm module] + [js glue] -- this is current approach, 
similar to golang's
0.2: wasi support - this approach means what wasi platforms 
provide interface for wasi application to interact with the system

I'm talking here about [wasm module] + [js glue] approach

1. this approach mean the task have to be separated to two 
subtasks:
1.1 js glue code, which allows wasm code to interact with js 
environment.
1.2 druntime have to be written, so Dlang code would be 
compilable with ldc (or yet better with dmd it self) with wasm 
triplet, without --betterC option, like any usual D program.


not so long ago I've started new project 
https://github.com/AnimusPEXUS/dwasmjs and I've wrote some basic 
js code to work with js values from inside D ( 
https://github.com/AnimusPEXUS/dwasmjs/blob/master/source/dwasmjs/dwasmjsi.js ) it's far from being complete. but the general Idea - is to store js values on js side of js<>d glue code. (also I think reflect module of js could be used to improve js interactions, to avoid eval() function use, as eval would slow everything down)

also, to address memory transfers from js to d (wasm) - I've 
figured out what js side can ask D side to allocate memory arrays 
as needed and js should write bytes back to pointer passed by D 
side.

this js side of glue is working with identifiers (randomly 
generated unique values). if D side needs to get some string - D 
side calls function on JS side with id of value from which D side 
wants to get string, and D side also passes pointer to 
preallocated array (D side gets size needed for array by calling 
another function).

in my code I've stacked with need to allocate memory inside of 
wasm module instance. I've seen ADR's mini-druntime for wasm and 
seen how he used ldc's special fields to calculate addressing for 
memory allocation and used special ldc function to grow wasm blob 
(I think, those function can be called from standard WebAssembly 
module of JS, avoiding need in special ldc functions)

vvv
so basically, the main problem now in literally is to write 
WebAssembly version of druntime. gluing it to js is not so 
problematic.
^^^

--------------

as for WASI support, I've didn't read documentation yet, but I 
imagine it provides some binary interface so it can be used to 
interact with the system, so WASI, probably should be separate 
version of druntime - separate from WebAssembly(64)

so currently I'm reading druntime and trying to figureout how to 
modify it to be compilable in wasm

--------------

additionally:

1. LDC have to support unsigned integers in wasm function 
parameters, as currently wasm generated by ldc puts i32 type on 
all parameters.

2. we have to answer question: does stdc parts of druntime also 
should be ported to WebAssembly, as I've discovered what, looks 
like druntime requires struct tm from sys.posix.time module




More information about the Digitalmars-d mailing list