aboutsummaryrefslogtreecommitdiff
path: root/src/zilch/Object.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/zilch/Object.zig')
-rw-r--r--src/zilch/Object.zig85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/zilch/Object.zig b/src/zilch/Object.zig
new file mode 100644
index 0000000..1d30b8a
--- /dev/null
+++ b/src/zilch/Object.zig
@@ -0,0 +1,85 @@
+const mod = @import("root.zig");
+const Options = mod.Options;
+const Env = mod.Env;
+const AnyFuture = Env.AnyFuture;
+const Future = Env.Future;
+const Function = Env.Function;
+
+const Self = @This();
+
+pub inline fn get(self: anytype, env: *Env, key: anytype) AnyFuture {
+ return env.call(bindings.get, .{ self, key });
+}
+
+pub inline fn set(self: anytype, env: *Env, key: anytype, value: anytype) Future(void) {
+ return .fromAny(env.call(bindings.set, .{ self, key, value }));
+}
+
+pub inline fn delete(self: anytype, env: *Env, key: anytype) Future(void) {
+ return .fromAny(env.call(bindings.delete, .{ self, key }));
+}
+
+pub inline fn call(self: anytype, env: *Env, method: []const u8, args: anytype) AnyFuture {
+ return env.call(bindings.call, .{ self, method } ++ args);
+}
+
+pub inline fn free(self: anytype, env: *Env) Future(void) {
+ return .fromAny(env.call(bindings.free, .{self}));
+}
+
+pub inline fn new(self: anytype, env: *Env) Future(*Self) {
+ return .fromAny(env.call(bindings.new, .{self}));
+}
+
+pub fn global(tag: @EnumLiteral()) Options.Global {
+ return .{ .name = @tagName(tag), .type = *Self };
+}
+
+pub const bindings = struct {
+ pub const get: Function = .from(
+ \\function(target, key) {
+ \\ return target[key];
+ \\}
+ );
+
+ pub const set: Function = .from(
+ \\function(target, key, value) {
+ \\ target[key] = value;
+ \\}
+ );
+
+ pub const delete: Function = .from(
+ \\function(target, key) {
+ \\ delete target[key];
+ \\}
+ );
+
+ pub const call: Function = .from(
+ \\function(target, method, ...args) {
+ \\ return target[method](...args);
+ \\}
+ );
+
+ pub const new: Function = .from(
+ \\function(target, ...args) {
+ \\ return new target(...args);
+ \\}
+ );
+
+ pub const free: Function = .from(
+ \\function(target) {
+ \\ const id = target[ZsId];
+ \\ delete target[ZsId];
+ \\ delete ObjectStore.store[id];
+ \\}
+ );
+};
+
+pub const functions = [_]Function{
+ bindings.get,
+ bindings.set,
+ bindings.delete,
+ bindings.call,
+ bindings.new,
+ bindings.free,
+};