diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-21 02:30:14 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-21 02:52:47 -0300 |
| commit | dea2920233bbb64b20f7ccebd42ac5beab14033d (patch) | |
| tree | 4d1622bffae6fbf653764ea33cb038afd6f02754 /build.zig | |
| parent | ee5b6aa3ef0bf398c5381498af40fa93d20cf0b3 (diff) | |
| download | pardes-dea2920233bbb64b20f7ccebd42ac5beab14033d.tar.gz pardes-dea2920233bbb64b20f7ccebd42ac5beab14033d.zip | |
library: a browser's module is pardes's build's to make: build.zig exports `wasmModule(b, .{ .language = "rust" })` (the user's ruling: the self-contained module is exposed to the consumer's build.zig): wasm32-wasi, a reactor, one language's grammar and what it needs with tree-sitter's runtime and the highlighter, or with no language the patch view alone; its exports are abi/wasm.zig's (beside the other boundaries: a module of its own whose few globals are the page's, not a core's), the same in every module: `reserve`/`release` (toilet's, at least one byte so reserve(0) is a pointer), `highlight(path, path_len, text, len)` (toilet's without its diff flag: diffs go to the patch view now), `patch(text, len)` (a 20-byte header: the rows' text, a style byte a byte, and nine-u32 records: kind, old, new (0 for none), code_start, the row's text and its path as offsets, flags bit 0 no newline), `memory`, `_initialize`; only those are exported (export_symbol_names, not rdynamic: tree-sitter's and libc's symbols stay inside), and a panic is one `fd_write` to stderr and a trap (std's handler writes through std.Io and imported 26 more WASI functions): a language's module imports fd_write, fd_seek, fd_close (libc's streams, tree-sitter's debug output), the patch view's none, 24,533 bytes (11,108 gzip; with tree-sitter's runtime in it, 139,337); rust's is 1,290,182 (183,568 gzip; its parser object alone 1,134,394); test/consumer makes both through `wasmModule` and runs them under node (wasm.mjs: a page with every WASI import a stub reads the header and records back: rows, numbers, paths, the changed `*`, a no-newline flag, reserve(0), a highlight), saying so when there is no node; `sizes` installs one module a language of the manifest
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'build.zig')
| -rw-r--r-- | build.zig | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -36,6 +36,12 @@ //! (`@import("pardes").grammar_manifest.all`: each grammar's name, its //! extensions and what it needs), is `grammar_manifest`. //! +//! A browser's module, one call a language (`wasmModule`): +//! +//! const pardes = @import("pardes"); +//! b.installArtifact(pardes.wasmModule(b, .{ .language = "rust" })); +//! b.installArtifact(pardes.wasmModule(b, .{})); // the patch view alone +//! //! Every dependency but tree-sitter's runtime is lazy in the manifest, so //! such a package fetches what it asked for and nothing else. //! `pardes-editor` has `pardes-syntax` in it as `.syntax`: a file belongs @@ -403,6 +409,46 @@ pub fn build(b: *std.Build) void { } } +/// A browser's module, one call a language (the head comment): wasm32-wasi +/// as a reactor (no `main`; `_initialize`, then any export), with +/// `language`'s grammar and what it needs, tree-sitter's runtime and +/// pardes's highlighter; or, with no language, the patch view alone, with +/// no tree-sitter in it. Its exports are abi/wasm.zig's (`memory`, +/// `_initialize`, `reserve`, `release`, `patch`, and with a language +/// `highlight`), the same in every module; docs/typ/building.typ has their +/// layouts and the WASI imports a page stubs. The package's dependency on +/// pardes is named "pardes". A name the manifest does not have fails the +/// configure, as `.languages` does. +pub fn wasmModule(b: *std.Build, options: struct { + language: ?[]const u8 = null, + optimize: std.lang.Optimize = .ReleaseSmall, +}) *std.Build.Step.Compile { + const target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .wasi }); + const pardes = if (options.language) |name| + b.dependency("pardes", .{ .target = target, .optimize = options.optimize, .languages = name }) + else + b.dependency("pardes", .{ .target = target, .optimize = options.optimize, .grammars = .none }); + const exports = b.addOptions(); + exports.addOption(bool, "highlight", options.language != null); + const module = b.addExecutable(.{ + .name = b.fmt("pardes-{s}", .{options.language orelse "patch"}), + .root_module = b.createModule(.{ + .target = target, + .optimize = options.optimize, + .root_source_file = pardes.path("abi/wasm.zig"), + .imports = &.{ + .{ .name = "pardes-syntax", .module = pardes.module("pardes-syntax") }, + .{ .name = "wasm_options", .module = exports.createModule() }, + }, + }), + }); + module.entry = .disabled; + module.wasi_exec_model = .reactor; + // Its own functions alone: tree-sitter's and libc's stay inside. + module.root_module.export_symbol_names = if (options.language != null) &.{ "reserve", "release", "patch", "highlight" } else &.{ "reserve", "release", "patch" }; + return module; +} + /// Installs `artifact` in the prefix and, given `home_bin`, copies it there /// too (`install` unlinks first, so a running binary is replaced). fn installHome(b: *std.Build, artifact: *std.Build.Step.Compile, home_bin: ?[]const u8) void { |
