4 ms·
Great. Now how do I call this API from WASM without any intervening JavaScript?
by drfuchs 3y ago
Great. Now how do I call this API from WASM without any intervening JavaScript?
- pjmlp 3y agoYou don't. Maybe when reference types eventually become a thing.
- davexunit 3y agoWASM GC reference types are rolling out to stable browser releases now. Chrome 119 just shipped with them enabled by default.
- pjmlp 3y agoThose ones are unrelated to exposing browser APIs directly to WASM code. Setup is still required.
- davexunit 3y agoI don't quite understand what you mean. WASM modules don't have direct access to anything. They have to be granted capabilities from by the host. Are you referring to optimizations that various engines do where they recognize well-known imports like Math.sin and compile things such that they don't invoke JS at all?
- pjmlp 3y agoExactly, hence it is currently impossible to use WebGPU without JavaScript's help. There are some proposals regarding exposing the browser IDL to WASM via reference types, but it is far from happening.
- davexunit 3y agoYou need the host (whatever it is) to provide the capabilities to WASM. On the web, the host environment is JS. Browser engines are free to optimize well-known imports with or without any new proposals. I guess I just don't really see the issue...
- pjmlp 3y agoThe issue is that currently this is impossible "Now how do I call this API from WASM without any intervening JavaScript?". Now the Web IDL bindings have evolved to some kind of reference types, and that is what I was talking about, not GC reference types. https://www.w3.org/2018/12/games-workshop/slides/08-web-idl-bindings.pdf https://www.w3.org/2018/12/games-workshop/slides/08-web-idl-... https://hacks.mozilla.org/2019/08/webassembly-interface-types/ https://hacks.mozilla.org/2019/08/webassembly-interface-type... https://component-model.bytecodealliance.org/introduction.html https://component-model.bytecodealliance.org/introduction.ht... However I did the mistake to keep calling them reference types, when in reality they are now called component model. As for the browser, yeah that is what matters for WebGPU, outside of the browser there are so many middleware options with much better tooling (GPU debuggers for Web 3D still aren't a thing 10 years later, besides poor SpectorJS), and access to latest hardware features, instead of what was the state of the art in 2015.
- davexunit 3y agoYou'll need some JavaScript to instantiate the WASM module and pass in a set of imported functions for using WebGPU. After passing in the capabilities it can be all WASM from there.
- jauntywundrkind 3y agoAfaik, there's no means to pass in imported functions right now. Rust has a very extensive bindgen to make it kind of look like you can, but it's really a huge payload of js running on the page's main-thread that's converting back and forth. At significant cost. We just shipped garbage collection, which is one precondition to actually being able to pass things around (so things passed in can participate properly in gc). Next is component-model, which allows for passing non-trivial objects around; currently it's just primitives like numbers that can be passed. After that goes in, hopefully it won't be long before we have host-object bridging, where platform objects can be sent across. https://github.com/WebAssembly/component-model https://github.com/WebAssembly/component-model
- davexunit 3y agoYou can pass around arbitrary JS objects via the (ref extern) type. The (import "module" "name" ...) form can be used to declare an imported thing (function, global, memory, table.) I don't do Rust so I don't know what the limitations are with Rust on WASM right now but speaking just about core WASM stuff all the basic pieces are there now.
- jauntywundrkind 3y agoAfaik, no. What you are talking about is the future, not anything available on any platforms today. Right now, different runtimes don't have any way to communicate what type an object or function is. That's what component model is trying to figure out. Without that, there's not really a way for a ref to do anyone any good, as far as I know. If you can link any docs or examples, that'd be great. I feel like it's been a long long long wait, & I've been very eager. But if I'm mistaken, and passing stuff across boundaries is possible, that'd be amazing to see.