Skip to content

Commit

Permalink
Add core.eprint
Browse files Browse the repository at this point in the history
  • Loading branch information
fubark committed Apr 15, 2024
1 parent 757fb90 commit 60d2546
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/builtins/builtins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const funcs = [_]NameFunc{
// Utils.
.{"copy", copy},
.{"dump", zErrFunc(dump)},
.{"eprint", eprint},
.{"errorReport", zErrFunc(errorReport)},
.{"getObjectRc", zErrFunc(getObjectRc)},
.{"is", is},
Expand Down Expand Up @@ -526,6 +527,30 @@ pub fn performGC(vm: *cy.VM, _: [*]const Value, _: u8) anyerror!Value {
return map;
}

pub fn eprint(vm: *cy.VM, args: [*]const cy.Value, _: u8) Value {
const err = eprint_c(vm, args[0]);
if (!err.isNull()) {
return Value.Interrupt;
}
return Value.Void;
}

pub fn eprint_c(ctx: cy.Context, arg: rt.Any) callconv(.C) rt.Error {
if (build_options.rt == .vm) {
const str = ctx.getOrBufPrintValueStr(&cy.tempBuf, arg) catch |err| {
return cy.builtins.prepThrowZError2(ctx, err, @errorReturnTrace());
};
rt.err(ctx, str);
rt.err(ctx, "\n");
} else {
const str = arg.type.toPrintString(ctx, arg);
rt.err(ctx, str.slice());
rt.err(ctx, "\n");
ctx.release(str.buf);
}
return rt.Error.initNull();
}

pub fn print(vm: *cy.VM, args: [*]const cy.Value, _: u8) Value {
const err = print_c(vm, args[0]);
if (!err.isNull()) {
Expand Down
3 changes: 3 additions & 0 deletions src/builtins/builtins_vm.cy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
--| Prints the result of `toCyon` on a value.
#host func dump(val any) void

--| Prints a value to the error stream. The host determines how it is printed.
#host func eprint(str any) void

#host func errorReport() String

--| Returns the current reference count of an object.
Expand Down
2 changes: 1 addition & 1 deletion test/core/table_access_panic.cy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ o.b
--cytest: error
--panic: Field not found in value.
--
--core:183:16 $get:
--core:186:16 $get:
-- return table_data[name]
-- ^
--main:2:3 main:
Expand Down

0 comments on commit 60d2546

Please sign in to comment.