@pkmn/engine 0.1.0-dev.cf6f5d2c → 0.1.0-dev.d1b39ff0

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 (60) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +95 -59
  3. package/build/pkg/addon.js +9 -9
  4. package/build/pkg/addon.js.map +1 -1
  5. package/build/pkg/data/ids.json +12 -12
  6. package/build/pkg/data/layout.json +27 -5
  7. package/build/pkg/data/protocol.json +69 -12
  8. package/build/pkg/data.d.ts +4 -4
  9. package/build/pkg/data.js.map +1 -1
  10. package/build/pkg/gen1.d.ts +2 -1
  11. package/build/pkg/gen1.js +38 -20
  12. package/build/pkg/gen1.js.map +1 -1
  13. package/build/pkg/index.d.ts +7 -4
  14. package/build/pkg/index.js.map +1 -1
  15. package/build/pkg/protocol.d.ts +4 -2
  16. package/build/pkg/protocol.js +65 -44
  17. package/build/pkg/protocol.js.map +1 -1
  18. package/build/test/display.js +15 -6
  19. package/build/test/display.js.map +1 -1
  20. package/build/tools/debug.js +5 -4
  21. package/build/tools/debug.js.map +1 -1
  22. package/build.zig +148 -126
  23. package/package.json +53 -24
  24. package/src/bin/install-pkmn-engine +35 -22
  25. package/src/lib/common/battle.zig +17 -0
  26. package/src/lib/common/data.zig +19 -31
  27. package/src/lib/common/debug.zig +22 -13
  28. package/src/lib/common/js.zig +118 -0
  29. package/src/lib/common/optional.zig +67 -0
  30. package/src/lib/common/options.zig +11 -0
  31. package/src/lib/common/protocol.zig +906 -367
  32. package/src/lib/common/rational.zig +454 -0
  33. package/src/lib/common/rng.zig +35 -26
  34. package/src/lib/common/util.zig +12 -0
  35. package/src/lib/gen1/calc.zig +802 -0
  36. package/src/lib/gen1/chance.zig +1159 -0
  37. package/src/lib/gen1/data/moves.zig +202 -190
  38. package/src/lib/gen1/data/species.zig +5 -5
  39. package/src/lib/gen1/data/types.zig +10 -10
  40. package/src/lib/gen1/data.zig +104 -90
  41. package/src/lib/gen1/helpers.zig +140 -42
  42. package/src/lib/gen1/mechanics.zig +1135 -773
  43. package/src/lib/gen2/calc.zig +135 -0
  44. package/src/lib/gen2/chance.zig +895 -0
  45. package/src/lib/gen2/data/items.zig +14 -20
  46. package/src/lib/gen2/data/moves.zig +664 -570
  47. package/src/lib/gen2/data/species.zig +2 -2
  48. package/src/lib/gen2/data/types.zig +137 -34
  49. package/src/lib/gen2/data.zig +130 -92
  50. package/src/lib/gen2/generated.zig +806 -0
  51. package/src/lib/gen2/helpers.zig +50 -26
  52. package/src/lib/gen2/mechanics.zig +3568 -12
  53. package/src/lib/node.zig +131 -0
  54. package/src/lib/pkmn.zig +81 -5
  55. package/src/lib/wasm.zig +13 -0
  56. package/src/test/display/pkmn.html.tmpl +5 -5
  57. package/src/lib/binding/node.zig +0 -229
  58. package/src/lib/binding/wasm.zig +0 -10
  59. package/src/lib/gen1/README.md +0 -804
  60. package/src/lib/gen2/README.md +0 -309
@@ -0,0 +1,131 @@
1
+ const std = @import("std");
2
+
3
+ const pkmn = @import("pkmn.zig");
4
+
5
+ const js = @import("common/js.zig");
6
+
7
+ const assert = std.debug.assert;
8
+
9
+ const c = @cImport({
10
+ @cDefine("NAPI_VERSION", "8");
11
+ @cInclude("node_api.h");
12
+ });
13
+
14
+ export fn napi_register_module_v1(env: c.napi_env, exports: c.napi_value) c.napi_value {
15
+ const properties = [_]c.napi_property_descriptor{
16
+ js.Property.init("engine", .{ .value = register(env) }),
17
+ };
18
+ assert(c.napi_define_properties(env, exports, properties.len, &properties) == c.napi_ok);
19
+ return exports;
20
+ }
21
+
22
+ pub fn register(env: c.napi_env) c.napi_value {
23
+ const object = js.Object.init(env);
24
+ const properties = [_]c.napi_property_descriptor{
25
+ js.Property.init("options", .{ .value = options(env) }),
26
+ js.Property.init("bindings", .{ .value = bindings(env) }),
27
+ };
28
+ assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
29
+ return object;
30
+ }
31
+
32
+ fn options(env: c.napi_env) c.napi_value {
33
+ const object = js.Object.init(env);
34
+ const properties = [_]c.napi_property_descriptor{
35
+ js.Property.init("showdown", .{ .value = js.Boolean.init(env, pkmn.options.showdown) }),
36
+ js.Property.init("log", .{ .value = js.Boolean.init(env, pkmn.options.log) }),
37
+ js.Property.init("chance", .{ .value = js.Boolean.init(env, pkmn.options.chance) }),
38
+ js.Property.init("calc", .{ .value = js.Boolean.init(env, pkmn.options.calc) }),
39
+ };
40
+ assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
41
+ return object;
42
+ }
43
+
44
+ fn bindings(env: c.napi_env) c.napi_value {
45
+ const array = js.Array.init(env, .{ .length = 1 });
46
+ js.Array.set(env, array, 0, bind(env, pkmn.gen1));
47
+ return array;
48
+ }
49
+
50
+ fn bind(env: c.napi_env, gen: anytype) c.napi_value {
51
+ const choices_size: u32 = @intCast(gen.CHOICES_SIZE);
52
+ const logs_size: u32 = @intCast(gen.LOGS_SIZE);
53
+ const object = js.Object.init(env);
54
+ const properties = [_]c.napi_property_descriptor{
55
+ js.Property.init("CHOICES_SIZE", .{ .value = js.Number.init(env, choices_size) }),
56
+ js.Property.init("LOGS_SIZE", .{ .value = js.Number.init(env, logs_size) }),
57
+ js.Property.init("update", .{ .method = update(gen) }),
58
+ js.Property.init("choices", .{ .method = choices(gen) }),
59
+ };
60
+ assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
61
+ return object;
62
+ }
63
+
64
+ fn update(gen: anytype) c.napi_callback {
65
+ return struct {
66
+ fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
67
+ var argc: usize = 4;
68
+ var argv: [4]c.napi_value = undefined;
69
+ assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
70
+ assert(argc == 4);
71
+
72
+ var data: ?*anyopaque = undefined;
73
+ var len: usize = 0;
74
+ assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
75
+ assert(len == @sizeOf(gen.Battle(gen.PRNG)));
76
+
77
+ var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
78
+ const c1: pkmn.Choice = @bitCast(js.Number.get(env, argv[1], u8));
79
+ const c2: pkmn.Choice = @bitCast(js.Number.get(env, argv[2], u8));
80
+
81
+ var vtype: c.napi_valuetype = undefined;
82
+ assert(c.napi_typeof(env, argv[3], &vtype) == c.napi_ok);
83
+ const result = switch (vtype) {
84
+ c.napi_undefined, c.napi_null => battle.update(c1, c2, &gen.NULL),
85
+ else => result: {
86
+ assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
87
+ assert(len == gen.LOGS_SIZE);
88
+
89
+ const buf = @as([*]u8, @ptrCast(data.?))[0..gen.LOGS_SIZE];
90
+ var stream: pkmn.protocol.ByteStream = .{ .buffer = buf };
91
+ // TODO: extract out
92
+ var opts = pkmn.battle.options(
93
+ pkmn.protocol.FixedLog{ .writer = stream.writer() },
94
+ gen.chance.NULL,
95
+ gen.calc.NULL,
96
+ );
97
+ break :result battle.update(c1, c2, &opts);
98
+ },
99
+ } catch unreachable;
100
+
101
+ return js.Number.init(env, @as(u8, @bitCast(result)));
102
+ }
103
+ }.call;
104
+ }
105
+
106
+ fn choices(gen: anytype) c.napi_callback {
107
+ return struct {
108
+ fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
109
+ var argc: usize = 4;
110
+ var argv: [4]c.napi_value = undefined;
111
+ assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
112
+ assert(argc == 4);
113
+
114
+ var data: ?*anyopaque = undefined;
115
+ var len: usize = 0;
116
+ assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
117
+ assert(len == @sizeOf(gen.Battle(gen.PRNG)));
118
+
119
+ var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
120
+ const player: pkmn.Player = @enumFromInt(js.Number.get(env, argv[1], u8));
121
+ const request: pkmn.Choice.Type = @enumFromInt(js.Number.get(env, argv[2], u8));
122
+
123
+ assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
124
+ assert(len == gen.CHOICES_SIZE);
125
+
126
+ const out = @as([*]pkmn.Choice, @ptrCast(data.?))[0..gen.CHOICES_SIZE];
127
+ const n = battle.choices(player, request, out);
128
+ return js.Number.init(env, @as(u8, @bitCast(n)));
129
+ }
130
+ }.call;
131
+ }
package/src/lib/pkmn.zig CHANGED
@@ -1,4 +1,4 @@
1
- /// Configurated Options for the pkmn package.
1
+ /// Configured Options for the pkmn package.
2
2
  pub const options = @import("common/options.zig");
3
3
  /// Configures the behavior of the pkmn package.
4
4
  pub const Options = options.Options;
@@ -22,13 +22,16 @@ pub const Choice = @import("common/data.zig").Choice;
22
22
  /// considered terminal.
23
23
  pub const Result = @import("common/data.zig").Result;
24
24
 
25
+ // TODO
26
+ pub const Array = @import("common/array.zig").Array;
27
+ // TODO
28
+ pub const Optional = @import("common/optional.zig").Optional;
29
+
25
30
  /// Pokémon Showdown's RNG (backed by a Generation V & VI RNG).
26
31
  pub const PSRNG = @import("common/rng.zig").PSRNG;
27
32
 
28
33
  /// Namespace for helpers related to protocol message logging.
29
- pub const protocol = if (options.internal) struct {
30
- pub usingnamespace @import("common/protocol.zig");
31
- } else struct {
34
+ pub const protocol = if (options.internal) @import("common/protocol.zig") else struct {
32
35
  /// Logs protocol information to its `Writer` during a battle update when
33
36
  /// `options.log` is enabled.
34
37
  pub const Log = @import("common/protocol.zig").Log;
@@ -46,9 +49,82 @@ pub const protocol = if (options.internal) struct {
46
49
  pub const NULL = @import("common/protocol.zig").NULL;
47
50
  };
48
51
 
49
- /// Namespace for Generation I Pokémon
52
+ pub usingnamespace @import("common/rational.zig");
53
+
54
+ /// Namespace for cross-generation battle-related types.
55
+ pub const battle = @import("common/battle.zig");
56
+
57
+ /// Namespace for Generation I Pokémon.
50
58
  pub const gen1 = struct {
51
59
  pub usingnamespace @import("gen1/data.zig");
60
+ /// Tracks chance actions and their associated probability during a
61
+ /// Generation I battle update when `options.chance` is enabled.
62
+ pub const Chance = @import("gen1/chance.zig").Chance;
63
+ /// Namespace for types associated with tracking Generation I Pokémon chance outcomes.
64
+ pub const chance = struct {
65
+ /// Actions taken by a hypothetical "chance player" that convey
66
+ /// information about which RNG events were observed during a Generation
67
+ /// I battle `update`. This can additionally be provided as input to the
68
+ /// `update` call via the `Calc` when `options.calc` is enabled to
69
+ /// override the normal behavior of the RNG in order to force specific
70
+ /// outcomes.
71
+ pub const Actions = @import("gen1/chance.zig").Actions;
72
+ /// Information about the RNG that was observed during a Generation I
73
+ /// battle `update` for a single player.
74
+ pub const Action = @import("gen1/chance.zig").Action;
75
+ /// TODO
76
+ pub const Durations = @import("gen1/chance.zig").Durations;
77
+ /// TODO
78
+ pub const Duration = @import("gen1/chance.zig").Duration;
79
+ /// Null object pattern implementation of Generation I `Chance` which
80
+ /// does nothing, though chance tracking should additionally be turned
81
+ /// off entirely via `options.chance`.
82
+ pub const NULL = @import("gen1/chance.zig").NULL;
83
+ };
84
+ /// Allows for forcing the value of specific RNG events during a Generation I battle `update`
85
+ /// via `overrides` and tracks `summaries` of information relevant to damage calculation.
86
+ pub const Calc = @import("gen1/calc.zig").Calc;
87
+ /// Namespace for types associated with supported Generation I Pokémon damage calc features.
88
+ pub const calc = if (options.internal) @import("gen1/calc.zig") else struct {
89
+ /// Information relevant to damage calculation that occured during a Generation I
90
+ /// battle `update`.
91
+ pub const Summaries = @import("gen1/calc.zig").Summaries;
92
+ /// Information relevant to damage calculation that occured during a Generation I
93
+ /// battle `update` for a single player.
94
+ pub const Summary = @import("gen1/calc.zig").Summary;
95
+ /// Null object pattern implementation of Generation I `Calc` which does nothing,
96
+ /// though damage calculator support should additionally be turned off
97
+ /// entirely via `options.calc`.
98
+ pub const NULL = @import("gen1/calc.zig").NULL;
99
+ };
52
100
  /// Provides helpers for initializing Generation I Pokémon battles.
53
101
  pub const helpers = @import("gen1/helpers.zig");
54
102
  };
103
+
104
+ /// TODO
105
+ pub const gen2 = struct {
106
+ pub usingnamespace @import("gen2/data.zig");
107
+ pub const Chance = @import("gen2/chance.zig").Chance;
108
+ pub const chance = struct {
109
+ pub const Actions = @import("gen2/chance.zig").Actions;
110
+ pub const Action = @import("gen2/chance.zig").Action;
111
+ pub const Durations = @import("gen2/chance.zig").Durations;
112
+ pub const Duration = @import("gen2/chance.zig").Duration;
113
+ pub const NULL = @import("gen2/chance.zig").NULL;
114
+ };
115
+ pub const Calc = @import("gen2/calc.zig").Calc;
116
+ pub const calc = if (options.internal) @import("gen2/calc.zig") else struct {
117
+ pub const Summaries = @import("gen2/calc.zig").Summaries;
118
+ pub const Summary = @import("gen2/calc.zig").Summary;
119
+ pub const NULL = @import("gen2/calc.zig").NULL;
120
+ };
121
+ pub const helpers = @import("gen2/helpers.zig");
122
+ };
123
+
124
+ // Internal APIs used by other pkmn libraries, not actually part of the public API.
125
+ // NOTE: `pub usingnamespace struct { ... }` here results in a (false?) dependency loop
126
+ pub const js = if (options.internal) @import("common/js.zig") else {};
127
+ pub const bindings = if (options.internal) struct {
128
+ pub const node = @import("node.zig");
129
+ pub const wasm = @import("wasm.zig");
130
+ } else {};
@@ -0,0 +1,13 @@
1
+ const std = @import("std");
2
+
3
+ const pkmn = @import("pkmn.zig");
4
+
5
+ export const SHOWDOWN = pkmn.options.showdown;
6
+ export const LOG = pkmn.options.log;
7
+ export const CHANCE = pkmn.options.chance;
8
+ export const CALC = pkmn.options.calc;
9
+
10
+ export const GEN1_CHOICES_SIZE =
11
+ std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(pkmn.gen1.CHOICES_SIZE))) catch unreachable;
12
+ export const GEN1_LOGS_SIZE =
13
+ std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(pkmn.gen1.LOGS_SIZE))) catch unreachable;
@@ -218,8 +218,9 @@
218
218
  border-radius: 0 4px 4px 0;
219
219
  }
220
220
  .moves ul {
221
- column-count: 2;
222
- column-gap: 2em;
221
+ display: grid;
222
+ grid-template-columns: repeat(2, auto);
223
+ gap: 0 2em;
223
224
  }
224
225
  .left {
225
226
  padding-right: 32px;
@@ -268,7 +269,7 @@
268
269
  }
269
270
  @media(max-width: 1200px) {
270
271
  .moves ul {
271
- column-count: 1;
272
+ grid-template-columns: repeat(1, auto);
272
273
  }
273
274
  }
274
275
  @media(max-width: 1100px) {
@@ -283,7 +284,7 @@
283
284
  margin-left: 5px;
284
285
  }
285
286
  .moves ul {
286
- column-count: 2;
287
+ grid-template-columns: repeat(2, auto);
287
288
  }
288
289
  .left {
289
290
  padding-right: 0;
@@ -309,7 +310,6 @@
309
310
  justify-content: center;
310
311
  }
311
312
  .moves ul {
312
- column-count: 1;
313
313
  display: inline-block;
314
314
  padding: 0;
315
315
  }
@@ -1,229 +0,0 @@
1
- const std = @import("std");
2
- const pkmn = @import("../pkmn.zig");
3
-
4
- const assert = std.debug.assert;
5
-
6
- const c = @cImport({
7
- @cDefine("NAPI_VERSION", "8");
8
- @cInclude("node_api.h");
9
- });
10
-
11
- export fn napi_register_module_v1(env: c.napi_env, exports: c.napi_value) c.napi_value {
12
- const properties = [_]c.napi_property_descriptor{
13
- Property.init("options", .{ .value = options(env) }),
14
- Property.init("bindings", .{ .value = bindings(env) }),
15
- };
16
- assert(c.napi_define_properties(env, exports, properties.len, &properties) == c.napi_ok);
17
- return exports;
18
- }
19
-
20
- fn options(env: c.napi_env) c.napi_value {
21
- var object = Object.init(env);
22
- const properties = [_]c.napi_property_descriptor{
23
- Property.init("showdown", .{ .value = Boolean.init(env, pkmn.options.showdown) }),
24
- Property.init("log", .{ .value = Boolean.init(env, pkmn.options.log) }),
25
- };
26
- assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
27
- return object;
28
- }
29
-
30
- fn bindings(env: c.napi_env) c.napi_value {
31
- var array = Array.init(env, .{ .length = 1 });
32
- Array.set(env, array, 0, bind(env, pkmn.gen1));
33
- return array;
34
- }
35
-
36
- fn bind(env: c.napi_env, gen: anytype) c.napi_value {
37
- const choices_size = @intCast(u32, gen.CHOICES_SIZE);
38
- const logs_size = @intCast(u32, gen.LOGS_SIZE);
39
- var object = Object.init(env);
40
- const properties = [_]c.napi_property_descriptor{
41
- Property.init("CHOICES_SIZE", .{ .value = Number.init(env, choices_size) }),
42
- Property.init("LOGS_SIZE", .{ .value = Number.init(env, logs_size) }),
43
- Property.init("update", .{ .method = update(gen) }),
44
- Property.init("choices", .{ .method = choices(gen) }),
45
- };
46
- assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
47
- return object;
48
- }
49
-
50
- fn update(gen: anytype) c.napi_callback {
51
- return struct {
52
- fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
53
- var argc: usize = 4;
54
- var argv: [4]c.napi_value = undefined;
55
- assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
56
- assert(argc == 4);
57
-
58
- var data: ?*anyopaque = undefined;
59
- var len: usize = 0;
60
- assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
61
- assert(len == @sizeOf(gen.Battle(gen.PRNG)));
62
- assert(data != null);
63
-
64
- var aligned = @alignCast(@alignOf(*gen.Battle(gen.PRNG)), data.?);
65
- var battle = @ptrCast(*gen.Battle(gen.PRNG), aligned);
66
- const c1 = @bitCast(pkmn.Choice, Number.get(env, argv[1], u8));
67
- const c2 = @bitCast(pkmn.Choice, Number.get(env, argv[2], u8));
68
-
69
- var vtype: c.napi_valuetype = undefined;
70
- assert(c.napi_typeof(env, argv[3], &vtype) == c.napi_ok);
71
- const result = switch (vtype) {
72
- c.napi_undefined, c.napi_null => battle.update(c1, c2, pkmn.protocol.NULL),
73
- else => result: {
74
- assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
75
- assert(len == gen.LOGS_SIZE);
76
- assert(data != null);
77
-
78
- var buf = @ptrCast([*]u8, data.?)[0..gen.LOGS_SIZE];
79
- var stream = pkmn.protocol.ByteStream{ .buffer = buf };
80
- var log = pkmn.protocol.FixedLog{ .writer = stream.writer() };
81
- break :result battle.update(c1, c2, log);
82
- },
83
- } catch unreachable;
84
-
85
- return Number.init(env, @bitCast(u8, result));
86
- }
87
- }.call;
88
- }
89
-
90
- fn choices(gen: anytype) c.napi_callback {
91
- return struct {
92
- fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
93
- var argc: usize = 4;
94
- var argv: [4]c.napi_value = undefined;
95
- assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
96
- assert(argc == 4);
97
-
98
- var data: ?*anyopaque = undefined;
99
- var len: usize = 0;
100
- assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
101
- assert(len == @sizeOf(gen.Battle(gen.PRNG)));
102
- assert(data != null);
103
-
104
- var aligned = @alignCast(@alignOf(*gen.Battle(gen.PRNG)), data.?);
105
- var battle = @ptrCast(*gen.Battle(gen.PRNG), aligned);
106
-
107
- const player = @intToEnum(pkmn.Player, Number.get(env, argv[1], u8));
108
- const request = @intToEnum(pkmn.Choice.Type, Number.get(env, argv[2], u8));
109
-
110
- assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
111
- assert(len == gen.CHOICES_SIZE);
112
- assert(data != null);
113
-
114
- var out = @ptrCast([*]pkmn.Choice, data.?)[0..gen.CHOICES_SIZE];
115
- const n = battle.choices(player, request, out);
116
- return Number.init(env, @bitCast(u8, n));
117
- }
118
- }.call;
119
- }
120
-
121
- const Array = struct {
122
- fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
123
- var result: c.napi_value = undefined;
124
- assert(c.napi_ok == if (o.length) |n|
125
- c.napi_create_array_with_length(env, n, &result)
126
- else
127
- c.napi_create_array(env, &result));
128
- return result;
129
- }
130
-
131
- fn set(env: c.napi_env, array: c.napi_value, index: u32, value: c.napi_value) void {
132
- assert(c.napi_set_element(env, array, index, value) == c.napi_ok);
133
- }
134
- };
135
-
136
- const Boolean = struct {
137
- fn init(env: c.napi_env, value: bool) c.napi_value {
138
- var result: c.napi_value = undefined;
139
- assert(c.napi_get_boolean(env, value, &result) == c.napi_ok);
140
- return result;
141
- }
142
- };
143
-
144
- const Number = struct {
145
- fn init(env: c.napi_env, value: anytype) c.napi_value {
146
- const T = @TypeOf(value);
147
- var result: c.napi_value = undefined;
148
- assert(c.napi_ok == switch (@typeInfo(T)) {
149
- .Int => |info| switch (info.bits) {
150
- 0...32 => switch (info.signedness) {
151
- .signed => c.napi_create_int32(env, @as(i32, value), &result),
152
- .unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
153
- },
154
- 33...52 => c.napi_create_int64(env, @as(i64, value), &result),
155
- else => @compileError("int can't be represented as JS number"),
156
- },
157
- else => @compileError("expected number, got: " ++ @typeName(T)),
158
- });
159
- return result;
160
- }
161
-
162
- fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
163
- switch (@typeInfo(T)) {
164
- .Int => |info| switch (info.bits) {
165
- 0...32 => switch (info.signedness) {
166
- .signed => {
167
- var result: i32 = undefined;
168
- assert(c.napi_get_value_int32(env, value, &result) == c.napi_ok);
169
- return if (info.bits == 32) result else @intCast(T, result);
170
- },
171
- .unsigned => {
172
- var result: u32 = undefined;
173
- assert(c.napi_get_value_uint32(env, value, &result) == c.napi_ok);
174
- return if (info.bits == 32) result else @intCast(T, result);
175
- },
176
- },
177
- 33...63 => {
178
- var result: i64 = undefined;
179
- assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
180
- return @intCast(T, result);
181
- },
182
- else => {
183
- var result: i64 = undefined;
184
- assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
185
- return switch (info.signedness) {
186
- .signed => @as(T, value),
187
- .unsigned => if (0 <= value) @intCast(T, value) else unreachable,
188
- };
189
- },
190
- },
191
- else => @compileError("expected number, got: " ++ @typeName(T)),
192
- }
193
- }
194
- };
195
-
196
- const Object = struct {
197
- fn init(env: c.napi_env) c.napi_value {
198
- var result: c.napi_value = undefined;
199
- assert(c.napi_create_object(env, &result) == c.napi_ok);
200
- return result;
201
- }
202
- };
203
-
204
- const Property = union(enum) {
205
- method: c.napi_callback,
206
- value: c.napi_value,
207
-
208
- fn init(comptime name: [:0]const u8, property: Property) c.napi_property_descriptor {
209
- return .{
210
- .utf8name = name,
211
- .name = null,
212
- .method = switch (property) {
213
- .method => |m| m,
214
- .value => null,
215
- },
216
- .getter = null,
217
- .setter = null,
218
- .value = switch (property) {
219
- .method => null,
220
- .value => |v| v,
221
- },
222
- .attributes = switch (property) {
223
- .method => c.napi_default,
224
- .value => c.napi_enumerable,
225
- },
226
- .data = null,
227
- };
228
- }
229
- };
@@ -1,10 +0,0 @@
1
- const std = @import("std");
2
- const pkmn = @import("../pkmn.zig");
3
-
4
- export const SHOWDOWN = pkmn.options.showdown;
5
- export const LOG = pkmn.options.log;
6
-
7
- export const GEN1_CHOICES_SIZE =
8
- std.math.ceilPowerOfTwo(u32, @intCast(u32, pkmn.gen1.CHOICES_SIZE)) catch unreachable;
9
- export const GEN1_LOGS_SIZE =
10
- std.math.ceilPowerOfTwo(u32, @intCast(u32, pkmn.gen1.LOGS_SIZE)) catch unreachable;