Share E-Book
Scan to open this page

Scan with your phone to open this page

AuthorUnknown

No description

AI Reading Assistant

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

AI guide
# WebAssembly标准入门 ## 【One-Line Pitch】 A comprehensive, hands-on introduction to WebAssembly—the new standard for high-performance, portable, secure code in the browser—covering everything from JavaScript integration to the underlying assembly language and binary format. Ideal for web developers seeking near-native performance, as well as C/C++ and Go programmers who want to run their code in the browser. ## 【Book Arc】 - **Opening (~0%–9%)**: The book opens with the history of web technologies—from JavaScript's rise via Ajax and the V8 engine, to earlier failed attempts at running native code in the browser (NPAPI, ActiveX, NaCL) and the eventual emergence of WebAssembly from asm.js. It then reviews essential JavaScript concepts (functions, closures, Promises, typed arrays) that serve as prerequisites for working with WebAssembly. - **Early (~9%–26%)**: Introduces the JavaScript API for WebAssembly: compiling and instantiating modules, the WebAssembly.Module object (including caching and custom sections), and the WebAssembly.Memory object for sharing data between JavaScript and WebAssembly. Covers practical patterns like fetchAndInstantiate() and memory growth. - **Early (~26%–30%)**: Explains the WebAssembly.Table object—a critical mechanism for implementing function pointers safely. Discusses why tables exist (security), how to create and populate them from JavaScript, and how WebAssembly code calls functions indirectly through table indices. - **Middle (~30%–48%)**: Dives into the WebAssembly assembly language (text format, .wat). Covers S-expressions, function signatures, local variables, globals, the stack-based virtual machine model, control flow (if/block/loop/br_table/return), and import/export declarations. Includes the start section for auto-executed functions. - **Middle (~48%–52%)**: Explains the binary format (.wasm), focusing on LEB128 variable-length integer encoding—the foundation for compact binary representation. Walks through a concrete example of encoding a number and dissects a minimal binary module section by section (Type, Function, Code segments). ## 【Key Takeaways】 - **WebAssembly solves JavaScript's performance ceiling** (Early): By providing a compact binary format that browsers can parse and optimize faster than JavaScript, WebAssembly achieves near-native execution speeds while remaining portable and secure. This matters for compute-intensive web applications like games, image processing, and scientific computing. - **The JavaScript API is the gateway** (Early): WebAssembly.compile() and WebAssembly.instantiate() are the primary asynchronous methods for loading modules. The streaming variants (compileStreaming/instantiateStreaming) are simpler but have limited browser support (Safari lacks them), so the book recommends the non-streaming versions for compatibility. - **Memory is a shared, linear array** (Early): WebAssembly.Memory is essentially an ArrayBuffer that both JavaScript and WebAssembly can access. A single memory object can be shared across multiple instances for inter-module communication. Key constraint: TypedArray views must be recreated after memory growth, and maximum capacity limits growth attempts. - **Tables enable safe function pointers** (Early): Because WebAssembly code cannot see real memory addresses, the Table object stores function references by index, allowing indirect calls (call_indirect) without exposing raw pointers. This design prevents malicious code from jumping to arbitrary addresses—a fundamental security feature. - **The stack machine model is the core abstraction** (Middle): WebAssembly is a stack-based virtual machine: instructions pop operands from and push results onto an operand stack. Understanding this model is essential for reading and writing .wat code, as every instruction's behavior is defined by its stack effects. - **Control flow uses structured blocks** (Middle): Unlike unstructured jumps, WebAssembly uses block, loop, and if constructs with labeled break targets (br, br_if, br_table). This structured approach keeps code verifiable and safe. The return instruction provides an early exit from functions. - **LEB128 encoding keeps binaries small** (Middle): Variable-length integer encoding uses 7 bits per byte with a continuation flag, making small numbers compact (1 byte for values < 128) while still supporting 64-bit integers. This is a key reason WebAssembly modules are smaller than equivalent asm.js code. - **i64 has a JavaScript boundary limitation** (Early): JavaScript's Number type cannot precisely represent 64-bit integers, so exported functions cannot use i64 parameters or return values—attempting to do so throws a TypeError. This is a practical constraint for cross-language interop. ## 【Reading Tips】 - **Skim the JavaScript history (0–4%)** if you're already familiar with web platform evolution; it's context-setting but not essential for hands-on work. Do read the JavaScript refresher on Promises and typed arrays if you need a quick review. - **Deep-read Chapters 3 and 4** (Early–Middle): These are the heart of the book. Work through every code example—especially the fibonacci and table-call examples—by running them in your browser. The .wat syntax becomes intuitive only through practice. - **Pay special attention to the Table and call_indirect material** (Early): This is the most conceptually challenging part for newcomers. The security rationale and the "index instead of address" model are worth understanding thoroughly before moving on. - **The binary format chapter (Middle)** can be skimmed on first reading unless you're building tooling. Focus on understanding LEB128 conceptually and recognizing the section structure; you can return to byte-level details when needed. - **Use the appendix of 200+ instructions** as a reference rather than reading it linearly. When you encounter an unfamiliar instruction in examples, look it up there. ## 【Coverage Limits】 This guide covers the book's core content: JavaScript API usage, the .wat text format, and the binary format fundamentals. The excerpts do not cover the later chapters on C/C++ and Go toolchain integration, which the book's introduction promises but which fall outside the sampled material. ##
Page 20
本地代码的ActiveX控件。同样,谷歌公司在2010年也开发了一 项Native Clients技术(简称NaCL)。然而这些技术都太过复杂、容 错性也不够强大,它们最终都未能成为行业标准。 除尝试直接运行本地代码这条路之外,也有技术人员开始另辟蹊 径,将其他语言直接转译为JavaScript后运行,2006年...
View in text
Excerpt 2
ole.log(result.instance.exports.showMeTheAnswer()) //42 本方法的返回值和异常与WebAssembly.instantiate()的第一种重 载形式相同。 提示 WebAssembly.compileStreaming()/WebAssembly.instan...
View in text
Excerpt 3
c $f0 (param i32)(result i32) i32.const 13 ) (func $f1 (result i32) i32.const 65540 i32.load ) (func (export "call_by_index") (param $index i32)(result   第4章...
View in text
Excerpt 4
t "js" "table" (table 1 anyfunc)) ;;import Table (import "js" "print_i32" (func $js_print_i32 (param i32))) ;;import Fucntion (import "js" "global_pi" (globa...
View in text
Excerpt 5
cal decl count 000001a: 41 ; i32.const 000001b: 2a ; i32 literal 000001c: 0b ; end 0000018: 04 ; FIXUP func body size 0000016: 06 ; FIXUP section size $ 5.7....
View in text
Excerpt 6
就可以被 C/C++代码使用了。 在使用my_run_script()函数前需要先包含它的声明。现在我们创 建main.cc文件演示my_run_script()的用法。通过额外的参数向 my_run_script()函数输入42,在JavaScript脚本中通过$arg来访问额 外的参数: // main.cc...
View in text
Excerpt 7
过查看生成的WebAssembly模块文 件,可以得知有以下的runtime包函数被导入了: func runtime.wasmExit(code int32) func runtime.wasmWrite(fd uintptr, p unsafe.Pointer, n int32) func runtime.n...
View in text
Excerpt 8
值 为1、2、4、8。“offset=...”可省略,默认值为0; “align=...”可省略,默认值为1。 i32.load8_u offset=o align=a:从栈顶弹出1个i32的值addr,从 内存的addr+o偏移处读取1 字节,并按无符号整数扩展为i32(高 位填充0)压入栈。a为地址对齐值,取...
View in text
Tags
AI categories
Programming LanguageWeb TechnologyBackend
ISBN: 7115500592
Language: English
File Format: PDF
File Size: 2.2 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…