Skip to content

Commit

Permalink
Refactor subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
bgk- committed May 28, 2024
1 parent 24269b0 commit 2baa216
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 153 deletions.
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.{
.name = "topiary",
.version = "0.12.2",
.version = "0.13.0",
.paths = .{""},
.dependencies = .{},
}
9 changes: 7 additions & 2 deletions src/compiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -723,10 +723,14 @@ pub const Compiler = struct {
const token = expr.token;
switch (expr.type) {
.binary => |bin| {
if (bin.operator == .less_than) {
if (bin.operator == .less_than or bin.operator == .less_than_equal) {
try self.compileExpression(bin.right);
try self.compileExpression(bin.left);
try self.writeOp(.greater_than, token);
try self.writeOp(switch (bin.operator) {
.less_than => .greater_than,
.less_than_equal => .greater_than_equal,
else => unreachable,
}, token);
return;
}
if (bin.operator == .assign) {
Expand Down Expand Up @@ -766,6 +770,7 @@ pub const Compiler = struct {
.equal => .equal,
.not_equal => .not_equal,
.greater_than => .greater_than,
.greater_than_equal => .greater_than_equal,
.@"or" => .@"or",
.@"and" => .@"and",
else => {
Expand Down
89 changes: 25 additions & 64 deletions src/export.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const Compiler = @import("compiler.zig").Compiler;
const compileSource = @import("compiler.test.zig").compileSource;
const Bytecode = @import("bytecode.zig").Bytecode;
const runners = @import("runner.zig");
const Subscriber = @import("subscriber.zig").Subscriber;
const Module = @import("module.zig").Module;
const File = @import("module.zig").File;
const Runner = runners.Runner;
Expand All @@ -20,6 +19,7 @@ const State = @import("./state.zig").State;
var alloc = std.heap.page_allocator;
var debug_log: ?OnExportDebugLog = null;
var debug_severity: Severity = .err;
var value_changed_callback: ?OnExportValueChanged = null;

const ExportLine = extern struct {
content: [*c]const u8,
Expand All @@ -39,7 +39,7 @@ const ExportChoice = extern struct {
ip: u32,
};

const OnExportValueChanged = *const fn (value: ExportValue) void;
const OnExportValueChanged = *const fn (name: [*c]const u8, value: ExportValue) void;
const OnExportLine = *const fn (vm_ptr: usize, dialogue: *ExportLine) void;
const OnExportChoices = *const fn (vm_ptr: usize, choices: [*]ExportChoice, choices_len: u8) void;

Expand Down Expand Up @@ -283,27 +283,6 @@ export fn destroyValue(value: *ExportValue) void {
value.deinit(alloc);
}

const ExportCallback = struct {
callback: OnExportValueChanged,

pub fn init(callback: OnExportValueChanged) ExportCallback {
return .{
.callback = callback,
};
}

pub fn onValueChanged(context_ptr: usize, value: Value) void {
var self: *ExportCallback = @ptrFromInt(context_ptr);
const ext = ExportValue.fromValue(value, alloc);
self.callback(ext);
}

pub fn onUnsubscribe(context_ptr: usize, allocator: std.mem.Allocator) void {
const self: *ExportCallback = @ptrFromInt(context_ptr);
allocator.destroy(self);
}
};

pub const ExportFunction = struct {
func: Delegate,

Expand Down Expand Up @@ -332,43 +311,33 @@ pub const ExportFunction = struct {
}
};

export fn subscribe(vm_ptr: usize, name_ptr: [*c]const u8, name_length: usize, callback_ptr: usize) void {
fn exportValueChangedCallback(name: []const u8, value: Value) void {
log("ValueChangedCallback: {s}", .{name}, .debug);
if (value_changed_callback) |cb| {
cb(name.ptr, ExportValue.fromValue(value, alloc));
}
}

export fn setSubscriberCallback(vm_ptr: usize, callback_ptr: usize) void {
value_changed_callback = @ptrFromInt(callback_ptr);
var vm: *Vm = @ptrFromInt(vm_ptr);
vm.value_subscriber_callback = &exportValueChangedCallback;
}

export fn subscribe(vm_ptr: usize, name_ptr: [*c]const u8, name_length: usize) bool {
var vm: *Vm = @ptrFromInt(vm_ptr);

const name = name_ptr[0..name_length];
const extern_callback = vm.allocator.create(ExportCallback) catch {
log("Could not allocate ExportCallback", .{}, .err);
return;
};
extern_callback.* = ExportCallback.init(@ptrFromInt(callback_ptr));

vm.subscribeDelegate(
name,
.{
.onUnsubscribe = &ExportCallback.onUnsubscribe,
.context_ptr = @intFromPtr(extern_callback),
.callback = &ExportCallback.onValueChanged,
},
) catch |err| {
return vm.subscribeToValueChange(name) catch |err| {
log("Could not subscribe to variable '{s}': {s}", .{ name, @errorName(err) }, .warn);
return;
return false;
};
}

export fn unsubscribe(vm_ptr: usize, name_ptr: [*c]const u8, name_length: usize, callback_ptr: usize) void {
export fn unsubscribe(vm_ptr: usize, name_ptr: [*c]const u8, name_length: usize) bool {
var vm: *Vm = @ptrFromInt(vm_ptr);

const name = name_ptr[0..name_length];
var callback: OnExportValueChanged = @ptrFromInt(callback_ptr);
const extern_callback: *ExportCallback = @fieldParentPtr("callback", &callback);

vm.unsubscribeDelegate(name, .{
.context_ptr = @intFromPtr(extern_callback),
.callback = &ExportCallback.onValueChanged,
}) catch |err| {
log("Could not unsubscribe from variable '{s}': {s}", .{ name, @errorName(err) }, .warn);
return;
};
return vm.unusbscribeToValueChange(name);
}

export fn createVm(source_ptr: [*c]const u8, source_len: usize, on_dialogue_ptr: usize, on_choice_ptr: usize) usize {
Expand Down Expand Up @@ -526,7 +495,8 @@ const TestRunner = struct {
}
};

fn testSubscriber(value: ExportValue) void {
fn testSubscriber(name: [*c]const u8, value: ExportValue) void {
std.log.warn("testSubscribe: {s}", .{name});
std.testing.expectEqualSlices(u8, "321 test", value.data.string[0..8]) catch {};
}

Expand Down Expand Up @@ -579,16 +549,12 @@ test "Create and Destroy Vm" {

const vm_ptr = createVm(buf.ptr, buf.len, @intFromPtr(on_dialogue), @intFromPtr(on_choices));
const vm: *Vm = @ptrFromInt(vm_ptr);
setSubscriberCallback(vm_ptr, @intFromPtr(&testSubscriber));

defer destroyVm(vm_ptr);
defer vm.bytecode.free(alloc);
const val_name = "value";
subscribe(
vm_ptr,
val_name,
val_name.len,
@intFromPtr(&testSubscriber),
);
subscribe(vm_ptr, val_name, val_name.len);
start(vm_ptr, "", 0);
while (canContinue(vm_ptr)) {
run(vm_ptr);
Expand All @@ -597,12 +563,7 @@ test "Create and Destroy Vm" {
break;
}
}
unsubscribe(
vm_ptr,
val_name,
val_name.len,
@intFromPtr(&testSubscriber),
);
unsubscribe(vm_ptr, val_name, val_name.len);

const list_name = "list";
var list_value: ExportValue = undefined;
Expand Down
2 changes: 2 additions & 0 deletions src/opcode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub const OpCode = enum(u8) {
equal,
not_equal,
greater_than,
greater_than_equal,
jump,
jump_if_false,
prong,
Expand Down Expand Up @@ -74,6 +75,7 @@ pub const OpCode = enum(u8) {
.equal => "OP_EQUAL",
.not_equal => "OP_NOT_EQUAL",
.greater_than => "OP_GREATER_THAN",
.greater_than_equal => "OP_GREATER_THAN_EQUAL",
.jump => "OP_JUMP",
.jump_if_false => "OP_JUMP_IF_FALSE",
.prong => "OP_PRONG",
Expand Down
61 changes: 0 additions & 61 deletions src/subscriber.zig

This file was deleted.

1 change: 1 addition & 0 deletions src/values.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub const False = Value{ .bool = false };
pub const Nil = Value{ .nil = {} };
pub const Void = Value{ .void = {} };

pub const OnValueChanged = *const fn ([]const u8, value: Value) void;
pub const Type = enum(u8) {
void,
nil,
Expand Down
8 changes: 5 additions & 3 deletions src/vm.test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1443,13 +1443,15 @@ test "Externs and Subscribers" {
defer vm.deinit();
defer vm.bytecode.free(testing.allocator);
const Listener = struct {
pub fn onChange(_: usize, value: Value) void {
std.debug.print("\nListener::{}\n", .{value});
pub fn onChange(name: []const u8, value: Value) void {
std.debug.print("\nListener \"{s}\"::{}\n", .{ name, value });
}
};
try vm.setExtern("value", .{ .number = 2 });
try vm.subscribeCallback("value", Listener.onChange);
try vm.subscribeToValueChange("value");
vm.value_subscriber_callback = &Listener.onChange;
try vm.interpret();
vm.unusbscribeToValueChange("value");
try testing.expect(case.value == vm.stack.previous().number);
}
}
Expand Down
Loading

0 comments on commit 2baa216

Please sign in to comment.