aboutsummaryrefslogtreecommitdiff
path: root/src/zilch/Env
diff options
context:
space:
mode:
Diffstat (limited to 'src/zilch/Env')
-rw-r--r--src/zilch/Env/Function.zig15
-rw-r--r--src/zilch/Env/call.zig5
-rw-r--r--src/zilch/Env/future.zig72
-rw-r--r--src/zilch/Env/js.zig22
-rw-r--r--src/zilch/Env/root.zig77
-rw-r--r--src/zilch/Env/value.sync-conflict-20261030-190805-2JYANE7.zig185
-rw-r--r--src/zilch/Env/value.zig183
7 files changed, 559 insertions, 0 deletions
diff --git a/src/zilch/Env/Function.zig b/src/zilch/Env/Function.zig
new file mode 100644
index 0000000..c37b1c7
--- /dev/null
+++ b/src/zilch/Env/Function.zig
@@ -0,0 +1,15 @@
+const std = @import("std");
+
+const Self = @This();
+
+content: []const u8,
+id: u32,
+await: bool,
+
+pub fn from(content: []const u8) Self {
+ return .{
+ .content = content,
+ .id = std.hash.XxHash32.hash(4654, content),
+ .await = std.mem.startsWith(u8, content, "async ") or std.mem.startsWith(u8, content, "async("),
+ };
+}
diff --git a/src/zilch/Env/call.zig b/src/zilch/Env/call.zig
new file mode 100644
index 0000000..1681165
--- /dev/null
+++ b/src/zilch/Env/call.zig
@@ -0,0 +1,5 @@
+
+pub const Call = extern struct {
+ kind: u32,
+ num_args: u8,
+};
diff --git a/src/zilch/Env/future.zig b/src/zilch/Env/future.zig
new file mode 100644
index 0000000..43f46c5
--- /dev/null
+++ b/src/zilch/Env/future.zig
@@ -0,0 +1,72 @@
+const std = @import("std");
+const Env = @import("root.zig");
+const z = @import("../root.zig");
+
+
+pub fn Future(Kind: type) type {
+ return struct {
+ const Self = @This();
+
+ future: AnyFuture,
+
+ pub fn fromAny(future: AnyFuture) Self {
+ return .{ .future = future };
+ }
+
+ pub fn await(self: *Self, env: *Env) !Kind {
+ return self.future.await(env, Kind);
+ }
+
+ pub fn ignore(self: *Self, env: *Env) void {
+ return self.future.ignore(env);
+ }
+ };
+}
+
+pub const AnyFuture = union(enum) {
+ const Self = @This();
+
+ ignored: void,
+ unresolved: struct {
+ id: usize,
+ value: *Env.Value,
+ },
+ resolved: Env.Value,
+ err: anyerror,
+
+ pub fn await(self: *Self, env: *Env, Kind: type) !Kind {
+ const result = try self.awaitAny(env);
+ return result.unwrap(Kind);
+ }
+
+ pub fn awaitAny(self: *Self, env: *Env) !Env.Value {
+ defer self.* = undefined;
+ switch (self.*) {
+ .ignored => {
+ env.flush();
+ return .from({});
+ },
+ .err => |err| return err,
+ .resolved => |result| return result,
+ .unresolved => |result| {
+ const value = result.value;
+ defer env.release(value);
+
+ env.flush();
+
+ self.* = .{ .resolved = value.* };
+
+ return value.*;
+ }
+ }
+ }
+
+ pub fn ignore(self: *Self, env: *Env) void {
+ defer self.* = .ignored;
+
+ switch (self.*) {
+ .unresolved => env.ignore(self),
+ else => {},
+ }
+ }
+};
diff --git a/src/zilch/Env/js.zig b/src/zilch/Env/js.zig
new file mode 100644
index 0000000..a653192
--- /dev/null
+++ b/src/zilch/Env/js.zig
@@ -0,0 +1,22 @@
+const std = @import("std");
+const Env = @import("root.zig");
+
+pub extern "object_store" fn process(
+ [*]const u16,
+ [*]const Env.Value,
+ [*]*Env.Value,
+ length: usize,
+) void;
+
+export fn valueAllocBuffer(value: *Env.Value, len: usize) [*]u8 {
+ const ptr: [*]u8 = @ptrCast(std.heap.wasm_allocator.alloc(u8, len) catch @panic("unable to allocate string"));
+ value.kind = .string;
+ value.value = .{
+ .string = .{
+ .ptr = ptr,
+ .len = len,
+ },
+ };
+
+ return @constCast(value.value.string.ptr);
+}
diff --git a/src/zilch/Env/root.zig b/src/zilch/Env/root.zig
new file mode 100644
index 0000000..d169b5f
--- /dev/null
+++ b/src/zilch/Env/root.zig
@@ -0,0 +1,77 @@
+const std = @import("std");
+
+const js = @import("js.zig");
+const Object = @import("../Object.zig");
+
+pub const Call = @import("call.zig").Call;
+pub const AnyFuture = @import("future.zig").AnyFuture;
+pub const Future = @import("future.zig").Future;
+pub const Value = @import("value.zig").Value;
+pub const Function = @import("Function.zig");
+
+calls: std.ArrayList(Call) = .empty,
+values: std.ArrayList(Value) = .empty,
+pool: std.heap.MemoryPool(Value) = .empty,
+results: std.ArrayList(?*Value) = .empty,
+gpa: std.mem.Allocator,
+
+const Self = @This();
+
+pub const default: Self = .{ .gpa = std.heap.wasm_allocator };
+
+pub fn call(self: *Self, function: Function, args: anytype) AnyFuture {
+ const id = self.calls.items.len;
+
+ self.calls.append(self.gpa, .{
+ .kind = function.id,
+ .num_args = args.len,
+ }) catch |err| return .{ .err = err };
+
+ inline for (args) |arg| {
+ self.values.append(self.gpa, .from(arg)) catch |err| return .{ .err = err };
+ }
+
+ const result = self.pool.create(self.gpa) catch |err| return .{ .err = err };
+ errdefer self.pool.destroy(result);
+ self.results.append(self.gpa, result) catch |err| return .{ .err = err };
+ return .{
+ .unresolved = .{
+ .id = id,
+ .value = result,
+ },
+ };
+}
+
+pub fn flush(self: *Self) void {
+ js.process(
+ @ptrCast(self.calls.items),
+ @ptrCast(self.values.items),
+ @ptrCast(self.results.items),
+ self.results.items.len,
+ );
+ self.calls.clearRetainingCapacity();
+ self.values.clearRetainingCapacity();
+ self.results.clearRetainingCapacity();
+}
+
+pub fn release(self: *Self, value: *Value) void {
+ self.pool.destroy(value);
+}
+
+pub fn ignore(self: *Self, future: *const AnyFuture) void {
+ if (self.results.items.len < future.unresolved.id and self.results.items[future.unresolved.id] == future.unresolved.value) {
+ self.pool.destroy(future.unresolved.value);
+ self.results.items[future.unresolved.id] = null;
+ }
+}
+
+pub fn deinit(self: *Self) void {
+ self.flush();
+
+ self.calls.deinit(self.gpa);
+ self.values.deinit(self.gpa);
+ self.pool.deinit(self.gpa);
+ self.results.deinit(self.gpa);
+
+ self.* = undefined;
+}
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/Env/value.zig b/src/zilch/Env/value.zig
new file mode 100644
index 0000000..ea54ff2
--- /dev/null
+++ b/src/zilch/Env/value.zig
@@ -0,0 +1,183 @@
+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 {
+ @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},
+ ));
+ }
+ },
+ };
+ }
+ };
+};