@pkmn/engine 0.1.0-dev.f3aab252 → 0.1.0-dev.f3b63b86

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 (80) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +35 -8
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +47 -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 +110 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon/wasm.d.ts +2 -0
  10. package/build/pkg/addon/wasm.js +53 -0
  11. package/build/pkg/addon/wasm.js.map +1 -0
  12. package/build/pkg/addon.d.ts +26 -4
  13. package/build/pkg/addon.js +34 -65
  14. package/build/pkg/addon.js.map +1 -1
  15. package/build/pkg/data/layout.json +15 -2
  16. package/build/pkg/data/protocol.json +23 -9
  17. package/build/pkg/data.d.ts +23 -1
  18. package/build/pkg/data.js.map +1 -1
  19. package/build/pkg/gen1.d.ts +15 -18
  20. package/build/pkg/gen1.js +28 -19
  21. package/build/pkg/gen1.js.map +1 -1
  22. package/build/pkg/index.d.ts +39 -89
  23. package/build/pkg/index.js +23 -70
  24. package/build/pkg/index.js.map +1 -1
  25. package/build/pkg/protocol.d.ts +20 -1
  26. package/build/pkg/protocol.js +105 -103
  27. package/build/pkg/protocol.js.map +1 -1
  28. package/build/tools/debug.js +68 -74
  29. package/build/tools/debug.js.map +1 -1
  30. package/build/tools/display/debug.jsx +152 -0
  31. package/build/tools/display/debug.jsx.map +1 -0
  32. package/build/tools/display/index.js +152 -0
  33. package/build/tools/display/index.js.map +1 -0
  34. package/build/tools/display/ui.jsx +359 -0
  35. package/build/tools/display/ui.jsx.map +1 -0
  36. package/build/tools/display/util.js +33 -0
  37. package/build/tools/display/util.js.map +1 -0
  38. package/build.zig +235 -213
  39. package/package.json +59 -141
  40. package/src/bin/install-pkmn-engine +25 -9
  41. package/src/bin/pkmn-debug +1 -1
  42. package/src/lib/bindings/c.zig +200 -0
  43. package/src/lib/bindings/node.zig +117 -0
  44. package/src/lib/bindings/wasm.zig +50 -0
  45. package/src/lib/common/array.zig +5 -12
  46. package/src/lib/common/debug.zig +30 -9
  47. package/src/lib/common/js.zig +3 -9
  48. package/src/lib/common/optional.zig +11 -21
  49. package/src/lib/common/options.zig +4 -0
  50. package/src/lib/common/protocol.zig +180 -171
  51. package/src/lib/common/rational.zig +278 -285
  52. package/src/lib/common/rng.zig +1 -3
  53. package/src/lib/common/util.zig +14 -31
  54. package/src/lib/gen1/calc.zig +258 -106
  55. package/src/lib/gen1/chance.zig +57 -54
  56. package/src/lib/gen1/data/moves.zig +2 -4
  57. package/src/lib/gen1/data/species.zig +1 -3
  58. package/src/lib/gen1/data/types.zig +3 -4
  59. package/src/lib/gen1/data.zig +12 -16
  60. package/src/lib/gen1/helpers.zig +13 -28
  61. package/src/lib/gen1/mechanics.zig +131 -101
  62. package/src/lib/gen2/calc.zig +12 -23
  63. package/src/lib/gen2/chance.zig +12 -18
  64. package/src/lib/gen2/data/items.zig +1 -3
  65. package/src/lib/gen2/data/moves.zig +2 -4
  66. package/src/lib/gen2/data/species.zig +1 -3
  67. package/src/lib/gen2/data/types.zig +7 -5
  68. package/src/lib/gen2/data.zig +19 -22
  69. package/src/lib/gen2/generated.zig +18 -23
  70. package/src/lib/gen2/helpers.zig +9 -18
  71. package/src/lib/gen2/mechanics.zig +50 -47
  72. package/src/lib/napi.h +2 -0
  73. package/src/lib/node.zig +4 -121
  74. package/src/lib/pkmn.zig +98 -19
  75. package/src/lib/wasm.zig +9 -9
  76. package/src/tools/display/dom.ts +93 -0
  77. package/src/tools/display/ui.css +354 -0
  78. package/build/test/display.js +0 -405
  79. package/build/test/display.js.map +0 -1
  80. package/src/test/display/pkmn.html.tmpl +0 -368
package/build.zig CHANGED
@@ -1,5 +1,9 @@
1
- const std = @import("std");
2
1
  const builtin = @import("builtin");
2
+ const std = @import("std");
3
+
4
+ fn ArrayList(comptime T: type) type {
5
+ return std.array_list.Aligned(T, null);
6
+ }
3
7
 
4
8
  pub const Options = struct {
5
9
  showdown: ?bool = null,
@@ -8,17 +12,6 @@ pub const Options = struct {
8
12
  calc: ?bool = null,
9
13
  };
10
14
 
11
- const Step = if (@hasDecl(std.Build, "Step")) std.Build.Step else .{
12
- .Compile = std.Build.CompileStep,
13
- .Options = std.Build.OptionsStep,
14
- .Run = std.Build.RunStep,
15
- };
16
- const has_path = @hasField(std.Build.LazyPath, "path");
17
- const root_module = @hasField(Step.Compile, "root_module");
18
- const force_pic = !@hasField(std.Build.SharedLibraryOptions, "pic");
19
- const ResolvedTarget =
20
- if (@hasDecl(std.Build, "ResolvedTarget")) std.Build.ResolvedTarget else std.zig.CrossTarget;
21
-
22
15
  pub fn module(b: *std.Build, options: Options) *std.Build.Module {
23
16
  const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
24
17
  const build_options = b.addOptions();
@@ -27,28 +20,30 @@ pub fn module(b: *std.Build, options: Options) *std.Build.Module {
27
20
  build_options.addOption(?bool, "chance", options.chance);
28
21
  build_options.addOption(?bool, "calc", options.calc);
29
22
 
30
- const root_source_file = .{ .cwd_relative = dirname ++ "/src/lib/pkmn.zig" };
31
- const imports = &.{.{ .name = "build_options", .module = build_options.createModule() }};
32
- return b.createModule(if (@hasDecl(std.Build, "CreateModuleOptions"))
33
- .{ .source_file = root_source_file, .dependencies = imports }
34
- else
35
- .{ .root_source_file = root_source_file, .imports = imports });
23
+ const root_source_file: std.Build.LazyPath =
24
+ .{ .cwd_relative = dirname ++ "/src/lib/pkmn.zig" };
25
+ const imports: []const std.Build.Module.Import =
26
+ &.{.{ .name = "build_options", .module = build_options.createModule() }};
27
+ return b.createModule(.{ .root_source_file = root_source_file, .imports = imports });
36
28
  }
37
29
 
38
30
  pub fn build(b: *std.Build) !void {
39
31
  const target = b.standardTargetOptions(.{});
40
32
  const optimize = b.standardOptimizeOption(.{});
41
33
 
42
- const default: ?bool = if (force_pic) false else null;
43
34
  const node_headers = b.option([]const u8, "node-headers", "Path to node-headers");
44
35
  const node_import_lib =
45
36
  b.option([]const u8, "node-import-library", "Path to node import library (Windows)");
37
+ const demo = if (try exists(b, "src/tools/demo.zig"))
38
+ b.option(bool, "demo", "Build the demo library") orelse false
39
+ else
40
+ false;
46
41
  const wasm = b.option(bool, "wasm", "Build a WASM library") orelse false;
47
42
  const wasm_stack_size =
48
43
  b.option(u64, "wasm-stack-size", "The size of WASM stack") orelse std.wasm.page_size;
49
44
  const dynamic = b.option(bool, "dynamic", "Build a dynamic library") orelse false;
50
- const strip = b.option(bool, "strip", "Strip debugging symbols from binary") orelse default;
51
- const pic = b.option(bool, "pic", "Force position independent code") orelse default;
45
+ const strip = b.option(bool, "strip", "Strip debugging symbols from binary");
46
+ const pic = b.option(bool, "pic", "Force position independent code");
52
47
  const emit_asm = b.option(bool, "emit-asm", "Output .s (assembly code)") orelse false;
53
48
  const emit_ll = b.option(bool, "emit-ll", "Output .ll (LLVM IR)") orelse false;
54
49
 
@@ -62,17 +57,12 @@ pub fn build(b: *std.Build) !void {
62
57
  var repository = std.mem.splitSequence(u8, parsed.value.object.get("repository").?.string, ":");
63
58
  std.debug.assert(std.mem.eql(u8, repository.first(), "github"));
64
59
 
65
- const fast = try std.SemanticVersion.parse("0.12.0-dev.866+3a47bc715");
66
- if (optimize != .Debug and fast.order(builtin.zig_version) == .lt) {
67
- std.log.warn("Release builds are ~10-20% slower than before Zig " ++
68
- "v0.12.0-dev.866+3a47bc715 due to ziglang/zig#17768", .{});
69
- }
70
-
71
60
  const showdown =
72
61
  b.option(bool, "showdown", "Enable Pokémon Showdown compatibility mode") orelse false;
73
62
  const log = b.option(bool, "log", "Enable protocol message logging") orelse false;
74
63
  const chance = b.option(bool, "chance", "Enable update probability tracking") orelse false;
75
64
  const calc = b.option(bool, "calc", "Enable damage calculator support") orelse false;
65
+ const extra = b.option([]const []const u8, "option", "Enable extra option");
76
66
 
77
67
  const options = b.addOptions();
78
68
  options.addOption(?bool, "showdown", showdown);
@@ -80,13 +70,27 @@ pub fn build(b: *std.Build) !void {
80
70
  options.addOption(?bool, "chance", chance);
81
71
  options.addOption(?bool, "calc", calc);
82
72
 
73
+ if (extra) |opts| {
74
+ for (opts) |opt| {
75
+ if (std.mem.indexOfScalar(u8, opt, '=')) |i| {
76
+ options.addOption(?bool, opt[0..i], if (std.mem.eql(u8, opt[i + 1 ..], "true"))
77
+ true
78
+ else if (std.mem.eql(u8, opt[i + 1 ..], "false"))
79
+ false
80
+ else {
81
+ std.log.err("Invalid option: {s}\n", .{opt});
82
+ return error.InvalidOption;
83
+ });
84
+ } else {
85
+ options.addOption(?bool, opt, true);
86
+ }
87
+ }
88
+ }
89
+
83
90
  const name = if (showdown) "pkmn-showdown" else "pkmn";
84
91
 
85
- const pkmn = if (@hasDecl(std.Build, "CreateModuleOptions")) null else b.addModule("pkmn", .{
86
- .root_source_file = if (has_path)
87
- .{ .path = "src/lib/pkmn.zig" }
88
- else
89
- b.path("src/lib/pkmn.zig"),
92
+ const pkmn: *std.Build.Module = b.addModule("pkmn", .{
93
+ .root_source_file = b.path("src/lib/pkmn.zig"),
90
94
  .optimize = optimize,
91
95
  .target = target,
92
96
  .imports = &.{.{ .name = "build_options", .module = options.createModule() }},
@@ -94,33 +98,37 @@ pub fn build(b: *std.Build) !void {
94
98
 
95
99
  var c = false;
96
100
  if (node_headers) |headers| {
97
- const addon = b.fmt("{s}.node", .{name});
98
- const path = if (has_path) .{ .path = "src/lib/node.zig" } else b.path("src/lib/node.zig");
99
- const lib = b.addSharedLibrary(if (force_pic) .{
100
- .name = addon,
101
- .root_source_file = path,
102
- .optimize = optimize,
101
+ const translate_c = b.addTranslateC(.{
102
+ .root_source_file = b.path("src/lib/napi.h"),
103
103
  .target = target,
104
- } else .{
105
- .name = addon,
106
- .root_source_file = path,
107
104
  .optimize = optimize,
108
- .target = target,
109
- .strip = strip,
110
- .pic = pic,
111
105
  });
112
- (if (root_module) lib.root_module else lib).addOptions("build_options", options);
113
- lib.addSystemIncludePath(if (has_path) .{ .path = headers } else b.path(headers));
114
- lib.linkLibC();
106
+ translate_c.addSystemIncludePath(b.path(headers));
107
+ const addon = b.fmt("{s}.node", .{name});
108
+ const path = b.path("src/lib/node.zig");
109
+ const lib = b.addLibrary(.{
110
+ .linkage = .dynamic,
111
+ .name = addon,
112
+ .root_module = b.createModule(.{
113
+ .root_source_file = path,
114
+ .optimize = optimize,
115
+ .target = target,
116
+ .strip = strip,
117
+ .pic = pic,
118
+ }),
119
+ });
120
+ lib.root_module.addOptions("build_options", options);
121
+ lib.root_module.addImport("napi", translate_c.createModule());
122
+ lib.root_module.link_libc = true;
115
123
  if (node_import_lib) |il| {
116
- lib.addObjectFile(if (has_path) .{ .path = il } else b.path(il));
117
- } else if (detect(target).os.tag == .windows) {
118
- try std.io.getStdErr().writeAll("Must provide --node-import-library path on Windows\n");
124
+ lib.root_module.addObjectFile(b.path(il));
125
+ } else if (target.result.os.tag == .windows) {
126
+ var err = std.Io.File.stderr().writer(b.graph.io, &.{});
127
+ try err.interface.writeAll("Must provide --node-import-library path on Windows\n");
119
128
  std.process.exit(1);
120
129
  }
121
130
  lib.linker_allow_shlib_undefined = true;
122
131
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
123
- if (force_pic and pic orelse false) lib.force_pic = pic;
124
132
  // Always emit to build/lib because this is where the driver code expects to find it
125
133
  // TODO: ziglang/zig#2231 - using the following used to work (though was hacky):
126
134
  //
@@ -131,116 +139,48 @@ pub fn build(b: *std.Build) !void {
131
139
  // rename the file ourself in install-pkmn-engine
132
140
  b.installArtifact(lib);
133
141
  } else if (wasm) {
134
- const entry = @hasDecl(Step.Compile, "Entry");
135
- const mode = switch (optimize) {
136
- .ReleaseFast, .ReleaseSafe => .ReleaseSmall,
137
- else => optimize,
138
- };
139
- const freestanding = if (@hasDecl(std.Build, "resolveTargetQuery"))
140
- b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding })
141
- else
142
- ResolvedTarget{ .cpu_arch = .wasm32, .os_tag = .freestanding };
143
- const path = if (has_path) .{ .path = "src/lib/wasm.zig" } else b.path("src/lib/wasm.zig");
144
- const bin = if (entry) bin: {
145
- const exe = b.addExecutable(if (force_pic) .{
146
- .name = name,
147
- .root_source_file = path,
148
- .optimize = mode,
149
- .target = freestanding,
150
- } else .{
151
- .name = name,
142
+ const path = "src/lib/wasm.zig";
143
+ try buildWasm(b, name, path, optimize, strip, pic, wasm_stack_size, null, options);
144
+ } else if (demo) {
145
+ const path = "src/tools/demo.zig";
146
+ const n = if (showdown) "demo-showdown" else "demo";
147
+ const mod = pkmn;
148
+ try buildWasm(b, n, path, optimize, strip, pic, wasm_stack_size, mod, options);
149
+ } else if (dynamic) {
150
+ const path = b.path("src/lib/c.zig");
151
+ const lib = b.addLibrary(.{
152
+ .linkage = .dynamic,
153
+ .name = name,
154
+ .root_module = b.createModule(.{
152
155
  .root_source_file = path,
153
- .optimize = mode,
154
- .target = freestanding,
156
+ .optimize = optimize,
157
+ .target = target,
155
158
  .strip = strip,
156
159
  .pic = pic,
157
- });
158
- (if (root_module) exe.root_module else exe).export_symbol_names = &[_][]const u8{
159
- "SHOWDOWN",
160
- "LOG",
161
- "CHANCE",
162
- "CALC",
163
- "GEN1_CHOICES_SIZE",
164
- "GEN1_LOGS_SIZE",
165
- };
166
- exe.entry = .disabled;
167
- break :bin exe;
168
- } else bin: {
169
- const lib = b.addSharedLibrary(.{
170
- .name = name,
171
- .root_source_file = path,
172
- .optimize = mode,
173
- .target = freestanding,
174
- });
175
- lib.rdynamic = true;
176
- break :bin lib;
177
- };
178
- bin.stack_size = wasm_stack_size;
179
- if (@hasDecl(@TypeOf(bin.*), "strip")) bin.strip = strip orelse false;
180
- if (force_pic and pic orelse false) bin.force_pic = pic;
181
- (if (root_module) bin.root_module else bin).addOptions("build_options", options);
182
-
183
- const opt = b.findProgram(&.{"wasm-opt"}, &.{"./node_modules/.bin"}) catch null;
184
- if (optimize != .Debug and opt != null) {
185
- const out = b.fmt("build/lib/{s}.wasm", .{name});
186
- const sh = b.addSystemCommand(&.{ opt.?, "-O4" });
187
- sh.addArtifactArg(bin);
188
- sh.addArg("-o");
189
- if (@hasDecl(@TypeOf(sh.*), "addFileSourceArg")) {
190
- sh.addFileSourceArg(if (has_path) .{ .path = out } else b.path(out));
191
- } else {
192
- sh.addFileArg(if (has_path) .{ .path = out } else b.path(out));
193
- }
194
- b.getInstallStep().dependOn(&sh.step);
195
- if (!entry) b.installArtifact(bin);
196
- } else if (entry) {
197
- b.getInstallStep().dependOn(&b.addInstallArtifact(bin, .{
198
- .dest_dir = .{ .override = std.Build.InstallDir{ .lib = {} } },
199
- }).step);
200
- }
201
- } else if (dynamic) {
202
- const path = if (has_path) .{ .path = "src/lib/c.zig" } else b.path("src/lib/c.zig");
203
- const lib = b.addSharedLibrary(if (force_pic) .{
204
- .name = name,
205
- .root_source_file = path,
206
- .version = try std.SemanticVersion.parse(version),
207
- .optimize = optimize,
208
- .target = target,
209
- } else .{
210
- .name = name,
211
- .root_source_file = path,
212
- .version = try std.SemanticVersion.parse(version),
213
- .optimize = optimize,
214
- .target = target,
215
- .strip = strip,
216
- .pic = pic,
160
+ }),
217
161
  });
218
- (if (root_module) lib.root_module else lib).addOptions("build_options", options);
219
- lib.addIncludePath(if (has_path) .{ .path = "src/include" } else b.path("src/include"));
162
+ lib.root_module.addOptions("build_options", options);
163
+ lib.root_module.addIncludePath(b.path("src/include"));
220
164
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
221
- if (force_pic and pic orelse false) lib.force_pic = pic;
222
165
  b.installArtifact(lib);
223
166
  c = true;
224
167
  } else {
225
- const path = if (has_path) .{ .path = "src/lib/c.zig" } else b.path("src/lib/c.zig");
226
- const lib = b.addStaticLibrary(if (force_pic) .{
168
+ const path = b.path("src/lib/c.zig");
169
+ const lib = b.addLibrary(.{
170
+ .linkage = .static,
227
171
  .name = name,
228
- .root_source_file = path,
229
- .optimize = optimize,
230
- .target = target,
231
- } else .{
232
- .name = name,
233
- .root_source_file = path,
234
- .optimize = optimize,
235
- .target = target,
236
- .strip = strip,
237
- .pic = pic,
172
+ .root_module = b.createModule(.{
173
+ .root_source_file = path,
174
+ .optimize = optimize,
175
+ .target = target,
176
+ .strip = strip,
177
+ .pic = pic,
178
+ }),
238
179
  });
239
- (if (root_module) lib.root_module else lib).addOptions("build_options", options);
240
- lib.addIncludePath(if (has_path) .{ .path = "src/include" } else b.path("src/include"));
180
+ lib.root_module.addOptions("build_options", options);
181
+ lib.root_module.addIncludePath(b.path("src/include"));
241
182
  lib.bundle_compiler_rt = true;
242
183
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
243
- if (force_pic and pic orelse false) lib.force_pic = pic;
244
184
  if (emit_asm) {
245
185
  b.getInstallStep().dependOn(&b.addInstallFileWithDir(
246
186
  lib.getEmittedAsm(),
@@ -260,22 +200,25 @@ pub fn build(b: *std.Build) !void {
260
200
  }
261
201
 
262
202
  if (c) {
263
- const path =
264
- if (has_path) .{ .path = "src/include/pkmn.h" } else b.path("src/include/pkmn.h");
203
+ const path = b.path("src/include/pkmn.h");
265
204
  const header = b.addInstallFileWithDir(path, .header, "pkmn.h");
266
205
  b.getInstallStep().dependOn(&header.step);
267
206
 
268
207
  const pc = b.fmt("lib{s}.pc", .{name});
269
- const file = try std.fs.path.relative(
208
+ const cwd = try std.process.getCwdAlloc(b.allocator);
209
+ const file = try std.Io.Dir.path.relative(
270
210
  b.allocator,
271
211
  try std.process.getCwdAlloc(b.allocator),
212
+ &b.graph.environ_map,
213
+ cwd,
272
214
  try b.cache_root.join(b.allocator, &.{pc}),
273
215
  );
274
- const pkgconfig_file = try std.fs.cwd().createFile(file, .{});
216
+ const pkgconfig_file = try std.Io.Dir.cwd().createFile(b.graph.io, file, .{});
217
+ defer pkgconfig_file.close(b.graph.io);
275
218
 
276
219
  const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
277
- const writer = pkgconfig_file.writer();
278
- try writer.print(
220
+ var writer = pkgconfig_file.writer(b.graph.io, &.{});
221
+ try writer.interface.print(
279
222
  \\prefix={0s}/{1s}
280
223
  \\includedir=${{prefix}}/include
281
224
  \\libdir=${{prefix}}/lib
@@ -287,12 +230,11 @@ pub fn build(b: *std.Build) !void {
287
230
  \\Cflags: -I${{includedir}}
288
231
  \\Libs: -L${{libdir}} -l{2s}
289
232
  , .{ dirname, b.install_path, name, repository.next().?, description, version });
290
- defer pkgconfig_file.close();
291
233
 
292
234
  b.installFile(file, b.fmt("share/pkgconfig/{s}", .{pc}));
293
235
  }
294
236
 
295
- const config = .{
237
+ const config: Config = .{
296
238
  .target = target,
297
239
  .optimize = optimize,
298
240
  .pic = pic,
@@ -303,7 +245,7 @@ pub fn build(b: *std.Build) !void {
303
245
  // TODO: tests can be run multiple times due to @imports
304
246
  const tests = TestStep.create(b, options, config);
305
247
 
306
- var exes = std.ArrayList(*Step.Compile).init(b.allocator);
248
+ var exes: ArrayList(*std.Build.Step.Compile) = .empty;
307
249
  const tools: ToolConfig = .{
308
250
  .options = .{
309
251
  .showdown = showdown,
@@ -350,14 +292,92 @@ pub fn build(b: *std.Build) !void {
350
292
  }
351
293
  }
352
294
 
295
+ fn buildWasm(
296
+ b: *std.Build,
297
+ name: []const u8,
298
+ root_src_file: []const u8,
299
+ optimize: std.builtin.OptimizeMode,
300
+ strip: ?bool,
301
+ pic: ?bool,
302
+ wasm_stack_size: u64,
303
+ import: ?*std.Build.Module,
304
+ options: anytype,
305
+ ) !void {
306
+ const mode = switch (optimize) {
307
+ .ReleaseFast, .ReleaseSafe => .ReleaseSmall,
308
+ else => optimize,
309
+ };
310
+ // https://webassembly.org/features/
311
+ var features = std.Target.wasm.featureSet(&.{
312
+ .atomics,
313
+ .bulk_memory,
314
+ // .exception_handling,
315
+ .extended_const,
316
+ // .half_precision,
317
+ // .multimemory,
318
+ .mutable_globals,
319
+ .nontrapping_fptoint,
320
+ .reference_types,
321
+ // .relaxed_simd,
322
+ .sign_ext,
323
+ .simd128,
324
+ // .tail_call,
325
+ });
326
+ if (builtin.zig_version.order(try std.SemanticVersion.parse("0.12.0")) != .lt) {
327
+ features.addFeature(@intFromEnum(std.Target.wasm.Feature.multivalue));
328
+ }
329
+ const freestanding = b.resolveTargetQuery(.{
330
+ .cpu_arch = .wasm32,
331
+ .os_tag = .freestanding,
332
+ .cpu_features_add = features,
333
+ });
334
+ const path = b.path(root_src_file);
335
+ const exe = b.addExecutable(.{
336
+ .name = name,
337
+ .root_module = b.createModule(.{
338
+ .root_source_file = path,
339
+ .optimize = mode,
340
+ .target = freestanding,
341
+ .strip = strip,
342
+ .pic = pic,
343
+ }),
344
+ });
345
+
346
+ var file = try std.Io.Dir.cwd().openFile(b.graph.io, root_src_file, .{});
347
+ defer file.close(b.graph.io);
348
+ var reader = file.reader(b.graph.io, &.{});
349
+ const bytes = try reader.interface.allocRemaining(b.allocator, .unlimited);
350
+ exe.root_module.export_symbol_names = try exports(b, bytes);
351
+ exe.entry = .disabled;
352
+
353
+ if (import) |i| {
354
+ exe.root_module.addImport("pkmn", i);
355
+ }
356
+ exe.stack_size = wasm_stack_size;
357
+ exe.root_module.addOptions("build_options", options);
358
+
359
+ const opt = b.findProgram(&.{"wasm-opt"}, &.{"./node_modules/.bin"}) catch null;
360
+ if (optimize != .Debug and opt != null) {
361
+ const out = b.fmt("build/lib/{s}.wasm", .{name});
362
+ const sh = b.addSystemCommand(&.{ opt.?, "--enable-bulk-memory", "--enable-simd", "-O4" });
363
+ sh.addArtifactArg(exe);
364
+ sh.addArg("-o");
365
+ sh.addFileArg(b.path(out));
366
+ b.getInstallStep().dependOn(&sh.step);
367
+ } else {
368
+ b.getInstallStep().dependOn(&b.addInstallArtifact(exe, .{
369
+ .dest_dir = .{ .override = std.Build.InstallDir{ .lib = {} } },
370
+ }).step);
371
+ }
372
+ }
373
+
353
374
  fn maybeStrip(
354
375
  b: *std.Build,
355
- artifact: *Step.Compile,
376
+ artifact: *std.Build.Step.Compile,
356
377
  step: *std.Build.Step,
357
378
  strip: ?bool,
358
379
  cmd: ?[]const u8,
359
380
  ) void {
360
- if (@hasDecl(@TypeOf(artifact.*), "strip")) artifact.strip = strip orelse false;
361
381
  if (!(strip orelse false) or cmd == null) return;
362
382
  // Using `strip -r -u` for dynamic libraries is supposed to work on macOS but doesn't...
363
383
  const mac = builtin.os.tag == .macos;
@@ -370,7 +390,7 @@ fn maybeStrip(
370
390
  }
371
391
 
372
392
  const Config = struct {
373
- target: ResolvedTarget,
393
+ target: std.Build.ResolvedTarget,
374
394
  optimize: std.builtin.OptimizeMode,
375
395
  pic: ?bool,
376
396
  strip: ?bool,
@@ -381,7 +401,7 @@ const TestStep = struct {
381
401
  step: std.Build.Step,
382
402
  build: bool,
383
403
 
384
- pub fn create(b: *std.Build, options: *Step.Options, config: Config) *TestStep {
404
+ pub fn create(b: *std.Build, options: *std.Build.Step.Options, config: Config) *TestStep {
385
405
  const coverage = b.option([]const u8, "test-coverage", "Generate test coverage");
386
406
  const test_filter =
387
407
  b.option([]const u8, "test-filter", "Skip tests that do not match filter");
@@ -390,28 +410,21 @@ const TestStep = struct {
390
410
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Run all tests", .owner = b });
391
411
  self.* = .{ .step = step, .build = test_filter == null };
392
412
 
393
- const path = if (has_path) .{ .path = "src/lib/test.zig" } else b.path("src/lib/test.zig");
394
- const tests = b.addTest(if (force_pic) .{
395
- .root_source_file = path,
396
- .optimize = config.optimize,
397
- .target = config.target,
398
- .filter = test_filter,
399
- } else .{
400
- .root_source_file = path,
401
- .optimize = config.optimize,
402
- .target = config.target,
403
- .filter = test_filter,
404
- .single_threaded = true,
405
- .strip = config.strip,
406
- .pic = config.pic,
413
+ const path = b.path("src/lib/test.zig");
414
+ const tests = b.addTest(.{
415
+ .root_module = b.createModule(.{
416
+ .root_source_file = path,
417
+ .optimize = config.optimize,
418
+ .target = config.target,
419
+ .single_threaded = true,
420
+ .strip = config.strip,
421
+ .pic = config.pic,
422
+ }),
423
+ .filters = if (test_filter) |filter| &.{filter} else &.{},
407
424
  });
408
- (if (root_module) tests.root_module else tests).addOptions("build_options", options);
425
+ tests.root_module.addOptions("build_options", options);
409
426
 
410
427
  maybeStrip(b, tests, &tests.step, config.strip, config.cmd);
411
- if (force_pic) {
412
- tests.single_threaded = true;
413
- if (config.pic orelse false) tests.force_pic = config.pic;
414
- }
415
428
  if (coverage) |c| {
416
429
  tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
417
430
  }
@@ -428,48 +441,36 @@ const ToolConfig = struct {
428
441
  tool: struct {
429
442
  tests: ?*TestStep,
430
443
  name: ?[]const u8 = null,
431
- exes: *std.ArrayList(*Step.Compile),
444
+ exes: *ArrayList(*std.Build.Step.Compile),
432
445
  },
433
446
  };
434
447
 
435
- fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*Step.Run {
436
- std.fs.cwd().access(path, .{}) catch |err| switch (err) {
437
- error.FileNotFound => return null,
438
- else => |e| return e,
439
- };
448
+ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.Run {
449
+ if (!try exists(b, path)) return null;
440
450
  var name = config.tool.name orelse std.fs.path.basename(path);
441
451
  const index = std.mem.lastIndexOfScalar(u8, name, '.');
442
452
  if (index) |i| name = name[0..i];
443
453
  if (config.options.showdown orelse false) name = b.fmt("{s}-showdown", .{name});
444
454
 
445
- const exe = b.addExecutable(if (force_pic) .{
446
- .name = name,
447
- .root_source_file = if (has_path) .{ .path = path } else b.path(path),
448
- .target = config.general.target,
449
- .optimize = config.general.optimize,
450
- } else .{
455
+ const exe = b.addExecutable(.{
451
456
  .name = name,
452
- .root_source_file = if (has_path) .{ .path = path } else b.path(path),
453
- .target = config.general.target,
454
- .optimize = config.general.optimize,
455
- .single_threaded = true,
456
- .strip = config.general.strip,
457
- .pic = config.general.pic,
457
+ .root_module = b.createModule(.{
458
+ .root_source_file = b.path(path),
459
+ .target = config.general.target,
460
+ .optimize = config.general.optimize,
461
+ .strip = config.general.strip,
462
+ .pic = config.general.pic,
463
+ }),
458
464
  });
459
465
 
460
- if (root_module) {
461
- const import = if (config.module) |m| m else module(b, config.options);
462
- exe.root_module.addImport("pkmn", import);
463
- } else {
464
- exe.addModule("pkmn", module(b, config.options));
466
+ const import = if (config.module) |m| m else module(b, config.options);
467
+ exe.root_module.addImport("pkmn", import);
468
+ if (config.general.target.result.os.tag == .windows) {
469
+ exe.root_module.linkSystemLibrary("advapi32", .{});
465
470
  }
466
471
 
467
- if (force_pic) {
468
- exe.single_threaded = true;
469
- if (config.general.pic orelse false) exe.force_pic = config.general.pic;
470
- }
471
472
  if (config.tool.tests) |ts| ts.step.dependOn(&exe.step);
472
- config.tool.exes.append(exe) catch @panic("OOM");
473
+ config.tool.exes.append(b.allocator, exe) catch @panic("OOM");
473
474
 
474
475
  const run = b.addRunArtifact(exe);
475
476
  maybeStrip(b, exe, &run.step, config.general.strip, config.general.cmd);
@@ -478,10 +479,18 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*Step.Run {
478
479
  return run;
479
480
  }
480
481
 
482
+ fn exists(b: *std.Build, path: []const u8) !bool {
483
+ std.Io.Dir.cwd().access(b.graph.io, path, .{}) catch |err| switch (err) {
484
+ error.FileNotFound => return false,
485
+ else => |e| return e,
486
+ };
487
+ return true;
488
+ }
489
+
481
490
  const ToolsStep = struct {
482
491
  step: std.Build.Step,
483
492
 
484
- pub fn create(b: *std.Build, exes: *std.ArrayList(*Step.Compile)) *ToolsStep {
493
+ pub fn create(b: *std.Build, exes: *ArrayList(*std.Build.Step.Compile)) *ToolsStep {
485
494
  const self = b.allocator.create(ToolsStep) catch @panic("OOM");
486
495
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Install tools", .owner = b });
487
496
  self.* = .{ .step = step };
@@ -490,7 +499,20 @@ const ToolsStep = struct {
490
499
  }
491
500
  };
492
501
 
493
- fn detect(target: anytype) std.Target {
494
- if (@hasField(@TypeOf(target), "result")) return target.result;
495
- return (std.zig.system.NativeTargetInfo.detect(target) catch unreachable).target;
502
+ pub fn exports(b: *std.Build, bytes: []const u8) ![][]const u8 {
503
+ var symbols: ArrayList([]const u8) = .empty;
504
+
505
+ var it = std.mem.splitSequence(u8, bytes, "export ");
506
+ _ = it.next();
507
+ while (it.next()) |s| {
508
+ if (std.mem.startsWith(u8, s, "const ")) {
509
+ const i = std.mem.indexOf(u8, s[6..], " ").?;
510
+ try symbols.append(b.allocator, s[6 .. 6 + i]);
511
+ } else { // "fn "
512
+ const i = std.mem.indexOf(u8, s[3..], "(").?;
513
+ try symbols.append(b.allocator, s[3 .. 3 + i]);
514
+ }
515
+ }
516
+
517
+ return symbols.items;
496
518
  }