News for hackers. Stuff that matters.
Re:WebAssembly from Scratch: From FizzBuzz to DooM by recursivedoubts on Wednesday July 14, 2021 @08:10AM (#27832176)
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.

Re:WebAssembly from Scratch: From FizzBuzz to DooM by skitter on Wednesday July 14, 2021 @08:23AM (#27832249)
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...

Re:WebAssembly from Scratch: From FizzBuzz to DooM by recursivedoubts on Wednesday July 14, 2021 @08:42AM (#27832393)
interesting, thank you
Re:WebAssembly from Scratch: From FizzBuzz to DooM by runnerup on Wednesday July 14, 2021 @09:03AM (#27832548)
thank you
Re:WebAssembly from Scratch: From FizzBuzz to DooM by flohofwoe on Wednesday July 14, 2021 @08:50AM (#27832445)
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).

Re:WebAssembly from Scratch: From FizzBuzz to DooM by mamcx on Wednesday July 14, 2021 @11:19AM (#27834062)
>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.

Re:WebAssembly from Scratch: From FizzBuzz to DooM by flohofwoe on Wednesday July 14, 2021 @12:25PM (#27834886)
This would still require conversion from and to Javascript strings, and doesn't help with any language compiled to WASM that isn't Rust. And it probably wouldn't even help Rust because such a native WASM string type would presumably live outside the WASM heap (because if the string data would be on the WASM heap, there's no need for a native string type).
Re:WebAssembly from Scratch: From FizzBuzz to DooM by mamcx on Wednesday July 14, 2021 @02:24PM (#27836500)
Any complex structure past int/floats requiere conversion. Heck, even floats and ints (for example: oCalm and anybody with more/less bits than JS).

So, given this is a fact, the best course of action is chosen the most safe alternative.

And for everyone else? Well an array of bits ant let the host/callers that are the only that know their own stuff deal with it.

INCLUDING Js.

Re:WebAssembly from Scratch: From FizzBuzz to DooM by brabel on Thursday July 15, 2021 @02:49AM (#27842053)
I don't know what you're arguing about. The interface types proposal defines a string like this:

    string ≡ (list char)
char is defined to be a Unicode scalar value (i.e., a non-surrogate code point).

Basically, this is "the most safe alternative"??!

Re:WebAssembly from Scratch: From FizzBuzz to DooM by mamcx on Thursday July 15, 2021 @11:23AM (#27845807)
I was arguing against the idea of "string are implemented differently by different languages, so WHICH one choose?".

A safe one. Your sample is that - except I think is better if is a utf-8 string, but this one works for me too-. What will be worrisome is if is made to be like in C.

Re:WebAssembly from Scratch: From FizzBuzz to DooM by masklinn on Saturday July 17, 2021 @06:34AM (#27864864)
> UTf-8, NOT null terminated, pascal like. > > ie: what rust have: > > https://doc.rust-lang.org/std/string/struct.String.html

Rust (or C++) strings are not pascal strings. In pascal strings, the "string buffer" also contains the length information, and historically it was all bytes with a length byte at the start, which was why your strings started at index 1 and limited to 254 bytes.

It's possible to modernise this style of strings to be less crummy (that is essentially what sds does), but C++/Rust string are a third take where the length (and capacity) are stored separately from the string buffer, and that buffer is always on the other side of a pointer (ignoring SSO, which Rust sadly doesn't have due to the original interface definition).

Re:WebAssembly from Scratch: From FizzBuzz to DooM by pjmlp on Wednesday July 14, 2021 @11:30AM (#27834213)
Unfortunely that is not what WASM designers decided when they went without memory tagging for linear memory segments.

So you get all the fun to corrupt linear memory C style.

Re:WebAssembly from Scratch: From FizzBuzz to DooM by flohofwoe on Wednesday July 14, 2021 @12:38PM (#27835045)
I think if you're afraid of memory corruption inside the WASM heap, it's better to use Rust instead of C or C++. WASM's job is to prevent code inside the sandbox from escaping the sandbox, not to prevent memory corruption inside the sandbox.
Re:WebAssembly from Scratch: From FizzBuzz to DooM by pjmlp on Wednesday July 14, 2021 @01:45PM (#27835894)
> I think if you're afraid of memory corruption inside the WASM heap, it's better to use Rust instead of C or C++.

Agreed, but as consumer from WASM modules that isn't your option to make.

> WASM's job is to prevent code inside the sandbox from escaping the sandbox, not to prevent memory corruption inside the sandbox.

That is not better than a typical OS process, just it happens to be randomly downloaded into my computer.

Re:WebAssembly from Scratch: From FizzBuzz to DooM by afiori on Thursday July 15, 2021 @08:03PM (#27851615)
It is a lot better: there is a sandbox with minimal surface area compared to no sandbox at all (except for memory isolation)
Re:WebAssembly from Scratch: From FizzBuzz to DooM by pjmlp on Friday July 16, 2021 @02:34AM (#27853559)
When they are finished with the WebAssembly roadmap there will be the same sandbox as a typical OS process, no different of running a ART executable on Android, bitcode on watchOS, MSIL on Windows, or TIMI on IBM i.
Re:WebAssembly from Scratch: From FizzBuzz to DooM by afiori on Friday July 16, 2021 @08:06AM (#27855425)
Of course, that is the plan, but even then it will still be possible to run WebAssebly modules with no permissions or limited permissions, as the sandbox was always there.

On the other hand I need to admit that I would have not forseen some of the more recent use cases for WebAssembly

https://bytecodealliance.org/articles/making-javascript-run-...

which can be reminescent of the "docker daemon running as root" issue.

Re:WebAssembly from Scratch: From FizzBuzz to DooM by miohtama on Thursday July 15, 2021 @01:08AM (#27841552)
DOM manipulation is slow is pretty much busted misconception today:

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

Re:WebAssembly from Scratch: From FizzBuzz to DooM by flohofwoe on Thursday July 15, 2021 @06:51AM (#27843355)
It is "slow" relative to the overhead of calling from WASM into JS to manipulate the DOM from JS.

To be fair, I don't know how many clock cycles creating, destroying or modifying a DOM node costs on average, but most likely "a lot" compared to the overhead of a WASM to JS call because a lot more machinery is involved.

Re:WebAssembly from Scratch: From FizzBuzz to DooM by miohtama on Thursday July 15, 2021 @06:52PM (#27851042)
You are correct, today. But the overhead from WASM to JS is disappering with host binding, also known as WebAssembly Interface Types proposal.

https://www.chromestatus.com/feature/6219189974990848

Re:WebAssembly from Scratch: From FizzBuzz to DooM by TazeTSchnitzel on Wednesday July 14, 2021 @08:59AM (#27832515)
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.

Re:WebAssembly from Scratch: From FizzBuzz to DooM by franz899 on Wednesday July 14, 2021 @08:23AM (#27832251)
WebAssembly's purpose was never to replace JavaScript but only to speed up certain parts of a website/app.
Re:WebAssembly from Scratch: From FizzBuzz to DooM by pjmlp on Wednesday July 14, 2021 @08:50AM (#27832446)
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.
Re:WebAssembly from Scratch: From FizzBuzz to DooM by cubano on Wednesday July 14, 2021 @07:28PM (#27839734)
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.
Re:WebAssembly from Scratch: From FizzBuzz to DooM by recursivedoubts on Wednesday July 14, 2021 @08:38AM (#27832369)
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

Re:WebAssembly from Scratch: From FizzBuzz to DooM by Deukhoofd on Wednesday July 14, 2021 @08:42AM (#27832390)
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.

Re:WebAssembly from Scratch: From FizzBuzz to DooM by jollybean on Wednesday July 14, 2021 @07:53PM (#27839920)
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.

Re:WebAssembly from Scratch: From FizzBuzz to DooM by HideousKojima on Wednesday July 14, 2021 @09:20AM (#27832698)
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).
Re:WebAssembly from Scratch: From FizzBuzz to DooM by Deukhoofd on Wednesday July 14, 2021 @09:59AM (#27833067)
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.

Re:WebAssembly from Scratch: From FizzBuzz to DooM by staticassertion on Wednesday July 14, 2021 @10:02AM (#27833099)
I feel the same way. I find it very odd that GC is something that WASM ever intends to think about. If shipping your entire runtime sucks, find a smaller runtime?
Re:WebAssembly from Scratch: From FizzBuzz to DooM by steveklabnik on Wednesday July 14, 2021 @11:15AM (#27834000)
Think of it more of "integration with a host environment's runtime" than a "adding a runtime to wasm directly."

(At least, that's what it used to be; I haven't been involved in WebAssembly for a long time.)

Re:WebAssembly from Scratch: From FizzBuzz to DooM by pjmlp on Wednesday July 14, 2021 @11:33AM (#27834254)
The problem is that each runtime has different GC requirements, so at best it will mean WASM GC semantics will be the underlying JS GC semantics, probably not what you want for a D or .NET GC, for example.
Re:WebAssembly from Scratch: From FizzBuzz to DooM by titzer on Wednesday July 14, 2021 @06:26PM (#27839228)
No one has ever articulated the details of what they mean by these runtimes having different GC requirements. JavaScript garbage collection has no "semantics"--it is entirely invisible to applications. Even WeakMap and WeakSet do not expose garbage collection details because they are not iterable.

The memory profile of JavaScript applications tends to look a lot like the memory profile of typical Java applications. It tends to be a law of large numbers.

Now if you want to talk about details of how we implement runtimes that do have observable GC details, like weak callbacks, Java's zoo of reference types, etc, then let's do that, because Wasm GC will eventually need to have low-level mechanisms to support those.

But if we're talking about a Wasm engine GC's ability to allocate, trace, move (or not!) little blocks of memory around, then I don't see any fundamental stumbling blocks to making that mechanism efficient and universal.

Re:WebAssembly from Scratch: From FizzBuzz to DooM by pjmlp on Thursday July 15, 2021 @01:18AM (#27841629)
For example, the existing JS GC doesn't need to expose ability to stop the GC, execute on demand, support value types, pinning memory, interior pointers, control GC regions, marshaling to native code to the developer, whereas a .NET or D GC does.

So if a future WASM GC doesn't offer APIs for such capabilities, it is useless from those runtimes point of view.

Re:WebAssembly from Scratch: From FizzBuzz to DooM by titzer on Thursday July 15, 2021 @09:54AM (#27844719)
Thanks for getting down to brass tacks. Of the things that you mentioned, I think that interior pointers are the only thing that is relevant.

Java has an API for executing the GC on demand, and VM engineers I have talked to over the years think it's a knob that apps shouldn't have.

Wasm already supports multiple return values, so you don't need to box value types on any boundary--they can be flattened whereever they occur.

Pinning memory has to do with interfacing native code that could potentially do unsafe things. That doesn't fit into wasm's model, and would only be necessary for interacting with platform APIs, which are being designed not to need that. Same for "marshalling to native code".

I don't understand what you mean by GC regions. Realtime Java had GC regions and a complex system for trying to allow threads to run without touching the heap. It really didn't go well. I think if regions are useful for a GC, the engine should do inference of them, because adding regions to the type system infects everything.

Re:WebAssembly from Scratch: From FizzBuzz to DooM by steveklabnik on Wednesday July 14, 2021 @11:39AM (#27834335)
I would assume that there’s Microsoft folks involved to make sure it works out satisfactory given their investment in Blazor, but yes, it’s always a possibility that an API is bad. I don’t know what their level of interest is in embedding wasm inside C# is.
Re:WebAssembly from Scratch: From FizzBuzz to DooM by pjmlp on Wednesday July 14, 2021 @01:43PM (#27835870)
That was just an example, there are a plethora from GC algorithms to chose from, which of them needs to be fine tuned for the specific runtime it is to be applied, if performance is of any concern to the language implementers.
Re:WebAssembly from Scratch: From FizzBuzz to DooM by titzer on Wednesday July 14, 2021 @06:39PM (#27839366)
I have worked on a number of runtimes and it is not generally the case that a GC needs to be "tuned" for a runtime, rather that a GC co-evolves with a runtime and features or misfeatures of the runtime determine the path of least resistance for developing more advanced GC algorithms. The interplay tends to involve a lot of technical debt if the separation is poor from the outset. But regardless, it's rare that a runtime develops more than a couple GC algorithms unless it has a very long lifetime or is explicitly designed to allow swappable GCs, like Jikes RVM with Mmtk.

GC performance depends more on the program than the language.

But regardless, the hardest parts of getting to advanced GCs, such as concurrent and parallel algorithms are usually very deep assumptions of single-threadedness and uninterruptibility that are debt in the runtime. It usually doesn't help that most runtimes are written in C/C++ and suffer that environment's complete uncooperativeness[1] in finding and manipulating roots.

[1] To the point of seeming hostility. It's been how many years and LLVM still fights against supporting stack maps?

Re:WebAssembly from Scratch: From FizzBuzz to DooM by staticassertion on Wednesday July 14, 2021 @12:01PM (#27834598)
Yeah, could be interesting. I guess it works for the JVM? I feel like people will still want to use their own runtimes but idk
Re:WebAssembly from Scratch: From FizzBuzz to DooM by pjmlp on Wednesday July 14, 2021 @11:31AM (#27834225)
My Intel CPU also doesn't have a GC, so that is how it is.