diff --git a/src/builtins/builtins.zig b/src/builtins/builtins.zig index 3eeab7368..921d30d56 100644 --- a/src/builtins/builtins.zig +++ b/src/builtins/builtins.zig @@ -34,6 +34,7 @@ const funcs = [_]NameFunc{ // Utils. .{"copy", copy}, .{"dump", zErrFunc(dump)}, + .{"eprint", eprint}, .{"errorReport", zErrFunc(errorReport)}, .{"getObjectRc", zErrFunc(getObjectRc)}, .{"is", is}, @@ -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()) { diff --git a/src/builtins/builtins_vm.cy b/src/builtins/builtins_vm.cy index 23365cbc3..4220fe818 100644 --- a/src/builtins/builtins_vm.cy +++ b/src/builtins/builtins_vm.cy @@ -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. diff --git a/test/core/table_access_panic.cy b/test/core/table_access_panic.cy index caf30888c..4477893cb 100644 --- a/test/core/table_access_panic.cy +++ b/test/core/table_access_panic.cy @@ -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: