Dynamic Object Indexing for Kym #31

Merged
kayomn merged 3 commits from kym-dynamic-indexing into main 2023-08-12 16:15:50 +02:00
3 changed files with 31 additions and 12 deletions
Showing only changes of commit 6c0e3477c2 - Show all commits

View File

@ -6,6 +6,9 @@ options = {
.width = 1280, .width = 1280,
.height = 800, .height = 800,
.tick_rate = 60, .tick_rate = 60,
["foo"] = "bar",
[42] = 42,
} }
@log_info(options.title) @log_info(options.title)

View File

@ -18,7 +18,7 @@ pub const Manifest = struct {
defer env.discard(manifest); defer env.discard(manifest);
const width = @as(u16, get: { 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); defer env.discard(ref);
const fixed = try env.unbox_fixed(ref); const fixed = try env.unbox_fixed(ref);
@ -32,7 +32,7 @@ pub const Manifest = struct {
}); });
const height = @as(u16, get: { 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); defer env.discard(ref);
const fixed = try env.unbox_fixed(ref); const fixed = try env.unbox_fixed(ref);
@ -46,7 +46,7 @@ pub const Manifest = struct {
}); });
const tick_rate = @as(f32, get: { 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); defer env.discard(ref);
break: get @floatCast(try env.unbox_float(ref)); break: get @floatCast(try env.unbox_float(ref));
@ -55,7 +55,7 @@ pub const Manifest = struct {
break: get self.tick_rate; 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); defer env.discard(ref);
const title_string = try env.unbox_string(ref); const title_string = try env.unbox_string(ref);

View File

@ -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 { pub fn make(allocator: coral.io.Allocator, error_handler: ErrorHandler) coral.io.AllocationError!RuntimeEnv {
return RuntimeEnv{ return RuntimeEnv{
.locals = RefList.make(allocator), .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); 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 { pub fn new_caller(env: *RuntimeEnv, value: Caller) RuntimeError!*RuntimeRef {
const Callable = struct { const Callable = struct {
fn call(method: Typeinfo.Method) RuntimeError!?*RuntimeRef { fn call(method: Typeinfo.Method) RuntimeError!?*RuntimeRef {