| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
// Signature for abort handler function. |
| 5 |
export type abort_handler = fn( |
| 6 |
path: *str, |
| 7 |
line: u64, |
| 8 |
col: u64, |
| 9 |
msg: str, |
| 10 |
) never; |
| 11 |
|
| 12 |
let handle_abort: *abort_handler = &platform_abort; |
| 13 |
|
| 14 |
// Sets a new global runtime abort handler, returning the previous handler. |
| 15 |
export fn onabort(handler: *abort_handler) *abort_handler = { |
| 16 |
const prev = handle_abort; |
| 17 |
handle_abort = handler; |
| 18 |
return prev; |
| 19 |
}; |
| 20 |
|
| 21 |
export @symbol("rt.abort") fn _abort( |
| 22 |
path: *str, |
| 23 |
line: u64, |
| 24 |
col: u64, |
| 25 |
msg: str, |
| 26 |
) never = { |
| 27 |
handle_abort(path, line, col, msg); |
| 28 |
}; |
| 29 |
|
| 30 |
// See harec:include/gen.h |
| 31 |
const reasons: [_]str = [ |
| 32 |
"slice or array access out of bounds", // 0 |
| 33 |
"type assertion failed", // 1 |
| 34 |
"execution reached unreachable code (compiler bug)", // 2 |
| 35 |
"slice allocation capacity smaller than initializer", // 3 |
| 36 |
"assertion failed", // 4 |
| 37 |
"error occurred", // 5 |
| 38 |
]; |
| 39 |
|
| 40 |
export fn abort_fixed(path: *str, line: u64, col: u64, i: u64) void = { |
| 41 |
handle_abort(path, line, col, reasons[i]); |
| 42 |
}; |