Skip to content

Commit

Permalink
added: panic builtin procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanfh committed May 8, 2024
1 parent ff0b89a commit a64e3ea
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 28 deletions.
2 changes: 1 addition & 1 deletion core/alloc/pool.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pool_allocator_proc :: (pool: &PoolAllocator($Elem), aa: AllocationAction, size:
}

case .Resize {
assert(false, "Cannot resize in a pool allocator!");
panic("Cannot resize in a pool allocator!");
return null;
}

Expand Down
7 changes: 6 additions & 1 deletion core/builtin.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ OnyxContext.get_user_data :: macro (c: &OnyxContext, $T: type_expr) -> &T {


// CLEANUP: Does assert() need to be in the builtin file?
// It uses context.assert_handler, but does it needt to be here?
// It uses context.assert_handler, but does it need to be here?

/// Checks if the condition is true. If not, invoke the context's
/// assert handler.
Expand All @@ -158,6 +158,11 @@ assert :: (cond: bool, msg: str, site := #callsite) {
}
}

/// Causes a runtime panic with the specified error message.
panic :: (msg: str, site := #callsite) {
context.assert_handler(msg, site);
}


//
// Basic logging
Expand Down
8 changes: 4 additions & 4 deletions core/container/optional.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Optional.unwrap :: (o: ?$T) -> T {
switch o {
case .Some as v do return v;
case _ {
assert(false, "Unwrapping empty Optional.");
panic("Unwrapping empty Optional.");
return .{};
}
}
Expand All @@ -111,7 +111,7 @@ Optional.unwrap_ptr :: (o: & ?$T) -> &T {
switch o {
case .Some as &v do return v;
case _ {
assert(false, "Unwrapping empty Optional.");
panic("Unwrapping empty Optional.");
return .{};
}
}
Expand All @@ -124,7 +124,7 @@ Optional.expect :: (o: ?$T, message: str) -> T {
switch o {
case .Some as v do return v;
case _ {
assert(false, message);
panic(message);
return .{};
}
}
Expand All @@ -137,7 +137,7 @@ Optional.expect_ptr :: (o: & ?$T, message: str) -> &T {
switch o {
case .Some as &v do return v;
case _ {
assert(false, message);
panic(message);
return .{};
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/container/result.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Result.unwrap :: (r: #Self) -> r.Ok_Type {
case .Ok as v do return v;
case .Err as err {
msg := tprintf("Unwrapping Result with error '{}'.", err);
assert(false, msg);
panic(msg);
return .{};
}
}
Expand All @@ -82,7 +82,7 @@ Result.expect :: (r: #Self, msg: str) -> r.Ok_Type {
switch r {
case .Ok as v do return v;
case _ {
assert(false, msg);
panic(msg);
return .{};
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/conv/format.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ format_any :: (output: &Format_Output, formatting: &Format, v: any) {
case i16, u16 do value = cast(u64) *(cast(&u16) v.data);
case i32, u32 do value = cast(u64) *(cast(&u32) v.data);
case i64, u64 do value = cast(u64) *(cast(&u64) v.data);
case _ do assert(false, "Bad enum backing type");
case _ do panic("Bad enum backing type");
}

if !formatting.interpret_numbers {
Expand Down Expand Up @@ -736,7 +736,7 @@ format_any :: (output: &Format_Output, formatting: &Format, v: any) {
case i16, u16 do tag_value = cast(u64) *(cast(&u16) v.data);
case i32, u32 do tag_value = cast(u64) *(cast(&u32) v.data);
case i64, u64 do tag_value = cast(u64) *(cast(&u64) v.data);
case _ do assert(false, "Bad union backing type");
case _ do panic("Bad union backing type");
}

variant := array.first(u.variants, [x](x.tag_value == ~~tag_value));
Expand Down
4 changes: 2 additions & 2 deletions core/encoding/osad.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ serialize :: (v: any, w: &io.Writer) -> bool {
case i16, u16 do tag_value = cast(u64) *(cast(&u16) v.data);
case i32, u32 do tag_value = cast(u64) *(cast(&u32) v.data);
case i64, u64 do tag_value = cast(u64) *(cast(&u64) v.data);
case _ do assert(false, "Bad union backing type");
case _ do panic("Bad union backing type");
}

variant := array.first(u_info.variants, [x](x.tag_value == ~~tag_value));
Expand Down Expand Up @@ -264,7 +264,7 @@ deserialize :: (target: rawptr, type: type_expr, r: &io.Reader, allocator := con
case i16, u16 do *(cast(&u16) target) = ~~variant_value;
case i32, u32 do *(cast(&u32) target) = ~~variant_value;
case i64, u64 do *(cast(&u64) target) = ~~variant_value;
case _ do assert(false, "Bad union backing type");
case _ do panic("Bad union backing type");
}

try(deserialize(base + u_info.alignment, variant.type, r));
Expand Down
4 changes: 2 additions & 2 deletions core/io/binary.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ binary_write :: (use bw: &BinaryWriter, $T: type_expr, v: &T) {
}

binary_write_slice :: (use bw: &BinaryWriter, sl: [] $T, output_size := false) {
assert(false, "binary_write_slice is not working at the moment");
panic("binary_write_slice is not working at the moment");
if output_size do binary_write(bw, i32, sl.count);

bytes := ([] u8).{
Expand Down Expand Up @@ -64,7 +64,7 @@ binary_read_slice :: (use br: &BinaryReader,
size := 0, read_size := false,
allocator := context.allocator) -> [] T {

assert(false, "binary_write_slice is not working at the moment");
panic("binary_write_slice is not working at the moment");

if size == 0 && read_size {
size = binary_read(br, i32);
Expand Down
8 changes: 4 additions & 4 deletions core/math/math.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,22 @@ cos :: (t: f32) -> f32 {
}

asin :: (t: f32) -> f32 {
assert(false, "asin is not implemented yet!");
panic("asin is not implemented yet!");
return 0;
}

acos :: (t: f32) -> f32 {
assert(false, "acos is not implemented yet!");
panic("acos is not implemented yet!");
return 0;
}

atan :: (t: f32) -> f32 {
assert(false, "atan is not implemented yet!");
panic("atan is not implemented yet!");
return 0;
}

atan2 :: (t: f32) -> f32 {
assert(false, "atan2 is not implemented yet!");
panic("atan2 is not implemented yet!");
return 0;
}

Expand Down
16 changes: 8 additions & 8 deletions core/onyx/cbindgen.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ compile_c_file :: (
if is_pointer(p) {
string.append(call_signature, "p");
} else {
assert(false, tprintf("Unsupported type in function pointer: {}", p));
panic(tprintf("Unsupported type in function pointer: {}", p));
}
}
}
Expand Down Expand Up @@ -373,7 +373,7 @@ compile_c_file :: (
if last_arg_is_return_value {
return_type := slice.get(method_info.parameter_types, -1);
if return_type->info().kind != .Pointer {
assert(false, tprintf("last_arg_is_return_value requires last parameter to be a pointer. ({} in {})", method_info.parameter_types, method_name));
panic(tprintf("last_arg_is_return_value requires last parameter to be a pointer. ({} in {})", method_info.parameter_types, method_name));
}

return_type = return_type->info()->as_pointer().to;
Expand Down Expand Up @@ -432,8 +432,8 @@ compile_c_file :: (
case .Multi_Pointer do return "ptr";
case .Array do return "ptr";

case .Function do return "i32"; // assert(false, "Passing functions between wasm and c is not yet supported.");
case .Slice do assert(false, "Passing a slice from c to wasm is not yet supported.");
case .Function do return "i32"; // panic("Passing functions between wasm and c is not yet supported.");
case .Slice do panic("Passing a slice from c to wasm is not yet supported.");
case .Enum do return type_to_wasm_type((cast(&Type_Info_Enum) param_info).backing_type);
case .Distinct do return type_to_wasm_type((cast(&Type_Info_Distinct) param_info).base_type);

Expand All @@ -448,11 +448,11 @@ compile_c_file :: (
return type_to_wasm_type(s_info.members[0].type);
}

assert(false, "Passing structures between wasm and c is not yet supported.");
panic("Passing structures between wasm and c is not yet supported.");
}

case .Union {
assert(false, "Passing unions between wasm and c is not yet supported.");
panic("Passing unions between wasm and c is not yet supported.");
}
}

Expand Down Expand Up @@ -487,7 +487,7 @@ compile_c_file :: (

case .Pointer do return "WASM_I32"; // This will also have to depend on the pointer size...
case .Multi_Pointer do return "WASM_I32"; // This will also have to depend on the pointer size...
case .Function do return "WASM_I32, WASM_I32, WASM_I32"; // assert(false, "Passing functions between wasm and c is not yet supported.");
case .Function do return "WASM_I32, WASM_I32, WASM_I32"; // panic("Passing functions between wasm and c is not yet supported.");
case .Array do return "WASM_I32";
case .Slice do return "WASM_I32,WASM_I32";
case .Enum do return type_encoding((cast(&Type_Info_Enum) param_info).backing_type);
Expand All @@ -499,7 +499,7 @@ compile_c_file :: (
return type_encoding(s_info.members[0].type);
}

// assert(false, "Passing structures between wasm and c is not yet supported.");
// panic("Passing structures between wasm and c is not yet supported.");
return "WASM_I32";
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/os/tty.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ tty_set :: (state: &TTY_State) -> bool {
} else {

tty_get :: () -> TTY_State {
assert(false, "core.os.tty_get not supported on this platform.");
panic("core.os.tty_get not supported on this platform.");
}

tty_set :: (state: &TTY_State) -> bool {
assert(false, "core.os.tty_get not supported on this platform.");
panic("core.os.tty_get not supported on this platform.");
return false;
}

Expand Down

0 comments on commit a64e3ea

Please sign in to comment.