From 19e1fb26344bd2af90e59b0055a6e74cf39c63cd Mon Sep 17 00:00:00 2001 From: kayomn Date: Sun, 5 Nov 2023 23:46:07 +0000 Subject: [PATCH] Streamline arity detection --- source/ona/kym.zig | 36 +++++++++++++++--------------------- source/ona/kym/Chunk.zig | 5 ----- source/ona/kym/Table.zig | 8 ++++---- 3 files changed, 19 insertions(+), 30 deletions(-) diff --git a/source/ona/kym.zig b/source/ona/kym.zig index a3e463e..2fc8f88 100644 --- a/source/ona/kym.zig +++ b/source/ona/kym.zig @@ -271,7 +271,7 @@ pub const RuntimeEnv = struct { } }, - .dynamic => |dynamic| dynamic.typeinfo().get_index(self, dynamic.userdata(), index), + .dynamic => |dynamic| dynamic.typeinfo().get(self, dynamic.userdata(), index), else => self.raise(error.TypeMismatch, "{typename} is not get-indexable", .{ .typename = indexable.typename(), @@ -528,17 +528,16 @@ pub const RuntimeEnv = struct { } pub fn push_frame(self: *RuntimeEnv, callable: *const RuntimeRef, arg_count: u8) RuntimeError!Frame { - const arity = switch (callable.object().payload) { - .dynamic => |dynamic| dynamic.typeinfo().get_arity(dynamic.userdata()), - else => 0, - }; + if (callable.as_dynamic(Chunk.typeinfo)) |chunk_userdata| { + const arity = @as(*Chunk, @ptrCast(@alignCast(chunk_userdata))).arity; - if (arg_count < arity) { - return self.raise(error.BadOperation, "expected `{expected}` {noun}, {provided} provided", .{ - .expected = arity, - .provided = arg_count, - .noun = if (arity == 1) "argument" else "arguments", - }); + if (arg_count < arity) { + return self.raise(error.BadOperation, "expected `{expected}` {noun}, {provided} provided", .{ + .expected = arity, + .provided = arg_count, + .noun = if (arity == 1) "argument" else "arguments", + }); + } } const frame = Frame{ @@ -607,7 +606,7 @@ pub const RuntimeEnv = struct { pub fn set(self: *RuntimeEnv, indexable: *RuntimeRef, index: *const RuntimeRef, value: ?*const RuntimeRef) RuntimeError!void { return switch (indexable.object().payload) { - .dynamic => |dynamic| dynamic.typeinfo().set_index(self, dynamic.userdata(), index, value), + .dynamic => |dynamic| dynamic.typeinfo().set(self, dynamic.userdata(), index, value), else => self.raise(error.TypeMismatch, "{typename} is not set-indexable", .{ .typename = indexable.typename(), @@ -992,24 +991,19 @@ pub const Typeinfo = struct { size: usize, destruct: ?*const fn (env: *RuntimeEnv, userdata: []coral.io.Byte) void = null, to_string: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte) coral.io.AllocationError!*RuntimeRef = default_to_string, - get_arity: *const fn (userdata: []coral.io.Byte) u8 = default_get_arity, call: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte, frame: *const Frame) RuntimeError!?*RuntimeRef = default_call, - get_index: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte, index: *const RuntimeRef) RuntimeError!?*RuntimeRef = default_get_index, - set_index: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte, value: *const RuntimeRef, value: ?*const RuntimeRef) RuntimeError!void = default_set_index, + get: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte, index: *const RuntimeRef) RuntimeError!?*RuntimeRef = default_get, + set: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte, value: *const RuntimeRef, value: ?*const RuntimeRef) RuntimeError!void = default_set, fn default_call(env: *RuntimeEnv, _: []coral.io.Byte, _: *const Frame) RuntimeError!?*RuntimeRef { return env.raise(error.BadOperation, "this dynamic object is not callable", .{}); } - fn default_get_arity(_: []coral.io.Byte) u8 { - return 0; - } - - fn default_get_index(env: *RuntimeEnv, _: []coral.io.Byte, _: *const RuntimeRef) RuntimeError!?*RuntimeRef { + fn default_get(env: *RuntimeEnv, _: []coral.io.Byte, _: *const RuntimeRef) RuntimeError!?*RuntimeRef { return env.raise(error.BadOperation, "this dynamic object is not get-indexable", .{}); } - fn default_set_index(env: *RuntimeEnv, _: []coral.io.Byte, _: *const RuntimeRef, _: ?*const RuntimeRef) RuntimeError!void { + fn default_set(env: *RuntimeEnv, _: []coral.io.Byte, _: *const RuntimeRef, _: ?*const RuntimeRef) RuntimeError!void { return env.raise(error.BadOperation, "this dynamic object is not set-indexable", .{}); } diff --git a/source/ona/kym/Chunk.zig b/source/ona/kym/Chunk.zig index 499d8dc..bcfd649 100644 --- a/source/ona/kym/Chunk.zig +++ b/source/ona/kym/Chunk.zig @@ -638,7 +638,6 @@ pub const typeinfo = &kym.Typeinfo{ .destruct = typeinfo_destruct, .call = typeinfo_call, .to_string = typeinfo_to_string, - .get_arity = typeinfo_get_arity, }; fn typeinfo_call(env: *kym.RuntimeEnv, userdata: []coral.io.Byte, frame: *const kym.Frame) kym.RuntimeError!?*kym.RuntimeRef { @@ -649,10 +648,6 @@ fn typeinfo_destruct(env: *kym.RuntimeEnv, userdata: []coral.io.Byte) void { @as(*Self, @ptrCast(@alignCast(userdata))).free(env); } -fn typeinfo_get_arity(userdata: []coral.io.Byte) u8 { - return @as(*Self, @ptrCast(@alignCast(userdata))).arity; -} - fn typeinfo_to_string(env: *kym.RuntimeEnv, userdata: []coral.io.Byte) coral.io.AllocationError!*kym.RuntimeRef { return env.to_string(@as(*Self, @ptrCast(@alignCast(userdata))).name); } diff --git a/source/ona/kym/Table.zig b/source/ona/kym/Table.zig index 4638ff5..aea1a72 100644 --- a/source/ona/kym/Table.zig +++ b/source/ona/kym/Table.zig @@ -47,15 +47,15 @@ pub const typeinfo = &kym.Typeinfo{ .size = @sizeOf(Self), .name = "table", .destruct = typeinfo_destruct, - .get_index = typeinfo_get_index, - .set_index = typeinfo_set_index, + .get = typeinfo_get, + .set = typeinfo_set, }; fn typeinfo_destruct(env: *kym.RuntimeEnv, userdata: []coral.io.Byte) void { @as(*Self, @ptrCast(@alignCast(userdata))).free(env); } -fn typeinfo_get_index(env: *kym.RuntimeEnv, userdata: []coral.io.Byte, index: *const kym.RuntimeRef) kym.RuntimeError!?*kym.RuntimeRef { +fn typeinfo_get(env: *kym.RuntimeEnv, userdata: []coral.io.Byte, index: *const kym.RuntimeRef) kym.RuntimeError!?*kym.RuntimeRef { const table = @as(*Self, @ptrCast(@alignCast(userdata))); const acquired_index = index.acquire(); @@ -79,7 +79,7 @@ fn typeinfo_get_index(env: *kym.RuntimeEnv, userdata: []coral.io.Byte, index: *c return null; } -fn typeinfo_set_index(env: *kym.RuntimeEnv, userdata: []coral.io.Byte, index: *const kym.RuntimeRef, value: ?*const kym.RuntimeRef) kym.RuntimeError!void { +fn typeinfo_set(env: *kym.RuntimeEnv, userdata: []coral.io.Byte, index: *const kym.RuntimeRef, value: ?*const kym.RuntimeRef) kym.RuntimeError!void { const table = @as(*Self, @ptrCast(@alignCast(userdata))); const acquired_index = index.acquire();