diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-06 15:08:13 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-06 15:08:13 +0200 |
| commit | 12a02f161504b49a54243bed996df6b44e25b7b4 (patch) | |
| tree | 5aa9d142a6faba51730d0eeb69ffc0a0766d21c6 /src/z | |
| parent | a4b2398aba2211ce5901ceb46f73057864bd1707 (diff) | |
refactor wasm
Diffstat (limited to 'src/z')
| -rw-r--r-- | src/z/App/main.js | 9 | ||||
| -rw-r--r-- | src/z/App/root.zig | 26 | ||||
| -rw-r--r-- | src/z/Env/root.zig | 7 | ||||
| -rw-r--r-- | src/z/Env/value.zig | 2 | ||||
| -rw-r--r-- | src/z/Object.zig (renamed from src/z/Env/Object.zig) | 8 | ||||
| -rw-r--r-- | src/z/log/js.zig (renamed from src/z/env/console.zig) | 0 | ||||
| -rw-r--r-- | src/z/log/root.zig (renamed from src/z/env/root.zig) | 18 | ||||
| -rw-r--r-- | src/z/root.zig | 33 |
8 files changed, 44 insertions, 59 deletions
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/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/Env/Object.zig b/src/z/Object.zig index 77afa1f..4e66e5b 100644 --- a/src/z/Env/Object.zig +++ b/src/z/Object.zig @@ -1,4 +1,6 @@ -const Env = @import("root.zig"); +const mod = @import("root.zig"); +const Options = mod.Options; +const Env = mod.Env; const AnyFuture = Env.AnyFuture; const Future = Env.Future; @@ -27,3 +29,7 @@ pub inline fn free(self: *Self, env: *Env) Future(.undefined) { 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/log/js.zig index 16f562c..16f562c 100644 --- a/src/z/env/console.zig +++ b/src/z/log/js.zig diff --git a/src/z/env/root.zig b/src/z/log/root.zig index 3ceac84..ba9141e 100644 --- a/src/z/env/root.zig +++ b/src/z/log/root.zig @@ -1,8 +1,8 @@ const std = @import("std"); -pub const console = @import("console.zig"); +pub const js = @import("js.zig"); -pub fn logFn( +pub fn default( comptime level: std.log.Level, comptime scope: @EnumLiteral(), comptime format: []const u8, @@ -12,10 +12,10 @@ pub fn logFn( var writer: std.Io.Writer = .fixed(&buffer); const func = switch (level) { - .err => console.err, - .warn => console.warn, - .info => console.log, - .debug => console.debug, + .err => js.err, + .warn => js.warn, + .info => js.log, + .debug => js.debug, }; if (scope != .default) { @@ -26,9 +26,3 @@ pub fn logFn( 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/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); |