Skip to content

Commit

Permalink
Fix enum ids
Browse files Browse the repository at this point in the history
  • Loading branch information
bgk- committed Jul 29, 2024
1 parent 3f06d1f commit e73ca92
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 119 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.14.0",
.version = "0.14.1",
.paths = .{""},
.dependencies = .{},
}
2 changes: 1 addition & 1 deletion src/backend/compiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ pub const Compiler = struct {
}

obj.* = .{
.id = UUID.fromString(e.name),
.id = UUID.fromStringHash(e.name),
.data = .{
.@"enum" = .{
.is_seq = e.is_seq,
Expand Down
10 changes: 4 additions & 6 deletions src/export/runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ pub const ExportRunner = struct {

var i: usize = 0;
while (i < dialogue.tags.len) : (i += 1) {
const tag = dialogue.tags[i];
self.tags[i] = .{ .ptr = tag.ptr, .len = tag.len };
self.tags[i] = .{ .ptr = dialogue.tags[i].ptr, .len = dialogue.tags[i].len };
}

self.dialogue = .{
Expand All @@ -147,7 +146,7 @@ pub const ExportRunner = struct {
.tags = &self.tags,
.tags_length = @intCast(dialogue.tags.len),
};
self.logger.log("Line:{s}: {s}", .{ dialogue.speaker orelse "", dialogue.content }, .debug);
self.logger.log("Line:{s}: {s} #{s}", .{ dialogue.speaker orelse "", dialogue.content, dialogue.tags }, .debug);
self.on_line(@intFromPtr(vm), &self.dialogue);
}

Expand All @@ -163,12 +162,11 @@ pub const ExportRunner = struct {
var t: usize = 0;
const t_start = t_count;
while (t < choices[i].tags.len) : (t += 1) {
const tag = choices[i].tags[t];
self.tags[t_count + t] = .{ .ptr = tag.ptr, .len = tag.len };
self.tags[t_count + t] = .{ .ptr = choices[i].tags[t].ptr, .len = choices[i].tags[t].len };
}
t_count += t;

self.logger.log("Choice: {s}", .{choices[i].content}, .debug);
self.logger.log("Choice: {s} #{s}", .{ choices[i].content, choices[i].tags }, .debug);
const content = choices[i].content;
result[i] = .{
.content = .{ .ptr = content.ptr, .len = content.len },
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/builtins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ pub const Add = struct {
else => unreachable,
}
if (args[0].obj.index) |i| {
vm.notifyValueChange(i, args[0]);
vm.notifyValueChange(i, Void, args[0]);
}
return Void;
}
Expand All @@ -242,7 +242,7 @@ pub const AddMap = struct {
else => unreachable,
}
if (args[0].obj.index) |i| {
vm.notifyValueChange(i, args[0]);
vm.notifyValueChange(i, Void, args[0]);
}
return Void;
}
Expand Down Expand Up @@ -272,7 +272,7 @@ pub const Remove = struct {
else => unreachable,
}
if (args[0].obj.index) |i| {
vm.notifyValueChange(i, args[0]);
vm.notifyValueChange(i, Void, args[0]);
}
return Void;
}
Expand Down Expand Up @@ -331,7 +331,7 @@ pub const Clear = struct {
else => unreachable,
}
if (args[0].obj.index) |i| {
vm.notifyValueChange(i, args[0]);
vm.notifyValueChange(i, Void, args[0]);
}
return Void;
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/runner.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Vm = @import("./vm.zig").Vm;
const Vm = @import("vm.zig").Vm;
const ID = @import("../utils/index.zig").UUID.ID;
const Value = @import("../types/index.zig").Value;

Expand Down
12 changes: 7 additions & 5 deletions src/runtime/state.zig
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,13 @@ pub const State = struct {
const values_items = v.object.get("values").?.array.items;
const vals = try vm.allocator.alloc([]const u8, values_items.len);
for (values_items, 0..) |t, i| vals[i] = try vm.allocator.dupe(u8, t.string);
var result = try vm.gc.create(vm, .{ .@"enum" = .{
.name = v.object.get("name").?.string,
.is_seq = v.object.get("is_seq").?.bool,
.values = vals,
} });
var result = try vm.gc.create(vm, .{
.@"enum" = .{
.name = try vm.allocator.dupe(u8, v.object.get("name").?.string),
.is_seq = v.object.get("is_seq").?.bool,
.values = vals,
},
});
result.obj.id = id.?;
try refs.put(id.?, result);
return result;
Expand Down
224 changes: 124 additions & 100 deletions src/runtime/vm.zig

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/types/value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ pub const Value = union(Type) {
const is_seq = try reader.readByte() == 1;
const values_length = try reader.readByte();
const obj = try allocator.create(Value.Obj);
obj.* = .{ .data = .{ .@"enum" = .{ .name = name_buf, .values = try allocator.alloc([]const u8, values_length), .is_seq = is_seq } } };
obj.* = .{ .id = id, .data = .{ .@"enum" = .{ .name = name_buf, .values = try allocator.alloc([]const u8, values_length), .is_seq = is_seq } } };
for (0..values_length) |i| {
const value_name_length = try reader.readByte();
const value_name_buf = try allocator.alloc(u8, value_name_length);
Expand Down
6 changes: 6 additions & 0 deletions src/utils/uuid.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ pub const UUID = struct {
}
return id;
}

pub fn fromStringHash(str: []const u8) ID {
if (str.len == 0) return Empty;
const hash = std.hash.Wyhash.hash(0, str);
return create(hash);
}
};
9 changes: 9 additions & 0 deletions test/vm.test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,11 @@ test "Save and Load State" {
\\ }
\\ var test = new Test{}
\\ var func = || return "func"
\\ enum Enum = {
\\ One,
\\ Two
\\ }
\\ var enumValue = Enum.One
;
const alloc = testing.allocator;

Expand All @@ -1535,6 +1540,10 @@ test "Save and Load State" {
\\ value += 5
\\ var outer = List{}
\\ var test = "t"
\\ enum Enum = {
\\ One,
\\ Two
\\ }
;

var mod2 = try Module.initEmpty(allocator);
Expand Down

0 comments on commit e73ca92

Please sign in to comment.