A cartoon intro to WebAssembly
hacks.mozilla.org
hacks.mozilla.org
https://hacks.mozilla.org/2017/02/a-crash-course-in-just-in-...
https://hacks.mozilla.org/2017/02/a-crash-course-in-assembly...
https://hacks.mozilla.org/2017/02/creating-and-working-with-...
https://hacks.mozilla.org/2017/02/what-makes-webassembly-fas...
https://hacks.mozilla.org/2017/02/where-is-webassembly-now-a...
Most of the people I know want to write Java/Python/Ruby/Elixir/Scala/C#/Clojure in the browser: high level scripting/VM based langauges which require no memory management and have high level features and data structures. As far as I can tell, these can't target WebAssembly since they don't target LLVM (and you certainly will never be able to compile a language like Ruby to LLVM)
I'd love to hear some of you guys talk about what uses you have for Webassembly and what exciting things it will let you do.
Nevertheless, here are a few of the main reasons you might want to work in a language that can easily target WASM:
1. Wholesale porting of desktop applications and game engines to the Web. This is the guise under which we developed asm.js, and it resonated well enough that Epic and Unity added support for asm.js to their engines.
2. Re-using existing, well-tested libraries that are written in C/C++. Why settle for zip.js if I can use libarchive directly? What about libraries that don't necessarily have high-quality JavaScript equivalents, like OpenCV, Box2D, or libsass?
3. Shipping high-performance codecs for multimedia formats like FLIF, BPG, and AV1 that aren't natively supported by browsers. Mozilla is actively using this strategy to develope the AV1 codec: https://hacks.mozilla.org/2017/02/webassembly-will-ease-coll...
4. Optimizing hot paths in JavaScript codebases, in the same way that Python or Ruby programmers will often re-implement individual functions or modules in C to overcome bottlenecks. For an example of this, consider the Flask microframework in Python, which depends on the Jinja2 templating library, which depends on Markupsafe for escaping HTML. Buried inside markupsafe is a 200 line file, "_speedups.c", that speeds things up. So even if end-developers aren't writing C/C++/Rust, they can still benefit from libraries that incorporate WASM.
5. Saving battery life. The fewer cycles it takes to finish a computation, the longer the CPU can spend sleeping. WASM provides significantly greater control over exactly what executes, and you can opt to burn CPU cycles on optimization once, during compilation, to get more a more efficient .wasm artifact for everyone. Since WASM is statically typed, the compiler also has many more optimizations available to it; no more worrying about monomorphism or type inference as with a JS VM's JIT compiler.
$ file /usr/bin/ruby
/usr/bin/ruby ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=4b353b68b7d8b46a570883c82efe48db9d77ef22, stripped
Looks like somebody managed it ;)
Snark aside, the trick for running managed languages on the WASM virtual architecture is the same as running them anywhere else: compile the runtime to the target, then ship the runtime with your program. That's what Go does, and why its hello world binary is 1.6 MB.
For a working example, PyPy.js (https://pypyjs.org) successfully ports Python to asm.js. It costs about 5MB in overhead, so it's not a practical alternative to JavaScript, but at this point it's mainly an optimization problem. If a language can statically analyze what libraries and runtime facilities a given program requires, then it can strip out everything else, leaving behind a reasonably sized binary for delivery over the Web.
That said, I agree that more people will want to work in higher level languages, though I kinda suspect "most" are going to still prefer Javascript for the forseeable future - so I sorta see JS compiling to WASM as inevitable.
Also, the author notes that future versions of WebAssembly may contain garbage collection features, which would be relevant to TypeScript.
"Garbage collection: If you can define your types ahead of time, you should be able to turn your code into WebAssembly. So code using something like TypeScript should be compilable to WebAssembly. The only hitch currently, though, is that WebAssembly doesn’t know how to interact with existing garbage collectors, like the one built in to the JS engine. The idea of this future feature is to give WebAssembly first-class access to the builtin GC with a set of low-level GC primitive types and operations."
Kind of like how it does on a regular compiler
A clue lies in the fact that all the major browser vendors (Google, Microsoft, Mozilla, Apple) are implementing WebAssembly. What is the strategic reason for this?
The speculation is that it all has to do with the rise of native mobile apps, which posed a threat to the web-entrenched interests. Google, I'm guessing, would prefer that you to stay in the web browser, using Google Search to to find all your applications, running them Google Chrome, and remaining a captive audience for Google's advertising clients. (instead of are searching for apps in the App Store and running them natively).
One important reason that native apps initially took off was that games, and other interactive content, could not be efficiently implemented for mobile web browsers. WebAssembly (in combination with WebGL) pretty much solves this problem. People have already ported a number of C++.OpenGL games to the web using emscripten.
> I, and everyone else I know, has little to no interest writing C/C++/Rust in the browser
I understand that most 3D game development is done in C++. I'm guessing that you don't know a lot of 3D game developers?
Besides games, many professional applications are developed in C++, for a variety of reasons. Examples could include scientific, engineering, graphics and audio processing software.
> Most of the people I know want to write Java/Python/Ruby/Elixir/Scala/C#/Clojure in the browser
Python is an interpreted language and CPython is implemented in C. You can cross-compile to WebAssembly today and start running Python scripts in the browser. Performance critical paths in Python scripts, like NumPy operations, which are offloaded to compiled C functions, would be offloaded to compiled WASM functions.
As for garbage collected languages, like Java and C#, future versions of WebAssembly will provide low-level GC primitives, which should theoretically allow those languages to run in the web browser as well. Having said that, you would incur the overhead of having to run the JVM/CLR virtual machine inside the browser engines's own JavaScript/WebAssembly virtual machine.
> I'd love to hear some of you guys talk about what uses you have for Webassembly and what exciting things it will let you do.
I suspect there is a lot of interest from C/C++/Rust developers already, and will continue to grow as WASM matures. Something like 10 million professional developers know and use C++ regularly, so this is a not insignificant usergroup on its own.
- Games
- Emulator cores
- Crypto
- Compression
- Software graphics
- Audio processing
- Computer vision
- Physics engines
- Other heavy math/matrix/constraint solvers
- In-memory databases
Besides games, all of these are library uses, and tend to have fixed structure definitions and often even fixed memory buffers to work with (crypto in particular). The developer is still writing the main application in JavaScript (or whatever other dynamic/GC'd language you wish to compile into JS) and gets the full speed and memory footprint benefit of pulling libraries such as these as opposed to seeking pure JS versions. - DRMWhen it's possible to ignore the DOM by uploading your own renderer (e.g. custom freetype/etc compiled to webasm), it's possible to prevent adblocking based on HTML/CSS selectors.
Yes, well-designed sites will use WebAssembly in other more reasonable ways. Entirely separate from that are the websites that try to disable the right-click context menu to "protect" images, and the businesses that are watching there revenue disappear with the rise in adblocking. Some businesses will - often foolishly - use all available means to "protect their content", so that's how they will use WebAssembly.
1) Oberon had an idea of 'slim binaries' portable binaries that can be compiled into object code in a single pass. Loading would be almost as fast loading a file. Slim binaries were designed to be slim and fast.
2) Then came java, jvm and java applets People said that jvm is the plaform. Java will be is just one language using that platform. Compiled java objects were not so slim and fast to recompile :(
3) JavaScript became de facto portable platform but they are not binary. Browser became the client virtual machine.
4) Now we have WebAssembly. Lets' hope that WebAssemply is actually slim and fast representation like slim binaries.
https://lists.w3.org/Archives/Public/public-webassembly/2017...
> WebAssembly has no access to any of the web API (DOM, ...)
WebAssembly can access those APIs via indirect, opaque shims today. Direct access is on the roadmap.
> WebAssembly cannot start without javascript as of right now.
We're still hammering out semantics on the HTML side, but WebAssembly modules can define a `start` entrypoint that is automatically invoked when the module is loaded. Combine that with ES6's `<script type="module">` and you've got WASM that can load and begin executing without JS.
> Flash died because of one platform not supporting it. The very same platform still doesn't support WebAssembly.
Apple is a party to WebAssembly's development and a co-signatory on the linked announcement that the WebAssembly binary format and Web APIs are complete. This doesn't mean they'll necessarily ship in a timely manner, or at all, but it's a damn good sign.
> I don't want to overhype it
That's fair. The world won't change overnight, but it's clear that the world is changing in fundamental ways. The initial changes may be insignificant, but they'll compound over time and as WebAssembly matures.
JavaScript isn't dead, and WebAssembly won't kill it, but it will dramatically change the landscape. In a similar vein, C will never kill Python, but I'd be shocked if most moderately complex Python applications didn't depend on a module implemented in C somewhere in their stack.
All the arguments in favor about saving bytes and offline compiling would seem like only short term gains since network, cpu's, and memory sizes are going to continue to improve.
And, it's certainly not Flash or Java Applets all over again since there are multiple competent vendors in the mix. Yet, I fear a new wave of unconstrained, impenetrable code schlock will flow from content creators once this thing hits the mainstream.
As the article mentions, the drastic speed improvement that JIT gave us let us build even bigger, more complex web apps. Maybe things will continue to get bigger and more complex and we'll need wasm, or maybe wasm will allow us to build apps not possible now.
> I've found that even the most aggressively minified uber-scripts can be pretty printed and studied.
I haven't found this to be the case. Most minifiers rename variables to one-letter, which makes any moderately-complex web app unreadable, or at least not very fun. Furthermore, I don't think that technically requiring source-code distribution for execution is really the solution - Open Source is a social problem at root, not technical.
I, for one, welcome the possibilities.
Debugging tools will need to improve, and you'll need to learn WASM's (limited) vocabulary, but if you're happy enough with asm.js, then I think you'll be ultimately be happier in the future. I, for one, would much rather see actual type signatures like: "param $x i32" than asm.js's cryptic annotations "x = x|0".
In my eyes, the size and offline compilation aren't even the biggest benefits. Having a low level assembly language for web computation lets people write in any language and compile it for the web. There's already an LLVM backend for it - although still experimental. But this may even mean we can simply port existing codebases to WebAssembly with little performance loss.
You've been able to compile stuff for web for a while, by transpiling to javascript, but the result has been too slow for many applications, so it never really picked up.
WebAssembly has less structure than minified JS (instructions are a linear list - a stack machine - for example) and it is less hackable (it isn't intended to be written or modified by hand).
WebAssembly will be somewhere in between native x86 and minified JS in terms of how open it is to being studied.
This is addressed in the WASM FAQ:
"Will WebAssembly support View Source on the Web?
Yes! WebAssembly defines a text format to be rendered when developers view the source of a WebAssembly module in any developer tool..." [1]
Oh, how quickly we forget.
JavaScript was hacked together in short time for the browser. You can't get any less server-side than that?
[1] https://en.wikipedia.org/wiki/Netscape_Enterprise_Server
I don't know what number crunching web applications the vendors are thinking of. I want wasm for client-side web programming without JS.
Games, and WebGL games in particular.
Just a few small typos to point out:
https://hacks.mozilla.org/2017/02/what-makes-webassembly-fas...
"Executing" section - "...know how to write code so that they compiler can type specialize it" - the compiler instead of they compiler
"Conclusion" section - "etching WebAssembly takes less time because it is more compact than JavaScript, even when compressed" - missing the f in fetching
What assumptions is wasm making that will prevent Go from targeting it, and why are those assumptions fundamentally harder for Go to target than LLVM IR?
> there is no efficient way to implement setjmp/longjmp functionality currently, therefore it's not a viable target of a gc port. We need to wait until it gets real stack unwinding and exception handling support.