Share E-Book
Scan to open this page

Scan with your phone to open this page

AuthorGerard Gallant

Write high-performance browser-based applications without relying just on JavaScript! By compiling to the WebAssembly binary format, your C, C++, or Rust code runs at near-native speed in the browser. WebAssembly delivers greater speed, opportunities to reuse existing code, and access to newer and faster libraries. Plus, you can easily interact with JavaScript when you need to. WebAssembly in Action teaches you how to write and run high performance browser-based applications using C++ and other languages supported by WebAssembly. In it, you'll learn to create native WebAssembly modules, interact with JavaScript components, and maximize performance with web workers and pthreads. And you’ll love how the clearly organized sections make it a breeze to find the important details about every function, feature, and technique. What’s Inside • Dynamic linking of multiple modules at runtime • Communicating between modules and JavaScript • Debugging with WebAssembly Text Format • Threading with web workers and pthreads Written for developers with a basic understanding of C/C++, JavaScript, and HTML. Gerard Gallant is a Microsoft Certified Professional and a Senior Software Developer at Dovico Software. He blogs regularly on Blogger.com and DZone.com.

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A hands-on guide to compiling C and C++ into WebAssembly with Emscripten, so you can run near-native-speed code in the browser and wire it cleanly into JavaScript. Best for web or systems developers who already know some C/C++, JavaScript, and HTML and want a practical path from "hello module" to threading and dynamic linking. 【Book Arc】 - **Opening (~0%–10%)**: Frames the "why" — what WebAssembly is, the asm.js lineage, and the problems it solves (performance, startup time, non-JavaScript languages, code reuse). Establishes the mental model before any tooling. - **Early (~10%–30%)**: Moves into module internals — known vs. custom sections, the binary/text formats, and the WebAssembly JavaScript API (fetch, instantiateStreaming). Then builds a first module from C source through Emscripten to a working HTML page. - **Early–Middle (~30%–45%)**: The code-reuse core: adapting an existing C/C++ codebase with `__EMSCRIPTEN__`, `extern "C"`, and `EMSCRIPTEN_KEEPALIVE`, then calling module functions from JavaScript via `ccall`. - **Middle (~45%–60%)**: Memory and string marshalling — the four basic types, `_malloc`/`_free`, `UTF8ToString`, and replacing `strcpy`-style patterns with host callbacks like `UpdateHostAboutError`. - **Late (~60%–85%)**: Scaling up — dynamic linking of multiple modules at runtime, debugging with the WebAssembly Text Format, and performance work with web workers and pthreads. - **Ending (~85%–100%)**: Consolidation through worked examples, exercises, and appendices; excerpts do not cover the closing chapters in detail. 【Key Takeaways】 - **WebAssembly is a compiler target, not a language you hand-write** (Opening): its binary format is compact, single-pass validatable, and parallel-compilable, which is where the speed and startup advantages come from. - **Static typing enables compile-from-the-start performance** (Opening): unlike JIT'd JavaScript that must be monitored first, Wasm is compiled to machine code immediately, and streaming compilation lets modules initialize as they download. - **Module sections are the architectural backbone** (Early): known sections appear once in fixed order, custom sections can appear anywhere and repeat — understanding this pays off when reading text format or debugging. - **Emscripten is the practical bridge from C/C++ to the browser** (Early–Middle): it generates the JavaScript "plumbing" that downloads, compiles, and instantiates your module so you can focus on your own code. - **Reusing existing C/C++ is a first-class workflow** (Middle): `__EMSCRIPTEN__`, `extern "C"`, and `EMSCRIPTEN_KEEPALIVE` let one codebase serve desktop and web without a rewrite. - **Only four primitive types cross the boundary** (Middle): integers and floats pass directly, but strings and complex data require manual memory management via `_malloc`, `_free`, and `UTF8ToString`. - **Host callbacks beat buffer-passing for error handling** (Middle): replacing `strcpy`-into-a-buffer with a call like `UpdateHostAboutError` simplifies the C side and avoids lifetime headaches. - **Threading and multi-module linking are the advanced frontier** (Late): web workers, pthreads, and runtime dynamic linking are where real performance gains and real complexity live. 【Reading Tips】 - **Deep-read the module-sections and memory chapters** (Early and Middle): these are the conceptual load-bearing walls; skimming them makes later debugging and marshalling feel like magic. - **Skim the introductory "what is WebAssembly" material** if you already know the landscape — it's orientation, not technique. - **Type along with the Emscripten build steps**: the toolchain flags and file layout are easy to misread but obvious once you've run them. - **Treat the exercises and appendices as checkpoints**, especially around `ccall`, exports, and name mangling — they surface the mistakes you'd otherwise hit in production. - **Watch the Windows-style path convention** noted in the text; *nix readers should mentally swap separators. 【Coverage Limits】 This guide is built from stratified excerpts covering roughly the first half of the book in detail; the threading, dynamic-linking, and debugging chapters are represented mainly by the table of contents and blurb, so their specifics are not deeply summarized here.
Page 9
er 7 ■ Opportunity for code reuse 7 1.3 How does it work? 8 Overview of how compilers work 9 ■ Loading, compiling, and instantiating a module 10 1.4 Structur...
View in text
Excerpt 2
n be placed before, in between, or after known sections and are used to specify data that doesn’t apply to the known sections. 30 CHAPTER 3 Creating your fir...
View in text
Excerpt 3
ded by the fetch method. Compiling the module as it’s being downloaded speeds up load time because the module is ready to be instantiated as soon as it finis...
View in text
Excerpt 4
ires interactions with the module’s memory. The Emscripten- generated JavaScript code provides a number of functions that help with this. Using C or C++ to c...
View in text
Excerpt 5
to use, add an include for the side_module_system_functions.h file in the extern "C" block. The first part of your validate.cpp file should now look like the...
View in text
Excerpt 6
avorite editor. You’ll need to add an array, containing the name of the side module you want linked, to the dynamicLibraries property of the Module object. A...
View in text
Excerpt 7
oductForm div to have it hidden by default. Add the closing div tag after the save button tag. Change the onclick value of the Save button from onClickSave t...
View in text
Excerpt 8
e) { compiledModule = e.data; Places the compiled module emscriptenModule = new Module({ into the global variable instantiateWasm: onInstantiateWasm Creates...
View in text
Tags
AI categories
Web TechnologyProgramming LanguageC++
ISBN: 1617295744
Publish Year: 2019
Language: English
Pages: 424
File Format: PDF
File Size: 20.0 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…