Surfing on wave.. WASM

Sebastiaan Koppe mail at skoppe.eu
Tue May 28 18:19:14 UTC 2019


On Tuesday, 28 May 2019 at 14:49:48 UTC, Adam D. Ruppe wrote:
> Yeah, and I like how you have a pretty good amount of type 
> safety.

As well as auto-completion. One serious downside is that there is 
so much binding code that a full recompile of the library is 
painstakingly slow. It could be because of the many uses of 
Sumtype and Optional, or that I am public importing everything 
through a package.d, idk.

> One of the things I like about strings though is working with 
> existing code. Like if I can just, on the D side, do 
> `window.my_own_object` I think that will be pretty cool.

Yeah, whichever way you go you cant go wrong. D has so much 
better tools to write bindings than Rust or Go.

```go
func main() {
     document := js.Global().Get("document")
     p := document.Call("createElement", "p")
     p.Set("innerHTML", "Hello WASM from Go!")
     document.Get("body").Call("appendChild", p)
}
```

```rust
#[wasm_bindgen(start)]
pub fn run() -> Result<(), JsValue> {
     let window = web_sys::window().expect("no global `window` 
exists");
     let document = window.document().expect("should have a 
document on window");
     let body = document.body().expect("document should have a 
body");
     let val = document.create_element("p")?;
     val.set_inner_html("Hello from Rust!");
     body.append_child(&val)?;
     Ok(())
}
```

> Indeed, that is next on my list. Actually, I kinda think I 
> could pass D slices in general pretty directly as an i64, 
> something like ptr << 32 | length... and a reinterpret cast on 
> the D side could just work to do that. But that might be too 
> clever for my own good.

You can just put string/array types in the extern(C) definition. 
Somehow LLVM expands array arguments into two u32's. Dunno why. 
And for return types it adds an u32 as first argument (on the js 
side) that points to the return value on the stack. Pretty rad.

See:
https://github.com/skoppe/spasm/blob/master/webidl/source/webidl/binding/test.d#L177,L192



More information about the Digitalmars-d mailing list