Share E-Book

WebAssembly原理与核心技术 (张秀宏)(Z-Library)

Author 张秀宏

Technology
Language Chinese

No Description

Format EPUB
Size 5.5 MB
187
Views
0
Downloads
0.00
Total Donations

AI Guide

AI Reading Assistant

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

Full assistant
AI guide
【One-Line Pitch】 A hands-on, "build-your-own-Wasm" guide that walks through WebAssembly's binary format, instruction set, and interpreter internals using Go, ideal for developers who want to truly understand Wasm rather than just use it. 【Book Arc】 - **Opening (~0%–4%)**: Sets up the book's "wheel-building" philosophy, introduces the four languages used (Go, WAT, Rust, JavaScript), and explains how to set up the toolchain (Go, Rust, WABT) and download the companion code from GitHub. - **Early (~4%–21%)**: Dives into the binary format (magic number, version, sections) and the instruction set, covering index spaces, LEB128 encoding, and core instructions like variable access and function calls, with WAT examples and hex dumps for hands-on analysis. - **Early–Middle (~21%–36%)**: Explains the text format (WAT) as a human-readable counterpart to binary, then shifts to building a virtual machine, starting with the operand stack and implementing basic instructions (constants, arithmetic, drop). - **Middle (~36%–50%)**: Adds memory management (pages, load/store instructions) and introduces function calls, including the call stack and control frames, with a recursive Fibonacci example to test the implementation. - **Middle–Late (~50%–54%)**: Covers structured control instructions (block, loop, if) and branch instructions (br, br_if), explaining jump labels and how they interact with the control stack, reusing function-call machinery for efficiency. 【Key Takeaways】 - **Index spaces organize all module elements** (Early): Functions, globals, tables, and memories each have their own index space, with imports and definitions sharing the same range; locals and labels are function-scoped. This mental model is crucial for decoding and executing any Wasm module. - **LEB128 is the backbone of compact binary encoding** (Early): This variable-length, little-endian scheme encodes small integers in fewer bytes, saving space for indices and lengths; understanding its signed/unsigned variants is essential for writing a decoder. - **The binary format is section-based and order-dependent** (Early): From magic number `\0asm` and version 1, through type, function, table, memory, and data sections, each section has a specific ID and structure; tools like `wasm-objdump` help verify your parsing. - **WAT is a syntactic sugar over binary** (Early): The text format uses parentheses and keywords (e.g., `module`, `func`, `param`) to express the same structures as binary, with identifiers (`$name`) and folding forms making it more human-friendly; mastering WAT aids in writing test cases. - **The operand stack is the core of the VM** (Early): A simple stack machine can execute most instructions—constants push, arithmetic pops two and pushes one, and `drop` discards the top; this foundation scales as more components are added. - **Memory is page-based with explicit bounds checking** (Middle): Wasm memory is a byte array divided into 64KB pages, with `grow` to expand and load/store instructions for reading/writing; boundary checks are critical to prevent crashes, and bulk-memory operations are a proposed optimization. - **Function calls rely on control frames, not just a call stack** (Middle): A control frame stores opcode, type, instructions, base pointer, and program counter, unifying function calls with structured control instructions; this design simplifies later implementation of blocks and loops. - **Branches are structured, not arbitrary** (Middle–Late): `br` and `br_if` only jump to labels defined by `block`, `if`, or `loop`, with `block`/`if` targeting the end and `loop` the start; this restriction ensures stack consistency and makes control flow predictable. 【Reading Tips】 - **Skim the setup chapter** (Opening): If you're comfortable with Go and command-line tools, skip ahead; just clone the repo and verify your environment with the provided commands. - **Deep-read the binary format chapters** (Early): Use `xxd` and `wasm-objdump` alongside the text to trace each section; this hands-on approach makes abstract encodings like LEB128 concrete. - **Treat WAT examples as executable specs** (Early): Compile and disassemble the provided `.wat` files to see how text maps to bytes; this reinforces both formats simultaneously. - **Focus on the VM architecture** (Middle): Pay attention to how the operand stack, memory, and control frames are structured in Go; these patterns are reusable for any interpreter project. - **Don't get stuck on every instruction** (Middle): The book groups similar instructions (e.g., all store variants); understand one pattern (like `i64.store`) and the rest follow—skim the tables and move on. 【Coverage Limits】 Excerpts cover roughly the first half of the book (through control instructions); later topics like indirect calls, linking, validation, and advanced proposals (e.g., bulk memory) are mentioned but not detailed here.

Passage locations

Excerpt 1
和跳转标签(详见第8章)在函数内有各自的索引空间。为了提高代码的可读性,我们给这些索引分别定义了类型别名,代码如下所示。 type ( TypeIdx = uint32 FuncIdx = uint32 TableIdx = uint32 MemIdx = uint32 GlobalIdx = uint32 Lo...
View in text
Excerpt 2
flag.Args()[0]) if err != nil { fmt.Println(err.Error()) os.Exit(1) } if *dumpFlag { dump(module) // 在dumper.go文件里 } } 执行命令查看结果。 $ cd code/go/ch02/wasm.go/ $...
View in text
Excerpt 3
} func i64Mul (...) { v2,v1 := vm.popU64(),vm.popU64(); vm.pushU64(v1 * v2) } func i64DivS(...) { v2,v1 := vm.popS64(),vm.popS64(); vm.pushS64(v1 / v2) } fun...
View in text
Excerpt 4
需要把5条变量指令添加到指令表中。这个改动比较简单,就不展示代码了。然后,需要在创建虚拟机实例后,分配并初始化全局变量。我们把全局变量初始化逻辑封装在initGlobals()方法里,代码如下所示。 func (vm *vm) initGlobals() { for _, global := range vm.m...
View in text

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
Back to List