// Downlevel mode's JSX output, matching ts.transpileModule. The classic runtime (`jsx: "react"`) is // `factory(tag, props | null, ...children)`; the automatic one (`jsx: "react-jsx"`) is `_jsx(tag, props, key)`, // with the children in props, `_jsxs` when they are an array, and `_createElement` from the import source when a // `key` follows a spread. strip.zena parses the JSX and calls these through the jsx* hooks, so only // build/downlevel.wasm links this file. Every inserted text is free of line breaks; a string that spans // lines in the source closes before each line break and continues after it (`"a"+" b"`). import { Memory } from 'zena:memory'; import { delta } from './lex.zena'; import { outReserve, outCommit, outInsert, mapInserted, mapCut, mapAt, mapSrcAt, mapSrcLine, mapSrcCol, mapKey, outEnd, outAt, outTruncate, } from './strip.zena'; import { JSX_ENT, JT_JSX, JT_JSXS, JT_CREATE, JT_FRAGMENT, JT_CHILDREN, JT_EMPTY, JT_IMPORT, JT_IJSX, JT_IJSXS, JT_IFRAGMENT, JT_ICREATE, JT_FROM, JT_RUNTIME, JT_END, JT_P_JSX, JT_P_JSXFRAG, JT_P_RUNTIME, JT_P_SOURCE, JT_CLASSIC, JT_AUTOMATIC, } from './keywords.zena'; // The factory names: the options' (`pragma`), then the file's `@jsx`/`@jsxFrag` comment pragmas, as bytes. // A comment pragma's length is -1 before one is seen and 0 when the first one seen is not an entity name, which // TypeScript ignores in favor of the option (it never looks at a second one). var pr: FixedArray = new FixedArray(128, 0); var prLen: i32 = 0; var factoryAt: i32 = 0; var factoryLen: i32 = 0; var fragAt: i32 = 0; var fragLen: i32 = 0; var cFactoryAt: i32 = 0; var cFactoryLen: i32 = -1; var cFragAt: i32 = 0; var cFragLen: i32 = -1; // The automatic runtime: the option, then the file's `@jsxRuntime` (-1 none, 0 classic, 1 automatic), and the // import source, the option's then the file's `@jsxImportSource`, which also selects the automatic runtime. var autoOpt: boolean = false; var cRuntime: i32 = -1; var srcAt: i32 = 0; var srcLen: i32 = 0; var cSrcAt: i32 = 0; var cSrcLen: i32 = -1; // The automatic runtime's names the file used: U_JSX, U_JSXS, U_FRAGMENT, U_CREATE. var used: i32 = 0; let U_JSX: i32 = 1; let U_JSXS: i32 = 2; let U_FRAGMENT: i32 = 4; let U_CREATE: i32 = 8; // The saved keys, innermost last: their bytes, each one's start in them, and each one's value's source line and // column (keyFrom, two per key), which keyBegin stacks (keyPend) until keyEnd saves the key. var keys: FixedArray = new FixedArray(64, 0); var keysLen: i32 = 0; var keyStarts: FixedArray = new FixedArray(8, 0); var keyFrom: FixedArray = new FixedArray(16, 0); var keyN: i32 = 0; var keyPend: FixedArray = new FixedArray(16, 0); var keyPendN: i32 = 0; // Kept text segments of the JSX text being written: [start, end) pairs. var seg: FixedArray = new FixedArray(64, 0); var segN: i32 = 0; function prPush(c: i32): void { if (prLen == pr.length) { let b = new FixedArray(pr.length * 2, 0); var i = 0; while (i < prLen) { b[i] = pr[i]; i = i + 1; } pr = b; } pr[prLen] = c; prLen = prLen + 1; } // The ABI's pragma(): `a` bytes of the factory, `b` of the fragment factory, then `c` of the import source, at // addr. Starts a stream's factories, so it also forgets the previous stream's comment pragmas and names used. export function pragma(addr: i32, a: i32, b: i32, c: i32, auto: boolean): void { prLen = 0; factoryAt = 0; factoryLen = a; fragAt = a; fragLen = b; srcAt = a + b; srcLen = c; var i = 0; while (i < a + b + c) { prPush(Memory.default.getU8(addr + i)); i = i + 1; } cFactoryLen = -1; cFragLen = -1; autoOpt = auto; cRuntime = -1; cSrcLen = -1; used = 0; keyN = 0; keysLen = 0; keyPendN = 0; } // Whether the file's JSX is written for the automatic runtime. export function auto(): boolean { if (cRuntime >= 0) return cRuntime == 1; return cSrcLen > 0 || autoOpt; } function inByte(p: i32): i32 { return Memory.default.getU8(p + delta); } function isLt(c: i32): boolean { return c == 10 || c == 13; } // ASCII whitespace, as the pragma regex's `\s` sees it. function isSpace(c: i32): boolean { return c == 32 || c == 9 || c == 10 || c == 13 || c == 11 || c == 12; } function isIdByte(c: i32, start: boolean): boolean { if ((c >= 97 && c <= 122) || (c >= 65 && c <= 90) || c == 95 || c == 36 || c >= 0x80) return true; return !start && c >= 48 && c <= 57; } // Input [a, b) is an entity name, `a` or `a.b.c`: what TypeScript's parseIsolatedEntityName accepts. function isEntityName(a: i32, b: i32): boolean { var start = true; var q = a; while (q < b) { let c = inByte(q); if (c == 46) { if (start) return false; start = true; } else { if (!isIdByte(c, start)) return false; start = false; } q = q + 1; } return !start; } // Input [a, b) is the text piece at `at`, ignoring ASCII case when `fold`. function wordIs(a: i32, b: i32, at: i32, fold: boolean): boolean { var i = 0; while (a + i < b) { var c = inByte(a + i); if (fold) c = c | 32; if (c != Memory.default.getU8(at + i)) return false; i = i + 1; } return Memory.default.getU8(at + i) == 126; } // Input [a, b) is a pragma name, ignoring ASCII case: 1 `jsx`, 2 `jsxfrag`, 3 `jsxruntime`, 4 `jsximportsource`, // else 0. function pragmaKind(a: i32, b: i32): i32 { if (wordIs(a, b, JT_P_JSX, true)) return 1; if (wordIs(a, b, JT_P_JSXFRAG, true)) return 2; if (wordIs(a, b, JT_P_RUNTIME, true)) return 3; if (wordIs(a, b, JT_P_SOURCE, true)) return 4; return 0; } // TypeScript's processCommentPragmas over the leading comments [0, end) of the file: in each block comment, // `/@(\S+)(\s+(?:\S.*)?)?$/gm`, where the argument is the first word after the name. The first `@jsx` and // `@jsxFrag` count, and the last `@jsxRuntime` and `@jsxImportSource`. export function pragmas(end: i32): void { var p = 0; if (end >= 2 && inByte(0) == 35 && inByte(1) == 33) { while (p < end && !isLt(inByte(p))) p = p + 1; } while (p + 1 < end) { let c = inByte(p); if (c == 47 && inByte(p + 1) == 42) { var q = p + 2; while (q + 1 < end && !(inByte(q) == 42 && inByte(q + 1) == 47)) q = q + 1; q = q + 2; if (q > end) q = end; commentPragmas(p, q); p = q; } else if (c == 47 && inByte(p + 1) == 47) { while (p < end && !isLt(inByte(p))) p = p + 1; } else { p = p + 1; } } } function commentPragmas(a: i32, b: i32): void { var p = a; while (p < b) { if (inByte(p) != 64) { p = p + 1; continue; } var q = p + 1; while (q < b && !isSpace(inByte(q))) q = q + 1; if (q == p + 1) { p = p + 1; continue; } let kind = pragmaKind(p + 1, q); var r = q; while (r < b && isSpace(inByte(r))) r = r + 1; let arg = r; while (r < b && !isSpace(inByte(r))) r = r + 1; if (arg < r) { if (kind == 1 && cFactoryLen < 0) { cFactoryAt = prLen; cFactoryLen = savePragma(arg, r); } else if (kind == 2 && cFragLen < 0) { cFragAt = prLen; cFragLen = savePragma(arg, r); } else if (kind == 3) { cRuntime = -1; if (wordIs(arg, r, JT_CLASSIC, false)) cRuntime = 0; else if (wordIs(arg, r, JT_AUTOMATIC, false)) cRuntime = 1; } else if (kind == 4) { cSrcAt = prLen; cSrcLen = r - arg; prSave(arg, r); } } while (r < b && !isLt(inByte(r))) r = r + 1; p = r; } } // Copies input [a, b) after the stored names. function prSave(a: i32, b: i32): void { var q = a; while (q < b) { prPush(inByte(q)); q = q + 1; } } // Copies input [a, b) after the stored names and returns its length, or 0 when it is not an entity name. function savePragma(a: i32, b: i32): i32 { if (!isEntityName(a, b)) return 0; prSave(a, b); return b - a; } // --------------------------------------------------------------------------- // Output. function put(c: i32): void { Memory.default.setU8(outReserve(1), c); outCommit(1); } function putIn(a: i32, b: i32): void { var q = a; while (q < b) { put(inByte(q)); q = q + 1; } } function putPr(at: i32, n: i32): void { var i = 0; while (i < n) { put(pr[at + i]); i = i + 1; } } // The line terminators of input [a, b), and nothing else: what keeps the source's lines. function lines(a: i32, b: i32): void { var q = a; while (q < b) { let c = inByte(q); if (isLt(c)) { put(c); } else if (c == 0xe2 && q + 2 < b && inByte(q + 1) == 0x80 && (inByte(q + 2) | 1) == 0xa9) { putIn(q, q + 3); q = q + 2; } q = q + 1; } } function putNull(): void { put(110); put(117); put(108); put(108); } // The text piece at `at` (keywords.zena's JT_*). function tx(at: i32): void { var i = at; while (Memory.default.getU8(i) != 126) { put(Memory.default.getU8(i)); i = i + 1; } } // `factory(`, then for a fragment `fragmentFactory,null`. export function open(frag: boolean): void { if (cFactoryLen > 0) putPr(cFactoryAt, cFactoryLen); else putPr(factoryAt, factoryLen); put(40); if (!frag) return; if (cFragLen > 0) putPr(cFragAt, cFragLen); else putPr(fragAt, fragLen); put(44); putNull(); } // Input [a, b), a tag or attribute name, as a string. export function quoted(a: i32, b: i32): void { put(34); mapAt(a); putIn(a, b); put(34); } // `ns:name` as one string; any line terminators between the two parts follow it. The name part, input [a2, b2), // is written on the prefix's line, so a2 is the source of the string's second half for a source map record. export function ns(a1: i32, b1: i32, a2: i32, b2: i32): void { put(34); mapAt(a1); putIn(a1, b1); put(58); putIn(a2, b2); put(34); lines(b1, a2); } // strip.zena's JX_* pieces. export function piece(k: i32): void { if (k >= 10) { autoPiece(k); return; } if (k == 0) { put(44); } else if (k == 1) { put(44); put(123); } else if (k == 2) { put(125); } else if (k == 3) { put(44); putNull(); } else if (k == 4) { put(41); } else if (k == 5) { put(58); } else if (k == 6) { put(58); put(116); put(114); put(117); put(101); } else if (k == 7) { put(44); put(40); } else if (k == 8) { put(40); } else { put(59); } } // --------------------------------------------------------------------------- // The automatic runtime. An element's callee is decided by its children, so strip.zena writes a six-byte callee // slot (`_jsx (`) at the element's start, holds the output from there under an anchor, and fills the slot in place // once a second child (or a spread one) or the end shows which callee it is. Only `_createElement(`, which is // longer than the slot, still opens room after it. A `key` is cut from the held output and written after the // props. function autoPiece(k: i32): void { if (k == 10) { tx(JT_CHILDREN); } else if (k == 11) { tx(JT_EMPTY); } else if (k == 12) { put(91); } else if (k == 13) { put(93); } else if (k == 14) { used = used | U_FRAGMENT; tx(JT_FRAGMENT); } else if (k == 15) { put(116); put(114); put(117); put(101); } else { tx(JT_JSX); } } // Fills the callee slot at output offset o with callee k (0 `_jsx (`, already there; 1 `_jsxs(`; 2 // `_createElement(`, which opens nine more bytes after the slot), and first, when kids is not -1, the blank at // kids (after o) with the `[` that opens the children. export function callee(o: i32, kids: i32, k: i32): void { if (kids >= 0) Memory.default.setU8(outAt(kids), 91); if (k == 0) { used = used | U_JSX; return; } var at = JT_JSXS; var a = outAt(o); if (k == 2) { at = JT_CREATE; a = outInsert(o + 6, 9) - 6; mapInserted(o + 6, 9); } var i = 0; while (Memory.default.getU8(at + i) != 126) { Memory.default.setU8(a + i, Memory.default.getU8(at + i)); i = i + 1; } if (k == 1) used = used | U_JSXS; else used = used | U_CREATE; } function keyByte(c: i32): void { if (keysLen == keys.length) { let b = new FixedArray(keys.length * 2, 0); var i = 0; while (i < keysLen) { b[i] = keys[i]; i = i + 1; } keys = b; } keys[keysLen] = c; keysLen = keysLen + 1; } function outByte(o: i32): i32 { return Memory.default.getU8(outAt(o)); } // Saves the output from offset keyAt as the innermost key and drops it, leaving its line terminators. In the saved // copy each line terminator is a space, so the key is written on one line, and so each line comment becomes a block // comment (a `*/` inside it becomes `* /`). Strings and templates are skipped whole; a regular expression holding a // quote or `//` is not told apart. export function keyEnd(keyAt: i32): void { if (keyN == keyStarts.length) { let b = new FixedArray(keyN * 2, 0); let f = new FixedArray(keyN * 4, 0); var i = 0; while (i < keyN) { b[i] = keyStarts[i]; f[i * 2] = keyFrom[i * 2]; f[i * 2 + 1] = keyFrom[i * 2 + 1]; i = i + 1; } keyStarts = b; keyFrom = f; } keyStarts[keyN] = keysLen; keyPendN = keyPendN - 1; keyFrom[keyN * 2] = keyPend[keyPendN * 2]; keyFrom[keyN * 2 + 1] = keyPend[keyPendN * 2 + 1]; keyN = keyN + 1; mapCut(keyAt); let end = outEnd(); var o = keyAt; var w = keyAt; var quote = 0; var comment = false; while (o < end) { let c = outByte(o); if (isLt(c)) { Memory.default.setU8(outAt(w), c); w = w + 1; if (comment) { keyByte(42); keyByte(47); comment = false; } keyByte(32); o = o + 1; continue; } if (comment && c == 42 && o + 1 < end && outByte(o + 1) == 47) { keyByte(42); keyByte(32); o = o + 1; continue; } keyByte(c); o = o + 1; if (comment) continue; if (quote != 0) { if (c == 92 && o < end) { keyByte(outByte(o)); o = o + 1; } else if (c == quote) { quote = 0; } } else if (c == 34 || c == 39 || c == 96) { quote = c; } else if (c == 47 && o < end && outByte(o) == 42) { keyByte(42); o = o + 1; while (o < end && !(outByte(o) == 42 && o + 1 < end && outByte(o + 1) == 47)) { let d = outByte(o); if (isLt(d)) { Memory.default.setU8(outAt(w), d); w = w + 1; keyByte(32); } else { keyByte(d); } o = o + 1; } } else if (c == 47 && o < end && outByte(o) == 47) { keyByte(42); o = o + 1; comment = true; } } if (comment) { keyByte(42); keyByte(47); } outTruncate(w); } // A `key` attribute whose value starts at input src, before its value is written; keyEnd pairs them. Keys nest // (an element in a key value may have its own), and so do these calls. The value's source position is saved for // the record key() writes, since the key is written on the element's last line, not the value's own. export function keyBegin(src: i32): void { if (keyPendN * 2 == keyPend.length) { let b = new FixedArray(keyPendN * 4, 0); var i = 0; while (i < keyPendN * 2) { b[i] = keyPend[i]; i = i + 1; } keyPend = b; } mapSrcAt(src); keyPend[keyPendN * 2] = mapSrcLine(); keyPend[keyPendN * 2 + 1] = mapSrcCol(); keyPendN = keyPendN + 1; } // `,` and the innermost saved key, which it forgets. export function key(): void { keyN = keyN - 1; let a = keyStarts[keyN]; put(44); mapKey(keyFrom[keyN * 2], keyFrom[keyN * 2 + 1]); var i = a; while (i < keysLen) { put(keys[i]); i = i + 1; } keysLen = a; } // The end of the file, right after its last token and before its trailing trivia: the imports of the names used. // Imports are hoisted, so they need not come first, and `;` ends any statement before them. Written before the // trivia, they never land inside a trailing line comment, so the file keeps its line count. export function tail(): void { if (used == 0) return; if ((used & (U_JSX | U_JSXS | U_FRAGMENT)) != 0) { tx(JT_IMPORT); var sep = false; if ((used & U_JSX) != 0) { tx(JT_IJSX); sep = true; } if ((used & U_JSXS) != 0) { if (sep) put(44); tx(JT_IJSXS); sep = true; } if ((used & U_FRAGMENT) != 0) { if (sep) put(44); tx(JT_IFRAGMENT); } tx(JT_FROM); source(); tx(JT_RUNTIME); tx(JT_END); } if ((used & U_CREATE) != 0) { tx(JT_IMPORT); tx(JT_ICREATE); tx(JT_FROM); source(); tx(JT_END); } } function source(): void { if (cSrcLen > 0) putPr(cSrcAt, cSrcLen); else putPr(srcAt, srcLen); } // --------------------------------------------------------------------------- // Strings. // The code point of the UTF-8 sequence at input q, and its length (1 to 4) in seqN. var seqN: i32 = 1; function codePointAt(q: i32): i32 { let c = inByte(q); if (c < 0x80) { seqN = 1; return c; } if (c < 0xe0) { seqN = 2; return ((c & 0x1f) << 6) | (inByte(q + 1) & 0x3f); } if (c < 0xf0) { seqN = 3; return ((c & 0x0f) << 12) | ((inByte(q + 1) & 0x3f) << 6) | (inByte(q + 2) & 0x3f); } seqN = 4; return ((c & 0x07) << 18) | ((inByte(q + 1) & 0x3f) << 12) | ((inByte(q + 2) & 0x3f) << 6) | (inByte(q + 3) & 0x3f); } // TypeScript's isLineBreak and isWhiteSpaceSingleLine. function isLineBreak(cp: i32): boolean { return cp == 10 || cp == 13 || cp == 0x2028 || cp == 0x2029; } function isSingleLineSpace(cp: i32): boolean { if (cp == 32 || cp == 9 || cp == 11 || cp == 12) return true; if (cp < 0x85) return false; return cp == 0x85 || cp == 0xa0 || cp == 0x1680 || (cp >= 0x2000 && cp <= 0x200b) || cp == 0x202f || cp == 0x205f || cp == 0x3000 || cp == 0xfeff; } function hexDigit(d: i32): i32 { if (d < 10) return 48 + d; return 87 + d; } // `\u{X}`. function putCodePoint(cp: i32): void { put(92); put(117); put(123); var shift = 20; while (shift > 0 && ((cp >> shift) & 15) == 0) shift = shift - 4; while (shift >= 0) { put(hexDigit((cp >> shift) & 15)); shift = shift - 4; } put(125); } // TypeScript's entities table (engine/jsx-entities.wat): the code point of input [a, b), or -1. function entityCodePoint(a: i32, b: i32): i32 { var r = JSX_ENT; while (true) { let n = Memory.default.getU8(r); if (n == 0) return -1; if (n == b - a) { var i = 0; while (i < n && Memory.default.getU8(r + 1 + i) == inByte(a + i)) i = i + 1; if (i == n) { let v = r + 1 + n; return Memory.default.getU8(v) | (Memory.default.getU8(v + 1) << 8) | (Memory.default.getU8(v + 2) << 16); } } r = r + n + 4; } } function isWordByte(c: i32): boolean { return (c >= 97 && c <= 122) || (c >= 65 && c <= 90) || (c >= 48 && c <= 57) || c == 95; } // TypeScript's decodeEntities at the `&` at input q, before e: `{`, `{` or `&name;`. Writes the code // point and returns the end, or returns q when there is none. function entity(q: i32, e: i32): i32 { var r = q + 1; if (r < e && inByte(r) == 35) { r = r + 1; var hex = false; if (r < e && inByte(r) == 120) { hex = true; r = r + 1; } let d0 = r; var v = 0; while (r < e) { let c = inByte(r); var d = -1; if (c >= 48 && c <= 57) d = c - 48; else if (hex && (c | 32) >= 97 && (c | 32) <= 102) d = (c | 32) - 87; if (d < 0) break; if (v < 0x1000000) { if (hex) v = v * 16 + d; else v = v * 10 + d; } r = r + 1; } if (r == d0 || r >= e || inByte(r) != 59) return q; putCodePoint(v); return r + 1; } let w = r; while (r < e && isWordByte(inByte(r))) r = r + 1; if (r == w || r >= e || inByte(r) != 59) return q; let cp = entityCodePoint(w, r); if (cp < 0) return q; putCodePoint(cp); return r + 1; } // The body of a string with value input [a, b) (entities decoded), escaped for double quotes. With `split`, a line // terminator (CRLF as one) becomes its escape, then `"+`, the terminator itself, and `"`. function strBody(a: i32, b: i32, split: boolean): void { var q = a; while (q < b) { let c = inByte(q); // An entity or an escape is written at another length than the source, so the text after it gets a record. if (c == 38) { let r = entity(q, b); if (r > q) { q = r; mapAt(q); continue; } } if (c == 34 || c == 92) { put(92); put(c); q = q + 1; mapAt(q); continue; } var n = 0; if (split && isLt(c)) n = 1; if (n == 1 && c == 13 && q + 1 < b && inByte(q + 1) == 10) n = 2; if (split && c == 0xe2 && q + 2 < b && inByte(q + 1) == 0x80 && (inByte(q + 2) | 1) == 0xa9) n = 3; if (n == 0) { put(c); q = q + 1; continue; } if (c == 10) { put(92); put(110); } else if (c == 13) { put(92); put(114); if (n == 2) { put(92); put(110); } } else { putCodePoint(codePointAt(q)); } put(34); put(43); putIn(q, q + n); q = q + n; put(34); mapAt(q); } } // An attribute string, input [a, b) including its quotes: no escapes in the source, entities decoded. export function str(a: i32, b: i32): void { put(34); mapAt(a + 1); strBody(a + 1, b - 1, true); put(34); } function addSeg(a: i32, b: i32): void { if (segN * 2 + 2 > seg.length) { let next = new FixedArray(seg.length * 2, 0); var i = 0; while (i < segN * 2) { next[i] = seg[i]; i = i + 1; } seg = next; } seg[segN * 2] = a; seg[segN * 2 + 1] = b; segN = segN + 1; } // Finds the kept segments of a JSX text child, input [a, b), and returns how many: none when it is only // whitespace across lines. TypeScript's fixupWhitespaceAndDecodeEntities: each line is trimmed (except the start // of the first and the end of the last), whitespace-only lines are dropped, and the rest are joined by one space. export function textSegs(a: i32, b: i32): i32 { segN = 0; var first = a; var lastEnd = -1; var q = a; while (q < b) { let cp = codePointAt(q); let n = seqN; if (isLineBreak(cp)) { if (first >= 0 && lastEnd >= 0) addSeg(first, lastEnd); first = -1; } else if (!isSingleLineSpace(cp)) { lastEnd = q + n; if (first < 0) first = q; } q = q + n; } if (first >= 0) addSeg(first, b); return segN; } // The text child textSegs just read, as `"..."` after a `,` when `comma`, or only its line terminators when it // has no segments. export function text(a: i32, b: i32, comma: boolean): void { if (segN == 0) { lines(a, b); return; } var p = a; var i = 0; while (i < segN) { let s = seg[i * 2]; if (i == 0) { lines(p, s); if (comma) put(44); put(34); } else { put(34); put(43); lines(p, s); put(34); put(32); } p = seg[i * 2 + 1]; mapAt(s); strBody(s, p, false); i = i + 1; } put(34); lines(p, b); }