@pkmn/engine 0.1.0-dev.683fba11 → 0.1.0-dev.6ba42cd5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +30 -4
  3. package/build/pkg/addon/browser.js +4 -1
  4. package/build/pkg/addon/browser.js.map +1 -1
  5. package/build/pkg/addon/node.js +20 -7
  6. package/build/pkg/addon/node.js.map +1 -1
  7. package/build/pkg/addon/wasm.js +23 -14
  8. package/build/pkg/addon/wasm.js.map +1 -1
  9. package/build/pkg/addon.d.ts +5 -5
  10. package/build/pkg/addon.js.map +1 -1
  11. package/build/pkg/gen1.d.ts +2 -1
  12. package/build/pkg/gen1.js +22 -9
  13. package/build/pkg/gen1.js.map +1 -1
  14. package/build/pkg/index.d.ts +2 -0
  15. package/build/pkg/index.js +17 -7
  16. package/build/pkg/index.js.map +1 -1
  17. package/build/tools/debug.js +36 -9
  18. package/build/tools/debug.js.map +1 -1
  19. package/build/tools/display/debug.jsx +18 -7
  20. package/build/tools/display/debug.jsx.map +1 -1
  21. package/build/tools/display/index.js +18 -7
  22. package/build/tools/display/index.js.map +1 -1
  23. package/build/tools/display/ui.jsx +18 -8
  24. package/build/tools/display/ui.jsx.map +1 -1
  25. package/build/tools/display/util.js +16 -1
  26. package/build/tools/display/util.js.map +1 -1
  27. package/build.zig +176 -198
  28. package/package.json +31 -131
  29. package/src/bin/install-pkmn-engine +24 -8
  30. package/src/lib/bindings/c.zig +200 -0
  31. package/src/lib/bindings/node.zig +117 -0
  32. package/src/lib/bindings/wasm.zig +50 -0
  33. package/src/lib/common/array.zig +5 -12
  34. package/src/lib/common/debug.zig +11 -21
  35. package/src/lib/common/js.zig +3 -9
  36. package/src/lib/common/optional.zig +11 -21
  37. package/src/lib/common/protocol.zig +176 -163
  38. package/src/lib/common/rational.zig +275 -284
  39. package/src/lib/common/util.zig +14 -31
  40. package/src/lib/gen1/calc.zig +65 -62
  41. package/src/lib/gen1/chance.zig +17 -32
  42. package/src/lib/gen1/data/types.zig +2 -2
  43. package/src/lib/gen1/data.zig +5 -5
  44. package/src/lib/gen1/helpers.zig +8 -10
  45. package/src/lib/gen1/mechanics.zig +30 -29
  46. package/src/lib/gen2/calc.zig +3 -6
  47. package/src/lib/gen2/chance.zig +12 -18
  48. package/src/lib/gen2/data/types.zig +6 -3
  49. package/src/lib/gen2/data.zig +8 -7
  50. package/src/lib/gen2/helpers.zig +4 -6
  51. package/src/lib/gen2/mechanics.zig +28 -17
  52. package/src/lib/napi.h +2 -0
  53. package/src/lib/node.zig +3 -118
  54. package/src/lib/pkmn.zig +94 -19
  55. package/src/lib/wasm.zig +9 -44
  56. package/src/tools/display/dom.ts +5 -8
@@ -3,19 +3,9 @@ const std = @import("std");
3
3
  const debug = std.debug;
4
4
  const io = std.io;
5
5
 
6
- const Array = if (@hasField(std.builtin.Type, "array")) .array else .Array;
7
- const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
8
- const Optional = if (@hasField(std.builtin.Type, "optional")) .optional else .Optional;
9
- const Pointer = if (@hasField(std.builtin.Type, "pointer")) .pointer else .Pointer;
10
- const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
11
- const Union = if (@hasField(std.builtin.Type, "union")) .@"union" else .Union;
12
-
13
6
  pub fn print(value: anytype) void {
14
- if (@hasDecl(debug, "lockStdErr")) debug.lockStdErr() else debug.getStderrMutex().lock();
15
- defer if (@hasDecl(debug, "unlockStdErr"))
16
- debug.unlockStdErr()
17
- else
18
- debug.getStderrMutex().unlock();
7
+ debug.lockStdErr();
8
+ defer debug.unlockStdErr();
19
9
  const stderr = io.getStdErr().writer();
20
10
 
21
11
  nosuspend {
@@ -29,7 +19,7 @@ pub fn print(value: anytype) void {
29
19
  }) catch return;
30
20
  } else {
31
21
  switch (@typeInfo(@TypeOf(value))) {
32
- Struct => |info| {
22
+ .@"struct" => |info| {
33
23
  if (info.is_tuple) {
34
24
  inline for (info.fields, 0..) |f, i| {
35
25
  inspect(@field(value, f.name));
@@ -52,32 +42,32 @@ fn inspect(value: anytype) void {
52
42
  nosuspend {
53
43
  const err = "Unable to format type '" ++ @typeName(@TypeOf(value)) ++ "'";
54
44
  switch (@typeInfo(@TypeOf(value))) {
55
- Array => |info| {
45
+ .array => |info| {
56
46
  if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
57
47
  @compileError(err);
58
48
  },
59
- Pointer => |ptr_info| switch (ptr_info.size) {
60
- .One => switch (@typeInfo(ptr_info.child)) {
61
- Array => |info| {
49
+ .pointer => |ptr_info| switch (ptr_info.size) {
50
+ .one => switch (@typeInfo(ptr_info.child)) {
51
+ .array => |info| {
62
52
  if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
63
53
  @compileError(err);
64
54
  },
65
- Enum, Union, Struct => return inspect(value.*),
55
+ .@"enum", .@"union", .@"struct" => return inspect(value.*),
66
56
  else => @compileError(err),
67
57
  },
68
- .Many, .C => {
58
+ .many, .c => {
69
59
  if (ptr_info.sentinel) |_| return inspect(std.mem.span(value));
70
60
  if (ptr_info.child == u8) {
71
61
  return stderr.print("{s}", .{std.mem.span(value)}) catch return;
72
62
  }
73
63
  @compileError(err);
74
64
  },
75
- .Slice => {
65
+ .slice => {
76
66
  if (ptr_info.child == u8) return stderr.print("{s}", .{value}) catch return;
77
67
  @compileError(err);
78
68
  },
79
69
  },
80
- Optional => stderr.print("{?}", .{value}) catch return,
70
+ .optional => stderr.print("{?}", .{value}) catch return,
81
71
  else => stderr.print("{}", .{value}) catch return,
82
72
  }
83
73
  }
@@ -1,14 +1,8 @@
1
1
  const std = @import("std");
2
+ const c = @import("napi");
2
3
 
3
4
  const assert = std.debug.assert;
4
5
 
5
- const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
6
-
7
- const c = @cImport({
8
- @cDefine("NAPI_VERSION", "8");
9
- @cInclude("node_api.h");
10
- });
11
-
12
6
  pub const Array = struct {
13
7
  pub fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
14
8
  var result: c.napi_value = undefined;
@@ -37,7 +31,7 @@ pub const Number = struct {
37
31
  const T = @TypeOf(value);
38
32
  var result: c.napi_value = undefined;
39
33
  assert(c.napi_ok == switch (@typeInfo(T)) {
40
- Int => |info| switch (info.bits) {
34
+ .int => |info| switch (info.bits) {
41
35
  0...32 => switch (info.signedness) {
42
36
  .signed => c.napi_create_int32(env, @as(i32, value), &result),
43
37
  .unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
@@ -52,7 +46,7 @@ pub const Number = struct {
52
46
 
53
47
  pub fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
54
48
  switch (@typeInfo(T)) {
55
- Int => |info| switch (info.bits) {
49
+ .int => |info| switch (info.bits) {
56
50
  0...32 => switch (info.signedness) {
57
51
  .signed => {
58
52
  var result: i32 = undefined;
@@ -3,38 +3,28 @@ const std = @import("std");
3
3
  const expect = std.testing.expect;
4
4
  const assert = std.debug.assert;
5
5
 
6
- const Bool = if (@hasField(std.builtin.Type, "bool")) .bool else .Bool;
7
-
8
- // TODO: ziglang/zig#104
6
+ /// Optimized optional representation which stores the empty None value as a sentinel.
7
+ /// NOTE: ziglang/zig#104
9
8
  pub fn Optional(comptime T: type) type {
10
9
  const fields = std.meta.fields(switch (@typeInfo(T)) {
11
- Bool => enum { false, true },
10
+ .bool => enum { false, true },
12
11
  else => T,
13
12
  });
14
13
 
15
- var enumFields: [fields.len + 1]std.builtin.Type.EnumField = undefined;
16
- var decls = [_]std.builtin.Type.Declaration{};
14
+ const tagType = std.math.IntFittingRange(0, fields.len);
15
+ var fieldNames: [fields.len + 1][]const u8 = undefined;
16
+ var fieldValues: [fields.len + 1]tagType = undefined;
17
17
 
18
- enumFields[0] = .{ .name = "None", .value = 0 };
18
+ fieldNames[0] = "None";
19
+ fieldValues[0] = 0;
19
20
 
20
21
  inline for (fields, 1..) |field, i| {
21
22
  assert(!std.mem.eql(u8, field.name, "None"));
22
- enumFields[i] = .{
23
- .name = field.name,
24
- .value = i,
25
- };
23
+ fieldNames[i] = field.name;
24
+ fieldValues[i] = i;
26
25
  }
27
26
 
28
- const options = .{
29
- .tag_type = std.math.IntFittingRange(0, fields.len),
30
- .fields = &enumFields,
31
- .decls = &decls,
32
- .is_exhaustive = true,
33
- };
34
- return @Type(if (@hasField(std.builtin.Type, "enum"))
35
- .{ .@"enum" = options }
36
- else
37
- .{ .Enum = options });
27
+ return @Enum(tagType, .exhaustive, &fieldNames, &fieldValues);
38
28
  }
39
29
 
40
30
  test Optional {