Provenance Tracking with Linvail


// Target //

/** @type {(value: unknown) => void} */
const log = /** @type {any} */ (globalThis).log;

/** @type {import("linvail").Library} */
const Linvail = /** @type {any} */ (globalThis).Linvail;

const { is, dir, Map: LinvailMap } = Linvail;

const assert = (/** @type {boolean} */ check) => {
if (!check) throw new Error("Assertion failure");
};

/** @type {(val1: unknown, val2: unknown) => void} */
const same = (val1, val2) => assert(is(val1, val2));

/** @type {(val1: unknown, val2: unknown) => void} */
const diff = (val1, val2) => assert(!is(val1, val2));

const num = 123;

// Provenancial Equality //
same(num, num);
diff(num, 123);

// Inspection //
log(num); // 123 (transparency preservation)
dir(num); // { __inner: 123, __index: <id> }

// Provenance Preservation >> Function //
const identity = (/** @type {unknown} */ x) => x;
same(identity(num), num);
diff(identity(num), 123);

// Provenance Preservation >> Plain Object //

// Instrumentation //

/** @type {import("../../context.d.ts").Context} */
const {
log,
target,
acorn: { parse },
astring: { generate },
aran: { setupile, transpile, retropile },
linvail: { weave, createRuntime },
} = /** @type {any} */ (globalThis).__context;

const advice_global_variable = "__LINVAIL_ADVICE__";
const dir = (/** @type {unknown} */ value) => log(JSON.stringify(value));

const intrinsics = globalThis.eval(generate(setupile({})));
const { advice, library } = createRuntime(intrinsics, { dir });
Reflect.defineProperty(globalThis, advice_global_variable, { value: advice });
Reflect.defineProperty(globalThis, "Linvail", { value: library });
Reflect.defineProperty(globalThis, "log", { value: log });
const root1 = parse(target, { sourceType: "script", ecmaVersion: 2024 });
const root2 = transpile({ path: "main", kind: "eval", root: root1 });
const root3 = weave(root2, { advice_global_variable });
const root4 = retropile(root3);
globalThis.eval(generate(root4));