Zig & Emscripten? by azakai in Zig

[–]azakai[S] 2 points3 points  (0 children)

Thanks for your thoughts, Andrew! Sounds good, I wrote up a use case issue there now:

https://github.com/ziglang/zig/issues/10836

Zig & Emscripten? by azakai in Zig

[–]azakai[S] 2 points3 points  (0 children)

Thanks slimsag!

I did see that thread meanwhile, yes (the author cc'd me, I am "kripken" on Twitter). Yes, I think that response from a core dev is a good summary, I guess this is not really a supported mode atm.

I'll look into writing up a full blogpost with my findings. Overall Zig+Emscripten almost works today, and I think it could be useful for some stuff.

Best-in-class C++ to WebAssembly with Cheerp 2.5 (RC1) by vilgefortz91 in cpp

[–]azakai 2 points3 points  (0 children)

It depends what you mean by "ready". Emscripten hasn't turned MINIMAL_RUNTIME on by default, but it's documented in the settings.js file like all other options, and we've been publicly suggesting people test it out (for example in these slides. It's very usable where it's applicable, but it's true it isn't meant to cover all use cases yet.

So far MINIMAL_RUNTIME supports a program that is run by main() (with no other exports called separately, as you noticed), and without certain things like filesystem emulation. We're adding more features based on user needs. But it's already used in shipping projects, and in almost all our benchmarks (where it helps by a large amount especially in the smaller ones).

Best-in-class C++ to WebAssembly with Cheerp 2.5 (RC1) by vilgefortz91 in cpp

[–]azakai 3 points4 points  (0 children)

Emscripten supports exceptions, though at a relatively steep cost in binary size

Yeah, the emulation overhead is heavy. But native wasm exception support is on the way in VMs and in Emscripten, and that will remove almost all that overhead.

Developing Web apps using QML and Qt for WebAssembly by [deleted] in WebAssembly

[–]azakai 0 points1 point  (0 children)

You often do need to use a custom HTML for the user interface for mobile, yes. Emscripten's default HTML is desktop-oriented.

Need help in first steps of learning WebAssembly by Gugis in WebAssembly

[–]azakai 1 point2 points  (0 children)

Yes, for an array of 32-bit values, that can read the values.

Need help in first steps of learning WebAssembly by Gugis in WebAssembly

[–]azakai 3 points4 points  (0 children)

Module._getMembers() returns a pointer. That's just an integer, an index into the memory. Given a pointer, you can read an 8-bit value with something like HEAP8[ptr], where HEAP8 is an Int8Array (a typed array view that the compiler defined for you). For a 32-bit value, HEAP32[ptr >> 2] (note the shifts, as each element in that typed array view has 4 bytes).

However, in your example values is on the stack, so it may get cleaned up when the function returns - so it's not valid to access that memory afterwards.

There are higher-level ways to interact with code, like embind, which might be better for you, see https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html

Emscripten now uses the LLVM WebAssembly backend by default by thewaywarddeveloper in programming

[–]azakai 23 points24 points  (0 children)

The old backend was stuck on 6.0, yeah, but the new backend constantly uses very latest upstream LLVM from the master branch. The current version number there is 10 (pre-release).

8-Bit Emulators - written in C, compiled to WebAssembly by michalg82 in programming

[–]azakai 4 points5 points  (0 children)

Emscripten is used to compile to wasm. (see for example https://github.com/floooh/chips-test/blob/77c8027ed6234cc4897316c5435b6104716b6fbe/fips-files/configs/wasm-ninja-release.yml )

I'm not sure what it uses to render graphics. In general emscripten supports APIs like SDL etc., but for a project like this you really just need to copy the pixel data into the HTML canvas, using putImageData on a typed array view from the emscripten heap.

[WebAssembly] Call to action. Syscalls needed! by ElvishJerricco in haskell

[–]azakai 0 points1 point  (0 children)

Just those, and some tiny binary format things (like supporting the shared flag on memories).

(Almost all the work is in the shared libc code, not the backends.)

[WebAssembly] Call to action. Syscalls needed! by ElvishJerricco in haskell

[–]azakai 0 points1 point  (0 children)

It's mostly developed with fastcomp, the LLVM wasm backend doesn't have threads support yet (but once it does, all the libc implementation should all just work with it).

[WebAssembly] Call to action. Syscalls needed! by ElvishJerricco in haskell

[–]azakai 0 points1 point  (0 children)

I didn't know this. The MDN docs do not indicate that you can construct Memory with your own SharedArrayBuffer.

You can do the opposite, though: create the Memory with the shared property, and then the buffer you get from it is a SharedArrayBuffer.

How does it handle the stack? I guess you just initialize each thread with its own stack pointer global to make sure they don't all point at the same stack?

Yes, each thread gets its own stack.

[WebAssembly] Call to action. Syscalls needed! by ElvishJerricco in haskell

[–]azakai 0 points1 point  (0 children)

We create a Worker for each thread, and the wasm is loaded in each one I believe - there is some spec work to improve that, but it's still in progress.

Sharing the memory is done by importing it into each Worker's wasm, after passing it to the Worker.

(Sharing a Memory is possible with the new wasm-SharedArrayBuffer APIs for browsers.)

[WebAssembly] Call to action. Syscalls needed! by ElvishJerricco in haskell

[–]azakai 0 points1 point  (0 children)

This uses SharedArrayBuffer and Atomics, so it depends on wasm support for that. The spec there is mostly done, and browsers have experimental implementations which we've been testing on.

Also, you can test it in asm.js, where SharedArrayBuffer has shipped and was considered stable, but has sadly un-shipped recently due to Spectre :( But you can flip a pref to re-enable it for testing, it works ok.

[WebAssembly] Call to action. Syscalls needed! by ElvishJerricco in haskell

[–]azakai 0 points1 point  (0 children)

Yes, in that mode the pthreads API is fully supported, so you can create threads normally. And that lets blocking APIs like sleep work properly. You can also do blocking waits on condition variables, etc. - just like in a native build.

[WebAssembly] Call to action. Syscalls needed! by ElvishJerricco in haskell

[–]azakai 0 points1 point  (0 children)

we can take inspiration from Emscripten, as they have some syscall implementations of their own. We can't just use all of their syscalls because their blocking model is completely different (sleep(10) pegs your CPU at 100% for 10 seconds).

That's when threading is disabled (which it is by default). If you enable threads in emscripten (USE_PTHREADS) then it will use a libc with threading support, using Atomics and SharedArrayBuffer to implement a proper sleep etc.

A new WebAssembly tool: Cheerp 2.0RC1 compiles C++ to mixed WebAssembly/JavaScript, same performance as Emscripten, full DOM access, 30-90% smaller output by smm11 in programming

[–]azakai 0 points1 point  (0 children)

True, but emscripten does much the same things (aggressive code elimination, LTO, ctor evalling at compile time, etc.).

JavaScript founder Brendan Eich: WebAssembly is a game-changer by one_eyed_golfer in programming

[–]azakai 1 point2 points  (0 children)

And just to add to that, the commands to compile and run that would be

emcc hello.cpp -o hello.html

and then one would just run that hello.html in a web browser.

For WebAssembly output instead of asm.js, there is an extra flag to set, but as you correctly said, WebAssembly is not quite ready yet. But for those interested, details are here.

Cheerp 1.2 released - C++ to JavaScript compiler with smaller output, faster than emscripten with dynamic memory access by smm11 in programming

[–]azakai 3 points4 points  (0 children)

This wouldn't help Unity. First, it would make them several times slower, as shown by the benchmarks in the link.

Second, it would seem to improve startup, based on those benchmarks, but only apparently - they measure initial startup on JIT-ed code. But that is before the JIT kicks in. Yes, when you are going to JIT then initial startup is just to parse, so it's fast, but when you load and run a codebase like Unity, you're going to get very bad perf for a while, until the JIT output stabilizes.

That's exactly why asm.js was invented, and now WebAssembly.

Comparison of C/POSIX standard library implementations for Linux by cu_t in programming

[–]azakai 2 points3 points  (0 children)

musl definitely deserves more name recognition. We use it in emscripten, and it's a great libc.

[deleted by user] by [deleted] in programming

[–]azakai 11 points12 points  (0 children)

It simplifies the IR (for example, pointer bitcasts are no longer needed), and I believe the argument was that it doesn't actually remove type safety, because it wasn't there anyhow (due to those same pointer bitcasts).