Refactor Kym #47

Merged
kayomn merged 4 commits from kym-refactor into main 2023-11-12 21:23:06 +01:00
2 changed files with 29 additions and 17 deletions
Showing only changes of commit 70746b4680 - Show all commits

View File

@ -2075,12 +2075,18 @@ pub const Typeinfo = struct {
userdata: []coral.io.Byte,
///
/// TODO:
/// Pushes the index object of the get operation to the top of the stack.
///
pub fn get_index(self: *const GetContext) *RuntimeObj {
/// *Note* the index is guaranteed to be a non-`null` value.
///
/// A [RuntimeError] is returned if the virtual machine is out of memory.
///
/// The [RuntimeEnv] is returned for function chaining.
///
pub fn push_index(self: *const GetContext) RuntimeError!*RuntimeEnv {
coral.debug.assert(self.env.locals.values.len > 0);
return self.env.locals.values[self.env.locals.values.len - 1].?.internal().acquire();
return self.env.push(self.env.locals.values[self.env.locals.values.len - 1]);
}
};
@ -2092,25 +2098,31 @@ pub const Typeinfo = struct {
userdata: []coral.io.Byte,
///
/// TODO:
/// Pushes the index object of the set operation to the top of the stack.
///
pub fn get_index(self: *const SetContext) *RuntimeObj {
/// *Note* the index is guaranteed to be a non-`null` value.
///
/// A [RuntimeError] is returned if the virtual machine is out of memory.
///
/// The [RuntimeEnv] is returned for function chaining.
///
pub fn push_index(self: *const SetContext) RuntimeError!*RuntimeEnv {
coral.debug.assert(self.env.locals.values.len > 0);
return self.env.locals.values[self.env.locals.values.len - 1].?.internal().acquire();
return self.env.push(self.env.locals.values[self.env.locals.values.len - 1]);
}
///
/// TODO:
/// Pushes the value of the set operation to the top of the stack.
///
pub fn get_value(self: *const SetContext) ?*RuntimeObj {
/// A [RuntimeError] is returned if the virtual machine is out of memory.
///
/// The [RuntimeEnv] is returned for function chaining.
///
pub fn push_value(self: *const SetContext) RuntimeError!*RuntimeEnv {
coral.debug.assert(self.env.locals.values.len > 1);
if (self.env.locals.values[self.env.locals.values.len - 2]) |value| {
return value.internal().acquire();
}
return null;
return self.env.push(self.env.locals.values[self.env.locals.values.len - 2]);
}
};

View File

@ -57,7 +57,7 @@ fn typeinfo_destruct(context: kym.Typeinfo.DestructContext) void {
fn typeinfo_get(context: kym.Typeinfo.GetContext) kym.RuntimeError!?*kym.RuntimeObj {
const table = @as(*Self, @ptrCast(@alignCast(context.userdata)));
const index = context.get_index();
const index = (try context.push_index()).pop().?;
defer context.env.release(index);
@ -81,7 +81,7 @@ fn typeinfo_get(context: kym.Typeinfo.GetContext) kym.RuntimeError!?*kym.Runtime
fn typeinfo_set(context: kym.Typeinfo.SetContext) kym.RuntimeError!void {
const table = @as(*Self, @ptrCast(@alignCast(context.userdata)));
const index = context.get_index();
const index = (try context.push_index()).pop().?;
errdefer context.env.release(index);
@ -98,7 +98,7 @@ fn typeinfo_set(context: kym.Typeinfo.SetContext) kym.RuntimeError!void {
context.env.release(replacing);
}
if (context.get_value()) |value| {
if ((try context.push_value()).pop()) |value| {
errdefer context.env.release(value);
maybe_replacing.* = value;
@ -110,7 +110,7 @@ fn typeinfo_set(context: kym.Typeinfo.SetContext) kym.RuntimeError!void {
}
}
const value = context.get_value() orelse {
const value = (try context.push_value()).pop() orelse {
if (table.associative.remove(index)) |removed| {
context.env.release(removed.key);
context.env.release(removed.value);