xmachines-eval-quickjs 6.0.0

A JavaScript evaluator (embedded QuickJS) for code nodes in xmachines-rs machines
Documentation
/// <reference path="./globals.d.ts" />
// The JavaScript side of xmachines-eval-quickjs's evaluator: compiles code nodes as
// XState's own evaluators do and calls them with XState's scope. Values
// cross to Rust as JSON text; `enq` and `spawn` call Rust through the bridge.
// Its helpers stay in the function, out of QuickJS's global scope. The
// globals it shares with Rust are declared in globals.d.ts, for the type
// checker.
(() => {
	// oxlint-disable-next-line no-underscore-dangle -- the name the Rust host defines
	const bridge = /** @type {XStatePrelude.Bridge} */ (globalThis.__xstate_bridge);
	// oxlint-disable-next-line no-underscore-dangle -- the name the Rust host defines
	delete globalThis.__xstate_bridge;

	/**
	 * @param {string} op
	 * @param {...unknown} args
	 */
	const call = (op, ...args) => {
		const out = bridge(op, JSON.stringify(args));
		return out === undefined ? undefined : JSON.parse(out);
	};

	// An actor reference as XState persists it ({ "@actor", incarnation }),
	// with the actor's id as a non-enumerable `id`, as live references have.
	/**
	 * @param {Exclude<XStatePrelude.Json, XStatePrelude.Json[]> | undefined} v
	 * @returns {v is XStatePrelude.Ref}
	 */
	// oxlint-disable-next-line unicorn/consistent-function-scoping -- kept out of the global scope
	const isRef = (v) =>
		v !== null &&
		typeof v === "object" &&
		typeof v["@actor"] === "string" &&
		"incarnation" in v &&
		Object.keys(v).length === 2;
	/**
	 * @param {XStatePrelude.Json | undefined} v
	 * @returns {XStatePrelude.Json | undefined}
	 */
	const revive = (v) => {
		if (Array.isArray(v)) v.forEach(revive);
		else if (isRef(v)) {
			const segment = v["@actor"].slice(v["@actor"].lastIndexOf("/") + 1);
			Object.defineProperty(v, "id", {
				value: decodeURIComponent(segment),
				enumerable: false,
			});
		} else if (v !== null && typeof v === "object")
			for (const key of Object.keys(v)) revive(v[key]);
		return v;
	};
	// What the bridge needs of a target: its ref and id.
	/** @param {XStatePrelude.Target} t */
	// oxlint-disable-next-line unicorn/consistent-function-scoping -- kept out of the global scope
	const target = (t) =>
		t !== null && typeof t === "object"
			? { "@actor": t["@actor"], incarnation: t.incarnation, id: t.id }
			: t;

	/**
	 * @param {unknown} action
	 * @param {unknown} params
	 */
	const enq = (action, params) => {
		if (typeof action !== "string") {
			throw new TypeError("enq(action, params): name a registered action as actions.<name>");
		}
		call("action", action, params);
	};
	Object.assign(
		enq,
		/** @satisfies {XStatePrelude.EnqueueActions} */ ({
			raise: (event, options) => void call("raise", event, options),
			emit: (event) => void call("emit", event),
			cancel: (id) => void call("cancel", id),
			log: (...args) => void call("log", ...args),
			sendTo: (to, event, options) => void call("sendTo", target(to), event, options),
			stop: (actor) => void call("stop", target(actor)),
			spawn: (src, options) => ({ id: call("spawn", src, options) }),
			listen: () => {
				throw new Error("enq.listen is not supported by xmachines-rs");
			},
			subscribeTo: () => {
				throw new Error("enq.subscribeTo is not supported by xmachines-rs");
			},
		}),
	);

	// XState's `reject`, in a transition's scope: checks its reasons and
	// returns the rejection result.
	/** @param {unknown} reasons */
	// oxlint-disable-next-line unicorn/consistent-function-scoping -- kept out of the global scope
	const reject = (reasons) => {
		if (
			!Array.isArray(reasons) ||
			reasons.length === 0 ||
			!reasons.every((r) => r !== null && typeof r === "object" && typeof r.type === "string")
		) {
			throw new Error(
				"reject(...) requires a nonempty array of rejection objects with a string type.",
			);
		}
		return { reject: [...reasons] };
	};

	// `actions.name` and `actors.name` are the names themselves.
	const names = new Proxy({}, { get: (_, key) => (typeof key === "string" ? key : undefined) });

	// The result as JSON text (`undefined` for undefined). A function or
	// symbol has no JSON form; as a result only its truthiness can matter.
	/**
	 * @param {unknown} value
	 * @returns {string | undefined}
	 */
	// oxlint-disable-next-line unicorn/consistent-function-scoping -- kept out of the global scope
	const result = (value) => {
		if (value === undefined) return undefined;
		if (typeof value === "function" || typeof value === "symbol") return "true";
		return JSON.stringify(value);
	};

	// XState's scope, its values parsed from JSON text when first read (code
	// usually reads one or two): `context`, `event`, `input`, `params`,
	// `self` (with `_parent`), `children`, `actions`, `actors`, and `enq`
	// and `reject` where XState has them. The values are getters on the prototype, so a
	// spread (`{ ...scope }`) copies only `enq`.
	const TEXTS = Symbol("texts");
	const VALUES = Symbol("values");
	const UNREAD = Symbol("unread");
	// References only need reviving where the text has one.
	/** @param {string} text */
	const parse = (text) =>
		text.includes('"@actor"') ? revive(JSON.parse(text)) : JSON.parse(text);
	/**
	 * @param {Scope | ContextScope} scope
	 * @param {number} i
	 */
	const value = (scope, i) => {
		const values = scope[VALUES];
		const v = values[i];
		if (v !== UNREAD) return v;
		const text = scope[TEXTS][i];
		return (values[i] = text === undefined || text === null ? undefined : parse(text));
	};
	class Scope {
		/**
		 * @param {Array<string | undefined>} texts
		 * @param {number} mode
		 */
		constructor(texts, mode) {
			this[TEXTS] = texts;
			/** @type {unknown[]} */
			this[VALUES] = [UNREAD, UNREAD, UNREAD, UNREAD, UNREAD];
			if (mode & 2) this.enq = enq;
			if (mode & 4) this.reject = reject;
		}
		get context() {
			return value(this, 0);
		}
		get event() {
			return value(this, 1);
		}
		get input() {
			return value(this, 2);
		}
		get params() {
			return value(this, 3);
		}
		// `self`, its parent and the children, fetched when first read: { self, parent?, children? }.
		get self() {
			const refs = this.#refs();
			if (refs.parent && !Object.hasOwn(refs.self, "_parent")) {
				Object.defineProperty(refs.self, "_parent", {
					value: refs.parent,
					enumerable: false,
				});
			}
			return refs.self;
		}
		get children() {
			return this.#refs().children ?? {};
		}
		/** @returns {XStatePrelude.Refs} */
		#refs() {
			const values = this[VALUES];
			return /** @type {XStatePrelude.Refs} */ (
				values[4] !== UNREAD ? values[4] : (values[4] = revive(call("refs")))
			);
		}
		get actions() {
			return names;
		}
		get actors() {
			return names;
		}
	}
	// The root context's scope (XState's context factory): `input`, `self`,
	// `actors` and `spawn`.
	class ContextScope {
		/** @param {Array<string | undefined>} texts */
		constructor(texts) {
			this[TEXTS] = texts;
			/** @type {unknown[]} */
			this[VALUES] = [UNREAD, UNREAD];
		}
		get input() {
			return value(this, 0);
		}
		get self() {
			return value(this, 1);
		}
		get actors() {
			return names;
		}
		/**
		 * @param {unknown} src
		 * @param {unknown} [options]
		 */
		spawn(src, options) {
			return revive(call("spawnContext", src, options));
		}
	}

	/** @type {Map<number, Function>} */
	const fns = new Map();
	// `mode`: 1 for `@code` (a function), plus 2 with `enq`, plus 4 with `reject`.
	/**
	 * @param {number} id
	 * @param {number} mode
	 * @param {Scope | (ContextScope & { enq?: undefined })} scope
	 */
	const invoke = (id, mode, scope) => {
		// The host compiles a node before it runs it.
		const fn = /** @type {Function} */ (fns.get(id));
		return result(mode & 1 ? fn(scope, scope.enq) : fn(scope));
	};
	// oxlint-disable-next-line no-underscore-dangle -- the name the Rust host reads
	globalThis.__xstate = {
		compile(id, kind, source) {
			fns.set(
				id,
				kind === "code"
					? Function(`return (${source});`)()
					: Function("scope", `with (scope) { return (${source}); }`),
			);
		},
		run: (id, mode, context, event, input, params) =>
			invoke(id, mode, new Scope([context, event, input, params], mode)),
		runContext: (id, mode, input, self) => invoke(id, mode, new ContextScope([input, self])),
	};
})();