Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ pm-stack/aggregator/events.json
/zig-out/
pkgs/id/examples/zig-sdk/.zig-cache/
pkgs/id/examples/zig-sdk/zig-out/
pkgs/id/examples/zig-counter/.zig-cache/
pkgs/id/examples/zig-counter/zig-out/

# mcpx tooling run from the checkout writes its schema cache in-tree
# (.cache/mcpx/schemas-<hash>.json); untracked, it made pm-git-sync's
Expand Down
40 changes: 40 additions & 0 deletions pkgs/id/examples/zig-counter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Zig counter world

A tiny pure Zig world program for `id`'s multiplayer service — the
F12.8 (#12596) twin of `../roc-counter` and `../rust-counter`, built
on the Zig guest SDK in `../zig-sdk`. The host owns the authoritative
state; guests send `inc`, and every participant sees the returned
`count=N` presentation. It demonstrates the SDK's screen helper (the
same boxed 20x3 screen JSON as the Roc and Rust programs), the
`records` export encoded with the SDK's json helper, and
snapshot/restore.

## Build

Requires the pinned Zig 0.16.0 (enforced by
`../zig-sdk/zig-toolchain.sh`):

```sh
ZIG=/path/to/zig ./build.sh # or put Zig 0.16.0 on PATH first
```

`build.sh` runs the host tests, builds the wasm32-freestanding
module, and refreshes the checked-in `counter.wasm`, which the host's
F12.7 conformance suite (`pkgs/id/crates/id-worlds/src/sandbox.rs`)
can load as the `zig-counter` fixture — no Zig toolchain is needed
to run the suite.

## Run a hosted world

```sh
id serve --world --world-admin-token "$WORLD_ADMIN" --world-module counter.wasm
```

The module has no Wasm imports and exports the full Plaza ABI v1
surface (see `../roc-world/ABI.md`): `memory`, `plaza_alloc`,
`plaza_free`, `plaza_init`, `plaza_update`, `plaza_view`,
`plaza_layout`, `plaza_records`, `plaza_wants`, `plaza_snapshot`,
`plaza_restore`, `plaza_out_len`, `plaza_abi_version`, and the
`plaza_error_ptr`/`plaza_error_len` diagnostic pair. Adding
`zig-counter` to the conformance suite's module list is the
remaining F12.7 step on #12596.
36 changes: 36 additions & 0 deletions pkgs/id/examples/zig-counter/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bash
# Build the Zig counter guest (#12596) and refresh the checked-in
# module the host conformance suite loads. Runs the host tests,
# builds the wasm32-freestanding module with the pinned Zig (the
# version guard is ../zig-sdk/zig-toolchain.sh), and copies the
# result to counter.wasm.
#
# Usage: build.sh
# Accepts no positional arguments.
set -euo pipefail

usage() {
echo "Usage: build.sh" >&2
}

if [[ $# -ne 0 ]]; then
usage
echo "Error: unaccounted argument(s): $*" >&2
exit 1
fi

cd "$(dirname "${BASH_SOURCE[0]}")"

ID_ZIG_BIN="${ZIG:-zig}"

if ! command -v "$ID_ZIG_BIN" >/dev/null 2>&1; then
echo "zig-counter build: the pinned Zig compiler is not on PATH." >&2
echo "zig-counter build: install Zig $(cat ../../nix/zig-version) (see pkgs/id/nix/zig-version) or set ZIG=/path/to/zig." >&2
exit 1
fi

bash ../zig-sdk/zig-toolchain.sh
"$ID_ZIG_BIN" build test
"$ID_ZIG_BIN" build
cp zig-out/bin/counter.wasm counter.wasm
echo "wrote counter.wasm"
58 changes: 58 additions & 0 deletions pkgs/id/examples/zig-counter/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const std = @import("std");

fn sdkModule(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) *std.Build.Module {
const dep = b.dependency("zig_sdk", .{
.target = target,
.optimize = optimize,
});
return dep.module("zig-sdk");
}

pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseSmall });
const wasm_target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
.abi = .none,
});

// The guest module: an import-free wasm32-freestanding executable
// with no entry point; the `plaza_*` exports in src/main.zig are
// the whole surface, and linear memory is exported for the host.
const guest_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = wasm_target,
.optimize = optimize,
.strip = true,
});
guest_mod.addImport("zig-sdk", sdkModule(b, wasm_target, optimize));

const guest = b.addExecutable(.{
.name = "counter",
.root_module = guest_mod,
});
guest.entry = .disabled;
guest.export_memory = true;
// Without an entry point the linker would GC the `plaza_*`
// exports as unreferenced; rdynamic roots every global symbol
// (the export fns are the only public globals in the module).
guest.rdynamic = true;
b.installArtifact(guest);

// Host tests for the world program (the rust-counter suite's
// mirror): the pure logic plus the screen/json documents.
const host_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = b.graph.host,
});
host_mod.addImport("zig-sdk", sdkModule(b, b.graph.host, .Debug));

const test_step = b.step("test", "Run the Zig counter world tests");
const unit_tests = b.addTest(.{ .root_module = host_mod });
const run = b.addRunArtifact(unit_tests);
test_step.dependOn(&run.step);
}
10 changes: 10 additions & 0 deletions pkgs/id/examples/zig-counter/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.{
.name = .zig_counter,
.version = "0.0.1",
.minimum_zig_version = "0.16.0",
.dependencies = .{
.zig_sdk = .{ .path = "../zig-sdk" },
},
.fingerprint = 0xb2b2d220e523eae3,
.paths = .{ "build.zig", "build.zig.zon", "src" },
}
Binary file added pkgs/id/examples/zig-counter/counter.wasm
Binary file not shown.
Loading