Fieldbooks / WebAssembly / Lesson 02
Lesson 02 of 08 · Course index 00 Start Here: What & WhyStack & control 01 Jump or trap?02 Stack types→ 03 Branch targets04 Unreachable codeMemory & calls 05 Page bounds06 Indirect callsVerification 07 Two verifier designs08 Transfer exam Lesson 02 / Stack & control
What does the add take? Build the input check for an add. Picture a stack as a list you fill and empty at the right. The label on a value is its type. In i32 and i64, i means whole number and the number gives its width in bits. In f32 and f64, f means a floating-point number, which can represent fractions. Each add takes two values of its own type.
+ Figure 02
Top of stack is on the right
i
1 · The big idea Unlike assembly languages that name CPU registers, WebAssembly is a stack machine : values sit on an ordered tray (top at the right). Every value carries an explicit type tag: i32 or i64 for whole integers (32 or 64 bits), and f32 or f64 for floating-point decimals.
Every instruction declares exact input and output types. For example, f32.add pops two f32 values and pushes one f32; drop discards the top item; i32.add pops two i32 values and pushes one i32.
If a reachable instruction asks for an i32 when the stack holds only one item (stack underflow ) or holds an f64 (type mismatch ), the verifier rejects the entire program before a single line runs.
→
2 · Worked step-by-step example Step-by-step worked trace for lesson 2 Stack Machine Tray: [f32, f32] -> f32.add -> drop -> [i32] -> i32.add [1.5, 2.25] f32 f32.add + drop -> [] i32.const 7 -> [7] Only 1 item on tray i32.add needs 2! Underflow -> REJECT Push i32.const 3 after 7 so both pops get i32 (result = 10). Worked Example: Watching the Stack Through f32.add, drop, i32.add
f32.const 1.5, f32.const 2.25 → stack is [f32, f32].f32.add pops two f32s and pushes their sum → stack is [f32].drop removes the top value → stack is empty [].i32.const 7 pushes one i32 → [i32]. Next, i32.add tries to pop two i32s: 1st pop gets i32, 2nd pop hits empty stack → rejected! Result: Pushing i32.const 3 gives [i32, i32] so both pops succeed and runtime returns 10.
Read the case [1.5 f32, 2.25 f32] → f32.add → [3.75 f32]
drop removes 3.75 → []
push 7 → [7 i32] → i32.add needs two
≡
3 · Bytecode stepper (explore before quizzing) Arity is the number of inputs. An add has arity two. Too few inputs cause stack underflow. Two inputs with the wrong labels cause a type mismatch. Both are errors the verifier can catch before running the code.
Bytecode is the list of machine instructions below. Step through Check types and Run values before answering the prediction below.
Check types Instruction step Step 0Ready 0 / 6
Start with an empty stack. Read the instructions in order.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.addOperand stack top at right [ ] Empty
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
Step forward →
Step 1f32.const 1 / 6
Push the type f32.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.addOperand stack top at right f32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 2f32.const 2 / 6
Push the type f32.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.addOperand stack top at right f32 f32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 3f32.add 3 / 6
Check two f32 inputs. Push one f32 result.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.addOperand stack top at right f32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 4drop 4 / 6
Remove one value of any type.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.addOperand stack top at right [ ] Empty
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 5i32.const 5 / 6
Push the type i32.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.addOperand stack top at right i32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 6i32.add 6 / 6
Stack underflow: i32 is missing at height 0.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.addOperand stack before rejection · top at right i32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Types rejected
Run values Execution is blocked. Stack underflow: i32 is missing at height 0.
?
4 · Make a prediction The first sum was removed. What happens when i32.add looks for its other input? ↗
5 · Repair the program Make the final i32.add return the whole number 10. Choose the value to push after 7. Push i64.const 3 Inspect this edit. The top value is a 64-bit integer. i32.add asks for a 32-bit integer on each pop.
Types rejected Run blocked Calls 0 Writes 0
Step through this edit Check types Instruction step Step 0Ready 0 / 7
Start with an empty stack. Read the instructions in order.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i64.const 306 i32.addOperand stack top at right [ ] Empty
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
Step forward →
Step 1f32.const 1 / 7
Push the type f32.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i64.const 306 i32.addOperand stack top at right f32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 2f32.const 2 / 7
Push the type f32.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i64.const 306 i32.addOperand stack top at right f32 f32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 3f32.add 3 / 7
Check two f32 inputs. Push one f32 result.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i64.const 306 i32.addOperand stack top at right f32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 4drop 4 / 7
Remove one value of any type.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i64.const 306 i32.addOperand stack top at right [ ] Empty
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 5i32.const 5 / 7
Push the type i32.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i64.const 306 i32.addOperand stack top at right i32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 6i64.const 6 / 7
Push the type i64.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i64.const 306 i32.addOperand stack top at right i32 i64
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 7i32.add 7 / 7
Type mismatch: expected i32, found i64.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i64.const 306 i32.addOperand stack before rejection · top at right i32 i64
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Types rejected
Run values Execution is blocked. Type mismatch: expected i32, found i64.
Push f64.const 3 Inspect this edit. Three can be represented as a floating-point value. Its f64 label still fails the i32 input check.
Types rejected Run blocked Calls 0 Writes 0
Step through this edit Check types Instruction step Step 0Ready 0 / 7
Start with an empty stack. Read the instructions in order.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 f64.const 306 i32.addOperand stack top at right [ ] Empty
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
Step forward →
Step 1f32.const 1 / 7
Push the type f32.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 f64.const 306 i32.addOperand stack top at right f32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 2f32.const 2 / 7
Push the type f32.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 f64.const 306 i32.addOperand stack top at right f32 f32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 3f32.add 3 / 7
Check two f32 inputs. Push one f32 result.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 f64.const 306 i32.addOperand stack top at right f32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 4drop 4 / 7
Remove one value of any type.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 f64.const 306 i32.addOperand stack top at right [ ] Empty
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 5i32.const 5 / 7
Push the type i32.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 f64.const 306 i32.addOperand stack top at right i32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 6f64.const 6 / 7
Push the type f64.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 f64.const 306 i32.addOperand stack top at right i32 f64
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 7i32.add 7 / 7
Type mismatch: expected i32, found f64.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 f64.const 306 i32.addOperand stack before rejection · top at right i32 f64
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Types rejected
Run values Execution is blocked. Type mismatch: expected i32, found f64.
Push i32.const 3 Repair confirmed. Both pops now find i32. Inspect the last runner step for the i32 result 10.
Types accepted Run finished Calls 0 Writes 0
Step through this edit Check types Instruction step Step 0Ready 0 / 7
Start with an empty stack. Read the instructions in order.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right [ ] Empty
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
Step forward →
Step 1f32.const 1 / 7
Push the type f32.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right f32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 2f32.const 2 / 7
Push the type f32.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right f32 f32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 3f32.add 3 / 7
Check two f32 inputs. Push one f32 result.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right f32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 4drop 4 / 7
Remove one value of any type.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right [ ] Empty
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 5i32.const 5 / 7
Push the type i32.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right i32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 6i32.const 6 / 7
Push the type i32.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right i32 i32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Step forward →
Step 7i32.add 7 / 7
Check two i32 inputs. Push one i32 result.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right i32
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 – 65528
– 65529
– 65530
– 65531
– 65532
– 65533
– 65534
– 65535
× 65536+
Type checking does not read or write bytes.
← Back Types accepted
Run values Instruction step Step 0Ready 0 / 7
Start with an empty stack and one page of zero bytes.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right [ ] Empty
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 00 65528
00 65529
00 65530
00 65531
00 65532
00 65533
00 65534
00 65535
× 65536+
Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.
Step forward →
Step 1f32.const 1 / 7
Push the value 1.5.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right f32 1.5
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 00 65528
00 65529
00 65530
00 65531
00 65532
00 65533
00 65534
00 65535
× 65536+
Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.
← Back Step forward →
Step 2f32.const 2 / 7
Push the value 2.25.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right f32 1.5 f32 2.25
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 00 65528
00 65529
00 65530
00 65531
00 65532
00 65533
00 65534
00 65535
× 65536+
Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.
← Back Step forward →
Step 3f32.add 3 / 7
Add 1.5 and 2.25.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right f32 3.75
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 00 65528
00 65529
00 65530
00 65531
00 65532
00 65533
00 65534
00 65535
× 65536+
Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.
← Back Step forward →
Step 4drop 4 / 7
Discard the top value.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right [ ] Empty
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 00 65528
00 65529
00 65530
00 65531
00 65532
00 65533
00 65534
00 65535
× 65536+
Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.
← Back Step forward →
Step 5i32.const 5 / 7
Push the value 7.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right i32 7
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 00 65528
00 65529
00 65530
00 65531
00 65532
00 65533
00 65534
00 65535
× 65536+
Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.
← Back Step forward →
Step 6i32.const 6 / 7
Push the value 3.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right i32 7 i32 3
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 00 65528
00 65529
00 65530
00 65531
00 65532
00 65533
00 65534
00 65535
× 65536+
Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.
← Back Step forward →
Step 7i32.add 7 / 7
Add 7 and 3.
Bytecode instruction address 00 f32.const 1.501 f32.const 2.2502 f32.add03 drop04 i32.const 705 i32.const 306 i32.addOperand stack top at right i32 10
Control label stack innermost at right function h=0
Each label keeps its entry height (h = stack height when the block opened).
Linear memory Tail of page 0 00 65528
00 65529
00 65530
00 65531
00 65532
00 65533
00 65534
00 65535
× 65536+
Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.
← Back Run finished
{ }
Code workshop Implement take in my-rules.mjs. Remove a value of the expected type. Report an empty stack or a mismatched type. The first command makes your own copy of the starter if needed. Edit the named function, then run the second command.
cp -n workshop/starter.mjs my-rules.mjs
node workshop/check.mjs 2 ./my-rules.mjsOptional worked example in a separate file Add a type check to lab.mjs. Call it twice for an add. Which call fails with only one i32?
function take(stack, want) {
if (!stack.length) throw Error("underflow")
if (stack.pop() !== want) throw Error("type mismatch")
}
const inputs = ["i32"]
take(inputs, "i32")
take(inputs, "i32")The second call throws. Start with two i32 labels and both calls pass. Change the top label to i64 to exercise the other error. Remove the throwing calls before the next trial.
Key Takeaway & Next Up Stack typing catches missing operands (underflow) and wrong bit-widths/types (like f64 vs. i32) statically before execution.
Next: Next, we trace how jumps (br) count nested blocks and loops outward.
Edit the bytecode Change the numbered inputs or instructions and run the checker again. Each run starts with a fresh page. This editor accepts a JSON object containing the decoded instructions.
Program Check and run
Free to Learn: Build a WebAssembly Stack-Machine Verifier & Linear-Memory Sandbox Lab