Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | 8x 8x 8x 8x 8x 8x 8x 8x 105x 105x 38x 7x 6x 6x 2x 6x 6x 6x 5x 6x 6x 222x 33x 33x 33x 34x 34x 34x 33x 602x 602x 602x 4x 598x 123x 472x 292x 292x 198x 289x 129x 129x 129x 129x 128x 129x 129x 1x 126x 129x 12x 12x 12x 10x 8x 129x 129x 84x 84x 45x 45x 53x 4x 49x 90x 90x 90x 287x 16x 16x 254x 43x 43x 43x 43x 43x 43x 383x 208x 47x 208x 175x 175x 168x 89x 89x 79x 40x 40x 175x 287x 94x 94x 94x 16x 16x 16x 94x 78x | import { hexword } from "./hex.js";
import { loadData } from "./loader.js";
import { matchRegions, NoNames, SymbolNames } from "./symbol-names.js";
/** @typedef {import("./symbol-names.js").SymbolSet} SymbolSet */
/** @typedef {import("./symbol-names.js").Region} Region */
export const Format = 1;
const AddressSpace = 0x10000;
const FirstRetryDelayMs = 5000;
const MaxRetryDelayMs = 5 * 60 * 1000;
export const RelativeJsonPath = /^(?:[\w-]+\/)*[\w-][\w.-]*\.json$/;
const NameLinkPlaceholders = ["name", "addr", "place"];
const Placeholder = /\{([^{}]*)\}/g;
const SiteOfTemplate = /^https:\/\/[^/{}]+\//;
export function isHttps(text) {
try {
return new URL(text).protocol === "https:";
} catch {
return false;
}
}
/**
* What the build rejects in a `nameLink` template: it starts with an https site and a slash, and has
* at least one placeholder, every one of them known.
* @returns {string[]}
*/
export function nameLinkProblems(template) {
if (typeof template !== "string") return ["nameLink isn't text"];
const problems = [];
if (!SiteOfTemplate.test(template) || !isHttps(template.replace(Placeholder, "x")))
problems.push(`nameLink is "${template}", which doesn't start with an https site and a slash`);
const placeholders = [...template.matchAll(Placeholder)].map(([, name]) => name);
if (!placeholders.length) problems.push("nameLink has no placeholder, so every name would link to one page");
for (const name of placeholders)
if (!NameLinkPlaceholders.includes(name)) problems.push(`nameLink has {${name}}, which isn't a placeholder`);
if (/[{}]/.test(template.replace(Placeholder, ""))) problems.push("nameLink has a brace outside a placeholder");
return problems;
}
/**
* The https URL of `name`'s place on its author's site, from the set's `nameLink`: undefined if the set
* has none, the template doesn't start with an https site (which no placeholder can then change), or it
* wants a place the set doesn't give the name.
* @param {SymbolSet} set
*/
export function nameUrl(set, name, address) {
if (typeof set.nameLink !== "string" || !SiteOfTemplate.test(set.nameLink)) return undefined;
const values = {
name: encodeURIComponent(name),
addr: hexword(address).toUpperCase(),
place: set.places.get(name),
};
let complete = true;
const url = set.nameLink.replace(Placeholder, (_, placeholder) => {
const value = values[placeholder];
if (typeof value !== "string") complete = false;
return value;
});
return complete && isHttps(url) ? url : undefined;
}
/** "0x1a2c" as a number; only a region's or a scope's end can be 0x10000. */
export function parseAddress(text, what, limit = AddressSpace - 1) {
const digits = /^0x([0-9a-f]{1,5})$/.exec(text)?.[1];
const value = digits === undefined ? NaN : parseInt(digits, 16);
if (!(value <= limit))
throw new Error(`${what} is "${text}", which is not an address up to 0x${limit.toString(16)}`);
return value;
}
function parseBytes(text, what) {
if (!/^(?:[0-9a-f]{2})+$/.test(text)) throw new Error(`${what} is "${text}", which is not lowercase hex bytes`);
return Uint8Array.from(text.match(/../g), (pair) => parseInt(pair, 16));
}
function parseNames(names, what) {
const byAddress = new Map();
for (const [name, address] of Object.entries(names ?? {}))
byAddress.set(parseAddress(address, `${what} ${name}`), name);
return byAddress;
}
/** @returns {Region} */
function parseRegion(name, region) {
const what = `region ${name}`;
const start = parseAddress(region.start, `${what}'s start`);
const end = parseAddress(region.end, `${what}'s end`, AddressSpace);
if (end <= start) throw new Error(`${what} ends before it starts`);
const anchors = (region.anchors ?? []).map((anchor, i) => ({
at: parseAddress(anchor.at, `${what}'s anchor ${i}`),
bytes: parseBytes(anchor.bytes, `${what}'s anchor ${i}'s bytes`),
}));
const minAnchors = region.minAnchors ?? 1;
if (!Number.isInteger(minAnchors) || minAnchors < 1)
throw new Error(`${what}'s minAnchors is ${minAnchors}, not a whole number of at least 1`);
const scopes = (region.scopes ?? []).map((scope, i) => parseScope(scope, `${what}'s scope ${i}`, start, end));
return { name, start, end, minAnchors, anchors, symbols: parseNames(region.symbols, `${what}'s symbol`), scopes };
}
/** @returns {import("./symbol-names.js").Scope} */
function parseScope(scope, what, regionStart, regionEnd) {
const start = parseAddress(scope.start, `${what}'s start`);
const end = parseAddress(scope.end, `${what}'s end`, AddressSpace);
if (end <= start) throw new Error(`${what} ends before it starts`);
if (start < regionStart || end > regionEnd) throw new Error(`${what} isn't inside its region`);
return { start, end, symbols: parseNames(scope.symbols, `${what}'s symbol`) };
}
function parseRegions(regions) {
return Object.entries(regions ?? {}).map(([name, region]) => parseRegion(name, region));
}
function checkFormat(json, what) {
if (json?.format !== Format) throw new Error(`${what} has format ${json?.format}, not ${Format}`);
}
/**
* A set file as the debugger uses it. Throws on anything it can't read; the build's checks are
* stricter (tools/symbols/build-index.js in https://github.com/mattgodbolt/beeb-meta-data).
* @returns {SymbolSet}
*/
export function parseSet(json) {
checkFormat(json, "the set");
return {
title: json.title,
licence: json.licence,
source: json.source,
notice: json.notice,
home: json.home,
nameLink: json.nameLink,
places: new Map(Object.entries(json.places ?? {})),
system: json.system === true,
globals: parseNames(json.globals, "global"),
workspace: parseNames(json.workspace, "workspace name"),
regions: parseRegions(json.regions),
};
}
/**
* The index's entries, each a set's location, relative to the index, and its regions without
* their symbols.
* @returns {{url: string, regions: Region[]}[]}
*/
export function parseIndex(json) {
checkFormat(json, "the index");
return json.sets.map((entry, i) => {
if (!RelativeJsonPath.test(entry.url))
throw new Error(`the index's set ${i} is at "${entry.url}", which is not a JSON file beside the index`);
return { url: entry.url, regions: parseRegions(entry.regions) };
});
}
async function loadJson(url) {
return JSON.parse(new TextDecoder().decode(await loadData(url)));
}
/** One file's fetches: at most one at a time, and after a failure, none until a delay that doubles each time. */
class Fetches {
constructor() {
this.inFlight = false;
this.failures = 0;
this.retryAt = 0;
}
ready(now) {
return !this.inFlight && now >= this.retryAt;
}
failed(now) {
this.retryAt = now + Math.min(FirstRetryDelayMs * 2 ** this.failures, MaxRetryDelayMs);
this.failures++;
}
}
/**
* The symbol sets under a base URL: its `index.json`, fetched the first time names are wanted, and
* each set it lists, fetched once its regions match. Dispatches "loaded" when a fetch brings
* something new, since the names for memory that hasn't changed may be different now.
*/
export class SymbolSets extends EventTarget {
/**
* @param {object} options
* @param {string | URL} options.baseUrl - an absolute URL ending in a slash
* @param {(url: string) => Promise<object>} [options.load] - fetches and parses one JSON file
* @param {() => number} [options.now] - milliseconds, for the delay before a retry
*/
constructor({ baseUrl, load = loadJson, now = () => Date.now() }) {
super();
this._indexUrl = new URL("index.json", baseUrl).href;
this._load = load;
this._now = now;
this._indexFetches = new Fetches();
/** @type {{url: string, regions: Region[], set: SymbolSet | null, fetches: Fetches}[] | null} */
this._entries = null;
}
/**
* The names for memory as `peek` reads it now, from the sets already fetched; starts fetching
* the index, or the sets that match and haven't been fetched yet.
* @param {(address: number) => number} peek
* @returns {SymbolNames}
*/
names(peek) {
if (!this._entries) {
this._fetch(this._indexFetches, this._indexUrl, (json) => {
this._entries = parseIndex(json).map(({ url, regions }) => ({
url: new URL(url, this._indexUrl).href,
regions,
set: null,
fetches: new Fetches(),
}));
});
return NoNames;
}
const matches = [];
for (const [entry, regions] of matchRegions(this._entries, peek)) {
if (entry.set) {
matches.push({ set: entry.set, regions });
continue;
}
this._fetch(entry.fetches, entry.url, (json) => {
entry.set = parseSet(json);
entry.regions = entry.set.regions;
});
}
return matches.length ? new SymbolNames(matches) : NoNames;
}
async _fetch(fetches, url, use) {
if (!fetches.ready(this._now())) return;
fetches.inFlight = true;
try {
use(await this._load(url));
} catch (error) {
fetches.failed(this._now());
console.warn(`Couldn't load the symbols in ${url}, so addresses stay plain for now: ${error.message}`);
return;
} finally {
fetches.inFlight = false;
}
this.dispatchEvent(new Event("loaded"));
}
}
|