Skip to content

Commit

Permalink
update zig to v0.11.0-dev.3859+88284c124
Browse files Browse the repository at this point in the history
  • Loading branch information
scheibo committed Jun 27, 2023
1 parent e5b5a99 commit 8b83af6
Show file tree
Hide file tree
Showing 19 changed files with 225 additions and 224 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ $ zig build --prefix /usr/local -Doptimize=ReleaseFast

The Zig website has [installation instructions](https://ziglang.org/learn/getting-started/) which
walk through how to install Zig on each platform - the engine code should work on Zig v0.11.0 dev
build 3737 or greater, though tracks Zig's master branch so this may change in the future if
build 3859 or greater, though tracks Zig's master branch so this may change in the future if
breaking language changes are introduced:

`libpkmn` can be built with `-Dshowdown` to instead produce the Pokémon Showdown compatible
Expand Down
2 changes: 1 addition & 1 deletion src/bin/install-pkmn-engine
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const path = require('path');
const {pipeline} = require('stream/promises');
const tty = require('tty');

const ZIG_VERSION = {major: 0, minor: 11, patch: 0, dev: 3737};
const ZIG_VERSION = {major: 0, minor: 11, patch: 0, dev: 3859};
const DEV = ZIG_VERSION.dev ? `-dev.${ZIG_VERSION.dev}` : '';
const VERSION = `${ZIG_VERSION.major}.${ZIG_VERSION.minor}.${ZIG_VERSION.patch}${DEV}`;
const INDEX = 'https://ziglang.org/download/index.json';
Expand Down
23 changes: 8 additions & 15 deletions src/lib/binding/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,29 @@ export const PKMN_LOGS_SIZE = pkmn.LOGS_SIZE;
export fn pkmn_choice_init(choice: u8, data: u8) u8 {
assert(choice <= @typeInfo(pkmn.Choice.Type).Enum.fields.len);
assert(data <= 6);
return @bitCast(u8, pkmn.Choice{
.type = @enumFromInt(pkmn.Choice.Type, choice),
.data = @intCast(u4, data),
});
return @bitCast(pkmn.Choice{ .type = @enumFromInt(choice), .data = @intCast(data) });
}

export fn pkmn_choice_type(choice: u8) u8 {
return @as(u8, @intFromEnum(@bitCast(pkmn.Choice, choice).type));
return @intFromEnum(@as(pkmn.Choice, @bitCast(choice)).type);
}

export fn pkmn_choice_data(choice: u8) u8 {
return @as(u8, @bitCast(pkmn.Choice, choice).data);
return @as(u8, @as(pkmn.Choice, @bitCast(choice)).data);
}

export fn pkmn_result_type(result: u8) u8 {
return @intFromEnum(@bitCast(pkmn.Result, result).type);
return @intFromEnum(@as(pkmn.Result, @bitCast(result)).type);
}

export fn pkmn_result_p1(result: u8) u8 {
assert(!pkmn_error(result));
return @intFromEnum(@bitCast(pkmn.Result, result).p1);
return @intFromEnum(@as(pkmn.Result, @bitCast(result)).p1);
}

export fn pkmn_result_p2(result: u8) u8 {
assert(!pkmn_error(result));
return @intFromEnum(@bitCast(pkmn.Result, result).p2);
return @intFromEnum(@as(pkmn.Result, @bitCast(result)).p2);
}

export fn pkmn_error(result: u8) bool {
Expand Down Expand Up @@ -74,7 +71,7 @@ export fn pkmn_gen1_battle_update(
if (buf) |b| {
var stream = pkmn.protocol.ByteStream{ .buffer = b[0..len] };
var log = pkmn.protocol.FixedLog{ .writer = stream.writer() };
return battle.update(c1, c2, log) catch return @bitCast(pkmn.Result, ERROR);
return battle.update(c1, c2, log) catch return @bitCast(ERROR);
}
}
return battle.update(c1, c2, pkmn.protocol.NULL) catch unreachable;
Expand All @@ -90,9 +87,5 @@ export fn pkmn_gen1_battle_choices(
assert(player <= @typeInfo(pkmn.Player).Enum.fields.len);
assert(request <= @typeInfo(pkmn.Choice.Type).Enum.fields.len);
assert(!pkmn.options.showdown or len > 0);
return battle.choices(
@enumFromInt(pkmn.Player, player),
@enumFromInt(pkmn.Choice.Type, request),
@ptrCast([]pkmn.Choice, out[0..len]),
);
return battle.choices(@enumFromInt(player), @enumFromInt(request), @ptrCast(out[0..len]));
}
37 changes: 17 additions & 20 deletions src/lib/binding/node.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ fn bindings(env: c.napi_env) c.napi_value {
}

fn bind(env: c.napi_env, gen: anytype) c.napi_value {
const choices_size = @intCast(u32, gen.CHOICES_SIZE);
const logs_size = @intCast(u32, gen.LOGS_SIZE);
const choices_size: u32 = @intCast(gen.CHOICES_SIZE);
const logs_size: u32 = @intCast(gen.LOGS_SIZE);
var object = Object.init(env);
const properties = [_]c.napi_property_descriptor{
Property.init("CHOICES_SIZE", .{ .value = Number.init(env, choices_size) }),
Expand All @@ -61,10 +61,9 @@ fn update(gen: anytype) c.napi_callback {
assert(len == @sizeOf(gen.Battle(gen.PRNG)));
assert(data != null);

var aligned = @alignCast(@alignOf(*gen.Battle(gen.PRNG)), data.?);
var battle = @ptrCast(*gen.Battle(gen.PRNG), aligned);
const c1 = @bitCast(pkmn.Choice, Number.get(env, argv[1], u8));
const c2 = @bitCast(pkmn.Choice, Number.get(env, argv[2], u8));
var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
const c1: pkmn.Choice = @bitCast(Number.get(env, argv[1], u8));
const c2: pkmn.Choice = @bitCast(Number.get(env, argv[2], u8));

var vtype: c.napi_valuetype = undefined;
assert(c.napi_typeof(env, argv[3], &vtype) == c.napi_ok);
Expand All @@ -75,14 +74,14 @@ fn update(gen: anytype) c.napi_callback {
assert(len == gen.LOGS_SIZE);
assert(data != null);

var buf = @ptrCast([*]u8, data.?)[0..gen.LOGS_SIZE];
var buf = @as([*]u8, @ptrCast(data.?))[0..gen.LOGS_SIZE];
var stream = pkmn.protocol.ByteStream{ .buffer = buf };
var log = pkmn.protocol.FixedLog{ .writer = stream.writer() };
break :result battle.update(c1, c2, log);
},
} catch unreachable;

return Number.init(env, @bitCast(u8, result));
return Number.init(env, @as(u8, @bitCast(result)));
}
}.call;
}
Expand All @@ -101,19 +100,17 @@ fn choices(gen: anytype) c.napi_callback {
assert(len == @sizeOf(gen.Battle(gen.PRNG)));
assert(data != null);

var aligned = @alignCast(@alignOf(*gen.Battle(gen.PRNG)), data.?);
var battle = @ptrCast(*gen.Battle(gen.PRNG), aligned);

const player = @enumFromInt(pkmn.Player, Number.get(env, argv[1], u8));
const request = @enumFromInt(pkmn.Choice.Type, Number.get(env, argv[2], u8));
var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
const player: pkmn.Player = @enumFromInt(Number.get(env, argv[1], u8));
const request: pkmn.Choice.Type = @enumFromInt(Number.get(env, argv[2], u8));

assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
assert(len == gen.CHOICES_SIZE);
assert(data != null);

var out = @ptrCast([*]pkmn.Choice, data.?)[0..gen.CHOICES_SIZE];
var out = @as([*]pkmn.Choice, @ptrCast(data.?))[0..gen.CHOICES_SIZE];
const n = battle.choices(player, request, out);
return Number.init(env, @bitCast(u8, n));
return Number.init(env, @as(u8, @bitCast(n)));
}
}.call;
}
Expand Down Expand Up @@ -166,25 +163,25 @@ const Number = struct {
.signed => {
var result: i32 = undefined;
assert(c.napi_get_value_int32(env, value, &result) == c.napi_ok);
return if (info.bits == 32) result else @intCast(T, result);
return if (info.bits == 32) result else @intCast(result);
},
.unsigned => {
var result: u32 = undefined;
assert(c.napi_get_value_uint32(env, value, &result) == c.napi_ok);
return if (info.bits == 32) result else @intCast(T, result);
return if (info.bits == 32) result else @intCast(result);
},
},
33...63 => {
var result: i64 = undefined;
assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
return @intCast(T, result);
return @intCast(result);
},
else => {
var result: i64 = undefined;
assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
return switch (info.signedness) {
.signed => @as(T, value),
.unsigned => if (0 <= value) @intCast(T, value) else unreachable,
.signed => value,
.unsigned => if (0 <= value) @intCast(value) else unreachable,
};
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/lib/binding/wasm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export const SHOWDOWN = pkmn.options.showdown;
export const LOG = pkmn.options.log;

export const GEN1_CHOICES_SIZE =
std.math.ceilPowerOfTwo(u32, @intCast(u32, pkmn.gen1.CHOICES_SIZE)) catch unreachable;
std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(pkmn.gen1.CHOICES_SIZE))) catch unreachable;
export const GEN1_LOGS_SIZE =
std.math.ceilPowerOfTwo(u32, @intCast(u32, pkmn.gen1.LOGS_SIZE)) catch unreachable;
std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(pkmn.gen1.LOGS_SIZE))) catch unreachable;
22 changes: 11 additions & 11 deletions src/lib/common/data.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub const Player = enum(u1) {

/// Returns a player's opponent.
pub inline fn foe(self: Player) Player {
return @enumFromInt(Player, ~@intFromEnum(self));
return @enumFromInt(~@intFromEnum(self));
}

/// Return's an identifier for the player's Pokémon at the one-indexed `id`.
Expand All @@ -22,8 +22,8 @@ pub const Player = enum(u1) {

test Player {
try expectEqual(Player.P2, Player.P1.foe());
try expectEqual(@as(u8, 0b0001), @bitCast(u8, Player.P1.ident(1)));
try expectEqual(@as(u8, 0b1101), @bitCast(u8, Player.P2.ident(5)));
try expectEqual(@as(u8, 0b0001), @as(u8, @bitCast(Player.P1.ident(1))));
try expectEqual(@as(u8, 0b1101), @as(u8, @bitCast(Player.P2.ident(5))));
}

/// An identifier for a specific Pokémon in battle.
Expand All @@ -40,18 +40,18 @@ pub const ID = packed struct {

/// Converts the identifier into a number.
pub inline fn int(self: ID) u4 {
return @intCast(u4, @bitCast(u8, self));
return @intCast(@as(u8, @bitCast(self)));
}

/// Decodes the identifier from a number.
pub inline fn from(id: u4) ID {
return @bitCast(ID, @as(u8, id));
return @bitCast(@as(u8, id));
}
};

test ID {
try expectEqual(@as(u8, 0b0001), @bitCast(u8, ID{ .player = .P1, .id = 1 }));
try expectEqual(@as(u8, 0b1101), @bitCast(u8, ID{ .player = .P2, .id = 5 }));
try expectEqual(@as(u8, 0b0001), @as(u8, @bitCast(ID{ .player = .P1, .id = 1 })));
try expectEqual(@as(u8, 0b1101), @as(u8, @bitCast(ID{ .player = .P2, .id = 5 })));
const id = ID{ .player = .P2, .id = 4 };
try expectEqual(id, ID.from(id.int()));
}
Expand Down Expand Up @@ -84,8 +84,8 @@ test Choice {
const p2: Choice = .{ .type = .Switch, .data = 5 };
try expectEqual(5, p2.data);
try expectEqual(Choice.Type.Move, p1.type);
try expectEqual(0b0001_0001, @bitCast(u8, p1));
try expectEqual(0b0001_0110, @bitCast(u8, p2));
try expectEqual(0b0001_0001, @as(u8, @bitCast(p1)));
try expectEqual(0b0001_0110, @as(u8, @bitCast(p2)));
}

/// The result of the battle - all results other than 'None' should be considered terminal.
Expand Down Expand Up @@ -124,6 +124,6 @@ pub const Result = packed struct {
};

test Result {
try expectEqual(0b0101_0000, @bitCast(u8, Result.Default));
try expectEqual(0b1000_0000, @bitCast(u8, Result{ .p2 = .Switch }));
try expectEqual(0b0101_0000, @as(u8, @bitCast(Result.Default)));
try expectEqual(0b1000_0000, @as(u8, @bitCast(Result{ .p2 = .Switch })));
}
2 changes: 1 addition & 1 deletion src/lib/common/data/items.zig.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub const Item = enum({{{ Item.type }}}) {
pub inline fn boost(item: Item) ?Type {
assert(item != .None);
if (item == .PolkadotBow) return .Normal;
return if (@intFromEnum(item) <= {{{ Item.boosts }}}) @enumFromInt(Type, @intFromEnum(item) - 1) else null;
return if (@intFromEnum(item) <= {{{ Item.boosts }}}) @enumFromInt(@intFromEnum(item) - 1) else null;
}

/// Whether or not this item is a Berry.
Expand Down
Loading

0 comments on commit 8b83af6

Please sign in to comment.