WebAssembly#

wasm/ is go-tony compiled to WebAssembly, with a JavaScript loader and TypeScript types. Every operation is go-tony's own, so it answers as go-tony and o do: every operator, expressions, and go-tony's normalized encoding. It is held to the whole conformance corpus.

The cost is size and an asynchronous start: the module is about 19.7 MB, 4.6 MB gzipped, most of it go-tony's expression evaluation, which patching links in. For a small library written in TypeScript, without every operation, see TypeScript.

Building#

The package is not published to npm yet. Building it needs Go, at the version the repository's go.work names, and Node:

cd wasm
npm run build   # tony.wasm, from go-tony/wasm, and Go's wasm_exec.js
npm test        # the conformance corpus and the API

It has no dependencies, and it has been run under Node only.

Use#

import { load } from "tony-wasm";

const tony = await load();
const doc = tony.parse("name: web\nreplicas: 2\n");
const next = tony.patch(doc, "replicas: 3\nimage: nginx\n");

tony.encode(next);
// "image: nginx\nname: web\nreplicas: 3\n"

tony.match(next, "replicas: 5\n", { explain: true });
// { matched: false, failures: 1,
//   explanation: 'replicas: value: expected 5, found 3' }

load answers the API. It takes where the module is -- a URL or a path, its bytes, a fetch Response, or a compiled WebAssembly.Module -- and defaults to the tony.wasm beside the package. Each call starts an instance of its own.

A node is an IR node in its JSON form, or Tony text, which is parsed first. An int is a number when it is a safe integer and a bigint past 2^53. irFromJSON and irToJSON read and write IR JSON.

method go-tony
parse(text, {comments}) parse.Parse, with parse.ParseComments
encode(node, {comments}) encode.Encode, with encode.EncodeComments
patch(doc, patch, {comments}) tony.Patch, with mergeop.Comments; null where the patch deletes the document
match(doc, pattern, {explain}) tony.Match; with explain, tony.Explaining and why it failed
trim(match, doc) tony.Trim
diff(from, to, {comments}) tony.DiffWith, with tony.DiffComments; null for no difference
reverse(diff) libdiff.Reverse

An error go-tony reports is thrown as a TonyError carrying its message.

Where it differs from go-tony#

What needs the host differs, since a WebAssembly module has none:

  • !pipe runs a command, and there are no processes, so a patch using it is refused with os/exec's message: exec: "<command>": executable file not found in $PATH;
  • the module is given no environment, so getenv() in an expression answers "".

Loading the module sets globalThis.Go, from the wasm_exec.js of the Go that built it, and sets stubs for globalThis.fs and globalThis.process where they are absent; go-tony reaches no file through them.