An indirect call can go wrong three ways, and a runtime must turn each into a clean trap, not a crash. Today you give traps a carried message and surface them from an indirect call gone bad.
Make call_indirect trap with a clear message on an out-of-range index, an empty slot, or a type mismatch.
A trap is WebAssembly’s controlled failure: execution stops and unwinds, but the host stays healthy. You have raised traps already - unreachable, divide-by-zero, an out-of-range conversion - and now is the moment to make them a proper carried value with a message, because call_indirect is where a trap is most likely and most informative. Three things can go wrong, and each must be a distinct trap: the index can be out of range (past the table’s end), the slot can be empty (no function was ever placed there), or the function found can have the wrong type (the run-time check from the last lesson fails).
The rule that matters is that all three become a clean Trap returned from invoke, never a host-language panic that takes the process down. This is the difference between a runtime and a toy: a real module handed bad data must fail predictably, with a message a caller can log (“undefined element: index out of bounds”), and leave the engine ready to run again. Make the trap a value your interpreter loop returns, and let Invoke hand it back to the host. Exercise each of the three cases against a table with a filled slot and an empty one to see the traps fire instead of a crash - that graceful failure is the property the finalize pass depends on for every unimplemented opcode too.
// A Trap is a value carrying why execution stopped. invoke returns it as an// error; it never panics the host.type Trap struct{ Msg string }func (t *Trap) Error() string { return "trap: " + t.Msg }// in call_indirect:if slot < 0 || slot >= len(table.Funcs) { return &Trap{"undefined element: index out of bounds"} }if table.Funcs[slot] < 0 { return &Trap{"uninitialized element"} }if actualType != expectedType { return &Trap{"indirect call type mismatch"} }