1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! Per-island panic isolation on a `panic = "abort"` target.
//!
//! `wasm32-unknown-unknown` has no unwinding runtime: a panic runs the panic
//! hook and then executes `unreachable`, which traps. A trap cannot be observed
//! from Rust — `catch_unwind` never returns `Err` — but it *can* be observed from
//! JavaScript, where it surfaces as a `RuntimeError` thrown out of whichever
//! wasm call was on the stack. So each island is hydrated through a small JS
//! trampoline, `krab_call_isolated`, which calls back into wasm inside a
//! `try`/`catch`:
//!
//! ```text
//! hydrate_within (wasm) ── krab_call_isolated (JS, try) ── island body (wasm)
//! ▲ │ catch │ panic → trap
//! └──────── Err(message) ────┘ ◀──────────────────────────┘
//! ```
//!
//! The trap discards only the wasm frames *above* the trampoline. The loop over
//! islands sits below it, so it sees an `Err`, stamps that one boundary
//! `error`, and carries on with the next island. Before this, the trap escaped
//! the loop itself and every later island stayed at `ssr`, silently.
//!
//! The trampoline is shipped as a wasm-bindgen `inline_js` snippet rather than
//! built with `new Function`, so it works under a Content-Security-Policy that
//! forbids `unsafe-eval`, and no consumer has to write it.
//!
//! # Where the message comes from
//!
//! A trap carries no payload — the JS side only sees `RuntimeError: unreachable`.
//! The panic hook, however, runs *before* the trap, so this module installs one
//! (chained in front of whatever hook was already set) that records the panic
//! message while an isolated call is in flight. If the application replaces the
//! hook afterwards, the record is simply missing and the JS error text is used.
//!
//! # Recovery is best-effort
//!
//! The wasm instance stays callable after a trap — nothing is re-instantiated —
//! but it is not necessarily *consistent*. Everything the trapped frames were
//! doing is abandoned mid-flight:
//!
//! - their heap allocations leak, and their shadow-stack space is not returned;
//! - a `RefCell` borrowed at the moment of the trap stays borrowed, so a later
//! use of the same cell panics (and is itself contained, one island at a
//! time);
//! - no drop guard runs, so `krab_core`'s reactive position — current owner,
//! current subscriber, `batch` and flush depths — is left as the dead island
//! had it. The hydration loop therefore snapshots it before each isolated
//! call (`krab_core::signal::snapshot_reactive_state`) and restores it after
//! a trap; without that the next island's scope was parented to the dead
//! one, and a stuck batch depth deferred every later signal write.
//!
//! This is acceptable because it only ever happens on a failure path that was
//! strictly worse before — one bad island used to take every later island with
//! it. A bundle built with `panic = "unwind"` (nightly `-Z build-std` with the
//! exception-handling proposal) would not need any of this; wasm-bindgen's
//! `--force-enable-abort-handler` flag, which poisons the instance on the first
//! trap, defeats it and must not be used with this runtime.
use ;
use Once;
use *;
use JsCast;
use Element;
// `undefined` means the body returned normally. Anything else is what was
// thrown — a `RuntimeError` for a Rust panic, or a JS exception that escaped a
// `web-sys` call not declared `catch`. A literal `throw undefined` is mapped to
// `null` so it is still reported as a failure.
extern "C"
thread_local!
static HOOK: Once = new;
/// Chain a recording hook in front of the current one, once per instance.
///
/// Installed lazily on the first isolated call rather than at start-up, so an
/// application that sets its own hook (`console_error_panic_hook`, typically)
/// before hydrating keeps it: the previous hook is taken and still called.
/// Record `info` for the enclosing isolated call, if there is one.
///
/// Every access is fallible on purpose. A panic *inside* a panic hook aborts
/// without running any hook at all, so this must not be able to panic even if
/// a trap has left the record cell borrowed.
/// Take the recorded message, tolerating a cell a trap left borrowed.
/// Run `body(element)` so that a panic or JS exception inside it is contained.
///
/// Returns `Err` with a description when the call trapped or threw. The frames
/// inside `body` are gone by then; the caller is responsible for putting the
/// boundary into a terminal state.
pub
/// A readable description of whatever JS threw.