diff options
Diffstat (limited to 'src/z/Env/root.zig')
| -rw-r--r-- | src/z/Env/root.zig | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/z/Env/root.zig b/src/z/Env/root.zig new file mode 100644 index 0000000..7b31e84 --- /dev/null +++ b/src/z/Env/root.zig @@ -0,0 +1,64 @@ +const std = @import("std"); + +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; + +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, + +pub fn init(gpa: std.mem.Allocator) Self { + return .{ .gpa = gpa }; +} + +pub fn getProp(self: *Self, id: u32, 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, + }, + }, + }); + const result = try self.pool.create(self.gpa); + result.kind = .uninit; + try self.results.append(self.gpa, result); + return .{ .result = 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.results.clearRetainingCapacity(); +} + +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; +} |