|
1 | | -@runtimeMode |
| 1 | +@noPervasives |
2 | 2 | module Exception |
3 | 3 |
|
4 | | -from "runtime/unsafe/wasmi32" include WasmI32 |
5 | | -use WasmI32.{ (==), (+), (-) } |
6 | | - |
7 | | -foreign wasm fd_write: |
8 | | - (WasmI32, WasmI32, WasmI32, WasmI32) => WasmI32 from "wasi_snapshot_preview1" |
9 | | - |
10 | | -primitive unreachable = "@unreachable" |
11 | | - |
12 | | -provide let mut printers = 0n |
13 | | - |
14 | | -// These functions are dangerous because they leak runtime memory and perform |
15 | | -// no GC operations. As such, they should only be called by this module and/or |
16 | | -// modules that understand these restrictions, namely Pervasives. |
17 | | - |
18 | | -provide let dangerouslyRegisterBasePrinter = f => { |
19 | | - let mut current = printers |
20 | | - while (true) { |
21 | | - // There will be at least one printer registered by the time this is called |
22 | | - let (_, next) = WasmI32.toGrain(current): |
23 | | - (Exception => Option<String>, WasmI32) |
24 | | - if (next == 0n) { |
25 | | - // Using a tuple in runtime mode is typically disallowed as there is no way |
26 | | - // to reclaim the memory, but this function is only called once |
27 | | - let newBase = (WasmI32.fromGrain(f), 0n) |
28 | | - WasmI32.store(current, WasmI32.fromGrain(newBase), 12n) |
29 | | - break |
30 | | - } |
31 | | - current = next |
32 | | - } |
33 | | - // We don't decRef the closure or arguments here to avoid a cyclic dep. on Memory. |
34 | | - // This is fine, as this function should only be called once. |
35 | | - void |
36 | | -} |
37 | | - |
38 | | -provide let dangerouslyRegisterPrinter = f => { |
39 | | - printers = WasmI32.fromGrain((f, printers)) |
40 | | - // We don't decRef the closure or arguments here to avoid a cyclic dep. on Memory. |
41 | | - // This is fine, as this function is only called seldomly. |
42 | | - void |
43 | | -} |
44 | | - |
45 | | -// avoid cirular dependency on gc |
46 | | -let incRef = v => { |
47 | | - let ptr = WasmI32.fromGrain(v) - 8n |
48 | | - WasmI32.store(ptr, WasmI32.load(ptr, 0n) + 1n, 0n) |
49 | | - v |
50 | | -} |
| 4 | +from "runtime/unsafe/panic" include Panic |
51 | 5 |
|
52 | 6 | let _GENERIC_EXCEPTION_NAME = "GrainException" |
53 | | - |
54 | | -let exceptionToString = (e: Exception) => { |
55 | | - let mut result = _GENERIC_EXCEPTION_NAME |
56 | | - let mut current = printers |
57 | | - while (true) { |
58 | | - if (current == 0n) return result |
59 | | - let (printer, next) = WasmI32.toGrain(current): |
60 | | - (Exception => Option<String>, WasmI32) |
61 | | - // as GC is not available, manually increment the references |
62 | | - match (incRef(printer)(incRef(e))) { |
63 | | - Some(str) => return str, |
64 | | - None => { |
65 | | - current = next |
| 7 | +let mut basePrinter = None |
| 8 | +let mut printers = [] |
| 9 | + |
| 10 | +/** |
| 11 | + * Registers a base exception printer. If no other exception printers are |
| 12 | + * registered, the base printer is used to convert an exception to a string. |
| 13 | + * |
| 14 | + * @param printer: The base exception printer to register |
| 15 | + * |
| 16 | + * @since v0.7.0 |
| 17 | + */ |
| 18 | +provide let registerBasePrinter = (printer: Exception => String) => |
| 19 | + basePrinter = Some(printer) |
| 20 | + |
| 21 | +/** |
| 22 | + * Registers an exception printer. When an exception is thrown, all registered |
| 23 | + * printers are called in order from the most recently registered printer to |
| 24 | + * the least recently registered printer. The first `Some` value returned is |
| 25 | + * used as the exception's string value. |
| 26 | + * |
| 27 | + * @param printer: The exception printer to register |
| 28 | + * |
| 29 | + * @since v0.7.0 |
| 30 | + */ |
| 31 | +provide let registerPrinter = (printer: Exception => Option<String>) => |
| 32 | + printers = [printer, ...printers] |
| 33 | + |
| 34 | +/** |
| 35 | + * Gets the string representation of the given exception. |
| 36 | + * |
| 37 | + * @param e: The exception to stringify |
| 38 | + * |
| 39 | + * @returns The string representation of the exception |
| 40 | + * |
| 41 | + * @since v0.7.0 |
| 42 | + */ |
| 43 | +provide let toString = (e: Exception) => { |
| 44 | + let rec exceptionToString = (e, printers) => { |
| 45 | + match (printers) { |
| 46 | + [] => match (basePrinter) { |
| 47 | + Some(f) => f(e), |
| 48 | + None => _GENERIC_EXCEPTION_NAME, |
| 49 | + }, |
| 50 | + [printer, ...rest] => { |
| 51 | + match (printer(e)) { |
| 52 | + Some(s) => s, |
| 53 | + None => exceptionToString(e, rest), |
| 54 | + } |
66 | 55 | }, |
67 | 56 | } |
68 | 57 | } |
69 | | - return result |
70 | | -} |
71 | | - |
72 | | -// HACK: Allocate static buffer for printing (40 bytes) |
73 | | -// Would be nice to have a better way to allocate a static block from |
74 | | -// the runtime heap, but this is the only module that needs to do it |
75 | | -let iov = WasmI32.fromGrain([> 0n, 0n, 0n, 0n, 0n, 0n, 0n, 0n, 0n, 0n]) |
76 | | - |
77 | | -provide let panic = (msg: String) => { |
78 | | - let ptr = WasmI32.fromGrain(msg) |
79 | | - let written = iov + 32n |
80 | | - let lf = iov + 36n |
81 | | - WasmI32.store(iov, ptr + 8n, 0n) |
82 | | - WasmI32.store(iov, WasmI32.load(ptr, 4n), 4n) |
83 | | - WasmI32.store8(lf, 10n, 0n) |
84 | | - WasmI32.store(iov, lf, 8n) |
85 | | - WasmI32.store(iov, 1n, 12n) |
86 | | - fd_write(2n, iov, 2n, written) |
87 | | - unreachable() |
| 58 | + exceptionToString(e, printers) |
88 | 59 | } |
89 | 60 |
|
| 61 | +/** |
| 62 | + * Throws an uncatchable exception and traps. |
| 63 | + * |
| 64 | + * @param e: The exception to throw |
| 65 | + */ |
90 | 66 | provide let panicWithException = (e: Exception) => { |
91 | | - panic(exceptionToString(e)) |
| 67 | + Panic.panic(toString(e)) |
92 | 68 | } |
93 | 69 |
|
| 70 | +// Runtime exceptions |
| 71 | + |
94 | 72 | provide exception DivisionByZero |
95 | 73 | provide exception ModuloByZero |
96 | 74 | provide exception Overflow |
@@ -126,4 +104,4 @@ let runtimeErrorPrinter = e => { |
126 | 104 | } |
127 | 105 | } |
128 | 106 |
|
129 | | -dangerouslyRegisterPrinter(runtimeErrorPrinter) |
| 107 | +registerPrinter(runtimeErrorPrinter) |
0 commit comments