diff options
author | Nguyễn Gia Phong <cnx@loang.net> | 2024-01-20 14:51:34 +0900 |
---|---|---|
committer | Nguyễn Gia Phong <cnx@loang.net> | 2024-01-20 14:52:55 +0900 |
commit | 54ad16f5516b235f304c789e134c5321c22ef913 (patch) | |
tree | 12df6322a5c4231d6d0bf3fba7058070ecada8a9 | |
parent | 8a0bec873acc2da61df247257d8978e037453b58 (diff) | |
download | jz-54ad16f5516b235f304c789e134c5321c22ef913.tar.gz |
Give up propagating error from Janet src eval
-rw-r--r-- | src/main.zig | 15 |
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); } |