@pkmn/engine 0.1.0-dev.c5fe6d07 → 0.1.0-dev.ce4c0b2a

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 (76) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +46 -19
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +43 -0
  5. package/build/pkg/addon/browser.js.map +1 -0
  6. package/build/pkg/addon/node.d.ts +3 -0
  7. package/build/pkg/addon/node.js +96 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon.d.ts +2 -0
  10. package/build/pkg/addon.js +35 -58
  11. package/build/pkg/addon.js.map +1 -1
  12. package/build/pkg/data/ids.json +12 -12
  13. package/build/pkg/data/layout.json +2 -2
  14. package/build/pkg/data/protocol.json +69 -12
  15. package/build/pkg/data.d.ts +27 -5
  16. package/build/pkg/data.js.map +1 -1
  17. package/build/pkg/gen1.js +3 -3
  18. package/build/pkg/gen1.js.map +1 -1
  19. package/build/pkg/index.d.ts +24 -10
  20. package/build/pkg/index.js +15 -10
  21. package/build/pkg/index.js.map +1 -1
  22. package/build/pkg/protocol.d.ts +20 -1
  23. package/build/pkg/protocol.js +123 -102
  24. package/build/pkg/protocol.js.map +1 -1
  25. package/build/tools/debug.js +36 -68
  26. package/build/tools/debug.js.map +1 -1
  27. package/build/tools/display/debug.jsx +128 -0
  28. package/build/tools/display/debug.jsx.map +1 -0
  29. package/build/tools/display/index.js +132 -0
  30. package/build/tools/display/index.js.map +1 -0
  31. package/build/tools/display/ui.jsx +349 -0
  32. package/build/tools/display/ui.jsx.map +1 -0
  33. package/build/tools/display/util.js +18 -0
  34. package/build/tools/display/util.js.map +1 -0
  35. package/build.zig +230 -99
  36. package/package.json +63 -30
  37. package/src/bin/install-pkmn-engine +29 -19
  38. package/src/bin/pkmn-debug +1 -1
  39. package/src/lib/common/array.zig +77 -0
  40. package/src/lib/common/data.zig +12 -24
  41. package/src/lib/common/debug.zig +11 -6
  42. package/src/lib/common/js.zig +4 -2
  43. package/src/lib/common/optional.zig +21 -10
  44. package/src/lib/common/options.zig +4 -0
  45. package/src/lib/common/protocol.zig +807 -327
  46. package/src/lib/common/rational.zig +125 -31
  47. package/src/lib/common/rng.zig +22 -13
  48. package/src/lib/common/util.zig +50 -1
  49. package/src/lib/gen1/calc.zig +660 -137
  50. package/src/lib/gen1/chance.zig +409 -284
  51. package/src/lib/gen1/data/moves.zig +192 -180
  52. package/src/lib/gen1/data/species.zig +3 -3
  53. package/src/lib/gen1/data/types.zig +7 -7
  54. package/src/lib/gen1/data.zig +48 -59
  55. package/src/lib/gen1/helpers.zig +94 -552
  56. package/src/lib/gen1/mechanics.zig +580 -518
  57. package/src/lib/gen2/calc.zig +161 -0
  58. package/src/lib/gen2/chance.zig +902 -0
  59. package/src/lib/gen2/data/items.zig +12 -18
  60. package/src/lib/gen2/data/moves.zig +663 -569
  61. package/src/lib/gen2/data/species.zig +1 -1
  62. package/src/lib/gen2/data/types.zig +134 -31
  63. package/src/lib/gen2/data.zig +107 -79
  64. package/src/lib/gen2/generated.zig +806 -0
  65. package/src/lib/gen2/helpers.zig +49 -25
  66. package/src/lib/gen2/mechanics.zig +3560 -12
  67. package/src/lib/{binding/node.zig → node.zig} +10 -13
  68. package/src/lib/pkmn.zig +32 -15
  69. package/src/lib/{binding/wasm.zig → wasm.zig} +2 -1
  70. package/src/tools/display/dom.ts +96 -0
  71. package/src/tools/display/ui.css +351 -0
  72. package/build/test/display.js +0 -400
  73. package/build/test/display.js.map +0 -1
  74. package/src/lib/gen1/README.md +0 -892
  75. package/src/lib/gen2/README.md +0 -308
  76. package/src/test/display/pkmn.html.tmpl +0 -368
package/package.json CHANGED
@@ -1,19 +1,28 @@
1
1
  {
2
2
  "name": "@pkmn/engine",
3
- "version": "0.1.0-dev.c5fe6d07",
3
+ "version": "0.1.0-dev.ce4c0b2a",
4
4
  "description": "A minimal, complete, Pokémon battle simulation engine optimized for performance",
5
5
  "repository": "github:pkmn/engine",
6
6
  "license": "MIT",
7
7
  "sideEffects": false,
8
8
  "main": "build/pkg/index.js",
9
9
  "types": "build/pkg/index.d.ts",
10
+ "browser": {
11
+ "./build/pkg/addon/node.js": "./build/pkg/addon/browser.js"
12
+ },
10
13
  "files": [
11
14
  "build.zig",
12
- "build/pkg/data.d.ts",
13
- "build/pkg/data.js.map",
14
15
  "build/pkg/addon.d.ts",
15
16
  "build/pkg/addon.js.map",
16
17
  "build/pkg/addon.js",
18
+ "build/pkg/addon/browser.d.ts",
19
+ "build/pkg/addon/browser.js.map",
20
+ "build/pkg/addon/browser.js",
21
+ "build/pkg/addon/node.d.ts",
22
+ "build/pkg/addon/node.js.map",
23
+ "build/pkg/addon/node.js",
24
+ "build/pkg/data.d.ts",
25
+ "build/pkg/data.js.map",
17
26
  "build/pkg/data.js",
18
27
  "build/pkg/data/ids.json",
19
28
  "build/pkg/data/layout.json",
@@ -27,13 +36,17 @@
27
36
  "build/pkg/protocol.d.ts",
28
37
  "build/pkg/protocol.js.map",
29
38
  "build/pkg/protocol.js",
30
- "build/test/display.js.map",
31
- "build/test/display.js",
32
39
  "build/tools/debug.js.map",
33
40
  "build/tools/debug.js",
34
- "src/bin/install-pkmn-engine",
35
- "src/lib/binding/node.zig",
36
- "src/lib/binding/wasm.zig",
41
+ "build/tools/display/index.js.map",
42
+ "build/tools/display/index.js",
43
+ "build/tools/display/debug.jsx.map",
44
+ "build/tools/display/debug.jsx",
45
+ "build/tools/display/ui.jsx.map",
46
+ "build/tools/display/ui.jsx",
47
+ "build/tools/display/util.js.map",
48
+ "build/tools/display/util.js",
49
+ "src/lib/common/array.zig",
37
50
  "src/lib/common/battle.zig",
38
51
  "src/lib/common/data.zig",
39
52
  "src/lib/common/debug.zig",
@@ -52,15 +65,21 @@
52
65
  "src/lib/gen1/data/types.zig",
53
66
  "src/lib/gen1/helpers.zig",
54
67
  "src/lib/gen1/mechanics.zig",
68
+ "src/lib/gen2/calc.zig",
69
+ "src/lib/gen2/chance.zig",
55
70
  "src/lib/gen2/data.zig",
56
71
  "src/lib/gen2/data/items.zig",
57
72
  "src/lib/gen2/data/moves.zig",
58
73
  "src/lib/gen2/data/species.zig",
59
74
  "src/lib/gen2/data/types.zig",
75
+ "src/lib/gen2/generated.zig",
60
76
  "src/lib/gen2/helpers.zig",
61
77
  "src/lib/gen2/mechanics.zig",
78
+ "src/lib/node.zig",
62
79
  "src/lib/pkmn.zig",
63
- "src/test/display/pkmn.html.tmpl"
80
+ "src/lib/wasm.zig",
81
+ "src/tools/display/dom.ts",
82
+ "src/tools/display/ui.css"
64
83
  ],
65
84
  "engines": {
66
85
  "node": ">=16.0.0"
@@ -73,30 +92,35 @@
73
92
  "access": "public"
74
93
  },
75
94
  "dependencies": {
76
- "@pkmn/data": "^0.7.46",
77
- "@pkmn/protocol": "^0.6.12"
95
+ "@pkmn/data": "^0.9.13",
96
+ "@pkmn/protocol": "^0.6.26"
78
97
  },
79
98
  "devDependencies": {
80
- "@pkmn/eslint-config": "^5.0.0",
81
- "@pkmn/img": "^0.2.13",
82
- "@pkmn/sets": "^5.0.4",
83
- "@pkmn/sim": "0.7.46",
84
- "@types/html-minifier": "^4.0.2",
85
- "@types/jest": "^29.5.3",
86
- "@types/minimist": "^1.2.2",
87
- "@types/mustache": "^4.2.2",
88
- "@types/node": "^20.4.2",
89
- "@vitest/coverage-c8": "^0.33.0",
90
- "binaryen": "^114.0.0",
91
- "eslint": "^8.44.0",
99
+ "@pkmn/eslint-config": "^8.3.0",
100
+ "@pkmn/img": "^0.2.32",
101
+ "@pkmn/sets": "^5.1.2",
102
+ "@pkmn/sim": "0.9.13",
103
+ "@types/html-minifier": "^4.0.5",
104
+ "@types/jest": "^29.5.12",
105
+ "@types/minimist": "^1.2.5",
106
+ "@types/mustache": "^4.2.5",
107
+ "@types/node": "^22.5.3",
108
+ "@types/react": "^18.2.45",
109
+ "@types/semver": "^7.5.8",
110
+ "@vitest/coverage-v8": "^2.0.5",
111
+ "binaryen": "^118.0.0",
112
+ "esbuild": "^0.23.1",
113
+ "eslint": "<=8",
92
114
  "html-minifier": "^4.0.0",
93
- "json-stringify-pretty-compact": "3.0.0",
115
+ "json-stringify-pretty-compact": "4.0.0",
94
116
  "minimist": "^1.2.8",
95
117
  "mustache": "^4.2.0",
118
+ "semver": "^7.6.3",
96
119
  "source-map-support": "^0.5.21",
120
+ "table": "^6.8.2",
97
121
  "trakr": "^0.2.0",
98
- "typescript": "^5.1.6",
99
- "vitest": "^0.33.0"
122
+ "typescript": "^5.5.4",
123
+ "vitest": "^2.0.5"
100
124
  },
101
125
  "scripts": {
102
126
  "lint": "eslint --cache src --ext ts,js && eslint src/bin/*",
@@ -106,6 +130,7 @@
106
130
  "test": "vitest src/pkg",
107
131
  "test:integration": "vitest src/test",
108
132
  "debug": "npm run compile && node build/tools/debug",
133
+ "demo": "npm run compile && node build/tools/demo",
109
134
  "fuzz": "npm run compile && node build/test/fuzz",
110
135
  "generate": "npm run compile && node build/tools/generate",
111
136
  "release": "npm run compile && node build/tools/release",
@@ -118,7 +143,7 @@
118
143
  "ignorePatterns": [
119
144
  "dist/",
120
145
  "node_modules/",
121
- "src/examples/zig",
146
+ "examples/zig",
122
147
  "build/"
123
148
  ],
124
149
  "env": {
@@ -134,14 +159,13 @@
134
159
  "parserOptions": {
135
160
  "project": [
136
161
  "./tsconfig.json",
137
- "./src/examples/js/tsconfig.json"
162
+ "./examples/js/tsconfig.json"
138
163
  ]
139
164
  }
140
165
  },
141
166
  {
142
167
  "files": [
143
168
  "src/pkg/index.ts",
144
- "src/test/display.ts",
145
169
  "src/test/integration.ts"
146
170
  ],
147
171
  "rules": {
@@ -153,7 +177,7 @@
153
177
  },
154
178
  {
155
179
  "files": [
156
- "src/pkg/addon.ts"
180
+ "src/pkg/addon/node.ts"
157
181
  ],
158
182
  "rules": {
159
183
  "@typescript-eslint/no-var-requires": "off"
@@ -168,6 +192,15 @@
168
192
  "@typescript-eslint/no-redundant-type-constituents": "off"
169
193
  }
170
194
  },
195
+ {
196
+ "files": [
197
+ "src/pkg/protocol.test.ts"
198
+ ],
199
+ "rules": {
200
+ "jest/no-standalone-expect": "off",
201
+ "jest/no-conditional-expect": "off"
202
+ }
203
+ },
171
204
  {
172
205
  "files": [
173
206
  "src/test/*.test.ts",
@@ -163,8 +163,10 @@ const install = async () => {
163
163
  }
164
164
 
165
165
  const index = JSON.parse(await fetch(INDEX));
166
- const version = Object.keys(index)[+!ZIG_VERSION.dev];
167
- const release = index[version][`${arch}-${platform}`];
166
+ // TODO: ziglang/zig#17768
167
+ // const version = Object.keys(index)[+!ZIG_VERSION.dev];
168
+ const version = VERSION;
169
+ const release = index[VERSION][`${arch}-${platform}`];
168
170
  const url = release.tarball;
169
171
 
170
172
  let base = path.basename(url);
@@ -283,19 +285,19 @@ const downloadImportLib = async () => {
283
285
  let error = false;
284
286
  for (let i = 2; i < process.argv.length; i++) {
285
287
  switch (process.argv[i]) {
286
- case "--silent": {
287
- SILENT = true;
288
- break;
289
- }
290
- case "--zig": {
291
- if (options || only) error = true;
292
- only = true;
293
- break;
294
- }
295
- default: {
296
- if (options || only || !process.argv[i].startsWith('--options=')) error = true;
297
- options = process.argv[i].slice(10).split(' ');
298
- }
288
+ case "--silent": {
289
+ SILENT = true;
290
+ break;
291
+ }
292
+ case "--zig": {
293
+ if (options || only) error = true;
294
+ only = true;
295
+ break;
296
+ }
297
+ default: {
298
+ if (options || only || !process.argv[i].startsWith('--options=')) error = true;
299
+ options = process.argv[i].slice(10).split(' ');
300
+ }
299
301
  }
300
302
  }
301
303
  if (error) {
@@ -370,8 +372,10 @@ const downloadImportLib = async () => {
370
372
  const args = ['build', '-p', 'build', ...opts];
371
373
  if (!process.env.DEBUG_PKMN_ENGINE) args.push('-Doptimize=ReleaseFast', '-Dstrip');
372
374
  if (config.type === 'node') {
373
- args.push(`-Dnode-headers=${headers}`);
374
- if (process.platform === 'win32') args.push(`-Dnode-import-library=${lib}`);
375
+ args.push(`-Dnode-headers=${path.relative(ROOT, headers)}`);
376
+ if (process.platform === 'win32') {
377
+ args.push(`-Dnode-import-library=${path.relative(ROOT, lib)}`);
378
+ }
375
379
  } else {
376
380
  args.push('-Dwasm');
377
381
  }
@@ -386,8 +390,14 @@ const downloadImportLib = async () => {
386
390
  const prefix = process.platform === 'win32' ? '' : 'lib';
387
391
  const ext = process.platform === 'darwin'
388
392
  ? 'dylib' : process.platform === 'win32' ? 'dll' : 'so';
389
- const actual = path.join('build', 'lib', `${prefix}pkmn${suffix}.node.${ext}`);
390
- fs.renameSync(path.join(ROOT, actual), path.join(ROOT, artifact));
393
+ try {
394
+ const actual = path.join(ROOT, 'build', 'lib', `${prefix}pkmn${suffix}.node.${ext}`);
395
+ fs.renameSync(actual, artifact);
396
+ } catch (err) {
397
+ // ziglang/zig#19743 changed this path
398
+ const actual = path.join(ROOT, 'build', 'bin', `${prefix}pkmn${suffix}.node.${ext}`);
399
+ fs.renameSync(actual, artifact);
400
+ }
391
401
  }
392
402
  }
393
403
  })().catch(e => {
@@ -15,7 +15,7 @@ const ROOT = resolve(__dirname, '..', '..');
15
15
  const packageJSON = JSON.parse(readFileSync(join(ROOT, 'package.json'), 'utf8'));
16
16
 
17
17
  const missing = [];
18
- for (const dep of ['@pkmn/img', 'html-minifier', 'mustache']) {
18
+ for (const dep of ['@pkmn/img', 'html-minifier', 'esbuild']) {
19
19
  try {
20
20
  require.resolve(dep);
21
21
  } catch (e) {
@@ -0,0 +1,77 @@
1
+ const std = @import("std");
2
+
3
+ const expectEqual = std.testing.expectEqual;
4
+ const assert = std.debug.assert;
5
+
6
+ const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
7
+ const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
8
+
9
+ /// Helpers for working with bit-packed arrays of non-powers-of-2 types.
10
+ /// NOTE: ziglang/zig#12547
11
+ pub fn Array(comptime n: comptime_int, comptime U: type) type {
12
+ return struct {
13
+ const size = @bitSizeOf(U);
14
+ const options = .{ .signedness = .unsigned, .bits = size * n };
15
+ pub const T = @Type(if (@hasField(std.builtin.Type, "int"))
16
+ .{ .int = options }
17
+ else
18
+ .{ .Int = options });
19
+
20
+ /// Returns the value stored at index `i` in the array `a`.
21
+ pub fn get(a: T, i: usize) U {
22
+ assert(i < n);
23
+ const shift = i * size;
24
+ const mask: T = (1 << size) - 1;
25
+ const result = (a >> @intCast(shift)) & mask;
26
+ return switch (@typeInfo(U)) {
27
+ Enum => @enumFromInt(result),
28
+ Int => @intCast(result),
29
+ else => unreachable,
30
+ };
31
+ }
32
+
33
+ /// Sets the value at index `i` to `val` in the array `a`.
34
+ pub fn set(a: T, i: usize, val: U) T {
35
+ assert(i < n);
36
+ const v: T = switch (@typeInfo(U)) {
37
+ Enum => @intFromEnum(val),
38
+ Int => @intCast(val),
39
+ else => unreachable,
40
+ };
41
+ const shift = i * size;
42
+ const mask: T = @as(T, @intCast((1 << size) - 1)) << @intCast(shift);
43
+ const result: T = a & ~mask;
44
+ return result | (v << @intCast(shift));
45
+ }
46
+ };
47
+ }
48
+
49
+ test Array {
50
+ const A = Array(5, u8);
51
+ var a: A.T = 0;
52
+ try expectEqual(u40, @TypeOf(a));
53
+
54
+ a = A.set(a, 4, 241);
55
+ try expectEqual(@as(u8, 241), A.get(a, 4));
56
+ a = A.set(a, 4, 1);
57
+ try expectEqual(@as(u8, 1), A.get(a, 4));
58
+
59
+ for (0..5) |i| a = A.set(a, i, @intCast(i));
60
+ for (0..5) |i| try expectEqual(@as(u8, @intCast(i)), A.get(a, i));
61
+
62
+ const Optional = @import("optional.zig").Optional;
63
+
64
+ const B = Array(4, Optional(bool));
65
+ var b: B.T = 0;
66
+ try expectEqual(u8, @TypeOf(b));
67
+
68
+ b = B.set(b, 2, .true);
69
+ try expectEqual(Optional(bool).None, B.get(b, 1));
70
+ try expectEqual(Optional(bool).true, B.get(b, 2));
71
+ try expectEqual(@as(u8, 0b00100000), b);
72
+ b = B.set(b, 2, .None);
73
+ b = B.set(b, 0, .false);
74
+ try expectEqual(Optional(bool).None, B.get(b, 2));
75
+ try expectEqual(Optional(bool).false, B.get(b, 0));
76
+ try expectEqual(@as(u8, 0b0000001), b);
77
+ }
@@ -9,12 +9,12 @@ pub const Player = enum(u1) {
9
9
  P2,
10
10
 
11
11
  /// Returns a player's opponent.
12
- pub inline fn foe(self: Player) Player {
12
+ pub fn foe(self: Player) Player {
13
13
  return @enumFromInt(~@intFromEnum(self));
14
14
  }
15
15
 
16
16
  /// Return's an identifier for the player's Pokémon at the one-indexed `id`.
17
- pub inline fn ident(self: Player, id: u3) ID {
17
+ pub fn ident(self: Player, id: u3) ID {
18
18
  assert(id > 0 and id <= 6);
19
19
  return .{ .id = id, .player = self };
20
20
  }
@@ -27,24 +27,20 @@ test Player {
27
27
  }
28
28
 
29
29
  /// An identifier for a specific Pokémon in battle.
30
- pub const ID = packed struct {
30
+ pub const ID = packed struct(u8) {
31
31
  /// The one-indexed team slot of the Pokémon
32
32
  id: u3 = 0,
33
33
  /// The Pokémon's trainer.
34
34
  player: Player = .P1,
35
35
  _: u4 = 0,
36
36
 
37
- comptime {
38
- assert(@sizeOf(ID) == 1);
39
- }
40
-
41
37
  /// Converts the identifier into a number.
42
- pub inline fn int(self: ID) u4 {
38
+ pub fn int(self: ID) u4 {
43
39
  return @intCast(@as(u8, @bitCast(self)));
44
40
  }
45
41
 
46
42
  /// Decodes the identifier from a number.
47
- pub inline fn from(id: u4) ID {
43
+ pub fn from(id: u4) ID {
48
44
  return @bitCast(@as(u8, id));
49
45
  }
50
46
  };
@@ -52,12 +48,12 @@ pub const ID = packed struct {
52
48
  test ID {
53
49
  try expectEqual(@as(u8, 0b0001), @as(u8, @bitCast(ID{ .player = .P1, .id = 1 })));
54
50
  try expectEqual(@as(u8, 0b1101), @as(u8, @bitCast(ID{ .player = .P2, .id = 5 })));
55
- const id = ID{ .player = .P2, .id = 4 };
51
+ const id: ID = .{ .player = .P2, .id = 4 };
56
52
  try expectEqual(id, ID.from(id.int()));
57
53
  }
58
54
 
59
55
  /// A choice made by a player during battle.
60
- pub const Choice = packed struct {
56
+ pub const Choice = packed struct(u8) {
61
57
  /// The choice type.
62
58
  type: Choice.Type = .Pass,
63
59
  /// The choice data:
@@ -73,10 +69,6 @@ pub const Choice = packed struct {
73
69
  Move,
74
70
  Switch,
75
71
  };
76
-
77
- comptime {
78
- assert(@sizeOf(Choice) == 1);
79
- }
80
72
  };
81
73
 
82
74
  test Choice {
@@ -84,12 +76,12 @@ test Choice {
84
76
  const p2: Choice = .{ .type = .Switch, .data = 5 };
85
77
  try expectEqual(5, p2.data);
86
78
  try expectEqual(Choice.Type.Move, p1.type);
87
- try expectEqual(0b0001_0001, @as(u8, @bitCast(p1)));
88
- try expectEqual(0b0001_0110, @as(u8, @bitCast(p2)));
79
+ try expectEqual(@as(u8, 0b0001_0001), @as(u8, @bitCast(p1)));
80
+ try expectEqual(@as(u8, 0b0001_0110), @as(u8, @bitCast(p2)));
89
81
  }
90
82
 
91
83
  /// The result of the battle - all results other than 'None' should be considered terminal.
92
- pub const Result = packed struct {
84
+ pub const Result = packed struct(u8) {
93
85
  /// The type of result from the perspective of Player 1.
94
86
  /// `Error` is not possible when in Pokémon Showdown compatibility mode.
95
87
  type: Result.Type = .None,
@@ -117,13 +109,9 @@ pub const Result = packed struct {
117
109
  pub const Error: Result = .{ .type = .Error };
118
110
  /// The default result.
119
111
  pub const Default: Result = .{ .p1 = .Move, .p2 = .Move };
120
-
121
- comptime {
122
- assert(@sizeOf(Result) == 1);
123
- }
124
112
  };
125
113
 
126
114
  test Result {
127
- try expectEqual(0b0101_0000, @as(u8, @bitCast(Result.Default)));
128
- try expectEqual(0b1000_0000, @as(u8, @bitCast(Result{ .p2 = .Switch })));
115
+ try expectEqual(@as(u8, 0b0101_0000), @as(u8, @bitCast(Result.Default)));
116
+ try expectEqual(@as(u8, 0b1000_0000), @as(u8, @bitCast(Result{ .p2 = .Switch })));
129
117
  }
@@ -3,9 +3,14 @@ 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 Optional = if (@hasField(std.builtin.Type, "optional")) .optional else .Optional;
8
+ const Pointer = if (@hasField(std.builtin.Type, "pointer")) .pointer else .Pointer;
9
+ const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
10
+
6
11
  pub fn print(value: anytype) void {
7
- debug.getStderrMutex().lock();
8
- defer debug.getStderrMutex().unlock();
12
+ debug.lockStdErr();
13
+ defer debug.unlockStdErr();
9
14
  const stderr = io.getStdErr().writer();
10
15
 
11
16
  nosuspend {
@@ -19,7 +24,7 @@ pub fn print(value: anytype) void {
19
24
  }) catch return;
20
25
  } else {
21
26
  switch (@typeInfo(@TypeOf(value))) {
22
- .Struct => |info| {
27
+ Struct => |info| {
23
28
  if (info.is_tuple) {
24
29
  inline for (info.fields, 0..) |f, i| {
25
30
  inspect(@field(value, f.name));
@@ -42,11 +47,11 @@ fn inspect(value: anytype) void {
42
47
  nosuspend {
43
48
  const err = "Unable to format type '" ++ @typeName(@TypeOf(value)) ++ "'";
44
49
  switch (@typeInfo(@TypeOf(value))) {
45
- .Array => |info| {
50
+ Array => |info| {
46
51
  if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
47
52
  @compileError(err);
48
53
  },
49
- .Pointer => |ptr_info| switch (ptr_info.size) {
54
+ Pointer => |ptr_info| switch (ptr_info.size) {
50
55
  .One => switch (@typeInfo(ptr_info.child)) {
51
56
  .Array => |info| {
52
57
  if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
@@ -67,7 +72,7 @@ fn inspect(value: anytype) void {
67
72
  @compileError(err);
68
73
  },
69
74
  },
70
- .Optional => stderr.print("{?}", .{value}) catch return,
75
+ Optional => stderr.print("{?}", .{value}) catch return,
71
76
  else => stderr.print("{}", .{value}) catch return,
72
77
  }
73
78
  }
@@ -2,6 +2,8 @@ const std = @import("std");
2
2
 
3
3
  const assert = std.debug.assert;
4
4
 
5
+ const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
6
+
5
7
  const c = @cImport({
6
8
  @cDefine("NAPI_VERSION", "8");
7
9
  @cInclude("node_api.h");
@@ -35,7 +37,7 @@ pub const Number = struct {
35
37
  const T = @TypeOf(value);
36
38
  var result: c.napi_value = undefined;
37
39
  assert(c.napi_ok == switch (@typeInfo(T)) {
38
- .Int => |info| switch (info.bits) {
40
+ Int => |info| switch (info.bits) {
39
41
  0...32 => switch (info.signedness) {
40
42
  .signed => c.napi_create_int32(env, @as(i32, value), &result),
41
43
  .unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
@@ -50,7 +52,7 @@ pub const Number = struct {
50
52
 
51
53
  pub fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
52
54
  switch (@typeInfo(T)) {
53
- .Int => |info| switch (info.bits) {
55
+ Int => |info| switch (info.bits) {
54
56
  0...32 => switch (info.signedness) {
55
57
  .signed => {
56
58
  var result: i32 = undefined;
@@ -3,10 +3,12 @@ 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
+
6
8
  // TODO: ziglang/zig#104
7
9
  pub fn Optional(comptime T: type) type {
8
10
  const fields = std.meta.fields(switch (@typeInfo(T)) {
9
- .Bool => enum { false, true },
11
+ Bool => enum { false, true },
10
12
  else => T,
11
13
  });
12
14
 
@@ -14,6 +16,7 @@ pub fn Optional(comptime T: type) type {
14
16
  var decls = [_]std.builtin.Type.Declaration{};
15
17
 
16
18
  enumFields[0] = .{ .name = "None", .value = 0 };
19
+
17
20
  inline for (fields, 1..) |field, i| {
18
21
  assert(!std.mem.eql(u8, field.name, "None"));
19
22
  enumFields[i] = .{
@@ -22,14 +25,16 @@ pub fn Optional(comptime T: type) type {
22
25
  };
23
26
  }
24
27
 
25
- return @Type(.{
26
- .Enum = .{
27
- .tag_type = std.math.IntFittingRange(0, fields.len + 1),
28
- .fields = &enumFields,
29
- .decls = &decls,
30
- .is_exhaustive = true,
31
- },
32
- });
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 });
33
38
  }
34
39
 
35
40
  test Optional {
@@ -45,7 +50,7 @@ test Optional {
45
50
  try expect(b != .true);
46
51
  try expect(b != .false);
47
52
 
48
- const Player = @import("./data.zig").Player;
53
+ const Player = @import("data.zig").Player;
49
54
  try expect(@bitSizeOf(Optional(Player)) == 2);
50
55
 
51
56
  const p: Optional(Player) = .P2;
@@ -57,4 +62,10 @@ test Optional {
57
62
  try expect(q == .None);
58
63
  try expect(q != .P1);
59
64
  try expect(q != .P2);
65
+
66
+ const Three = enum(u2) { A, B, C };
67
+ try expect(@bitSizeOf(Optional(Three)) == 2);
68
+
69
+ const Four = enum(u2) { A, B, C, D };
70
+ try expect(@bitSizeOf(Optional(Four)) == 3);
60
71
  }
@@ -10,6 +10,8 @@ pub const Options = struct {
10
10
  advance: ?bool = null,
11
11
  ebc: ?bool = null,
12
12
  key: ?bool = null,
13
+ miss: ?bool = null,
14
+ overwrite: ?bool = null,
13
15
  internal: ?bool = null,
14
16
  };
15
17
 
@@ -25,6 +27,8 @@ pub const calc = get("calc", false);
25
27
  pub const advance = get("advance", true);
26
28
  pub const ebc = get("ebc", true);
27
29
  pub const key = get("key", false);
30
+ pub const miss = get("miss", true);
31
+ pub const overwrite = get("overwrite", true);
28
32
  pub const internal = get("internal", false);
29
33
 
30
34
  fn get(comptime name: []const u8, default: bool) bool {