1. 1

    Has anyone embedded a WASM engine, whether Wasm3 or otherwise? I’d be interested in hearing experiences.

    1. 4

      I worked on a production “hosted function” system first in go with life then rewrote to rust with lucet. Was very nice to work with

      1. 4

        I’ve used wasmtime from Rust a couple times, though not for anything large. I made a language that compiled to wasm and used wasmtime to run its test cases. The interface is Rust-unsafe but pretty easy: load a wasm module, look up a function by name, give it a signature, and then you can just call it like any other function. Never got complicated enough to do things like pass pointers around though, or make host functions accessible to the wasm code.

        1. 3

          Microsoft Flight Simulator (2020) somewhat embeds a Wasm engine. Addons are written in Wasm and compiled to native code using inNative, a LLVM frontend for Wasm, which solves problems with multi-platform support and restrictions on JIT compilation on consoles. The PC builds embed inNative’s JIT, which uses LLVM’s JIT interface, for faster development edit/test cycles.

          1. 3

            That’s sensational! I had been wondering when we were going to improve on LUA for modding.

            I’m also somewhat keen on the idea of sandboxing native libraries that are called from high-level languages; if the performance overhead can be brought suitably low, I would really like to be relatively safe from library segfaults (especially for image processing).

          2. 1

            What do you mean by embedded. Like, in an app or on some hw device?

            1. 4

              Just in a C++ app :) Actually I have an idea to embed WASM in https://www.oilshell.org/ . One use case is to solve some bootstrapping problems with dev tools. For example, if you have a tool written in a native language like a parser generator, then it’s somewhat of a pain for people to either build those, or for the maintainer to distribute binaries for them (especially if they change often).

              So it seems natural to write a shell script and call out to an “embedded” arch-independent binary in those cases. (Though this probably won’t happen for a long time.)

              (BTW the work on wasm3 seems very cool, I looked at the code a bit, and hope to learn more about WASI)

              1. 1

                I think wasm3 is perfect for this scenario. Especially if you realize that wasm “plugins” can be written in a variety of languages. C/C++, Rust, TinyGo, AssemblyScript, Swift…

                1. 1

                  Yes the polyglot nature is very natural for shell :) How stable is WASI now?

                  Is it easy to compile and run some C code like this with wasm3 ? Can I just use clang and musl libc or are there some other tools? Any examples to start from? I have run wasm in the browser but I didn’t compile any C.

                  int main(char** argv, int argc) {
                     read(0, 1024);  // read from stdin
                     write(2, argv[0]);  // write to stderr
                  
                     char *p = getenv("PATH");
                     write(1, p);
                     return 0;
                  }
                  

                  So I want to call main directly; so I guess I need a wasm stub that calls it?

                  I think I want to provide only a argv/ENV/stdin/stdout/stderr interface to simulate a sandboxed C program. I’m not sure binary blobs loaded into the shell to be able to read and write arbitrary files. The files should be opened in the shell, like this:

                  my-wasm-program.wasm <input.txt >output.txt
                  

                  This also has some bearing on incremental computation like Make, e.g. knowing the inputs and outputs precisely from shell, rather than having to analyze C code or WASM code.

                  1. 1

                    This is exactly what you want. You can compile C to Wasi easily using wasienv. Also, it’s a matter of runtime configuration, not to allow FS access. Std in/out are open by default, but can also be blocked.

                    1. 2

                      Hm so how do I embed it in an application and use the C API? I looked at the README.md, the doc/ folder, and this header:

                      https://github.com/wasm3/wasm3/blob/main/source/wasm3.h

                      I don’t see any C code examples?

                      In contrast the Python binding has an example in the README:

                      https://github.com/wasm3/pywasm3

                      1. 2

                        Good idea. I’ll create some kind of tutorial ;)

                        1. 0

                          I don’t see any C code examples?

                          Check out this: https://github.com/wasm3/wasm3/blob/main/docs/Cookbook.md

            1. 4

              Can someone point me any popular real-world web apps using Wasm?

              Also how easy is it to interoperate with existing JS libraries when you compile your app to Wasm?

              What language are you compiling your code to Wasm from?

              1. 6

                I think Figma uses wasm (see https://www.figma.com/blog/webassembly-cut-figmas-load-time-by-3x/).

                I haven’t done anything serious in wasm, but I recall it being surprisingly easy. You can have a look here, I think this is what I followed to get set up: https://rustwasm.github.io/docs/book/game-of-life/hello-world.html.

                I used Rust, but it’s the only non gc language I know, so I did not have much choice.

                1. 5

                  I believe https://sandspiel.club/ and https://orb.farm/ use wasm, generated from Rust. There’s significant amount of JS and shader code as well though. Details in https://maxbittker.com/making-sandspiel

                  1. 3

                    Ok so, I could not point you to a real-world web app that is fully wasm but rather, uses libraries like libsodiumjs which is wasm. uBlock Origin also uses wasm but that’s an extension and wasm only works for extensions in firefox for some funny reason.

                    1. 1

                      Check out this list: https://madewithwebassembly.com

                    1. 0

                      Offtopic: Is there info on the actual file WASM format?

                      Ontopic: Interesting project! Do you have any idea whether tail calls will be easier to implement in an interpreter vs. an optimizing compiler?

                      (Obligatory Why The Fuck is this written in C?)

                      1. 1

                        https://webassembly.github.io/spec/core/ - the specification ;) It’s written in C, as it needs to be uber-portable (and it is!)

                        1. 1

                          https://webassembly.github.io/spec/core/ - the specification

                          Seems like they still haven’t managed to spec meta data fields for version information or organization&author?

                          That’s kinda underwhelming, but not really surprising considering its web origins. :-/

                          uber-portable

                          UB all the chips? ;-)

                      1. 1

                        The Cosmopolitan build is intriguing but a bit confusing:

                        $ file wasm3-cosmopolitan.com 
                        wasm3-cosmopolitan.com: DOS/MBR boot sector
                        

                        which is supposedly in APE format. from the readme: “APE is a polyglot format that runs natively on Linux + Mac + Windows + FreeBSD + OpenBSD + NetBSD + BIOS.” not sure how to run this. simply setting the execute bit gives “zsh: exec format error: ./wasm3-cosmopolitan.com”

                        1. 6

                          zsh currently has some POSIX incompatibility, but you can run it with

                          sh -c "./wasm3-cosmopolitan.com <args>"
                          

                          also if you have wine installed or some specific environment, you may need to register the binfmt:

                          sudo sh -c "echo ':APE:M::MZqFpD::/bin/sh:' >/proc/sys/fs/binfmt_misc/register"
                          
                          1. 2

                            Same error here. @jart got a patch merged into ZSH, so it’s probably that you don’t have master. It works on modern Bash versions though

                          1. 1

                            Wow. Nice work. Is there a path to get a window/framebuffer from this?

                            1. 2

                              I saw some example for Windows, but it’s not cross-platform.

                            1. 1

                              Ooh! Cool to see someone is actually using APE out in the wild.

                              I thought it was a novelty at the time but maybe it’ll be more than that?

                              1. 15

                                Well, to be fair, the author of this tool is the person who created APE as well.

                                1. 3

                                  Wasm3 is using it to provide a portable, universal WebAssembly engine (I’m one of the authors).