That Conference 2018, Kalahari Resort, Lake Delton, WI
WebAssembly: The New Endgame? – David Pine
Day 2, 7 Aug 2018 2:30 PM
Disclaimer: This post contains my own thoughts and notes based on attending That Conference 2018 presentations. Some content maps directly to what was originally presented. Other content is paraphrased or represents my own thoughts and opinions and should not be construed as reflecting the opinion of the speakers.
Executive Summary
- WebAssembly allows writing high-level source code and compiling to a binary format that runs in execution engine on the client
- Allows doing C#, for example, in browser
- Blazor works with Mono to allow .NET on client, including Razor syntax
Wat?!
- Engines for WebAssembly
What
- Binary instruction for a stack-based virtual machine
- Binary
- AST (Abstract Syntax Tree)
- Stored as binary
- Stack-based
- Pushing and Popping onto stack
Today
- Binary execution format for web
Future
- Mobile and IoT
Wasm
- Portable target compilation of high-level languages
- For C, C++, C#, Rust, Go
Example
- Compile C#, target WASM, you get binary file
- File can run in your browser
Demo
- Game
- In browser debug, you see demo.wasm and not much else
Chrome has it
- What about other platforms?
- Big movement, WebAssembly now supported everywhere
- W3C – World Wide Web Consortium
Text Format (.wat)
- There’s a text-format human readable version of the final assembly
- Hard to read, since it’s really the compiled output
What it is not
- Not assembly-level CPU-specific machine code
- Doesn’t run on the CPU but runs in browser’s execution engine
- But you get near-native performance, since it’s close to the metal
- Not a 3rd-party plugin
- WebAssembly runs in same sandbox as JavaScript
- Not versioned
- Backwards-compatible
- Not JavaScript replacement
- WA plays nicely with JS
- Not a programming language
- If you hand-coded .WAT files, it would be
- But it’s really just a format or compilation target
- Not the ideal tool for every web project
- Different tools, e.g. JavaScript, C, to do HTTP request
Emscripten
- Tool for compiling to WASM
Demo: Emscripten
- emcc
- Takes C source file as input (e.g. hello.c)
- Target WASM
- Run output in browser–we get console output in the browser
WASM Explorer / Fiddle
- Call WASM assembly from JavaScript
Use Cases
- Games, rich client, etc.
Hanselman interview of Steve Sanderson
- Web development has been constrained because everything has had to be in JavaScript
- Web development will be able to grow up and mature a bit
My interview with Sanderson
- Give developers a wider choices of languages and platforms
- Industry healthier if multiple paradigms catered to
How
- Mono Runtime
- Cross-platform .NET runtime
- Using it now for Blazor
Blazor
- Browser + Razor
- Experimental project at the moment
- Not yet a committed product
How Blazor works
- Dev time
- C# / Razor source files
- Runtime in browser
- mono.wasm – WebAssembly binary executed natively
- Browser APIs – visible DOM, HTTP requests, etc
- YourApp.wasm – WebAssembly version of your library
Demo – Blazor
- bit.ly/BlazingChuck
- Project template in VStudio
- ASP.NET Core Hosted
- You get Client stuff and Server stuff, as well as Shared stuff
- Shared library has web objects shared across server and client
- Template gives you a simple multi-page web site with a few things
- Fetch some data, click a button, etc.
- Server has controller that returns some basic data
- Razor syntax that turns into HTML
- Inject HttpClient into component on client
Look at code for Blazor
- Debug C# code in browser
- Hit breakpoint after interacting with something in the browser
- Program.cs with main entrypoint
- CreateHostBuilder
- Wire up various services
- Add component (app)
- Standard markup with Razor binding to C# object (model)
Other debugging
- Currently not pre-compilation, but running in interpreted mode
- Full-on .NET DLLs on client, e.g. mscorlib
- For small app, we’re at 3.5 MB
- But this will get optimized
Mono.wasm – is the .NET runtime running in a WebAssembly
Resources
With Unity, can target WASM
- Unity in browser