Rename ZonElement.free to deinit to follow zig convention.

This commit is contained in:
IntegratedQuantum 2025-01-01 20:27:31 +01:00
parent 1c5229e480
commit de2e4aedb4
10 changed files with 43 additions and 43 deletions

View File

@ -481,7 +481,7 @@ pub const Command = struct { // MARK: Command
}; };
if(data.len > 12) { if(data.len > 12) {
const zon = ZonElement.parseFromString(main.stackAllocator, data[12..]); const zon = ZonElement.parseFromString(main.stackAllocator, data[12..]);
defer zon.free(main.stackAllocator); defer zon.deinit(main.stackAllocator);
self.item = try Item.init(zon); self.item = try Item.init(zon);
} }
if(self.amount > 0) { // Create if(self.amount > 0) { // Create
@ -513,7 +513,7 @@ pub const Command = struct { // MARK: Command
std.mem.writeInt(i32, data.addMany(4)[0..4], self.amount, .big); std.mem.writeInt(i32, data.addMany(4)[0..4], self.amount, .big);
if(self.item) |item| { if(self.item) |item| {
const zon = ZonElement.initObject(main.stackAllocator); const zon = ZonElement.initObject(main.stackAllocator);
defer zon.free(main.stackAllocator); defer zon.deinit(main.stackAllocator);
item.insertIntoZon(main.stackAllocator, zon); item.insertIntoZon(main.stackAllocator, zon);
const string = zon.toStringEfficient(main.stackAllocator, &.{}); const string = zon.toStringEfficient(main.stackAllocator, &.{});
defer main.stackAllocator.free(string); defer main.stackAllocator.free(string);
@ -1104,7 +1104,7 @@ pub const Command = struct { // MARK: Command
std.mem.writeInt(u16, data.addMany(2)[0..2], self.amount, .big); std.mem.writeInt(u16, data.addMany(2)[0..2], self.amount, .big);
if(self.item) |item| { if(self.item) |item| {
const zon = ZonElement.initObject(main.stackAllocator); const zon = ZonElement.initObject(main.stackAllocator);
defer zon.free(main.stackAllocator); defer zon.deinit(main.stackAllocator);
item.insertIntoZon(main.stackAllocator, zon); item.insertIntoZon(main.stackAllocator, zon);
const string = zon.toStringEfficient(main.stackAllocator, &.{}); const string = zon.toStringEfficient(main.stackAllocator, &.{});
defer main.stackAllocator.free(string); defer main.stackAllocator.free(string);
@ -1118,7 +1118,7 @@ pub const Command = struct { // MARK: Command
var item: ?Item = null; var item: ?Item = null;
if(data.len > 10) { if(data.len > 10) {
const zon = ZonElement.parseFromString(main.stackAllocator, data[10..]); const zon = ZonElement.parseFromString(main.stackAllocator, data[10..]);
defer zon.free(main.stackAllocator); defer zon.deinit(main.stackAllocator);
item = try Item.init(zon); item = try Item.init(zon);
} }
return .{ return .{

View File

@ -521,7 +521,7 @@ pub const meshes = struct { // MARK: meshes
const path = buffer[0.._path.len]; const path = buffer[0.._path.len];
const textureInfoPath = extendedPath(path, &buffer, "_textureInfo.zig.zon"); const textureInfoPath = extendedPath(path, &buffer, "_textureInfo.zig.zon");
const textureInfoZon = main.files.readToZon(main.stackAllocator, textureInfoPath) catch .null; const textureInfoZon = main.files.readToZon(main.stackAllocator, textureInfoPath) catch .null;
defer textureInfoZon.free(main.stackAllocator); defer textureInfoZon.deinit(main.stackAllocator);
const animationFrames = textureInfoZon.get(u32, "frames", 1); const animationFrames = textureInfoZon.get(u32, "frames", 1);
const animationTime = textureInfoZon.get(u32, "time", 1); const animationTime = textureInfoZon.get(u32, "time", 1);
animation.append(.{.startFrame = @intCast(blockTextures.items.len), .frames = animationFrames, .time = animationTime}); animation.append(.{.startFrame = @intCast(blockTextures.items.len), .frames = animationFrames, .time = animationTime});

View File

@ -174,7 +174,7 @@ pub fn deinit() void {
pub fn save() void { // MARK: save() pub fn save() void { // MARK: save()
const guiZon = ZonElement.initObject(main.stackAllocator); const guiZon = ZonElement.initObject(main.stackAllocator);
defer guiZon.free(main.stackAllocator); defer guiZon.deinit(main.stackAllocator);
for(windowList.items) |window| { for(windowList.items) |window| {
const windowZon = ZonElement.initObject(main.stackAllocator); const windowZon = ZonElement.initObject(main.stackAllocator);
for(window.relativePosition, 0..) |relPos, i| { for(window.relativePosition, 0..) |relPos, i| {
@ -219,7 +219,7 @@ fn load() void {
} }
break :blk .null; break :blk .null;
}; };
defer zon.free(main.stackAllocator); defer zon.deinit(main.stackAllocator);
for(windowList.items) |window| { for(windowList.items) |window| {
const windowZon = zon.getChild(window.id); const windowZon = zon.getChild(window.id);

View File

@ -43,7 +43,7 @@ fn flawedCreateWorld() !void {
const generatorSettingsPath = std.fmt.allocPrint(main.stackAllocator.allocator, "saves/{s}/generatorSettings.zig.zon", .{worldName}) catch unreachable; const generatorSettingsPath = std.fmt.allocPrint(main.stackAllocator.allocator, "saves/{s}/generatorSettings.zig.zon", .{worldName}) catch unreachable;
defer main.stackAllocator.free(generatorSettingsPath); defer main.stackAllocator.free(generatorSettingsPath);
const generatorSettings = main.ZonElement.initObject(main.stackAllocator); const generatorSettings = main.ZonElement.initObject(main.stackAllocator);
defer generatorSettings.free(main.stackAllocator); defer generatorSettings.deinit(main.stackAllocator);
const climateGenerator = main.ZonElement.initObject(main.stackAllocator); const climateGenerator = main.ZonElement.initObject(main.stackAllocator);
climateGenerator.put("id", "cubyz:noise_based_voronoi"); // TODO: Make this configurable climateGenerator.put("id", "cubyz:noise_based_voronoi"); // TODO: Make this configurable
generatorSettings.put("climateGenerator", climateGenerator); generatorSettings.put("climateGenerator", climateGenerator);

View File

@ -91,7 +91,7 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
} }
} }
self.list.deinit(self.allocator.allocator); self.list.deinit(self.allocator.allocator);
self.lastUpdates.free(self.allocator); self.lastUpdates.deinit(self.allocator);
} }
pub fn loadFrom(self: *ItemDropManager, zon: ZonElement) void { pub fn loadFrom(self: *ItemDropManager, zon: ZonElement) void {
@ -206,7 +206,7 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
const i: u16 = @intCast(self.isEmpty.findFirstSet() orelse { const i: u16 = @intCast(self.isEmpty.findFirstSet() orelse {
self.emptyMutex.unlock(); self.emptyMutex.unlock();
const zon = itemStack.store(main.stackAllocator); const zon = itemStack.store(main.stackAllocator);
defer zon.free(main.stackAllocator); defer zon.deinit(main.stackAllocator);
const string = zon.toString(main.stackAllocator); const string = zon.toString(main.stackAllocator);
defer main.stackAllocator.free(string); defer main.stackAllocator.free(string);
std.log.err("Item drop capacitiy limit reached. Failed to add itemStack: {s}", .{string}); std.log.err("Item drop capacitiy limit reached. Failed to add itemStack: {s}", .{string});

View File

@ -645,7 +645,7 @@ pub const Protocols = struct {
switch(data[0]) { switch(data[0]) {
stepUserData => { stepUserData => {
const zon = ZonElement.parseFromString(main.stackAllocator, data[1..]); const zon = ZonElement.parseFromString(main.stackAllocator, data[1..]);
defer zon.free(main.stackAllocator); defer zon.deinit(main.stackAllocator);
const name = zon.get([]const u8, "name", "unnamed"); const name = zon.get([]const u8, "name", "unnamed");
const version = zon.get([]const u8, "version", "unknown"); const version = zon.get([]const u8, "version", "unknown");
std.log.info("User {s} joined using version {s}.", .{name, version}); std.log.info("User {s} joined using version {s}.", .{name, version});
@ -664,7 +664,7 @@ pub const Protocols = struct {
conn.user.?.initPlayer(name); conn.user.?.initPlayer(name);
const zonObject = ZonElement.initObject(main.stackAllocator); const zonObject = ZonElement.initObject(main.stackAllocator);
defer zonObject.free(main.stackAllocator); defer zonObject.deinit(main.stackAllocator);
zonObject.put("player", conn.user.?.player.save(main.stackAllocator)); zonObject.put("player", conn.user.?.player.save(main.stackAllocator));
zonObject.put("spawn", main.server.world.?.spawn); zonObject.put("spawn", main.server.world.?.spawn);
zonObject.put("blockPalette", main.server.world.?.blockPalette.save(main.stackAllocator)); zonObject.put("blockPalette", main.server.world.?.blockPalette.save(main.stackAllocator));
@ -689,7 +689,7 @@ pub const Protocols = struct {
}, },
stepServerData => { stepServerData => {
const zon = ZonElement.parseFromString(main.stackAllocator, data[1..]); const zon = ZonElement.parseFromString(main.stackAllocator, data[1..]);
defer zon.free(main.stackAllocator); defer zon.deinit(main.stackAllocator);
try conn.manager.world.?.finishHandshake(zon); try conn.manager.world.?.finishHandshake(zon);
conn.handShakeState.store(stepComplete, .monotonic); conn.handShakeState.store(stepComplete, .monotonic);
conn.handShakeWaiting.broadcast(); // Notify the waiting client thread. conn.handShakeWaiting.broadcast(); // Notify the waiting client thread.
@ -712,7 +712,7 @@ pub const Protocols = struct {
pub fn clientSide(conn: *Connection, name: []const u8) void { pub fn clientSide(conn: *Connection, name: []const u8) void {
const zonObject = ZonElement.initObject(main.stackAllocator); const zonObject = ZonElement.initObject(main.stackAllocator);
defer zonObject.free(main.stackAllocator); defer zonObject.deinit(main.stackAllocator);
zonObject.putOwnedString("version", settings.version); zonObject.putOwnedString("version", settings.version);
zonObject.putOwnedString("name", name); zonObject.putOwnedString("name", name);
const prefix = [1]u8 {stepUserData}; const prefix = [1]u8 {stepUserData};
@ -924,7 +924,7 @@ pub const Protocols = struct {
pub const asynchronous = false; pub const asynchronous = false;
fn receive(conn: *Connection, data: []const u8) !void { fn receive(conn: *Connection, data: []const u8) !void {
const zonArray = ZonElement.parseFromString(main.stackAllocator, data); const zonArray = ZonElement.parseFromString(main.stackAllocator, data);
defer zonArray.free(main.stackAllocator); defer zonArray.deinit(main.stackAllocator);
var i: u32 = 0; var i: u32 = 0;
while(i < zonArray.array.items.len) : (i += 1) { while(i < zonArray.array.items.len) : (i += 1) {
const elem = zonArray.array.items[i]; const elem = zonArray.array.items[i];
@ -996,7 +996,7 @@ pub const Protocols = struct {
type_timeAndBiome => { type_timeAndBiome => {
if(conn.manager.world) |world| { if(conn.manager.world) |world| {
const zon = ZonElement.parseFromString(main.stackAllocator, data[1..]); const zon = ZonElement.parseFromString(main.stackAllocator, data[1..]);
defer zon.free(main.stackAllocator); defer zon.deinit(main.stackAllocator);
const expectedTime = zon.get(i64, "time", 0); const expectedTime = zon.get(i64, "time", 0);
var curTime = world.gameTime.load(.monotonic); var curTime = world.gameTime.load(.monotonic);
if(@abs(curTime -% expectedTime) >= 10) { if(@abs(curTime -% expectedTime) >= 10) {
@ -1060,7 +1060,7 @@ pub const Protocols = struct {
pub fn sendTimeAndBiome(conn: *Connection, world: *const main.server.ServerWorld) void { pub fn sendTimeAndBiome(conn: *Connection, world: *const main.server.ServerWorld) void {
const zon = ZonElement.initObject(main.stackAllocator); const zon = ZonElement.initObject(main.stackAllocator);
defer zon.free(main.stackAllocator); defer zon.deinit(main.stackAllocator);
zon.put("time", world.gameTime); zon.put("time", world.gameTime);
const pos = conn.user.?.player.pos; const pos = conn.user.?.player.pos;
zon.put("biome", (world.getBiome(@intFromFloat(pos[0]), @intFromFloat(pos[1]), @intFromFloat(pos[2]))).id); zon.put("biome", (world.getBiome(@intFromFloat(pos[0]), @intFromFloat(pos[1]), @intFromFloat(pos[2]))).id);

View File

@ -289,7 +289,7 @@ pub fn freeUserListAndDecreaseRefCount(allocator: utils.NeverFailingAllocator, l
fn sendEntityUpdates(comptime getInitialList: bool, allocator: utils.NeverFailingAllocator) if(getInitialList) []const u8 else void { fn sendEntityUpdates(comptime getInitialList: bool, allocator: utils.NeverFailingAllocator) if(getInitialList) []const u8 else void {
// Send the entity updates: // Send the entity updates:
const updateList = main.ZonElement.initArray(main.stackAllocator); const updateList = main.ZonElement.initArray(main.stackAllocator);
defer updateList.free(main.stackAllocator); defer updateList.deinit(main.stackAllocator);
defer updateList.array.clearAndFree(); // The children are freed in other locations. defer updateList.array.clearAndFree(); // The children are freed in other locations.
if(world.?.itemDropManager.lastUpdates.array.items.len != 0) { if(world.?.itemDropManager.lastUpdates.array.items.len != 0) {
updateList.array.append(.null); updateList.array.append(.null);
@ -302,18 +302,18 @@ fn sendEntityUpdates(comptime getInitialList: bool, allocator: utils.NeverFailin
defer main.stackAllocator.free(updateData); defer main.stackAllocator.free(updateData);
if(world.?.itemDropManager.lastUpdates.array.items.len != 0) { if(world.?.itemDropManager.lastUpdates.array.items.len != 0) {
const alloc = world.?.itemDropManager.lastUpdates.array.allocator; const alloc = world.?.itemDropManager.lastUpdates.array.allocator;
world.?.itemDropManager.lastUpdates.free(alloc); world.?.itemDropManager.lastUpdates.deinit(alloc);
world.?.itemDropManager.lastUpdates = main.ZonElement.initArray(alloc); world.?.itemDropManager.lastUpdates = main.ZonElement.initArray(alloc);
} }
var initialList: []const u8 = undefined; var initialList: []const u8 = undefined;
if(getInitialList) { if(getInitialList) {
const list = main.ZonElement.initArray(main.stackAllocator); const list = main.ZonElement.initArray(main.stackAllocator);
defer list.free(main.stackAllocator); defer list.deinit(main.stackAllocator);
list.array.append(.null); list.array.append(.null);
const itemDropList = world.?.itemDropManager.getInitialList(main.stackAllocator); const itemDropList = world.?.itemDropManager.getInitialList(main.stackAllocator);
list.array.appendSlice(itemDropList.array.items); list.array.appendSlice(itemDropList.array.items);
itemDropList.array.items.len = 0; itemDropList.array.items.len = 0;
itemDropList.free(main.stackAllocator); itemDropList.deinit(main.stackAllocator);
initialList = list.toStringEfficient(allocator, &.{}); initialList = list.toStringEfficient(allocator, &.{});
} }
const userList = getUserListAndIncreaseRefCount(main.stackAllocator); const userList = getUserListAndIncreaseRefCount(main.stackAllocator);
@ -423,7 +423,7 @@ pub fn removePlayer(user: *User) void { // MARK: removePlayer()
sendMessage(message); sendMessage(message);
// Let the other clients know about that this new one left. // Let the other clients know about that this new one left.
const zonArray = main.ZonElement.initArray(main.stackAllocator); const zonArray = main.ZonElement.initArray(main.stackAllocator);
defer zonArray.free(main.stackAllocator); defer zonArray.deinit(main.stackAllocator);
zonArray.array.append(.{.int = user.id}); zonArray.array.append(.{.int = user.id});
const data = zonArray.toStringEfficient(main.stackAllocator, &.{}); const data = zonArray.toStringEfficient(main.stackAllocator, &.{});
defer main.stackAllocator.free(data); defer main.stackAllocator.free(data);
@ -448,7 +448,7 @@ pub fn connectInternal(user: *User) void {
// Let the other clients know about this new one. // Let the other clients know about this new one.
{ {
const zonArray = main.ZonElement.initArray(main.stackAllocator); const zonArray = main.ZonElement.initArray(main.stackAllocator);
defer zonArray.free(main.stackAllocator); defer zonArray.deinit(main.stackAllocator);
const entityZon = main.ZonElement.initObject(main.stackAllocator); const entityZon = main.ZonElement.initObject(main.stackAllocator);
entityZon.put("id", user.id); entityZon.put("id", user.id);
entityZon.put("name", user.name); entityZon.put("name", user.name);
@ -461,7 +461,7 @@ pub fn connectInternal(user: *User) void {
} }
{ // Let this client know about the others: { // Let this client know about the others:
const zonArray = main.ZonElement.initArray(main.stackAllocator); const zonArray = main.ZonElement.initArray(main.stackAllocator);
defer zonArray.free(main.stackAllocator); defer zonArray.deinit(main.stackAllocator);
for(userList) |other| { for(userList) |other| {
const entityZon = main.ZonElement.initObject(main.stackAllocator); const entityZon = main.ZonElement.initObject(main.stackAllocator);
entityZon.put("id", other.id); entityZon.put("id", other.id);

View File

@ -378,7 +378,7 @@ const WorldIO = struct { // MARK: WorldIO
/// Load the seed, which is needed before custom item and ore generation. /// Load the seed, which is needed before custom item and ore generation.
pub fn loadWorldSeed(self: WorldIO) !u64 { pub fn loadWorldSeed(self: WorldIO) !u64 {
const worldData = try self.dir.readToZon(main.stackAllocator, "world.zig.zon"); const worldData = try self.dir.readToZon(main.stackAllocator, "world.zig.zon");
defer worldData.free(main.stackAllocator); defer worldData.deinit(main.stackAllocator);
if(worldData.get(u32, "version", 0) != worldDataVersion) { if(worldData.get(u32, "version", 0) != worldDataVersion) {
std.log.err("Cannot read world file version {}. Expected version {}.", .{worldData.get(u32, "version", 0), worldDataVersion}); std.log.err("Cannot read world file version {}. Expected version {}.", .{worldData.get(u32, "version", 0), worldDataVersion});
return error.OldWorld; return error.OldWorld;
@ -388,7 +388,7 @@ const WorldIO = struct { // MARK: WorldIO
pub fn loadWorldData(self: WorldIO) !void { pub fn loadWorldData(self: WorldIO) !void {
const worldData = try self.dir.readToZon(main.stackAllocator, "world.zig.zon"); const worldData = try self.dir.readToZon(main.stackAllocator, "world.zig.zon");
defer worldData.free(main.stackAllocator); defer worldData.deinit(main.stackAllocator);
self.world.doGameTimeCycle = worldData.get(bool, "doGameTimeCycle", true); self.world.doGameTimeCycle = worldData.get(bool, "doGameTimeCycle", true);
self.world.gameTime = worldData.get(i64, "gameTime", 0); self.world.gameTime = worldData.get(i64, "gameTime", 0);
@ -398,7 +398,7 @@ const WorldIO = struct { // MARK: WorldIO
pub fn saveWorldData(self: WorldIO) !void { pub fn saveWorldData(self: WorldIO) !void {
const worldData = ZonElement.initObject(main.stackAllocator); const worldData = ZonElement.initObject(main.stackAllocator);
defer worldData.free(main.stackAllocator); defer worldData.deinit(main.stackAllocator);
worldData.put("version", worldDataVersion); worldData.put("version", worldDataVersion);
worldData.put("seed", self.world.seed); worldData.put("seed", self.world.seed);
worldData.put("doGameTimeCycle", self.world.doGameTimeCycle); worldData.put("doGameTimeCycle", self.world.doGameTimeCycle);
@ -757,7 +757,7 @@ pub const ServerWorld = struct { // MARK: ServerWorld
try self.wio.saveWorldData(); try self.wio.saveWorldData();
var buf: [32768]u8 = undefined; var buf: [32768]u8 = undefined;
const zon = files.readToZon(main.stackAllocator, try std.fmt.bufPrint(&buf, "saves/{s}/items.zig.zon", .{self.name})) catch .null; const zon = files.readToZon(main.stackAllocator, try std.fmt.bufPrint(&buf, "saves/{s}/items.zig.zon", .{self.name})) catch .null;
defer zon.free(main.stackAllocator); defer zon.deinit(main.stackAllocator);
self.itemDropManager.loadFrom(zon); self.itemDropManager.loadFrom(zon);
} }
@ -765,7 +765,7 @@ pub const ServerWorld = struct { // MARK: ServerWorld
pub fn findPlayer(self: *ServerWorld, user: *User) void { pub fn findPlayer(self: *ServerWorld, user: *User) void {
var buf: [1024]u8 = undefined; var buf: [1024]u8 = undefined;
const playerData = files.readToZon(main.stackAllocator, std.fmt.bufPrint(&buf, "saves/{s}/player/{s}.zig.zon", .{self.name, user.name}) catch "") catch .null; // TODO: Utils.escapeFolderName(user.name) const playerData = files.readToZon(main.stackAllocator, std.fmt.bufPrint(&buf, "saves/{s}/player/{s}.zig.zon", .{self.name, user.name}) catch "") catch .null; // TODO: Utils.escapeFolderName(user.name)
defer playerData.free(main.stackAllocator); defer playerData.deinit(main.stackAllocator);
const player = &user.player; const player = &user.player;
if(playerData == .null) { if(playerData == .null) {
// Generate a new player: // Generate a new player:
@ -779,7 +779,7 @@ pub const ServerWorld = struct { // MARK: ServerWorld
// TODO: Save chunks and player data // TODO: Save chunks and player data
try self.wio.saveWorldData(); try self.wio.saveWorldData();
const itemDropZon = self.itemDropManager.store(main.stackAllocator); const itemDropZon = self.itemDropManager.store(main.stackAllocator);
defer itemDropZon.free(main.stackAllocator); defer itemDropZon.deinit(main.stackAllocator);
var buf: [32768]u8 = undefined; var buf: [32768]u8 = undefined;
try files.writeZon(try std.fmt.bufPrint(&buf, "saves/{s}/items.zig.zon", .{self.name}), itemDropZon); try files.writeZon(try std.fmt.bufPrint(&buf, "saves/{s}/items.zig.zon", .{self.name}), itemDropZon);
} }

View File

@ -70,7 +70,7 @@ pub fn init() void {
} }
break :blk .null; break :blk .null;
}; };
defer zon.free(main.stackAllocator); defer zon.deinit(main.stackAllocator);
inline for(@typeInfo(@This()).@"struct".decls) |decl| { inline for(@typeInfo(@This()).@"struct".decls) |decl| {
const is_const = @typeInfo(@TypeOf(&@field(@This(), decl.name))).pointer.is_const; // Sadly there is no direct way to check if a declaration is const. const is_const = @typeInfo(@TypeOf(&@field(@This(), decl.name))).pointer.is_const; // Sadly there is no direct way to check if a declaration is const.
@ -124,7 +124,7 @@ pub fn deinit() void {
pub fn save() void { pub fn save() void {
const zonObject = ZonElement.initObject(main.stackAllocator); const zonObject = ZonElement.initObject(main.stackAllocator);
defer zonObject.free(main.stackAllocator); defer zonObject.deinit(main.stackAllocator);
inline for(@typeInfo(@This()).@"struct".decls) |decl| { inline for(@typeInfo(@This()).@"struct".decls) |decl| {
const is_const = @typeInfo(@TypeOf(&@field(@This(), decl.name))).pointer.is_const; // Sadly there is no direct way to check if a declaration is const. const is_const = @typeInfo(@TypeOf(&@field(@This(), decl.name))).pointer.is_const; // Sadly there is no direct way to check if a declaration is const.

View File

@ -205,7 +205,7 @@ pub const ZonElement = union(enum) { // MARK: Zon
} }
} }
pub fn free(self: *const ZonElement, allocator: NeverFailingAllocator) void { pub fn deinit(self: *const ZonElement, allocator: NeverFailingAllocator) void {
switch(self.*) { switch(self.*) {
.int, .float, .bool, .null, .string => return, .int, .float, .bool, .null, .string => return,
.stringOwned => { .stringOwned => {
@ -213,7 +213,7 @@ pub const ZonElement = union(enum) { // MARK: Zon
}, },
.array => { .array => {
for(self.array.items) |*elem| { for(self.array.items) |*elem| {
elem.free(allocator); elem.deinit(allocator);
} }
self.array.clearAndFree(); self.array.clearAndFree();
allocator.destroy(self.array); allocator.destroy(self.array);
@ -223,7 +223,7 @@ pub const ZonElement = union(enum) { // MARK: Zon
while(true) { while(true) {
const elem = iterator.next() orelse break; const elem = iterator.next() orelse break;
allocator.free(elem.key_ptr.*); allocator.free(elem.key_ptr.*);
elem.value_ptr.free(allocator); elem.value_ptr.deinit(allocator);
} }
self.object.clearAndFree(); self.object.clearAndFree();
allocator.destroy(self.object); allocator.destroy(self.object);
@ -563,7 +563,7 @@ const Parser = struct { // MARK: Parser
if(map.fetchPut(key, value) catch unreachable) |old| { if(map.fetchPut(key, value) catch unreachable) |old| {
printError(chars, index.*, "Duplicate key."); printError(chars, index.*, "Duplicate key.");
allocator.free(old.key); allocator.free(old.key);
old.value.free(allocator); old.value.deinit(allocator);
} }
skipWhitespaces(chars, index); skipWhitespaces(chars, index);
if(index.* < chars.len and chars[index.*] == ',') { if(index.* < chars.len and chars[index.*] == ',') {
@ -796,23 +796,23 @@ test "element parsing" {
index = 0; index = 0;
var result: ZonElement = Parser.parseElement(allocator, "\"abcd\\\"\\\\ħσ→ ↑Φ∫€ ⌬ ε→Π\"", &index); var result: ZonElement = Parser.parseElement(allocator, "\"abcd\\\"\\\\ħσ→ ↑Φ∫€ ⌬ ε→Π\"", &index);
try std.testing.expectEqualStrings("abcd\"\\ħσ→ ↑Φ∫€ ⌬ ε→Π", result.as([]const u8, "")); try std.testing.expectEqualStrings("abcd\"\\ħσ→ ↑Φ∫€ ⌬ ε→Π", result.as([]const u8, ""));
result.free(allocator); result.deinit(allocator);
index = 0; index = 0;
result = Parser.parseElement(allocator, "\"12345", &index); result = Parser.parseElement(allocator, "\"12345", &index);
try std.testing.expectEqualStrings("12345", result.as([]const u8, "")); try std.testing.expectEqualStrings("12345", result.as([]const u8, ""));
result.free(allocator); result.deinit(allocator);
// Object: // Object:
index = 0; index = 0;
result = Parser.parseElement(allocator, ".{.name = 1}", &index); result = Parser.parseElement(allocator, ".{.name = 1}", &index);
try std.testing.expectEqual(.object, std.meta.activeTag(result)); try std.testing.expectEqual(.object, std.meta.activeTag(result));
try std.testing.expectEqual(result.object.get("name"), ZonElement{.int = 1}); try std.testing.expectEqual(result.object.get("name"), ZonElement{.int = 1});
result.free(allocator); result.deinit(allocator);
index = 0; index = 0;
result = Parser.parseElement(allocator, ".{@\"object\"=.{},}", &index); result = Parser.parseElement(allocator, ".{@\"object\"=.{},}", &index);
try std.testing.expectEqual(.object, std.meta.activeTag(result)); try std.testing.expectEqual(.object, std.meta.activeTag(result));
try std.testing.expectEqual(.array, std.meta.activeTag(result.object.get("object") orelse .null)); try std.testing.expectEqual(.array, std.meta.activeTag(result.object.get("object") orelse .null));
result.free(allocator); result.deinit(allocator);
index = 0; index = 0;
result = Parser.parseElement(allocator, ".{ .object1 = \"\" \n, .object2 =\t.{\n},.object3 =1.0e4\t,@\"\nobject1\"=.{},@\"\tobject1θ\"=.{},}", &index); result = Parser.parseElement(allocator, ".{ .object1 = \"\" \n, .object2 =\t.{\n},.object3 =1.0e4\t,@\"\nobject1\"=.{},@\"\tobject1θ\"=.{},}", &index);
try std.testing.expectEqual(.object, std.meta.activeTag(result)); try std.testing.expectEqual(.object, std.meta.activeTag(result));
@ -820,7 +820,7 @@ test "element parsing" {
try std.testing.expectEqual(.stringOwned, std.meta.activeTag(result.object.get("object1") orelse .null)); try std.testing.expectEqual(.stringOwned, std.meta.activeTag(result.object.get("object1") orelse .null));
try std.testing.expectEqual(.array, std.meta.activeTag(result.object.get("\nobject1") orelse .null)); try std.testing.expectEqual(.array, std.meta.activeTag(result.object.get("\nobject1") orelse .null));
try std.testing.expectEqual(.array, std.meta.activeTag(result.object.get("\tobject1θ") orelse .null)); try std.testing.expectEqual(.array, std.meta.activeTag(result.object.get("\tobject1θ") orelse .null));
result.free(allocator); result.deinit(allocator);
//Array: //Array:
index = 0; index = 0;
@ -828,12 +828,12 @@ test "element parsing" {
try std.testing.expectEqual(.array, std.meta.activeTag(result)); try std.testing.expectEqual(.array, std.meta.activeTag(result));
try std.testing.expectEqual(.stringOwned, std.meta.activeTag(result.array.items[0])); try std.testing.expectEqual(.stringOwned, std.meta.activeTag(result.array.items[0]));
try std.testing.expectEqual(ZonElement{.int=1}, result.array.items[1]); try std.testing.expectEqual(ZonElement{.int=1}, result.array.items[1]);
result.free(allocator); result.deinit(allocator);
index = 0; index = 0;
result = Parser.parseElement(allocator, ".{ \"name\"\t1\n, 17.1}", &index); result = Parser.parseElement(allocator, ".{ \"name\"\t1\n, 17.1}", &index);
try std.testing.expectEqual(.array, std.meta.activeTag(result)); try std.testing.expectEqual(.array, std.meta.activeTag(result));
try std.testing.expectEqual(.stringOwned, std.meta.activeTag(result.array.items[0])); try std.testing.expectEqual(.stringOwned, std.meta.activeTag(result.array.items[0]));
try std.testing.expectEqual(ZonElement{.int=1}, result.array.items[1]); try std.testing.expectEqual(ZonElement{.int=1}, result.array.items[1]);
try std.testing.expectEqual(ZonElement{.float=17.1}, result.array.items[2]); try std.testing.expectEqual(ZonElement{.float=17.1}, result.array.items[2]);
result.free(allocator); result.deinit(allocator);
} }