Improve debugging in error conditions
When there is an error, we should be able to print a backtrace with function names, source locations, and ideally local variable names, types, and values.
Debugging metadata may take a considerable amount of space. We should support splitting off debugging metadata outside the main wasm module.
We need to be able to support backtraces composed of continuations from multiple module instances.
Ideally we would be able to populate a hash table keying funcref values to debugging metadata. However we can't do this within wasm, because funcrefs aren't comparable by ref.eq, and like other reference-typed values we can't obtain a hash value for them. JavaScript can do it, though; there, (ref.func X) === (ref.func Y) iff X is Y and the funcrefs are from the same instance. I was thinking that we could do a halfway solution, relying on the host (e.g. JS) to maintain a funcref-to-integer map, but because we need to distinguish between instances, this doesn't work. So we might as well maintain a funcref-to-metadata map on the JS side.
Taking into account the need to key the debug info on the function identity, at the minimum we need to provide an export from a module which can return an array of funcrefs, one for each return continuation. Call it get_return_continuations. Then either the module provides get_return_continuation_metadata (returning an equally-sized array, corresponding to all the metadata for the module) or it can be provided by a third-party module.
At that point you are just missing source location info for exceptions, i.e., the point at which an exception is thrown. Given that some exceptions are thrown by the stdlib, I think you want to thread an additional "source location ID" i32 through, embedded in the generated code and passed to the stdlib. Then you need to be able to go from ID to source location info. Not sure best to get this through the toolchain though!