Просмотр исходного кода

Update to Zig 0.12.0-dev.271+5cc1831ca

Daniel Sanchez 1 год назад
Родитель
Сommit
906d0260a3
5 измененных файлов с 23 добавлено и 23 удалено
  1. 17 19
      build.zig
  2. BIN
      game.wasm
  3. 4 3
      script.js
  4. 2 1
      src/main.zig
  5. BIN
      zig-out/lib/DodgeBallz.wasm

+ 17 - 19
build.zig

@@ -1,39 +1,37 @@
 const std = @import("std");
 
 const page_size = 65536; // in bytes
-// initial and max memory must correspond to the memory size defined in script.js.
-const wasm_initial_memory = 10 * page_size;
+// Initial and max memory must correspond to the memory size defined in script.js.
+// TODO: This used to be 10 pages. Find out why this needs 17 pages.
+const wasm_initial_memory = 17 * page_size;
 const wasm_max_memory = wasm_initial_memory;
 
 pub fn build(b: *std.build.Builder) void {
-    // Adds the option -Drelease=[bool] to create a release build, which we set to be ReleaseSmall by default.
-    b.setPreferredReleaseMode(.ReleaseSmall);
-    // Standard release options allow the person running `zig build` to select
-    // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
-    const mode = b.standardReleaseOptions();
 
-    const lib = b.addSharedLibrary("game", "src/main.zig", .unversioned);
-    lib.setBuildMode(mode);
-    lib.setTarget(.{
-        .cpu_arch = .wasm32,
-        .os_tag = .freestanding,
-        .abi = .musl,
+    const lib = b.addSharedLibrary(.{
+        .name = "DodgeBallz",
+        .root_source_file = .{ .path = "src/main.zig" },
+        .target = .{
+            .cpu_arch = .wasm32,
+            .os_tag = .freestanding,
+            .abi = .musl,
+        },
+        .optimize = .ReleaseSmall,
     });
-    lib.setOutputDir(".");
 
     // https://github.com/ziglang/zig/issues/8633
+    lib.global_base = 6560;
     lib.import_memory = true; // import linear memory from the environment
     lib.initial_memory = wasm_initial_memory; // initial size of the linear memory (1 page = 64kB)
     lib.max_memory = wasm_initial_memory; // maximum size of the linear memory
-    // lib.global_base = 6560; // offset in linear memory to place global data
 
     // Pass options from the build script to the files being compiled. This is awesome!
     const lib_options = b.addOptions();
     lib.addOptions("build_options", lib_options);
     lib_options.addOption(usize, "memory_size", wasm_max_memory);
 
-    lib.install();
-
-    const step = b.step("game", "Compiles src/main.zig");
-    step.dependOn(&lib.step);
+     // This declares intent for the library to be installed into the standard
+     // location when the user invokes the "install" step (the default step when
+     // running `zig build`).
+     b.installArtifact(lib);
 }


+ 4 - 3
script.js

@@ -66,9 +66,10 @@ function jsConsoleLogVector2D(x, y) {
     console.log("{x: " + x + ", y:" + y + "}");
 }
 
+// See build.zig for reasoning
 var memory = new WebAssembly.Memory({
-    initial: 10 /* pages */,
-    maximum: 10 /* pages */,
+    initial: 17 /* pages */,
+    maximum: 17 /* pages */,
 });
 
 const wasm = {
@@ -91,7 +92,7 @@ const wasm = {
 };
 
 function loadGame() {
-    WebAssembly.instantiateStreaming(fetch("game.wasm"), wasm.imports).then((result) => {
+    WebAssembly.instantiateStreaming(fetch("zig-out/lib/DodgeBallz.wasm"), wasm.imports).then((result) => {
         wasm.exports = result.instance.exports;
         window.addEventListener("keydown", (event) => {
             const key = event.key;

+ 2 - 1
src/main.zig

@@ -13,7 +13,8 @@ export fn game_init(board_width: f32, board_height: f32) void {
     // The imported memory is actually the value of the first element of the memory.
     // To use as an slice, we take the address of the first element and cast it into a slice of bytes.
     // We pass the memory size using build options (see build.zing).
-    var memory_buffer = @ptrCast([*]u8, &memory)[0..build_options.memory_size];
+    var memory_buffer_ptr: [*]u8 = @ptrCast(&memory);
+    var memory_buffer = memory_buffer_ptr[0..build_options.memory_size];
 
     // When the upper bound of memory can be established, FixedBufferAllocator is a great choice.
     fixed_buffer = std.heap.FixedBufferAllocator.init(memory_buffer);

BIN
zig-out/lib/DodgeBallz.wasm