Streamline arity detection
continuous-integration/drone/pr Build is passing Details
continuous-integration/drone/push Build is passing Details

This commit is contained in:
kayomn 2023-11-05 23:46:07 +00:00
parent 75ddf3f2bb
commit 19e1fb2634
3 changed files with 19 additions and 30 deletions

View File

@ -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", .{ else => self.raise(error.TypeMismatch, "{typename} is not get-indexable", .{
.typename = indexable.typename(), .typename = indexable.typename(),
@ -528,10 +528,8 @@ pub const RuntimeEnv = struct {
} }
pub fn push_frame(self: *RuntimeEnv, callable: *const RuntimeRef, arg_count: u8) RuntimeError!Frame { pub fn push_frame(self: *RuntimeEnv, callable: *const RuntimeRef, arg_count: u8) RuntimeError!Frame {
const arity = switch (callable.object().payload) { if (callable.as_dynamic(Chunk.typeinfo)) |chunk_userdata| {
.dynamic => |dynamic| dynamic.typeinfo().get_arity(dynamic.userdata()), const arity = @as(*Chunk, @ptrCast(@alignCast(chunk_userdata))).arity;
else => 0,
};
if (arg_count < arity) { if (arg_count < arity) {
return self.raise(error.BadOperation, "expected `{expected}` {noun}, {provided} provided", .{ return self.raise(error.BadOperation, "expected `{expected}` {noun}, {provided} provided", .{
@ -540,6 +538,7 @@ pub const RuntimeEnv = struct {
.noun = if (arity == 1) "argument" else "arguments", .noun = if (arity == 1) "argument" else "arguments",
}); });
} }
}
const frame = Frame{ const frame = Frame{
.callable = callable.acquire(), .callable = callable.acquire(),
@ -607,7 +606,7 @@ pub const RuntimeEnv = struct {
pub fn set(self: *RuntimeEnv, indexable: *RuntimeRef, index: *const RuntimeRef, value: ?*const RuntimeRef) RuntimeError!void { pub fn set(self: *RuntimeEnv, indexable: *RuntimeRef, index: *const RuntimeRef, value: ?*const RuntimeRef) RuntimeError!void {
return switch (indexable.object().payload) { 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", .{ else => self.raise(error.TypeMismatch, "{typename} is not set-indexable", .{
.typename = indexable.typename(), .typename = indexable.typename(),
@ -992,24 +991,19 @@ pub const Typeinfo = struct {
size: usize, size: usize,
destruct: ?*const fn (env: *RuntimeEnv, userdata: []coral.io.Byte) void = null, 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, 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, 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, get: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte, index: *const RuntimeRef) RuntimeError!?*RuntimeRef = default_get,
set_index: *const fn (env: *RuntimeEnv, userdata: []coral.io.Byte, value: *const RuntimeRef, value: ?*const RuntimeRef) RuntimeError!void = default_set_index, 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 { fn default_call(env: *RuntimeEnv, _: []coral.io.Byte, _: *const Frame) RuntimeError!?*RuntimeRef {
return env.raise(error.BadOperation, "this dynamic object is not callable", .{}); return env.raise(error.BadOperation, "this dynamic object is not callable", .{});
} }
fn default_get_arity(_: []coral.io.Byte) u8 { fn default_get(env: *RuntimeEnv, _: []coral.io.Byte, _: *const RuntimeRef) RuntimeError!?*RuntimeRef {
return 0;
}
fn default_get_index(env: *RuntimeEnv, _: []coral.io.Byte, _: *const RuntimeRef) RuntimeError!?*RuntimeRef {
return env.raise(error.BadOperation, "this dynamic object is not get-indexable", .{}); 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", .{}); return env.raise(error.BadOperation, "this dynamic object is not set-indexable", .{});
} }

View File

@ -638,7 +638,6 @@ pub const typeinfo = &kym.Typeinfo{
.destruct = typeinfo_destruct, .destruct = typeinfo_destruct,
.call = typeinfo_call, .call = typeinfo_call,
.to_string = typeinfo_to_string, .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 { 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); @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 { 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); return env.to_string(@as(*Self, @ptrCast(@alignCast(userdata))).name);
} }

View File

@ -47,15 +47,15 @@ pub const typeinfo = &kym.Typeinfo{
.size = @sizeOf(Self), .size = @sizeOf(Self),
.name = "table", .name = "table",
.destruct = typeinfo_destruct, .destruct = typeinfo_destruct,
.get_index = typeinfo_get_index, .get = typeinfo_get,
.set_index = typeinfo_set_index, .set = typeinfo_set,
}; };
fn typeinfo_destruct(env: *kym.RuntimeEnv, userdata: []coral.io.Byte) void { fn typeinfo_destruct(env: *kym.RuntimeEnv, userdata: []coral.io.Byte) void {
@as(*Self, @ptrCast(@alignCast(userdata))).free(env); @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 table = @as(*Self, @ptrCast(@alignCast(userdata)));
const acquired_index = index.acquire(); const acquired_index = index.acquire();
@ -79,7 +79,7 @@ fn typeinfo_get_index(env: *kym.RuntimeEnv, userdata: []coral.io.Byte, index: *c
return null; 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 table = @as(*Self, @ptrCast(@alignCast(userdata)));
const acquired_index = index.acquire(); const acquired_index = index.acquire();