hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

abort+test.ha (1394B)


      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 // Sets a new global runtime abort handler.
     13 export fn onabort(handler: *abort_handler) void = {
     14 	return; // no-op on +test (XXX: Do something here?)
     15 };
     16 
     17 export type abort_reason = struct {
     18 	path: nullable *str,
     19 	line: u64,
     20 	col: u64,
     21 	msg: str,
     22 };
     23 
     24 export let jmp: nullable *jmpbuf = null;
     25 export let reason: abort_reason = abort_reason { ... };
     26 
     27 export @symbol("rt.abort") fn _abort(
     28 	path: *str,
     29 	line: u64,
     30 	col: u64,
     31 	msg: str,
     32 ) void = {
     33 	match (jmp) {
     34 	case let j: *jmpbuf =>
     35 		reason = abort_reason {
     36 			path = path,
     37 			line = line,
     38 			col = col,
     39 			msg = msg,
     40 		};
     41 		longjmp(j, 1); // test::status::ABORT
     42 	case null =>
     43 		platform_abort(path, line, col, msg);
     44 	};
     45 };
     46 
     47 // See harec:include/gen.h
     48 const reasons: [_]str = [
     49 	"slice or array access out of bounds",			// 0
     50 	"type assertion failed",				// 1
     51 	"out of memory",					// 2
     52 	"static insert/append exceeds slice capacity",		// 3
     53 	"execution reached unreachable code (compiler bug)",	// 4
     54 	"slice allocation capacity smaller than initializer",	// 5
     55 	"assertion failed",					// 6
     56 	"error occurred",					// 7
     57 ];
     58 
     59 export fn abort_fixed(path: *str, line: u64, col: u64, i: u64) void = {
     60 	_abort(path, line, col, reasons[i]);
     61 };