about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/main.zig15
1 files changed, 6 insertions, 9 deletions
diff --git a/src/main.zig b/src/main.zig
index f147a7a..33288c1 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -44,13 +44,13 @@ pub const coreEnv = c.janet_core_env;
 /// Janet value structure.
 pub const Value = c.Janet;
 
-pub fn eval(env: *Table, str: []const u8, src: [*:0]const u8) union(enum) {
-    out: Value,
-    err: Value,
-} {
+/// Evaluate the Janet program given in str.
+pub fn eval(env: *Table, str: []const u8, src: [*:0]const u8) !Value {
     var ret: Value = undefined;
     const errflags = c.janet_dobytes(env, str.ptr, @intCast(str.len), src, &ret);
-    return if (errflags == 0) .{ .out = ret } else .{ .err = ret };
+    // Errors are already logged by Janet,
+    // among them only one is returned and not machine-readable.
+    return if (errflags == 0) ret else error.JanetError;
 }
 
 /// Wrap native type in Janet value.
@@ -162,8 +162,5 @@ test def {
         .documentation = "(add a b)\n\nReturn a + b.",
         .source = @src(),
     });
-    switch (eval(env, "(add 3 7)", "add")) {
-        .out => |out| try expectEqual(unwrap(i32, out), 10),
-        .err => return error.EvaluationFailure,
-    }
+    try expectEqual(unwrap(i32, try eval(env, "(add 3 7)", "add")), 10);
 }