Feels like the web world is desperately in need of competent threading capabilities. Can I (practically) use Rust or Go in the browser via wasm already!?
Feels like the web world is desperately in need of competent threading capabilities. Can I (practically) use Rust or Go in the browser via wasm already!?
You can use “WASM threads” from Rust or Go. Or JavaScript for that matter. Under the hood it’s all workers and shared array buffer, nothing to do with the language. Last time I checked you can’t even use std::thread for instance in Rust, since the compiler doesn’t understand workers (because workers aren’t actually part of WASM).
None of which will help you when updating the DOM anyway, since that happens in its own thread. It’s up to the browser how they run their rendering engine and I doubt they’re ever likely to allow direct control over that.
You can (built into rust and tinygo for go.)! And wasm-threading has been around for a while! https://webassembly.org/features/ But I don't think these wasm-threading capabilities are well harnesses by any languages yet.
There been a decade of webworkers being super possible. That does get multi-threading! With transferable memory & everything. So, this isn't like some new super non-js thing. Some folks do use workers, but alas not popular enough, not something React or Angular helps steer folks to.
Whats super neat and tricky here is that the iframe in separate origin-agent-cluster has a full DOM implementation that's there & raring to go. We already could have tried using jsdom or happy-dom or a wasm-powered alternative in a worker to do something like this, already. This is super cool though because it's much lower code! It uses a dom runtime that's already loaded & available (the browser's) rather than having to ship & load a separate DOM runtime.
But to answer your question, yes you can use Rust and Go practically in the browser. It just isn’t all that helpful except in narrow circumstances.
The difference with Rust and Go is they have better synchronization capabilities - where JavaScript largely forces you to clone data between threads.
So while you may still be messaging worker threads in Rust/Go - threads share memory which is fast and you have access to things like atomics and mutexes.
We can use Rust in the browser today with WASM and it's super cool - but without something like WASI, extensive thunking through JavaScript is required, plus there are issues with threading.
I hope that one day we can initialize a wasm module via a script tag with the browser offering a wasi interface
`<script src="module.wasm" type="application/wasi">`
And that browsers allow for threading without the restrictive security headers we have today
https://github.com/WebAssembly/threads/blob/main/proposals/t...
But that’s not going to help when accessing the DOM.
Is there a proposal(s) covering DOM access?
I remember interface types were a thing a few years back. If you have access to a dom object (I assume by pointer?) and it is removed/GC'd - would the wasm module have a null pointer?
Isn't that what SharedArrayBuffer is for (avoiding)?
Again, there’s very few actual uses for wasm. The main one is when you have a preexisting library in a different language, other than that you don’t gain any real functionality from using it.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
In the past I have used a SharedArrayBuffer to store a custom Graph implementation sent between threads. I stored the adjacency list in the SAB with the rest of the data being stored as a plain object and cloned/sent via postMessage between threads.
This worked but is extremely janky, required an unsafe custom data structure, manual/unsafe memory page management, was difficult to read/review and the performance was lacklustre because half of the data structure was plain JS objects that needed to be serialized and cloned anyway.
Simply, JavaScript sucks for multithreading and it would be great to have the ability to write high performance, multithreaded, complex web applications in a language designed with parallelism in mind - like Rust or Go.
While wasm _today_ cannot offer that (at least not practically) - there is certainly a use case for replacing JavaScript in that context and I hope that it does.
JS will still be useful for basic applications that just need a little scripting
I must question whether the problem of random sites wanting to spin up a whole ton of cores that consume arbitrary amounts of compute and memory really needs to be solved.
>big web apps
So you've defined the goalpost as "big web apps". There are billions more use cases for Javascript than however many "big web apps" might exist. So your phrasing is too broad, maybe consider:
>>It's crazy that people say that WASM won't replace JavaScript in big web apps.
Nobody's saying that. You are free to use WASM for your "big web app". Nobody and nothing is stopping you.
WASM certainly has its place, but it also certainly won't replace JavaScript in most of the places JavaScript is used every day. "Big web apps" are (my guess) maybe 1,000,000 of the 49,501,698 websites that use JavaScript, but it would depend on how you define "big web app" (and I really don't want go down that goal-posting rabbit hole). There are 13.8 million people writing javascript every day, how many of them do you think are working on "big web apps" that actually need WASM? How many of them do you think want to switch to Rust from Javascript? "developers" always seem to think everyone else should be some rockstar 10x programmer, but most use cases for javascript are pretty simple, and yet totally effective for what the requirements typically are.
I don’t think everybody needs to be building big web apps, I just think that most full time developers writing JS (or TS) are probably on a team working on a web app, so the introduction of a build process isn’t really an issue.
Of course it’s valid to write good ol’ javascript. I do it all the time. I’m always happy to avoid dealing with webpack