From 5117cd94bdcc5b76ce4cb428b5ea47a6c5ab6ecf Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Mon, 5 Oct 2026 16:58:46 +0200 Subject: wasm implement callMethod --- src/app/root.zig | 12 +++--- src/z/Env/Future.zig | 4 +- src/z/Env/root.zig | 47 +++++++++++++++------ src/z/Env/value.zig | 116 +++++++++++++++++++++++++++++++++++++++++---------- src/z/app/main.js | 2 +- 5 files changed, 137 insertions(+), 44 deletions(-) diff --git a/src/app/root.zig b/src/app/root.zig index 13d26b8..3d2a82e 100644 --- a/src/app/root.zig +++ b/src/app/root.zig @@ -1,13 +1,13 @@ const std = @import("std"); -pub const z = @import("z"); +const z = @import("z"); export fn run() void { var env: z.Env = .init(std.heap.wasm_allocator); defer env.deinit(); - var ref = env.getProp(1, "console") catch unreachable; - var buffer: [255]u8 = undefined; - var writer: std.Io.Writer = .fixed(&buffer); - writer.print("hello, wasm: {}", .{ref.await(&env, .object)}) catch unreachable; - z.env.console.log(writer.buffered()); + const console = (env.getProp(z.Env.window, "console") catch unreachable).await(&env, .object); + + const call = env.callMethod(console, "log", .{ "hello, wasm!" }) catch unreachable; + + call.await(&env, .undefined); } diff --git a/src/z/Env/Future.zig b/src/z/Env/Future.zig index 17266b9..2a74936 100644 --- a/src/z/Env/Future.zig +++ b/src/z/Env/Future.zig @@ -5,7 +5,7 @@ const Self = @This(); result: *Env.Value, -pub fn await(self: *Self, env: *Env, comptime kind: Env.Value.Kind) kind.asType() { +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); @@ -16,7 +16,7 @@ pub fn await(self: *Self, env: *Env, comptime kind: Env.Value.Kind) kind.asType( }; } -pub fn awaitAny(self: *Self, env: *Env) Env.Value { +pub fn awaitAny(self: *const Self, env: *Env) Env.Value { defer env.pool.destroy(self.result); if (self.result.kind != .uninit) { diff --git a/src/z/Env/root.zig b/src/z/Env/root.zig index 7b31e84..c2ebade 100644 --- a/src/z/Env/root.zig +++ b/src/z/Env/root.zig @@ -4,9 +4,13 @@ const mod = @import("../root.zig"); const js = @import("js.zig"); const Self = @This(); + pub const Call = @import("call.zig").Call; pub const Future = @import("Future.zig"); pub const Value = @import("value.zig").Value; +pub const Object = extern struct {}; + +pub const window: *Object = @ptrFromInt(1); calls: std.ArrayList(Call) = .empty, values: std.ArrayList(Value) = .empty, @@ -18,24 +22,39 @@ pub fn init(gpa: std.mem.Allocator) Self { return .{ .gpa = gpa }; } -pub fn getProp(self: *Self, id: u32, key: []const u8) !Future { +pub fn getProp(self: *Self, object: *Object, key: []const u8) !Future { try self.calls.append(self.gpa, .{ .kind = .get_prop, .num_args = 2, }); - try self.values.append(self.gpa, .{ - .kind = .object, - .value = .{ .object = id }, - }); - try self.values.append(self.gpa, .{ - .kind = .string, - .value = .{ - .string = .{ - .ptr = @ptrCast(key), - .len = key.len, - }, - }, + 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, + key: []const u8, + args: anytype, +) !Future { + try self.calls.append(self.gpa, .{ + .kind = .call_method, + .num_args = 2 + args.len, }); + + try self.values.append(self.gpa, .from(object)); + try self.values.append(self.gpa, .from(key)); + + 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; try self.results.append(self.gpa, result); @@ -49,6 +68,8 @@ pub fn flush(self: *Self) void { @ptrCast(self.results.items), self.results.items.len, ); + self.calls.clearRetainingCapacity(); + self.values.clearRetainingCapacity(); self.results.clearRetainingCapacity(); } diff --git a/src/z/Env/value.zig b/src/z/Env/value.zig index 4630a76..b22b88a 100644 --- a/src/z/Env/value.zig +++ b/src/z/Env/value.zig @@ -1,7 +1,34 @@ +const std = @import("std"); +const Object = @import("root.zig").Object; + +pub fn isString(comptime T: type) bool { + return comptime blk: { + const info = @typeInfo(T); + if (info != .pointer) break :blk false; + + const ptr = &info.pointer; + if (ptr.is_volatile or ptr.is_allowzero) break :blk false; + + if (ptr.size == .slice) { + break :blk ptr.child == u8; + } + + if (ptr.size == .one) { + const child = @typeInfo(ptr.child); + if (child == .array) { + const arr = &child.array; + break :blk arr.child == u8; + } + } + + break :blk false; + }; +} + pub const Value = extern struct { kind: Kind, value: extern union { - object: u32, + object: *Object, string: extern struct { ptr: [*]const u8, len: usize, @@ -12,25 +39,70 @@ pub const Value = extern struct { undefined: void, }, - pub const Kind = enum(u8) { - uninit = 0, - object = 1, - string = 2, - int = 3, - uint = 4, - float = 5, - undefined = 6, - - pub fn asType(comptime self: Kind) type { - return switch (self) { - .object => return u32, - .string => return []const u8, - .int => return isize, - .uint => return usize, - .float => f64, - .undefined => return void, - .uninit => @compileError("uninit has no associated type."), - }; - } - }; + pub fn from(value: anytype) Value { + const T = @TypeOf(value); + + return switch (T) { + *Object => .{ + .kind = .object, + .value = .{ .object = value }, + }, + usize => .{ + .kind = .uint, + .value = .{ .uint = value }, + }, + isize => .{ + .kind = .int, + .value = .{ .int = value }, + }, + f64, f32, f16 => .{ + .kind = .float, + .value = .{ .float = value }, + }, + void => .{ + .kind = .undefined, + .value = .{ .undefined = {} }, + }, + else => str: { + if (comptime isString(T)) { + break :str .{ + .kind = .string, + .value = .{ + .string = .{ + .ptr = @ptrCast(value), + .len = value.len, + }, + }, + }; + } else { + @compileError(std.fmt.comptimePrint( + "cannot auto convert type '{}'.", + .{T}, + )); + } + }, + }; + } + + pub const Kind = enum(u8) { + uninit = 0, + object = 1, + string = 2, + int = 3, + uint = 4, + float = 5, + undefined = 6, + + pub fn asType(comptime self: Kind) type { + return switch (self) { + .object => return *Object, + .string => return []const u8, + .int => return isize, + .uint => return usize, + .float => f64, + .undefined => return void, + .uninit => @compileError("uninit has no associated type."), + }; + } + }; }; diff --git a/src/z/app/main.js b/src/z/app/main.js index bad4422..07fa9b8 100644 --- a/src/z/app/main.js +++ b/src/z/app/main.js @@ -213,7 +213,7 @@ const imports = { warn(buffer, length) { console.warn(decodeString(buffer, length)) }, - error(buffer, length) { + err(buffer, length) { console.error(decodeString(buffer, length)) }, debug(buffer, length) { -- cgit v1.2.3-70-g09d2