Skip to content

Commit

Permalink
fix(std): assert message parameter is optional
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Sep 15, 2023
1 parent 266c384 commit fa9cfb5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/lib/buzz_std.zig
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,18 @@ export fn assert(ctx: *api.NativeCtx) c_int {
const message_value = ctx.vm.bz_peek(0);

if (!condition_value.boolean()) {
var len: usize = 0;
const message = api.Value.bz_valueToString(message_value, &len).?;
// TODO: debug.getTrace
std.io.getStdOut().writer().print(
"Assert failed: {s}\n",
.{
message[0..len],
},
) catch unreachable;
if (message_value.isObj()) {
var len: usize = 0;
const message = api.Value.bz_valueToString(message_value, &len).?;
std.io.getStdOut().writer().print(
"Assert failed: {s}\n",
.{
message[0..len],
},
) catch unreachable;
} else {
std.io.getStdOut().writer().print("Assert failed\n", .{}) catch unreachable;
}

std.os.exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/std.buzz
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
|| If condition is false print message and exit program
|| @param condition assert condition
|| @param message message printed if `condition` is false
export extern fun assert(bool condition, str message) > void;
export extern fun assert(bool condition, str? message = null) > void;

|| Prints value on stdout
|| @param value value to print
Expand Down

0 comments on commit fa9cfb5

Please sign in to comment.