aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-10-05 19:38:16 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-10-05 19:38:16 +0200
commitec8ab44877368a1ac27dcdd615ad00bd1249407b (patch)
treeeb0578e4a7ffae77e4b352d8e5aa00ade68d9e24
parent5117cd94bdcc5b76ce4cb428b5ea47a6c5ab6ecf (diff)
add new method
-rw-r--r--src/app/root.zig30
-rw-r--r--src/z/Env/Future.zig7
-rw-r--r--src/z/Env/call.zig1
-rw-r--r--src/z/Env/root.zig77
-rw-r--r--src/z/Env/value.zig7
-rw-r--r--src/z/app/main.js48
-rw-r--r--src/z/env/root.zig32
7 files changed, 177 insertions, 25 deletions
diff --git a/src/app/root.zig b/src/app/root.zig
index 3d2a82e..757e1ca 100644
--- a/src/app/root.zig
+++ b/src/app/root.zig
@@ -1,13 +1,37 @@
const std = @import("std");
const z = @import("z");
-export fn run() void {
+pub const panic = z.env.panic;
+pub const std_options: std.Options = .{
+ .logFn = z.env.logFn,
+};
+
+fn run_wrapped() !void {
var env: z.Env = .init(std.heap.wasm_allocator);
defer env.deinit();
- const console = (env.getProp(z.Env.window, "console") catch unreachable).await(&env, .object);
+ _ = try env.setProp(z.Env.window, "my_prop", 45);
+ _ = try env.deleteProp(z.Env.window, "my_prop");
+
+ const console = (try env.getProp(z.Env.window, "console")).await(&env, .object);
+ const object = (try env.getProp(z.Env.window, "Object")).await(&env, .object);
- const call = env.callMethod(console, "log", .{ "hello, wasm!" }) catch unreachable;
+ const obj = try env.new(object, .{});
+
+ const call = try env.callMethod(console, "log", .{ "hello, wasm!", obj.await(&env, .object) });
call.await(&env, .undefined);
+
+ _ = try env.free(console);
+}
+
+export fn run() void {
+ run_wrapped() catch |e| {
+ var buffer: [1024]u8 = undefined;
+ var writer: std.Io.Writer = .fixed(&buffer);
+
+ writer.print("{}", .{e}) catch unreachable;
+
+ z.env.console.log(writer.buffered());
+ };
}
diff --git a/src/z/Env/Future.zig b/src/z/Env/Future.zig
index 2a74936..1e28ac7 100644
--- a/src/z/Env/Future.zig
+++ b/src/z/Env/Future.zig
@@ -1,5 +1,6 @@
const std = @import("std");
const Env = @import("root.zig");
+const z = @import("../root.zig");
const Self = @This();
@@ -7,7 +8,11 @@ result: *Env.Value,
pub fn await(self: *const Self, env: *Env, comptime kind: Env.Value.Kind) kind.asType() {
const result = self.awaitAny(env);
- std.debug.assert(kind == result.kind);
+
+ if (kind != result.kind) {
+ std.log.err("unexpected {}, expected {}", .{result.kind, kind});
+ @panic("type error");
+ }
return switch (kind) {
.uninit => @panic("uninitialized result"),
diff --git a/src/z/Env/call.zig b/src/z/Env/call.zig
index bf64392..a5db835 100644
--- a/src/z/Env/call.zig
+++ b/src/z/Env/call.zig
@@ -10,6 +10,7 @@ pub const Call = extern struct {
del_prop = 3,
call_method = 4,
call = 5,
+ new = 6,
};
};
diff --git a/src/z/Env/root.zig b/src/z/Env/root.zig
index c2ebade..c4979b9 100644
--- a/src/z/Env/root.zig
+++ b/src/z/Env/root.zig
@@ -22,7 +22,7 @@ pub fn init(gpa: std.mem.Allocator) Self {
return .{ .gpa = gpa };
}
-pub fn getProp(self: *Self, object: *Object, key: []const u8) !Future {
+pub fn getProp(self: *Self, object: *Object, key: anytype) !Future {
try self.calls.append(self.gpa, .{
.kind = .get_prop,
.num_args = 2,
@@ -33,6 +33,40 @@ pub fn getProp(self: *Self, object: *Object, key: []const u8) !Future {
return self.registerFuture();
}
+pub fn setProp(
+ self: *Self,
+ object: *Object,
+ key: anytype,
+ value: anytype,
+) !Future {
+ try self.calls.append(self.gpa, .{
+ .kind = .set_prop,
+ .num_args = 3,
+ });
+
+ try self.values.append(self.gpa, .from(object));
+ try self.values.append(self.gpa, .from(key));
+ try self.values.append(self.gpa, .from(value));
+
+ return self.registerFuture();
+}
+
+pub fn deleteProp(
+ self: *Self,
+ object: *Object,
+ key: []const u8,
+) !Future {
+ try self.calls.append(self.gpa, .{
+ .kind = .del_prop,
+ .num_args = 2,
+ });
+
+ try self.values.append(self.gpa, .from(object));
+ try self.values.append(self.gpa, .from(key));
+
+ return self.registerFuture();
+}
+
pub fn callMethod(
self: *Self,
object: *Object,
@@ -54,6 +88,47 @@ pub fn callMethod(
return self.registerFuture();
}
+pub fn call(self: *Self, object: *Object, args: anytype) !Future {
+ try self.calls.append(self.gpa, .{
+ .kind = .call,
+ .num_args = 1 + args.len,
+ });
+
+ try self.values.append(self.gpa, .from(object));
+
+ inline for (args) |arg| {
+ try self.values.append(self.gpa, .from(arg));
+ }
+
+ return self.registerFuture();
+}
+
+pub fn free(self: *Self, object: *Object) !Future {
+ try self.calls.append(self.gpa, .{
+ .kind = .free,
+ .num_args = 1,
+ });
+
+ try self.values.append(self.gpa, .from(object));
+
+ return self.registerFuture();
+}
+
+pub fn new(self: *Self, object: *Object, args: anytype) !Future {
+ try self.calls.append(self.gpa, .{
+ .kind = .new,
+ .num_args = 1 + args.len,
+ });
+
+ try self.values.append(self.gpa, .from(object));
+
+ inline for (args) |arg| {
+ try self.values.append(self.gpa, .from(arg));
+ }
+
+ return self.registerFuture();
+}
+
fn registerFuture(self: *Self) !Future {
const result = try self.pool.create(self.gpa);
result.kind = .uninit;
diff --git a/src/z/Env/value.zig b/src/z/Env/value.zig
index b22b88a..b077fb2 100644
--- a/src/z/Env/value.zig
+++ b/src/z/Env/value.zig
@@ -47,6 +47,13 @@ pub const Value = extern struct {
.kind = .object,
.value = .{ .object = value },
},
+ comptime_int => if (value >= 0) .{
+ .kind = .uint,
+ .value = .{ .uint = value },
+ } else .{
+ .kind = .int,
+ .value = .{ .int = value },
+ },
usize => .{
.kind = .uint,
.value = .{ .uint = value },
diff --git a/src/z/app/main.js b/src/z/app/main.js
index 07fa9b8..9747bfe 100644
--- a/src/z/app/main.js
+++ b/src/z/app/main.js
@@ -4,7 +4,7 @@ function decodeString(base, length) {
}
class ObjectStore {
- static id_name = "+-store-id";
+ static id_name = "@id";
static types = Object.freeze({
0: "uninit",
@@ -31,9 +31,10 @@ class ObjectStore {
3: 'delProp',
4: 'callMethod',
5: 'call',
+ 6: 'new',
});
- static store = { 0: null, 1: window };
+ static store = { 0: null };
static generateFreeId() {
let id = 0;
@@ -45,8 +46,9 @@ class ObjectStore {
return id;
}
- static free(id) {
- delete ObjectStore.store[id][ObjectStore.id_name];
+ static free(target) {
+ const id = target[ObjectStore.id_name];
+ delete target[ObjectStore.id_name];
delete ObjectStore.store[id];
}
@@ -70,12 +72,8 @@ class ObjectStore {
return target(args);
}
- static decodeValue(view, offset) {
- if (ObjectStore.types[kind] === 'string') {
- const ptr = view.getUint32((offset * 4) * 2, true);
- const len = view.getUint32((offset * 4) * 2 + 4, true);
- return decodeString(ptr, len);
- }
+ static new(target, ...args) {
+ return new target(...args);
}
static decodeCall(view, offset) {
@@ -118,17 +116,26 @@ class ObjectStore {
}
}
+ static registerObject(id, obj) {
+ ObjectStore.store[id] = obj;
+ Object.defineProperty(obj, ObjectStore.id_name, {
+ enumerable: false,
+ writable: false,
+ configurable: true,
+ value: id,
+ })
+ }
+
static writeValue(ptr, value) {
const mem = new DataView(wasm.memory.buffer, ptr);
- if (typeof value === 'object') {
+ if (typeof value === 'object' || typeof value === 'function') {
mem.setUint8(0, ObjectStore.types['object'], true);
let id = value == null ? 0 : value[ObjectStore.id_name];
if (id === undefined) {
id = ObjectStore.generateFreeId();
- ObjectStore.store[id] = value;
- value[ObjectStore.id_name] = id;
+ ObjectStore.registerObject(id, value);
}
mem.setUint32(8, id, true);
@@ -137,20 +144,21 @@ class ObjectStore {
}
if (typeof value === 'number' && Number.isInteger(value) && value >= 0) {
- // uint
- throw "unimplemented";
+ mem.setUint8(0, ObjectStore.types['uint'], true);
+ mem.setUint32(8, value, true)
return;
}
if (typeof value === 'number' && Number.isInteger(value)) {
- // int
- throw "unimplemented";
+ mem.setUint8(0, ObjectStore.types['int'], true);
+ mem.setInt32(8, value, true)
return;
}
if (typeof value === 'number') {
// float
- throw "unimplemented";
+ mem.setUint8(0, ObjectStore.types['float'], true);
+ mem.setFloat64(8, value, true)
return;
}
@@ -162,7 +170,7 @@ class ObjectStore {
if (typeof value === 'undefined') {
// undefined
- throw "unimplemented";
+ mem.setUint8(0, ObjectStore.types['undefined'], true);
return;
}
}
@@ -238,7 +246,7 @@ WebAssembly.instantiateStreaming(fetch("index.wasm"), imports).then(
IsizeArray: is64 ? BigInt64Array : Int32Array,
}
- window[ObjectStore.id_name] = 1;
+ ObjectStore.registerObject(1, window);
wasm.exports.run();
},
diff --git a/src/z/env/root.zig b/src/z/env/root.zig
index 9e49181..3ceac84 100644
--- a/src/z/env/root.zig
+++ b/src/z/env/root.zig
@@ -1,2 +1,34 @@
+const std = @import("std");
pub const console = @import("console.zig");
+
+pub fn logFn(
+ comptime level: std.log.Level,
+ comptime scope: @EnumLiteral(),
+ comptime format: []const u8,
+ args: anytype,
+) void {
+ var buffer: [1024]u8 = undefined;
+ var writer: std.Io.Writer = .fixed(&buffer);
+
+ const func = switch (level) {
+ .err => console.err,
+ .warn => console.warn,
+ .info => console.log,
+ .debug => console.debug,
+ };
+
+ if (scope != .default) {
+ writer.print("[{s}]: ", .{@tagName(scope)}) catch unreachable;
+ }
+ writer.print(format, args) catch unreachable;
+
+ func(writer.buffered());
+}
+
+fn defaultPanic(msg: []const u8, _: ?usize) noreturn {
+ console.err(msg);
+ @trap();
+}
+
+pub const panic = std.debug.FullPanic(defaultPanic);