Comment by weinzierl

Comment by weinzierl 5 hours ago

5 replies

That's why I wrote direct DOM access above. Sure, we can load extra JS in an addition to WASM and and funnel everything through JS. Some say, it does not matter.

I think it does, but it is hard to track the initiatives that tackle this. That's why I'm asking.

flohofwoe 5 hours ago

WASM-GC is essentially a way to hook into an externally provided garbage collector, it doesn't help much with calling into web APIs.

The DOM has been designed as a JS API (for better or worse), accessing that from WASM will always require to go through some FFI layer (this layer may be hidden and automatically created at runtime, but it still needs to exist).

The question is just how much marshalling needs to happen in that FFI layer. Making the DOM (or other Web APIs) actually WASM friendly would require an alternative DOM API which looks more like a very low-level C API - e.g. not using Javascript objects/strings at all, but only numbers, some of them 'pointers' into an ArrayBuffer (e.g. the WASM heap).

There's also a middle-way of adding such 'garbage free' functions to web APIs which allow to be called with less overhead in the JS engine, for instance WebGPU has such functions, and they were specifically added to reduce marshalling and GC overhead when called from WASM.

E.g. GC-free Web APIs would be much more useful than GC support in WASM for interacting with the browser side. GC support in WASM is mainly useful for languages that depend on garbage collection, because those can delegate the GC runtime tasks to the JS engine's garbage collector.

  • jech an hour ago

    > The DOM has been designed as a JS API

    My understanding is that it originated as a straight binding to Netscape Navigator's C++ API, so it's actually originally a C++ API.

    • flohofwoe 24 minutes ago

      Calling an "idiomatic" C++ API from WASM would also require a (COM like) shim though since C++ has no standardized ABI. E.g. as soon as your API uses things like std::string it already gets extremely hairy.

  • weinzierl 3 hours ago

    If I remember correctly one reason for the direct DOM initiative been held was that it depended on the WASM GC being completed.

    "The DOM has been designed as a JS API (for better or worse), accessing that from WASM will always require to go through some FFI layer (this layer may be hidden and automatically created at runtime, but it still needs to exist)."

    I don't see that. The DOM is the underlying data structure and what we need is direct access to it form WASM, without FFI or going through JS. WASM should be a first class citizen next to JS.

    • flohofwoe an hour ago

      That would require a "C ABI friendly" alternative DOM API. That's hardly worth the trouble since the performance problems lurk in the actual DOM design and implementation, not in the JS shim. It would make more sense to go that way for lower level web APIs like WebGL or WebGPU.

      Also if we're talking big changes like this it would actually make more sense to implement the DOM on top of the 3D APIs, and not the 3D APIs as an appendix of the DOM ;)