diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-11-02 08:54:45 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-11-02 08:54:45 +0100 |
| commit | 4363d446c1f40515f7830caa644cc4e580841122 (patch) | |
| tree | 4d28407c1295ffe59279b731a74c10efccbe7733 /src/zilch | |
| parent | d38e1ffb1533565ecea6dd83cca88d36690fadef (diff) | |
| parent | d5c1b186e41b74046a5cf13528727cb5171663f2 (diff) | |
Merge branch 'develop' of https://gitlab.com/Jockerkat/memora into dev
Diffstat (limited to 'src/zilch')
| -rw-r--r-- | src/zilch/Env/value.sync-conflict-20261030-190805-2JYANE7.zig | 185 | ||||
| -rw-r--r-- | src/zilch/app/load.js | 4 | ||||
| -rw-r--r-- | src/zilch/bridge/root.zig | 40 | ||||
| -rw-r--r-- | src/zilch/bridge/server.root.zig | 10 | ||||
| -rw-r--r-- | src/zilch/bridge/wasm.root.zig | 0 | ||||
| -rw-r--r-- | src/zilch/root.zig | 2 |
6 files changed, 239 insertions, 2 deletions
diff --git a/src/zilch/Env/value.sync-conflict-20261030-190805-2JYANE7.zig b/src/zilch/Env/value.sync-conflict-20261030-190805-2JYANE7.zig new file mode 100644 index 0000000..7ce2ce8 --- /dev/null +++ b/src/zilch/Env/value.sync-conflict-20261030-190805-2JYANE7.zig @@ -0,0 +1,185 @@ +const std = @import("std"); +const Object = @import("../Object.zig"); +const AnyFuture = @import("future.zig").AnyFuture; + +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 fn isObjectRef(comptime T: type) bool { + comptime { + const info = @typeInfo(T); + if (info != .pointer) return false; + + const ptr = &info.pointer; + return ptr.size == .one and @sizeOf(ptr.child) == 0; + } +} + +pub const Value = extern struct { + kind: Kind, + value: extern union { + object: *anyopaque, + string: extern struct { + ptr: [*]const u8, + len: usize, + }, + int: isize, + uint: usize, + float: f64, + undefined: void, + future: usize, + }, + + pub fn from(value: anytype) Value { + const T = @TypeOf(value); + + return switch (T) { + comptime_int => if (value >= 0) .{ + .kind = .uint, + .value = .{ .uint = value }, + } else .{ + .kind = .int, + .value = .{ .int = 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 = {} }, + }, + AnyFuture => .{ + .kind = .future, + .value = .{ .future = value.unresolved.id }, + }, + else => blk: { + if (comptime isString(T)) { + break :blk .{ + .kind = .string, + .value = .{ + .string = .{ + .ptr = @ptrCast(value), + .len = value.len, + }, + }, + }; + } else if (comptime isObjectRef(T)) { + break :blk .{ + .kind = .object, + .value = .{ .object = @ptrCast(value) }, + }; + } else if (@typeInfo(T) == .int) { + // TODO: weil kuscheln mit janina. + } else { + @compileError(std.fmt.comptimePrint( + "cannot auto convert type '{}'.", + .{T}, + )); + } + }, + }; + } + + pub fn unwrap(self: *const Value, T: type) T { + if (Kind.fromType(T) != self.kind) { + std.log.err("unexpected {}, expected {}", .{ self.kind, T }); + @panic("type error"); + } + + return switch (T) { + comptime_int => self.value.int, + usize => self.value.uint, + isize => self.value.int, + f64, f32, f16 => self.value.float, + void => {}, + AnyFuture => @panic("unsupported return type"), + else => blk: { + if (comptime isString(T)) { + break :blk self.value.string.ptr[0..self.value.string.len]; + } else if (comptime isObjectRef(T)) { + break :blk @ptrCast(self.value.object); + } else { + @compileError(std.fmt.comptimePrint( + "cannot auto convert type '{}'.", + .{T}, + )); + } + }, + }; + } + + pub const Kind = enum(u8) { + object = 0, + string = 1, + int = 2, + uint = 3, + float = 4, + undefined = 5, + future = 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, + .future => AnyFuture, + }; + } + + pub fn fromType(T: type) Kind { + return switch (T) { + comptime_int => .int, + usize => .uint, + isize => .int, + f64, f32, f16 => .float, + void => .undefined, + AnyFuture => .future, + else => blk: { + if (comptime isString(T)) { + break :blk .string; + } else if (comptime isObjectRef(T)) { + break :blk .object; + } else { + @compileError(std.fmt.comptimePrint( + "cannot auto convert type '{}'.", + .{T}, + )); + } + }, + }; + } + }; +}; diff --git a/src/zilch/app/load.js b/src/zilch/app/load.js index 5cc569f..ed3ab85 100644 --- a/src/zilch/app/load.js +++ b/src/zilch/app/load.js @@ -15,8 +15,8 @@ const imports = { debug(buffer, length) { console.debug(ObjectStore.decodeString(buffer, length)) }, - } -} + }, +}; const wasm_ready = WebAssembly.instantiateStreaming(fetch("index.wasm"), imports).then( ({ instance, module }) => { diff --git a/src/zilch/bridge/root.zig b/src/zilch/bridge/root.zig new file mode 100644 index 0000000..166aadc --- /dev/null +++ b/src/zilch/bridge/root.zig @@ -0,0 +1,40 @@ +const std = @import("std"); +const builtin = @import("builtin"); + +const mod = @import("../root.zig"); +const Env = mod.Env; +const Function = Env.Function; + +const wasm = @import("wasm.root.zig"); +const server = @import("server.root.zig"); + +pub fn Bridge(T: type) type { + if (builtin.target.cpu.arch.isWasm()) { + return wasm.Bridge(T); + } else { + return server.Bridge(T); + } +} + +pub const Context = struct { + gpa: std.mem.Allocator, + arena: std.mem.Allocator, + io: std.Io, +}; + +pub const bindings = struct { + pub const call: Function = .from( + \\async function(ptr, len, result_ptr, len_check) { + \\ const response = await fetch("/api", { + \\ method: "POST", + \\ body: new DateView(wasm.memory.buffer, ptr, len), + \\ }) + \\ const bytes = await response.bytes(); + \\ if (bytes.length != len_check) { + \\ throw "response has incorrect size"; + \\ } + \\ const memory = new Uint8Array(wasm.memory.buffer); + \\ memory.set(bytes, result_ptr); + \\} + ); +}; diff --git a/src/zilch/bridge/server.root.zig b/src/zilch/bridge/server.root.zig new file mode 100644 index 0000000..504286c --- /dev/null +++ b/src/zilch/bridge/server.root.zig @@ -0,0 +1,10 @@ +const std = @import("std"); +const http = @import("http"); + +pub fn Bridge(T: type) type { + _ = T; + return struct { + pub fn handle() void { + } + }; +} diff --git a/src/zilch/bridge/wasm.root.zig b/src/zilch/bridge/wasm.root.zig new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/zilch/bridge/wasm.root.zig diff --git a/src/zilch/root.zig b/src/zilch/root.zig index e6112ae..bf0f1ad 100644 --- a/src/zilch/root.zig +++ b/src/zilch/root.zig @@ -10,6 +10,8 @@ pub const Value = Env.Value; pub const log = @import("log/root.zig"); pub const html = @import("html/root.zig"); +pub const bridge = @import("bridge/root.zig"); + pub const Object = @import("Object.zig"); pub const Promise = @import("Promise.zig"); pub const Callable = @import("Callable.zig"); |