From 12a02f161504b49a54243bed996df6b44e25b7b4 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Tue, 6 Oct 2026 15:08:13 +0200 Subject: refactor wasm --- src/app/root.zig | 42 ++++++++++++++++++++---------------------- src/z/App/main.js | 9 --------- src/z/App/root.zig | 26 ++++++++++++++++---------- src/z/Env/Object.zig | 29 ----------------------------- src/z/Env/root.zig | 7 ++++--- src/z/Env/value.zig | 2 +- src/z/Object.zig | 35 +++++++++++++++++++++++++++++++++++ src/z/env/console.zig | 24 ------------------------ src/z/env/root.zig | 34 ---------------------------------- src/z/log/js.zig | 24 ++++++++++++++++++++++++ src/z/log/root.zig | 28 ++++++++++++++++++++++++++++ src/z/root.zig | 33 ++++++++++----------------------- 12 files changed, 138 insertions(+), 155 deletions(-) delete mode 100644 src/z/Env/Object.zig create mode 100644 src/z/Object.zig delete mode 100644 src/z/env/console.zig delete mode 100644 src/z/env/root.zig create mode 100644 src/z/log/js.zig create mode 100644 src/z/log/root.zig diff --git a/src/app/root.zig b/src/app/root.zig index dcba16f..6de9216 100644 --- a/src/app/root.zig +++ b/src/app/root.zig @@ -2,36 +2,40 @@ const builtin = @import("builtin"); const std = @import("std"); const z = @import("z"); -pub const panic = z.env.panic; +pub const panic = z.panic; pub const std_options: std.Options = .{ - .logFn = z.env.logFn, + .logFn = z.log.default, }; -pub const app: z.App(.{ .name = "Memora" }, .{ - .window = *z.Env.Object, - .Object = *z.Env.Object, +pub const app: z.App(.{ + .name = "Memora", + .globals = &.{ + z.Object.global(.window), + z.Object.global(.Object), + }, }) = .init; -const js = struct { - const window = app.globals.window; - const Object = app.globals.Object; -}; - fn run_wrapped() !void { var env: z.Env = .init(std.heap.wasm_allocator); defer env.deinit(); - _ = js.window.set(&env, "my_prop", 45); - _ = js.window.delete(&env, "my_prop"); + // Set and delete properties + _ = app.globals.window.set(&env, "my_prop", 45); + _ = app.globals.window.delete(&env, "my_prop"); + // Get properties const console = try app.globals.window.get(&env, "console").await(&env, .object); const location = try app.globals.window.get(&env, "location").await(&env, .object); + // Create new object const obj = app.globals.Object.new(&env); + + // Get string, needs to be freed. const hostname = try location.get(&env, "hostname").await(&env, .string); defer std.heap.wasm_allocator.free(hostname); - const call = console.call( + // Call method of an object + try console.call( &env, .log, .{ @@ -39,21 +43,15 @@ fn run_wrapped() !void { try obj.await(&env), hostname, }, - ); - - try call.await(&env, .undefined); + ).await(&env, .undefined); + // Unlink Object from global store. _ = env.free(console); } fn run() callconv(.c) 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()); + std.log.err("{}", .{e}); }; } diff --git a/src/z/App/main.js b/src/z/App/main.js index 6251a47..a875c1d 100644 --- a/src/z/App/main.js +++ b/src/z/App/main.js @@ -20,18 +20,9 @@ const imports = { WebAssembly.instantiateStreaming(fetch("index.wasm"), imports).then( ({ instance, module }) => { - const is64 = new Uint8Array( - instance.exports.memory.buffer, - instance.exports.bit_length, - 1, - )[0] == 8 - window.wasm = { exports: instance.exports, memory: instance.exports.memory, - is64: is64, - UsizeArray: is64 ? BigUint64Array : Uint32Array, - IsizeArray: is64 ? BigInt64Array : Int32Array, } wasm.exports.run(); diff --git a/src/z/App/root.zig b/src/z/App/root.zig index d684248..326f3c0 100644 --- a/src/z/App/root.zig +++ b/src/z/App/root.zig @@ -2,19 +2,25 @@ const builtin = @import("builtin"); const std = @import("std"); const h = @import("html"); -pub const Options = struct { name: []const u8 }; +pub const Options = struct { + name: []const u8, + globals: []const Global, -pub fn App(comptime options: Options, globals: anytype) type { - const info = @typeInfo(@TypeOf(globals)).@"struct"; + pub const Global = struct { + name: []const u8, + type: type, + }; +}; +pub fn App(comptime options: Options) type { if (builtin.target.cpu.arch.isWasm()) { - var names: [info.fields.len][]const u8 = undefined; - var types: [info.fields.len]type = undefined; - var attrs: [info.fields.len]std.builtin.Type.StructField.Attributes = undefined; + var names: [options.globals.len][]const u8 = undefined; + var types: [options.globals.len]type = undefined; + var attrs: [options.globals.len]std.builtin.Type.StructField.Attributes = undefined; - for (info.fields, 0..) |field, index| { + for (options.globals, 0..) |field, index| { names[index] = field.name; - types[index] = @field(globals, field.name); + types[index] = field.type; attrs[index] = .{}; } @@ -23,7 +29,7 @@ pub fn App(comptime options: Options, globals: anytype) type { globals: G = g: { var gl: G = undefined; - for (info.fields, 1..) |field, id| { + for (options.globals, 1..) |field, id| { @field(gl, field.name) = @ptrFromInt(id); } @@ -47,7 +53,7 @@ pub fn App(comptime options: Options, globals: anytype) type { .native(.script, .{}, .content(reg: { var register_code: []const u8 = ""; - for (info.fields, 1..) |field, id| { + for (options.globals, 1..) |field, id| { register_code = register_code ++ std.fmt.comptimePrint( "ObjectStore.registerObject({}, {s});", .{ id, field.name }, diff --git a/src/z/Env/Object.zig b/src/z/Env/Object.zig deleted file mode 100644 index 77afa1f..0000000 --- a/src/z/Env/Object.zig +++ /dev/null @@ -1,29 +0,0 @@ -const Env = @import("root.zig"); -const AnyFuture = Env.AnyFuture; -const Future = Env.Future; - -const Self = @This(); - -pub inline fn get(self: *Self, env: *Env, key: anytype) AnyFuture { - return env.getProp(self, key); -} - -pub inline fn set(self: *Self, env: *Env, key: anytype, value: anytype) Future(.undefined) { - return env.setProp(self, key, value); -} - -pub inline fn delete(self: *Self, env: *Env, key: anytype) Future(.undefined) { - return env.deleteProp(self, key); -} - -pub inline fn call(self: *Self, env: *Env, method: @EnumLiteral(), args: anytype) AnyFuture { - return env.callMethod(self, @tagName(method), args); -} - -pub inline fn free(self: *Self, env: *Env) Future(.undefined) { - return env.free(self); -} - -pub fn new(self: *Self, env: *Env) Future(.object) { - return env.new(self, .{}); -} diff --git a/src/z/Env/root.zig b/src/z/Env/root.zig index 5a08362..6cc5ae3 100644 --- a/src/z/Env/root.zig +++ b/src/z/Env/root.zig @@ -1,15 +1,16 @@ const std = @import("std"); -const js = @import("js.zig"); const Self = @This(); -pub const Call = @import("call.zig").Call; +const js = @import("js.zig"); const future = @import("future.zig"); +const Object = @import("../Object.zig"); + +pub const Call = @import("call.zig").Call; pub const AnyFuture = future.AnyFuture; pub const Future = future.Future; pub const Value = @import("value.zig").Value; -pub const Object = @import("Object.zig"); calls: std.ArrayList(Call) = .empty, values: std.ArrayList(Value) = .empty, diff --git a/src/z/Env/value.zig b/src/z/Env/value.zig index b077fb2..59b788f 100644 --- a/src/z/Env/value.zig +++ b/src/z/Env/value.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const Object = @import("root.zig").Object; +const Object = @import("../Object.zig"); pub fn isString(comptime T: type) bool { return comptime blk: { diff --git a/src/z/Object.zig b/src/z/Object.zig new file mode 100644 index 0000000..4e66e5b --- /dev/null +++ b/src/z/Object.zig @@ -0,0 +1,35 @@ +const mod = @import("root.zig"); +const Options = mod.Options; +const Env = mod.Env; +const AnyFuture = Env.AnyFuture; +const Future = Env.Future; + +const Self = @This(); + +pub inline fn get(self: *Self, env: *Env, key: anytype) AnyFuture { + return env.getProp(self, key); +} + +pub inline fn set(self: *Self, env: *Env, key: anytype, value: anytype) Future(.undefined) { + return env.setProp(self, key, value); +} + +pub inline fn delete(self: *Self, env: *Env, key: anytype) Future(.undefined) { + return env.deleteProp(self, key); +} + +pub inline fn call(self: *Self, env: *Env, method: @EnumLiteral(), args: anytype) AnyFuture { + return env.callMethod(self, @tagName(method), args); +} + +pub inline fn free(self: *Self, env: *Env) Future(.undefined) { + return env.free(self); +} + +pub fn new(self: *Self, env: *Env) Future(.object) { + return env.new(self, .{}); +} + +pub fn global(tag: @EnumLiteral()) Options.Global { + return .{ .name = @tagName(tag), .type = *Self }; +} diff --git a/src/z/env/console.zig b/src/z/env/console.zig deleted file mode 100644 index 16f562c..0000000 --- a/src/z/env/console.zig +++ /dev/null @@ -1,24 +0,0 @@ - -const js = struct { - extern "console" fn log(buffer: [*]const u8, len: usize) void; - extern "console" fn warn(buffer: [*]const u8, len: usize) void; - extern "console" fn err(buffer: [*]const u8, len: usize) void; - extern "console" fn debug(buffer: [*]const u8, len: usize) void; -}; - - -pub fn log(buffer: []const u8) void { - js.log(@ptrCast(buffer), buffer.len); -} - -pub fn warn(buffer: []const u8) void { - js.warn(@ptrCast(buffer), buffer.len); -} - -pub fn err(buffer: []const u8) void { - js.err(@ptrCast(buffer), buffer.len); -} - -pub fn debug(buffer: []const u8) void { - js.debug(@ptrCast(buffer), buffer.len); -} diff --git a/src/z/env/root.zig b/src/z/env/root.zig deleted file mode 100644 index 3ceac84..0000000 --- a/src/z/env/root.zig +++ /dev/null @@ -1,34 +0,0 @@ -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); diff --git a/src/z/log/js.zig b/src/z/log/js.zig new file mode 100644 index 0000000..16f562c --- /dev/null +++ b/src/z/log/js.zig @@ -0,0 +1,24 @@ + +const js = struct { + extern "console" fn log(buffer: [*]const u8, len: usize) void; + extern "console" fn warn(buffer: [*]const u8, len: usize) void; + extern "console" fn err(buffer: [*]const u8, len: usize) void; + extern "console" fn debug(buffer: [*]const u8, len: usize) void; +}; + + +pub fn log(buffer: []const u8) void { + js.log(@ptrCast(buffer), buffer.len); +} + +pub fn warn(buffer: []const u8) void { + js.warn(@ptrCast(buffer), buffer.len); +} + +pub fn err(buffer: []const u8) void { + js.err(@ptrCast(buffer), buffer.len); +} + +pub fn debug(buffer: []const u8) void { + js.debug(@ptrCast(buffer), buffer.len); +} diff --git a/src/z/log/root.zig b/src/z/log/root.zig new file mode 100644 index 0000000..ba9141e --- /dev/null +++ b/src/z/log/root.zig @@ -0,0 +1,28 @@ +const std = @import("std"); + +pub const js = @import("js.zig"); + +pub fn default( + 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 => js.err, + .warn => js.warn, + .info => js.log, + .debug => js.debug, + }; + + if (scope != .default) { + writer.print("[{s}]: ", .{@tagName(scope)}) catch unreachable; + } + writer.print(format, args) catch unreachable; + + func(writer.buffered()); +} + diff --git a/src/z/root.zig b/src/z/root.zig index fc42acd..e18193b 100644 --- a/src/z/root.zig +++ b/src/z/root.zig @@ -1,29 +1,16 @@ const builtin = @import("builtin"); const std = @import("std"); -const wasm = wasm_struct: { - if (builtin.target.cpu.arch.isWasm()) { - break :wasm_struct struct { - pub const env = @import("env/root.zig"); - pub const Env = @import("Env/root.zig"); - - export const bit_length: u8 = @sizeOf(usize); - }; - } - - break :wasm_struct struct { - pub const Env = struct { - pub const Object = struct {}; - }; - }; -}; - -pub const env = wasm.env; -pub const Env = wasm.Env; +pub const log = @import("log/root.zig"); +pub const Env = @import("Env/root.zig"); +pub const Object = @import("Object.zig"); pub const App = @import("App/root.zig").App; +pub const Options = @import("App/root.zig").Options; + +fn defaultPanic(msg: []const u8, _: ?usize) noreturn { + log.js.err(msg); + @trap(); +} -pub const allocator: std.mem.Allocator = if (builtin.target.cpu.arch.isWasm()) .{ - .ptr = undefined, - .vtable = &std.heap.BrkAllocator.vtable, -} else .failing; +pub const panic = std.debug.FullPanic(defaultPanic); -- cgit v1.2.3-70-g09d2