aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-10-15 21:57:59 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-10-15 21:57:59 +0200
commitdd24e86dad0402917b1713d1afeefaf65c3d1e51 (patch)
tree7f6c9f4d23eb8e60b6b7374233f33423cff40708 /src
parenta1dc55be6e695a1c04e5e9e0f1ce7406a5dd3dff (diff)
zilch: reusing results in processing loop
Diffstat (limited to 'src')
-rw-r--r--src/app/components/navbutton/root.zig15
-rw-r--r--src/zilch/App/object-store.js13
-rw-r--r--src/zilch/Env/future.zig26
-rw-r--r--src/zilch/Env/root.zig17
-rw-r--r--src/zilch/Env/value.zig10
-rw-r--r--src/zilch/Object.zig30
6 files changed, 70 insertions, 41 deletions
diff --git a/src/app/components/navbutton/root.zig b/src/app/components/navbutton/root.zig
index f2b71e6..6a403cb 100644
--- a/src/app/components/navbutton/root.zig
+++ b/src/app/components/navbutton/root.zig
@@ -1,6 +1,7 @@
const std = @import("std");
const app = @import("root").app;
const zilch = @import("zilch");
+const object = zilch.Object.bindings;
const Self = @This();
@@ -22,10 +23,16 @@ pub fn init(self: *Self, element: *zilch.Object) !void {
defer env.deinit();
self.element = element;
- self.shadow_root = try self.element.get(&env, "shadowRoot").await(&env, .object);
- const button = try self.shadow_root.call(&env, "getElementById", .{"button"}).await(&env, .object);
- defer _ = button.free(&env);
- self.button.classList = try button.get(&env, "classList").await(&env, .object);
+
+ var shadow_root = self.element.get(&env, "shadowRoot");
+
+ var button = env.call(object.call, .{shadow_root, "getElementById", "button"});
+
+ var class_list = env.call(object.get, .{button, "classList"});
+ self.button.classList = try class_list.await(&env, .object);
+
+ self.shadow_root = try shadow_root.await(&env, .object);
+ _ = (try button.await(&env, .object)).free(&env);
}
pub fn deinit(self: *Self) !void {
diff --git a/src/zilch/App/object-store.js b/src/zilch/App/object-store.js
index d1fe42f..5199235 100644
--- a/src/zilch/App/object-store.js
+++ b/src/zilch/App/object-store.js
@@ -7,6 +7,7 @@ class ObjectStore {
4: "uint",
5: "float",
6: "undefined",
+ 7: "future",
"uninit": 0,
"object": 1,
@@ -15,6 +16,7 @@ class ObjectStore {
"uint": 4,
"float": 5,
"undefined": 6,
+ "future": 7,
});
static store = { 0: null };
@@ -46,7 +48,7 @@ class ObjectStore {
}
}
- static decodeValue(view, offset) {
+ static decodeValue(view, offset, results) {
const kind = ObjectStore.types[view.getUint8(offset, true)];
if (!kind) throw "unknown type, cannot decode";
@@ -73,6 +75,10 @@ class ObjectStore {
if (kind === 'float') {
return view.getFloat64(offset + 8, true);
}
+
+ if (kind === 'future') {
+ return results[view.getUint32(offset + 8, true)];
+ }
}
static registerObject(id, obj) {
@@ -150,13 +156,14 @@ class ObjectStore {
const calls_view = new DataView(wasm.memory.buffer, calls_base);
const values_view = new DataView(wasm.memory.buffer, values_base);
const results_view = new DataView(wasm.memory.buffer, result_base);
+ const results = [];
for (let i = 0; i < length; ++i) {
const method = ObjectStore.decodeCall(calls_view, offset.call);
offset.call += 8;
let args = new Array(method.num_args).fill().map(_ => {
- const value = ObjectStore.decodeValue(values_view, offset.value);
+ const value = ObjectStore.decodeValue(values_view, offset.value, results);
offset.value += 16;
return value;
});
@@ -165,6 +172,8 @@ class ObjectStore {
const result_ptr = results_view.getUint32(offset.result, true);
offset.result += 4;
+ results.push(result);
+
ObjectStore.writeValue(result_ptr, result);
}
}
diff --git a/src/zilch/Env/future.zig b/src/zilch/Env/future.zig
index 6e0cc63..8b88cd8 100644
--- a/src/zilch/Env/future.zig
+++ b/src/zilch/Env/future.zig
@@ -22,10 +22,14 @@ pub fn Future(comptime kind: Env.Value.Kind) type {
pub const AnyFuture = union(enum) {
const Self = @This();
- result: *Env.Value,
+ unresolved: struct {
+ id: usize,
+ value: *Env.Value,
+ },
+ resolved: Env.Value,
err: anyerror,
- pub fn await(self: *const Self, env: *Env, comptime kind: Env.Value.Kind) !kind.asType() {
+ pub fn await(self: *Self, env: *Env, comptime kind: Env.Value.Kind) !kind.asType() {
const result = try self.awaitAny(env);
if (kind != result.kind) {
@@ -34,25 +38,25 @@ pub const AnyFuture = union(enum) {
}
return switch (kind) {
- .uninit => @panic("uninitialized result"),
.string => result.value.string.ptr[0..result.value.string.len],
else => @field(result.value, @tagName(kind)),
};
}
- pub fn awaitAny(self: *const Self, env: *Env) !Env.Value {
+ pub fn awaitAny(self: *Self, env: *Env) !Env.Value {
+ defer self.* = undefined;
switch (self.*) {
.err => |err| return err,
- .result => |result| {
- defer env.release(self.*);
-
- if (result.kind != .uninit) {
- return result.*;
- }
+ .resolved => |result| return result,
+ .unresolved => |result| {
+ const value = result.value;
+ defer env.release(value);
env.flush();
- return result.*;
+ self.* = .{ .resolved = value.* };
+
+ return value.*;
}
}
}
diff --git a/src/zilch/Env/root.zig b/src/zilch/Env/root.zig
index 5ab3fbd..5244830 100644
--- a/src/zilch/Env/root.zig
+++ b/src/zilch/Env/root.zig
@@ -20,6 +20,8 @@ const Self = @This();
pub const default: Self = .{ .gpa = std.heap.wasm_allocator };
pub fn call(self: *Self, function: Function, args: anytype) AnyFuture {
+ const id = self.calls.items.len;
+
self.calls.append(self.gpa, .{
.kind = function.id,
.num_args = args.len,
@@ -31,9 +33,13 @@ pub fn call(self: *Self, function: Function, args: anytype) AnyFuture {
const result = self.pool.create(self.gpa) catch |err| return .{ .err = err };
errdefer self.pool.destroy(result);
- result.kind = .uninit;
self.results.append(self.gpa, result) catch |err| return .{ .err = err };
- return .{ .result = result };
+ return .{
+ .unresolved = .{
+ .id = id,
+ .value = result,
+ },
+ };
}
pub fn flush(self: *Self) void {
@@ -48,11 +54,8 @@ pub fn flush(self: *Self) void {
self.results.clearRetainingCapacity();
}
-pub fn release(self: *Self, future: AnyFuture) void {
- switch (future) {
- .err => {},
- .result => |result| self.pool.destroy(result),
- }
+pub fn release(self: *Self, value: *Value) void {
+ self.pool.destroy(value);
}
pub fn deinit(self: *Self) void {
diff --git a/src/zilch/Env/value.zig b/src/zilch/Env/value.zig
index 59b788f..cedaa89 100644
--- a/src/zilch/Env/value.zig
+++ b/src/zilch/Env/value.zig
@@ -1,5 +1,6 @@
const std = @import("std");
const Object = @import("../Object.zig");
+const AnyFuture = @import("future.zig").AnyFuture;
pub fn isString(comptime T: type) bool {
return comptime blk: {
@@ -37,6 +38,7 @@ pub const Value = extern struct {
uint: usize,
float: f64,
undefined: void,
+ future: usize,
},
pub fn from(value: anytype) Value {
@@ -70,6 +72,10 @@ pub const Value = extern struct {
.kind = .undefined,
.value = .{ .undefined = {} },
},
+ AnyFuture => .{
+ .kind = .future,
+ .value = .{ .future = value.unresolved.id },
+ },
else => str: {
if (comptime isString(T)) {
break :str .{
@@ -92,13 +98,13 @@ pub const Value = extern struct {
}
pub const Kind = enum(u8) {
- uninit = 0,
object = 1,
string = 2,
int = 3,
uint = 4,
float = 5,
undefined = 6,
+ future = 7,
pub fn asType(comptime self: Kind) type {
return switch (self) {
@@ -108,7 +114,7 @@ pub const Value = extern struct {
.uint => return usize,
.float => f64,
.undefined => return void,
- .uninit => @compileError("uninit has no associated type."),
+ .future => AnyFuture,
};
}
};
diff --git a/src/zilch/Object.zig b/src/zilch/Object.zig
index 6c0eeee..194c43b 100644
--- a/src/zilch/Object.zig
+++ b/src/zilch/Object.zig
@@ -8,19 +8,19 @@ const Function = Env.Function;
const Self = @This();
pub inline fn get(self: *Self, env: *Env, key: anytype) AnyFuture {
- return env.call(bindings.get_prop, .{ self, key });
+ return env.call(bindings.get, .{ self, key });
}
pub inline fn set(self: *Self, env: *Env, key: anytype, value: anytype) Future(.undefined) {
- return .fromAny(env.call(bindings.set_prop, .{ self, key, value }));
+ return .fromAny(env.call(bindings.set, .{ self, key, value }));
}
pub inline fn delete(self: *Self, env: *Env, key: anytype) Future(.undefined) {
- return .fromAny(env.call(bindings.delete_prop, .{ self, key }));
+ return .fromAny(env.call(bindings.delete, .{ self, key }));
}
pub inline fn call(self: *Self, env: *Env, method: []const u8, args: anytype) AnyFuture {
- return env.call(bindings.call_method, .{ self, method } ++ args);
+ return env.call(bindings.call, .{ self, method } ++ args);
}
pub inline fn free(self: *Self, env: *Env) Future(.undefined) {
@@ -35,38 +35,38 @@ pub fn global(tag: @EnumLiteral()) Options.Global {
return .{ .name = @tagName(tag), .type = *Self };
}
-const bindings = struct {
- const get_prop: Function = .from(
+pub const bindings = struct {
+ pub const get: Function = .from(
\\function(target, key) {
\\ return target[key];
\\}
);
- const set_prop: Function = .from(
+ pub const set: Function = .from(
\\function(target, key, value) {
\\ target[key] = value;
\\}
);
- const delete_prop: Function = .from(
+ pub const delete: Function = .from(
\\function(target, key) {
\\ delete target[key];
\\}
);
- const call_method: Function = .from(
+ pub const call: Function = .from(
\\function(target, method, ...args) {
\\ return target[method](...args);
\\}
);
- const new: Function = .from(
+ pub const new: Function = .from(
\\function(target, ...args) {
\\ return new target(...args);
\\}
);
- const free: Function = .from(
+ pub const free: Function = .from(
\\function(target) {
\\ const id = target[ZsId];
\\ delete target[ZsId];
@@ -76,10 +76,10 @@ const bindings = struct {
};
pub const functions = [_]Function{
- bindings.get_prop,
- bindings.set_prop,
- bindings.delete_prop,
- bindings.call_method,
+ bindings.get,
+ bindings.set,
+ bindings.delete,
+ bindings.call,
bindings.new,
bindings.free,
};