From 6c0e3477c22208b08ab0088990b7bd575e70f29a Mon Sep 17 00:00:00 2001 From: kayomn Date: Sat, 12 Aug 2023 14:53:27 +0100 Subject: [PATCH] Separate get-extension functions from Kym env. --- debug/app.ona | 3 +++ source/ona/app.zig | 8 ++++---- source/ona/kym.zig | 32 ++++++++++++++++++++++++-------- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/debug/app.ona b/debug/app.ona index ea265f4..6f78ba6 100644 --- a/debug/app.ona +++ b/debug/app.ona @@ -6,6 +6,9 @@ options = { .width = 1280, .height = 800, .tick_rate = 60, + + ["foo"] = "bar", + [42] = 42, } @log_info(options.title) diff --git a/source/ona/app.zig b/source/ona/app.zig index 81d51f5..296b82f 100644 --- a/source/ona/app.zig +++ b/source/ona/app.zig @@ -18,7 +18,7 @@ pub const Manifest = struct { defer env.discard(manifest); const width = @as(u16, get: { - if (try env.get_field(manifest, "width")) |ref| { + if (try kym.get_field(env, manifest, "width")) |ref| { defer env.discard(ref); const fixed = try env.unbox_fixed(ref); @@ -32,7 +32,7 @@ pub const Manifest = struct { }); const height = @as(u16, get: { - if (try env.get_field(manifest, "height")) |ref| { + if (try kym.get_field(env, manifest, "height")) |ref| { defer env.discard(ref); const fixed = try env.unbox_fixed(ref); @@ -46,7 +46,7 @@ pub const Manifest = struct { }); const tick_rate = @as(f32, get: { - if (try env.get_field(manifest, "tick_rate")) |ref| { + if (try kym.get_field(env, manifest, "tick_rate")) |ref| { defer env.discard(ref); break: get @floatCast(try env.unbox_float(ref)); @@ -55,7 +55,7 @@ pub const Manifest = struct { break: get self.tick_rate; }); - if (try env.get_field(manifest, "title")) |ref| { + if (try kym.get_field(env, manifest, "title")) |ref| { defer env.discard(ref); const title_string = try env.unbox_string(ref); diff --git a/source/ona/kym.zig b/source/ona/kym.zig index 0ee0442..fb4b6f1 100644 --- a/source/ona/kym.zig +++ b/source/ona/kym.zig @@ -1056,14 +1056,6 @@ pub const RuntimeEnv = struct { }; } - pub fn get_field(self: *RuntimeEnv, indexable: *RuntimeRef, field: []const coral.io.Byte) RuntimeError!?*RuntimeRef { - const field_symbol = try self.new_symbol(field); - - defer self.discard(field_symbol); - - return self.get(indexable, field_symbol); - } - pub fn make(allocator: coral.io.Allocator, error_handler: ErrorHandler) coral.io.AllocationError!RuntimeEnv { return RuntimeEnv{ .locals = RefList.make(allocator), @@ -1399,6 +1391,30 @@ pub fn bind_syscaller(env: *RuntimeEnv, name: []const coral.io.Byte, caller: Cal try env.bind_system(name, callable); } +pub fn get_field(env: *RuntimeEnv, indexable: *RuntimeRef, field: []const coral.io.Byte) RuntimeError!?*RuntimeRef { + const field_symbol = try env.new_symbol(field); + + defer env.discard(field_symbol); + + return env.get(indexable, field_symbol); +} + +pub fn get_index(env: *RuntimeEnv, indexable: *RuntimeRef, index: Fixed) RuntimeError!?*RuntimeRef { + const index_number = try env.new_fixed(index); + + defer env.discard(index_number); + + return env.get(indexable, index_number); +} + +pub fn get_key(env: *RuntimeEnv, indexable: *RuntimeRef, key: []const coral.io.Byte) RuntimeError!?*RuntimeRef { + const key_string = try env.new_string(key); + + defer env.discard(key_string); + + return env.get(indexable, key_string); +} + pub fn new_caller(env: *RuntimeEnv, value: Caller) RuntimeError!*RuntimeRef { const Callable = struct { fn call(method: Typeinfo.Method) RuntimeError!?*RuntimeRef {