aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/app/components/navbutton/root.zig37
-rw-r--r--src/zilch/App/object-store.js2
-rw-r--r--src/zilch/Callable.zig2
-rw-r--r--src/zilch/Env/future.zig17
-rw-r--r--src/zilch/Env/root.zig9
-rw-r--r--src/zilch/Object.zig12
-rw-r--r--src/zilch/Promise.zig2
-rw-r--r--src/zilch/root.zig1
8 files changed, 45 insertions, 37 deletions
diff --git a/src/app/components/navbutton/root.zig b/src/app/components/navbutton/root.zig
index 8b2b59d..2f26116 100644
--- a/src/app/components/navbutton/root.zig
+++ b/src/app/components/navbutton/root.zig
@@ -3,7 +3,6 @@ const app = @import("root").app;
const zilch = @import("zilch");
const Object = zilch.Object;
const Promise = zilch.Promise;
-const object = zilch.Object.bindings;
const Self = @This();
@@ -20,7 +19,7 @@ button: struct {
classList: *zilch.Object,
},
-pub fn init(self: *Self, element: *zilch.Object) !void {
+pub fn init(self: *Self, element: *Object) !void {
var env: zilch.Env = .default;
defer env.deinit();
@@ -28,13 +27,17 @@ pub fn init(self: *Self, element: *zilch.Object) !void {
var shadow_root = self.element.get(&env, "shadowRoot");
- var button = env.call(object.call, .{shadow_root, "getElementById", "button"});
+ var class_list = cls: {
+ var button = Object.call(shadow_root, &env, "getElementById", .{"button"});
+ defer button.ignore(&env);
+ defer _ = Object.free(button, &env);
+
+ break :cls Object.get(button, &env, "classList");
+ };
- 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 {
@@ -50,26 +53,6 @@ pub fn open(self: *Self) !void {
var env: zilch.Env = .default;
defer env.deinit();
- _ = self.button.classList.call(&env, "toggle", .{"open"});
-
- var promise_future = app.globals.fetch.call(&env, .{"/"});
- const promise = try promise_future.await(&env, *Promise);
- defer _ = promise.as(Object).free(&env);
-
- _ = promise.then(&env, handleResponse, self);
-}
-
-fn handleResponse(self: *Self, response: *Object) !void {
- var env: zilch.Env = .default;
- defer env.deinit();
- defer _ = response.free(&env);
-
- var promise_future = response.call(&env, "text", .{});
- const promise = try promise_future.await(&env, *Promise);
- _ = promise.then(&env, handleText, self);
-}
-
-fn handleText(self: *Self, text: []const u8) !void {
- _ = self;
- std.log.info("text: {s}", .{text});
+ var toggle_call = self.button.classList.call(&env, "toggle", .{"open"});
+ toggle_call.ignore(&env);
}
diff --git a/src/zilch/App/object-store.js b/src/zilch/App/object-store.js
index 6699c41..9a68478 100644
--- a/src/zilch/App/object-store.js
+++ b/src/zilch/App/object-store.js
@@ -90,6 +90,8 @@ class ObjectStore {
}
static writeValue(ptr, value) {
+ if (ptr === 0) return;
+
const mem = new DataView(wasm.memory.buffer, ptr);
if (typeof value === 'object' || typeof value === 'function') {
diff --git a/src/zilch/Callable.zig b/src/zilch/Callable.zig
index 6732c69..d668953 100644
--- a/src/zilch/Callable.zig
+++ b/src/zilch/Callable.zig
@@ -6,7 +6,7 @@ const Function = Env.Function;
const Self = @This();
-pub inline fn call(self: *Self, env: *Env, args: anytype) AnyFuture {
+pub inline fn call(self: anytype, env: *Env, args: anytype) AnyFuture {
return env.call(bindings.call, .{ self } ++ args);
}
diff --git a/src/zilch/Env/future.zig b/src/zilch/Env/future.zig
index 1b44d85..f829009 100644
--- a/src/zilch/Env/future.zig
+++ b/src/zilch/Env/future.zig
@@ -13,15 +13,20 @@ pub fn Future(Kind: type) type {
return .{ .future = future };
}
- pub fn await(self: *const Self, env: *Env) !Kind {
+ pub fn await(self: *Self, env: *Env) !Kind {
return self.future.await(env, Kind);
}
+
+ pub fn ignore(self: *Self, env: *Env) void {
+ return self.future.ignore(env);
+ }
};
}
pub const AnyFuture = union(enum) {
const Self = @This();
+ ignored: void,
unresolved: struct {
id: usize,
value: *Env.Value,
@@ -37,6 +42,7 @@ pub const AnyFuture = union(enum) {
pub fn awaitAny(self: *Self, env: *Env) !Env.Value {
defer self.* = undefined;
switch (self.*) {
+ .ignored => @panic("awaited ignored value"),
.err => |err| return err,
.resolved => |result| return result,
.unresolved => |result| {
@@ -51,4 +57,13 @@ pub const AnyFuture = union(enum) {
}
}
}
+
+ pub fn ignore(self: *Self, env: *Env) void {
+ defer self.* = .ignored;
+
+ switch (self.*) {
+ .unresolved => env.ignore(self),
+ else => {},
+ }
+ }
};
diff --git a/src/zilch/Env/root.zig b/src/zilch/Env/root.zig
index 5244830..d169b5f 100644
--- a/src/zilch/Env/root.zig
+++ b/src/zilch/Env/root.zig
@@ -12,7 +12,7 @@ pub const Function = @import("Function.zig");
calls: std.ArrayList(Call) = .empty,
values: std.ArrayList(Value) = .empty,
pool: std.heap.MemoryPool(Value) = .empty,
-results: std.ArrayList(*Value) = .empty,
+results: std.ArrayList(?*Value) = .empty,
gpa: std.mem.Allocator,
const Self = @This();
@@ -58,6 +58,13 @@ pub fn release(self: *Self, value: *Value) void {
self.pool.destroy(value);
}
+pub fn ignore(self: *Self, future: *const AnyFuture) void {
+ if (self.results.items.len < future.unresolved.id and self.results.items[future.unresolved.id] == future.unresolved.value) {
+ self.pool.destroy(future.unresolved.value);
+ self.results.items[future.unresolved.id] = null;
+ }
+}
+
pub fn deinit(self: *Self) void {
self.flush();
diff --git a/src/zilch/Object.zig b/src/zilch/Object.zig
index b37f40b..1d30b8a 100644
--- a/src/zilch/Object.zig
+++ b/src/zilch/Object.zig
@@ -7,27 +7,27 @@ const Function = Env.Function;
const Self = @This();
-pub inline fn get(self: *Self, env: *Env, key: anytype) AnyFuture {
+pub inline fn get(self: anytype, env: *Env, key: anytype) AnyFuture {
return env.call(bindings.get, .{ self, key });
}
-pub inline fn set(self: *Self, env: *Env, key: anytype, value: anytype) Future(void) {
+pub inline fn set(self: anytype, env: *Env, key: anytype, value: anytype) Future(void) {
return .fromAny(env.call(bindings.set, .{ self, key, value }));
}
-pub inline fn delete(self: *Self, env: *Env, key: anytype) Future(void) {
+pub inline fn delete(self: anytype, env: *Env, key: anytype) Future(void) {
return .fromAny(env.call(bindings.delete, .{ self, key }));
}
-pub inline fn call(self: *Self, env: *Env, method: []const u8, args: anytype) AnyFuture {
+pub inline fn call(self: anytype, env: *Env, method: []const u8, args: anytype) AnyFuture {
return env.call(bindings.call, .{ self, method } ++ args);
}
-pub inline fn free(self: *Self, env: *Env) Future(void) {
+pub inline fn free(self: anytype, env: *Env) Future(void) {
return .fromAny(env.call(bindings.free, .{self}));
}
-pub inline fn new(self: *Self, env: *Env) Future(*Self) {
+pub inline fn new(self: anytype, env: *Env) Future(*Self) {
return .fromAny(env.call(bindings.new, .{self}));
}
diff --git a/src/zilch/Promise.zig b/src/zilch/Promise.zig
index 09654e3..76362e7 100644
--- a/src/zilch/Promise.zig
+++ b/src/zilch/Promise.zig
@@ -13,7 +13,7 @@ const Self = @This();
// else an allocation would be necessary!
var promise_value: Value = undefined;
-pub inline fn then(self: *Self, env: *Env, func: anytype, ud: *anyopaque) Future(void) {
+pub inline fn then(self: anytype, env: *Env, func: anytype, ud: *anyopaque) Future(void) {
const wrapper = struct {
fn wrapper(userdata: *anyopaque) callconv(.c) void {
const args = @typeInfo(@TypeOf(func)).@"fn".params;
diff --git a/src/zilch/root.zig b/src/zilch/root.zig
index 1ececbb..9bca612 100644
--- a/src/zilch/root.zig
+++ b/src/zilch/root.zig
@@ -6,6 +6,7 @@ pub const Call = Env.Call;
pub const AnyFuture = Env.AnyFuture;
pub const Future = Env.Future;
pub const Value = Env.Value;
+
pub const log = @import("log/root.zig");
pub const html = @import("html/root.zig");