Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: 作者: [加拿大] C.杰勒德·加伦特(C. Gerard Gallant) 译者: 单业

Rating No ratings yet

特别说明 ●抽丝剥茧,逐步解构WebAssembly的复杂生态 ●突破JavaScript瓶颈,让C/C++代码快速运行于浏览器端 ●边学边练,配套练习答案帮你熟练掌握WebAssembly 简介 在人们极力渴求提高JavaScript性能的情况下,WebAssembly应运而生,现已得到所有主流Web浏览器的支持。由于其卓越的性能和可移植性,WebAssembly也被用于Web浏览器之外的许多场景。本书围绕WebAssembly技术栈介绍如何通过C、C++等语言编写高性能的浏览器端应用程序。你将掌握WebAssembly的基础知识,学习如何创建原生WebAssembly模块,与JavaScript组件交互,使用WebAssembly文本格式进行调试,并利用多线程支持机制。 本书特色 WebAssembly是可移植、体积小、加载快并且兼容Web浏览器的全新类汇编语言格式,其具有高效、安全、开放、标准等特性。使用WebAssembly,你能够不再依赖JavaScript编写基于浏览器的高性能应用程序。通过编译为WebAssembly二进制格式,你的C/C++代码或Rust代码都可以在浏览器中以接近原生的速度运行。 本书介绍如何使用C/C++语言和Emscripten工具包编写并运行高性能的Web应用程序。你将学习如何创建原生WebAssembly模块、如何与JavaScript组件交互,以及如何使用Web worker和pthread最大限度地提高性能。 ●多个模块在运行时的动态链接 ●模块与JavaScript之间的通信 ●利用WebAssembly文本格式进行调试 ●利用Web worker和pthread进行多线程编程 作译者介绍 C.杰勒德•加伦特(C. Gerard Gallant),微软认证专家,Dovico公司高级软件工程师,DZone技术网站专栏作者。 【译者简介】 单业,计算机专业硕士,软件工程师,现居于上海,另译有《你不知道的JavaScript》(中卷和下卷)。

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 escaping JavaScript's performance ceiling by compiling C/C++ into WebAssembly, aimed at web developers who already know some JavaScript and want to ship near-native-speed browser code without abandoning their existing codebase. 【Book Arc】 - **Opening (~0%–10%)**: Frames why WebAssembly exists — the interpreter/JIT trade-off, the shift from glue language to full application platform — then walks through the binary format itself: modules, sections, s-expressions, and the sandboxing model that keeps Wasm inside the JavaScript VM's security envelope. - **Early (~10%–33%)**: The practical core. You set up Emscripten, compile a first C program, and learn the three output shapes (HTML template, JS plumbing, bare `.wasm`). Then the book reverses the arrow: instead of JS calling into Wasm, Wasm calls back into JavaScript, first via Emscripten's library merging, then via function pointers and Promises. - **Middle (~33%–52%)**: Advanced composition. Dynamic linking lets two or more modules join at runtime; multiple instances of one module can each link to a shared core module. The book builds this out with a real validation scenario where common logic lives in one module and page-specific modules link to it. - **Late (~52%–70%)**: Concurrency and beyond-the-browser. Web Workers prefetch modules on background threads; pthreads bring parallel processing inside the module. Then the same modules run under Node.js, with Emscripten's generated JS detecting its environment automatically. - **Ending (~70%–100%)**: Debugging and testing. A card-matching game is built purely in WebAssembly text format so you can read what the browser shows in View Source, then extended to demonstrate debugging options, and finally wrapped with integration tests. 【Key Takeaways】 - **WebAssembly's real win is predictability, not raw speed** (Opening): JavaScript's JIT optimizes hot paths but can deoptimize; compiled Wasm arrives already in machine code, so performance is consistent rather than best-case. This matters when you're choosing whether to port logic at all. - **The security model is architectural, not bolted on** (Opening): modules never touch device memory directly — the host passes an ArrayBuffer as linear memory, and the framework bounds-checks every access. Table entries are reached by index through the framework, not by raw pointer. Understanding this explains why Wasm can't just do anything a native binary can. - **Emscripten's plumbing is a convenience you should learn to remove** (Early): the generated JS hides loading, instantiation, and helpers like `ccall`. The book deliberately shows the bare `WebAssembly.instantiateStreaming` path so you can see what's actually happening — useful when plumbing gets in your way. - **Conditional compilation keeps one codebase portable** (Early): `__EMSCRIPTEN__` lets the same C/C++ source serve desktop and Wasm targets, including headers only when Emscripten is compiling. This is the practical answer to "how do I reuse existing code without forking it." - **Function pointers turn a one-way call into a conversation** (Early): passing `OnSuccess`/`OnError` pointers into a module lets it report back directly instead of stuffing error strings into a caller-supplied buffer. The JS side wraps this in a Promise, which is the idiomatic bridge. - **Dynamic linking trades size for modularity** (Middle): a shared core module plus thin page-specific modules avoids duplicating validation logic. The cost is build complexity — `MAIN_MODULE=2` and dead-code elimination interact, and you must explicitly export the C library functions your side modules need. - **Threading in Wasm is opt-in and environment-sensitive** (Late): Web Workers handle prefetching and background work; pthreads handle in-module parallelism. Neither is free — the book treats them as advanced topics precisely because the setup is non-trivial. - **Text format is your debugging window** (Ending): building a game in s-expressions makes the module's structure legible in the browser. When binary debugging stalls, the text format is what you read. 【Reading Tips】 - Read chapters 1–3 in order and do not skip Appendix A. The toolchain setup is the single biggest source of early friction, and the book explicitly says the appendix exists to get you to a working build. - Parts 1–2 (through chapter 6) are the core; treat them as a linear course. Parts 3–4 (chapters 7–13) are modular — jump to dynamic linking, threading, or debugging based on what you're actually building. - When the book shows the "no Emscripten plumbing" path, slow down. It's the least convenient chapter and the most educational one; the raw WebAssembly API is what you'll need when the generated code misbehaves. - Keep the card-matching game chapters for last even if debugging is your immediate need — they assume the text-format grounding from earlier chapters. - The excerpts do not cover the book's treatment of Rust or non-C/C++ toolchains in depth; if that's your stack, verify coverage before buying. 【Coverage Limits】 This guide is synthesized from stratified excerpts covering the book's structure, early chapters, and part introductions; specific code listings, later chapter details, and the full debugging/testing content are only partially represented. Claims about chapter contents beyond what the excerpts describe are marked as such.
Page 18
机器语言(1和0,或者本地代码)时,你通常会编写更接近于人 类语言的某种东西。尽管使用从计算机细节中抽象出来的代码更容 易,但计算机处理器并不理解你的代码,因此运行时需要将你编写的 内容转换为机器码。 JavaScript是一种解释型编程语言,也就是说,它会在执行时读入你 编写的代码,并将这些指令即时翻译为机器码...
View in text
Excerpt 2
产环境中的代码会采用4.1节介绍的流程,其中 Emscripten在生成的模块中包含你的代码使用的C标准库函数。在那种 流程中,Emscripten还会生成JavaScript plumbing文件来处理模块的 加载和实例化,并包含ccall这样的辅助函数来简化与模块的交互过 程。 如图4-9所示,本节的流程与4...
View in text
Excerpt 3
ble段的大小,因为那就是要插入 JavaScript函数的位置的索引值。可以使用WebAssembly.Table对 象的grow方法将Table段的大小增加指定的元素个数。这里只需要增 加一个函数,因此告诉Table增长1。 第三部分 高级主题 至此你已经了解了创建并使用WebAssembly模块的基础内容,...
View in text
Excerpt 4
oduct', ←--- - 调用模块中的函数ValidateProduct 'number', ['string', 'number', 'number'], [productId, arrayPointer, arrayLength]); orderModule._free(arrayPointer); ←-...
View in text
Excerpt 5
在先跳过type节点,写好模块函数后再回来添加这些节 点。 向module节点添加的第一个段是import节点。 11.1.5 import节点 Import已知段(参见图11-8)声明要导入模块的所有条目,其中可以 包括Function、Table、Memory或Global导入。对于正在创建的这个模 块来说,...
View in text
Excerpt 6
ateTriesTotal 属性。 图12-20 Firefox中的Scopes一节展示了这个函数作用域中的全局变 量 信息 在Chrome开发者工具中,Scopes面板不会显示全局变量的 值。如果展开Scopes面板的local条目,那么会有一个stack条 目,其中显示了当前栈上的值。而Firefox不会展示...
View in text
Excerpt 7
en的支撑数组中添加和移除函数指针? 答案 addFunction和removeFunction。 D.4.2 练习2 WebAssembly使用哪个指令来调用定义在Table段中的函数? 答案 call_indirect。 D.5 第7章 第7章有两个练习。 D.5.1 练习1 使用本章中学到的动态链接方法之一...
View in text
Excerpt 8
t这一行代码之前的右括号。在(br $while)语句之后为if语 句放一个右括号。 作者简介 C.杰勒德•加伦特(C. Gerard Gallant),微软认证专家,Dovico公 司高级软件工程师,DZone技术网站专栏作者。 看完了 如果您对本书内容有疑问,可发邮件至contact@turingbook.c...
View in text
Tags
AI categories
Web TechnologyProgramming LanguageSoftware
ISBN: 7115561451
Publish Year: 2021
Language: Chinese
Pages: 500
File Format: PDF
File Size: 9.8 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…