Readit News logoReadit News
milliams · 5 years ago
I found this video https://www.youtube.com/watch?v=r-A78RgMhZU "A Talk Near the Future of Python (a.k.a., Dave live-codes a WebAssembly Interpreter)" to be a brilliant introduction to WASM as well as writing interpreters in general. I'm a relative novice in the subject and it was pitched right at my level.
Quekid5 · 5 years ago
Dave Beazley is a truly amazing presenter. Another favorite of his of mine is his epic tale of how he ended up demolishing an opposing side's case in a civil lawsuit... by sheer luck of having a Python interpreter avaialable. For for you viewing pleasure:

   https://www.youtube.com/watch?v=RZ4Sn-Y7AP8

1vuio0pswjnm7 · 5 years ago
I learned about this author from SWIG. I have always liked that project. https://en.wikipedia.org/wiki/SWIG
devwastaken · 5 years ago
Very nice. I like these tutorials showing the nuts and bolts of wasm and C without just throwing it at emscripten toolchain.

I'm curious if there's perf differences between canvas and webgl canvas. This project uses just canvas, but iirc passing frames to be rendered by webgl is faster. Perhaps I'm wrong in this context.

I also don't see threading in here. Makes sense for a demo, but if this were to be used performantly you'd have to throw it all in a webworker so it doesn't block the main thread. This is one point of contention with wasm because it's not straightforward to render to a canvas/webgl on the main thread from a worker thread. OffscreenCanvas is one workaround but not supported by FF or safari.

throw_m239339 · 5 years ago
Seems to me the original rendering pipeline wasn't GPU based. The author is just dumping whatever the game renders in a canvas element, there is no need for webgl for that.

Also Canvas is getting some GPU acceleration in some browsers:

https://developers.google.com/web/updates/2012/07/Taking-adv...

pjmlp · 5 years ago
Usually canvas hardware acceleration is not taken advantage of unless you do CSS tricks like setting an element with another depth order.

https://www.programmersought.com/article/58016098343/

In any case, given how browsers black list drivers and GPU models, even WebGL isn't guaranteed to be accelerated.

With these constrains the Web will never be as fast as native for graphics programming.

They took us Flash away, but forgot to ensure the same capabilities were kept.

westoncb · 5 years ago
GP was asking whether it would be more performant to do via webgl though. (It's already known that canvas is currently used)

My bet is that webgl probably would be, but I'd also be curious to hear from someone with more info.

Seems to basically come down to whether canvas of webgl does a more efficient version of some kind of memory copy onto the GPU side.

djhworld · 5 years ago
There's also the problem of getting keyboard input in and out of the web worker in a performant manner.

I tried this a few years ago with a Gameboy emulator I had ported from Go to webassembly and used web workers to run the emulator in.

Getting the keyboard input in, in a performant way was a real struggle using postMessage, although I'll admit I'm not the best at web programming so someone more skilled might have been able to do it better

musicale · 5 years ago
Do you mean that keyboard input into a web worker is too slow, or it uses too much memory or cpu or power or ???

How is keyboard input different from any other sort of data round trip to/from a web worker?

Deleted Comment

sitkack · 5 years ago
Why this arch over having the UI run in the main thread and sending events into the wasm worker?
asiachick · 5 years ago
You could pass it via a SharedArrayBuffer
pjmlp · 5 years ago
Pity that WebAssembly Studio development seems to have stalled.

https://webassembly.studio/

It is the easiest way to get into WASM.

Threading requires sending custom headers by the way.

dQw4w9WgXcQ · 5 years ago
Any thoughts on how you could revive it?
pjc50 · 5 years ago
Given that

> Doom has a global variable screens[0] which is a byte array of SCREENWIDTH*SCREENHEIGHT, i.e. 320x200 with the current screen contents.

It would seem to me that the right approach would be to hoist out Doom's main loop so you just have a renderFrame() function, then put something on the main browser thread to "blit" the image into the canvas itself.

dividuum · 5 years ago
As you wrote "the right approach would be", I'll add: That's exactly what the code does.
sharikone · 5 years ago
Just a point for the first chapters: you are not required to run your own local server (even if things push in that direction)

You can include the wasm as an ArrayBuffer or as a base64 encoded string and hardcode it in the javascript. Now it will run even in a static html.

jjice · 5 years ago
It's incredible how far the web has come. I remember the first time I saw a browser GameBoy emulator and I was amazed. Maybe I should port my GB emulator to WASM...
sigg3 · 5 years ago
> It's incredible how far the web has come.

I agree and disagree. It seems like no one is questioning why we need to use legacy web browsers in between all the code we're executing locally.

It's like a new iteration of old tech like lisp machines, which started out as specific purpose only to grow into complete environments (afaik).

In this regard, we haven't come far, it's just the syntax that has changed.

Deleted Comment

vitiral · 5 years ago
This is exactly the kind of tutorial I've been waiting for for years. The way blocks and breaks work is especially non-intuitive if you are used to either assembly or regular languages, and you START with it. Good work, really loving this tutorial!
Mizza · 5 years ago
I love seeing this kind of tutorial, that isn't just a step-by-step guide, but also an exploration of the thought process and trial-and-error that goes on in crafting each step, so thanks for sharing.

Looks like a lot of the work on the Doom port (https://github.com/diekmann/wasm-fizzbuzz/tree/main/doom) is about getting common functions from the C standard library to work in WASM. Surely this seems like a good opportunity for a new Free Software initiative - something optimized, properly licensed/credited and easy for everybody to use?

ronsor · 5 years ago
There's already wasi-libc: https://github.com/WebAssembly/wasi-libc
jgrahamc · 5 years ago
If you'd like to try (multiplayer) Doom in WASM there's https://silentspacemarine.com/
Mizza · 5 years ago
If anybody wants to play some Deathmatch: https://silentspacemarine.com/dbee695faaa29aefe9a14d5798689e...
ralphc · 5 years ago
I tried this, how do you open doors?
recursivedoubts · 5 years ago
I can't tell if the lack of strings and DOM API interop in web assembly is on purpose or not.

If it is on purpose, what an absolutely diabolical way to ensure javascript language dominance in the browser: give people a way to port their language to the browser, but make it incredibly difficult to do anything.

skitter · 5 years ago
Afaik for the WebAssembly MVP, the goal was to have a simple, efficient compile target - therefore only integers and floats. To make wasm more useful & easier to integrate, the plan calls for interface types[0], which allow both accessing complex (JS) objects and calling browser APIs.

[0] https://github.com/WebAssembly/interface-types/blob/master/p...

recursivedoubts · 5 years ago
interesting, thank you
runnerup · 5 years ago
thank you
flohofwoe · 5 years ago
What type of strings though? Exposing Javascript string objects in WASM doesn't make much sense if the code is expecting C strings for instance. Same for other languages, those all have their own incompatible internal representations for strings. The only somewhat interop-friendly string type is a zero-terminated bag of bytes, usually UTF-8 encoded (aka C strings), but that's a different string representation than Javascript uses.

The Emscripten SDK offers helper functions to marshal high level data types like Javascript strings to UTF-8 encoded C strings on the WASM heap and back to JS, so it's not that bad.

DOM access can be achieved with helper libraries which call out into JS. And since any sort of DOM manipulation is extremely slow anyway there's not much of a performance difference even with the overhead of calling out from WASM into JS (which actually is quite fast nowadays).

mamcx · 5 years ago
>What type of strings though?

The good one!

UTf-8, NOT null terminated, pascal like.

ie: what rust have:

https://doc.rust-lang.org/std/string/struct.String.html

REPEAT the mistakes of C (and considering the security angle! in a browser!) must be a big no.

miohtama · 5 years ago
DOM manipulation is slow is pretty much busted misconception today:

https://svelte.dev/blog/virtual-dom-is-pure-overhead

TazeTSchnitzel · 5 years ago
The difficulty is inherent; C, C++ and so on live in a very different world to JavaScript. Whether or not WebAssembly had direct interaction with JavaScript objects at launch or not, writing bridging code would still be tedious.

But there's no reason you must write this yourself. Others have done the hard work for you and written libraries.

franz899 · 5 years ago
WebAssembly's purpose was never to replace JavaScript but only to speed up certain parts of a website/app.
pjmlp · 5 years ago
That was the original message used to sell WebAssembly, however when the real goal is to replace ActiveX, Flash, Silverlight and PNaCL it was obvious that it would grew beyond that.
cubano · 5 years ago
really? i thought it's purpose was to take us back to the good old days of sellable proprietary binary blobs instead of the more open HTML/JS/CSS stack.
recursivedoubts · 5 years ago
that's not how I remember it:

https://brendaneich.com/2015/06/from-asm-js-to-webassembly/

edit: from the linked article, in case it isn't clear, an HN comment by eich:

"Sure, in userland many languages compile to assembly. Hmm, where have I heard that word lately?"[1]

[1] - https://news.ycombinator.com/item?id=9554914

Deukhoofd · 5 years ago
The lack of strings makes sense, as many different languages and standard libraries have their own implementations of it, that can behave slightly differently. It now puts the implementation of the string to the compilers/linkers, as is generally the case for assembly as well.

The lack of a DOM API is something I sorely miss as well. It's currently possible (and not that hard, you can just interact with JS), but comes with such performance overhead that you lose the entire benefit of WASM.

jollybean · 5 years ago
WASM is supposed to be 'assembly' level a little bit like java bytecodes. So it's lower level than 'strings'.

But as you have pointed out, the missing layer on top i.e. the 'thing we can practically use' is a big gaping hole and it's a little bit diabolical.

The fact that JS has gotten so much faster and the lack of both higher-level abstractions and notably a really good 'bridge' to JS means it's lagged in terms of material applicability.

HideousKojima · 5 years ago
It's also still missing proper garbage collection, meaning languages like C# have to include basically the entire runtime if you compile to WebAssembly. This is a major part of why Blazor apps in .NET 5 are ~2MB for a simple "Hello World" (closer to 8MB if you use the AOT compilation options in the .NET 6 preview).
Deukhoofd · 5 years ago
Why would WASM have garbage collection? It's an assembly target, not a runtime. What if languages would want different memory management strategies?

I know it's an existing proposal for WASM, but it feels so massively out of scope. If the issue is having to include runtimes in the WASM binary it might be more useful to think about how we could serve runtimes in a more efficient way.

pjmlp · 5 years ago
My Intel CPU also doesn't have a GC, so that is how it is.

Deleted Comment

Jnr · 5 years ago
Interesting that Firefox by default did not render the fizzbuzz demo correctly by default. I had to click the canvas icon next to address bar and allow the canvas usage. And it did not show a prompt either. It just looked broken by default.

Screenshot from Firefox vs Chrome https://i.imgur.com/Af8nTim.png

smichel17 · 5 years ago
...Good! The "prompt" permission model is fundamentally broken, because all it does is train you to click through the prompt.

The "click the blocking button and turn it off" model is much better. It still trains you to turn off blocking when something is broken. However, crucially, that's only when it's broken. When it's not broken, you just use the site, instead of habitually clicking through the permission prompt that's just harvesting data, not actually needed to function.

And yes, malicious sites can of course display themselves as falsely broken until you grant the permissions. But this makes them more annoying to use, granting a UX edge to the honest sites which don't request unnecessary permissions. In other words, the incentives of sites and users are more aligned.

singularity2001 · 5 years ago

   >>> all it does is train you to click through the prompt.
no, if I want extra functionality, I click it, otherwise I ignore it. (random page wants my location? nah)

modular blocking prompts were broken, optional prompts are fine

dimes · 5 years ago
You probably have resist fingerprinting turned on
Jnr · 5 years ago
Most likely. I don't remember all the settings that I have turned on at some point. :)
fouc · 5 years ago
FF 89 worked fine here (mac), either your FF version is old or it's some sort of FF/linux issue ?