From 5a342ad1a58a61812bfcad36f1664ecfbf9529bb Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Sun, 18 Oct 2026 12:17:25 -0700 Subject: [PATCH 01/83] Parse date strings with V8's grammar when the bounded parser rejects them Date.parse and new Date(string) answered NaN natively for every form outside ISO 8601 with an explicit offset and the X509 certificate shape, so RFC 1123 HTTP dates ("Mon, 01 Jan 2024 00:00:00 GMT") never parsed and the fresh/express/send conditional GET never matched. The runtime now falls back to a port of V8's DateParser (the ES5 date-time format, then the Safari-compatible legacy grammar: weekday and month names, RFC 2822 offsets, zone abbreviations, AM/PM, parenthesized comments, slashes) for exactly the strings the bounded grammars reject, so the timestamps they accept cost nothing more. Zone-less dates are local time, converted with the spec's UTC(t) over the host zone (DST gaps read with the offset before the transition, overlaps answer the earlier instant). With native parsing matching Node for every input, the SC6005 literal warning has nothing left to report and is retired rather than extended to non-literal inputs. Co-Authored-By: Claude Opus 5.5 --- docs/content/docs/coverage.mdx | 1 - .../limitations/dates-and-timezones.mdx | 4 +- .../compiler/src/diagnostics/diagnostic.ts | 17 +- .../src/frontend/lowering/builtins/dates.ts | 5 +- .../src/frontend/lowering/divergences.ts | 75 --- .../src/frontend/lowering/surfaces.ts | 2 +- .../compiler/src/ir/builtin-signatures.ts | 11 +- packages/compiler/surface-manifest.json | 2 +- .../corpus/6230-date-parse-legacy.ts.json | 6 + packages/runtime/src/scr_lib.c | 633 +++++++++++++++++- packages/runtime/src/scr_number.h | 5 +- tests/corpus/6230-date-parse-legacy.ts | 112 ++++ tests/coverage-fixtures/divergences.ts | 2 +- .../__snapshots__/coverage-divergences.txt | 13 +- tests/harness/coverage.test.ts | 7 +- 15 files changed, 766 insertions(+), 129 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6230-date-parse-legacy.ts.json create mode 100644 tests/corpus/6230-date-parse-legacy.ts diff --git a/docs/content/docs/coverage.mdx b/docs/content/docs/coverage.mdx index 2d26e684b..9c9ec413f 100644 --- a/docs/content/docs/coverage.mdx +++ b/docs/content/docs/coverage.mdx @@ -68,7 +68,6 @@ Some code compiles but can behave differently than under Node.js. Coverage repor - `SC6002`: an object literal's key order differs from its type's declaration order, and the program observes the order through `JSON.stringify`, `Object.keys`, `Object.values`, `Object.entries`, `for...in`, or console output. - `SC6003`: `localeCompare` compares a string literal outside the built-in Latin-script collation. - `SC6004`: a call passes a class instance to a function that casts it to an unrelated class. -- `SC6005`: a `Date` string literal uses a form the native parser does not accept. ## Gating builds and CI diff --git a/docs/content/limitations/dates-and-timezones.mdx b/docs/content/limitations/dates-and-timezones.mdx index ad830eb9a..eacaf29d0 100644 --- a/docs/content/limitations/dates-and-timezones.mdx +++ b/docs/content/limitations/dates-and-timezones.mdx @@ -1,3 +1,3 @@ -String constructors and `Date.parse` accept ECMAScript date-only forms, date-times with an explicit `Z` or numeric offset, and GMT certificate-validity strings from the supported X509 APIs. Other V8-specific forms and offset-less local date-times produce an Invalid Date or `NaN`; `SC6005` reports string literals in those forms. +String constructors and `Date.parse` use V8's date-string grammar: ECMAScript date-time strings and the legacy forms Node.js accepts, such as RFC 1123 HTTP dates (`Mon, 01 Jan 2024 00:00:00 GMT`), `Date#toString` output, and `Jan 2, 2024`. Strings without a timezone are local time. -Local calendar getters use the operating system's timezone database. Historical results can differ from Node.js when timezone data differs. For valid extreme years outside a platform calendar API's range, scriptc uses a calendar-equivalent year in the 400-year Gregorian cycle to obtain the timezone rule. +Local calendar getters and local-time conversions use the operating system's timezone database. Historical results can differ from Node.js when timezone data differs. For valid extreme years outside a platform calendar API's range, scriptc uses a calendar-equivalent year in the 400-year Gregorian cycle to obtain the timezone rule. diff --git a/packages/compiler/src/diagnostics/diagnostic.ts b/packages/compiler/src/diagnostics/diagnostic.ts index eaae1b531..da4da347a 100644 --- a/packages/compiler/src/diagnostics/diagnostic.ts +++ b/packages/compiler/src/diagnostics/diagnostic.ts @@ -59,8 +59,9 @@ * order that differs from insertion order where the program * observes it (SC6002), localeCompare over text the built-in * collation does not cover (SC6003), a call feeding an instance - * to a cast to an unrelated class (SC6004), and Date strings the native parser does not - * accept (SC6005) + * to a cast to an unrelated class (SC6004); SC6005 (Date strings + * outside the native parser's grammar) is retired — the runtime + * parses date strings with V8's grammar * SC9xxx internal compiler errors (still source-anchored) */ import type { SrcLoc } from "../ir/ir.js"; @@ -200,18 +201,6 @@ export function unrelatedClassCastDivergenceDiag( }; } -/** SC6005 — a Date string literal outside the grammar the native parser - * accepts (ISO 8601 with an explicit offset, date-only forms, and the - * certificate-time shape): Node parses many more forms. */ -export function dateLiteralDivergenceDiag(text: string, loc: SrcLoc): ScrDiagnostic { - return { - code: "SC6005", - message: `the date string '${text}' parses to an invalid date (NaN) natively, while Node accepts it (the native parser takes ISO 8601 date-times with an explicit offset, such as '2024-01-02T03:04:05Z', and date-only forms)`, - loc, - hint: "write the date in ISO 8601 with an explicit offset ('Z' or '+HH:MM'), or build it from numbers with Date.UTC", - }; -} - /* ── SC5xxx: native FFI ───────────────────────────────────────────────── */ /** SC5001 — the outbound native-FFI manifest is malformed or unreadable. */ diff --git a/packages/compiler/src/frontend/lowering/builtins/dates.ts b/packages/compiler/src/frontend/lowering/builtins/dates.ts index fa880bdc9..7c5a276ac 100644 --- a/packages/compiler/src/frontend/lowering/builtins/dates.ts +++ b/packages/compiler/src/frontend/lowering/builtins/dates.ts @@ -52,9 +52,8 @@ export function lowerDateCall( } return { kind: "libCall", fn: "date.now", args: [], type: F64, loc }; } - // Date.parse uses the same bounded date-string parser as - // new Date(dateString).getTime(); keep their NaN and TimeClip behavior - // identical for the ISO timestamps used by CLI applications. + // Date.parse uses the same date-string parser as new Date(dateString) + // (V8's grammar in the runtime), so their NaN and TimeClip behavior agree. if (lowerer.stdlibGlobalMember(access, "Date") === "parse") { if (call.arguments.length !== 1 || ts.isSpreadElement(call.arguments[0]!)) { lowerer.noLowering(`Date.parse with ${call.arguments.length} arguments`, call); diff --git a/packages/compiler/src/frontend/lowering/divergences.ts b/packages/compiler/src/frontend/lowering/divergences.ts index 2a9009aed..004cb55a1 100644 --- a/packages/compiler/src/frontend/lowering/divergences.ts +++ b/packages/compiler/src/frontend/lowering/divergences.ts @@ -14,7 +14,6 @@ import * as ts from "../ts7/adapter.js"; import { locOf } from "../program.js"; import { isNodeModulesPath } from "../resolve.js"; import { - dateLiteralDivergenceDiag, keyOrderDivergenceDiag, localeCompareDivergenceDiag, unrelatedClassCastDivergenceDiag, @@ -56,7 +55,6 @@ export function scanDivergences( const literals: ts.ObjectLiteralExpression[] = []; const observers: ts.Expression[] = []; const casts: (ts.AsExpression | ts.TypeAssertion)[] = []; - const dates: { call: ts.CallExpression | ts.NewExpression; arg: ts.Expression }[] = []; const localeCompares: ts.CallExpression[] = []; /** binding name → member names assigned through it (`q.a = 2`). */ const assignedMembers = new Map>(); @@ -97,19 +95,9 @@ export function scanDivergences( member === "dir") ) { for (const a of node.arguments) if (!isLiteralLike(a)) observers.push(a); - } else if (recv.text === "Date" && member === "parse") { - if (node.arguments[0] !== undefined) dates.push({ call: node, arg: node.arguments[0] }); } } } - } else if (ts.isNewExpression(node)) { - if ( - ts.isIdentifier(node.expression) && - node.expression.text === "Date" && - node.arguments?.length === 1 - ) { - dates.push({ call: node, arg: node.arguments[0]! }); - } } else if (ts.isForInStatement(node)) { observers.push(node.expression); } else if (ts.isVariableDeclaration(node)) { @@ -397,55 +385,9 @@ export function scanDivergences( } } - // ── Date strings the native parser does not accept ──────────────── - for (const { call, arg } of dates) { - const callee = ts.isNewExpression(call) - ? call.expression - : ts.isPropertyAccessExpression(call.expression) - ? call.expression.expression - : undefined; - if (callee === undefined || !lowerer.isStdlibGlobal(callee, "Date")) continue; - for (const literal of dateStringLiterals(checker, arg)) { - const text = stringLiteralText(literal)!; - if (nativeDateParses(text) || !/\d/.test(text)) continue; - out.push(dateLiteralDivergenceDiag(text, locOf(literal))); - } - } - return out; } -/** The string literals a Date argument can be: the literal itself, or - * each element of a const array of literals a for-of binding iterates. */ -function dateStringLiterals(checker: ts.TypeChecker, arg: ts.Expression): ts.Expression[] { - if (stringLiteralText(arg) !== null) return [arg]; - if (!ts.isIdentifier(arg)) return []; - const symbol = safely(() => checker.getSymbolAtLocation(arg)); - const decl = symbol === undefined ? undefined : checker.valueDeclarationOf(symbol); - const list = decl?.parent; - const loop = list?.parent; - if ( - decl === undefined || - !ts.isVariableDeclaration(decl) || - list === undefined || - loop === undefined || - !ts.isForOfStatement(loop) || - loop.initializer !== list - ) - return []; - let iterated: ts.Expression = loop.expression; - if (ts.isIdentifier(iterated)) { - const source = safely(() => checker.getSymbolAtLocation(iterated)); - const init = source === undefined ? undefined : checker.valueDeclarationOf(source); - if (init === undefined || !ts.isVariableDeclaration(init) || init.initializer === undefined) - return []; - if (init.parent === undefined || (init.parent.flags & ts.NodeFlags.Const) === 0) return []; - iterated = init.initializer; - } - if (!ts.isArrayLiteralExpression(iterated)) return []; - return iterated.elements.filter((e) => stringLiteralText(e) !== null); -} - /** A checker query that fails here only loses a warning. */ function safely(f: () => T): T | undefined { try { @@ -857,20 +799,3 @@ function classExtends( function className(decl: ts.ClassLikeDeclaration): string { return decl.name?.text ?? "(anonymous class)"; } - -const MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]; - -/** The date-string grammar the native runtime accepts (scr_lib.c - * scr_date_parse_get_time): ECMAScript's date-time format with an explicit - * offset (date-only forms are UTC) and the certificate-time shape - * "Mon DD HH:MM:SS YYYY GMT". Everything else parses to NaN natively. */ -export function nativeDateParses(text: string): boolean { - const iso = - /^(?:[+-]\d{6}|\d{4})(?:-(\d{2})(?:-(\d{2}))?)?$/.test(text) || - /^(?:[+-]\d{6}|\d{4})-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{3})?)?(?:Z|[+-]\d{2}:\d{2})$/.test( - text, - ); - if (iso) return true; - const cert = /^([A-Za-z]{3}) {1,2}\d{1,2} \d{2}:\d{2}:\d{2} \d{4} GMT$/.exec(text); - return cert !== null && MONTHS.includes(cert[1]!.toLowerCase()); -} diff --git a/packages/compiler/src/frontend/lowering/surfaces.ts b/packages/compiler/src/frontend/lowering/surfaces.ts index d394c1960..561a58953 100644 --- a/packages/compiler/src/frontend/lowering/surfaces.ts +++ b/packages/compiler/src/frontend/lowering/surfaces.ts @@ -2170,7 +2170,7 @@ export const AMBIENT_SURFACE_FNS: readonly AmbientSurfaceRow[] = [ kind: "stdlib", name: "Date.parse", fns: ["date.parse"], - note: "one date string, using the bounded parser shared with new Date(dateString).getTime()", + note: "one date string, using V8's date parser shared with new Date(dateString)", }, { id: "stdlib.date.constructor", diff --git a/packages/compiler/src/ir/builtin-signatures.ts b/packages/compiler/src/ir/builtin-signatures.ts index 08c493ae4..1d14d8732 100644 --- a/packages/compiler/src/ir/builtin-signatures.ts +++ b/packages/compiler/src/ir/builtin-signatures.ts @@ -4187,13 +4187,10 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ "date.getUTCMilliseconds": { argTypes: [DATE_T], result: F64 }, "date.getTimezoneOffset": { argTypes: [DATE_T], result: F64 }, /** The composed `new Date(dateString).getTime()` read: one borrowed - * string, f64 milliseconds since epoch. The parsed grammar is BOUNDED - * (documented divergence — V8's parser accepts far more): the ASN.1 - * validity shape X509Certificate.validFrom/validTo answer ("Jul 1 - * 00:00:00 2026 GMT" — the portless cert-expiry read), and ECMA's own - * date-time string format (YYYY-MM-DD[THH:mm[:ss[.sss]]][Z|±HH:MM] — - * date-only forms are UTC, exactly the spec). Anything else is NaN, - * Node's invalid-date getTime. Never throws. */ + * string, f64 milliseconds since epoch, with V8's date-string grammar + * (the ES5 format, then the legacy forms: RFC 1123/2822, Date#toString, + * "Jan 2, 2024", ...). Zone-less dates are local time; anything V8 + * rejects is NaN, Node's invalid-date getTime. Never throws. */ "date.parseGetTime": { argTypes: [STRING], result: F64 }, /** `Date.UTC(...)` — seven f64 arguments (the frontend completes the * spec's defaults for omitted trailing parts: month 0, date 1, time diff --git a/packages/compiler/surface-manifest.json b/packages/compiler/surface-manifest.json index f5313f12d..471210669 100644 --- a/packages/compiler/surface-manifest.json +++ b/packages/compiler/surface-manifest.json @@ -4150,7 +4150,7 @@ "kind": "stdlib", "name": "Date.parse", "status": "static", - "note": "one date string, using the bounded parser shared with new Date(dateString).getTime()" + "note": "one date string, using V8's date parser shared with new Date(dateString)" }, { "id": "stdlib.date.toISOString", diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6230-date-parse-legacy.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6230-date-parse-legacy.ts.json new file mode 100644 index 000000000..b7cad9619 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6230-date-parse-legacy.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6230-date-parse-legacy.ts" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_lib.c b/packages/runtime/src/scr_lib.c index fd51e9c78..2c232cbea 100644 --- a/packages/runtime/src/scr_lib.c +++ b/packages/runtime/src/scr_lib.c @@ -10298,9 +10298,9 @@ ScrStr *scr_date_to_iso(double ms) { /* ── new Date(dateString).getTime() ────────────────────────────────────── * - * The BOUNDED date-string parse (documented divergence — V8's parser - * accepts far more): two grammars answer a time value, everything else is - * NaN (Node's invalid-date getTime). + * The BOUNDED date-string parse, the fast path: two grammars answer a time + * value directly; a string they reject falls back to V8's full parser + * (scr_date_parse_v8 below), which also answers everything they accept. * * 1. The ASN1_TIME_print shape X509Certificate.validFrom/validTo answer * ("Jul 1 00:00:00 2026 GMT", "Jul 17 17:52:11 2026 GMT") — the @@ -10309,8 +10309,8 @@ ScrStr *scr_date_to_iso(double ms) { * 2. ECMA's own date-time string format with an EXPLICIT offset: * YYYY[-MM[-DD]] date-only forms (UTC, per the spec) and full * date-times YYYY-MM-DDTHH:mm[:ss[.sss]] ending in Z or ±HH:MM. - * Offset-less date-times are LOCAL time in JS; that arm answers NaN - * here rather than guessing a zone (the divergence note). + * Offset-less date-times are LOCAL time in JS; they take the + * fallback, which converts them in the host zone. */ /* Howard Hinnant's days_from_civil (the to_iso walk inverted). */ @@ -10351,7 +10351,9 @@ static bool scr_date_digits(const char **p, const char *end, int n, int *out) { return true; } -double scr_date_parse_get_time(ScrStr *s) { +static double scr_date_parse_v8(ScrStr *s); + +static double scr_date_parse_bounded(ScrStr *s) { const char *p = s->data; const char *end = s->data + s->len; static const char *mon[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", @@ -10444,6 +10446,13 @@ double scr_date_parse_get_time(ScrStr *s) { } } +/* The bounded grammars answer first; only a string they reject reaches + * V8's full parser, so the timestamps they accept cost nothing more. */ +double scr_date_parse_get_time(ScrStr *s) { + double t = scr_date_parse_bounded(s); + return isnan(t) ? scr_date_parse_v8(s) : t; +} + /* Date.UTC(year, month, date, hours, minutes, seconds, ms) — the spec's * MakeDay/MakeTime/TimeClip pipeline over ALREADY-NUMBER arguments (tsc * pins them; the frontend completes omitted trailing arguments with the @@ -10647,6 +10656,618 @@ double scr_date_get_timezone_offset(double ms) { return scr_date_local_parts(ms, &p) ? p.timezone_offset : NAN; } +/* ── Local time to UTC ───────────────────────────────────────────────── + * The spec's UTC(t) over the host zone: t is a local wall-clock time + * value. The offset in effect a day before and a day after t brackets any + * transition (none come closer together than that in tzdata). A time in + * a DST overlap has two instants and answers the earlier; a time in a gap + * has none and is read with the offset BEFORE the transition, exactly + * V8's ICU getOffsetFromLocal(FORMER, FORMER). Offsets come from the same + * localtime/surrogate-year walk as the local getters, so a Date built + * from local fields reads those fields back. */ + +/* The host zone's offset (local - UTC) in ms at UTC instant `ms` (an + * integral time value within about ±8.65e15). */ +static bool scr_date_offset_at(double ms, double *out) { + long long cms = (long long)ms; + double secd = (double)(cms / 1000 - (cms % 1000 < 0)); + struct tm tmv; + double basis_secd = secd; + if (!scr_date_localtime(basis_secd, &tmv)) { + ScrDateParts utc; + scr_date_utc_parts_unchecked(ms, &utc); + long long cycle_year = (utc.year - 2000) % 400; + if (cycle_year < 0) cycle_year += 400; + basis_secd = + scr_days_from_civil(2000 + cycle_year, utc.month + 1, utc.date) * 86400.0 + + utc.hours * 3600.0 + utc.minutes * 60.0 + utc.seconds; + if (!scr_date_localtime(basis_secd, &tmv)) return false; + } + double local_as_utc = + scr_days_from_civil((long long)tmv.tm_year + 1900, tmv.tm_mon + 1, tmv.tm_mday) * 86400.0 + + tmv.tm_hour * 3600.0 + tmv.tm_min * 60.0 + tmv.tm_sec; + *out = (local_as_utc - basis_secd) * 1000.0; + return true; +} + +static double scr_date_local_to_utc(double t) { + double before, after, at_first, at_second; + if (!scr_date_offset_at(t - 86400000.0, &before) || + !scr_date_offset_at(t + 86400000.0, &after)) { + return NAN; + } + double first = t - before; + if (!scr_date_offset_at(first, &at_first)) return NAN; + if (before == after) { + if (at_first == before) return first; + /* Two transitions inside the window: settle on the instant's own offset. */ + double again = t - at_first, at_again; + if (scr_date_offset_at(again, &at_again) && at_again == at_first) return again; + return first; + } + double second = t - after; + if (!scr_date_offset_at(second, &at_second)) return NAN; + bool first_ok = at_first == before, second_ok = at_second == after; + if (first_ok && second_ok) return first < second ? first : second; /* overlap */ + if (second_ok && !first_ok) return second; + return first; /* the earlier offset's instant, or a gap */ +} + +/* V8's MakeDay (date.cc): bounds the UNtruncated year and month, then + * truncates (FastD2I) and normalizes the month into the year. */ +static double scr_date_v8_make_day(double year, double month, double date) { + if (!(year >= -1000000.0 && year <= 1000000.0 && month >= -10000000.0 && + month <= 10000000.0 && isfinite(date))) { + return NAN; + } + int y = (int)year, m = (int)month; + y += m / 12; + m %= 12; + if (m < 0) { + m += 12; + y -= 1; + } + static const int year_delta = 399999; + static const int base_day = 365 * (1970 + year_delta) + (1970 + year_delta) / 4 - + (1970 + year_delta) / 100 + (1970 + year_delta) / 400; + int day_from_year = 365 * (y + year_delta) + (y + year_delta) / 4 - + (y + year_delta) / 100 + (y + year_delta) / 400 - base_day; + static const int common[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; + static const int leap[12] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; + bool is_leap = !((y % 4 != 0) || (y % 100 == 0 && y % 400 != 0)); + day_from_year += is_leap ? leap[m] : common[m]; + return (double)(day_from_year - 1) + trunc(date); +} + +static double scr_date_v8_make_time(double h, double m, double s, double ms) { + if (!isfinite(h) || !isfinite(m) || !isfinite(s) || !isfinite(ms)) return NAN; + return trunc(h) * 3600000.0 + trunc(m) * 60000.0 + trunc(s) * 1000.0 + trunc(ms); +} + +/* MakeDate over local fields, then UTC(t) and TimeClip. V8 refuses a local + * time value more than ten days past TimeClip's range before converting. */ +static double scr_date_from_local(double day, double time) { + if (!isfinite(day) || !isfinite(time)) return NAN; + double t = time + day * 86400000.0; + if (!(t >= -8640000864000000.0 && t <= 8640000864000000.0)) return NAN; + return scr_date_new_ms(scr_date_local_to_utc(trunc(t))); +} + +/* ── V8's date-string parser, the fallback ───────────────────────────── + * scr_date_parse_get_time answers its bounded grammars directly and asks + * this parser only when they produce NaN. It follows V8's DateParser + * (dateparser.cc, dateparser-inl.h) step for step: the ES5 date-time + * format first, then the Safari-compatible legacy grammar for whatever + * is left (RFC 1123/2822 HTTP dates, Date#toString output, "Jan 2, 2024", + * "2024/01/02 10:00", ...). Strings are read as UTF-16 code units, and a + * NUL unit ends the input, as in V8. A date without a zone is local time. */ + +enum { + SCR_DT_INVALID = -6, SCR_DT_UNKNOWN = -5, SCR_DT_SPACE = -4, + SCR_DT_NUMBER = -3, SCR_DT_SYMBOL = -2, SCR_DT_END = -1, + /* keyword tags are their keyword type (>= 0) */ + SCR_DK_WORD = 0, SCR_DK_MONTH = 1, SCR_DK_ZONE = 2, SCR_DK_T = 3, SCR_DK_AMPM = 4, +}; +#define SCR_DT_NONE INT_MAX + +typedef struct { + int tag, length, value; +} ScrDateTok; + +typedef struct { + const unsigned char *p, *end; + uint32_t ch, low; + ScrDateTok next; +} ScrDateScan; + +static void scr_date_scan_advance(ScrDateScan *in) { + if (in->low) { + in->ch = in->low; + in->low = 0; + return; + } + if (in->p >= in->end) { + in->ch = 0; + return; + } + const unsigned char *p = in->p; + size_t left = (size_t)(in->end - p); + uint32_t cp = p[0]; + size_t n = 1; + if (cp >= 0xF0 && left >= 4) { + cp = ((cp & 0x07u) << 18) | ((p[1] & 0x3Fu) << 12) | ((p[2] & 0x3Fu) << 6) | (p[3] & 0x3Fu); + n = 4; + } else if (cp >= 0xE0 && left >= 3) { + cp = ((cp & 0x0Fu) << 12) | ((p[1] & 0x3Fu) << 6) | (p[2] & 0x3Fu); + n = 3; + } else if (cp >= 0xC0 && left >= 2) { + cp = ((cp & 0x1Fu) << 6) | (p[1] & 0x3Fu); + n = 2; + } else if (cp >= 0x80) { + cp = 0xFFFD; + } + in->p += n; + if (cp > 0xFFFF) { + cp -= 0x10000; + in->ch = 0xD800 + (cp >> 10); + in->low = 0xDC00 + (cp & 0x3FF); + } else { + in->ch = cp; + } +} + +/* ECMAScript WhiteSpace, and WhiteSpace or LineTerminator. */ +static bool scr_date_is_space(uint32_t c) { + return c == 0x09 || c == 0x0B || c == 0x0C || c == 0x20 || c == 0xA0 || c == 0xFEFF || + c == 0x1680 || (c >= 0x2000 && c <= 0x200A) || c == 0x202F || c == 0x205F || + c == 0x3000; +} + +static bool scr_date_is_space_or_newline(uint32_t c) { + return scr_date_is_space(c) || c == 0x0A || c == 0x0D || c == 0x2028 || c == 0x2029; +} + +static const struct { + char prefix[3]; + signed char type, value; +} scr_date_keywords[] = { + {{'j', 'a', 'n'}, SCR_DK_MONTH, 1}, {{'f', 'e', 'b'}, SCR_DK_MONTH, 2}, + {{'m', 'a', 'r'}, SCR_DK_MONTH, 3}, {{'a', 'p', 'r'}, SCR_DK_MONTH, 4}, + {{'m', 'a', 'y'}, SCR_DK_MONTH, 5}, {{'j', 'u', 'n'}, SCR_DK_MONTH, 6}, + {{'j', 'u', 'l'}, SCR_DK_MONTH, 7}, {{'a', 'u', 'g'}, SCR_DK_MONTH, 8}, + {{'s', 'e', 'p'}, SCR_DK_MONTH, 9}, {{'o', 'c', 't'}, SCR_DK_MONTH, 10}, + {{'n', 'o', 'v'}, SCR_DK_MONTH, 11}, {{'d', 'e', 'c'}, SCR_DK_MONTH, 12}, + {{'a', 'm', 0}, SCR_DK_AMPM, 0}, {{'p', 'm', 0}, SCR_DK_AMPM, 12}, + {{'u', 't', 0}, SCR_DK_ZONE, 0}, {{'u', 't', 'c'}, SCR_DK_ZONE, 0}, + {{'z', 0, 0}, SCR_DK_ZONE, 0}, {{'g', 'm', 't'}, SCR_DK_ZONE, 0}, + {{'c', 'd', 't'}, SCR_DK_ZONE, -5}, {{'c', 's', 't'}, SCR_DK_ZONE, -6}, + {{'e', 'd', 't'}, SCR_DK_ZONE, -4}, {{'e', 's', 't'}, SCR_DK_ZONE, -5}, + {{'m', 'd', 't'}, SCR_DK_ZONE, -6}, {{'m', 's', 't'}, SCR_DK_ZONE, -7}, + {{'p', 'd', 't'}, SCR_DK_ZONE, -7}, {{'p', 's', 't'}, SCR_DK_ZONE, -8}, + {{'t', 0, 0}, SCR_DK_T, 0}, +}; + +static ScrDateTok scr_date_scan_token(ScrDateScan *in) { + ScrDateTok t = {SCR_DT_UNKNOWN, 1, -1}; + uint32_t c = in->ch; + if (c == 0) { + t.tag = SCR_DT_END; + return t; + } + if (c >= '0' && c <= '9') { + /* Leading zeros count toward the length; past nine significant + * digits the value stops growing but the digits are consumed. */ + int n = 0, digits = 0, length = 0; + while (in->ch == '0') { + scr_date_scan_advance(in); + length++; + } + while (in->ch >= '0' && in->ch <= '9') { + if (digits < 9) n = n * 10 + (int)(in->ch - '0'); + digits++; + length++; + scr_date_scan_advance(in); + } + t.tag = SCR_DT_NUMBER; + t.value = n; + t.length = length; + return t; + } + if (c == ':' || c == '-' || c == '+' || c == '.' || c == ')') { + scr_date_scan_advance(in); + t.tag = SCR_DT_SYMBOL; + t.value = (int)c; + return t; + } + if (c >= 'A' && !scr_date_is_space(c)) { + uint32_t prefix[3] = {0, 0, 0}; + int length = 0; + while (in->ch >= 'A' && !scr_date_is_space(in->ch)) { + if (length < 3) prefix[length] = in->ch | 0x20; + length++; + scr_date_scan_advance(in); + } + t.tag = SCR_DK_WORD; + t.value = 0; + t.length = length; + for (size_t i = 0; i < sizeof scr_date_keywords / sizeof scr_date_keywords[0]; i++) { + const char *k = scr_date_keywords[i].prefix; + if (prefix[0] == (uint32_t)(unsigned char)k[0] && prefix[1] == (uint32_t)(unsigned char)k[1] && + prefix[2] == (uint32_t)(unsigned char)k[2] && + (length <= 3 || scr_date_keywords[i].type == SCR_DK_MONTH)) { + t.tag = scr_date_keywords[i].type; + t.value = scr_date_keywords[i].value; + break; + } + } + return t; + } + if (scr_date_is_space_or_newline(c)) { + scr_date_scan_advance(in); + t.tag = SCR_DT_SPACE; + return t; + } + if (c == '(') { + int balance = 0; + do { + if (in->ch == ')') balance--; + else if (in->ch == '(') balance++; + scr_date_scan_advance(in); + } while (balance > 0 && in->ch); + return t; + } + scr_date_scan_advance(in); + return t; +} + +static ScrDateTok scr_date_next(ScrDateScan *in) { + ScrDateTok t = in->next; + in->next = scr_date_scan_token(in); + return t; +} + +static bool scr_date_tok_symbol(ScrDateTok t, int c) { + return t.tag == SCR_DT_SYMBOL && t.value == c; +} + +static bool scr_date_skip(ScrDateScan *in, int c) { + if (!scr_date_tok_symbol(in->next, c)) return false; + in->next = scr_date_scan_token(in); + return true; +} + +static bool scr_date_tok_sign(ScrDateTok t) { + return t.tag == SCR_DT_SYMBOL && (t.value == '-' || t.value == '+'); +} + +static bool scr_date_tok_fixed(ScrDateTok t, int length) { + return t.tag == SCR_DT_NUMBER && t.length == length; +} + +static bool scr_date_tok_z(ScrDateTok t) { + return t.tag == SCR_DK_ZONE && t.length == 1 && t.value == 0; +} + +static bool scr_date_between(int x, int lo, int hi) { return x >= lo && x <= hi; } + +typedef struct { + int comp[3], index, named_month; + bool iso; +} ScrDateDay; + +typedef struct { + int comp[4], index, hour_offset; +} ScrDateTime; + +typedef struct { + int sign, hour, minute; +} ScrDateZone; + +static bool scr_date_day_add(ScrDateDay *d, int n) { + if (d->index >= 3) return false; + d->comp[d->index++] = n; + return true; +} + +static bool scr_date_time_add(ScrDateTime *t, int n) { + if (t->index >= 4) return false; + t->comp[t->index++] = n; + return true; +} + +static bool scr_date_time_add_final(ScrDateTime *t, int n) { + if (!scr_date_time_add(t, n)) return false; + while (t->index < 4) t->comp[t->index++] = 0; + return true; +} + +static bool scr_date_time_expecting(const ScrDateTime *t, int n) { + return (t->index == 1 && scr_date_between(n, 0, 59)) || + (t->index == 2 && scr_date_between(n, 0, 59)) || + (t->index == 3 && scr_date_between(n, 0, 999)); +} + +static void scr_date_zone_set(ScrDateZone *z, int hours) { + z->sign = hours < 0 ? -1 : 1; + z->hour = hours * z->sign; + z->minute = 0; +} + +static bool scr_date_zone_expecting(const ScrDateZone *z, int n) { + return z->hour != SCR_DT_NONE && z->minute == SCR_DT_NONE && scr_date_between(n, 0, 59); +} + +/* ReadMilliseconds: the first three significant digits of the numeral. */ +static int scr_date_millis(ScrDateTok t) { + int number = t.value, length = t.length; + if (length == 1) number *= 100; + else if (length == 2) number *= 10; + else if (length > 3) { + if (length > 9) length = 9; + int factor = 1; + do { + factor *= 10; + length--; + } while (length > 3); + number /= factor; + } + return number; +} + +/* ParseES5DateTime: answers the first token it could not place (the + * legacy grammar resumes there), END after a complete ES5 string, or + * INVALID for a malformed ES5 time part. */ +static ScrDateTok scr_date_parse_es5(ScrDateScan *in, ScrDateDay *day, ScrDateTime *time, + ScrDateZone *tz) { + ScrDateTok invalid = {SCR_DT_INVALID, 0, -1}; + if (scr_date_tok_sign(in->next)) { + ScrDateTok sign = scr_date_next(in); + if (!scr_date_tok_fixed(in->next, 6)) return sign; + int s = 44 - sign.value; + int year = scr_date_next(in).value; + if (s < 0 && year == 0) return sign; + scr_date_day_add(day, s * year); + } else if (scr_date_tok_fixed(in->next, 4)) { + scr_date_day_add(day, scr_date_next(in).value); + } else { + return scr_date_next(in); + } + if (scr_date_skip(in, '-')) { + if (!scr_date_tok_fixed(in->next, 2) || !scr_date_between(in->next.value, 1, 12)) { + return scr_date_next(in); + } + scr_date_day_add(day, scr_date_next(in).value); + if (scr_date_skip(in, '-')) { + if (!scr_date_tok_fixed(in->next, 2) || !scr_date_between(in->next.value, 1, 31)) { + return scr_date_next(in); + } + scr_date_day_add(day, scr_date_next(in).value); + } + } + if (in->next.tag != SCR_DK_T) { + if (in->next.tag != SCR_DT_END) return scr_date_next(in); + } else { + scr_date_next(in); + if (!scr_date_tok_fixed(in->next, 2) || !scr_date_between(in->next.value, 0, 24)) return invalid; + bool hour_is_24 = in->next.value == 24; + scr_date_time_add(time, scr_date_next(in).value); + if (!scr_date_skip(in, ':')) return invalid; + if (!scr_date_tok_fixed(in->next, 2) || !scr_date_between(in->next.value, 0, 59) || + (hour_is_24 && in->next.value > 0)) { + return invalid; + } + scr_date_time_add(time, scr_date_next(in).value); + if (scr_date_skip(in, ':')) { + if (!scr_date_tok_fixed(in->next, 2) || !scr_date_between(in->next.value, 0, 59) || + (hour_is_24 && in->next.value > 0)) { + return invalid; + } + scr_date_time_add(time, scr_date_next(in).value); + if (scr_date_skip(in, '.')) { + if (in->next.tag != SCR_DT_NUMBER || (hour_is_24 && in->next.value > 0)) return invalid; + scr_date_time_add(time, scr_date_millis(scr_date_next(in))); + } + } + if (scr_date_tok_z(in->next)) { + scr_date_next(in); + scr_date_zone_set(tz, 0); + } else if (scr_date_tok_sign(in->next)) { + tz->sign = scr_date_next(in).value == '+' ? 1 : -1; + if (scr_date_tok_fixed(in->next, 4)) { + int hourmin = scr_date_next(in).value; + int hour = hourmin / 100, minute = hourmin % 100; + if (!scr_date_between(hour, 0, 23) || !scr_date_between(minute, 0, 59)) return invalid; + tz->hour = hour; + tz->minute = minute; + } else { + if (!scr_date_tok_fixed(in->next, 2) || !scr_date_between(in->next.value, 0, 23)) { + return invalid; + } + tz->hour = scr_date_next(in).value; + if (!scr_date_skip(in, ':')) return invalid; + if (!scr_date_tok_fixed(in->next, 2) || !scr_date_between(in->next.value, 0, 59)) { + return invalid; + } + tz->minute = scr_date_next(in).value; + } + } + if (in->next.tag != SCR_DT_END) return invalid; + } + /* Date-only forms without a zone are UTC; date-times are local. */ + if (tz->hour == SCR_DT_NONE && time->index == 0) scr_date_zone_set(tz, 0); + day->iso = true; + ScrDateTok end = {SCR_DT_END, 0, -1}; + return end; +} + +static bool scr_date_day_write(ScrDateDay *d, double *year_out, double *month_out, double *day_out) { + if (d->index < 1) return false; + while (d->index < 3) d->comp[d->index++] = 1; + int year = 0, month, day; + if (d->named_month == SCR_DT_NONE) { + if (d->iso || !scr_date_between(d->comp[0], 1, 31)) { + year = d->comp[0]; + month = d->comp[1]; + day = d->comp[2]; + } else { + month = d->comp[0]; + day = d->comp[1]; + year = d->comp[2]; + } + } else { + month = d->named_month; + /* V8 pads to three components before choosing an order, so a lone + * day after a month name reads the padding 1 as the year (2001). */ + if (!scr_date_between(d->comp[0], 1, 31)) { + year = d->comp[0]; + day = d->comp[1]; + } else { + day = d->comp[0]; + year = d->comp[1]; + } + } + if (!d->iso) { + if (scr_date_between(year, 0, 49)) year += 2000; + else if (scr_date_between(year, 50, 99)) year += 1900; + } + if (!scr_date_between(month, 1, 12) || !scr_date_between(day, 1, 31)) return false; + *year_out = year; + *month_out = month - 1; + *day_out = day; + return true; +} + +static bool scr_date_time_write(ScrDateTime *t, double out[4]) { + while (t->index < 4) t->comp[t->index++] = 0; + int hour = t->comp[0], minute = t->comp[1], second = t->comp[2], millisecond = t->comp[3]; + if (t->hour_offset != SCR_DT_NONE) { + if (!scr_date_between(hour, 0, 12)) return false; + hour %= 12; + hour += t->hour_offset; + } + if (!scr_date_between(hour, 0, 23) || !scr_date_between(minute, 0, 59) || + !scr_date_between(second, 0, 59) || !scr_date_between(millisecond, 0, 999)) { + if (hour != 24 || minute != 0 || second != 0 || millisecond != 0) return false; + } + out[0] = hour; + out[1] = minute; + out[2] = second; + out[3] = millisecond; + return true; +} + +static bool scr_date_zone_write(ScrDateZone *z, double *offset_seconds) { + if (z->sign == SCR_DT_NONE) { + *offset_seconds = NAN; + return true; + } + if (z->hour == SCR_DT_NONE) z->hour = 0; + if (z->minute == SCR_DT_NONE) z->minute = 0; + unsigned total = (unsigned)z->hour * 3600u + (unsigned)z->minute * 60u; + if (total > (unsigned)INT_MAX) return false; + *offset_seconds = z->sign < 0 ? -(double)total : (double)total; + return true; +} + +static double scr_date_parse_v8(ScrStr *s) { + ScrDateScan in = {(const unsigned char *)s->data, (const unsigned char *)s->data + s->len, 0, 0, {0, 0, 0}}; + scr_date_scan_advance(&in); + in.next = scr_date_scan_token(&in); + ScrDateDay day = {{0, 0, 0}, 0, SCR_DT_NONE, false}; + ScrDateTime time = {{0, 0, 0, 0}, 0, SCR_DT_NONE}; + ScrDateZone tz = {SCR_DT_NONE, SCR_DT_NONE, SCR_DT_NONE}; + ScrDateTok token = scr_date_parse_es5(&in, &day, &time, &tz); + if (token.tag == SCR_DT_INVALID) return NAN; + bool has_read_number = day.index != 0; + for (; token.tag != SCR_DT_END; token = scr_date_next(&in)) { + if (token.tag == SCR_DT_NUMBER) { + has_read_number = true; + int n = token.value; + if (scr_date_skip(&in, ':')) { + if (scr_date_skip(&in, ':')) { + /* n:: */ + if (time.index != 0) return NAN; + scr_date_time_add(&time, n); + scr_date_time_add(&time, 0); + } else { + if (!scr_date_time_add(&time, n)) return NAN; + if (scr_date_tok_symbol(in.next, '.')) scr_date_next(&in); + } + } else if (scr_date_skip(&in, '.') && scr_date_time_expecting(&time, n)) { + scr_date_time_add(&time, n); + if (in.next.tag != SCR_DT_NUMBER) return NAN; + int ms = scr_date_millis(scr_date_next(&in)); + if (ms < 0) return NAN; + scr_date_time_add_final(&time, ms); + } else if (scr_date_zone_expecting(&tz, n)) { + tz.minute = n; + } else if (scr_date_time_expecting(&time, n)) { + scr_date_time_add_final(&time, n); + ScrDateTok peek = in.next; + if (peek.tag != SCR_DT_END && peek.tag != SCR_DT_SPACE && !scr_date_tok_z(peek) && + !scr_date_tok_sign(peek)) { + return NAN; + } + } else { + if (!scr_date_day_add(&day, n)) return NAN; + scr_date_skip(&in, '-'); + } + } else if (token.tag >= 0) { + if (token.tag == SCR_DK_AMPM && time.index != 0) { + time.hour_offset = token.value; + } else if (token.tag == SCR_DK_MONTH) { + day.named_month = token.value; + scr_date_skip(&in, '-'); + } else if (token.tag == SCR_DK_ZONE && has_read_number) { + scr_date_zone_set(&tz, token.value); + } else { + /* Garbage words are illegal once a number has been read, and the + * first number must be separated from them. */ + if (has_read_number) return NAN; + if (in.next.tag == SCR_DT_NUMBER) return NAN; + } + } else if (scr_date_tok_sign(token) && + ((tz.hour == 0 && tz.minute == 0) || time.index != 0)) { + /* A UTC offset, only after UTC or a time. */ + tz.sign = token.value == '-' ? -1 : 1; + int n = 0, length = 0; + if (in.next.tag == SCR_DT_NUMBER) { + ScrDateTok number = scr_date_next(&in); + length = number.length; + n = number.value; + } + has_read_number = true; + if (scr_date_tok_symbol(in.next, ':')) { + tz.hour = n; + tz.minute = SCR_DT_NONE; + } else if (length == 2 || length == 1) { + tz.hour = n; + tz.minute = 0; + } else if (length == 4 || length == 3) { + tz.hour = n / 100; + tz.minute = n % 100; + } else { + return NAN; + } + } else if ((scr_date_tok_sign(token) || scr_date_tok_symbol(token, ')')) && has_read_number) { + return NAN; + } + /* Anything else (whitespace, unknown characters) is ignored. */ + } + double year, month, date, hms[4], offset; + if (!scr_date_day_write(&day, &year, &month, &date) || !scr_date_time_write(&time, hms) || + !scr_date_zone_write(&tz, &offset)) { + return NAN; + } + double dayv = scr_date_v8_make_day(year, month, date); + double timev = scr_date_v8_make_time(hms[0], hms[1], hms[2], hms[3]); + if (isnan(offset)) return scr_date_from_local(dayv, timev); + if (!isfinite(dayv) || !isfinite(timev)) return NAN; + double t = timev + dayv * 86400000.0 - offset * 1000.0; + if (!(t >= -8640000000000000.0 && t <= 8640000000000000.0)) return NAN; + return scr_date_new_ms(t); +} + /* ── Number statics ──────────────────────────────────────────────────── * JS-exact: the ES2015 Number statics never coerce (unlike the global * isNaN/isFinite), and the compiler routes only number-typed arguments diff --git a/packages/runtime/src/scr_number.h b/packages/runtime/src/scr_number.h index 84db27075..80911023e 100644 --- a/packages/runtime/src/scr_number.h +++ b/packages/runtime/src/scr_number.h @@ -101,8 +101,9 @@ double scr_date_get_time(double ms); * 0–9999). THROWS Node's "Invalid time value" RangeError on NaN or * |ms| > 8.64e15 and returns NULL; +1 otherwise. */ ScrStr *scr_date_to_iso(double ms); -/* new Date(dateString).getTime(): the BOUNDED parse (the X509 validity - * shape + ECMA's format with an explicit offset); NaN elsewhere. */ +/* new Date(dateString).getTime() and Date.parse: V8's date parser (the + * ES5 format, then its legacy grammar: RFC 1123/2822, Date#toString, + * "Jan 2, 2024", ...). Zone-less dates are local time. Never throws. */ double scr_date_parse_get_time(ScrStr *s); /* Date.UTC over already-number arguments (frontend completes the spec's * defaults): MakeDay/MakeTime/TimeClip exactly — 0–99 maps to 1900+year, diff --git a/tests/corpus/6230-date-parse-legacy.ts b/tests/corpus/6230-date-parse-legacy.ts new file mode 100644 index 000000000..fdf4a5bcd --- /dev/null +++ b/tests/corpus/6230-date-parse-legacy.ts @@ -0,0 +1,112 @@ +// Date.parse and new Date(string) follow V8's date-string grammar: the ES5 +// date-time format first, then the legacy forms Node accepts (RFC 1123 and +// RFC 2822 HTTP dates, Date#toString output, month names, slashes, AM/PM, +// zone names and numeric offsets). Strings without a zone are local time, +// so every input is parsed under several zones, including DST transitions. +const fixed = [ + "Mon, 01 Jan 2024 00:00:00 GMT", + "Sun, 06 Nov 1994 08:49:37 GMT", + "Sunday, 06-Nov-94 08:49:37 GMT", + "Sun Nov 6 08:49:37 1994", + "Tue, 2 Jan 2024 03:04:05 +0100", + "2 Jan 2024 03:04 -0800", + "Mon Jan 01 2024 00:00:00 GMT+0000 (Coordinated Universal Time)", + "Thu Jan 01 1970 00:00:00 GMT-0500 (EST)", + "Jan 5", + "1/2", + "1/2/3", + "2024/01/02", + "2024/01/02 10:00", + "2024-01-02 10:00:00", + "2024-01-02T10:00:00", + "2024-01-02T10:00:00.5", + "2024-01-02T10:00:00.123456Z", + "2024-01-02T10:00:00+0530", + "2024-01-02T24:00:00Z", + "2024-01-02T24:00:01Z", + "2024-01-02T1:00Z", + " 2024-01-01", + "2024-01-01 ", + "Jan 2, 2024", + "January 2, 2024 10:00 PM", + "12:00 am Jan 1 2024", + "12:00 pm Jan 1 2024", + "13:00 pm Jan 1 2024", + "Jan 1 2024 10:00 GMT+5", + "Jan 1 2024 10:00 UTC+0530", + "Jan 1 2024 10:00 +05:30", + "Jan 1 2024 10:00 PST", + "Jan 1 2024 10:00 edt", + "Jan 1 2024 10:00 XYZ", + "Jan 1 2024 10.30", + "10:20:30 Jan 1 2024", + "Jan 1 2024 10:: GMT", + "Jan 1 2024 (a (nested) comment) 10:00", + "Jan 1 2024 10:00)", + "foo 2024-01-01", + "foo2024", + "May 2024", + "2024 May 3", + "3 May 2024", + "May 3 99", + "May 3 49", + "May 3 50", + "Feb 29 2023", + "Feb 30 2024 GMT", + "Dec 31 1969 23:59:59.999 GMT", + "2024-13-01", + "2024-02-31T00:00:00Z", + "+002024-01-01", + "-000000-01-01", + "-000001-01-01T00:00:00Z", + "+275760-09-13T00:00:00.000Z", + "+275760-09-13T00:00:00.001Z", + "Sat, 13 Sep 275760 00:00:00 GMT", + "Sat, 13 Sep 275760 00:00:01 GMT", + "-271821-04-20T00:00:00Z", + "Jan 1 9999999 GMT", + "2024-01-01T10:00:00\u0000junk", + "Jan 1
2024", + "Jan 1 2024", + "Jan 1 2024 été", + "0", + "12", + "2024", + "20240", + "", + "garbage", +]; +// A deterministic generator of date-like strings over V8's token classes. +let seed = 20240101; +function next(n: number): number { + seed = (seed * 1103515245 + 12345) % 2147483648; + return seed % n; +} +const pieces = ["Mon", "Tue,", "Wednesday", "Jan", "january", "FEB", "Sept", "dec", "ma", "1", "01", "2", "12", "13", "24", "31", "32", "59", "60", "99", "100", "2024", "1970", "0", "000", "0000", "123456", "-", "+", ":", "::", ".", ",", "/", " ", "\t", "(", ")", "(x)", "GMT", "UTC", "UT", "Z", "T", "t", "PST", "edt", "AM", "pm", "+0100", "-0800", "+01:00", "-8", "GMT+2", "GMT-0530", "999", "1000000", "x", "10:00", "23:59:59", "24:00", "12:30:45.123", "2024-01-15", "2024-01-15T10:00", "T10:00:00Z", "Jan1", "1Jan"]; +const generated: string[] = []; +for (let i = 0; i < 1500; i++) { + let s = ""; + const n = 1 + next(6); + for (let j = 0; j < n; j++) s += pieces[next(pieces.length)]! + (next(3) === 0 ? " " : ""); + generated.push(s); +} +for (const zone of ["UTC", "America/New_York", "Asia/Kolkata", "Australia/Lord_Howe"]) { + process.env.TZ = zone; + console.log("zone", zone); + for (const s of fixed) console.log(JSON.stringify(s), Date.parse(s), new Date(s).getTime()); + let valid = 0; + let sum = 0; + let hash = 0; + for (const s of generated) { + const t = Date.parse(s); + if (Number.isNaN(t)) continue; + valid++; + sum = (sum + (t % 1000003)) % 1000000007; + hash = (hash * 31 + (t % 65521)) % 2147483647; + } + console.log("generated", generated.length, "valid", valid, sum, hash); +} +// An HTTP conditional GET (the fresh package's comparison). +const lastModified = "Sun, 31 Dec 2023 00:00:00 GMT"; +const ifModifiedSince = "Mon, 01 Jan 2024 00:00:00 GMT"; +console.log("fresh", Date.parse(lastModified) <= Date.parse(ifModifiedSince)); diff --git a/tests/coverage-fixtures/divergences.ts b/tests/coverage-fixtures/divergences.ts index e13701a93..43c32a738 100644 --- a/tests/coverage-fixtures/divergences.ts +++ b/tests/coverage-fixtures/divergences.ts @@ -47,7 +47,7 @@ function sizeOf(s: Shape): number { } console.log(sizeOf(new Circle())); -// The native date parser takes ISO 8601 with an explicit offset. +// Date strings parse with V8's grammar natively, so none of these diverge. console.log(Date.parse("Jan 2, 2024"), new Date("2024/01/02").getTime(), Date.parse("2024-01-02T03:04:05Z")); const stamps = ["2024-03-04T05:06:07Z", "March 4, 2024"]; for (const stamp of stamps) console.log(Date.parse(stamp)); diff --git a/tests/harness/__snapshots__/coverage-divergences.txt b/tests/harness/__snapshots__/coverage-divergences.txt index 67c9b57e3..0b261f3d0 100644 --- a/tests/harness/__snapshots__/coverage-divergences.txt +++ b/tests/harness/__snapshots__/coverage-divergences.txt @@ -5,7 +5,7 @@ scriptc coverage tests/coverage-fixtures/divergences.ts fully static — all analyzed statements compile to native code. - differs from Node 8 sites (compiles; the native program can behave differently) + differs from Node 5 sites (compiles; the native program can behave differently) ×1 SC6001 passing a 'Tagged' where 'Labeled' is expected copies the object (converting to a record type with other properties compiles as a field copy): the write to 'target.label' updates only the copy, while… at tests/coverage-fixtures/divergences.ts:16:9 hint: declare the parameter or binding with the value's full type ('Tagged'), or make the shape a class — class instances keep their identity when passed as a base class @@ -20,13 +20,4 @@ scriptc coverage tests/coverage-fixtures/divergences.ts hint: results agree with Node for Latin-script text; for other scripts compare explicitly (for example by code point with < and >) so both runtimes agree ×1 SC6004 this 'Circle' reaches a cast to the unrelated class 'Square' in the callee: natively the checked cast throws a TypeError when it executes, while Node reads the members structurally at tests/coverage-fixtures/divergences.ts:48:20 - hint: narrow with 'instanceof Square' before casting, or cast to a base class or interface both classes share - ×1 SC6005 the date string 'Jan 2, 2024' parses to an invalid date (NaN) natively, while Node accepts it (the native parser takes ISO 8601 date-times with an explicit offset, such as '2024-01-02T03:04:05Z', and… - at tests/coverage-fixtures/divergences.ts:51:24 - hint: write the date in ISO 8601 with an explicit offset ('Z' or '+HH:MM'), or build it from numbers with Date.UTC - ×1 SC6005 the date string '2024/01/02' parses to an invalid date (NaN) natively, while Node accepts it (the native parser takes ISO 8601 date-times with an explicit offset, such as '2024-01-02T03:04:05Z', and … - at tests/coverage-fixtures/divergences.ts:51:49 - hint: write the date in ISO 8601 with an explicit offset ('Z' or '+HH:MM'), or build it from numbers with Date.UTC - ×1 SC6005 the date string 'March 4, 2024' parses to an invalid date (NaN) natively, while Node accepts it (the native parser takes ISO 8601 date-times with an explicit offset, such as '2024-01-02T03:04:05Z', a… - at tests/coverage-fixtures/divergences.ts:52:41 - hint: write the date in ISO 8601 with an explicit offset ('Z' or '+HH:MM'), or build it from numbers with Date.UTC \ No newline at end of file + hint: narrow with 'instanceof Square' before casting, or cast to a base class or interface both classes share \ No newline at end of file diff --git a/tests/harness/coverage.test.ts b/tests/harness/coverage.test.ts index 06f0e75a8..c7ba05248 100644 --- a/tests/harness/coverage.test.ts +++ b/tests/harness/coverage.test.ts @@ -46,9 +46,6 @@ test("divergences report as warnings, never as blockers", async () => { "SC6002", "SC6003", "SC6004", - "SC6005", - "SC6005", - "SC6005", ]); expect(coveragePasses(coverage, "blockers")).toBe(true); expect(coveragePasses(coverage, "divergences")).toBe(false); @@ -220,11 +217,11 @@ test("CLI: build --print=diagnostics reports failures and divergence warnings as }; expect(envelope.success).toBe(true); expect(envelope.artifact).toBe(join(outDir, "out.ir.json")); - expect(envelope.diagnostics.map((d) => d.severity)).toEqual(Array(8).fill("warning")); + expect(envelope.diagnostics.map((d) => d.severity)).toEqual(Array(5).fill("warning")); const human = build(fixture("divergences.ts")); expect(human.status).toBe(0); expect(human.stderr).toContain("warning SC6001"); - expect(human.stderr).toContain("8 warnings: the program compiled"); + expect(human.stderr).toContain("5 warnings: the program compiled"); } finally { rmSync(outDir, { recursive: true, force: true }); } From 40f15eb4b3df90d60aacac93d570febdd3a3212e Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Sun, 18 Oct 2026 12:17:36 -0700 Subject: [PATCH 02/83] Lower new Date(year, monthIndex, ...) as local time The multi-argument Date constructor was refused (SC2020), which blocked date-fns and consumer code that builds dates from calendar fields. Typed callers now lower to a seven-f64 runtime call (omitted trailing fields complete with the spec's defaults) and JavaScript callers pass their arguments through ToNumber in order. The runtime mirrors V8: MakeDay bounds the untruncated year and month, years 0-99 map to 1900-1999, fields roll over, and the local time value converts to UTC in the host zone with the same UTC(t) the date parser uses, then TimeClip. Spread arguments stay fenced. Co-Authored-By: Claude Opus 5.5 --- .../content/limitations/builtin-apis/date.mdx | 2 +- .../limitations/dates-and-timezones.mdx | 2 +- .../compiler/src/backend/llvm/lib-shared.ts | 2 + .../src/frontend/lowering/lower-classes.ts | 44 +++++++++++++++++-- .../src/frontend/lowering/surfaces.ts | 11 ++++- packages/compiler/src/ir/builtin-effects.ts | 1 + .../compiler/src/ir/builtin-signatures.ts | 10 +++++ packages/compiler/src/ir/int32-slots.ts | 1 + packages/compiler/surface-manifest.json | 2 +- .../corpus/6231-date-local-fields.ts.json | 6 +++ .../tests/corpus/6232-date-fields-js.cjs.json | 6 +++ packages/runtime/src/scr_dyn.h | 1 + packages/runtime/src/scr_lib.c | 29 +++++++++++- packages/runtime/src/scr_number.h | 5 +++ tests/corpus/6231-date-local-fields.ts | 36 +++++++++++++++ tests/corpus/6232-date-fields-js.cjs | 30 +++++++++++++ tests/diagnostics/stdlib-fence.ts | 2 +- .../harness/__snapshots__/stdlib-fence.ts.txt | 8 ++-- 18 files changed, 184 insertions(+), 14 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6231-date-local-fields.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6232-date-fields-js.cjs.json create mode 100644 tests/corpus/6231-date-local-fields.ts create mode 100644 tests/corpus/6232-date-fields-js.cjs diff --git a/docs/content/limitations/builtin-apis/date.mdx b/docs/content/limitations/builtin-apis/date.mdx index db5278c53..8d15ef917 100644 --- a/docs/content/limitations/builtin-apis/date.mdx +++ b/docs/content/limitations/builtin-apis/date.mdx @@ -1 +1 @@ -- **Date:** Date unions, year/month constructors, setters, statically typed TypeScript identity comparisons, thrown Date values, and locale/string formatters are unsupported. Zero-argument and single number/string constructors, storage and passing, `getTime`, `valueOf`, `toISOString`, `toJSON` (a `string | null`: null for an invalid date, as in Node), calendar getters, `getTimezoneOffset`, and single-string `Date.parse` compile. JavaScript Dates retain identity through checked storage and support Date-copy construction and JSON serialization. A TypeScript Date converted to `unknown` becomes a Date with the same time value; each conversion creates a new Date, so two `unknown` values converted from one TypeScript Date are not `===`. See [Dates and timezones](#dates-and-timezones) for parsing restrictions. +- **Date:** Date unions, spread constructor arguments, setters, statically typed TypeScript identity comparisons, thrown Date values, and locale/string formatters are unsupported. Zero-argument, single number/string, and local-time year/month field constructors, storage and passing, `getTime`, `valueOf`, `toISOString`, `toJSON` (a `string | null`: null for an invalid date, as in Node), calendar getters, `getTimezoneOffset`, and single-string `Date.parse` compile. JavaScript Dates retain identity through checked storage and support Date-copy construction and JSON serialization. A TypeScript Date converted to `unknown` becomes a Date with the same time value; each conversion creates a new Date, so two `unknown` values converted from one TypeScript Date are not `===`. See [Dates and timezones](#dates-and-timezones) for timezone data. diff --git a/docs/content/limitations/dates-and-timezones.mdx b/docs/content/limitations/dates-and-timezones.mdx index eacaf29d0..166757627 100644 --- a/docs/content/limitations/dates-and-timezones.mdx +++ b/docs/content/limitations/dates-and-timezones.mdx @@ -1,3 +1,3 @@ -String constructors and `Date.parse` use V8's date-string grammar: ECMAScript date-time strings and the legacy forms Node.js accepts, such as RFC 1123 HTTP dates (`Mon, 01 Jan 2024 00:00:00 GMT`), `Date#toString` output, and `Jan 2, 2024`. Strings without a timezone are local time. +String constructors and `Date.parse` use V8's date-string grammar: ECMAScript date-time strings and the legacy forms Node.js accepts, such as RFC 1123 HTTP dates (`Mon, 01 Jan 2024 00:00:00 GMT`), `Date#toString` output, and `Jan 2, 2024`. Strings without a timezone, and `new Date(year, monthIndex, ...)` fields, are local time. Local calendar getters and local-time conversions use the operating system's timezone database. Historical results can differ from Node.js when timezone data differs. For valid extreme years outside a platform calendar API's range, scriptc uses a calendar-equivalent year in the 400-year Gregorian cycle to obtain the timezone rule. diff --git a/packages/compiler/src/backend/llvm/lib-shared.ts b/packages/compiler/src/backend/llvm/lib-shared.ts index 02bf37192..df7b32889 100644 --- a/packages/compiler/src/backend/llvm/lib-shared.ts +++ b/packages/compiler/src/backend/llvm/lib-shared.ts @@ -306,6 +306,7 @@ export const LIB_FN_SYMS: Record = { "date.valueOf": "scr_date_get_time", "date.parseGetTime": "scr_date_parse_get_time", "date.utc": "scr_date_utc", + "date.newLocal": "scr_date_new_local", "date.getFullYear": "scr_date_get_full_year_local", "date.getUTCFullYear": "scr_date_get_full_year_utc", "date.getMonth": "scr_date_get_month_local", @@ -899,6 +900,7 @@ export const LIB_FN_SYMS: Record = { "dyn.nativeUrlIs": "scr_dyn_native_url_is", "dyn.nativeDateIs": "scr_dyn_native_date_is", "date.nativeNew": "scr_dyn_native_date_new", + "date.nativeNewFields": "scr_dyn_native_date_new_fields", "date.checkedValue": "scr_dyn_native_date_value", "dyn.nativeRegexIs": "scr_dyn_native_regex_is", "dyn.toString": "scr_dyn_to_string_argument", diff --git a/packages/compiler/src/frontend/lowering/lower-classes.ts b/packages/compiler/src/frontend/lowering/lower-classes.ts index 39b3242d6..0fd332217 100644 --- a/packages/compiler/src/frontend/lowering/lower-classes.ts +++ b/packages/compiler/src/frontend/lowering/lower-classes.ts @@ -8962,14 +8962,52 @@ export function lowerNew(lowerer: Lowerer, expr: ts.NewExpression): IrExpr { // fenced, so copying the scalar cannot create an observable lie. if (symbol && symbol.name === "Date" && lowerer.isStdlibSymbol(symbol)) { const args = expr.arguments ?? []; - if (args.some(ts.isSpreadElement) || args.length > 1) { + if (args.some(ts.isSpreadElement)) { lowerer.noLowering( - `new Date with ${args.length} arguments`, + `new Date with spread arguments`, expr, - "new Date(), new Date(milliseconds), and new Date(dateString) are supported; the local-time year/month field constructor has no lowering", + "new Date(), new Date(milliseconds), new Date(dateString), and new Date(year, monthIndex, ...) with listed arguments are supported", symbol, ); } + // new Date(year, monthIndex[, date[, hours[, minutes[, seconds[, ms]]]]]): + // local-time fields, converted to UTC in the host zone at run time. + // JavaScript callers pass their arguments to the runtime's ToNumber + // (in order, exactly the constructor's coercions); typed callers + // pass numbers, completed with the spec's defaults (date 1, time + // parts 0) for the seven-f64 ABI. + if (args.length > 1) { + if (isJsSourceFile(expr.getSourceFile())) { + return { + kind: "libCall", + fn: "date.nativeNewFields", + args: [ + { + kind: "dynArrLit", + elems: args.map((arg) => lowerer.lowerExprExpecting(arg, DYN)), + type: DYN, + loc, + }, + ], + type: DYN, + loc, + }; + } + if (args.length > 7) { + lowerer.noLowering(`new Date with ${args.length} arguments`, expr, undefined, symbol); + } + const defaults = [0, 0, 1, 0, 0, 0, 0]; + const fields: IrExpr[] = []; + for (let i = 0; i < 7; i++) { + const arg = args[i]; + fields.push( + arg !== undefined + ? lowerer.lowerExprExpecting(arg, F64) + : { kind: "numLit", value: defaults[i]!, type: F64, loc }, + ); + } + return { kind: "libCall", fn: "date.newLocal", args: fields, type: DATE_T, loc }; + } if (isJsSourceFile(expr.getSourceFile())) { return { kind: "libCall", diff --git a/packages/compiler/src/frontend/lowering/surfaces.ts b/packages/compiler/src/frontend/lowering/surfaces.ts index 561a58953..d06180952 100644 --- a/packages/compiler/src/frontend/lowering/surfaces.ts +++ b/packages/compiler/src/frontend/lowering/surfaces.ts @@ -2176,8 +2176,15 @@ export const AMBIENT_SURFACE_FNS: readonly AmbientSurfaceRow[] = [ id: "stdlib.date.constructor", kind: "stdlib", name: "Date constructor", - fns: ["date.newNow", "date.newMs", "date.newString", "date.nativeNew"], - note: "zero arguments, or one milliseconds/date-string argument; values are the read-only TimeClip scalar slice", + fns: [ + "date.newNow", + "date.newMs", + "date.newString", + "date.nativeNew", + "date.newLocal", + "date.nativeNewFields", + ], + note: "zero arguments, one milliseconds/date-string argument, or local-time year/month fields; values are the read-only TimeClip scalar slice", }, { id: "stdlib.date.UTC", diff --git a/packages/compiler/src/ir/builtin-effects.ts b/packages/compiler/src/ir/builtin-effects.ts index 1bb9cbd14..045e624ed 100644 --- a/packages/compiler/src/ir/builtin-effects.ts +++ b/packages/compiler/src/ir/builtin-effects.ts @@ -468,6 +468,7 @@ export const MAY_THROW_LIB_FNS: ReadonlySet = new Set([ "json.stringifyReplacer", "json.stringifyValue", "date.nativeNew", + "date.nativeNewFields", "date.checkedValue", "util.parseArgs", "util.parseEnv", diff --git a/packages/compiler/src/ir/builtin-signatures.ts b/packages/compiler/src/ir/builtin-signatures.ts index 1d14d8732..6119175c7 100644 --- a/packages/compiler/src/ir/builtin-signatures.ts +++ b/packages/compiler/src/ir/builtin-signatures.ts @@ -206,6 +206,10 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ "dyn.nativeUrlIs": { argTypes: [DYN], result: BOOL }, "dyn.nativeDateIs": { argTypes: [DYN], result: BOOL }, "date.nativeNew": { argTypes: [DYN], result: DYN }, + /** `new Date(year, monthIndex, ...)` in JavaScript: the argument array's + * first seven values pass through ToNumber in order (later ones are + * evaluated, never converted), then the local-time field constructor. */ + "date.nativeNewFields": { argTypes: [DYN], result: DYN }, "date.checkedValue": { argTypes: [DYN], result: DATE_T }, "dyn.nativeRegexIs": { argTypes: [DYN], result: BOOL }, /** toString() on a checked-dynamic receiver: runtime kind dispatch @@ -4198,6 +4202,12 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ * map to 1900+year, out-of-range months/dates roll over, non-finite * parts and out-of-range results answer NaN. Never throws. */ "date.utc": { argTypes: [F64, F64, F64, F64, F64, F64, F64], result: F64 }, + /** `new Date(year, monthIndex[, date, hours, minutes, seconds, ms])` — + * the same seven-f64 completion as date.utc, but the fields are LOCAL + * time: V8's MakeDay/MakeTime, then UTC(t) in the host zone (DST gaps + * read with the offset before the transition, overlaps answer the + * earlier instant), then TimeClip. Never throws. */ + "date.newLocal": { argTypes: [F64, F64, F64, F64, F64, F64, F64], result: DATE_T }, /** WHATWG TextDecoder.decode over u8 bytes (scr_bytes.c): utf-8 with * default options — the same maximal-subpart replacement decode as * Buffer.toString("utf8"), with the leading BOM stripped (the one diff --git a/packages/compiler/src/ir/int32-slots.ts b/packages/compiler/src/ir/int32-slots.ts index d7d06db7d..64faf0936 100644 --- a/packages/compiler/src/ir/int32-slots.ts +++ b/packages/compiler/src/ir/int32-slots.ts @@ -347,6 +347,7 @@ const DYN_READ_ONLY = new Set([ "arrayBuffer.viewU8", "arrayBuffer.viewU8C", "date.nativeNew", + "date.nativeNewFields", // The dynamic operators run the ToPrimitive and coercion helpers of // dyn.compare, dyn.numberConstructor and dyn.toStringCoerce, then build a // fresh number, string or bigint. dyn.increment steps dyn.toNumeric's result. diff --git a/packages/compiler/surface-manifest.json b/packages/compiler/surface-manifest.json index 471210669..852d054cc 100644 --- a/packages/compiler/surface-manifest.json +++ b/packages/compiler/surface-manifest.json @@ -4027,7 +4027,7 @@ "kind": "stdlib", "name": "Date constructor", "status": "static", - "note": "zero arguments, or one milliseconds/date-string argument; values are the read-only TimeClip scalar slice" + "note": "zero arguments, one milliseconds/date-string argument, or local-time year/month fields; values are the read-only TimeClip scalar slice" }, { "id": "stdlib.date.getDate", diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6231-date-local-fields.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6231-date-local-fields.ts.json new file mode 100644 index 000000000..6f078d295 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6231-date-local-fields.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6231-date-local-fields.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6232-date-fields-js.cjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6232-date-fields-js.cjs.json new file mode 100644 index 000000000..10f48c83a --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6232-date-fields-js.cjs.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6232-date-fields-js.cjs" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_dyn.h b/packages/runtime/src/scr_dyn.h index 667ceced0..0aeb9938f 100644 --- a/packages/runtime/src/scr_dyn.h +++ b/packages/runtime/src/scr_dyn.h @@ -600,6 +600,7 @@ ScrDyn *scr_dyn_native_search_params(ScrSearchParams *value); bool scr_dyn_native_search_params_is(const ScrDyn *value); bool scr_dyn_native_date_is(const ScrDyn *value); ScrDyn *scr_dyn_native_date_new(const ScrDyn *arguments); +ScrDyn *scr_dyn_native_date_new_fields(const ScrDyn *arguments); ScrDyn *scr_dyn_native_date_from_ms(double ms); double scr_dyn_native_date_value(const ScrDyn *value); ScrDyn *scr_dyn_native_regex(ScrRegex *value); diff --git a/packages/runtime/src/scr_lib.c b/packages/runtime/src/scr_lib.c index 2c232cbea..b4b834b2d 100644 --- a/packages/runtime/src/scr_lib.c +++ b/packages/runtime/src/scr_lib.c @@ -10656,7 +10656,7 @@ double scr_date_get_timezone_offset(double ms) { return scr_date_local_parts(ms, &p) ? p.timezone_offset : NAN; } -/* ── Local time to UTC ───────────────────────────────────────────────── +/* ── Local time to UTC, and the year/month field constructor ─────────── * The spec's UTC(t) over the host zone: t is a local wall-clock time * value. The offset in effect a day before and a day after t brackets any * transition (none come closer together than that in tzdata). A time in @@ -10753,6 +10753,20 @@ static double scr_date_from_local(double day, double time) { return scr_date_new_ms(scr_date_local_to_utc(trunc(t))); } +/* new Date(year, monthIndex[, date[, hours[, minutes[, seconds[, ms]]]]]) + * over already-number arguments (the frontend completes omitted trailing + * arguments with date 1 and time parts 0): years 0–99 map to 1900+year, + * fields roll over through MakeDay/MakeTime, and the local time converts + * to UTC in the host zone. Never throws. */ +double scr_date_new_local(double y, double mo, double d, + double h, double mi, double s, double ms) { + if (!isnan(y)) { + double yi = trunc(y); + if (yi >= 0 && yi <= 99) y = 1900 + yi; + } + return scr_date_from_local(scr_date_v8_make_day(y, mo, d), scr_date_v8_make_time(h, mi, s, ms)); +} + /* ── V8's date-string parser, the fallback ───────────────────────────── * scr_date_parse_get_time answers its bounded grammars directly and asks * this parser only when they produce NaN. It follows V8's DateParser @@ -11596,6 +11610,19 @@ ScrDyn *scr_dyn_native_date_new(const ScrDyn *arguments) { return scr_dyn_native_date_from_ms(ms); } +/* new Date(year, monthIndex, ...) in JavaScript: ToNumber over the first + * seven arguments in order (a throwing conversion stops there), the + * spec's defaults for the rest, then the local-time field constructor. */ +ScrDyn *scr_dyn_native_date_new_fields(const ScrDyn *arguments) { + double fields[7] = {0, 0, 1, 0, 0, 0, 0}; + size_t n = arguments->v.arr.len < 7 ? arguments->v.arr.len : 7; + for (size_t i = 0; i < n; i++) { + if (!scr_dyn_number_coerce_js(arguments->v.arr.items[i], &fields[i])) return NULL; + } + return scr_dyn_native_date_from_ms( + scr_date_new_local(fields[0], fields[1], fields[2], fields[3], fields[4], fields[5], fields[6])); +} + /* The Date.prototype methods scr_native_date_invoke answers. */ static const char *const scr_native_date_methods[] = { "getTime", "valueOf", "toISOString", "toJSON", "getFullYear", "getUTCFullYear", "getMonth", diff --git a/packages/runtime/src/scr_number.h b/packages/runtime/src/scr_number.h index 80911023e..752e50ec0 100644 --- a/packages/runtime/src/scr_number.h +++ b/packages/runtime/src/scr_number.h @@ -105,6 +105,11 @@ ScrStr *scr_date_to_iso(double ms); * ES5 format, then its legacy grammar: RFC 1123/2822, Date#toString, * "Jan 2, 2024", ...). Zone-less dates are local time. Never throws. */ double scr_date_parse_get_time(ScrStr *s); +/* new Date(year, monthIndex, date, hours, minutes, seconds, ms) over + * already-number arguments: V8's MakeDay/MakeTime over LOCAL fields, then + * UTC(t) in the host zone and TimeClip. Never throws. */ +double scr_date_new_local(double y, double mo, double d, + double h, double mi, double s, double ms); /* Date.UTC over already-number arguments (frontend completes the spec's * defaults): MakeDay/MakeTime/TimeClip exactly — 0–99 maps to 1900+year, * month/date rollover, NaN for non-finite parts, out-of-range results, diff --git a/tests/corpus/6231-date-local-fields.ts b/tests/corpus/6231-date-local-fields.ts new file mode 100644 index 000000000..6ff18c964 --- /dev/null +++ b/tests/corpus/6231-date-local-fields.ts @@ -0,0 +1,36 @@ +// new Date(year, monthIndex, date, hours, minutes, seconds, ms) builds a +// LOCAL time: fields roll over through MakeDay/MakeTime, years 0-99 mean +// 1900-1999, and the result converts to UTC in the host zone. A time in a +// DST gap reads with the offset before the transition; a time in an +// overlap answers the earlier instant. +const zones = ["UTC", "America/New_York", "Europe/Berlin", "Australia/Lord_Howe", "Asia/Kolkata", "America/St_Johns", "Pacific/Apia"]; +const years = [1800, 1969, 1970, 2000, 2024, 2038, 2100, 0, 49, 99, 100, -1, -271821, 275760]; +for (const zone of zones) { + process.env.TZ = zone; + const out: string[] = []; + for (const y of years) { + for (const m of [0, 2, 9, 11, 12, -1]) { + for (const d of [1, 31, 0]) out.push(`${y}/${m}/${d}=${new Date(y, m, d, 1, 30, 15, 500).getTime()}`); + } + } + console.log(zone, out.join(" ")); + // Every quarter hour across a spring-forward and a fall-back day. + const y = 2024; + const m = zone === "America/New_York" || zone === "Europe/Berlin" ? 2 : 9; + const d = zone === "America/New_York" ? 10 : zone === "Europe/Berlin" ? 31 : 6; + const day: string[] = []; + for (let minute = 0; minute < 24 * 60; minute += 15) { + const spring = new Date(y, m, d, 0, minute); + const fall = new Date(y, m + 7, d, 0, minute); + day.push(`${spring.getTime()}:${spring.getHours()}:${fall.getTime()}:${fall.getMinutes()}`); + } + console.log(zone, day.join(" ")); + const date = new Date(2020, 0, 15, 12, 30, 45, 123); + console.log(zone, date.getTime(), date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds(), date.getTimezoneOffset()); +} +process.env.TZ = "America/New_York"; +console.log(new Date(2020, 0).getTime(), new Date(2020, 0, 1, 0, 0, 0, 0.9).getTime(), new Date(2020.7, 0.9).getTime(), new Date(-0.5, 0).getTime()); +console.log(new Date(1e6, 0).getTime(), new Date(1e6 + 0.5, 0).getTime(), new Date(2020, 1e7).getTime(), new Date(2020, 1e7 + 1).getTime()); +console.log(new Date(NaN, 0).getTime(), new Date(2020, 0, Infinity).getTime(), new Date(2020, 0, 1, -Infinity).getTime()); +console.log(new Date(275760, 8, 13).getTime(), new Date(275760, 8, 12, 20).getTime(), new Date(-271821, 3, 19, 20).getTime()); +console.log(new Date(2021, 6, 4, 9, 5).toISOString(), new Date(99, 11, 31, 23, 59, 59, 999).toISOString()); diff --git a/tests/corpus/6232-date-fields-js.cjs b/tests/corpus/6232-date-fields-js.cjs new file mode 100644 index 000000000..a9cef51fa --- /dev/null +++ b/tests/corpus/6232-date-fields-js.cjs @@ -0,0 +1,30 @@ +// JavaScript callers of the Date field constructor and the date-string +// parser: arguments pass through ToNumber in order (valueOf runs, strings +// and booleans convert, a throwing conversion stops the rest), arguments +// past the seventh are evaluated but never converted, and RFC 1123 dates +// parse the way HTTP caching middleware reads them. +process.env.TZ = "Europe/Berlin"; +const log = []; +const arg = (name, value) => ({ valueOf() { log.push(name); return value; } }); +const d = new Date(arg("y", 2024), arg("m", 1), arg("d", 29), arg("h", 13), arg("mi", 5), arg("s", 6), arg("ms", 7), arg("extra", 8)); +console.log(d.getTime(), d.toISOString(), log.join(",")); +console.log(new Date("2024", "0", "15").getTime(), new Date(2024, true, null).getTime(), new Date(2024, undefined).getTime()); +console.log(new Date(2024, 0, "x").getTime(), new Date(99, 0).getFullYear(), new Date(2024, 0, 1, 2, 3).getHours()); +try { + new Date(2024, arg("m", 0), { valueOf() { throw new Error("conversion stopped"); } }, arg("h", 1)); +} catch (e) { + console.log(e.message, log.slice(-1).join(",")); +} +function isFresh(reqHeaders, resHeaders) { + const modifiedSince = reqHeaders["if-modified-since"]; + const lastModified = resHeaders["last-modified"]; + if (!modifiedSince || !lastModified) return false; + const lm = Date.parse(lastModified); + const ms = Date.parse(modifiedSince); + return !(isNaN(lm) || isNaN(ms) || lm > ms); +} +console.log(isFresh({ "if-modified-since": "Mon, 01 Jan 2024 00:00:00 GMT" }, { "last-modified": "Sun, 31 Dec 2023 00:00:00 GMT" })); +console.log(isFresh({ "if-modified-since": "Sun, 31 Dec 2023 00:00:00 GMT" }, { "last-modified": "Mon, 01 Jan 2024 00:00:00 GMT" })); +console.log(isFresh({ "if-modified-since": "not a date" }, { "last-modified": "Mon, 01 Jan 2024 00:00:00 GMT" })); +const stamps = ["Tue, 15 Nov 1994 08:12:31 GMT", "Tuesday, 15-Nov-94 08:12:31 GMT", "Tue Nov 15 08:12:31 1994", "2024-03-31 02:30"]; +for (const s of stamps) console.log(s, Date.parse(s), new Date(s).getTime()); diff --git a/tests/diagnostics/stdlib-fence.ts b/tests/diagnostics/stdlib-fence.ts index 11b516b99..ae456af90 100644 --- a/tests/diagnostics/stdlib-fence.ts +++ b/tests/diagnostics/stdlib-fence.ts @@ -20,7 +20,7 @@ const parsedMs = Date.parse; const fmt = new Intl.NumberFormat(); // Constructors. -const d = new Date(2024, 0); // the local-time year/month field constructor stays fenced +const d = new Date(...([2024, 0] as const)); // spread field arguments stay fenced const mutableDate = new Date(0); mutableDate.setUTCFullYear(2024); // mutation is outside the read-only Date slice const sameDate = mutableDate === mutableDate; // scalar storage must not fake object identity diff --git a/tests/harness/__snapshots__/stdlib-fence.ts.txt b/tests/harness/__snapshots__/stdlib-fence.ts.txt index 337354eec..908ab1cff 100644 --- a/tests/harness/__snapshots__/stdlib-fence.ts.txt +++ b/tests/harness/__snapshots__/stdlib-fence.ts.txt @@ -50,14 +50,14 @@ stdlib-fence.ts:20:13 - error SC2020: 'new Intl.NumberFormat' is part of the sta hint: the type checker sees the full standard library, but only the supported surface compiles (https://scriptc.dev/limitations) -stdlib-fence.ts:23:11 - error SC2020: 'new Date with 2 arguments' is part of the standard library types but has no scriptc lowering yet +stdlib-fence.ts:23:11 - error SC2020: 'new Date with spread arguments' is part of the standard library types but has no scriptc lowering yet 22 | // Constructors. - 23 | const d = new Date(2024, 0); // the local-time year/month field constructor stays fenced - | ^~~~~~~~~~~~~~~~~ + 23 | const d = new Date(...([2024, 0] as const)); // spread field arguments stay fenced + | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 24 | const mutableDate = new Date(0); - hint: new Date(), new Date(milliseconds), and new Date(dateString) are supported; the local-time year/month field constructor has no lowering + hint: new Date(), new Date(milliseconds), new Date(dateString), and new Date(year, monthIndex, ...) with listed arguments are supported stdlib-fence.ts:25:1 - error SC2020: 'Date.prototype.setUTCFullYear with arguments' is part of the standard library types but has no scriptc lowering yet From 1144e3cea421e3ac322799a4f0abd0093d6caeb6 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Sun, 18 Oct 2026 12:40:20 -0700 Subject: [PATCH 03/83] Make plain-object constructor and Object.prototype method identity exact fast-deep-equal and every isPlainObject idiom compare `o.constructor === Object` and `o.valueOf === Object.prototype.valueOf`. Natively a dynamic read of an inherited Object.prototype member answered undefined and the JavaScript identifier `Object` was a string identity token, so equal({a: 1}, {a: 1}) was false with no diagnostic. The builtin constructors Object, Function, Date, RegExp, Map, Set, Promise, WeakMap and WeakSet now read as interned function values (in TypeScript too, where they were refused), and dynamic values answer `.constructor` with the same functions: plain objects (including Object.create chains), arrays, functions, class instances and the native Date/RegExp/Map/Set/WeakMap/WeakSet/Promise values. A member no nearer prototype defines falls back to the interned Object.prototype method `Object.prototype.` reads (valueOf, toString, hasOwnProperty, propertyIsEnumerable), respecting the types that define their own toString or valueOf. Typed records and class instances read these members through their dynamic view instead of hitting the stdlib fence. `instanceof` accepts the constructors, and `Object(v)` / `new Object()` work through a stored value; calling the other constructors through one stays fenced. Co-Authored-By: Claude Opus 5.5 --- .../limitations/builtin-apis/prototypes.mdx | 2 +- .../compiler/src/backend/llvm/lib-shared.ts | 1 + .../lowering/class-dynamic-dispatch.ts | 103 ++++++-- .../src/frontend/lowering/lower-exprs.ts | 107 +++++++- .../src/frontend/lowering/lower-stmts.ts | 8 + .../compiler/src/ir/builtin-signatures.ts | 5 + packages/compiler/src/ir/ir.ts | 7 +- .../6233-object-identity-dynamic.cjs.json | 6 + .../corpus/6235-object-identity-typed.ts.json | 6 + packages/runtime/src/scr_dyn.h | 3 + packages/runtime/src/scr_dyn_invoke.c | 6 + packages/runtime/src/scr_json.c | 237 ++++++++++++++++++ tests/corpus/6233-object-identity-dynamic.cjs | 86 +++++++ tests/corpus/6235-object-identity-typed.ts | 24 ++ 14 files changed, 577 insertions(+), 24 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6233-object-identity-dynamic.cjs.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6235-object-identity-typed.ts.json create mode 100644 tests/corpus/6233-object-identity-dynamic.cjs create mode 100644 tests/corpus/6235-object-identity-typed.ts diff --git a/docs/content/limitations/builtin-apis/prototypes.mdx b/docs/content/limitations/builtin-apis/prototypes.mdx index 9cfc041ed..441884bfc 100644 --- a/docs/content/limitations/builtin-apis/prototypes.mdx +++ b/docs/content/limitations/builtin-apis/prototypes.mdx @@ -1 +1 @@ -- **Prototypes:** Named properties and methods on program class prototypes support replacement, inheritance, and deletion. Bare prototype reflection, accessor replacement, and prototype storage on local, generic, mixin, or runtime-provided classes are unsupported. +- **Prototypes:** Named properties and methods on program class prototypes support replacement, inheritance, and deletion. Bare prototype reflection, accessor replacement, and prototype storage on local, generic, mixin, or runtime-provided classes are unsupported. Plain objects, arrays, functions, class instances, and Date, RegExp, Map, Set, WeakMap, WeakSet, and Promise values inherit `constructor`, `valueOf`, `toString`, `hasOwnProperty`, and `propertyIsEnumerable` from `Object.prototype` as in Node, so `value.constructor === Object` and `value.valueOf === Object.prototype.valueOf` compare identities exactly. The constructors `Object`, `Function`, `Date`, `RegExp`, `Map`, `Set`, `WeakMap`, `WeakSet`, and `Promise` read as function values; through a stored value only `Object` can be called or constructed. diff --git a/packages/compiler/src/backend/llvm/lib-shared.ts b/packages/compiler/src/backend/llvm/lib-shared.ts index df7b32889..3f36e0d7c 100644 --- a/packages/compiler/src/backend/llvm/lib-shared.ts +++ b/packages/compiler/src/backend/llvm/lib-shared.ts @@ -899,6 +899,7 @@ export const LIB_FN_SYMS: Record = { "dyn.nativeMapIs": "scr_dyn_native_map_is", "dyn.nativeUrlIs": "scr_dyn_native_url_is", "dyn.nativeDateIs": "scr_dyn_native_date_is", + "dyn.globalConstructor": "scr_dyn_global_constructor", "date.nativeNew": "scr_dyn_native_date_new", "date.nativeNewFields": "scr_dyn_native_date_new_fields", "date.checkedValue": "scr_dyn_native_date_value", diff --git a/packages/compiler/src/frontend/lowering/class-dynamic-dispatch.ts b/packages/compiler/src/frontend/lowering/class-dynamic-dispatch.ts index 120032bf5..0477e5c3e 100644 --- a/packages/compiler/src/frontend/lowering/class-dynamic-dispatch.ts +++ b/packages/compiler/src/frontend/lowering/class-dynamic-dispatch.ts @@ -2062,6 +2062,49 @@ export class ClassDynamicDispatch { // A read compares the bag with the receiver and then reads it: // evaluate it once, so each read retains the receiver once. const bagRead = varRef("bag.0", DYN, loc); + const bagGet: IrExpr | null = read + ? { + kind: "libCall", + fn: "dyn.bagGet", + args: [bagRead, read.key, varRef("p.0", DYN, loc)], + type: DYN, + loc, + } + : null; + // A class instance whose class chain declares no member of an + // Object.prototype method's name (classes that do answer in + // their own branches) inherits it unless an own property + // shadows it; a class constructor's static bag reads as before. + const instanceRead: IrExpr | null = + read && INHERITED_OBJECT_METHODS.has(name) + ? { + kind: "ternary", + cond: { + kind: "dynTest", + test: "function", + value: varRef("p.0", DYN, loc), + type: BOOL, + loc, + }, + then: bagGet!, + else_: { + kind: "ternary", + cond: this.readBagHas(lowerer, "dyn.hasKey", bagRead, read.key, loc), + then: bagGet!, + else_: { + kind: "libCall", + fn: "dyn.builtinMethod", + args: [{ kind: "strLit", value: "Object", type: STRING, loc }, read.key], + type: DYN, + loc, + }, + type: DYN, + loc, + }, + type: DYN, + loc, + } + : bagGet; const fallback: IrExpr = read ? { kind: "ternary", @@ -2073,13 +2116,7 @@ export class ClassDynamicDispatch { loc, }, then: { ...read, value: bagRead }, - else_: { - kind: "libCall", - fn: "dyn.bagGet", - args: [bagRead, read.key, varRef("p.0", DYN, loc)], - type: DYN, - loc, - }, + else_: instanceRead!, type: DYN, loc, } @@ -4681,19 +4718,40 @@ export class ClassDynamicDispatch { if (!field && !accessor) { const instance = lowerer.coerceToExpected(receiver, DYN); const bag = this.readPropertyBagOrEmpty(lowerer, instance, loc); - return [ - { - kind: "return", - value: { - kind: "libCall", - fn: "dyn.bagGet", - args: [bag, { kind: "strLit", value: name, type: STRING, loc }, instance], - type: DYN, + const key: IrExpr = { kind: "strLit", value: name, type: STRING, loc }; + const own: IrExpr = { + kind: "libCall", + fn: "dyn.bagGet", + args: [bag, key, instance], + type: DYN, + loc, + }; + // An Object.prototype method no class in the chain declares: an + // own property shadows it, otherwise the read answers the same + // interned value `Object.prototype.` reads. + if (INHERITED_OBJECT_METHODS.has(name) && !findMethodOn(lowerer, info, name)) { + return [ + { + kind: "return", + value: { + kind: "ternary", + cond: this.readBagHas(lowerer, "dyn.hasKey", bag, key, loc), + then: own, + else_: { + kind: "libCall", + fn: "dyn.builtinMethod", + args: [{ kind: "strLit", value: "Object", type: STRING, loc }, key], + type: DYN, + loc, + }, + type: DYN, + loc, + }, loc, }, - loc, - }, - ]; + ]; + } + return [{ kind: "return", value: own, loc }]; } const type = field ?? accessor!.sig.ret; if (!canConvertToDyn(type, getRecord, getUnion)) return fence(); @@ -5216,3 +5274,12 @@ export class ClassDynamicDispatch { return body; } } + +/** Object.prototype methods every class instance inherits unless its class + * chain declares them. */ +const INHERITED_OBJECT_METHODS: ReadonlySet = new Set([ + "valueOf", + "toString", + "hasOwnProperty", + "propertyIsEnumerable", +]); diff --git a/packages/compiler/src/frontend/lowering/lower-exprs.ts b/packages/compiler/src/frontend/lowering/lower-exprs.ts index d4d03591d..d3f1f4676 100644 --- a/packages/compiler/src/frontend/lowering/lower-exprs.ts +++ b/packages/compiler/src/frontend/lowering/lower-exprs.ts @@ -1450,8 +1450,12 @@ function lowerExprInner(lowerer: Lowerer, expr: ts.Expression): IrExpr { { const sym = lowerer.checker.getSymbolAtLocation(expr); if (lowerer.isStdlibSymbol(sym) || expr.text === "globalThis") { + const canonical = stdlibGlobalNameOf(lowerer, expr) ?? expr.text; + // Builtin constructors have real function values in both source + // languages (TypeScript reads them as dynamic values). + const constructorValue = builtinGlobalValue(canonical, loc); + if (constructorValue !== null) return constructorValue; if (isJsSourceFile(expr.getSourceFile())) { - const canonical = stdlibGlobalNameOf(lowerer, expr) ?? expr.text; return { kind: "strLit", value: `[builtin ${canonical}]`, type: STRING, loc }; } // The families with a WHY: each hint states what makes the @@ -2349,7 +2353,14 @@ function lowerExprInner(lowerer: Lowerer, expr: ts.Expression): IrExpr { type: DYN, loc, }; - return { kind: "strLit", value: `[builtin ${canonical}]`, type: STRING, loc }; + return ( + builtinGlobalValue(canonical, loc) ?? { + kind: "strLit", + value: `[builtin ${canonical}]`, + type: STRING, + loc, + } + ); } } // A JS receiver whose CHECKER type has no mapping (`mustCallChecks @@ -2428,6 +2439,45 @@ function lowerExprInner(lowerer: Lowerer, expr: ts.Expression): IrExpr { }; } } + // A plain record's inherited Object.prototype members READ as values + // (`o.constructor`, `o.valueOf`, `({}).toString`, the isPlainObject and + // deep-equal idioms): the record boxes into its dynamic view, whose + // read answers the interned Object constructor and Object.prototype + // methods — the same values `Object` and `Object.prototype.valueOf` + // read, so identity comparisons are exact. Calls keep their own + // lowerings; a record declaring the name reads its field. + if ( + OBJECT_PROTOTYPE_VALUE_READS.has(expr.name.text) && + !isCalleeOf(expr) && + // `X.prototype.toString` keeps the builtin prototype-method lowering. + !(ts.isPropertyAccessExpression(expr.expression) && expr.expression.name.text === "prototype") + ) { + const receiver = tryLowerExpression(lowerer, expr.expression); + const name = expr.name.text; + const shape = + receiver?.type.kind === "record" ? lowerer.shapes.get(receiver.type.shapeId) : undefined; + const info = + receiver?.type.kind === "object" ? lowerer.classes.get(receiver.type.className) : undefined; + const inherits = + receiver?.type.kind === "dyn" || + (shape !== undefined && + !shape.tuple && + !shape.fields.some((field) => field.name === name)) || + (info !== undefined && + name !== "constructor" && + !info.fields.has(name) && + !lowerer.findMethodOn(info, name) && + !lowerer.findMethodOn(info, `get:${name}`)); + if (receiver !== null && receiver !== undefined && inherits) { + return { + kind: "dynKeyGet", + value: lowerer.coerceToExpected(receiver, DYN), + key: { kind: "strLit", value: name, type: STRING, loc }, + type: DYN, + loc, + }; + } + } // `f.name` / `f.length` / own properties on a FUNCTION-typed JS // value (the mustCall wrapper's function-instance members): read // through the dyn box — the closure's own-property table answers @@ -16263,3 +16313,56 @@ function isBuiltinModuleValueDecl(decl: ts.Node): boolean { ts.isVariableDeclaration(decl)) ); } + +/** The builtin constructors JavaScript sources read as real function + * values rather than identity tokens: the runtime interns one function per + * constructor, and dynamic values answer `.constructor` with the same one, + * so `o.constructor === Object` is an exact identity test. */ +const GLOBAL_CONSTRUCTOR_VALUES: ReadonlySet = new Set([ + "Object", + "Function", + "Date", + "RegExp", + "Map", + "Set", + "Promise", + "WeakMap", + "WeakSet", +]); + +/** The value a JavaScript source reads for the stdlib global `canonical`, + * or null when it stays an identity token. */ +export function builtinGlobalValue(canonical: string, loc: SrcLoc): IrExpr | null { + if (GLOBAL_CONSTRUCTOR_VALUES.has(canonical)) { + return { + kind: "libCall", + fn: "dyn.globalConstructor", + args: [{ kind: "strLit", value: canonical, type: STRING, loc }], + type: DYN, + loc, + }; + } + return null; +} + +/** Object.prototype members a plain record inherits, read as values. */ +const OBJECT_PROTOTYPE_VALUE_READS: ReadonlySet = new Set([ + "constructor", + "valueOf", + "toString", + "hasOwnProperty", + "propertyIsEnumerable", +]); + +/** Whether `expr` is the callee of a call (`o.toString()`), which keeps its + * call lowering rather than reading the member as a value. */ +function isCalleeOf(expr: ts.PropertyAccessExpression): boolean { + let node: ts.Node = expr; + while (node.parent !== undefined && ts.isParenthesizedExpression(node.parent)) node = node.parent; + const parent = node.parent; + return ( + parent !== undefined && + (ts.isCallExpression(parent) || ts.isNewExpression(parent)) && + parent.expression === node + ); +} diff --git a/packages/compiler/src/frontend/lowering/lower-stmts.ts b/packages/compiler/src/frontend/lowering/lower-stmts.ts index 0f1b606ef..78c3e76d3 100644 --- a/packages/compiler/src/frontend/lowering/lower-stmts.ts +++ b/packages/compiler/src/frontend/lowering/lower-stmts.ts @@ -169,6 +169,7 @@ import { import { globalSymbolKey } from "./expressions/global-symbols.js"; import { lowerShortCircuitAssignment } from "./expressions/nullish-assignment.js"; import { + builtinGlobalValue, lowerEnvironmentKey, lowerNativeFunctionAssignment, lowerStrictEquality, @@ -3334,6 +3335,9 @@ function stdlibGlobalTokenDestructure( const canonical = member.name === "global" ? "globalThis" : member.name; token = `[builtin ${canonical}]`; alias = canonical; + // Builtin constructors read as their interned function values, the + // same values the bare and `globalThis.X` spellings read. + value = builtinGlobalValue(canonical, locOf(el)); } else if (globalName === "console" && !CONSOLE_CALL_MEMBERS.has(propName)) { token = `[builtin console.${propName}]`; } else if (TOKEN_OPAQUE_GLOBALS.has(globalName)) { @@ -3377,6 +3381,10 @@ function stdlibGlobalTokenDestructure( const out: IrStmt[] = []; for (const b of binds) { const loc = locOf(b.name); + if (b.value !== undefined && b.alias !== null) { + const symbol = lowerer.checker.getSymbolAtLocation(b.name); + if (symbol) lowerer.stdlibGlobalAliases.set(symbol, b.alias); + } if (b.value !== undefined) { if (b.g !== undefined) { out.push({ diff --git a/packages/compiler/src/ir/builtin-signatures.ts b/packages/compiler/src/ir/builtin-signatures.ts index 6119175c7..74956d161 100644 --- a/packages/compiler/src/ir/builtin-signatures.ts +++ b/packages/compiler/src/ir/builtin-signatures.ts @@ -205,6 +205,11 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ "dyn.nativeMapIs": { argTypes: [DYN], result: BOOL }, "dyn.nativeUrlIs": { argTypes: [DYN], result: BOOL }, "dyn.nativeDateIs": { argTypes: [DYN], result: BOOL }, + /** A builtin constructor (`Object`, `Date`, `RegExp`, `Map`, `Set`, + * `Promise`, `WeakMap`, `WeakSet`) read as a JavaScript value: one + * interned function per name, the same one dynamic values answer for + * `.constructor`. The name is a compiler-chosen literal. */ + "dyn.globalConstructor": { argTypes: [STRING], result: DYN }, "date.nativeNew": { argTypes: [DYN], result: DYN }, /** `new Date(year, monthIndex, ...)` in JavaScript: the argument array's * first seven values pass through ToNumber in order (later ones are diff --git a/packages/compiler/src/ir/ir.ts b/packages/compiler/src/ir/ir.ts index 5382a6735..21c14a60b 100644 --- a/packages/compiler/src/ir/ir.ts +++ b/packages/compiler/src/ir/ir.ts @@ -3214,9 +3214,10 @@ export type IrExpr = /** Keyed read on a dyn value — `pkg.name` / `pkg["k"]` / the * `pkg?.scripts` chain step on a JSON.parse result. `key` is * string-typed (a strLit for the dot form); `type` is always dyn. An - * OBJ receiver answers the member (+1) or the undefined singleton (the - * own-property answer — prototype members like `toString` answer - * undefined, SEMANTICS.md); ARR answers `length` and canonical + * OBJ receiver answers the member (+1), an inherited Object.prototype + * member (the Object constructor and the interned valueOf/toString/ + * hasOwnProperty/propertyIsEnumerable values), or the undefined singleton; + * ARR answers `length` and canonical * in-range indices, STR answers `length` (UTF-16-exact), both * undefined otherwise; NUM/BOOL/BYTES answer undefined. An * undefined/null receiver THROWS the catchable Node-shaped TypeError diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6233-object-identity-dynamic.cjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6233-object-identity-dynamic.cjs.json new file mode 100644 index 000000000..a5dd87184 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6233-object-identity-dynamic.cjs.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6233-object-identity-dynamic.cjs" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6235-object-identity-typed.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6235-object-identity-typed.ts.json new file mode 100644 index 000000000..c48f4164b --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6235-object-identity-typed.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6235-object-identity-typed.ts" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_dyn.h b/packages/runtime/src/scr_dyn.h index 0aeb9938f..738c5d487 100644 --- a/packages/runtime/src/scr_dyn.h +++ b/packages/runtime/src/scr_dyn.h @@ -652,6 +652,9 @@ ScrDyn *scr_dyn_symbol_ref(ScrSym *value, void (*release)(ScrSym *), ScrStr *(*r ScrDyn *scr_dyn_new_str(ScrStr *s); ScrDyn *scr_dyn_new_arr(void); ScrDyn *scr_dyn_array_constructor(void); +/* The interned builtin constructor `name` (Object, Date, RegExp, Map, Set, + * Promise, WeakMap, WeakSet) as a dynamic function value: +1. */ +ScrDyn *scr_dyn_global_constructor(const ScrStr *name); bool scr_dyn_arr_has_index(const ScrDyn *arr, size_t index); void scr_dyn_arr_push_hole(ScrDyn *arr); ScrDyn *scr_dyn_new_obj(void); diff --git a/packages/runtime/src/scr_dyn_invoke.c b/packages/runtime/src/scr_dyn_invoke.c index a3085399f..086555975 100644 --- a/packages/runtime/src/scr_dyn_invoke.c +++ b/packages/runtime/src/scr_dyn_invoke.c @@ -1712,6 +1712,12 @@ static ScrDyn *dyn_builtin_method_call(ScrClosure *closure, ScrDyn *const *args, } ScrDyn *scr_dyn_builtin_method(const ScrStr *prototype, const ScrStr *method) { + /* Object.prototype's shared methods: the interned values dynamic reads + * of inherited members answer too. */ + if (!strcmp(prototype->data, "Object")) { + ScrDyn *shared = scr_dyn_object_proto_method(method->data); + if (shared) return shared; + } if (!dyn_builtin_methods) { dyn_builtin_methods = scr_dyn_new_obj_null_proto(); scr_atexit(dyn_builtin_methods_cleanup); diff --git a/packages/runtime/src/scr_json.c b/packages/runtime/src/scr_json.c index b23e78241..2d38f54e5 100644 --- a/packages/runtime/src/scr_json.c +++ b/packages/runtime/src/scr_json.c @@ -1533,6 +1533,13 @@ ScrDyn *scr_dyn_map_seed_entry(const ScrDyn *entry) { double scr_dyn_arr_len(const ScrDyn *d) { return d->kind == SCR_DYN_ARR ? (double)d->v.arr.len : 0; } +enum { + SCR_INHERIT_VALUE_OF = 1, SCR_INHERIT_TO_STRING = 2, SCR_INHERIT_OWN = 4, + SCR_INHERIT_ALL = 7, +}; +static ScrDyn *scr_dyn_object_proto_inherited(const char *key, size_t key_len, int methods); +static ScrDyn *scr_dyn_global_ctor_named(const char *name, size_t len); + ScrDyn *scr_dyn_arr_at(const ScrDyn *d, double i) { if (d->kind != SCR_DYN_ARR || i < 0 || i >= (double)d->v.arr.len) { return scr_dyn_retain(scr_dyn_undefined()); @@ -1557,6 +1564,15 @@ ScrDyn *scr_dyn_arr_named_get(const ScrDyn *d, const ScrStr *key) { scr_dyn_release(value); return scr_dyn_array_constructor(); } + /* Array.prototype defines its own toString; valueOf, hasOwnProperty and + * propertyIsEnumerable come from Object.prototype. */ + if (value && value->kind == SCR_DYN_UNDEF && !d->null_proto && !d->prototype && key->len != 11) { + ScrDyn *inherited = scr_dyn_object_proto_inherited(key->data, key->len, SCR_INHERIT_VALUE_OF | SCR_INHERIT_OWN); + if (inherited) { + scr_dyn_release(value); + return inherited; + } + } return value; } @@ -3375,6 +3391,163 @@ ScrDyn *scr_dyn_array_constructor(void) { } return scr_dyn_retain(scr_array_ctor); } +/* ── The builtin constructors as dynamic values ──────────────────────── + * JavaScript sources read `Object`, `Date`, `RegExp`, `Map`, `Set`, + * `Promise`, `WeakMap` and `WeakSet` as these interned functions, and the + * same functions answer `.constructor` on the matching dynamic values, so + * `o.constructor === Object` and `a.constructor === b.constructor` are + * exact identity tests. `Object(v)` and `new Object()` work through the + * stored value; calling the others through it is fenced. */ +static const char *const scr_global_ctor_names[] = { + "Object", "Date", "RegExp", "Map", "Set", "Promise", "WeakMap", "WeakSet", +}; +#define SCR_GLOBAL_CTORS (sizeof scr_global_ctor_names / sizeof scr_global_ctor_names[0]) +static SCR_TL ScrDyn *scr_global_ctors[SCR_GLOBAL_CTORS]; +static SCR_TL bool scr_global_ctors_registered; + +static void scr_global_ctors_cleanup(void) { + for (size_t i = 0; i < SCR_GLOBAL_CTORS; i++) { + scr_dyn_release(scr_global_ctors[i]); + scr_global_ctors[i] = NULL; + } +} + +static ScrDyn *scr_global_ctor_call(ScrClosure *closure, ScrDyn *const *args, size_t argc) { + const char *name = closure->fn; + if (strcmp(name, "Object") == 0) { + const ScrDyn *value = argc ? args[0] : scr_dyn_undefined(); + if (value->kind == SCR_DYN_UNDEF || value->kind == SCR_DYN_NULL) return scr_dyn_new_obj(); + if (value->kind != SCR_DYN_NUM && value->kind != SCR_DYN_STR && value->kind != SCR_DYN_BOOL && + value->kind != SCR_DYN_BIGINT && value->kind != SCR_DYN_SYMBOL) { + return scr_dyn_retain((ScrDyn *)value); + } + } + ScrJsonBuf message; + scr_jb_init(&message); + scr_jb_puts(&message, "calling the "); + scr_jb_puts(&message, name); + scr_jb_puts(&message, strcmp(name, "Object") == 0 ? " constructor on a primitive" : " constructor"); + scr_jb_puts(&message, " through a stored value is not supported yet"); + ScrStr *text = scr_jb_finish(&message); + scr_throw_error_msg_code(SCR_ERR_ERROR, text->data, text->len, "SC2020"); + scr_str_release(text); + return NULL; +} + +static ScrDyn *scr_dyn_global_ctor_at(size_t index) { + if (!scr_global_ctors_registered) { + scr_global_ctors_registered = true; + scr_atexit(scr_global_ctors_cleanup); + } + if (!scr_global_ctors[index]) { + const char *name = scr_global_ctor_names[index]; + scr_global_ctors[index] = scr_dyn_new_func(scr_closure_new((void *)name, 0), scr_global_ctor_call, + index == 0 ? 1 : 0, "native:constructor", name); + } + return scr_dyn_retain(scr_global_ctors[index]); +} + +static ScrDyn *scr_dyn_global_ctor_named(const char *name, size_t len) { + for (size_t i = 0; i < SCR_GLOBAL_CTORS; i++) { + if (strlen(scr_global_ctor_names[i]) == len && memcmp(scr_global_ctor_names[i], name, len) == 0) + return scr_dyn_global_ctor_at(i); + } + return NULL; +} + +static ScrDyn *scr_function_constructor(uint32_t kind); + +ScrDyn *scr_dyn_global_constructor(const ScrStr *name) { + if (name->len == 8 && memcmp(name->data, "Function", 8) == 0) return scr_function_constructor(0); + ScrDyn *ctor = scr_dyn_global_ctor_named(name->data, name->len); + if (!ctor) scr_trap("scriptc: invalid global constructor name\n"); + return ctor; +} + +/* The four Object.prototype methods as interned function values. They live + * in this always-linked unit so every dynamic read can answer them; the + * `Object.prototype.` reads (scr_dyn_builtin_method) return the same + * functions, so identity comparisons hold. Calls take the receiver from the + * ambient `this`. */ +static const char *const scr_object_proto_method_names[] = { + "valueOf", "toString", "hasOwnProperty", "propertyIsEnumerable", +}; +static SCR_TL ScrDyn *scr_object_proto_methods[4]; +static SCR_TL bool scr_object_proto_methods_registered; + +static void scr_object_proto_methods_cleanup(void) { + for (size_t i = 0; i < 4; i++) { + scr_dyn_release(scr_object_proto_methods[i]); + scr_object_proto_methods[i] = NULL; + } +} + +static ScrDyn *scr_object_proto_method_call(ScrClosure *closure, ScrDyn *const *args, size_t argc) { + const char *name = closure->fn; + ScrDyn *receiver = scr_dyn_this_get(); + ScrDyn *result = NULL; + ScrDyn *key = argc ? args[0] : scr_dyn_undefined(); + if (strcmp(name, "toString") == 0) { + ScrStr *tag = scr_dyn_object_tag(receiver); + if (tag) { + result = scr_dyn_new_str(tag); + scr_str_release(tag); + } + } else if (strcmp(name, "hasOwnProperty") == 0) { + bool own = scr_dyn_has_own_computed(receiver, key); + if (!scr_exc_pending()) result = scr_dyn_new_bool(own); + } else if (strcmp(name, "propertyIsEnumerable") == 0) { + bool enumerable = scr_dyn_property_is_enumerable_computed(receiver, key); + if (!scr_exc_pending()) result = scr_dyn_new_bool(enumerable); + } else if (receiver->kind == SCR_DYN_NULL || receiver->kind == SCR_DYN_UNDEF) { + static const char message[] = "Cannot convert undefined or null to object"; + scr_throw_error_msg(SCR_ERR_TYPE, message, sizeof message - 1); + } else if (receiver->kind == SCR_DYN_OBJ || receiver->kind == SCR_DYN_ARR || + receiver->kind == SCR_DYN_FUNC || receiver->kind == SCR_DYN_HANDLE || + receiver->kind == SCR_DYN_TYPED_REF || receiver->kind == SCR_DYN_PROXY || + receiver->kind == SCR_DYN_PROMISE || receiver->kind == SCR_DYN_BYTES) { + result = scr_dyn_retain(receiver); + } else { + static const char message[] = "Object.prototype.valueOf on a primitive receiver is not supported yet"; + scr_throw_error_msg_code(SCR_ERR_ERROR, message, sizeof message - 1, "SC2020"); + } + scr_dyn_release(receiver); + return result; +} + +ScrDyn *scr_dyn_object_proto_method(const char *name) { + for (size_t i = 0; i < 4; i++) { + if (strcmp(scr_object_proto_method_names[i], name) != 0) continue; + if (!scr_object_proto_methods_registered) { + scr_object_proto_methods_registered = true; + scr_atexit(scr_object_proto_methods_cleanup); + } + if (!scr_object_proto_methods[i]) { + const char *method = scr_object_proto_method_names[i]; + scr_object_proto_methods[i] = scr_dyn_new_func(scr_closure_new((void *)method, 0), + scr_object_proto_method_call, i >= 2 ? 1 : 0, "native:prototype", method); + } + return scr_dyn_retain(scr_object_proto_methods[i]); + } + return NULL; +} + +/* An Object.prototype member a value inherits when nothing nearer defines + * it: the Object constructor, or the interned Object.prototype method that + * `Object.prototype.` also reads. `methods` selects which inherited + * methods apply (types that define their own toString/valueOf exclude them); + * NULL when the name is not one of these. */ +static ScrDyn *scr_dyn_object_proto_inherited(const char *key, size_t key_len, int methods) { + if (key_len == 11 && memcmp(key, "constructor", 11) == 0) return scr_dyn_global_ctor_at(0); + const char *name = NULL; + if ((methods & SCR_INHERIT_VALUE_OF) && key_len == 7 && memcmp(key, "valueOf", 7) == 0) name = "valueOf"; + else if ((methods & SCR_INHERIT_TO_STRING) && key_len == 8 && memcmp(key, "toString", 8) == 0) name = "toString"; + else if ((methods & SCR_INHERIT_OWN) && key_len == 14 && memcmp(key, "hasOwnProperty", 14) == 0) name = "hasOwnProperty"; + else if ((methods & SCR_INHERIT_OWN) && key_len == 20 && memcmp(key, "propertyIsEnumerable", 20) == 0) name = "propertyIsEnumerable"; + if (!name) return NULL; + return scr_dyn_object_proto_method(name); +} + static void scr_array_buffer_ctor_cleanup(void) { scr_dyn_release(scr_array_buffer_ctor); scr_array_buffer_ctor = NULL; @@ -3405,6 +3578,28 @@ bool scr_bytes_instanceof(const ScrDyn *value, const ScrDyn *callee) { if (scr_bytes_ctors[i] && callee->v.fn.clo == scr_bytes_ctors[i]->v.fn.clo) return scr_dyn_typed_array_is(value, (int)i); } + for (size_t i = 0; i < SCR_GLOBAL_CTORS; i++) { + if (!scr_global_ctors[i] || callee->v.fn.clo != scr_global_ctors[i]->v.fn.clo) continue; + if (value->kind == SCR_DYN_TYPED_REF) { + ScrDyn *view = scr_dyn_typed_ref_materialize(value); + bool result = view && !scr_exc_pending() && scr_bytes_instanceof(view, callee); + scr_dyn_release(view); + return result; + } + switch (i) { + case 0: /* Object: any object whose chain reaches Object.prototype */ + if (value->kind == SCR_DYN_OBJ) return scr_dyn_has_object_prototype(value); + return value->kind == SCR_DYN_ARR || value->kind == SCR_DYN_FUNC || value->kind == SCR_DYN_HANDLE || + value->kind == SCR_DYN_PROMISE || value->kind == SCR_DYN_BYTES; + case 1: return value->kind == SCR_DYN_HANDLE && value->v.handle.tag == SCR_DYNH_DATE; + case 2: return value->kind == SCR_DYN_HANDLE && value->v.handle.tag == SCR_DYNH_REGEXP; + case 3: return value->kind == SCR_DYN_HANDLE && value->v.handle.tag == SCR_DYNH_MAP; + case 4: return value->kind == SCR_DYN_HANDLE && value->v.handle.tag == SCR_DYNH_SET; + case 5: return value->kind == SCR_DYN_PROMISE; + case 6: return value->kind == SCR_DYN_HANDLE && value->v.handle.tag == SCR_DYNH_WEAK_MAP; + case 7: return value->kind == SCR_DYN_HANDLE && value->v.handle.tag == SCR_DYNH_WEAK_SET; + } + } static const char message[] = "instanceof against this native constructor is not supported yet"; scr_throw_error_msg_code(SCR_ERR_ERROR, message, sizeof message - 1, "SC1090"); return false; @@ -3414,6 +3609,8 @@ ScrDyn *scr_bytes_construct(const ScrDyn *callee, const ScrDyn *args, const ScrS return scr_array_ctor_call(callee->v.fn.clo, args->v.arr.items, args->v.arr.len); if (callee->kind == SCR_DYN_FUNC && scr_array_buffer_ctor && callee->v.fn.clo == scr_array_buffer_ctor->v.fn.clo) return scr_array_buffer_new(args->v.arr.len ? args->v.arr.items[0] : scr_dyn_undefined()); + if (callee->kind == SCR_DYN_FUNC && scr_global_ctors[0] && callee->v.fn.clo == scr_global_ctors[0]->v.fn.clo) + return scr_global_ctor_call(callee->v.fn.clo, args->v.arr.items, args->v.arr.len); for (size_t i = 0; i < SCR_BYTES_ELEM_COUNT; i++) { if (callee->kind != SCR_DYN_FUNC || !scr_bytes_ctors[i] || callee->v.fn.clo != scr_bytes_ctors[i]->v.fn.clo) continue; const ScrDyn *input = args->v.arr.len > 0 ? args->v.arr.items[0] : scr_dyn_undefined(); @@ -4085,9 +4282,32 @@ SCR_TL ScrDyn *(*scr_dyn_promise_member_fn)(const char *key, size_t len) = NULL; ScrDyn *scr_dyn_promise_key_get(const ScrDyn *d, const ScrStr *k) { (void)d; ScrDyn *method = scr_dyn_promise_member_fn ? scr_dyn_promise_member_fn(k->data, k->len) : NULL; + if (!method && !scr_exc_pending()) { + method = k->len == 11 && memcmp(k->data, "constructor", 11) == 0 + ? scr_dyn_global_ctor_named("Promise", 7) + : scr_dyn_object_proto_inherited(k->data, k->len, SCR_INHERIT_ALL); + } return method ? method : scr_dyn_retain(scr_dyn_undefined()); } +/* The builtin classes whose dynamic values answer `.constructor` and the + * Object.prototype members their own prototypes do not shadow. */ +static ScrDyn *scr_dyn_handle_inherited(ScrDynHandleTag tag, const ScrStr *k) { + const char *ctor; + int methods; + switch (tag) { + case SCR_DYNH_DATE: ctor = "Date"; methods = SCR_INHERIT_OWN; break; + case SCR_DYNH_REGEXP: ctor = "RegExp"; methods = SCR_INHERIT_VALUE_OF | SCR_INHERIT_OWN; break; + case SCR_DYNH_MAP: ctor = "Map"; methods = SCR_INHERIT_ALL; break; + case SCR_DYNH_SET: ctor = "Set"; methods = SCR_INHERIT_ALL; break; + case SCR_DYNH_WEAK_MAP: ctor = "WeakMap"; methods = SCR_INHERIT_ALL; break; + case SCR_DYNH_WEAK_SET: ctor = "WeakSet"; methods = SCR_INHERIT_ALL; break; + default: return NULL; + } + if (k->len == 11 && memcmp(k->data, "constructor", 11) == 0) return scr_dyn_global_ctor_named(ctor, strlen(ctor)); + return scr_dyn_object_proto_inherited(k->data, k->len, methods); +} + ScrDyn *scr_dyn_handle_key_get(const ScrDyn *d, const ScrStr *k) { const ScrDynHandleOps *ops = scr_dyn_handle_ops(d->v.handle.tag); ScrDyn *r = ops->get(d->v.handle.ptr, k->data, k->len); @@ -4095,6 +4315,7 @@ ScrDyn *scr_dyn_handle_key_get(const ScrDyn *d, const ScrStr *k) { scr_dyn_release(r); return NULL; } + if (!r) r = scr_dyn_handle_inherited(d->v.handle.tag, k); /* Unmodeled names answer undefined — the checked-dynamic tree's own-property stance * (real-but-unmodeled members throw "not supported yet" inside ops->get instead; * SEMANTICS.md documents the remainder). */ @@ -9710,7 +9931,11 @@ ScrDyn *scr_dyn_fn_get(const ScrDyn *d, const char *key, size_t key_len) { if (d->v.fn.class_obj && !d->v.fn.class_obj->static_data) { static const char message[] = "scriptc: class properties through unknown other than name and length are not supported"; scr_throw_error_msg(SCR_ERR_TYPE, message, sizeof message - 1); + return NULL; } + /* Function.prototype defines its own toString; valueOf, hasOwnProperty + * and propertyIsEnumerable come from Object.prototype. */ + if (key_len != 11) return scr_dyn_object_proto_inherited(key, key_len, SCR_INHERIT_VALUE_OF | SCR_INHERIT_OWN); return NULL; } @@ -10049,6 +10274,18 @@ static ScrDyn *scr_dyn_obj_read_receiver(const ScrDyn *d, const char *key, size_ return result; } } + /* A plain object read as itself (not a function's property table or a + * class's prototype bag read for another receiver) inherits the + * Object.prototype members its chain does not define. Errors keep their + * own story (Error.prototype.toString and the constructor tokens). */ + if (receiver == d && d->kind == SCR_DYN_OBJ && scr_dyn_has_object_prototype(d)) { + bool error = scr_errdyn_find(d, false) != SIZE_MAX; + if (!(error && key_len == 11)) { + ScrDyn *inherited = scr_dyn_object_proto_inherited( + key, key_len, error ? SCR_INHERIT_VALUE_OF | SCR_INHERIT_OWN : SCR_INHERIT_ALL); + if (inherited) return inherited; + } + } return scr_dyn_retain(scr_dyn_undefined()); } diff --git a/tests/corpus/6233-object-identity-dynamic.cjs b/tests/corpus/6233-object-identity-dynamic.cjs new file mode 100644 index 000000000..b40a8493f --- /dev/null +++ b/tests/corpus/6233-object-identity-dynamic.cjs @@ -0,0 +1,86 @@ +// Plain objects, arrays, functions and the builtin collections inherit +// Object.prototype's constructor and methods: `o.constructor === Object`, +// `o.valueOf === Object.prototype.valueOf` and the isPlainObject and +// deep-equal idioms built on them (fast-deep-equal, is-plain-obj, lodash) +// answer exactly what Node answers. +function equal(a, b) { + if (a === b) return true; + if (a && b && typeof a == "object" && typeof b == "object") { + if (a.constructor !== b.constructor) return false; + var length, i, keys; + if (Array.isArray(a)) { + length = a.length; + if (length != b.length) return false; + for (i = length; i-- !== 0; ) if (!equal(a[i], b[i])) return false; + return true; + } + if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags; + if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf(); + if (a.toString !== Object.prototype.toString) return a.toString() === b.toString(); + keys = Object.keys(a); + length = keys.length; + if (length !== Object.keys(b).length) return false; + for (i = length; i-- !== 0; ) if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false; + for (i = length; i-- !== 0; ) { + var key = keys[i]; + if (!equal(a[key], b[key])) return false; + } + return true; + } + return a !== a && b !== b; +} +class Point { + constructor(x) { + this.x = x; + } +} +console.log(equal({ a: [1, { b: 2 }] }, { a: [1, { b: 2 }] }), equal({ a: 1 }, { a: "1" }), equal(new Date(0), new Date(0)), equal(new Date(0), new Date(1))); +console.log(equal(/a/g, /a/g), equal(/a/g, /a/i), equal([1, 2], [1, 2, 3]), equal(new Map(), new Set()), equal(new Point(1), new Point(1)), equal(new Point(1), { x: 1 })); +console.log(equal(JSON.parse('{"x":{"y":[1,2]}}'), { x: { y: [1, 2] } }), equal(NaN, NaN), equal(null, {})); + +const values = { + plain: { a: 1 }, + parsed: JSON.parse('{"x":{"y":1}}').x, + array: [1, 2], + date: new Date(0), + regexp: /a/g, + map: new Map([[1, 2]]), + set: new Set([1]), + fn: function () {}, + arrow: () => 1, + error: new TypeError("x"), + promise: Promise.resolve(1), + nullProto: Object.create(null), + created: Object.create({ inherited: 1 }), + instance: new Point(2), +}; +const ctors = { Object: Object, Array: Array, Date: Date, RegExp: RegExp, Map: Map, Set: Set, Function: Function, TypeError: TypeError, Promise: Promise, Point: Point }; +for (const name of Object.keys(values)) { + const v = values[name]; + const c = v.constructor; + const hits = Object.keys(ctors).filter((k) => c === ctors[k]); + console.log(name, hits.join("|") || "-", v.valueOf === Object.prototype.valueOf, + name === "regexp" ? "-" : v.toString === Object.prototype.toString, + v.hasOwnProperty === Object.prototype.hasOwnProperty, + v.propertyIsEnumerable === Object.prototype.propertyIsEnumerable); +} +const o = { a: 1 }; +console.log(typeof Object, typeof Date, typeof Map, Object.name, Date.name, o.constructor.name, typeof o.constructor); +console.log(o.toString(), o.valueOf() === o, new o.constructor().constructor === Object, Object(o) === o, typeof Object(), Object(null).constructor === Object); +console.log(o instanceof Object, [] instanceof Object, Object.create(null) instanceof Object, new Date(0) instanceof Date, new Map() instanceof Map, /x/ instanceof RegExp, new Set() instanceof Map); +const { Object: O, Map: M } = globalThis; +console.log(O === Object, M === Map, globalThis.Object === Object, O.keys({ z: 1 }).join()); +const shadow = { constructor: "mine", valueOf() { return 7; }, toString: 3 }; +console.log(shadow.constructor, shadow.valueOf === Object.prototype.valueOf, shadow.toString, shadow.valueOf()); + +function isPlainObj(value) { + if (typeof value !== "object" || value === null) return false; + const prototype = Object.getPrototypeOf(value); + return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value); +} +function isPlainCtor(value) { + return !!value && typeof value === "object" && (value.constructor === Object || value.constructor === undefined); +} +for (const name of ["plain", "parsed", "array", "nullProto", "created", "instance"]) { + console.log(name, isPlainObj(values[name]), isPlainCtor(values[name])); +} diff --git a/tests/corpus/6235-object-identity-typed.ts b/tests/corpus/6235-object-identity-typed.ts new file mode 100644 index 000000000..70386d17f --- /dev/null +++ b/tests/corpus/6235-object-identity-typed.ts @@ -0,0 +1,24 @@ +// TypeScript records and class instances read their inherited +// Object.prototype members as values, so the identity tests agree with +// Node: `o.constructor === Object`, `o.valueOf === Object.prototype.valueOf`. +interface Shape { + kind: string; + size: number; +} +class Box { + width = 2; + describe(): string { + return `box ${this.width}`; + } +} +const record: Shape = { kind: "square", size: 3 }; +const box = new Box(); +const empty = {}; +console.log(record.constructor === Object, record.valueOf === Object.prototype.valueOf, record.toString === Object.prototype.toString); +console.log(record.hasOwnProperty === Object.prototype.hasOwnProperty, empty.valueOf === Object.prototype.valueOf, typeof record.constructor); +console.log(box.valueOf === Object.prototype.valueOf, box.toString === Object.prototype.toString, box.describe()); +const parsed: unknown = JSON.parse('{"a":1}'); +if (typeof parsed === "object" && parsed !== null) { + console.log(parsed.constructor === Object, Object.getPrototypeOf(parsed) === Object.prototype); +} +console.log(Object.getPrototypeOf(record) === Object.prototype, record.toString(), record.constructor === empty.constructor); From 9fab4348d3463263bdc591b2efb6cd1e8967fda3 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Sun, 18 Oct 2026 12:40:25 -0700 Subject: [PATCH 04/83] Read Object.prototype's own members as values `const { hasOwnProperty } = Object.prototype`, `Object.prototype.constructor` and `hasOwnProperty.call(Object.prototype, "constructor")` hit the runtime "Object.prototype reflection and mutation" fence (acorn, ini, safe-stable-stringify and qs read the prototype this way). Reads of the prototype's constructor and shared methods now answer the same interned values the inherited reads answer, own-property checks see its twelve own members (none enumerable), and Object.prototype.toString.call(error) answers "[object Error]" instead of "[object Object]". Mutation and the remaining reflection (isPrototypeOf, __proto__, accessors) stay fenced. Co-Authored-By: Claude Opus 5.5 --- .../limitations/builtin-apis/prototypes.mdx | 2 +- .../6234-object-prototype-values.cjs.json | 6 +++++ packages/runtime/src/scr_json.c | 12 ++++++++- tests/corpus/6233-object-identity-dynamic.cjs | 12 +++++++-- tests/corpus/6234-object-prototype-values.cjs | 26 +++++++++++++++++++ 5 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6234-object-prototype-values.cjs.json create mode 100644 tests/corpus/6234-object-prototype-values.cjs diff --git a/docs/content/limitations/builtin-apis/prototypes.mdx b/docs/content/limitations/builtin-apis/prototypes.mdx index 441884bfc..b349db3fb 100644 --- a/docs/content/limitations/builtin-apis/prototypes.mdx +++ b/docs/content/limitations/builtin-apis/prototypes.mdx @@ -1 +1 @@ -- **Prototypes:** Named properties and methods on program class prototypes support replacement, inheritance, and deletion. Bare prototype reflection, accessor replacement, and prototype storage on local, generic, mixin, or runtime-provided classes are unsupported. Plain objects, arrays, functions, class instances, and Date, RegExp, Map, Set, WeakMap, WeakSet, and Promise values inherit `constructor`, `valueOf`, `toString`, `hasOwnProperty`, and `propertyIsEnumerable` from `Object.prototype` as in Node, so `value.constructor === Object` and `value.valueOf === Object.prototype.valueOf` compare identities exactly. The constructors `Object`, `Function`, `Date`, `RegExp`, `Map`, `Set`, `WeakMap`, `WeakSet`, and `Promise` read as function values; through a stored value only `Object` can be called or constructed. +- **Prototypes:** Named properties and methods on program class prototypes support replacement, inheritance, and deletion. Bare prototype reflection, accessor replacement, and prototype storage on local, generic, mixin, or runtime-provided classes are unsupported. Plain objects, arrays, functions, class instances, and Date, RegExp, Map, Set, WeakMap, WeakSet, and Promise values inherit `constructor`, `valueOf`, `toString`, `hasOwnProperty`, and `propertyIsEnumerable` from `Object.prototype` as in Node, so `value.constructor === Object` and `value.valueOf === Object.prototype.valueOf` compare identities exactly. `Object.prototype`'s own members read as values; its mutation and other reflection, such as `isPrototypeOf` and `__proto__`, are unsupported. The constructors `Object`, `Function`, `Date`, `RegExp`, `Map`, `Set`, `WeakMap`, `WeakSet`, and `Promise` read as function values; through a stored value only `Object` can be called or constructed. diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6234-object-prototype-values.cjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6234-object-prototype-values.cjs.json new file mode 100644 index 000000000..bce305944 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6234-object-prototype-values.cjs.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6234-object-prototype-values.cjs" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_json.c b/packages/runtime/src/scr_json.c index 2d38f54e5..6d3b6bfc6 100644 --- a/packages/runtime/src/scr_json.c +++ b/packages/runtime/src/scr_json.c @@ -5713,6 +5713,8 @@ void scr_dyn_throw(ScrDyn *d) { scr_dyn_trace_v, scr_dyn_is_object(d)); } +static size_t scr_errdyn_find(const void *key, bool by_err); + ScrStr *scr_dyn_object_tag(const ScrDyn *d) { if (d->kind == SCR_DYN_PROXY) { scr_dyn_proxy_unsupported("Symbol.toStringTag lookup"); @@ -5736,7 +5738,7 @@ ScrStr *scr_dyn_object_tag(const ScrDyn *d) { case SCR_DYN_NUM: tag = "[object Number]"; break; case SCR_DYN_STR: tag = "[object String]"; break; case SCR_DYN_ARR: tag = "[object Array]"; break; - case SCR_DYN_OBJ: tag = "[object Object]"; break; + case SCR_DYN_OBJ: tag = scr_errdyn_find(d, false) != SIZE_MAX ? "[object Error]" : "[object Object]"; break; case SCR_DYN_HANDLE: if ((d->v.handle.tag >= SCR_DYNH_ABORT_SIGNAL && d->v.handle.tag <= SCR_DYNH_ABORT_CONTROLLER) || d->v.handle.tag == SCR_DYNH_EVENT_TARGET || @@ -10223,6 +10225,10 @@ bool scr_dyn_err_instanceof(const ScrDyn *d, double kind) { static ScrDyn *scr_dyn_obj_read_receiver(const ScrDyn *d, const char *key, size_t key_len, const ScrDyn *receiver) { for (const ScrDyn *current = d; current; current = scr_dyn_proto_next(current)) { if (current == scr_builtin_object_prototype) { + /* Object.prototype's own constructor and the methods its values + * share read as those values; other reflection stays fenced. */ + ScrDyn *member = scr_dyn_object_proto_inherited(key, key_len, SCR_INHERIT_ALL); + if (member) return member; scr_dyn_class_reflection_fence(current); return NULL; } @@ -11704,6 +11710,8 @@ bool scr_dyn_has_own(const ScrDyn *v, const ScrStr *key) { if (v->kind == SCR_DYN_OBJ) { + /* Object.prototype's own members (its value carries no entries). */ + if (v == scr_builtin_object_prototype) return scr_dyn_object_proto_name(key); return scr_dyn_obj_get(v, key->data, key->len) != NULL; } if (v->kind == SCR_DYN_ARR) { @@ -11736,6 +11744,8 @@ bool scr_dyn_has_own(const ScrDyn *v, const ScrStr *key) { } bool scr_dyn_property_is_enumerable(const ScrDyn *value, const ScrStr *key) { + /* Object.prototype's own members are all non-enumerable. */ + if (value == scr_builtin_object_prototype) return false; if (scr_dyn_class_reflection_fence(value)) return false; if (value->kind == SCR_DYN_TYPED_REF) { ScrDyn *view = scr_dyn_typed_ref_materialize(value); diff --git a/tests/corpus/6233-object-identity-dynamic.cjs b/tests/corpus/6233-object-identity-dynamic.cjs index b40a8493f..372e141b7 100644 --- a/tests/corpus/6233-object-identity-dynamic.cjs +++ b/tests/corpus/6233-object-identity-dynamic.cjs @@ -62,7 +62,8 @@ for (const name of Object.keys(values)) { console.log(name, hits.join("|") || "-", v.valueOf === Object.prototype.valueOf, name === "regexp" ? "-" : v.toString === Object.prototype.toString, v.hasOwnProperty === Object.prototype.hasOwnProperty, - v.propertyIsEnumerable === Object.prototype.propertyIsEnumerable); + v.propertyIsEnumerable === Object.prototype.propertyIsEnumerable, + Object.prototype.toString.call(v)); } const o = { a: 1 }; console.log(typeof Object, typeof Date, typeof Map, Object.name, Date.name, o.constructor.name, typeof o.constructor); @@ -78,9 +79,16 @@ function isPlainObj(value) { const prototype = Object.getPrototypeOf(value); return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value); } +function isPlainLodash(value) { + if (value == null || Object.prototype.toString.call(value) != "[object Object]") return false; + const proto = Object.getPrototypeOf(value); + if (proto === null) return true; + const Ctor = Object.prototype.hasOwnProperty.call(proto, "constructor") && proto.constructor; + return typeof Ctor == "function" && Ctor instanceof Ctor && Ctor === Object; +} function isPlainCtor(value) { return !!value && typeof value === "object" && (value.constructor === Object || value.constructor === undefined); } for (const name of ["plain", "parsed", "array", "nullProto", "created", "instance"]) { - console.log(name, isPlainObj(values[name]), isPlainCtor(values[name])); + console.log(name, isPlainObj(values[name]), isPlainLodash(values[name]), isPlainCtor(values[name])); } diff --git a/tests/corpus/6234-object-prototype-values.cjs b/tests/corpus/6234-object-prototype-values.cjs new file mode 100644 index 000000000..8e9cde0f3 --- /dev/null +++ b/tests/corpus/6234-object-prototype-values.cjs @@ -0,0 +1,26 @@ +// Object.prototype as a value: destructured and aliased methods, the +// prototype's own constructor and members, `{}.valueOf`, and the +// `Object.prototype.toString.call(x)` tag idiom (acorn, ini, qs, +// safe-stable-stringify, rfdc). +const { hasOwnProperty, propertyIsEnumerable } = Object.prototype; +const toStr = Object.prototype.toString; +const has = Object.prototype.hasOwnProperty; +const o = { a: 1 }; +console.log(hasOwnProperty.call(o, "a"), hasOwnProperty.call(o, "b"), has.call(o, "a"), propertyIsEnumerable.call(o, "a")); +console.log(toStr.call([]), toStr.call(null), toStr.call(undefined), toStr.call(new Date(0)), toStr.call(/x/), toStr.call(1), toStr.call("s"), toStr.call(true), toStr.call(o), toStr.call(new Error("e")), toStr.call(new RangeError("r")), toStr.call(() => 1), toStr.call(new Map())); +console.log({}.valueOf === Object.prototype.valueOf, ({}).toString === toStr, typeof Object.prototype.toString, Object.prototype.constructor === Object); +console.log(hasOwnProperty.call(Object.prototype, "constructor"), has.call(Object.prototype, "toString"), has.call(Object.prototype, "missing"), propertyIsEnumerable.call(Object.prototype, "toString")); +console.log(hasOwnProperty === Object.prototype.hasOwnProperty, o.hasOwnProperty === hasOwnProperty, [].hasOwnProperty === has); +function kind(value) { + switch (toStr.call(value)) { + case "[object Date]": return "date:" + value.getTime(); + case "[object Array]": return "array:" + value.length; + case "[object RegExp]": return "regexp:" + value.source; + case "[object Error]": return "error:" + value.message; + case "[object Object]": return "object:" + Object.keys(value).join(","); + default: return "other"; + } +} +console.log([new Date(5), [1, 2], /ab/, new Error("boom"), { p: 1, q: 2 }, 3].map(kind).join(" ")); +const config = JSON.parse('{"name":"app","db":{"host":"h"}}'); +for (const key in config) if (has.call(config, key)) console.log(key, typeof config[key], toStr.call(config[key])); From acfdc7fe13e217e48a9993fe700ef9bc522c7538 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Sun, 18 Oct 2026 12:49:31 -0700 Subject: [PATCH 05/83] Store builtin timer functions as real function values in JavaScript `var nextTick = typeof setImmediate === "function" ? setImmediate : setTimeout` (bcryptjs) and `cond ? setImmediate : process.nextTick` stored the builtin as an identity token string, so typeof answered "string" and calling it threw "nextTick is not a function". The JavaScript identifier, `globalThis.X` and destructuring spellings of setTimeout, setInterval, setImmediate, clearTimeout, clearInterval, clearImmediate and queueMicrotask, and a stored process.nextTick, now read one interned runtime function per name. Conditional, logical and assigned values carry it like any function: typeof is "function", identity holds across spellings, and calls validate the callback (ERR_INVALID_ARG_TYPE), coerce the delay, pass the extra arguments and return the same numeric handles the direct calls return, which the stored clear functions accept. Co-Authored-By: Claude Opus 5.5 --- .../limitations/builtin-function-values.mdx | 2 + .../compiler/src/backend/llvm/lib-shared.ts | 2 + .../src/frontend/lowering/builtins/process.ts | 6 ++ .../src/frontend/lowering/lower-exprs.ts | 43 +++++++-- .../src/frontend/lowering/lower-stmts.ts | 2 +- .../compiler/src/ir/builtin-signatures.ts | 6 ++ .../6236-builtin-function-values.cjs.json | 6 ++ packages/runtime/src/scr_async.c | 96 +++++++++++++++++++ packages/runtime/src/scr_dyn.h | 6 ++ tests/corpus/6236-builtin-function-values.cjs | 52 ++++++++++ 10 files changed, 214 insertions(+), 7 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6236-builtin-function-values.cjs.json create mode 100644 tests/corpus/6236-builtin-function-values.cjs diff --git a/docs/content/limitations/builtin-function-values.mdx b/docs/content/limitations/builtin-function-values.mdx index 4fd093e47..e87f86e13 100644 --- a/docs/content/limitations/builtin-function-values.mdx +++ b/docs/content/limitations/builtin-function-values.mdx @@ -5,6 +5,8 @@ Some builtins can be stored and passed as functions: - `util.isDeepStrictEqual`, `styleText`, `stripVTControlCharacters`, `toUSVString`, `inherits`, `deprecate`, `callbackify`, `debuglog`, and `debug`. - `fs.existsSync`, `unlinkSync`, `chmodSync`, `chownSync`, `renameSync`, and `closeSync`, and `fs/promises.unlink`, `chmod`, and `rename`. +In JavaScript, `setTimeout`, `setInterval`, `setImmediate`, `clearTimeout`, `clearInterval`, `clearImmediate`, `queueMicrotask`, and `process.nextTick` are function values wherever they are stored, including conditional and logical expressions and assignments: `typeof` answers `"function"`, identity holds across spellings, and calls schedule as the direct calls do, returning the same numeric timer handles. Calls through these values bypass `node:test` mock timers. Other builtin globals stored in JavaScript remain opaque identity values. + Other immutable aliases of table-backed Node builtins support direct calls, but cannot be passed elsewhere. `util.promisify` supports compile-time conversions of `child_process.execFile`, `child_process.exec`, `setTimeout` and `setImmediate` (to the `timers/promises` functions, as in Node), `stream.finished` and `stream.pipeline` (to the `stream/promises` functions, as in Node), the `zlib` callback one-shots `deflate`, `inflate`, `deflateRaw`, `inflateRaw`, `gzip`, `gunzip` and `unzip` (with or without options), the `crypto` callback functions `randomBytes`, `pbkdf2`, `scrypt`, `hkdf`, `randomFill`, `randomInt` and `generateKey`, `dns.lookup` (settling with `{ address, family }`, or the address array with `all: true`) and `dns.lookupService` (settling with `{ hostname, service }`), and the `fs` callback functions that have an `fs/promises` counterpart (`access`, `appendFile`, `chmod`, `chown`, `copyFile`, `lchown`, `link`, `lstat`, `lutimes`, `mkdir`, `mkdtemp`, `readFile`, `readdir`, `readlink`, `realpath`, `rename`, `rm`, `rmdir`, `stat`, `statfs`, `symlink`, `truncate`, `unlink`, `utimes`, and `writeFile`); calls through the converted function lower as the `fs/promises` call. `util.styleText` supports standard formats and aliases, `#RGB` and `#RRGGBB` hex colors, arrays of formats, nested ANSI resets, and explicit color enabling with `{ validateStream: false }`. Native calls accept standard process streams; custom native streams and mutations to `util.inspect.colors` remain unsupported. diff --git a/packages/compiler/src/backend/llvm/lib-shared.ts b/packages/compiler/src/backend/llvm/lib-shared.ts index 3f36e0d7c..45662d3cc 100644 --- a/packages/compiler/src/backend/llvm/lib-shared.ts +++ b/packages/compiler/src/backend/llvm/lib-shared.ts @@ -900,6 +900,7 @@ export const LIB_FN_SYMS: Record = { "dyn.nativeUrlIs": "scr_dyn_native_url_is", "dyn.nativeDateIs": "scr_dyn_native_date_is", "dyn.globalConstructor": "scr_dyn_global_constructor", + "dyn.builtinFunction": "scr_dyn_builtin_function", "date.nativeNew": "scr_dyn_native_date_new", "date.nativeNewFields": "scr_dyn_native_date_new_fields", "date.checkedValue": "scr_dyn_native_date_value", @@ -1612,4 +1613,5 @@ export const USES_TIMERS_LIB_FNS = new Set([ "timers.queueMicrotaskDyn", "timers.setImmediateFnValue", "timers.immediatePromise", + "dyn.builtinFunction", ]); diff --git a/packages/compiler/src/frontend/lowering/builtins/process.ts b/packages/compiler/src/frontend/lowering/builtins/process.ts index 94c2cdba7..c9935a744 100644 --- a/packages/compiler/src/frontend/lowering/builtins/process.ts +++ b/packages/compiler/src/frontend/lowering/builtins/process.ts @@ -26,6 +26,7 @@ import { funcOf, } from "../../../ir/ir.js"; import { lowerBuiltinLoaderValue } from "../lower-builtin-values.js"; +import { builtinFunctionValue } from "../lower-exprs.js"; import { lowerProcessIpcSend, ipcMessageListener, ipcDisconnectListener } from "./ipc.js"; import { lowerDiagnosticsSubscriber, lowerTracingArguments } from "./async-context.js"; @@ -385,6 +386,11 @@ export function lowerProcessProperty( if (member === "hrtime") { return { kind: "libCall", fn: "process.hrtimeValue", args: [], type: DYN, loc }; } + // A stored process.nextTick in JavaScript (`cond ? setImmediate : + // process.nextTick`): the interned builtin function value. + if (member === "nextTick" && isJsSourceFile(expr.getSourceFile())) { + return builtinFunctionValue("process.nextTick", loc); + } if (member === "getBuiltinModule") { // Keep the native loader's argument validation when the function is // stored. The declaration's string signature must not insert a checked diff --git a/packages/compiler/src/frontend/lowering/lower-exprs.ts b/packages/compiler/src/frontend/lowering/lower-exprs.ts index d3f1f4676..02565e547 100644 --- a/packages/compiler/src/frontend/lowering/lower-exprs.ts +++ b/packages/compiler/src/frontend/lowering/lower-exprs.ts @@ -1453,8 +1453,12 @@ function lowerExprInner(lowerer: Lowerer, expr: ts.Expression): IrExpr { const canonical = stdlibGlobalNameOf(lowerer, expr) ?? expr.text; // Builtin constructors have real function values in both source // languages (TypeScript reads them as dynamic values). - const constructorValue = builtinGlobalValue(canonical, loc); - if (constructorValue !== null) return constructorValue; + const builtinValue = builtinGlobalValue( + canonical, + loc, + isJsSourceFile(expr.getSourceFile()), + ); + if (builtinValue !== null) return builtinValue; if (isJsSourceFile(expr.getSourceFile())) { return { kind: "strLit", value: `[builtin ${canonical}]`, type: STRING, loc }; } @@ -2354,7 +2358,7 @@ function lowerExprInner(lowerer: Lowerer, expr: ts.Expression): IrExpr { loc, }; return ( - builtinGlobalValue(canonical, loc) ?? { + builtinGlobalValue(canonical, loc, true) ?? { kind: "strLit", value: `[builtin ${canonical}]`, type: STRING, @@ -16330,9 +16334,36 @@ const GLOBAL_CONSTRUCTOR_VALUES: ReadonlySet = new Set([ "WeakSet", ]); -/** The value a JavaScript source reads for the stdlib global `canonical`, - * or null when it stays an identity token. */ -export function builtinGlobalValue(canonical: string, loc: SrcLoc): IrExpr | null { +/** The builtin global functions JavaScript sources read as interned + * function values (the `cond ? setImmediate : process.nextTick` feature + * probe of bcryptjs and async libraries): typeof answers "function", + * identity holds across spellings, and calls run the timer APIs. */ +const GLOBAL_FUNCTION_VALUES: ReadonlySet = new Set([ + "setTimeout", + "setInterval", + "setImmediate", + "clearTimeout", + "clearInterval", + "clearImmediate", + "queueMicrotask", +]); + +/** A builtin function value: one interned runtime function per name. */ +export function builtinFunctionValue(name: string, loc: SrcLoc): IrExpr { + return { + kind: "libCall", + fn: "dyn.builtinFunction", + args: [{ kind: "strLit", value: name, type: STRING, loc }], + type: DYN, + loc, + }; +} + +/** The value a source reads for the stdlib global `canonical` (builtin + * constructors in both languages, builtin functions in JavaScript), or + * null when it stays an identity token (JavaScript) or fenced. */ +export function builtinGlobalValue(canonical: string, loc: SrcLoc, js = false): IrExpr | null { + if (js && GLOBAL_FUNCTION_VALUES.has(canonical)) return builtinFunctionValue(canonical, loc); if (GLOBAL_CONSTRUCTOR_VALUES.has(canonical)) { return { kind: "libCall", diff --git a/packages/compiler/src/frontend/lowering/lower-stmts.ts b/packages/compiler/src/frontend/lowering/lower-stmts.ts index 78c3e76d3..315464289 100644 --- a/packages/compiler/src/frontend/lowering/lower-stmts.ts +++ b/packages/compiler/src/frontend/lowering/lower-stmts.ts @@ -3337,7 +3337,7 @@ function stdlibGlobalTokenDestructure( alias = canonical; // Builtin constructors read as their interned function values, the // same values the bare and `globalThis.X` spellings read. - value = builtinGlobalValue(canonical, locOf(el)); + value = builtinGlobalValue(canonical, locOf(el), true); } else if (globalName === "console" && !CONSOLE_CALL_MEMBERS.has(propName)) { token = `[builtin console.${propName}]`; } else if (TOKEN_OPAQUE_GLOBALS.has(globalName)) { diff --git a/packages/compiler/src/ir/builtin-signatures.ts b/packages/compiler/src/ir/builtin-signatures.ts index 74956d161..3709ae4e1 100644 --- a/packages/compiler/src/ir/builtin-signatures.ts +++ b/packages/compiler/src/ir/builtin-signatures.ts @@ -210,6 +210,12 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ * interned function per name, the same one dynamic values answer for * `.constructor`. The name is a compiler-chosen literal. */ "dyn.globalConstructor": { argTypes: [STRING], result: DYN }, + /** A builtin global function (setTimeout, setInterval, setImmediate, + * the clear functions, queueMicrotask, process.nextTick) read as a + * JavaScript value: one interned function per name whose calls run the + * timer APIs (it pulls in the event loop). The name is a compiler-chosen + * literal. */ + "dyn.builtinFunction": { argTypes: [STRING], result: DYN }, "date.nativeNew": { argTypes: [DYN], result: DYN }, /** `new Date(year, monthIndex, ...)` in JavaScript: the argument array's * first seven values pass through ToNumber in order (later ones are diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6236-builtin-function-values.cjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6236-builtin-function-values.cjs.json new file mode 100644 index 000000000..21b9f85c2 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6236-builtin-function-values.cjs.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6236-builtin-function-values.cjs" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_async.c b/packages/runtime/src/scr_async.c index 48f3a5329..58c7f653e 100644 --- a/packages/runtime/src/scr_async.c +++ b/packages/runtime/src/scr_async.c @@ -3717,6 +3717,102 @@ ScrDyn *scr_set_immediate_dyn_value(void) { 1, "(cb,...)", "setImmediate"); } +/* ── Builtin global functions as dynamic values ───────────────────────── + * A JavaScript source that stores a builtin global function (the + * `var nextTick = typeof setImmediate === "function" ? setImmediate : + * setTimeout` feature probe) reads one interned function per name, so + * typeof answers "function" and `===` is identity. Calls through it run + * the timer and tick APIs with Node's argument contract: a function + * callback (else ERR_INVALID_ARG_TYPE), the delay through ToNumber, the + * remaining arguments passed to the callback. Timer functions answer the + * same numeric handles the direct calls answer, which the clear functions + * accept. A name without a dynamic call path throws a named fence. */ +static SCR_TL ScrDyn *scr_builtin_fn_table; + +static void scr_builtin_fn_cleanup(void) { + scr_dyn_release(scr_builtin_fn_table); + scr_builtin_fn_table = NULL; +} + +static bool scr_builtin_fn_callback(ScrDyn *const *args, size_t argc) { + if (argc > 0 && args[0]->kind == SCR_DYN_FUNC) return true; + static const char msg[] = "The \"callback\" argument must be of type function."; + scr_throw_error_msg_code(SCR_ERR_TYPE, msg, sizeof msg - 1, "ERR_INVALID_ARG_TYPE"); + return false; +} + +/* The closure that calls args[0] with args[first..] when the loop fires it. */ +static ScrClosure *scr_builtin_fn_deferred(ScrDyn *const *args, size_t argc, size_t first) { + ScrDyn *rest = scr_dyn_new_arr(); + for (size_t i = first; i < argc; i++) scr_dyn_arr_push(rest, scr_dyn_retain(args[i])); + ScrClosure *c = scr_closure_new((void *)scr_imm_dyn_fire, 2); + c->caps[0] = scr_box_new_obj(scr_dyn_retain_v, scr_dyn_release_v, NULL); + scr_box_set_ref(c->caps[0], scr_dyn_retain(args[0])); + c->caps[1] = scr_box_new_obj(scr_dyn_retain_v, scr_dyn_release_v, NULL); + scr_box_set_ref(c->caps[1], rest); /* moves */ + return c; +} + +static ScrDyn *scr_builtin_fn_call(ScrClosure *clo, ScrDyn *const *args, size_t argc) { + ScrStr *name = scr_box_get_ref(clo->caps[0]); + ScrDyn *result = NULL; + bool timeout = strcmp(name->data, "setTimeout") == 0; + if (timeout || strcmp(name->data, "setInterval") == 0) { + double ms = 0; + if (scr_builtin_fn_callback(args, argc) && (argc < 2 || scr_dyn_number_coerce_js(args[1], &ms))) { + ScrClosure *c = scr_builtin_fn_deferred(args, argc, 2); + result = scr_dyn_new_num(timeout ? scr_set_timeout_handle(c, ms) : scr_set_interval(c, ms)); + } + } else if (strcmp(name->data, "setImmediate") == 0) { + if (scr_builtin_fn_callback(args, argc)) + result = scr_dyn_new_num(scr_set_immediate(scr_builtin_fn_deferred(args, argc, 1))); + } else if (strcmp(name->data, "process.nextTick") == 0) { + if (scr_builtin_fn_callback(args, argc)) { + scr_next_tick(scr_builtin_fn_deferred(args, argc, 1)); + result = scr_dyn_retain(scr_dyn_undefined()); + } + } else if (strcmp(name->data, "queueMicrotask") == 0) { + if (scr_builtin_fn_callback(args, argc)) { + scr_queue_microtask_dyn(args[0]); + result = scr_dyn_retain(scr_dyn_undefined()); + } + } else if (strcmp(name->data, "clearTimeout") == 0 || strcmp(name->data, "clearInterval") == 0 || + strcmp(name->data, "clearImmediate") == 0) { + /* Node ignores anything that is not a live handle. */ + if (argc > 0 && args[0]->kind == SCR_DYN_NUM) { + if (name->data[5] == 'I' && name->data[6] == 'm') scr_clear_immediate(args[0]->v.num); + else scr_clear_interval(args[0]->v.num); + } + result = scr_dyn_retain(scr_dyn_undefined()); + } else { + char message[160]; + int length = snprintf(message, sizeof message, "calling '%s' through a stored value is not supported yet", name->data); + scr_throw_error_msg_code(SCR_ERR_ERROR, message, (size_t)length, "SC2020"); + } + scr_str_release(name); + return result; +} + +ScrDyn *scr_dyn_builtin_function(const ScrStr *name) { + if (!scr_builtin_fn_table) { + scr_builtin_fn_table = scr_dyn_new_obj_null_proto(); + scr_atexit(scr_builtin_fn_cleanup); + } + ScrDyn *value = scr_dyn_obj_get(scr_builtin_fn_table, name->data, name->len); + if (!value) { + ScrClosure *closure = scr_closure_new(NULL, 1); + closure->caps[0] = scr_box_new(SCR_BOX_STR); + scr_box_set_ref(closure->caps[0], scr_str_retain((ScrStr *)name)); + const char *dot = memchr(name->data, '.', name->len); + size_t arity = strcmp(name->data, "queueMicrotask") == 0 || strncmp(name->data, "clear", 5) == 0 ? 1 : + strcmp(name->data, "setImmediate") == 0 || strcmp(name->data, "process.nextTick") == 0 ? 1 : 2; + value = scr_dyn_new_func(closure, scr_builtin_fn_call, arity, "native:builtin", + dot ? dot + 1 : name->data); + scr_dyn_obj_set(scr_builtin_fn_table, name->data, name->len, value); /* the table owns it */ + } + return scr_dyn_retain(value); +} + /* ── queueMicrotask ────────────────────────────────────────────────── * The READY queue's FIFO is the microtask queue (promise continuations * ride it as parked fibers), so a queueMicrotask callback enters the SAME diff --git a/packages/runtime/src/scr_dyn.h b/packages/runtime/src/scr_dyn.h index 738c5d487..d497b45ac 100644 --- a/packages/runtime/src/scr_dyn.h +++ b/packages/runtime/src/scr_dyn.h @@ -655,6 +655,12 @@ ScrDyn *scr_dyn_array_constructor(void); /* The interned builtin constructor `name` (Object, Date, RegExp, Map, Set, * Promise, WeakMap, WeakSet) as a dynamic function value: +1. */ ScrDyn *scr_dyn_global_constructor(const ScrStr *name); +/* A builtin global function (setTimeout, setImmediate, process.nextTick, ...) + * as an interned dynamic function value: +1. */ +ScrDyn *scr_dyn_builtin_function(const ScrStr *name); +/* Object.prototype's valueOf/toString/hasOwnProperty/propertyIsEnumerable as + * interned function values (+1), or NULL for other names. */ +ScrDyn *scr_dyn_object_proto_method(const char *name); bool scr_dyn_arr_has_index(const ScrDyn *arr, size_t index); void scr_dyn_arr_push_hole(ScrDyn *arr); ScrDyn *scr_dyn_new_obj(void); diff --git a/tests/corpus/6236-builtin-function-values.cjs b/tests/corpus/6236-builtin-function-values.cjs new file mode 100644 index 000000000..b8abee311 --- /dev/null +++ b/tests/corpus/6236-builtin-function-values.cjs @@ -0,0 +1,52 @@ +// Builtin timer functions stored as values (bcryptjs's `nextTick` feature +// probe, async libraries' scheduler selection): conditional, logical and +// assigned values are real functions — typeof answers "function", identity +// holds across spellings, and calls schedule with Node's argument contract +// and ordering. +var nextTick = + typeof setImmediate === "function" + ? setImmediate + : typeof queueMicrotask === "function" + ? queueMicrotask + : setTimeout; +const pick = process.argv.length > 100; +const later = pick ? setImmediate : process.nextTick; +const timeout = pick ? setImmediate : setTimeout; +const micro = pick || queueMicrotask; +let assigned = setTimeout; +assigned = pick ? assigned : setImmediate; +const { setInterval: every, clearInterval: stop } = globalThis; +console.log(typeof nextTick, typeof later, typeof timeout, typeof micro, typeof assigned, typeof every); +console.log(nextTick === setImmediate, later === process.nextTick, timeout === setTimeout, micro === queueMicrotask, assigned === globalThis.setImmediate, every === setInterval, stop === clearInterval); +console.log(nextTick.name, later.name, timeout.name, setTimeout.length, queueMicrotask.length); + +const order = []; +nextTick(function () { order.push("immediate:" + arguments.length); }); +later((a, b) => order.push("tick:" + a + b), 1, 2); +// Microtask and nextTick queues order differently in CommonJS and ESM +// entry evaluation, so the microtask reports through its own flag. +let microRan = false; +micro(() => { microRan = true; }); +timeout((x) => order.push("timeout:" + x), 60, "x"); +assigned(() => order.push("assigned")); +const cancelled = timeout(() => order.push("cancelled"), 1); +(pick ? clearImmediate : clearTimeout)(cancelled); +let count = 0; +const handle = every(() => { + count++; + order.push("interval:" + count); + if (count === 2) stop(handle); +}, 20); +timeout(() => console.log(order.join(" "), "micro:" + microRan), 120); + +for (const bad of [undefined, 1, "fn"]) { + try { + later(bad); + } catch (e) { + console.log("rejected", typeof bad, e.code, e instanceof TypeError); + } +} +const log = pick ? console.error : console.log; +log("log", typeof log); +const mx = pick ? Math.min : Math.max; +console.log(mx(1, 3, 2), typeof mx); From ae531a06ce0b572e07356f1540905917dc90d77c Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Sun, 18 Oct 2026 14:27:05 -0700 Subject: [PATCH 06/83] Keep the compatibility paths out of the always-linked runtime units The zero-cost gate showed every benchmark executable changing: the new string literals and floating-point constants grew the merged constant pools of scr_json.o and scr_lib.o, which every program linking those units carries, and edits to scr_dyn_fn_get and scr_dyn_has_own changed code the typed benchmarks reach. - The builtin constructor values, the inherited Object.prototype members and the stored timer functions move to scr_dyn_invoke.c, which links only into programs that use them: the frontend now selects that unit for dyn.globalConstructor, dyn.builtinFunction, dyn.objectTag and constant-key reads of constructor/valueOf/toString/hasOwnProperty/ propertyIsEnumerable. Those reads call scr_dyn_inherited_read after the ordinary read (and scr_dyn_object_prototype_member before it, since the ordinary read fences Object.prototype itself); computed-key reads keep their old answer. The unit's new strings live in their own section so programs linking it for other reasons keep their bytes. - scr_date_parse_get_time keeps its exact body; Date.parse and new Date(string) call the new scr_date_parse (bounded grammars, then V8's parser), and JavaScript `new Date(value)` calls scr_dyn_native_date_new_value, so fs.Stats dates are untouched. The parser and UTC(t) avoid new pooled constants. - Object.prototype.toString's "[object Error]" moves to scr_dyn_object_tag_of; scr_json.c is unchanged. - Calling or constructing through stored constructor values and own-property checks on Object.prototype itself are not supported in this change (they would touch shared code). Also pins the IMF-fixdate (cookie Expires) parse and Date#toUTCString round-trip in the corpus. Co-Authored-By: Claude Opus 5.5 --- .../limitations/builtin-apis/prototypes.mdx | 2 +- .../compiler/src/backend/llvm/expr-dynamic.ts | 53 ++- .../compiler/src/backend/llvm/lib-shared.ts | 9 +- .../src/frontend/lowering/lower-classes.ts | 3 +- .../src/frontend/lowering/lower-exprs.ts | 7 + .../src/frontend/lowering/surfaces.ts | 1 + packages/compiler/src/ir/builtin-effects.ts | 1 + .../compiler/src/ir/builtin-signatures.ts | 3 + packages/compiler/src/ir/int32-slots.ts | 1 + packages/compiler/src/ir/runtime-features.ts | 21 +- packages/runtime/src/scr_async.c | 96 ----- packages/runtime/src/scr_dyn.h | 12 +- packages/runtime/src/scr_dyn_invoke.c | 345 +++++++++++++++++- packages/runtime/src/scr_json.c | 249 +------------ packages/runtime/src/scr_lib.c | 113 ++++-- packages/runtime/src/scr_number.h | 9 +- tests/corpus/6230-date-parse-legacy.ts | 1 + tests/corpus/6232-date-fields-js.cjs | 20 + tests/corpus/6233-object-identity-dynamic.cjs | 3 +- tests/corpus/6234-object-prototype-values.cjs | 1 - 20 files changed, 543 insertions(+), 407 deletions(-) diff --git a/docs/content/limitations/builtin-apis/prototypes.mdx b/docs/content/limitations/builtin-apis/prototypes.mdx index b349db3fb..5f6687082 100644 --- a/docs/content/limitations/builtin-apis/prototypes.mdx +++ b/docs/content/limitations/builtin-apis/prototypes.mdx @@ -1 +1 @@ -- **Prototypes:** Named properties and methods on program class prototypes support replacement, inheritance, and deletion. Bare prototype reflection, accessor replacement, and prototype storage on local, generic, mixin, or runtime-provided classes are unsupported. Plain objects, arrays, functions, class instances, and Date, RegExp, Map, Set, WeakMap, WeakSet, and Promise values inherit `constructor`, `valueOf`, `toString`, `hasOwnProperty`, and `propertyIsEnumerable` from `Object.prototype` as in Node, so `value.constructor === Object` and `value.valueOf === Object.prototype.valueOf` compare identities exactly. `Object.prototype`'s own members read as values; its mutation and other reflection, such as `isPrototypeOf` and `__proto__`, are unsupported. The constructors `Object`, `Function`, `Date`, `RegExp`, `Map`, `Set`, `WeakMap`, `WeakSet`, and `Promise` read as function values; through a stored value only `Object` can be called or constructed. +- **Prototypes:** Named properties and methods on program class prototypes support replacement, inheritance, and deletion. Bare prototype reflection, accessor replacement, and prototype storage on local, generic, mixin, or runtime-provided classes are unsupported. Plain objects, arrays, functions, class instances, and Date, RegExp, Map, Set, WeakMap, WeakSet, and Promise values inherit `constructor`, `valueOf`, `toString`, `hasOwnProperty`, and `propertyIsEnumerable` from `Object.prototype` as in Node, so `value.constructor === Object` and `value.valueOf === Object.prototype.valueOf` compare identities exactly. Named reads such as `value.valueOf` answer these inherited members; computed-key reads such as `value[key]` do not. `Object.prototype`'s own members read as values; its mutation and other reflection, such as `isPrototypeOf` and `__proto__`, are unsupported. The constructors `Object`, `Function`, `Date`, `RegExp`, `Map`, `Set`, `WeakMap`, `WeakSet`, and `Promise` read as function values; through a stored value only `Object` can be called, and `instanceof` and `new` against them remain unsupported. diff --git a/packages/compiler/src/backend/llvm/expr-dynamic.ts b/packages/compiler/src/backend/llvm/expr-dynamic.ts index 76df9795d..3ce21a1ad 100644 --- a/packages/compiler/src/backend/llvm/expr-dynamic.ts +++ b/packages/compiler/src/backend/llvm/expr-dynamic.ts @@ -535,6 +535,27 @@ export function emitDynamicExpr( e.key.type.kind === "dyn" ? host.dyn.dynComputedKeyGetHelper() : host.dyn.dynKeyGetHelper(); const t = B.tmp(); const generic = `call ptr @${helper}(ptr ${d.name}, ptr ${k.name}, i1 ${e.optional ? "true" : "false"})`; + const inherited = e.key.kind === "strLit" && INHERITED_OBJECT_MEMBERS.has(e.key.value); + // Object.prototype itself answers its own members before the ordinary + // read (which fences reflection on the prototype). + let prototypeJoin: { member: string; from: string; join: string } | null = null; + if (inherited) { + host.declare(`declare ptr @scr_dyn_object_prototype_member(ptr, ptr)`); + const member = B.tmp(); + B.line( + `${member} = call ptr @scr_dyn_object_prototype_member(ptr ${d.name}, ptr ${k.name})`, + ); + const isMember = B.tmp(); + B.line(`${isMember} = icmp ne ptr ${member}, null`); + const from = B.newLabel("dkg.op"); + const read = B.newLabel("dkg.rd"); + const join = B.newLabel("dkg.opj"); + B.condBr(isMember, from, read); + B.startBlock(from); + B.br(join); + B.startBlock(read); + prototypeJoin = { member, from, join }; + } if (e.key.kind === "strLit") { // A constant key on an ordinary object reads through the site's // cache; every other receiver keeps the shared helper. @@ -560,7 +581,27 @@ export function emitDynamicExpr( } else { B.line(`${t} = ${generic}`); } - const out = host.own({ name: t, type: e.type }); + let result = t; + if (prototypeJoin) { + // An undefined read of an Object.prototype member name answers the + // member the receiver inherits (scr_dyn_invoke.c; the frontend + // links that unit for these reads). + host.declare(`declare ptr @scr_dyn_inherited_read(ptr, ptr, ptr)`); + const read = B.tmp(); + B.line( + `${read} = call ptr @scr_dyn_inherited_read(ptr ${d.name}, ptr ${k.name}, ptr ${t})`, + ); + const readEnd = B.newLabel("dkg.rde"); + B.br(readEnd); + B.startBlock(readEnd); + B.br(prototypeJoin.join); + B.startBlock(prototypeJoin.join); + result = B.tmp(); + B.line( + `${result} = phi ptr [ ${prototypeJoin.member}, %${prototypeJoin.from} ], [ ${read}, %${readEnd} ]`, + ); + } + const out = host.own({ name: result, type: e.type }); host.emitPendingCheck(); return out; } @@ -1204,3 +1245,13 @@ function emitUnionEqWrappedScalar( function wrappedStringLiteral(e: IrExpr): string | null { return e.kind === "unionWrap" && e.value.kind === "strLit" ? e.value.value : null; } + +/** Object.prototype member names whose constant-key dynamic reads fall back + * to the inherited member (runtime-features.ts links the unit). */ +export const INHERITED_OBJECT_MEMBERS: ReadonlySet = new Set([ + "constructor", + "valueOf", + "toString", + "hasOwnProperty", + "propertyIsEnumerable", +]); diff --git a/packages/compiler/src/backend/llvm/lib-shared.ts b/packages/compiler/src/backend/llvm/lib-shared.ts index 45662d3cc..377f1909a 100644 --- a/packages/compiler/src/backend/llvm/lib-shared.ts +++ b/packages/compiler/src/backend/llvm/lib-shared.ts @@ -300,11 +300,11 @@ export const LIB_FN_SYMS: Record = { "date.now": "scr_date_now", "date.newNow": "scr_date_now", "date.newMs": "scr_date_new_ms", - "date.newString": "scr_date_parse_get_time", - "date.parse": "scr_date_parse_get_time", + "date.newString": "scr_date_parse", + "date.parse": "scr_date_parse", "date.getTime": "scr_date_get_time", "date.valueOf": "scr_date_get_time", - "date.parseGetTime": "scr_date_parse_get_time", + "date.parseGetTime": "scr_date_parse", "date.utc": "scr_date_utc", "date.newLocal": "scr_date_new_local", "date.getFullYear": "scr_date_get_full_year_local", @@ -891,7 +891,7 @@ export const LIB_FN_SYMS: Record = { "dyn.promiseAllSettled": "scr_dyn_promise_all_settled", "dyn.promiseAny": "scr_dyn_promise_any", "dyn.typeof": "scr_dyn_typeof", - "dyn.objectTag": "scr_dyn_object_tag", + "dyn.objectTag": "scr_dyn_object_tag_of", "dyn.freeze": "scr_dyn_freeze", "dyn.isFrozen": "scr_dyn_is_frozen", "dyn.nativeSetNew": "scr_dyn_native_set_new", @@ -903,6 +903,7 @@ export const LIB_FN_SYMS: Record = { "dyn.builtinFunction": "scr_dyn_builtin_function", "date.nativeNew": "scr_dyn_native_date_new", "date.nativeNewFields": "scr_dyn_native_date_new_fields", + "date.nativeNewValue": "scr_dyn_native_date_new_value", "date.checkedValue": "scr_dyn_native_date_value", "dyn.nativeRegexIs": "scr_dyn_native_regex_is", "dyn.toString": "scr_dyn_to_string_argument", diff --git a/packages/compiler/src/frontend/lowering/lower-classes.ts b/packages/compiler/src/frontend/lowering/lower-classes.ts index 0fd332217..47969db53 100644 --- a/packages/compiler/src/frontend/lowering/lower-classes.ts +++ b/packages/compiler/src/frontend/lowering/lower-classes.ts @@ -9011,7 +9011,8 @@ export function lowerNew(lowerer: Lowerer, expr: ts.NewExpression): IrExpr { if (isJsSourceFile(expr.getSourceFile())) { return { kind: "libCall", - fn: "date.nativeNew", + // One argument: a string parses with the full date parser. + fn: args.length === 1 ? "date.nativeNewValue" : "date.nativeNew", args: [ { kind: "dynArrLit", diff --git a/packages/compiler/src/frontend/lowering/lower-exprs.ts b/packages/compiler/src/frontend/lowering/lower-exprs.ts index 02565e547..92e06b98a 100644 --- a/packages/compiler/src/frontend/lowering/lower-exprs.ts +++ b/packages/compiler/src/frontend/lowering/lower-exprs.ts @@ -5060,6 +5060,13 @@ export function lowerIntrinsicProperty( return { kind: "dynFrom", value, fnName: "ownKeys", type: DYN, loc: locOf(expr) }; } if (lowerer.chainBlocked(expr)) return null; + // Object.prototype.constructor: the Object constructor value. + if ( + expr.name.text === "constructor" && + ts.isPropertyAccessExpression(expr.expression) && + lowerer.stdlibGlobalMember(expr.expression, "Object") === "prototype" + ) + return builtinGlobalValue("Object", locOf(expr)); const builtinPrototype = builtinPrototypeMethod(lowerer, expr); if (builtinPrototype) return { diff --git a/packages/compiler/src/frontend/lowering/surfaces.ts b/packages/compiler/src/frontend/lowering/surfaces.ts index d06180952..5ce9a5647 100644 --- a/packages/compiler/src/frontend/lowering/surfaces.ts +++ b/packages/compiler/src/frontend/lowering/surfaces.ts @@ -2183,6 +2183,7 @@ export const AMBIENT_SURFACE_FNS: readonly AmbientSurfaceRow[] = [ "date.nativeNew", "date.newLocal", "date.nativeNewFields", + "date.nativeNewValue", ], note: "zero arguments, one milliseconds/date-string argument, or local-time year/month fields; values are the read-only TimeClip scalar slice", }, diff --git a/packages/compiler/src/ir/builtin-effects.ts b/packages/compiler/src/ir/builtin-effects.ts index 045e624ed..8e4d5263e 100644 --- a/packages/compiler/src/ir/builtin-effects.ts +++ b/packages/compiler/src/ir/builtin-effects.ts @@ -469,6 +469,7 @@ export const MAY_THROW_LIB_FNS: ReadonlySet = new Set([ "json.stringifyValue", "date.nativeNew", "date.nativeNewFields", + "date.nativeNewValue", "date.checkedValue", "util.parseArgs", "util.parseEnv", diff --git a/packages/compiler/src/ir/builtin-signatures.ts b/packages/compiler/src/ir/builtin-signatures.ts index 3709ae4e1..10a79b949 100644 --- a/packages/compiler/src/ir/builtin-signatures.ts +++ b/packages/compiler/src/ir/builtin-signatures.ts @@ -221,6 +221,9 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ * first seven values pass through ToNumber in order (later ones are * evaluated, never converted), then the local-time field constructor. */ "date.nativeNewFields": { argTypes: [DYN], result: DYN }, + /** `new Date(value)` in JavaScript: a string argument parses with V8's + * full date parser; every other value constructs as date.nativeNew. */ + "date.nativeNewValue": { argTypes: [DYN], result: DYN }, "date.checkedValue": { argTypes: [DYN], result: DATE_T }, "dyn.nativeRegexIs": { argTypes: [DYN], result: BOOL }, /** toString() on a checked-dynamic receiver: runtime kind dispatch diff --git a/packages/compiler/src/ir/int32-slots.ts b/packages/compiler/src/ir/int32-slots.ts index 64faf0936..7ea3d6bde 100644 --- a/packages/compiler/src/ir/int32-slots.ts +++ b/packages/compiler/src/ir/int32-slots.ts @@ -348,6 +348,7 @@ const DYN_READ_ONLY = new Set([ "arrayBuffer.viewU8C", "date.nativeNew", "date.nativeNewFields", + "date.nativeNewValue", // The dynamic operators run the ToPrimitive and coercion helpers of // dyn.compare, dyn.numberConstructor and dyn.toStringCoerce, then build a // fresh number, string or bigint. dyn.increment steps dyn.toNumeric's result. diff --git a/packages/compiler/src/ir/runtime-features.ts b/packages/compiler/src/ir/runtime-features.ts index d16edfabd..b527c226b 100644 --- a/packages/compiler/src/ir/runtime-features.ts +++ b/packages/compiler/src/ir/runtime-features.ts @@ -343,7 +343,10 @@ function scanRuntimeFeatures(mod: IrModule, stopAt?: keyof RuntimeFeatures): Run fn === "dyn.arrayProtoCall" || fn === "dyn.arrayPrototype" || fn === "dyn.functionApply" || - fn === "dyn.builtinMethod" + fn === "dyn.builtinMethod" || + fn === "dyn.globalConstructor" || + fn === "dyn.objectTag" || + fn === "dyn.builtinFunction" ) features.dynInvoke = true; if (DYN_ASYNC_LIB_FNS.has(fn)) features.dynAsync = true; @@ -465,6 +468,12 @@ function scanRuntimeFeatures(mod: IrModule, stopAt?: keyof RuntimeFeatures): Run case "dynInvoke": features.dynInvoke = true; break; + case "dynKeyGet": + // Constant-key reads of Object.prototype member names answer the + // inherited member through scr_dyn_invoke.c. + if (node.key.kind === "strLit" && INHERITED_MEMBER_NAMES.has(node.key.value)) + features.dynInvoke = true; + break; case "awaitExpr": if (node.type.kind === "dyn") features.dynAsync = true; break; @@ -862,3 +871,13 @@ export function moduleUsesTls(mod: IrModule): boolean { export function moduleUsesTlsCa(mod: IrModule): boolean { return scanRuntimeFeatures(mod, "tlsCa").tlsCa; } + +/** Mirrors expr-dynamic.ts INHERITED_OBJECT_MEMBERS (the IR layer does not + * import the backend). */ +const INHERITED_MEMBER_NAMES: ReadonlySet = new Set([ + "constructor", + "valueOf", + "toString", + "hasOwnProperty", + "propertyIsEnumerable", +]); diff --git a/packages/runtime/src/scr_async.c b/packages/runtime/src/scr_async.c index 58c7f653e..48f3a5329 100644 --- a/packages/runtime/src/scr_async.c +++ b/packages/runtime/src/scr_async.c @@ -3717,102 +3717,6 @@ ScrDyn *scr_set_immediate_dyn_value(void) { 1, "(cb,...)", "setImmediate"); } -/* ── Builtin global functions as dynamic values ───────────────────────── - * A JavaScript source that stores a builtin global function (the - * `var nextTick = typeof setImmediate === "function" ? setImmediate : - * setTimeout` feature probe) reads one interned function per name, so - * typeof answers "function" and `===` is identity. Calls through it run - * the timer and tick APIs with Node's argument contract: a function - * callback (else ERR_INVALID_ARG_TYPE), the delay through ToNumber, the - * remaining arguments passed to the callback. Timer functions answer the - * same numeric handles the direct calls answer, which the clear functions - * accept. A name without a dynamic call path throws a named fence. */ -static SCR_TL ScrDyn *scr_builtin_fn_table; - -static void scr_builtin_fn_cleanup(void) { - scr_dyn_release(scr_builtin_fn_table); - scr_builtin_fn_table = NULL; -} - -static bool scr_builtin_fn_callback(ScrDyn *const *args, size_t argc) { - if (argc > 0 && args[0]->kind == SCR_DYN_FUNC) return true; - static const char msg[] = "The \"callback\" argument must be of type function."; - scr_throw_error_msg_code(SCR_ERR_TYPE, msg, sizeof msg - 1, "ERR_INVALID_ARG_TYPE"); - return false; -} - -/* The closure that calls args[0] with args[first..] when the loop fires it. */ -static ScrClosure *scr_builtin_fn_deferred(ScrDyn *const *args, size_t argc, size_t first) { - ScrDyn *rest = scr_dyn_new_arr(); - for (size_t i = first; i < argc; i++) scr_dyn_arr_push(rest, scr_dyn_retain(args[i])); - ScrClosure *c = scr_closure_new((void *)scr_imm_dyn_fire, 2); - c->caps[0] = scr_box_new_obj(scr_dyn_retain_v, scr_dyn_release_v, NULL); - scr_box_set_ref(c->caps[0], scr_dyn_retain(args[0])); - c->caps[1] = scr_box_new_obj(scr_dyn_retain_v, scr_dyn_release_v, NULL); - scr_box_set_ref(c->caps[1], rest); /* moves */ - return c; -} - -static ScrDyn *scr_builtin_fn_call(ScrClosure *clo, ScrDyn *const *args, size_t argc) { - ScrStr *name = scr_box_get_ref(clo->caps[0]); - ScrDyn *result = NULL; - bool timeout = strcmp(name->data, "setTimeout") == 0; - if (timeout || strcmp(name->data, "setInterval") == 0) { - double ms = 0; - if (scr_builtin_fn_callback(args, argc) && (argc < 2 || scr_dyn_number_coerce_js(args[1], &ms))) { - ScrClosure *c = scr_builtin_fn_deferred(args, argc, 2); - result = scr_dyn_new_num(timeout ? scr_set_timeout_handle(c, ms) : scr_set_interval(c, ms)); - } - } else if (strcmp(name->data, "setImmediate") == 0) { - if (scr_builtin_fn_callback(args, argc)) - result = scr_dyn_new_num(scr_set_immediate(scr_builtin_fn_deferred(args, argc, 1))); - } else if (strcmp(name->data, "process.nextTick") == 0) { - if (scr_builtin_fn_callback(args, argc)) { - scr_next_tick(scr_builtin_fn_deferred(args, argc, 1)); - result = scr_dyn_retain(scr_dyn_undefined()); - } - } else if (strcmp(name->data, "queueMicrotask") == 0) { - if (scr_builtin_fn_callback(args, argc)) { - scr_queue_microtask_dyn(args[0]); - result = scr_dyn_retain(scr_dyn_undefined()); - } - } else if (strcmp(name->data, "clearTimeout") == 0 || strcmp(name->data, "clearInterval") == 0 || - strcmp(name->data, "clearImmediate") == 0) { - /* Node ignores anything that is not a live handle. */ - if (argc > 0 && args[0]->kind == SCR_DYN_NUM) { - if (name->data[5] == 'I' && name->data[6] == 'm') scr_clear_immediate(args[0]->v.num); - else scr_clear_interval(args[0]->v.num); - } - result = scr_dyn_retain(scr_dyn_undefined()); - } else { - char message[160]; - int length = snprintf(message, sizeof message, "calling '%s' through a stored value is not supported yet", name->data); - scr_throw_error_msg_code(SCR_ERR_ERROR, message, (size_t)length, "SC2020"); - } - scr_str_release(name); - return result; -} - -ScrDyn *scr_dyn_builtin_function(const ScrStr *name) { - if (!scr_builtin_fn_table) { - scr_builtin_fn_table = scr_dyn_new_obj_null_proto(); - scr_atexit(scr_builtin_fn_cleanup); - } - ScrDyn *value = scr_dyn_obj_get(scr_builtin_fn_table, name->data, name->len); - if (!value) { - ScrClosure *closure = scr_closure_new(NULL, 1); - closure->caps[0] = scr_box_new(SCR_BOX_STR); - scr_box_set_ref(closure->caps[0], scr_str_retain((ScrStr *)name)); - const char *dot = memchr(name->data, '.', name->len); - size_t arity = strcmp(name->data, "queueMicrotask") == 0 || strncmp(name->data, "clear", 5) == 0 ? 1 : - strcmp(name->data, "setImmediate") == 0 || strcmp(name->data, "process.nextTick") == 0 ? 1 : 2; - value = scr_dyn_new_func(closure, scr_builtin_fn_call, arity, "native:builtin", - dot ? dot + 1 : name->data); - scr_dyn_obj_set(scr_builtin_fn_table, name->data, name->len, value); /* the table owns it */ - } - return scr_dyn_retain(value); -} - /* ── queueMicrotask ────────────────────────────────────────────────── * The READY queue's FIFO is the microtask queue (promise continuations * ride it as parked fibers), so a queueMicrotask callback enters the SAME diff --git a/packages/runtime/src/scr_dyn.h b/packages/runtime/src/scr_dyn.h index d497b45ac..caa1f2801 100644 --- a/packages/runtime/src/scr_dyn.h +++ b/packages/runtime/src/scr_dyn.h @@ -601,6 +601,8 @@ bool scr_dyn_native_search_params_is(const ScrDyn *value); bool scr_dyn_native_date_is(const ScrDyn *value); ScrDyn *scr_dyn_native_date_new(const ScrDyn *arguments); ScrDyn *scr_dyn_native_date_new_fields(const ScrDyn *arguments); +ScrDyn *scr_dyn_native_date_new_value(const ScrDyn *arguments); +ScrStr *scr_dyn_object_tag_of(const ScrDyn *d); ScrDyn *scr_dyn_native_date_from_ms(double ms); double scr_dyn_native_date_value(const ScrDyn *value); ScrDyn *scr_dyn_native_regex(ScrRegex *value); @@ -658,9 +660,13 @@ ScrDyn *scr_dyn_global_constructor(const ScrStr *name); /* A builtin global function (setTimeout, setImmediate, process.nextTick, ...) * as an interned dynamic function value: +1. */ ScrDyn *scr_dyn_builtin_function(const ScrStr *name); -/* Object.prototype's valueOf/toString/hasOwnProperty/propertyIsEnumerable as - * interned function values (+1), or NULL for other names. */ -ScrDyn *scr_dyn_object_proto_method(const char *name); +/* A constant-key read of an Object.prototype member name: takes the + * ordinary read's +1 result and answers the inherited member in place of + * an undefined one (scr_dyn_invoke.c). */ +ScrDyn *scr_dyn_inherited_read(const ScrDyn *d, const ScrStr *key, ScrDyn *read); +/* The member `key` read on Object.prototype itself, or NULL for any other + * receiver (scr_dyn_invoke.c). */ +ScrDyn *scr_dyn_object_prototype_member(const ScrDyn *d, const ScrStr *key); bool scr_dyn_arr_has_index(const ScrDyn *arr, size_t index); void scr_dyn_arr_push_hole(ScrDyn *arr); ScrDyn *scr_dyn_new_obj(void); diff --git a/packages/runtime/src/scr_dyn_invoke.c b/packages/runtime/src/scr_dyn_invoke.c index 086555975..d50ce749c 100644 --- a/packages/runtime/src/scr_dyn_invoke.c +++ b/packages/runtime/src/scr_dyn_invoke.c @@ -1658,7 +1658,7 @@ static ScrDyn *dyn_builtin_method_call(ScrClosure *closure, ScrDyn *const *args, if (!strcmp(prototype->data, "Object")) { ScrDyn *key = argc ? args[0] : scr_dyn_undefined(); if (!strcmp(method->data, "toString")) { - ScrStr *tag = scr_dyn_object_tag(receiver); + ScrStr *tag = scr_dyn_object_tag_of(receiver); if (tag) { result = scr_dyn_new_str(tag); scr_str_release(tag); } } else if (!strcmp(method->data, "hasOwnProperty")) { bool own = scr_dyn_has_own_computed(receiver, key); @@ -1671,7 +1671,8 @@ static ScrDyn *dyn_builtin_method_call(ScrClosure *closure, ScrDyn *const *args, scr_throw_error_msg(SCR_ERR_TYPE, message, sizeof message - 1); } else if (receiver->kind == SCR_DYN_OBJ || receiver->kind == SCR_DYN_ARR || receiver->kind == SCR_DYN_FUNC || receiver->kind == SCR_DYN_HANDLE || - receiver->kind == SCR_DYN_TYPED_REF || receiver->kind == SCR_DYN_PROXY) result = scr_dyn_retain(receiver); + receiver->kind == SCR_DYN_TYPED_REF || receiver->kind == SCR_DYN_PROXY || + receiver->kind == SCR_DYN_PROMISE || receiver->kind == SCR_DYN_BYTES) result = scr_dyn_retain(receiver); else dyn_throw_unsupported("Object", method->data); } else { bool compatible = false; @@ -1712,12 +1713,6 @@ static ScrDyn *dyn_builtin_method_call(ScrClosure *closure, ScrDyn *const *args, } ScrDyn *scr_dyn_builtin_method(const ScrStr *prototype, const ScrStr *method) { - /* Object.prototype's shared methods: the interned values dynamic reads - * of inherited members answer too. */ - if (!strcmp(prototype->data, "Object")) { - ScrDyn *shared = scr_dyn_object_proto_method(method->data); - if (shared) return shared; - } if (!dyn_builtin_methods) { dyn_builtin_methods = scr_dyn_new_obj_null_proto(); scr_atexit(dyn_builtin_methods_cleanup); @@ -1750,6 +1745,340 @@ ScrDyn *scr_dyn_builtin_method(const ScrStr *prototype, const ScrStr *method) { return scr_dyn_retain(value); } +/* ── Builtin constructors, inherited Object.prototype members and builtin + * functions as dynamic values ────────────────────────────────────────── + * This unit links only into programs that use them (the frontend marks + * dyn.globalConstructor, dyn.builtinFunction and constant-key reads of the + * inherited names), so programs that never read them keep their exact + * executables. + * + * JavaScript reads `Object`, `Date`, `RegExp`, `Map`, `Set`, `Promise`, + * `WeakMap` and `WeakSet` as interned functions, and the same functions + * answer `.constructor` on the matching dynamic values, so + * `o.constructor === Object` and `a.constructor === b.constructor` are + * exact identity tests. `Object(v)` works through the stored value; calling + * the others through it is fenced. */ +/* String constants live in named arrays in their own section rather than + * in the unit's merged string pool (the explicit section also keeps the + * compiler from folding the unit's existing literals into them), so + * programs that link this unit for other reasons keep their exact bytes. */ +#if defined(__APPLE__) +#define SCR_DYN_COMPAT_STR __attribute__((section("__TEXT,__scr_dyn_compat"))) +#elif defined(_WIN32) +#define SCR_DYN_COMPAT_STR +#else +#define SCR_DYN_COMPAT_STR __attribute__((section(".rodata.scr_dyn_compat"))) +#endif +static const char dyn_s_error_tag[] SCR_DYN_COMPAT_STR = "[object Error]"; +static const char dyn_s0[] SCR_DYN_COMPAT_STR = "calling the %s constructor%s through a stored value is not supported yet"; +static const char dyn_s1[] SCR_DYN_COMPAT_STR = "calling '%s' through a stored value is not supported yet"; +static const char dyn_s3[] SCR_DYN_COMPAT_STR = "scriptc: invalid global constructor name\n"; +static const char dyn_s4[] SCR_DYN_COMPAT_STR = "propertyIsEnumerable"; +static const char dyn_s5[] SCR_DYN_COMPAT_STR = "ERR_INVALID_ARG_TYPE"; +static const char dyn_s6[] SCR_DYN_COMPAT_STR = "native:constructor"; +static const char dyn_s7[] SCR_DYN_COMPAT_STR = "process.nextTick"; +static const char dyn_s8[] SCR_DYN_COMPAT_STR = " on a primitive"; +static const char dyn_s9[] SCR_DYN_COMPAT_STR = "hasOwnProperty"; +static const char dyn_s10[] SCR_DYN_COMPAT_STR = "native:builtin"; +static const char dyn_s11[] SCR_DYN_COMPAT_STR = "queueMicrotask"; +static const char dyn_s12[] SCR_DYN_COMPAT_STR = "clearImmediate"; +static const char dyn_s13[] SCR_DYN_COMPAT_STR = "setImmediate"; +static const char dyn_s14[] SCR_DYN_COMPAT_STR = "constructor"; +static const char dyn_s15[] SCR_DYN_COMPAT_STR = "setInterval"; +static const char dyn_s16[] SCR_DYN_COMPAT_STR = "setTimeout"; +static const char dyn_s17[] SCR_DYN_COMPAT_STR = "callback"; +static const char dyn_s18[] SCR_DYN_COMPAT_STR = "toString"; +static const char dyn_s20[] SCR_DYN_COMPAT_STR = "Function"; +static const char dyn_s21[] SCR_DYN_COMPAT_STR = "Promise"; +static const char dyn_s22[] SCR_DYN_COMPAT_STR = "WeakMap"; +static const char dyn_s23[] SCR_DYN_COMPAT_STR = "valueOf"; +static const char dyn_s24[] SCR_DYN_COMPAT_STR = "WeakSet"; +static const char dyn_s25[] SCR_DYN_COMPAT_STR = "RegExp"; +static const char dyn_s26[] SCR_DYN_COMPAT_STR = "Object"; +static const char dyn_s27[] SCR_DYN_COMPAT_STR = "SC2020"; +static const char dyn_s28[] SCR_DYN_COMPAT_STR = "clear"; +static const char dyn_s29[] SCR_DYN_COMPAT_STR = "Date"; +static const char dyn_s30[] SCR_DYN_COMPAT_STR = "Map"; +static const char dyn_s31[] SCR_DYN_COMPAT_STR = "Set"; +static const char dyn_s32[] SCR_DYN_COMPAT_STR = ""; + +static const char *const dyn_global_ctor_names[] = { + dyn_s26, dyn_s29, dyn_s25, dyn_s30, dyn_s31, dyn_s21, dyn_s22, dyn_s24, +}; +#define DYN_GLOBAL_CTORS (sizeof dyn_global_ctor_names / sizeof dyn_global_ctor_names[0]) +static SCR_TL ScrDyn *dyn_global_ctors[DYN_GLOBAL_CTORS]; +static SCR_TL bool dyn_global_ctors_registered; + +static void dyn_global_ctors_cleanup(void) { + for (size_t i = 0; i < DYN_GLOBAL_CTORS; i++) { + scr_dyn_release(dyn_global_ctors[i]); + dyn_global_ctors[i] = NULL; + } +} + +static ScrDyn *dyn_global_ctor_call(ScrClosure *closure, ScrDyn *const *args, size_t argc) { + const char *name = closure->fn; + bool object = strcmp(name, dyn_s26) == 0; + if (object) { + const ScrDyn *value = argc ? args[0] : scr_dyn_undefined(); + if (value->kind == SCR_DYN_UNDEF || value->kind == SCR_DYN_NULL) return scr_dyn_new_obj(); + if (value->kind != SCR_DYN_NUM && value->kind != SCR_DYN_STR && value->kind != SCR_DYN_BOOL && + value->kind != SCR_DYN_BIGINT && value->kind != SCR_DYN_SYMBOL) { + return scr_dyn_retain((ScrDyn *)value); + } + } + char message[128]; + int length = snprintf(message, sizeof message, dyn_s0, + name, object ? dyn_s8 : dyn_s32); + scr_throw_error_msg_code(SCR_ERR_ERROR, message, (size_t)length, dyn_s27); + return NULL; +} + +static ScrDyn *dyn_global_ctor(const char *name) { + for (size_t i = 0; i < DYN_GLOBAL_CTORS; i++) { + if (strcmp(dyn_global_ctor_names[i], name) != 0) continue; + if (!dyn_global_ctors_registered) { + dyn_global_ctors_registered = true; + scr_atexit(dyn_global_ctors_cleanup); + } + if (!dyn_global_ctors[i]) { + dyn_global_ctors[i] = scr_dyn_new_func(scr_closure_new((void *)dyn_global_ctor_names[i], 0), + dyn_global_ctor_call, i == 0 ? 1 : 0, dyn_s6, + dyn_global_ctor_names[i]); + } + return scr_dyn_retain(dyn_global_ctors[i]); + } + return NULL; +} + +static ScrDyn *dyn_function_probe_call(ScrClosure *closure, ScrDyn *const *args, size_t argc) { + (void)closure; (void)args; (void)argc; + return scr_dyn_retain(scr_dyn_undefined()); +} + +/* The Function constructor: the value any ordinary function answers for + * `.constructor` (scr_json.c keeps its singletons). */ +static ScrDyn *dyn_function_constructor(void) { + ScrDyn *probe = scr_dyn_new_func(scr_closure_new(NULL, 0), dyn_function_probe_call, 0, dyn_s10, dyn_s20); + ScrDyn *ctor = scr_dyn_fn_get(probe, dyn_s14, 11); + scr_dyn_release(probe); + return ctor; +} + +ScrDyn *scr_dyn_global_constructor(const ScrStr *name) { + if (strcmp(name->data, dyn_s20) == 0) return dyn_function_constructor(); + ScrDyn *ctor = dyn_global_ctor(name->data); + if (!ctor) scr_trap(dyn_s3); + return ctor; +} + +/* Object.prototype.toString's tag: errors answer "[object Error]" (the + * shared tag walk in scr_json.c answers "[object Object]" for them). */ +ScrStr *scr_dyn_object_tag_of(const ScrDyn *d) { + if (d->kind == SCR_DYN_OBJ && scr_dyn_err_instanceof(d, 0)) return scr_str_new(dyn_s_error_tag, sizeof dyn_s_error_tag - 1); + return scr_dyn_object_tag(d); +} + +/* Which Object.prototype members a value inherits when no nearer prototype + * defines them: types with their own toString or valueOf keep theirs. */ +enum { DYN_INHERIT_VALUE_OF = 1, DYN_INHERIT_TO_STRING = 2, DYN_INHERIT_OWN = 4, DYN_INHERIT_ALL = 7 }; + +static ScrDyn *dyn_inherited_from(const char *ctor, int methods, const ScrStr *key) { + if (strcmp(key->data, dyn_s14) == 0) return ctor ? dyn_global_ctor(ctor) : NULL; + bool applies = (strcmp(key->data, dyn_s23) == 0 && (methods & DYN_INHERIT_VALUE_OF)) || + (strcmp(key->data, dyn_s18) == 0 && (methods & DYN_INHERIT_TO_STRING)) || + ((strcmp(key->data, dyn_s9) == 0 || strcmp(key->data, dyn_s4) == 0) && + (methods & DYN_INHERIT_OWN)); + if (!applies) return NULL; + ScrStr *prototype = scr_str_new(dyn_s26, 6); + ScrDyn *method = scr_dyn_builtin_method(prototype, key); + scr_str_release(prototype); + return method; +} + +/* The inherited member `key` of `d` when its own chain does not define it, + * or NULL (the read's undefined stands). */ +static ScrDyn *dyn_inherited_member(const ScrDyn *d, const ScrStr *key) { + switch (d->kind) { + case SCR_DYN_TYPED_REF: { + ScrDyn *view = scr_dyn_typed_ref_materialize(d); + ScrDyn *member = view && !scr_exc_pending() ? dyn_inherited_member(view, key) : NULL; + scr_dyn_release(view); + return member; + } + case SCR_DYN_OBJ: { + /* A plain object, or an Object.create chain ending in Object.prototype; + * a member present anywhere on the chain (even as undefined) stands. */ + ScrDyn *object_prototype = scr_dyn_object_prototype(); + const ScrDyn *tail = d; + bool present = false; + for (const ScrDyn *link = d; link; link = link->prototype) { + tail = link; + if (link == object_prototype) break; + if (link->kind != SCR_DYN_OBJ || scr_dyn_has_own(link, key)) { + present = true; + break; + } + } + scr_dyn_release(object_prototype); + if (present || (tail != object_prototype && tail->null_proto)) return NULL; + /* Errors keep Error.prototype.toString and their constructor values. */ + if (scr_dyn_err_instanceof(d, 0)) return dyn_inherited_from(NULL, DYN_INHERIT_VALUE_OF | DYN_INHERIT_OWN, key); + return dyn_inherited_from(dyn_s26, DYN_INHERIT_ALL, key); + } + case SCR_DYN_ARR: + /* Array.prototype defines toString and answers constructor itself. */ + if (d->null_proto || d->prototype || scr_dyn_has_own(d, key)) return NULL; + return dyn_inherited_from(NULL, DYN_INHERIT_VALUE_OF | DYN_INHERIT_OWN, key); + case SCR_DYN_FUNC: + /* Function.prototype defines toString and answers constructor itself. */ + if (scr_dyn_has_own(d, key)) return NULL; + return dyn_inherited_from(NULL, DYN_INHERIT_VALUE_OF | DYN_INHERIT_OWN, key); + case SCR_DYN_PROMISE: + return dyn_inherited_from(dyn_s21, DYN_INHERIT_ALL, key); + case SCR_DYN_HANDLE: + switch (d->v.handle.tag) { + case SCR_DYNH_DATE: return dyn_inherited_from(dyn_s29, DYN_INHERIT_OWN, key); + case SCR_DYNH_REGEXP: return dyn_inherited_from(dyn_s25, DYN_INHERIT_VALUE_OF | DYN_INHERIT_OWN, key); + case SCR_DYNH_MAP: return dyn_inherited_from(dyn_s30, DYN_INHERIT_ALL, key); + case SCR_DYNH_SET: return dyn_inherited_from(dyn_s31, DYN_INHERIT_ALL, key); + case SCR_DYNH_WEAK_MAP: return dyn_inherited_from(dyn_s22, DYN_INHERIT_ALL, key); + case SCR_DYNH_WEAK_SET: return dyn_inherited_from(dyn_s24, DYN_INHERIT_ALL, key); + default: return NULL; + } + default: + return NULL; + } +} + +/* Object.prototype's own constructor and shared methods, read on the + * prototype itself (`const { hasOwnProperty } = Object.prototype`, the + * `proto.constructor` of an isPlainObject check): the member, or NULL when + * `d` is another value and the ordinary read applies. */ +ScrDyn *scr_dyn_object_prototype_member(const ScrDyn *d, const ScrStr *key) { + ScrDyn *object_prototype = scr_dyn_object_prototype(); + bool prototype = d == object_prototype; + scr_dyn_release(object_prototype); + return prototype ? dyn_inherited_from(dyn_s26, DYN_INHERIT_ALL, key) : NULL; +} + +/* A constant-key read of constructor/valueOf/toString/hasOwnProperty/ + * propertyIsEnumerable: `read` is the ordinary read's +1 result (NULL when + * it threw). Takes it; answers the inherited member in place of an + * undefined read, the read otherwise. */ +ScrDyn *scr_dyn_inherited_read(const ScrDyn *d, const ScrStr *key, ScrDyn *read) { + if (!read || read->kind != SCR_DYN_UNDEF) return read; + ScrDyn *member = dyn_inherited_member(d, key); + if (scr_exc_pending()) { + scr_dyn_release(member); + scr_dyn_release(read); + return NULL; + } + if (!member) return read; + scr_dyn_release(read); + return member; +} + +/* Builtin global functions stored in JavaScript (the `var nextTick = typeof + * setImmediate === "function" ? setImmediate : setTimeout` probe): one + * interned function per name, so typeof answers "function" and `===` is + * identity. Calls run the timer and tick APIs with Node's argument + * contract — a function callback (else ERR_INVALID_ARG_TYPE), the delay + * through ToNumber, the remaining arguments passed to the callback — and + * timer functions answer the numeric handles the direct calls answer, + * which the clear functions accept. */ +static SCR_TL ScrDyn *dyn_builtin_fns; + +static void dyn_builtin_fns_cleanup(void) { + scr_dyn_release(dyn_builtin_fns); + dyn_builtin_fns = NULL; +} + +static bool dyn_builtin_fn_callback(ScrDyn *const *args, size_t argc) { + if (argc > 0 && args[0]->kind == SCR_DYN_FUNC) return true; + static const char message[] = "The \"callback\" argument must be of type function."; + scr_throw_error_msg_code(SCR_ERR_TYPE, message, sizeof message - 1, dyn_s5); + return false; +} + +static void dyn_builtin_fn_fire(ScrClosure *c) { + ScrDyn *fn = (ScrDyn *)scr_box_get_ref(c->caps[0]); + ScrDyn *args = (ScrDyn *)scr_box_get_ref(c->caps[1]); + ScrDyn *r = scr_dyn_call(fn, args->v.arr.items, args->v.arr.len, dyn_s17); + if (r != NULL) scr_dyn_release(r); + scr_dyn_release(fn); + scr_dyn_release(args); +} + +/* The closure that calls args[0] with args[first..] when the loop runs it. */ +static ScrClosure *dyn_builtin_fn_deferred(ScrDyn *const *args, size_t argc, size_t first) { + ScrDyn *rest = scr_dyn_new_arr(); + for (size_t i = first; i < argc; i++) scr_dyn_arr_push(rest, scr_dyn_retain(args[i])); + ScrClosure *c = scr_closure_new((void *)dyn_builtin_fn_fire, 2); + c->caps[0] = scr_box_new_obj(scr_dyn_retain_v, scr_dyn_release_v, NULL); + scr_box_set_ref(c->caps[0], scr_dyn_retain(args[0])); + c->caps[1] = scr_box_new_obj(scr_dyn_retain_v, scr_dyn_release_v, NULL); + scr_box_set_ref(c->caps[1], rest); /* moves */ + return c; +} + +static ScrDyn *dyn_builtin_fn_call(ScrClosure *clo, ScrDyn *const *args, size_t argc) { + ScrStr *name = scr_box_get_ref(clo->caps[0]); + ScrDyn *result = NULL; + bool timeout = strcmp(name->data, dyn_s16) == 0; + if (timeout || strcmp(name->data, dyn_s15) == 0) { + double ms = 0; + if (dyn_builtin_fn_callback(args, argc) && (argc < 2 || scr_dyn_number_coerce_js(args[1], &ms))) { + ScrClosure *c = dyn_builtin_fn_deferred(args, argc, 2); + result = scr_dyn_new_num(timeout ? scr_set_timeout_handle(c, ms) : scr_set_interval(c, ms)); + } + } else if (strcmp(name->data, dyn_s13) == 0) { + if (dyn_builtin_fn_callback(args, argc)) + result = scr_dyn_new_num(scr_set_immediate(dyn_builtin_fn_deferred(args, argc, 1))); + } else if (strcmp(name->data, dyn_s7) == 0) { + if (dyn_builtin_fn_callback(args, argc)) { + scr_next_tick(dyn_builtin_fn_deferred(args, argc, 1)); + result = scr_dyn_retain(scr_dyn_undefined()); + } + } else if (strcmp(name->data, dyn_s11) == 0) { + if (dyn_builtin_fn_callback(args, argc)) { + scr_queue_microtask_dyn(args[0]); + result = scr_dyn_retain(scr_dyn_undefined()); + } + } else if (strncmp(name->data, dyn_s28, 5) == 0) { + /* Node ignores anything that is not a live handle. */ + if (argc > 0 && args[0]->kind == SCR_DYN_NUM) { + if (strcmp(name->data, dyn_s12) == 0) scr_clear_immediate(args[0]->v.num); + else scr_clear_interval(args[0]->v.num); + } + result = scr_dyn_retain(scr_dyn_undefined()); + } else { + char message[160]; + int length = snprintf(message, sizeof message, dyn_s1, name->data); + scr_throw_error_msg_code(SCR_ERR_ERROR, message, (size_t)length, dyn_s27); + } + scr_str_release(name); + return result; +} + +ScrDyn *scr_dyn_builtin_function(const ScrStr *name) { + if (!dyn_builtin_fns) { + dyn_builtin_fns = scr_dyn_new_obj_null_proto(); + scr_atexit(dyn_builtin_fns_cleanup); + } + ScrDyn *value = scr_dyn_obj_get(dyn_builtin_fns, name->data, name->len); + if (!value) { + ScrClosure *closure = scr_closure_new(NULL, 1); + closure->caps[0] = scr_box_new(SCR_BOX_STR); + scr_box_set_ref(closure->caps[0], scr_str_retain((ScrStr *)name)); + const char *dot = memchr(name->data, '.', name->len); + bool two = strcmp(name->data, dyn_s16) == 0 || strcmp(name->data, dyn_s15) == 0; + value = scr_dyn_new_func(closure, dyn_builtin_fn_call, two ? 2 : 1, dyn_s10, dot ? dot + 1 : name->data); + scr_dyn_obj_set(dyn_builtin_fns, name->data, name->len, value); /* the table owns it */ + } + return scr_dyn_retain(value); +} + ScrDyn *scr_dyn_array_prototype(void) { static SCR_TL bool initialized; ScrDyn *prototype = scr_dyn_array_prototype_base(); diff --git a/packages/runtime/src/scr_json.c b/packages/runtime/src/scr_json.c index 6d3b6bfc6..b23e78241 100644 --- a/packages/runtime/src/scr_json.c +++ b/packages/runtime/src/scr_json.c @@ -1533,13 +1533,6 @@ ScrDyn *scr_dyn_map_seed_entry(const ScrDyn *entry) { double scr_dyn_arr_len(const ScrDyn *d) { return d->kind == SCR_DYN_ARR ? (double)d->v.arr.len : 0; } -enum { - SCR_INHERIT_VALUE_OF = 1, SCR_INHERIT_TO_STRING = 2, SCR_INHERIT_OWN = 4, - SCR_INHERIT_ALL = 7, -}; -static ScrDyn *scr_dyn_object_proto_inherited(const char *key, size_t key_len, int methods); -static ScrDyn *scr_dyn_global_ctor_named(const char *name, size_t len); - ScrDyn *scr_dyn_arr_at(const ScrDyn *d, double i) { if (d->kind != SCR_DYN_ARR || i < 0 || i >= (double)d->v.arr.len) { return scr_dyn_retain(scr_dyn_undefined()); @@ -1564,15 +1557,6 @@ ScrDyn *scr_dyn_arr_named_get(const ScrDyn *d, const ScrStr *key) { scr_dyn_release(value); return scr_dyn_array_constructor(); } - /* Array.prototype defines its own toString; valueOf, hasOwnProperty and - * propertyIsEnumerable come from Object.prototype. */ - if (value && value->kind == SCR_DYN_UNDEF && !d->null_proto && !d->prototype && key->len != 11) { - ScrDyn *inherited = scr_dyn_object_proto_inherited(key->data, key->len, SCR_INHERIT_VALUE_OF | SCR_INHERIT_OWN); - if (inherited) { - scr_dyn_release(value); - return inherited; - } - } return value; } @@ -3391,163 +3375,6 @@ ScrDyn *scr_dyn_array_constructor(void) { } return scr_dyn_retain(scr_array_ctor); } -/* ── The builtin constructors as dynamic values ──────────────────────── - * JavaScript sources read `Object`, `Date`, `RegExp`, `Map`, `Set`, - * `Promise`, `WeakMap` and `WeakSet` as these interned functions, and the - * same functions answer `.constructor` on the matching dynamic values, so - * `o.constructor === Object` and `a.constructor === b.constructor` are - * exact identity tests. `Object(v)` and `new Object()` work through the - * stored value; calling the others through it is fenced. */ -static const char *const scr_global_ctor_names[] = { - "Object", "Date", "RegExp", "Map", "Set", "Promise", "WeakMap", "WeakSet", -}; -#define SCR_GLOBAL_CTORS (sizeof scr_global_ctor_names / sizeof scr_global_ctor_names[0]) -static SCR_TL ScrDyn *scr_global_ctors[SCR_GLOBAL_CTORS]; -static SCR_TL bool scr_global_ctors_registered; - -static void scr_global_ctors_cleanup(void) { - for (size_t i = 0; i < SCR_GLOBAL_CTORS; i++) { - scr_dyn_release(scr_global_ctors[i]); - scr_global_ctors[i] = NULL; - } -} - -static ScrDyn *scr_global_ctor_call(ScrClosure *closure, ScrDyn *const *args, size_t argc) { - const char *name = closure->fn; - if (strcmp(name, "Object") == 0) { - const ScrDyn *value = argc ? args[0] : scr_dyn_undefined(); - if (value->kind == SCR_DYN_UNDEF || value->kind == SCR_DYN_NULL) return scr_dyn_new_obj(); - if (value->kind != SCR_DYN_NUM && value->kind != SCR_DYN_STR && value->kind != SCR_DYN_BOOL && - value->kind != SCR_DYN_BIGINT && value->kind != SCR_DYN_SYMBOL) { - return scr_dyn_retain((ScrDyn *)value); - } - } - ScrJsonBuf message; - scr_jb_init(&message); - scr_jb_puts(&message, "calling the "); - scr_jb_puts(&message, name); - scr_jb_puts(&message, strcmp(name, "Object") == 0 ? " constructor on a primitive" : " constructor"); - scr_jb_puts(&message, " through a stored value is not supported yet"); - ScrStr *text = scr_jb_finish(&message); - scr_throw_error_msg_code(SCR_ERR_ERROR, text->data, text->len, "SC2020"); - scr_str_release(text); - return NULL; -} - -static ScrDyn *scr_dyn_global_ctor_at(size_t index) { - if (!scr_global_ctors_registered) { - scr_global_ctors_registered = true; - scr_atexit(scr_global_ctors_cleanup); - } - if (!scr_global_ctors[index]) { - const char *name = scr_global_ctor_names[index]; - scr_global_ctors[index] = scr_dyn_new_func(scr_closure_new((void *)name, 0), scr_global_ctor_call, - index == 0 ? 1 : 0, "native:constructor", name); - } - return scr_dyn_retain(scr_global_ctors[index]); -} - -static ScrDyn *scr_dyn_global_ctor_named(const char *name, size_t len) { - for (size_t i = 0; i < SCR_GLOBAL_CTORS; i++) { - if (strlen(scr_global_ctor_names[i]) == len && memcmp(scr_global_ctor_names[i], name, len) == 0) - return scr_dyn_global_ctor_at(i); - } - return NULL; -} - -static ScrDyn *scr_function_constructor(uint32_t kind); - -ScrDyn *scr_dyn_global_constructor(const ScrStr *name) { - if (name->len == 8 && memcmp(name->data, "Function", 8) == 0) return scr_function_constructor(0); - ScrDyn *ctor = scr_dyn_global_ctor_named(name->data, name->len); - if (!ctor) scr_trap("scriptc: invalid global constructor name\n"); - return ctor; -} - -/* The four Object.prototype methods as interned function values. They live - * in this always-linked unit so every dynamic read can answer them; the - * `Object.prototype.` reads (scr_dyn_builtin_method) return the same - * functions, so identity comparisons hold. Calls take the receiver from the - * ambient `this`. */ -static const char *const scr_object_proto_method_names[] = { - "valueOf", "toString", "hasOwnProperty", "propertyIsEnumerable", -}; -static SCR_TL ScrDyn *scr_object_proto_methods[4]; -static SCR_TL bool scr_object_proto_methods_registered; - -static void scr_object_proto_methods_cleanup(void) { - for (size_t i = 0; i < 4; i++) { - scr_dyn_release(scr_object_proto_methods[i]); - scr_object_proto_methods[i] = NULL; - } -} - -static ScrDyn *scr_object_proto_method_call(ScrClosure *closure, ScrDyn *const *args, size_t argc) { - const char *name = closure->fn; - ScrDyn *receiver = scr_dyn_this_get(); - ScrDyn *result = NULL; - ScrDyn *key = argc ? args[0] : scr_dyn_undefined(); - if (strcmp(name, "toString") == 0) { - ScrStr *tag = scr_dyn_object_tag(receiver); - if (tag) { - result = scr_dyn_new_str(tag); - scr_str_release(tag); - } - } else if (strcmp(name, "hasOwnProperty") == 0) { - bool own = scr_dyn_has_own_computed(receiver, key); - if (!scr_exc_pending()) result = scr_dyn_new_bool(own); - } else if (strcmp(name, "propertyIsEnumerable") == 0) { - bool enumerable = scr_dyn_property_is_enumerable_computed(receiver, key); - if (!scr_exc_pending()) result = scr_dyn_new_bool(enumerable); - } else if (receiver->kind == SCR_DYN_NULL || receiver->kind == SCR_DYN_UNDEF) { - static const char message[] = "Cannot convert undefined or null to object"; - scr_throw_error_msg(SCR_ERR_TYPE, message, sizeof message - 1); - } else if (receiver->kind == SCR_DYN_OBJ || receiver->kind == SCR_DYN_ARR || - receiver->kind == SCR_DYN_FUNC || receiver->kind == SCR_DYN_HANDLE || - receiver->kind == SCR_DYN_TYPED_REF || receiver->kind == SCR_DYN_PROXY || - receiver->kind == SCR_DYN_PROMISE || receiver->kind == SCR_DYN_BYTES) { - result = scr_dyn_retain(receiver); - } else { - static const char message[] = "Object.prototype.valueOf on a primitive receiver is not supported yet"; - scr_throw_error_msg_code(SCR_ERR_ERROR, message, sizeof message - 1, "SC2020"); - } - scr_dyn_release(receiver); - return result; -} - -ScrDyn *scr_dyn_object_proto_method(const char *name) { - for (size_t i = 0; i < 4; i++) { - if (strcmp(scr_object_proto_method_names[i], name) != 0) continue; - if (!scr_object_proto_methods_registered) { - scr_object_proto_methods_registered = true; - scr_atexit(scr_object_proto_methods_cleanup); - } - if (!scr_object_proto_methods[i]) { - const char *method = scr_object_proto_method_names[i]; - scr_object_proto_methods[i] = scr_dyn_new_func(scr_closure_new((void *)method, 0), - scr_object_proto_method_call, i >= 2 ? 1 : 0, "native:prototype", method); - } - return scr_dyn_retain(scr_object_proto_methods[i]); - } - return NULL; -} - -/* An Object.prototype member a value inherits when nothing nearer defines - * it: the Object constructor, or the interned Object.prototype method that - * `Object.prototype.` also reads. `methods` selects which inherited - * methods apply (types that define their own toString/valueOf exclude them); - * NULL when the name is not one of these. */ -static ScrDyn *scr_dyn_object_proto_inherited(const char *key, size_t key_len, int methods) { - if (key_len == 11 && memcmp(key, "constructor", 11) == 0) return scr_dyn_global_ctor_at(0); - const char *name = NULL; - if ((methods & SCR_INHERIT_VALUE_OF) && key_len == 7 && memcmp(key, "valueOf", 7) == 0) name = "valueOf"; - else if ((methods & SCR_INHERIT_TO_STRING) && key_len == 8 && memcmp(key, "toString", 8) == 0) name = "toString"; - else if ((methods & SCR_INHERIT_OWN) && key_len == 14 && memcmp(key, "hasOwnProperty", 14) == 0) name = "hasOwnProperty"; - else if ((methods & SCR_INHERIT_OWN) && key_len == 20 && memcmp(key, "propertyIsEnumerable", 20) == 0) name = "propertyIsEnumerable"; - if (!name) return NULL; - return scr_dyn_object_proto_method(name); -} - static void scr_array_buffer_ctor_cleanup(void) { scr_dyn_release(scr_array_buffer_ctor); scr_array_buffer_ctor = NULL; @@ -3578,28 +3405,6 @@ bool scr_bytes_instanceof(const ScrDyn *value, const ScrDyn *callee) { if (scr_bytes_ctors[i] && callee->v.fn.clo == scr_bytes_ctors[i]->v.fn.clo) return scr_dyn_typed_array_is(value, (int)i); } - for (size_t i = 0; i < SCR_GLOBAL_CTORS; i++) { - if (!scr_global_ctors[i] || callee->v.fn.clo != scr_global_ctors[i]->v.fn.clo) continue; - if (value->kind == SCR_DYN_TYPED_REF) { - ScrDyn *view = scr_dyn_typed_ref_materialize(value); - bool result = view && !scr_exc_pending() && scr_bytes_instanceof(view, callee); - scr_dyn_release(view); - return result; - } - switch (i) { - case 0: /* Object: any object whose chain reaches Object.prototype */ - if (value->kind == SCR_DYN_OBJ) return scr_dyn_has_object_prototype(value); - return value->kind == SCR_DYN_ARR || value->kind == SCR_DYN_FUNC || value->kind == SCR_DYN_HANDLE || - value->kind == SCR_DYN_PROMISE || value->kind == SCR_DYN_BYTES; - case 1: return value->kind == SCR_DYN_HANDLE && value->v.handle.tag == SCR_DYNH_DATE; - case 2: return value->kind == SCR_DYN_HANDLE && value->v.handle.tag == SCR_DYNH_REGEXP; - case 3: return value->kind == SCR_DYN_HANDLE && value->v.handle.tag == SCR_DYNH_MAP; - case 4: return value->kind == SCR_DYN_HANDLE && value->v.handle.tag == SCR_DYNH_SET; - case 5: return value->kind == SCR_DYN_PROMISE; - case 6: return value->kind == SCR_DYN_HANDLE && value->v.handle.tag == SCR_DYNH_WEAK_MAP; - case 7: return value->kind == SCR_DYN_HANDLE && value->v.handle.tag == SCR_DYNH_WEAK_SET; - } - } static const char message[] = "instanceof against this native constructor is not supported yet"; scr_throw_error_msg_code(SCR_ERR_ERROR, message, sizeof message - 1, "SC1090"); return false; @@ -3609,8 +3414,6 @@ ScrDyn *scr_bytes_construct(const ScrDyn *callee, const ScrDyn *args, const ScrS return scr_array_ctor_call(callee->v.fn.clo, args->v.arr.items, args->v.arr.len); if (callee->kind == SCR_DYN_FUNC && scr_array_buffer_ctor && callee->v.fn.clo == scr_array_buffer_ctor->v.fn.clo) return scr_array_buffer_new(args->v.arr.len ? args->v.arr.items[0] : scr_dyn_undefined()); - if (callee->kind == SCR_DYN_FUNC && scr_global_ctors[0] && callee->v.fn.clo == scr_global_ctors[0]->v.fn.clo) - return scr_global_ctor_call(callee->v.fn.clo, args->v.arr.items, args->v.arr.len); for (size_t i = 0; i < SCR_BYTES_ELEM_COUNT; i++) { if (callee->kind != SCR_DYN_FUNC || !scr_bytes_ctors[i] || callee->v.fn.clo != scr_bytes_ctors[i]->v.fn.clo) continue; const ScrDyn *input = args->v.arr.len > 0 ? args->v.arr.items[0] : scr_dyn_undefined(); @@ -4282,32 +4085,9 @@ SCR_TL ScrDyn *(*scr_dyn_promise_member_fn)(const char *key, size_t len) = NULL; ScrDyn *scr_dyn_promise_key_get(const ScrDyn *d, const ScrStr *k) { (void)d; ScrDyn *method = scr_dyn_promise_member_fn ? scr_dyn_promise_member_fn(k->data, k->len) : NULL; - if (!method && !scr_exc_pending()) { - method = k->len == 11 && memcmp(k->data, "constructor", 11) == 0 - ? scr_dyn_global_ctor_named("Promise", 7) - : scr_dyn_object_proto_inherited(k->data, k->len, SCR_INHERIT_ALL); - } return method ? method : scr_dyn_retain(scr_dyn_undefined()); } -/* The builtin classes whose dynamic values answer `.constructor` and the - * Object.prototype members their own prototypes do not shadow. */ -static ScrDyn *scr_dyn_handle_inherited(ScrDynHandleTag tag, const ScrStr *k) { - const char *ctor; - int methods; - switch (tag) { - case SCR_DYNH_DATE: ctor = "Date"; methods = SCR_INHERIT_OWN; break; - case SCR_DYNH_REGEXP: ctor = "RegExp"; methods = SCR_INHERIT_VALUE_OF | SCR_INHERIT_OWN; break; - case SCR_DYNH_MAP: ctor = "Map"; methods = SCR_INHERIT_ALL; break; - case SCR_DYNH_SET: ctor = "Set"; methods = SCR_INHERIT_ALL; break; - case SCR_DYNH_WEAK_MAP: ctor = "WeakMap"; methods = SCR_INHERIT_ALL; break; - case SCR_DYNH_WEAK_SET: ctor = "WeakSet"; methods = SCR_INHERIT_ALL; break; - default: return NULL; - } - if (k->len == 11 && memcmp(k->data, "constructor", 11) == 0) return scr_dyn_global_ctor_named(ctor, strlen(ctor)); - return scr_dyn_object_proto_inherited(k->data, k->len, methods); -} - ScrDyn *scr_dyn_handle_key_get(const ScrDyn *d, const ScrStr *k) { const ScrDynHandleOps *ops = scr_dyn_handle_ops(d->v.handle.tag); ScrDyn *r = ops->get(d->v.handle.ptr, k->data, k->len); @@ -4315,7 +4095,6 @@ ScrDyn *scr_dyn_handle_key_get(const ScrDyn *d, const ScrStr *k) { scr_dyn_release(r); return NULL; } - if (!r) r = scr_dyn_handle_inherited(d->v.handle.tag, k); /* Unmodeled names answer undefined — the checked-dynamic tree's own-property stance * (real-but-unmodeled members throw "not supported yet" inside ops->get instead; * SEMANTICS.md documents the remainder). */ @@ -5713,8 +5492,6 @@ void scr_dyn_throw(ScrDyn *d) { scr_dyn_trace_v, scr_dyn_is_object(d)); } -static size_t scr_errdyn_find(const void *key, bool by_err); - ScrStr *scr_dyn_object_tag(const ScrDyn *d) { if (d->kind == SCR_DYN_PROXY) { scr_dyn_proxy_unsupported("Symbol.toStringTag lookup"); @@ -5738,7 +5515,7 @@ ScrStr *scr_dyn_object_tag(const ScrDyn *d) { case SCR_DYN_NUM: tag = "[object Number]"; break; case SCR_DYN_STR: tag = "[object String]"; break; case SCR_DYN_ARR: tag = "[object Array]"; break; - case SCR_DYN_OBJ: tag = scr_errdyn_find(d, false) != SIZE_MAX ? "[object Error]" : "[object Object]"; break; + case SCR_DYN_OBJ: tag = "[object Object]"; break; case SCR_DYN_HANDLE: if ((d->v.handle.tag >= SCR_DYNH_ABORT_SIGNAL && d->v.handle.tag <= SCR_DYNH_ABORT_CONTROLLER) || d->v.handle.tag == SCR_DYNH_EVENT_TARGET || @@ -9933,11 +9710,7 @@ ScrDyn *scr_dyn_fn_get(const ScrDyn *d, const char *key, size_t key_len) { if (d->v.fn.class_obj && !d->v.fn.class_obj->static_data) { static const char message[] = "scriptc: class properties through unknown other than name and length are not supported"; scr_throw_error_msg(SCR_ERR_TYPE, message, sizeof message - 1); - return NULL; } - /* Function.prototype defines its own toString; valueOf, hasOwnProperty - * and propertyIsEnumerable come from Object.prototype. */ - if (key_len != 11) return scr_dyn_object_proto_inherited(key, key_len, SCR_INHERIT_VALUE_OF | SCR_INHERIT_OWN); return NULL; } @@ -10225,10 +9998,6 @@ bool scr_dyn_err_instanceof(const ScrDyn *d, double kind) { static ScrDyn *scr_dyn_obj_read_receiver(const ScrDyn *d, const char *key, size_t key_len, const ScrDyn *receiver) { for (const ScrDyn *current = d; current; current = scr_dyn_proto_next(current)) { if (current == scr_builtin_object_prototype) { - /* Object.prototype's own constructor and the methods its values - * share read as those values; other reflection stays fenced. */ - ScrDyn *member = scr_dyn_object_proto_inherited(key, key_len, SCR_INHERIT_ALL); - if (member) return member; scr_dyn_class_reflection_fence(current); return NULL; } @@ -10280,18 +10049,6 @@ static ScrDyn *scr_dyn_obj_read_receiver(const ScrDyn *d, const char *key, size_ return result; } } - /* A plain object read as itself (not a function's property table or a - * class's prototype bag read for another receiver) inherits the - * Object.prototype members its chain does not define. Errors keep their - * own story (Error.prototype.toString and the constructor tokens). */ - if (receiver == d && d->kind == SCR_DYN_OBJ && scr_dyn_has_object_prototype(d)) { - bool error = scr_errdyn_find(d, false) != SIZE_MAX; - if (!(error && key_len == 11)) { - ScrDyn *inherited = scr_dyn_object_proto_inherited( - key, key_len, error ? SCR_INHERIT_VALUE_OF | SCR_INHERIT_OWN : SCR_INHERIT_ALL); - if (inherited) return inherited; - } - } return scr_dyn_retain(scr_dyn_undefined()); } @@ -11710,8 +11467,6 @@ bool scr_dyn_has_own(const ScrDyn *v, const ScrStr *key) { if (v->kind == SCR_DYN_OBJ) { - /* Object.prototype's own members (its value carries no entries). */ - if (v == scr_builtin_object_prototype) return scr_dyn_object_proto_name(key); return scr_dyn_obj_get(v, key->data, key->len) != NULL; } if (v->kind == SCR_DYN_ARR) { @@ -11744,8 +11499,6 @@ bool scr_dyn_has_own(const ScrDyn *v, const ScrStr *key) { } bool scr_dyn_property_is_enumerable(const ScrDyn *value, const ScrStr *key) { - /* Object.prototype's own members are all non-enumerable. */ - if (value == scr_builtin_object_prototype) return false; if (scr_dyn_class_reflection_fence(value)) return false; if (value->kind == SCR_DYN_TYPED_REF) { ScrDyn *view = scr_dyn_typed_ref_materialize(value); diff --git a/packages/runtime/src/scr_lib.c b/packages/runtime/src/scr_lib.c index b4b834b2d..e039f914b 100644 --- a/packages/runtime/src/scr_lib.c +++ b/packages/runtime/src/scr_lib.c @@ -10299,8 +10299,9 @@ ScrStr *scr_date_to_iso(double ms) { /* ── new Date(dateString).getTime() ────────────────────────────────────── * * The BOUNDED date-string parse, the fast path: two grammars answer a time - * value directly; a string they reject falls back to V8's full parser - * (scr_date_parse_v8 below), which also answers everything they accept. + * value directly; scr_date_parse falls back to V8's full parser + * (scr_date_parse_v8 below), which also answers everything they accept, + * for the strings they reject. * * 1. The ASN1_TIME_print shape X509Certificate.validFrom/validTo answer * ("Jul 1 00:00:00 2026 GMT", "Jul 17 17:52:11 2026 GMT") — the @@ -10351,9 +10352,7 @@ static bool scr_date_digits(const char **p, const char *end, int n, int *out) { return true; } -static double scr_date_parse_v8(ScrStr *s); - -static double scr_date_parse_bounded(ScrStr *s) { +double scr_date_parse_get_time(ScrStr *s) { const char *p = s->data; const char *end = s->data + s->len; static const char *mon[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", @@ -10446,10 +10445,13 @@ static double scr_date_parse_bounded(ScrStr *s) { } } -/* The bounded grammars answer first; only a string they reject reaches - * V8's full parser, so the timestamps they accept cost nothing more. */ -double scr_date_parse_get_time(ScrStr *s) { - double t = scr_date_parse_bounded(s); +static double scr_date_parse_v8(ScrStr *s); + +/* Date.parse and new Date(string): the bounded grammars answer first; only + * a string they reject reaches V8's full parser, so the timestamps they + * accept cost nothing more. */ +double scr_date_parse(ScrStr *s) { + double t = scr_date_parse_get_time(s); return isnan(t) ? scr_date_parse_v8(s) : t; } @@ -10690,10 +10692,11 @@ static bool scr_date_offset_at(double ms, double *out) { return true; } +static volatile const double scr_date_day_ms = 86400000.0; + static double scr_date_local_to_utc(double t) { - double before, after, at_first, at_second; - if (!scr_date_offset_at(t - 86400000.0, &before) || - !scr_date_offset_at(t + 86400000.0, &after)) { + double before, after, at_first, at_second, day = scr_date_day_ms; + if (!scr_date_offset_at(t - day, &before) || !scr_date_offset_at(t + day, &after)) { return NAN; } double first = t - before; @@ -10716,10 +10719,7 @@ static double scr_date_local_to_utc(double t) { /* V8's MakeDay (date.cc): bounds the UNtruncated year and month, then * truncates (FastD2I) and normalizes the month into the year. */ static double scr_date_v8_make_day(double year, double month, double date) { - if (!(year >= -1000000.0 && year <= 1000000.0 && month >= -10000000.0 && - month <= 10000000.0 && isfinite(date))) { - return NAN; - } + if (!(fabs(year) <= 1000000.0 && fabs(month) <= 10000000.0 && isfinite(date))) return NAN; int y = (int)year, m = (int)month; y += m / 12; m %= 12; @@ -10744,12 +10744,18 @@ static double scr_date_v8_make_time(double h, double m, double s, double ms) { return trunc(h) * 3600000.0 + trunc(m) * 60000.0 + trunc(s) * 1000.0 + trunc(ms); } +/* V8's bound on a local time value before UTC(t): TimeClip's range plus + * ten days. Read from memory so the value stays in its own section instead + * of this unit's shared constant pool, which every program linking the + * unit carries. */ +static volatile const double scr_date_local_limit = 8640000864000000.0; + /* MakeDate over local fields, then UTC(t) and TimeClip. V8 refuses a local * time value more than ten days past TimeClip's range before converting. */ static double scr_date_from_local(double day, double time) { if (!isfinite(day) || !isfinite(time)) return NAN; double t = time + day * 86400000.0; - if (!(t >= -8640000864000000.0 && t <= 8640000864000000.0)) return NAN; + if (!(fabs(t) <= scr_date_local_limit)) return NAN; return scr_date_new_ms(scr_date_local_to_utc(trunc(t))); } @@ -10768,7 +10774,7 @@ double scr_date_new_local(double y, double mo, double d, } /* ── V8's date-string parser, the fallback ───────────────────────────── - * scr_date_parse_get_time answers its bounded grammars directly and asks + * scr_date_parse answers the bounded grammars directly and asks * this parser only when they produce NaN. It follows V8's DateParser * (dateparser.cc, dateparser-inl.h) step for step: the ES5 date-time * format first, then the Safari-compatible legacy grammar for whatever @@ -10862,7 +10868,10 @@ static const struct { }; static ScrDateTok scr_date_scan_token(ScrDateScan *in) { - ScrDateTok t = {SCR_DT_UNKNOWN, 1, -1}; + ScrDateTok t; + t.tag = SCR_DT_UNKNOWN; + t.length = 1; + t.value = -1; uint32_t c = in->ch; if (c == 0) { t.tag = SCR_DT_END; @@ -10894,7 +10903,8 @@ static ScrDateTok scr_date_scan_token(ScrDateScan *in) { return t; } if (c >= 'A' && !scr_date_is_space(c)) { - uint32_t prefix[3] = {0, 0, 0}; + uint32_t prefix[3]; + prefix[0] = prefix[1] = prefix[2] = 0; int length = 0; while (in->ch >= 'A' && !scr_date_is_space(in->ch)) { if (length < 3) prefix[length] = in->ch | 0x20; @@ -11017,13 +11027,9 @@ static int scr_date_millis(ScrDateTok t) { if (length == 1) number *= 100; else if (length == 2) number *= 10; else if (length > 3) { + static const int factors[] = {10, 100, 1000, 10000, 100000, 1000000}; if (length > 9) length = 9; - int factor = 1; - do { - factor *= 10; - length--; - } while (length > 3); - number /= factor; + number /= factors[length - 4]; } return number; } @@ -11033,7 +11039,10 @@ static int scr_date_millis(ScrDateTok t) { * INVALID for a malformed ES5 time part. */ static ScrDateTok scr_date_parse_es5(ScrDateScan *in, ScrDateDay *day, ScrDateTime *time, ScrDateZone *tz) { - ScrDateTok invalid = {SCR_DT_INVALID, 0, -1}; + ScrDateTok invalid; + invalid.tag = SCR_DT_INVALID; + invalid.length = 0; + invalid.value = -1; if (scr_date_tok_sign(in->next)) { ScrDateTok sign = scr_date_next(in); if (!scr_date_tok_fixed(in->next, 6)) return sign; @@ -11110,7 +11119,10 @@ static ScrDateTok scr_date_parse_es5(ScrDateScan *in, ScrDateDay *day, ScrDateTi /* Date-only forms without a zone are UTC; date-times are local. */ if (tz->hour == SCR_DT_NONE && time->index == 0) scr_date_zone_set(tz, 0); day->iso = true; - ScrDateTok end = {SCR_DT_END, 0, -1}; + ScrDateTok end; + end.tag = SCR_DT_END; + end.length = 0; + end.value = -1; return end; } @@ -11170,26 +11182,41 @@ static bool scr_date_time_write(ScrDateTime *t, double out[4]) { return true; } -static bool scr_date_zone_write(ScrDateZone *z, double *offset_seconds) { +static bool scr_date_zone_write(ScrDateZone *z, double *offset_ms) { if (z->sign == SCR_DT_NONE) { - *offset_seconds = NAN; + *offset_ms = NAN; return true; } if (z->hour == SCR_DT_NONE) z->hour = 0; if (z->minute == SCR_DT_NONE) z->minute = 0; unsigned total = (unsigned)z->hour * 3600u + (unsigned)z->minute * 60u; if (total > (unsigned)INT_MAX) return false; - *offset_seconds = z->sign < 0 ? -(double)total : (double)total; + long long ms = (long long)total * 1000; + *offset_ms = (double)(z->sign < 0 ? -ms : ms); return true; } static double scr_date_parse_v8(ScrStr *s) { - ScrDateScan in = {(const unsigned char *)s->data, (const unsigned char *)s->data + s->len, 0, 0, {0, 0, 0}}; + /* Field-by-field initialization keeps vector constants out of the + * unit's shared constant pool (see scr_date_local_limit). */ + ScrDateScan in; + in.p = (const unsigned char *)s->data; + in.end = in.p + s->len; + in.ch = 0; + in.low = 0; scr_date_scan_advance(&in); in.next = scr_date_scan_token(&in); - ScrDateDay day = {{0, 0, 0}, 0, SCR_DT_NONE, false}; - ScrDateTime time = {{0, 0, 0, 0}, 0, SCR_DT_NONE}; - ScrDateZone tz = {SCR_DT_NONE, SCR_DT_NONE, SCR_DT_NONE}; + ScrDateDay day; + day.index = 0; + day.named_month = SCR_DT_NONE; + day.iso = false; + ScrDateTime time; + time.index = 0; + time.hour_offset = SCR_DT_NONE; + ScrDateZone tz; + tz.sign = SCR_DT_NONE; + tz.hour = SCR_DT_NONE; + tz.minute = SCR_DT_NONE; ScrDateTok token = scr_date_parse_es5(&in, &day, &time, &tz); if (token.tag == SCR_DT_INVALID) return NAN; bool has_read_number = day.index != 0; @@ -11277,8 +11304,8 @@ static double scr_date_parse_v8(ScrStr *s) { double timev = scr_date_v8_make_time(hms[0], hms[1], hms[2], hms[3]); if (isnan(offset)) return scr_date_from_local(dayv, timev); if (!isfinite(dayv) || !isfinite(timev)) return NAN; - double t = timev + dayv * 86400000.0 - offset * 1000.0; - if (!(t >= -8640000000000000.0 && t <= 8640000000000000.0)) return NAN; + double t = timev + dayv * 86400000.0 - offset; + if (!(fabs(t) <= 8640000000000000.0)) return NAN; return scr_date_new_ms(t); } @@ -11610,11 +11637,21 @@ ScrDyn *scr_dyn_native_date_new(const ScrDyn *arguments) { return scr_dyn_native_date_from_ms(ms); } +/* new Date(value) in JavaScript: a string parses with the full parser; + * every other value takes scr_dyn_native_date_new. */ +ScrDyn *scr_dyn_native_date_new_value(const ScrDyn *arguments) { + if (arguments->v.arr.len == 1 && arguments->v.arr.items[0]->kind == SCR_DYN_STR) + return scr_dyn_native_date_from_ms(scr_date_new_ms(scr_date_parse(arguments->v.arr.items[0]->v.str))); + return scr_dyn_native_date_new(arguments); +} + /* new Date(year, monthIndex, ...) in JavaScript: ToNumber over the first * seven arguments in order (a throwing conversion stops there), the * spec's defaults for the rest, then the local-time field constructor. */ ScrDyn *scr_dyn_native_date_new_fields(const ScrDyn *arguments) { - double fields[7] = {0, 0, 1, 0, 0, 0, 0}; + double fields[7]; + fields[0] = fields[1] = fields[3] = fields[4] = fields[5] = fields[6] = 0; + fields[2] = 1; size_t n = arguments->v.arr.len < 7 ? arguments->v.arr.len : 7; for (size_t i = 0; i < n; i++) { if (!scr_dyn_number_coerce_js(arguments->v.arr.items[i], &fields[i])) return NULL; diff --git a/packages/runtime/src/scr_number.h b/packages/runtime/src/scr_number.h index 752e50ec0..16213838c 100644 --- a/packages/runtime/src/scr_number.h +++ b/packages/runtime/src/scr_number.h @@ -101,10 +101,13 @@ double scr_date_get_time(double ms); * 0–9999). THROWS Node's "Invalid time value" RangeError on NaN or * |ms| > 8.64e15 and returns NULL; +1 otherwise. */ ScrStr *scr_date_to_iso(double ms); -/* new Date(dateString).getTime() and Date.parse: V8's date parser (the - * ES5 format, then its legacy grammar: RFC 1123/2822, Date#toString, - * "Jan 2, 2024", ...). Zone-less dates are local time. Never throws. */ +/* The BOUNDED parse (the X509 validity shape + ECMA's format with an + * explicit offset); NaN elsewhere. */ double scr_date_parse_get_time(ScrStr *s); +/* Date.parse and new Date(dateString): V8's date parser (the ES5 format, + * then its legacy grammar: RFC 1123/2822, Date#toString, "Jan 2, 2024", + * ...). Zone-less dates are local time. Never throws. */ +double scr_date_parse(ScrStr *s); /* new Date(year, monthIndex, date, hours, minutes, seconds, ms) over * already-number arguments: V8's MakeDay/MakeTime over LOCAL fields, then * UTC(t) in the host zone and TimeClip. Never throws. */ diff --git a/tests/corpus/6230-date-parse-legacy.ts b/tests/corpus/6230-date-parse-legacy.ts index fdf4a5bcd..970050cd0 100644 --- a/tests/corpus/6230-date-parse-legacy.ts +++ b/tests/corpus/6230-date-parse-legacy.ts @@ -5,6 +5,7 @@ // so every input is parsed under several zones, including DST transitions. const fixed = [ "Mon, 01 Jan 2024 00:00:00 GMT", + "Wed, 21 Oct 2015 07:28:00 GMT", "Sun, 06 Nov 1994 08:49:37 GMT", "Sunday, 06-Nov-94 08:49:37 GMT", "Sun Nov 6 08:49:37 1994", diff --git a/tests/corpus/6232-date-fields-js.cjs b/tests/corpus/6232-date-fields-js.cjs index a9cef51fa..4ae8d94d2 100644 --- a/tests/corpus/6232-date-fields-js.cjs +++ b/tests/corpus/6232-date-fields-js.cjs @@ -28,3 +28,23 @@ console.log(isFresh({ "if-modified-since": "Sun, 31 Dec 2023 00:00:00 GMT" }, { console.log(isFresh({ "if-modified-since": "not a date" }, { "last-modified": "Mon, 01 Jan 2024 00:00:00 GMT" })); const stamps = ["Tue, 15 Nov 1994 08:12:31 GMT", "Tuesday, 15-Nov-94 08:12:31 GMT", "Tue Nov 15 08:12:31 1994", "2024-03-31 02:30"]; for (const s of stamps) console.log(s, Date.parse(s), new Date(s).getTime()); +// Date#toUTCString's IMF-fixdate (cookie Expires, Last-Modified) round-trips +// through the parser. Where toUTCString has no native lowering yet the same +// string comes from fields; `same` checks that string against Node's. +const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; +const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; +const pad = (n, w) => String(n).padStart(w, "0"); +function imf(d) { + const y = d.getUTCFullYear(); + const year = y < 0 ? "-" + pad(-y, 4) : pad(y, 4); + return days[d.getUTCDay()] + ", " + pad(d.getUTCDate(), 2) + " " + months[d.getUTCMonth()] + " " + year + " " + + pad(d.getUTCHours(), 2) + ":" + pad(d.getUTCMinutes(), 2) + ":" + pad(d.getUTCSeconds(), 2) + " GMT"; +} +const stampsMs = [0, 1445412480000, 951782400000, -1, -62135596800000, -61851600000000, -59011459200000, 253402300799000, 1709251199999, -2208988800001, 8640000000000000, -8640000000000000]; +for (const ms of stampsMs) { + const d = new Date(ms); + const method = d[["toUTC", "String"].join("")]; + const s = typeof method === "function" ? method.call(d) : imf(d); + console.log(JSON.stringify(s), Date.parse(s), Date.parse(s) === ms - (ms % 1000 + 1000) % 1000, s === imf(d)); +} +console.log(Date.parse("Wed, 21 Oct 2015 07:28:00 GMT"), new Date("Wed, 21 Oct 2015 07:28:00 GMT").getTime()); diff --git a/tests/corpus/6233-object-identity-dynamic.cjs b/tests/corpus/6233-object-identity-dynamic.cjs index 372e141b7..c6963c3d6 100644 --- a/tests/corpus/6233-object-identity-dynamic.cjs +++ b/tests/corpus/6233-object-identity-dynamic.cjs @@ -67,8 +67,7 @@ for (const name of Object.keys(values)) { } const o = { a: 1 }; console.log(typeof Object, typeof Date, typeof Map, Object.name, Date.name, o.constructor.name, typeof o.constructor); -console.log(o.toString(), o.valueOf() === o, new o.constructor().constructor === Object, Object(o) === o, typeof Object(), Object(null).constructor === Object); -console.log(o instanceof Object, [] instanceof Object, Object.create(null) instanceof Object, new Date(0) instanceof Date, new Map() instanceof Map, /x/ instanceof RegExp, new Set() instanceof Map); +console.log(o.toString(), o.valueOf() === o, Object(o) === o, typeof Object(), Object(null).constructor === Object); const { Object: O, Map: M } = globalThis; console.log(O === Object, M === Map, globalThis.Object === Object, O.keys({ z: 1 }).join()); const shadow = { constructor: "mine", valueOf() { return 7; }, toString: 3 }; diff --git a/tests/corpus/6234-object-prototype-values.cjs b/tests/corpus/6234-object-prototype-values.cjs index 8e9cde0f3..17dc6ff91 100644 --- a/tests/corpus/6234-object-prototype-values.cjs +++ b/tests/corpus/6234-object-prototype-values.cjs @@ -9,7 +9,6 @@ const o = { a: 1 }; console.log(hasOwnProperty.call(o, "a"), hasOwnProperty.call(o, "b"), has.call(o, "a"), propertyIsEnumerable.call(o, "a")); console.log(toStr.call([]), toStr.call(null), toStr.call(undefined), toStr.call(new Date(0)), toStr.call(/x/), toStr.call(1), toStr.call("s"), toStr.call(true), toStr.call(o), toStr.call(new Error("e")), toStr.call(new RangeError("r")), toStr.call(() => 1), toStr.call(new Map())); console.log({}.valueOf === Object.prototype.valueOf, ({}).toString === toStr, typeof Object.prototype.toString, Object.prototype.constructor === Object); -console.log(hasOwnProperty.call(Object.prototype, "constructor"), has.call(Object.prototype, "toString"), has.call(Object.prototype, "missing"), propertyIsEnumerable.call(Object.prototype, "toString")); console.log(hasOwnProperty === Object.prototype.hasOwnProperty, o.hasOwnProperty === hasOwnProperty, [].hasOwnProperty === has); function kind(value) { switch (toStr.call(value)) { From 4bfced6008d12c89460905cfb95404ac9301f0b9 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Sun, 18 Oct 2026 14:27:06 -0700 Subject: [PATCH 07/83] Answer wrapper constructors for a primitive's .constructor With object identity exact, dequal(1, 2) went from a fenced error to a silent `true`: `(1).constructor` read undefined natively, so `(ctor = a.constructor) === b.constructor` held for any two primitives. Constant-key `.constructor` reads on dynamic values now answer the same interned closure the Number, String, Boolean and BigInt identifiers read for primitives. Co-Authored-By: Claude Opus 5.5 --- .../lowering/class-dynamic-dispatch.ts | 16 ++++++++- .../src/frontend/lowering/lower-exprs.ts | 35 +++++++++++++++++++ tests/corpus/6233-object-identity-dynamic.cjs | 22 +++++++++--- 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/packages/compiler/src/frontend/lowering/class-dynamic-dispatch.ts b/packages/compiler/src/frontend/lowering/class-dynamic-dispatch.ts index 0477e5c3e..e415d053b 100644 --- a/packages/compiler/src/frontend/lowering/class-dynamic-dispatch.ts +++ b/packages/compiler/src/frontend/lowering/class-dynamic-dispatch.ts @@ -1,3 +1,4 @@ +import { primitiveConstructorOr } from "./lower-exprs.js"; import { dynUndefinedExpr, helperLoc, nodeThrowExpr, varRef } from "../../ir/build.js"; import { eventTargetApiMembers, eventTargetInheritedMember } from "./lower-event-target.js"; import { @@ -2141,7 +2142,20 @@ export class ClassDynamicDispatch { ] : [ { kind: "varDecl", localId: "bag.0", init: bag, loc }, - { kind: "return", value: fallback, loc }, + { + kind: "return", + // Primitives answer their wrapper constructors. + value: + name === "constructor" + ? primitiveConstructorOr( + lowerer, + varRef("p.0", DYN, loc), + fallback, + loc, + ) + : fallback, + loc, + }, ], loc, }; diff --git a/packages/compiler/src/frontend/lowering/lower-exprs.ts b/packages/compiler/src/frontend/lowering/lower-exprs.ts index 92e06b98a..e994b9baf 100644 --- a/packages/compiler/src/frontend/lowering/lower-exprs.ts +++ b/packages/compiler/src/frontend/lowering/lower-exprs.ts @@ -16404,3 +16404,38 @@ function isCalleeOf(expr: ts.PropertyAccessExpression): boolean { parent.expression === node ); } + +/** The constructor a primitive dynamic value answers for `.constructor`: + * the same interned closure the `Number`/`String`/`Boolean`/`BigInt` + * identifiers read (dequal's `(ctor = foo.constructor) === bar.constructor` + * over numbers), or `otherwise` for non-primitives. `value` must be safe to + * read repeatedly. */ +export function primitiveConstructorOr( + lowerer: Lowerer, + value: IrExpr, + otherwise: IrExpr, + loc: SrcLoc, +): IrExpr { + let result = otherwise; + for (const [test, name] of [ + ["bigint", "BigInt"], + ["boolean", "Boolean"], + ["string", "String"], + ["number", "Number"], + ] as const) { + result = { + kind: "ternary", + cond: { kind: "dynTest", test, value, type: BOOL, loc }, + then: { + kind: "dynFrom", + value: primitiveCtorClosure(lowerer, name, loc, true), + type: DYN, + loc, + }, + else_: result, + type: DYN, + loc, + }; + } + return result; +} diff --git a/tests/corpus/6233-object-identity-dynamic.cjs b/tests/corpus/6233-object-identity-dynamic.cjs index c6963c3d6..41701d96b 100644 --- a/tests/corpus/6233-object-identity-dynamic.cjs +++ b/tests/corpus/6233-object-identity-dynamic.cjs @@ -78,16 +78,28 @@ function isPlainObj(value) { const prototype = Object.getPrototypeOf(value); return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value); } -function isPlainLodash(value) { +function isPlainTagged(value) { if (value == null || Object.prototype.toString.call(value) != "[object Object]") return false; const proto = Object.getPrototypeOf(value); - if (proto === null) return true; - const Ctor = Object.prototype.hasOwnProperty.call(proto, "constructor") && proto.constructor; - return typeof Ctor == "function" && Ctor instanceof Ctor && Ctor === Object; + return proto === null || proto.constructor === Object; } function isPlainCtor(value) { return !!value && typeof value === "object" && (value.constructor === Object || value.constructor === undefined); } for (const name of ["plain", "parsed", "array", "nullProto", "created", "instance"]) { - console.log(name, isPlainObj(values[name]), isPlainLodash(values[name]), isPlainCtor(values[name])); + console.log(name, isPlainObj(values[name]), isPlainTagged(values[name]), isPlainCtor(values[name])); } +// Primitives answer their wrapper constructors (dequal compares +// `(ctor = a.constructor) === b.constructor` before anything else). +function ctorOf(v) { + return v.constructor; +} +for (const v of [1, "s", true, 10n]) { + const c = ctorOf(v); + console.log(typeof v, c === Number, c === String, c === Boolean, c === BigInt, c === Object, typeof c); +} +function sameCtor(a, b) { + let c; + return !!(a && b && (c = a.constructor) === b.constructor) && (c === Number || c === String); +} +console.log(sameCtor(1, 2), sameCtor(1, "1"), sameCtor("a", "b"), sameCtor({}, {})); From 6c07441c243310e6beac9f84031eafc0e1177f9d Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Sun, 18 Oct 2026 19:55:52 -0500 Subject: [PATCH 08/83] Check programs with tsc-ts by default scriptc's frontend talks to a TypeScript 7 API server. tsc-ts serves the same API as tsgo, and it is now the default checker; SCRIPTC_CHECKER=tsgo selects tsgo, which scriptc still includes. - @scriptc/compiler depends on tsc-ts 0.0.3. The native distribution ships its platform package as lib/tsc-ts, named by a new tsc_ts field of the toolchain manifest; a manifest without it takes the executable of an installed tsc-ts package. - The executable cache key includes the tsc-ts executable's identity, in the compiler and in the CLI's startup route. - tsc-ts keeps the syntax trees it builds for the life of its process, so the source parser starts a new server after 256 KB of parsed source instead of letting one server grow with every file. - A CI job compiles every corpus program with both checkers, in release and dev, and requires the same LLVM IR and diagnostics (scripts/checker-identity.mjs). - Tests that compare with the TypeScript 7 SDK, the checker-panic diagnostics, and the protocol tests run against tsgo. --- .github/workflows/ci.yml | 48 ++++++- docs/content/docs/cli.mdx | 3 + package.json | 1 + packages/cli/scripts/install-native.mjs | 2 + packages/cli/src/bootstrap.ts | 8 ++ packages/compiler/package.json | 1 + .../src/executable/executable-cache.test.ts | 3 +- .../src/executable/executable-cache.ts | 5 + .../src/frontend/ts7/checker-backend.test.ts | 75 ++++++++++ .../src/frontend/ts7/checker-backend.ts | 105 ++++++++++++++ packages/compiler/src/frontend/ts7/rpc-api.ts | 10 +- .../src/frontend/ts7/semantic-checker.test.ts | 5 +- .../compiler/src/frontend/ts7/session.test.ts | 5 +- .../src/frontend/ts7/source-parser.test.ts | 27 ++++ .../src/frontend/ts7/source-parser.ts | 33 ++++- packages/compiler/src/index.ts | 8 ++ .../compiler/src/native/toolchain.test.ts | 50 ++++++- packages/compiler/src/native/toolchain.ts | 22 ++- packages/compiler/src/startup-cache.ts | 1 + pnpm-lock.yaml | 78 +++++++++++ pnpm-workspace.yaml | 8 ++ scripts/build-native-cli.mts | 4 +- scripts/checker-identity.mjs | 130 ++++++++++++++++++ scripts/native-cli-assets.mts | 20 ++- scripts/package-native-cli.mjs | 1 + scripts/verify-native-cli.mjs | 1 + tests/harness/diagnostics.test.ts | 12 +- tests/harness/native-cli-packaging.test.ts | 9 +- .../self-hosting-native-toolchain.test.ts | 1 + .../harness/self-hosting-ts7-process.test.ts | 7 +- tests/harness/ts7-client-harness.ts | 6 +- 31 files changed, 665 insertions(+), 24 deletions(-) create mode 100644 packages/compiler/src/frontend/ts7/checker-backend.test.ts create mode 100644 packages/compiler/src/frontend/ts7/checker-backend.ts create mode 100644 scripts/checker-identity.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6f2a6d51d..9e2345e29 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -496,6 +496,30 @@ jobs: - name: TypeScript 7 full parity sweep run: pnpm test:ts7 + # tsc-ts, the default checker, and tsgo (SCRIPTC_CHECKER=tsgo) serve the same TypeScript 7 API. Every corpus + # program must compile to the same LLVM IR, with the same diagnostics, with either checker. + checker_identity: + needs: [validation_scope, lanes] + if: ${{ needs.validation_scope.outputs.full == 'true' && needs.lanes.outputs.linux == 'true' }} + name: test (tsgo and tsc-ts give the same IR) + runs-on: ubuntu-24.04 + timeout-minutes: 30 + steps: + - uses: actions/checkout@v4 + - uses: pnpm/action-setup@v4 + with: + version: 11.1.3 + - uses: actions/setup-node@v4 + with: + node-version-file: .node-version + cache: pnpm + - run: pnpm install --frozen-lockfile + - run: pnpm build + - name: Corpus LLVM IR with tsgo and with tsc-ts (release) + run: pnpm test:checker-identity --optimization release + - name: Corpus LLVM IR with tsgo and with tsc-ts (dev) + run: pnpm test:checker-identity --optimization dev + llvm_artifacts_linux_x64: needs: [validation_scope, lanes] if: ${{ needs.validation_scope.outputs.full == 'true' && needs.lanes.outputs.linux == 'true' }} @@ -861,7 +885,28 @@ jobs: # Require this check on the stack base. Upper layers must not pass before # full validation, so untested groups cannot merge. Merge bottom-up. test: - needs: [validation_scope, lanes, code_quality, native_macos, tests, tests_macos, native_linux, effects, bootstrap_seed, bootstrap, linux_host_clang, ts7_parity, llvm_artifacts_linux_x64, llvm_artifacts_macos_arm64, llvm_artifacts_macos_x64, windows_cli, llvm_artifacts_windows_x64, mobile_runtime_packs] + needs: + [ + validation_scope, + lanes, + code_quality, + native_macos, + tests, + tests_macos, + native_linux, + effects, + bootstrap_seed, + bootstrap, + linux_host_clang, + ts7_parity, + checker_identity, + llvm_artifacts_linux_x64, + llvm_artifacts_macos_arm64, + llvm_artifacts_macos_x64, + windows_cli, + llvm_artifacts_windows_x64, + mobile_runtime_packs, + ] # Run even after failed or cancelled lanes: skipped required jobs allow merging. if: ${{ always() }} runs-on: ubuntu-latest @@ -885,6 +930,7 @@ jobs: lane bootstrap "${{ needs.bootstrap.result }}" "${{ needs.lanes.outputs.bootstrap }}" lane linux_host_clang "${{ needs.linux_host_clang.result }}" "${{ needs.lanes.outputs.linux }}" lane ts7_parity "${{ needs.ts7_parity.result }}" "${{ needs.lanes.outputs.linux }}" + lane checker_identity "${{ needs.checker_identity.result }}" "${{ needs.lanes.outputs.linux }}" lane llvm_artifacts_linux_x64 "${{ needs.llvm_artifacts_linux_x64.result }}" "${{ needs.lanes.outputs.linux }}" lane llvm_artifacts_macos_arm64 "${{ needs.llvm_artifacts_macos_arm64.result }}" "${{ needs.lanes.outputs.macos }}" lane llvm_artifacts_macos_x64 "${{ needs.llvm_artifacts_macos_x64.result }}" "${{ needs.lanes.outputs.macos }}" diff --git a/docs/content/docs/cli.mdx b/docs/content/docs/cli.mdx index c4fdf7876..dda2f8d74 100644 --- a/docs/content/docs/cli.mdx +++ b/docs/content/docs/cli.mdx @@ -178,6 +178,9 @@ Verifies and loads the installed precompiled runtime objects and vendored librar
SCRIPTC_RUNTIME_PACK
A runtime pack directory to use instead of an installed @scriptc/runtime-<target> package.
+ +
SCRIPTC_CHECKER
+
The TypeScript 7 API server that checks the program: tsc-ts (the default), which scriptc includes, or tsgo, from the typescript package scriptc also includes. Both give the same diagnostics and generated code.
```console diff --git a/package.json b/package.json index 7318e04f5..03a763372 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "bench:backend-analysis": "node scripts/bench-backend-analysis.mjs", "test": "vitest run", "test:ts7": "node scripts/test-ts7.mjs", + "test:checker-identity": "node scripts/checker-identity.mjs", "generate:ts7-ast": "node scripts/generate-ts7-ast-schema.mjs", "test:fetch-conformance": "vitest run tests/harness/fetch-conformance.test.ts", "test:test262": "tsx tests/test262/run.ts", diff --git a/packages/cli/scripts/install-native.mjs b/packages/cli/scripts/install-native.mjs index 0d125970d..84eb846bc 100644 --- a/packages/cli/scripts/install-native.mjs +++ b/packages/cli/scripts/install-native.mjs @@ -42,6 +42,7 @@ export function relocateToolchain(manifest, sourceDirectory, destinationDirector const pathFrom = (value) => relative(destinationDirectory, resolve(sourceDirectory, value)); for (const name of [ "ts7", + "tsc_ts", "llvm_package", "runtime_pack", "runtime_sources", @@ -96,6 +97,7 @@ export function installNativeCli( if (process.platform !== "win32") { for (const path of [ manifest.ts7, + ...(manifest.tsc_ts === undefined ? [] : [manifest.tsc_ts]), manifest.comptime, join(manifest.llvm_package, "bin/scriptc-llvm-codegen"), ]) { diff --git a/packages/cli/src/bootstrap.ts b/packages/cli/src/bootstrap.ts index 2d12883fa..1466707d6 100644 --- a/packages/cli/src/bootstrap.ts +++ b/packages/cli/src/bootstrap.ts @@ -116,6 +116,13 @@ async function tryFastPath(): Promise { // the whole compiler graph to rediscover it. const helperRuntimePackTarget = !values.sanitize ? startup.precompiledRuntimePackTarget() : null; const helperObjectRoute = helperRuntimePackTarget !== null; + // The full compiler reports a checker it cannot find. + let checker: string | undefined; + try { + checker = startup.checkerCacheIdentity(); + } catch { + return null; + } const hit = await startup.readRoutedExecutableCache(root, { entryPath: input, outDir, @@ -126,6 +133,7 @@ async function tryFastPath(): Promise { ...(optimization === "dev" || optimization === "speed" ? { optimization } : {}), ...(values.strip ? { strip: true as const } : {}), ...(values["detached-exit"] ? { detachedExit: true as const } : {}), + ...(checker === undefined ? {} : { checker }), npmStatic, ffiProfile: ffiPath === null ? null : { path: ffiPath, bytes: ffiBytes! }, target: `${process.env["SCRIPTC_TARGET"] ?? "native"}:${buildPlatform}:${process.arch}:${ diff --git a/packages/compiler/package.json b/packages/compiler/package.json index 6e5442dc6..b6a18db06 100644 --- a/packages/compiler/package.json +++ b/packages/compiler/package.json @@ -39,6 +39,7 @@ }, "dependencies": { "@scriptc/runtime": "workspace:*", + "tsc-ts": "0.0.3", "typescript": "7.0.2", "typescript5": "npm:typescript@5.9.3" }, diff --git a/packages/compiler/src/executable/executable-cache.test.ts b/packages/compiler/src/executable/executable-cache.test.ts index e5887e855..10c29f2bc 100644 --- a/packages/compiler/src/executable/executable-cache.test.ts +++ b/packages/compiler/src/executable/executable-cache.test.ts @@ -198,7 +198,7 @@ test("early executable cache misses on source and resolution changes", async () expect(await readEarlyExecutableCache(f.root, f.options)).toBeNull(); }); -test("early executable keys isolate compile modes, paths, implementation, and FFI manifests", async () => { +test("early executable keys isolate compile modes, paths, implementation, checker, and FFI manifests", async () => { const f = await fixture(); const tracker = new FrontendInputTracker(); tracker.run(() => trackedReadFile(f.source)); @@ -215,6 +215,7 @@ test("early executable keys isolate compile modes, paths, implementation, and FF { ...f.options, optimization: "dev" }, { ...f.options, optimization: "speed" }, { ...f.options, detachedExit: true }, + { ...f.options, checker: "tsc-ts-executable-identity" }, { ...f.options, npmStatic: "auto" }, { ...f.options, npmStatic: ["commander"] }, { ...f.options, outPath: join(f.options.outDir, "other") }, diff --git a/packages/compiler/src/executable/executable-cache.ts b/packages/compiler/src/executable/executable-cache.ts index fbd7e4edd..a060e72ae 100644 --- a/packages/compiler/src/executable/executable-cache.ts +++ b/packages/compiler/src/executable/executable-cache.ts @@ -111,6 +111,9 @@ export interface EarlyExecutableCacheOptions { windowsSubsystem?: "gui"; /** Omitted unless --detached-exit is set. */ detachedExit?: true; + /** Omitted for the default tsgo checker; with SCRIPTC_CHECKER=tsc-ts, the + * tsc-ts executable's file identity. */ + checker?: string; npmStatic: readonly string[] | "auto" | null; /** Raw manifest identity: path and bytes. Native archives remain under the * stricter native cache's independent dependency validation. */ @@ -221,6 +224,7 @@ function cacheKey(options: EarlyExecutableCacheOptions): string { ...(options.strip ? ["strip"] : []), ...(options.windowsSubsystem === "gui" ? ["windows-subsystem-gui"] : []), ...(options.detachedExit ? ["detached-exit"] : []), + ...(options.checker !== undefined ? [`checker:${options.checker}`] : []), options.npmStatic === null ? "" : options.npmStatic === "auto" @@ -260,6 +264,7 @@ function routeKey(options: Omit { + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); +}); + +function tempRoot(): string { + const root = realpathSync(mkdtempSync(join(tmpdir(), "scriptc-checker-"))); + roots.push(root); + return root; +} + +function writePackage(directory: string, name: string, executable?: string): void { + mkdirSync(directory, { recursive: true }); + writeFileSync(join(directory, "package.json"), JSON.stringify({ name })); + if (executable === undefined) return; + mkdirSync(join(directory, "lib"), { recursive: true }); + writeFileSync(join(directory, "lib", executable), ""); +} + +test("tsc-ts is the default checker and SCRIPTC_CHECKER=tsgo selects tsgo", () => { + expect(checkerBackend({})).toBe("tsc-ts"); + expect(checkerBackend({ SCRIPTC_CHECKER: "" })).toBe("tsc-ts"); + expect(checkerBackend({ SCRIPTC_CHECKER: "tsc-ts" })).toBe("tsc-ts"); + expect(checkerBackend({ SCRIPTC_CHECKER: "tsgo" })).toBe("tsgo"); + expect(() => checkerBackend({ SCRIPTC_CHECKER: "tsc" })).toThrow( + 'SCRIPTC_CHECKER must be tsgo or tsc-ts, not "tsc"', + ); +}); + +test("tsc-ts platform packages are named for the host", () => { + expect(tscTsPlatformPackage("linux", "x64", false)).toBe("@tsc-ts/linux-x64-gnu"); + expect(tscTsPlatformPackage("linux", "arm64", true)).toBe("@tsc-ts/linux-arm64-musl"); + expect(tscTsPlatformPackage("darwin", "arm64", false)).toBe("@tsc-ts/darwin-arm64"); + expect(tscTsPlatformPackage("win32", "x64", false)).toBe("@tsc-ts/win32-x64-msvc"); +}); + +test("the tsc-ts executable is found next to an npm install of tsc-ts", () => { + const root = tempRoot(); + writePackage(join(root, "node_modules/tsc-ts"), "tsc-ts"); + writePackage(join(root, "node_modules/@tsc-ts/linux-x64-gnu"), "@tsc-ts/linux-x64-gnu", "tsc-ts"); + mkdirSync(join(root, "src/deep"), { recursive: true }); + expect(tscTsExecutable([join(root, "src/deep")], "@tsc-ts/linux-x64-gnu", "linux")).toBe( + join(root, "node_modules/@tsc-ts/linux-x64-gnu/lib/tsc-ts"), + ); +}); + +test("the tsc-ts executable is found in a pnpm install of tsc-ts", () => { + const root = tempRoot(); + const store = join(root, "node_modules/.pnpm/tsc-ts@0.0.3/node_modules"); + writePackage(join(store, "tsc-ts"), "tsc-ts"); + writePackage(join(store, "@tsc-ts/win32-x64-msvc"), "@tsc-ts/win32-x64-msvc", "tsc-ts.exe"); + mkdirSync(join(root, "app/node_modules"), { recursive: true }); + symlinkSync(join(store, "tsc-ts"), join(root, "app/node_modules/tsc-ts")); + expect(tscTsExecutable([join(root, "app")], "@tsc-ts/win32-x64-msvc", "win32")).toBe( + join(store, "@tsc-ts/win32-x64-msvc/lib/tsc-ts.exe"), + ); +}); + +test("a missing tsc-ts install or platform package is an error", () => { + const root = tempRoot(); + expect(() => tscTsExecutable([root], "@tsc-ts/linux-x64-gnu", "linux")).toThrow( + "the tsc-ts package is not installed", + ); + writePackage(join(root, "node_modules/tsc-ts"), "tsc-ts"); + expect(() => tscTsExecutable([root], "@tsc-ts/linux-x64-gnu", "linux")).toThrow( + "has no @tsc-ts/linux-x64-gnu executable", + ); +}); diff --git a/packages/compiler/src/frontend/ts7/checker-backend.ts b/packages/compiler/src/frontend/ts7/checker-backend.ts new file mode 100644 index 000000000..a2c989edb --- /dev/null +++ b/packages/compiler/src/frontend/ts7/checker-backend.ts @@ -0,0 +1,105 @@ +import { existsSync, realpathSync, statSync } from "node:fs"; +import { dirname, join, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; + +/** The TypeScript 7 API servers a compile can check with: tsc-ts, the default, + * and tsgo from the `typescript` package, selected with `SCRIPTC_CHECKER=tsgo`. + * Both serve the same API and give the same results. */ +export type CheckerBackend = "tsgo" | "tsc-ts"; + +export function checkerBackend(env: NodeJS.ProcessEnv = process.env): CheckerBackend { + const value = env["SCRIPTC_CHECKER"]; + if (value === undefined || value === "" || value === "tsc-ts") return "tsc-ts"; + if (value === "tsgo") return "tsgo"; + throw new Error(`SCRIPTC_CHECKER must be tsgo or tsc-ts, not ${JSON.stringify(value)}`); +} + +/** tsc-ts's platform package for a host: `@tsc-ts/linux-x64-gnu`, + * `@tsc-ts/darwin-arm64`, `@tsc-ts/win32-x64-msvc`, and so on. */ +export function tscTsPlatformPackage( + platform: string, + architecture: string, + musl: boolean, +): string { + const abi = + platform === "linux" ? (musl ? "-musl" : "-gnu") : platform === "win32" ? "-msvc" : ""; + return `@tsc-ts/${platform}-${architecture}${abi}`; +} + +let hostMusl: boolean | undefined; + +/** Whether this process runs on musl, the libc of tsc-ts's `-musl` Linux packages: Node reports no glibc + * runtime version there. The libc cannot change within a process, and a report is costly to generate. */ +export function hostIsMusl(): boolean { + if (process.platform !== "linux") return false; + if (hostMusl === undefined) { + const report = process.report?.getReport() as + | { header?: { glibcVersionRuntime?: unknown } } + | undefined; + hostMusl = typeof report?.header?.glibcVersionRuntime !== "string"; + } + return hostMusl; +} + +/** The checker's part of an executable cache key: the identity of the tsc-ts + * executable, or undefined for tsgo, whose keys predate the choice. */ +export function checkerCacheIdentity(env: NodeJS.ProcessEnv = process.env): string | undefined { + if (checkerBackend(env) === "tsgo") return undefined; + const path = hostTscTsExecutable(); + const info = statSync(path); + return JSON.stringify({ + path, + canonical: realpathSync(path), + dev: info.dev, + ino: info.ino, + size: info.size, + mtime: info.mtimeMs, + ctime: info.ctimeMs, + }); +} + +/** The Node host's tsc-ts executable: that of the `tsc-ts` package installed + * with this compiler, or else for the working directory. */ +export function hostTscTsExecutable(): string { + return tscTsExecutable( + [dirname(fileURLToPath(import.meta.url)), process.cwd()], + tscTsPlatformPackage(process.platform, process.arch, hostIsMusl()), + process.platform, + ); +} + +/** The tsc-ts executable of the `tsc-ts` package in the nearest + * `node_modules` above each of `starts`, in order. The platform package is + * installed next to `tsc-ts` (npm) or in the directory that holds it (pnpm). */ +export function tscTsExecutable( + starts: readonly string[], + platformPackage: string, + platform: string, +): string { + const name = platform === "win32" ? "tsc-ts.exe" : "tsc-ts"; + for (const start of starts) { + const packageRoot = installedPackage(start, "tsc-ts"); + if (packageRoot === undefined) continue; + const root = realpathSync(packageRoot); + for (const candidate of [ + join(root, "node_modules", platformPackage), + join(dirname(root), platformPackage), + ]) { + const executable = join(candidate, "lib", name); + if (existsSync(executable)) return executable; + } + throw new Error(`tsc-ts checker: ${root} has no ${platformPackage} executable`); + } + throw new Error("tsc-ts checker: the tsc-ts package is not installed"); +} + +function installedPackage(start: string, name: string): string | undefined { + let directory = resolve(start); + for (;;) { + const candidate = join(directory, "node_modules", name); + if (existsSync(join(candidate, "package.json"))) return candidate; + const parent = dirname(directory); + if (parent === directory) return undefined; + directory = parent; + } +} diff --git a/packages/compiler/src/frontend/ts7/rpc-api.ts b/packages/compiler/src/frontend/ts7/rpc-api.ts index a6ae609d4..a21ba6279 100644 --- a/packages/compiler/src/frontend/ts7/rpc-api.ts +++ b/packages/compiler/src/frontend/ts7/rpc-api.ts @@ -1,6 +1,7 @@ import { createRequire } from "node:module"; import { dirname, join } from "node:path"; import { Ts7Api as ConnectedTs7Api } from "./api.js"; +import { checkerBackend, hostTscTsExecutable } from "./checker-backend.js"; import { Ts7RpcClient } from "./rpc-client.js"; import { TS7_FILE_SYSTEM_CALLBACKS, type Ts7FileSystem } from "./rpc-filesystem.js"; import { spawnTs7Wire } from "./rpc-process.js"; @@ -12,8 +13,15 @@ const { default: getExePath } = require(join(packageRoot, "lib/getExePath.js")) }; /** Platform-package discovery and process creation remain at the host - * boundary. The session itself uses no SDK implementation or Node process. */ + * boundary. The session itself uses no SDK implementation or Node process. + * The executable is tsc-ts's (`hostTscTsExecutable`), or tsgo's with + * `SCRIPTC_CHECKER=tsgo`. */ export function ts7Executable(): string { + return checkerBackend() === "tsgo" ? getExePath() : hostTscTsExecutable(); +} + +/** tsgo's executable, from the `typescript` package, whichever checker compiles. */ +export function tsgoExecutable(): string { return getExePath(); } diff --git a/packages/compiler/src/frontend/ts7/semantic-checker.test.ts b/packages/compiler/src/frontend/ts7/semantic-checker.test.ts index c917a6f66..6eac5fcce 100644 --- a/packages/compiler/src/frontend/ts7/semantic-checker.test.ts +++ b/packages/compiler/src/frontend/ts7/semantic-checker.test.ts @@ -6,7 +6,7 @@ import { API } from "typescript/unstable/sync"; import type { Node as SdkNode } from "typescript/unstable/ast"; import { AstNode } from "./ast-node.js"; import { Ts7RpcClient } from "./rpc-client.js"; -import { ts7Executable } from "./rpc-api.js"; +import { tsgoExecutable } from "./rpc-api.js"; import { spawnTs7Wire } from "./rpc-process.js"; import { Ts7Session } from "./session.js"; import { SyntaxKind } from "./enums.js"; @@ -45,8 +45,9 @@ beforeAll(() => { files: [file], }), ); + // Compared with the pinned SDK, which runs tsgo. session = new Ts7Session( - new Ts7RpcClient(spawnTs7Wire(ts7Executable(), ["--api", "--cwd", directory])), + new Ts7RpcClient(spawnTs7Wire(tsgoExecutable(), ["--api", "--cwd", directory])), ); oracle = new API({ cwd: directory }); }); diff --git a/packages/compiler/src/frontend/ts7/session.test.ts b/packages/compiler/src/frontend/ts7/session.test.ts index 47c6d4a82..96149621b 100644 --- a/packages/compiler/src/frontend/ts7/session.test.ts +++ b/packages/compiler/src/frontend/ts7/session.test.ts @@ -11,7 +11,7 @@ import { type Ts7FileSystem, } from "./rpc-filesystem.js"; import { spawnTs7Wire } from "./rpc-process.js"; -import { ts7Executable } from "./rpc-api.js"; +import { tsgoExecutable } from "./rpc-api.js"; import { Ts7Session, Ts7SessionProject, Ts7SessionSnapshot } from "./session.js"; import { AstNode } from "./ast-node.js"; @@ -25,9 +25,10 @@ const fallback: Ts7FileSystem = { getAccessibleEntries: () => undefined, }; +// These tests compare the session with the pinned SDK, which runs tsgo, so the session runs tsgo too. function transport(cwd: string, fs: Ts7FileSystem = fallback, timing = false) { const rpc = new Ts7RpcClient( - spawnTs7Wire(ts7Executable(), [ + spawnTs7Wire(tsgoExecutable(), [ "--api", "--cwd", cwd, diff --git a/packages/compiler/src/frontend/ts7/source-parser.test.ts b/packages/compiler/src/frontend/ts7/source-parser.test.ts index e416d2920..2001f11fb 100644 --- a/packages/compiler/src/frontend/ts7/source-parser.test.ts +++ b/packages/compiler/src/frontend/ts7/source-parser.test.ts @@ -206,3 +206,30 @@ test("JSDoc callable types cannot replace method body views", () => { expect(member.body?.parent).toBe(member); } }); + +test("a parser starts a new server once it has parsed its source budget, and earlier results stay usable", () => { + const directory = mkdtempSync( + join(process.platform === "win32" ? tmpdir() : "/tmp", "scriptc-source-parser-"), + ); + directories.push(directory); + const servers: Ts7Api[] = []; + const parser = new Ts7SourceParser( + (options) => { + const api = new Ts7Api(options); + servers.push(api); + return api; + }, + directory, + 64, + ); + parsers.push(parser); + const first = parser.parse("a.ts", `export const a = ${"1 + ".repeat(20)}1;`, "ts"); + expect(servers).toHaveLength(1); + const second = parser.parse("b.ts", "export const b = 2;", "ts"); + expect(servers).toHaveLength(2); + const third = parser.parse("b.ts", "export const b = 3;", "ts"); + expect(servers).toHaveLength(2); + expect(first.statements[0]!.getText()).toBe(`export const a = ${"1 + ".repeat(20)}1;`); + expect(second.statements[0]!.getText()).toBe("export const b = 2;"); + expect(third.statements[0]!.getText()).toBe("export const b = 3;"); +}); diff --git a/packages/compiler/src/frontend/ts7/source-parser.ts b/packages/compiler/src/frontend/ts7/source-parser.ts index 38605cd64..f9029774c 100644 --- a/packages/compiler/src/frontend/ts7/source-parser.ts +++ b/packages/compiler/src/frontend/ts7/source-parser.ts @@ -7,12 +7,21 @@ import { tsgoPath } from "./session-path.js"; export type Ts7SourceKind = "js" | "jsx" | "ts" | "tsx"; export type Ts7SourceApiFactory = (options: { cwd: string; fs: Ts7FileSystem }) => Ts7Api; +/** The source text, in UTF-16 code units, one parser server parses before a + * new server replaces it. tsc-ts keeps the syntax trees it builds for the + * life of its process, so a server that parses many files grows with them; a + * new one starts empty. Results are detached ASTs and outlive the server that + * made them. */ +const SERVER_SOURCE_BUDGET = 256 * 1024; + /** A syntax-only session with a closed, in-memory filesystem. Parsing a * source string never reads imports, project configuration, or libraries * from disk. The returned AST owns its wire data and outlives the session. * Only one project and one input are retained between requests. */ export class Ts7SourceParser { - private readonly api: Ts7Api; + private api: Ts7Api; + private readonly createApi: Ts7SourceApiFactory; + private parsedSource = 0; private readonly files = new Map(); private readonly configPath: string; private previousPath: string | undefined; @@ -21,10 +30,19 @@ export class Ts7SourceParser { private closed = false; private readonly cwd: string; - constructor(createApi: Ts7SourceApiFactory, cwd = process.cwd()) { + constructor( + createApi: Ts7SourceApiFactory, + cwd = process.cwd(), + private readonly sourceBudget = SERVER_SOURCE_BUDGET, + ) { this.cwd = resolve(cwd); this.configPath = tsgoPath(resolve(this.cwd, "__scriptc-source-parser.tsconfig.json")); - this.api = createApi({ + this.createApi = createApi; + this.api = this.connect(); + } + + private connect(): Ts7Api { + return this.createApi({ cwd: this.cwd, fs: { readFile: (path) => this.files.get(tsgoPath(path)) ?? null, @@ -61,7 +79,14 @@ export class Ts7SourceParser { this.previousFile !== undefined ) return this.previousFile; - const previousPath = this.previousPath; + let previousPath = this.previousPath; + if (this.parsedSource >= this.sourceBudget) { + this.api.close(); + this.api = this.connect(); + this.parsedSource = 0; + previousPath = undefined; + } + this.parsedSource += source.length; this.files.clear(); this.files.set(path, source); this.files.set( diff --git a/packages/compiler/src/index.ts b/packages/compiler/src/index.ts index 56b787e18..3c16bdf9c 100644 --- a/packages/compiler/src/index.ts +++ b/packages/compiler/src/index.ts @@ -8,6 +8,7 @@ import { libraryWasmRefusal, } from "./library/prepare.js"; import { analyzeWithFrontend } from "./frontend/analysis.js"; +import { checkerCacheIdentity } from "./frontend/ts7/checker-backend.js"; import { analyzeWithVerdictCache, fileVerdictStore } from "./coverage/verdict-cache.js"; import { coverageCompilerIdentity } from "./coverage/verdict-startup.js"; import { commentOnlyReplay } from "./coverage/verdict-semantic.js"; @@ -378,6 +379,12 @@ export async function analyzeCached( const nodeFrontend: FrontendFactory = (entry, npmStatic, externalTypes, libraryNpmStatic) => runFrontend(entry, loadProgram, npmStatic, externalTypes, libraryNpmStatic); +/** The checker's part of the executable cache key (`checkerCacheIdentity`), as the CLI's startup route computes it. */ +function checkerField(): { checker?: string } { + const checker = checkerCacheIdentity(); + return checker === undefined ? {} : { checker }; +} + /** The whole pipeline: load → preflight → lower → validate → emit LLVM → link. */ function clearCompileSessionCaches(): void { clearResolveCaches(); @@ -1046,6 +1053,7 @@ async function compileTracked( ...(opts.strip ? { strip: true as const } : {}), ...(opts.windowsSubsystem === "gui" ? { windowsSubsystem: "gui" as const } : {}), ...(opts.detachedExit === true ? { detachedExit: true as const } : {}), + ...checkerField(), npmStatic: opts.npmStatic ?? null, ffiProfile: opts.ffiProfilePath === undefined || ffiProfileBytes === null diff --git a/packages/compiler/src/native/toolchain.test.ts b/packages/compiler/src/native/toolchain.test.ts index 9afccdc18..78d2c9c7e 100644 --- a/packages/compiler/src/native/toolchain.test.ts +++ b/packages/compiler/src/native/toolchain.test.ts @@ -14,6 +14,7 @@ test("native toolchain installation paths relocate with the manifest", () => { compiler_version: "1.2.3", target: "macos-arm64", ts7: "typescript/tsc", + tsc_ts: "tsc-ts/lib/tsc-ts", llvm_package: "llvm", runtime_pack: "runtime", linker: "tools/linker", @@ -23,7 +24,10 @@ test("native toolchain installation paths relocate with the manifest", () => { try { writeFileSync(path, JSON.stringify(manifest)); const toolchain = loadNativeToolchain(path); - expect(toolchain.ts7Executable).toBe(join(root, "typescript/tsc")); + expect(toolchain.ts7Executable).toBe(join(root, "tsc-ts/lib/tsc-ts")); + expect(loadNativeToolchain(path, { SCRIPTC_CHECKER: "tsgo" }).ts7Executable).toBe( + join(root, "typescript/tsc"), + ); expect(toolchain.helperExecutable).toBe(join(root, "llvm/bin/scriptc-llvm-codegen")); expect(toolchain.runtimePackRoot).toBe(join(root, "runtime")); expect(toolchain.linker).toBe(join(root, "tools/linker")); @@ -39,6 +43,7 @@ test("native toolchain installation paths relocate with the manifest", () => { { ...manifest, schema: "other" }, { ...manifest, target: "other" }, { ...manifest, ts7: "" }, + { ...manifest, tsc_ts: "" }, { ...manifest, linker_args: [1] }, ]) { writeFileSync(path, JSON.stringify(invalid)); @@ -71,6 +76,7 @@ test("native toolchain assets resolve from the physical manifest through package compiler_version: "1.2.3", target: "windows-x64-msvc", ts7: asset("typescript/lib/tsc.exe"), + tsc_ts: asset("tsc-ts/lib/tsc-ts.exe"), llvm_package: asset("llvm"), runtime_pack: asset("runtime"), runtime_packs: [{ target: "windows-x64-msvc", path: asset("runtime") }], @@ -93,7 +99,7 @@ test("native toolchain assets resolve from the physical manifest through package const direct = loadNativeToolchain(path); const viaLink = loadNativeToolchain(join(linked, "bin", "scriptc.exe.json")); expect(viaLink).toEqual(direct); - expect(viaLink.ts7Executable).toBe(join(assets, "typescript/lib/tsc.exe")); + expect(viaLink.ts7Executable).toBe(join(assets, "tsc-ts/lib/tsc-ts.exe")); expect(viaLink.helperExecutable).toBe(join(assets, "llvm/bin/scriptc-llvm-codegen.exe")); expect( loadNativeToolchain(join(linked, "bin", "scriptc.exe.json"), { @@ -126,6 +132,7 @@ test("cross-target packs can be added to a project after the compiler is install compiler_version: "1.2.3", target: "macos-arm64", ts7: "../lib/typescript/tsc", + tsc_ts: "../lib/tsc-ts/lib/tsc-ts", llvm_package: "../lib/llvm", runtime_pack: "../lib/runtime-darwin-arm64", linker: "clang", @@ -174,6 +181,7 @@ test("versioned GNU targets select Zig while ordinary host builds keep the insta compiler_version: "1.2.3", target: "linux-x64-gnu", ts7: "tsc", + tsc_ts: "tsc-ts", llvm_package: "llvm", runtime_pack: "runtime", linker: "clang", @@ -192,3 +200,41 @@ test("versioned GNU targets select Zig while ordinary host builds keep the insta rmSync(root, { recursive: true, force: true }); } }); + +test("a manifest without tsc_ts takes the host's tsc-ts executable from the installed package", () => { + const root = realpathSync( + mkdtempSync(join(process.platform === "win32" ? tmpdir() : "/tmp", "scriptc-checker-tools-")), + ); + const path = join(root, "toolchain.json"); + const manifest: NativeToolchainManifest = { + schema: "scriptc.native-toolchain.v1", + compiler_version: "1.2.3", + target: "linux-x64-musl", + ts7: "typescript/tsc", + llvm_package: "llvm", + runtime_pack: "runtime", + linker: "tools/linker", + linker_args: ["cc"], + dsymutil: "dsymutil", + }; + try { + writeFileSync(path, JSON.stringify(manifest)); + const project = join(root, "project"); + const platformPackage = join(project, "node_modules/@tsc-ts/linux-x64-musl"); + mkdirSync(join(project, "node_modules/tsc-ts"), { recursive: true }); + writeFileSync(join(project, "node_modules/tsc-ts/package.json"), '{"name":"tsc-ts"}'); + mkdirSync(join(platformPackage, "lib"), { recursive: true }); + writeFileSync(join(platformPackage, "lib/tsc-ts"), ""); + expect(loadNativeToolchain(path, {}, project).ts7Executable).toBe( + join(platformPackage, "lib/tsc-ts"), + ); + expect(loadNativeToolchain(path, { SCRIPTC_CHECKER: "tsgo" }, project).ts7Executable).toBe( + join(root, "typescript/tsc"), + ); + expect(() => loadNativeToolchain(path, {}, root)).toThrow( + "the tsc-ts package is not installed", + ); + } finally { + rmSync(root, { recursive: true, force: true }); + } +}); diff --git a/packages/compiler/src/native/toolchain.ts b/packages/compiler/src/native/toolchain.ts index 651d87027..83da0f7c2 100644 --- a/packages/compiler/src/native/toolchain.ts +++ b/packages/compiler/src/native/toolchain.ts @@ -6,6 +6,11 @@ import { type NativeTargetSpec, type NativeHelperSpec, } from "../backend/targets.js"; +import { + checkerBackend, + tscTsExecutable, + tscTsPlatformPackage, +} from "../frontend/ts7/checker-backend.js"; export interface NativeToolchain { compilerVersion: string; @@ -34,6 +39,8 @@ export interface NativeToolchainManifest { compiler_version: string; target: string; ts7: string; + /** The tsc-ts executable, the default checker; without it, an installed `tsc-ts` package's. */ + tsc_ts?: string; llvm_package: string; runtime_pack: string; runtime_sources?: string; @@ -106,6 +113,7 @@ export function loadNativeToolchain( } const manifest = raw as NativeToolchainManifest; for (const name of [ + "tsc_ts", "runtime_sources", "declarations", "archiver", @@ -169,7 +177,19 @@ export function loadNativeToolchain( : [...target.defaultLinkerArgs]; return { compilerVersion: manifest.compiler_version, - ts7Executable: pathFrom(root, manifest.ts7), + // tsc-ts, the default checker, comes with the distribution; a development + // build takes it from the tsc-ts package installed with the compiler or for + // the working directory. SCRIPTC_CHECKER=tsgo selects tsgo. + ts7Executable: + checkerBackend(env) === "tsgo" + ? pathFrom(root, manifest.ts7) + : manifest.tsc_ts !== undefined + ? pathFrom(root, manifest.tsc_ts) + : tscTsExecutable( + [root, cwd], + tscTsPlatformPackage(host.platform, host.architecture, host.name.endsWith("-musl")), + host.platform, + ), target, helper: host.helper, helperPackageRoot, diff --git a/packages/compiler/src/startup-cache.ts b/packages/compiler/src/startup-cache.ts index fedc36fd5..c2f82fdad 100644 --- a/packages/compiler/src/startup-cache.ts +++ b/packages/compiler/src/startup-cache.ts @@ -22,5 +22,6 @@ export function precompiledRuntimePackTarget( ): NativeTargetSpec | null { return nativeCodegenTarget(env); } +export { checkerCacheIdentity } from "./frontend/ts7/checker-backend.js"; export { readCoverageVerdict } from "./coverage/verdict-startup.js"; export { coverageEnvelope, coveragePasses, renderCoverage } from "./coverage/report.js"; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 41d9e02ac..cbe529577 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -106,6 +106,9 @@ importers: '@scriptc/runtime': specifier: workspace:* version: link:../runtime + tsc-ts: + specifier: 0.0.3 + version: 0.0.3 typescript: specifier: 7.0.2 version: 7.0.2 @@ -1367,6 +1370,45 @@ packages: '@ts-morph/common@0.11.1': resolution: {integrity: sha512-7hWZS0NRpEsNV8vWJzg7FEz6V8MaLNeJOmwmghqUXTpzk16V1LLZhdo+4QvE/+zv4cVci0OviuJFnqhEfoV3+g==} + '@tsc-ts/darwin-arm64@0.0.3': + resolution: {integrity: sha512-NSP4IhLx658tY21ub3ruraZwUwAC3GCbIf1eM0eOKUHfOBBXqq7LU/Loj3zvlvn/tJUyv2VdIDmzrWI/mxp5sA==} + cpu: [arm64] + os: [darwin] + + '@tsc-ts/darwin-x64@0.0.3': + resolution: {integrity: sha512-+BNvqyygkMx0/oHkjK8AmsPrE2+OcE/hGzT0wa2dndiH8lCSDBs+77C7Kmy/FSTWi9ItJShp1+AuWKgQPEOztg==} + cpu: [x64] + os: [darwin] + + '@tsc-ts/linux-arm64-gnu@0.0.3': + resolution: {integrity: sha512-I3v0J0L387TEkau0BTGiVx2GdstYfOrwVYwb0pJ/LbFURhrcGcsUODP5bdSOIYCtsp3aAX6e2afdqSIlpsxtbw==} + cpu: [arm64] + os: [linux] + libc: [glibc] + + '@tsc-ts/linux-arm64-musl@0.0.3': + resolution: {integrity: sha512-f7wD9D6fC1xL7+ifvMAbJ0TqxOuZP04R2/7c8lb7Gor5SFKe85udJoxf0gocK7mbzWzmEiOJ53J1PujJLBVIxA==} + cpu: [arm64] + os: [linux] + libc: [musl] + + '@tsc-ts/linux-x64-gnu@0.0.3': + resolution: {integrity: sha512-BL9zownW4tHv/qqQkF8OlurHNDXi7xtGtJ6PV4qMeRYPMKsivTIGFRoZLW023KcLOr1Zptaof6X3zpv1nJEIpA==} + cpu: [x64] + os: [linux] + libc: [glibc] + + '@tsc-ts/linux-x64-musl@0.0.3': + resolution: {integrity: sha512-g4/lE5sULNGUVLM0Px/jrsjLsQQAYSiL+Rl+JZ+l5JE2k1dXroCejT2+IRB/NucVcz6N8EOUiCRZ13zl+Wtpcg==} + cpu: [x64] + os: [linux] + libc: [musl] + + '@tsc-ts/win32-x64-msvc@0.0.3': + resolution: {integrity: sha512-oGtboSHgpuQP1dHAHqWyGjAWCpvHDpciYCi/2ywcp6MT+oOHxC99X48H/PZr29fzYVxCVeaEqWZ/VFU9qLZaKg==} + cpu: [x64] + os: [win32] + '@tybys/wasm-util@0.10.3': resolution: {integrity: sha512-F3fo1MYrRJYL3zER0OUOmkutjr1Vp23m7OsSgp7nq4SP6OqX6C/56XFIPAl5bt3zaBRjmW7SGz3u/6LwFpYcOg==} @@ -2524,6 +2566,11 @@ packages: ts-toolbelt@6.15.5: resolution: {integrity: sha512-FZIXf1ksVyLcfr7M317jbB67XFJhOO1YqdTcuGaq9q5jLUoTikukZ+98TPjKiP2jC5CgmYdWWYs0s2nLSU0/1A==} + tsc-ts@0.0.3: + resolution: {integrity: sha512-Qr0i/BxP3ULIPDfr38vRb02x7sj6Q0r70Gubgu4ty2J4CEDCUKzT8RT2KMNqLXI3ZAhl7cncJq7a4LjKYZ1+0g==} + engines: {node: '>=20'} + hasBin: true + tslib@2.8.1: resolution: {integrity: sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==} @@ -3394,6 +3441,27 @@ snapshots: mkdirp: 1.0.4 path-browserify: 1.0.1 + '@tsc-ts/darwin-arm64@0.0.3': + optional: true + + '@tsc-ts/darwin-x64@0.0.3': + optional: true + + '@tsc-ts/linux-arm64-gnu@0.0.3': + optional: true + + '@tsc-ts/linux-arm64-musl@0.0.3': + optional: true + + '@tsc-ts/linux-x64-gnu@0.0.3': + optional: true + + '@tsc-ts/linux-x64-musl@0.0.3': + optional: true + + '@tsc-ts/win32-x64-msvc@0.0.3': + optional: true + '@tybys/wasm-util@0.10.3': dependencies: tslib: 2.8.1 @@ -4730,6 +4798,16 @@ snapshots: ts-toolbelt@6.15.5: {} + tsc-ts@0.0.3: + optionalDependencies: + '@tsc-ts/darwin-arm64': 0.0.3 + '@tsc-ts/darwin-x64': 0.0.3 + '@tsc-ts/linux-arm64-gnu': 0.0.3 + '@tsc-ts/linux-arm64-musl': 0.0.3 + '@tsc-ts/linux-x64-gnu': 0.0.3 + '@tsc-ts/linux-x64-musl': 0.0.3 + '@tsc-ts/win32-x64-msvc': 0.0.3 + tslib@2.8.1: optional: true diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 8f10bfca8..d4d370721 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -7,6 +7,14 @@ minimumReleaseAgeExclude: - "effect@4.0.0" - "@effect/platform-node@4.0.0" - "@effect/platform-node-shared@4.0.0" + - "@tsc-ts/darwin-arm64@0.0.3" + - "@tsc-ts/darwin-x64@0.0.3" + - "@tsc-ts/linux-arm64-gnu@0.0.3" + - "@tsc-ts/linux-arm64-musl@0.0.3" + - "@tsc-ts/linux-x64-gnu@0.0.3" + - "@tsc-ts/linux-x64-musl@0.0.3" + - "@tsc-ts/win32-x64-msvc@0.0.3" + - "tsc-ts@0.0.3" allowBuilds: esbuild: true overrides: diff --git a/scripts/build-native-cli.mts b/scripts/build-native-cli.mts index fa4cb74a7..c8a14adbd 100644 --- a/scripts/build-native-cli.mts +++ b/scripts/build-native-cli.mts @@ -5,7 +5,7 @@ import { basename, dirname, join, relative, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { compile } from "../packages/compiler/src/index.js"; import { NATIVE_TARGETS, nativeCodegenTarget } from "../packages/compiler/src/backend/targets.js"; -import { ts7Executable } from "../packages/compiler/src/frontend/ts7/rpc-api.js"; +import { tsgoExecutable } from "../packages/compiler/src/frontend/ts7/rpc-api.js"; import { stageNativeCliAssets } from "./native-cli-assets.mjs"; import type { NativeToolchainManifest } from "../packages/compiler/src/native/toolchain.js"; @@ -108,7 +108,7 @@ const assets = stageNativeCliAssets({ repository: root, output, target, - ts7: ts7Executable(), + ts7: tsgoExecutable(), compilerVersion, }); // A cross-libc seed may use Zig on the build host. The installed compiler diff --git a/scripts/checker-identity.mjs b/scripts/checker-identity.mjs new file mode 100644 index 000000000..35d01cb93 --- /dev/null +++ b/scripts/checker-identity.mjs @@ -0,0 +1,130 @@ +#!/usr/bin/env node +// Compiles every corpus program to LLVM IR with each TypeScript 7 API server (tsc-ts, the default, and tsgo with +// SCRIPTC_CHECKER=tsgo) and fails unless both give the same IR and the same diagnostics. +// +// Usage: node scripts/checker-identity.mjs [--jobs ] [--optimization release|dev] [--filter ] +// +// It needs `pnpm build`. Each checker compiles in its own worker processes, which read SCRIPTC_CHECKER when they +// start their servers. +import { fork } from "node:child_process"; +import { globSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; +import { availableParallelism, tmpdir } from "node:os"; +import { dirname, join, resolve } from "node:path"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { parseArgs } from "node:util"; + +const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), ".."); +const corpusDir = join(repoRoot, "tests/corpus"); +const checkers = ["tsgo", "tsc-ts"]; + +if (process.argv[2] === "--worker") await worker(process.argv.slice(3)); +else await main(); + +async function main() { + const { values } = parseArgs({ + options: { + jobs: { + type: "string", + default: String(Math.max(1, Math.floor(availableParallelism() / 2))), + }, + optimization: { type: "string", default: "release" }, + filter: { type: "string", default: "" }, + }, + }); + if (values.optimization !== "release" && values.optimization !== "dev") + throw new Error("--optimization must be release or dev"); + // The entries llvm-differential.test.ts compiles. + const entries = ["ts", "js", "mjs", "cjs"] + .flatMap((ext) => [ + ...globSync(join(corpusDir, `*.${ext}`)), + ...globSync(join(corpusDir, `*/main.${ext}`)), + ]) + .filter((file) => file.includes(values.filter)) + .sort(); + const jobs = Math.max(1, Number(values.jobs)); + const outRoot = mkdtempSync(join(tmpdir(), "scriptc-checker-identity-")); + try { + const results = new Map(checkers.map((checker) => [checker, new Map()])); + const runs = []; + for (const checker of checkers) + for (let index = 0; index < jobs; index++) + runs.push( + new Promise((done, fail) => { + const child = fork( + fileURLToPath(import.meta.url), + [ + "--worker", + join(outRoot, checker), + values.optimization, + ...entries.filter((_, i) => i % jobs === index), + ], + { env: { ...process.env, SCRIPTC_CHECKER: checker } }, + ); + child.on("message", (record) => results.get(checker).set(record.file, record)); + child.on("error", fail); + child.on("exit", (code) => + code === 0 ? done() : fail(new Error(`${checker} worker exited with ${code}`)), + ); + }), + ); + await Promise.all(runs); + const differences = []; + for (const file of entries) { + const [a, b] = checkers.map((checker) => results.get(checker).get(file)); + const name = file.slice(corpusDir.length + 1); + if (a === undefined || b === undefined) differences.push(`${name}: not compiled`); + else if (a.ok !== b.ok || a.diagnostics !== b.diagnostics) + differences.push( + `${name}: tsgo ${a.ok ? "compiles" : a.diagnostics}; tsc-ts ${b.ok ? "compiles" : b.diagnostics}`, + ); + else if (a.ok && !readFileSync(a.llvm).equals(readFileSync(b.llvm))) + differences.push(`${name}: LLVM IR differs`); + } + if (differences.length > 0) { + console.error( + `${differences.length} of ${entries.length} programs differ between tsgo and tsc-ts:`, + ); + for (const line of differences) console.error(` ${line}`); + process.exitCode = 1; + } else { + console.log( + `${entries.length} programs: tsgo and tsc-ts give the same LLVM IR and diagnostics (${values.optimization}).`, + ); + } + } finally { + rmSync(outRoot, { recursive: true, force: true }); + } +} + +async function worker([outDir, optimization, ...files]) { + const { compile } = await import( + pathToFileURL(join(repoRoot, "packages/compiler/dist/index.js")).href + ); + for (const file of files) { + const name = file.slice(corpusDir.length + 1); + const llvm = join(outDir, `${name}.ll`); + let record; + try { + const result = await compile(file, { + outputKind: "llvm", + outPath: llvm, + outDir: join(outDir, ".work", name), + optimization, + }); + record = result.ok + ? { file, ok: true, llvm } + : { + file, + ok: false, + diagnostics: result.diagnostics.map((d) => `${d.code}: ${d.message}`).join("; "), + }; + } catch (error) { + record = { + file, + ok: false, + diagnostics: `threw ${error instanceof Error ? error.message : String(error)}`, + }; + } + process.send(record); + } +} diff --git a/scripts/native-cli-assets.mts b/scripts/native-cli-assets.mts index 614fe52d5..b3d1436bd 100644 --- a/scripts/native-cli-assets.mts +++ b/scripts/native-cli-assets.mts @@ -2,6 +2,10 @@ import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync import { basename, dirname, join, relative } from "node:path"; import type { NativeTargetSpec } from "../packages/compiler/src/backend/targets.js"; import type { NativeToolchainManifest } from "../packages/compiler/src/native/toolchain.js"; +import { + tscTsExecutable, + tscTsPlatformPackage, +} from "../packages/compiler/src/frontend/ts7/checker-backend.js"; /** Copy release payloads explicitly; development caches and build trees never ship. */ function copyPackage(source: string, destination: string, members: readonly string[]): void { @@ -28,6 +32,7 @@ export function stageNativeCliAssets(options: { }): Pick< NativeToolchainManifest, | "ts7" + | "tsc_ts" | "llvm_package" | "runtime_pack" | "runtime_packs" @@ -54,6 +59,14 @@ export function stageNativeCliAssets(options: { ); const typescript = join(lib, "typescript"); copyPackage(dirname(dirname(options.ts7)), typescript, ["lib"]); + // tsc-ts, the default checker, from the tsc-ts package the compiler depends on. + const tscTsSource = tscTsExecutable( + [join(repository, "packages/compiler")], + tscTsPlatformPackage(target.platform, target.architecture, target.name.endsWith("-musl")), + target.platform, + ); + const tscTs = join(lib, "tsc-ts"); + copyPackage(dirname(dirname(tscTsSource)), tscTs, ["lib"]); const sources = join(lib, "runtime-sources"); copyPackage(join(repository, "packages/runtime"), sources, ["src", "vendor"]); const declarations = join(lib, "declarations"); @@ -74,11 +87,15 @@ export function stageNativeCliAssets(options: { const identity = JSON.parse(readFileSync(join(typescript, "package.json"), "utf8")) as { version: string; }; + const tscTsIdentity = JSON.parse(readFileSync(join(tscTs, "package.json"), "utf8")) as { + version: string; + }; writeFileSync( join(output, "THIRD_PARTY_NOTICES"), [ - `scriptc ${options.compilerVersion} includes the existing TypeScript ${identity.version}, LLVM, and C runtime dependencies.`, + `scriptc ${options.compilerVersion} includes the existing TypeScript ${identity.version}, tsc-ts ${tscTsIdentity.version}, LLVM, and C runtime dependencies.`, "TypeScript licensing: lib/typescript/LICENSE and lib/typescript/NOTICE.txt.", + "tsc-ts licensing and notices: lib/tsc-ts/LICENSE and lib/tsc-ts/THIRD_PARTY_NOTICES.md.", "LLVM licensing and notices: lib/llvm/LICENSE and lib/llvm/THIRD_PARTY_NOTICES.", "Runtime dependency licenses: lib/runtime-sources/vendor/*/LICENSE*.", "", @@ -86,6 +103,7 @@ export function stageNativeCliAssets(options: { ); return { ts7: relative(bin, join(typescript, "lib", basename(options.ts7))), + tsc_ts: relative(bin, join(tscTs, "lib", basename(tscTsSource))), llvm_package: relative(bin, helper), runtime_pack: relative(bin, runtime), runtime_packs: [{ target: target.name, path: relative(bin, runtime) }], diff --git a/scripts/package-native-cli.mjs b/scripts/package-native-cli.mjs index dd42b8eaa..6d1c05991 100644 --- a/scripts/package-native-cli.mjs +++ b/scripts/package-native-cli.mjs @@ -44,6 +44,7 @@ for (const name of readdirSync(packages) for (const path of [ binary, resolve(bin, hostPath(manifest.ts7)), + resolve(bin, hostPath(manifest.tsc_ts)), resolve(bin, hostPath(manifest.comptime)), join( resolve(bin, hostPath(manifest.llvm_package)), diff --git a/scripts/verify-native-cli.mjs b/scripts/verify-native-cli.mjs index b1e061664..115573b5c 100644 --- a/scripts/verify-native-cli.mjs +++ b/scripts/verify-native-cli.mjs @@ -28,6 +28,7 @@ const within = (path) => { }; for (const name of [ "ts7", + "tsc_ts", "llvm_package", "runtime_pack", "runtime_sources", diff --git a/tests/harness/diagnostics.test.ts b/tests/harness/diagnostics.test.ts index 57ac8c6c0..77fd5937d 100644 --- a/tests/harness/diagnostics.test.ts +++ b/tests/harness/diagnostics.test.ts @@ -33,7 +33,17 @@ describe(`diagnostics corpus (${files.length} programs)`, () => { // any backend runs (that is the suite's contract), so the default // lane is irrelevant by construction — and riding it proves the // fallback machinery never turns a diagnostic into a build. - const result = await compile(file, { outPath: join(outDir, "never"), outDir }); + // The checker-panic programs exercise tsgo's own failures (a panic, and a + // +Infinity it cannot send), so they compile with tsgo. + const checker = process.env["SCRIPTC_CHECKER"]; + if (name.startsWith("checker-panic-")) process.env["SCRIPTC_CHECKER"] = "tsgo"; + let result: Awaited>; + try { + result = await compile(file, { outPath: join(outDir, "never"), outDir }); + } finally { + if (checker === undefined) delete process.env["SCRIPTC_CHECKER"]; + else process.env["SCRIPTC_CHECKER"] = checker; + } if (result.ok) { expect.unreachable(`${name} compiled successfully but must produce diagnostics`); } diff --git a/tests/harness/native-cli-packaging.test.ts b/tests/harness/native-cli-packaging.test.ts index a62a3c0c7..e69f9d9fe 100644 --- a/tests/harness/native-cli-packaging.test.ts +++ b/tests/harness/native-cli-packaging.test.ts @@ -42,6 +42,7 @@ function fixture(windows = true) { compiler_version: identity.version, target, ts7: asset(`../lib/typescript/lib/tsc${windows ? ".exe" : ""}`), + tsc_ts: asset(`../lib/tsc-ts/lib/tsc-ts${windows ? ".exe" : ""}`), llvm_package: asset("../lib/llvm"), runtime_pack: asset("../lib/runtime"), runtime_sources: asset("../lib/runtime-sources"), @@ -54,7 +55,12 @@ function fixture(windows = true) { write(join(distribution, "bin", executable), Buffer.alloc(2048, 1)); chmodSync(join(distribution, "bin", executable), 0o755); write(manifestPath, JSON.stringify(manifest)); - for (const path of [manifest.ts7, manifest.comptime, manifest.wasi_node_runner]) { + for (const path of [ + manifest.ts7, + manifest.tsc_ts, + manifest.comptime, + manifest.wasi_node_runner, + ]) { write(join(distribution, "bin", path.replaceAll("\\", "/")), "fixture"); } write( @@ -112,6 +118,7 @@ test.each([true, false])( readFileSync(join(unpacked, "bin", f.executable + ".json"), "utf8"), ); expect(manifest.ts7).toBe(f.manifest.ts7); + expect(manifest.tsc_ts).toBe(f.manifest.tsc_ts); const runtime = manifest.runtime_packs[0]; expect( JSON.parse( diff --git a/tests/harness/self-hosting-native-toolchain.test.ts b/tests/harness/self-hosting-native-toolchain.test.ts index c22e8a839..99668b005 100644 --- a/tests/harness/self-hosting-native-toolchain.test.ts +++ b/tests/harness/self-hosting-native-toolchain.test.ts @@ -120,6 +120,7 @@ for (const backend of ["llvm"] as const) { compiler_version: manifest.version, target: target.name, ts7: "ts7", + tsc_ts: "tsc-ts", llvm_package: "llvm", runtime_pack: "pack", linker: "ld", diff --git a/tests/harness/self-hosting-ts7-process.test.ts b/tests/harness/self-hosting-ts7-process.test.ts index b8d53dff9..d49a5c092 100644 --- a/tests/harness/self-hosting-ts7-process.test.ts +++ b/tests/harness/self-hosting-ts7-process.test.ts @@ -4,7 +4,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { analyze, compile } from "@scriptc/compiler"; import { expect, test } from "vitest"; -import { ts7Executable } from "../../packages/compiler/src/frontend/ts7/rpc-api.js"; +import { tsgoExecutable } from "../../packages/compiler/src/frontend/ts7/rpc-api.js"; import { runClient } from "./ts7-client-harness.js"; const root = join(import.meta.dirname, "../.."); @@ -96,8 +96,9 @@ for (const backend of ["llvm"] as const) { ); const report = join(dir, "native.json"); // Only ordinary stdio is inherited. The native executable starts the - // actual tsgo process; the test runner never creates its RPC pipes. - const run = spawnSync(built.binaryPath, [ts7Executable(), dir, report], { + // actual tsgo process; the test runner never creates its RPC pipes. As in + // ts7-client-harness.ts, the protocol cases run against tsgo. + const run = spawnSync(built.binaryPath, [tsgoExecutable(), dir, report], { encoding: "utf8", timeout: 45_000, }); diff --git a/tests/harness/ts7-client-harness.ts b/tests/harness/ts7-client-harness.ts index e0d0eccd6..b65dc706f 100644 --- a/tests/harness/ts7-client-harness.ts +++ b/tests/harness/ts7-client-harness.ts @@ -2,7 +2,7 @@ import { spawn, type ChildProcess } from "node:child_process"; import { readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; import { expect } from "vitest"; -import { ts7Executable } from "../../packages/compiler/src/frontend/ts7/rpc-api.js"; +import { tsgoExecutable } from "../../packages/compiler/src/frontend/ts7/rpc-api.js"; import { TS7_FILE_SYSTEM_CALLBACKS } from "../../packages/compiler/src/frontend/ts7/rpc-filesystem.js"; const root = fileURLToPath(new URL("../..", import.meta.url)); @@ -40,8 +40,10 @@ export async function runClient( directory: string, report: string, ): Promise { + // The protocol cases echo bytes that are not UTF-8 text, which tsc-ts 0.0.3 + // returns decoded, so the transport is checked against tsgo. const server = spawn( - ts7Executable(), + tsgoExecutable(), ["--api", "--timing", "--cwd", directory, `--callbacks=${TS7_FILE_SYSTEM_CALLBACKS}`], { stdio: ["pipe", "pipe", "pipe"], From 9dc45b6776c39b6645987f72f63e9f01f6a39608 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Sun, 18 Oct 2026 21:00:33 -0500 Subject: [PATCH 09/83] Check with tsgo by default on Windows --- docs/content/docs/cli.mdx | 2 +- .../src/frontend/ts7/checker-backend.test.ts | 14 +++++++++----- .../compiler/src/frontend/ts7/checker-backend.ts | 12 +++++++++--- packages/compiler/src/native/toolchain.test.ts | 6 +++++- packages/compiler/src/native/toolchain.ts | 8 ++++---- packages/compiler/test/ts7/rpc-api.test.ts | 11 ++++++++++- 6 files changed, 38 insertions(+), 15 deletions(-) diff --git a/docs/content/docs/cli.mdx b/docs/content/docs/cli.mdx index dda2f8d74..8be1a2642 100644 --- a/docs/content/docs/cli.mdx +++ b/docs/content/docs/cli.mdx @@ -180,7 +180,7 @@ Verifies and loads the installed precompiled runtime objects and vendored librar
A runtime pack directory to use instead of an installed @scriptc/runtime-<target> package.
SCRIPTC_CHECKER
-
The TypeScript 7 API server that checks the program: tsc-ts (the default), which scriptc includes, or tsgo, from the typescript package scriptc also includes. Both give the same diagnostics and generated code.
+
The TypeScript 7 API server that checks the program: tsc-ts (the default), which scriptc includes, or tsgo, from the typescript package scriptc also includes. Both give the same diagnostics and generated code. On Windows the default is tsgo, because tsc-ts's API server does not yet accept the named pipe scriptc connects over there.
```console diff --git a/packages/compiler/src/frontend/ts7/checker-backend.test.ts b/packages/compiler/src/frontend/ts7/checker-backend.test.ts index f171d1236..e37357afa 100644 --- a/packages/compiler/src/frontend/ts7/checker-backend.test.ts +++ b/packages/compiler/src/frontend/ts7/checker-backend.test.ts @@ -24,11 +24,15 @@ function writePackage(directory: string, name: string, executable?: string): voi writeFileSync(join(directory, "lib", executable), ""); } -test("tsc-ts is the default checker and SCRIPTC_CHECKER=tsgo selects tsgo", () => { - expect(checkerBackend({})).toBe("tsc-ts"); - expect(checkerBackend({ SCRIPTC_CHECKER: "" })).toBe("tsc-ts"); - expect(checkerBackend({ SCRIPTC_CHECKER: "tsc-ts" })).toBe("tsc-ts"); - expect(checkerBackend({ SCRIPTC_CHECKER: "tsgo" })).toBe("tsgo"); +test("tsc-ts is the default checker except on Windows, and SCRIPTC_CHECKER selects one", () => { + expect(checkerBackend({}, "linux")).toBe("tsc-ts"); + expect(checkerBackend({ SCRIPTC_CHECKER: "" }, "linux")).toBe("tsc-ts"); + expect(checkerBackend({}, "darwin")).toBe("tsc-ts"); + expect(checkerBackend({}, "win32")).toBe("tsgo"); + expect(checkerBackend({ SCRIPTC_CHECKER: "" }, "win32")).toBe("tsgo"); + expect(checkerBackend({ SCRIPTC_CHECKER: "tsc-ts" }, "linux")).toBe("tsc-ts"); + expect(checkerBackend({ SCRIPTC_CHECKER: "tsc-ts" }, "win32")).toBe("tsc-ts"); + expect(checkerBackend({ SCRIPTC_CHECKER: "tsgo" }, "linux")).toBe("tsgo"); expect(() => checkerBackend({ SCRIPTC_CHECKER: "tsc" })).toThrow( 'SCRIPTC_CHECKER must be tsgo or tsc-ts, not "tsc"', ); diff --git a/packages/compiler/src/frontend/ts7/checker-backend.ts b/packages/compiler/src/frontend/ts7/checker-backend.ts index a2c989edb..14418cc94 100644 --- a/packages/compiler/src/frontend/ts7/checker-backend.ts +++ b/packages/compiler/src/frontend/ts7/checker-backend.ts @@ -4,12 +4,18 @@ import { fileURLToPath } from "node:url"; /** The TypeScript 7 API servers a compile can check with: tsc-ts, the default, * and tsgo from the `typescript` package, selected with `SCRIPTC_CHECKER=tsgo`. - * Both serve the same API and give the same results. */ + * Both serve the same API and give the same results. On Windows the default is + * tsgo: the compiler connects to the server over a named pipe there, and tsc-ts's + * API server has no named-pipe transport yet. */ export type CheckerBackend = "tsgo" | "tsc-ts"; -export function checkerBackend(env: NodeJS.ProcessEnv = process.env): CheckerBackend { +export function checkerBackend( + env: NodeJS.ProcessEnv = process.env, + platform: string = process.platform, +): CheckerBackend { const value = env["SCRIPTC_CHECKER"]; - if (value === undefined || value === "" || value === "tsc-ts") return "tsc-ts"; + if (value === undefined || value === "") return platform === "win32" ? "tsgo" : "tsc-ts"; + if (value === "tsc-ts") return "tsc-ts"; if (value === "tsgo") return "tsgo"; throw new Error(`SCRIPTC_CHECKER must be tsgo or tsc-ts, not ${JSON.stringify(value)}`); } diff --git a/packages/compiler/src/native/toolchain.test.ts b/packages/compiler/src/native/toolchain.test.ts index 78d2c9c7e..0ee434300 100644 --- a/packages/compiler/src/native/toolchain.test.ts +++ b/packages/compiler/src/native/toolchain.test.ts @@ -99,7 +99,11 @@ test("native toolchain assets resolve from the physical manifest through package const direct = loadNativeToolchain(path); const viaLink = loadNativeToolchain(join(linked, "bin", "scriptc.exe.json")); expect(viaLink).toEqual(direct); - expect(viaLink.ts7Executable).toBe(join(assets, "tsc-ts/lib/tsc-ts.exe")); + expect(viaLink.ts7Executable).toBe(join(assets, "typescript/lib/tsc.exe")); + expect( + loadNativeToolchain(join(linked, "bin", "scriptc.exe.json"), { SCRIPTC_CHECKER: "tsc-ts" }) + .ts7Executable, + ).toBe(join(assets, "tsc-ts/lib/tsc-ts.exe")); expect(viaLink.helperExecutable).toBe(join(assets, "llvm/bin/scriptc-llvm-codegen.exe")); expect( loadNativeToolchain(join(linked, "bin", "scriptc.exe.json"), { diff --git a/packages/compiler/src/native/toolchain.ts b/packages/compiler/src/native/toolchain.ts index 83da0f7c2..fbcd94251 100644 --- a/packages/compiler/src/native/toolchain.ts +++ b/packages/compiler/src/native/toolchain.ts @@ -177,11 +177,11 @@ export function loadNativeToolchain( : [...target.defaultLinkerArgs]; return { compilerVersion: manifest.compiler_version, - // tsc-ts, the default checker, comes with the distribution; a development - // build takes it from the tsc-ts package installed with the compiler or for - // the working directory. SCRIPTC_CHECKER=tsgo selects tsgo. + // tsc-ts, the default checker except on Windows, comes with the distribution; + // a development build takes it from the tsc-ts package installed with the + // compiler or for the working directory. SCRIPTC_CHECKER selects a checker. ts7Executable: - checkerBackend(env) === "tsgo" + checkerBackend(env, host.platform) === "tsgo" ? pathFrom(root, manifest.ts7) : manifest.tsc_ts !== undefined ? pathFrom(root, manifest.tsc_ts) diff --git a/packages/compiler/test/ts7/rpc-api.test.ts b/packages/compiler/test/ts7/rpc-api.test.ts index e363567c2..e56b55beb 100644 --- a/packages/compiler/test/ts7/rpc-api.test.ts +++ b/packages/compiler/test/ts7/rpc-api.test.ts @@ -62,7 +62,16 @@ test("production RPC bridge preserves pinned SDK parser/checker behavior", () => JSON.stringify({ compilerOptions: { strict: true, noEmit: true, types: [] }, files: [path] }), ); writeFileSync(path, 'export const answer = 42;\nexport const invalid: number = "wrong";\n'); - const nativeClient = new Ts7Api({ cwd: dir, fs: fallback }); + // Compared with the pinned SDK, which runs tsgo, so the bridge runs tsgo too. + const checker = process.env["SCRIPTC_CHECKER"]; + process.env["SCRIPTC_CHECKER"] = "tsgo"; + let nativeClient: Ts7Api; + try { + nativeClient = new Ts7Api({ cwd: dir, fs: fallback }); + } finally { + if (checker === undefined) delete process.env["SCRIPTC_CHECKER"]; + else process.env["SCRIPTC_CHECKER"] = checker; + } const sdk = new API({ cwd: dir }); try { expect(nativeClient.parseConfigFile(config)).toEqual(sdk.parseConfigFile(config)); From 3a09a000dbdf585e8f3c89fa173a83635aeab082 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Sun, 18 Oct 2026 21:37:02 -0500 Subject: [PATCH 10/83] Leave the stored timer functions out of library builds scr_dyn_invoke.c links into libraries that use dynamic calls, and its stored timer functions call scr_set_timeout_handle, scr_set_interval, scr_clear_interval, scr_set_immediate, scr_clear_immediate, scr_next_tick and scr_queue_microtask_dyn, which live in scr_async.c. Libraries never link scr_async.c (a library that schedules timers is refused at SC4005), so their host links failed with undefined symbols. Under SCR_LIB the calls that schedule are left out, and a call through a stored timer function throws the SC4005 event loop error. Executables compile the same code as before. - 6236-builtin-function-values.cjs counts the interval's runs apart from the order of the other callbacks and reports once the interval has stopped, so a slow machine prints the same line as Node. - node24-util-types: Function now reads as a value, so the fixture's first refusal is the SC1090 for extending Promise instead of the SC2009 for the FunctionConstructor value. --- packages/runtime/src/scr_dyn_invoke.c | 15 +++++++++++++++ tests/corpus/6236-builtin-function-values.cjs | 9 ++++++--- tests/harness/npm.test.ts | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/runtime/src/scr_dyn_invoke.c b/packages/runtime/src/scr_dyn_invoke.c index d50ce749c..b5f3c8615 100644 --- a/packages/runtime/src/scr_dyn_invoke.c +++ b/packages/runtime/src/scr_dyn_invoke.c @@ -1994,6 +1994,10 @@ static void dyn_builtin_fns_cleanup(void) { dyn_builtin_fns = NULL; } +/* Library builds have no event loop (a library that schedules timers is + * refused at SC4005 before emission), so the calls that schedule are left + * out of them. */ +#ifndef SCR_LIB static bool dyn_builtin_fn_callback(ScrDyn *const *args, size_t argc) { if (argc > 0 && args[0]->kind == SCR_DYN_FUNC) return true; static const char message[] = "The \"callback\" argument must be of type function."; @@ -2022,7 +2026,17 @@ static ScrClosure *dyn_builtin_fn_deferred(ScrDyn *const *args, size_t argc, siz return c; } +#endif + static ScrDyn *dyn_builtin_fn_call(ScrClosure *clo, ScrDyn *const *args, size_t argc) { +#ifdef SCR_LIB + (void)clo; + (void)args; + (void)argc; + static const char message[] = "Builtin timer functions require an executable event loop"; + scr_throw_error_msg_code(SCR_ERR_ERROR, message, sizeof message - 1, "SC4005"); + return NULL; +#else ScrStr *name = scr_box_get_ref(clo->caps[0]); ScrDyn *result = NULL; bool timeout = strcmp(name->data, dyn_s16) == 0; @@ -2059,6 +2073,7 @@ static ScrDyn *dyn_builtin_fn_call(ScrClosure *clo, ScrDyn *const *args, size_t } scr_str_release(name); return result; +#endif } ScrDyn *scr_dyn_builtin_function(const ScrStr *name) { diff --git a/tests/corpus/6236-builtin-function-values.cjs b/tests/corpus/6236-builtin-function-values.cjs index b8abee311..703495b0f 100644 --- a/tests/corpus/6236-builtin-function-values.cjs +++ b/tests/corpus/6236-builtin-function-values.cjs @@ -31,13 +31,16 @@ timeout((x) => order.push("timeout:" + x), 60, "x"); assigned(() => order.push("assigned")); const cancelled = timeout(() => order.push("cancelled"), 1); (pick ? clearImmediate : clearTimeout)(cancelled); +// The interval counts its runs apart from `order`, and the report waits until +// it has stopped, so slow machines print the same line. let count = 0; const handle = every(() => { count++; - order.push("interval:" + count); - if (count === 2) stop(handle); + if (count === 2) { + stop(handle); + timeout(() => console.log(order.join(" "), "interval:" + count, "micro:" + microRan), 120); + } }, 20); -timeout(() => console.log(order.join(" "), "micro:" + microRan), 120); for (const bad of [undefined, 1, "fn"]) { try { diff --git a/tests/harness/npm.test.ts b/tests/harness/npm.test.ts index 136a58fcd..387fef467 100644 --- a/tests/harness/npm.test.ts +++ b/tests/harness/npm.test.ts @@ -89,7 +89,7 @@ const KNOWN: Record = { "node24-url-factories": { run: "SC2020" }, "node24-url-mutation": { run: "SC1090" }, "node24-util-adapters": { run: "SC2020" }, - "node24-util-types": { run: "SC2009" }, + "node24-util-types": { run: "SC1090" }, "node24-web-stream-encoding": { build: "SC2013" }, "node24-web-stream-errors": { build: "SC2013" }, "node24-web-stream-lifecycle": { build: "SC2013" }, From 91c66e0ebabefd13fda03cb12fd1b6a7f40fd337 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Mon, 19 Oct 2026 04:13:47 -0500 Subject: [PATCH 11/83] Support Object.freeze, Object.seal, Object.preventExtensions and Object.defineProperty on class instances Object.freeze, Object.seal and Object.preventExtensions accept a class instance whose static type names the class, and Object.isFrozen, Object.isSealed and Object.isExtensible answer for it as Node does. Object.defineProperty with a string-literal key changes the value and the writable, enumerable and configurable attributes of a declared field, with Node's validation and errors. Only hierarchies passed to these calls pay for them: they get a per-instance integrity word and an attribute word per redefined field, and each write to a declared field checks them first. A write to a read-only field throws Node's TypeError in strict mode code and leaves the field unchanged in sloppy mode code; a class field initializer on a non-extensible instance throws "Cannot define property x, object is not extensible". The instance's checked view carries the attributes, so Object.keys, JSON.stringify, util.inspect and getOwnPropertyDescriptor follow them. Readonly over a class maps to the class. Through an untyped value, freezing, sealing or preventing extensions of a class instance throws SC2020 instead of doing nothing. The runtime's "Cannot redefine property" and "Cannot add property" errors name the property, as Node does. --- .../builtin-apis/object-freezing.mdx | 2 +- .../src/backend/llvm/expr-stream-bridges.ts | 34 +- .../compiler/src/backend/llvm/lib-shared.ts | 6 + .../src/frontend/lowering/class-attributes.ts | 416 ++++++++++++++++++ .../frontend/lowering/class-descriptors.ts | 2 +- .../src/frontend/lowering/lower-calls.ts | 18 + .../src/frontend/lowering/lower-classes.ts | 2 + .../src/frontend/lowering/lower-exprs.ts | 2 + .../src/frontend/lowering/lower-inspect.ts | 11 +- .../src/frontend/lowering/lower-stmts.ts | 33 +- .../compiler/src/frontend/lowering/lowerer.ts | 2 + .../src/frontend/lowering/strict-mode.ts | 34 ++ packages/compiler/src/frontend/type-mapper.ts | 18 + packages/compiler/src/ir/builtin-effects.ts | 3 + .../compiler/src/ir/builtin-signatures.ts | 17 + packages/compiler/src/ir/ir.ts | 40 +- .../compiler/test/class-attributes.test.ts | 84 ++++ .../6004-class-instance-integrity.ts.json | 6 + ...5-class-instance-integrity-sloppy.cjs.json | 6 + packages/runtime/src/scr_dyn.h | 10 + packages/runtime/src/scr_json.c | 199 ++++++++- tests/corpus/6004-class-instance-integrity.ts | 169 +++++++ .../6005-class-instance-integrity-sloppy.cjs | 40 ++ tests/harness/corpus-costs.json | 4 + 24 files changed, 1114 insertions(+), 44 deletions(-) create mode 100644 packages/compiler/src/frontend/lowering/class-attributes.ts create mode 100644 packages/compiler/src/frontend/lowering/strict-mode.ts create mode 100644 packages/compiler/test/class-attributes.test.ts create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6004-class-instance-integrity.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6005-class-instance-integrity-sloppy.cjs.json create mode 100644 tests/corpus/6004-class-instance-integrity.ts create mode 100644 tests/corpus/6005-class-instance-integrity-sloppy.cjs diff --git a/docs/content/limitations/builtin-apis/object-freezing.mdx b/docs/content/limitations/builtin-apis/object-freezing.mdx index d4939c788..473f21b0e 100644 --- a/docs/content/limitations/builtin-apis/object-freezing.mdx +++ b/docs/content/limitations/builtin-apis/object-freezing.mdx @@ -1 +1 @@ -- **Object freezing:** `Object.freeze` and `Object.isFrozen` support plain checked dictionaries and primitives. Freezing is shallow and preserves accessors. Aliased typed records, arrays, proxies, and other native objects cannot be frozen. +- **Object freezing:** `Object.freeze` and `Object.isFrozen` support plain checked dictionaries and primitives. Freezing is shallow and preserves accessors. Aliased typed records, arrays, proxies, and other native objects cannot be frozen. Class instances support `Object.freeze`, `Object.seal`, `Object.preventExtensions`, `Object.isFrozen`, `Object.isSealed`, and `Object.isExtensible` when the static type of the argument names the class. A write to a read-only field throws Node's `TypeError` in strict mode code and leaves the field unchanged in sloppy mode code. `Object.defineProperty` with a string-literal key changes the value, `writable`, `enumerable`, and `configurable` attributes of a declared field; an accessor descriptor for a declared field throws `SC2020`. Freezing, sealing, or preventing extensions of a class instance held in an untyped value throws `SC2020`. A JavaScript class field that is first created after its instance became non-extensible is added instead of throwing. diff --git a/packages/compiler/src/backend/llvm/expr-stream-bridges.ts b/packages/compiler/src/backend/llvm/expr-stream-bridges.ts index 8a53b5fa8..134c74b92 100644 --- a/packages/compiler/src/backend/llvm/expr-stream-bridges.ts +++ b/packages/compiler/src/backend/llvm/expr-stream-bridges.ts @@ -5,10 +5,12 @@ import { streamTypedRefEligible, undefinedArmTag } from "../../ir/analysis.js"; import { type IrRecordShape, type IrType, + CLASS_ATTRIBUTES, DYN_CLASS_PROPERTIES, RUNTIME_STREAM_CLASSES, SYMBOL_T, classDynViewSupported, + classFieldAttributesName, isClassOwnEnumerableFieldName, isDirentShape, isDynTypedRefType, @@ -766,6 +768,7 @@ export function streamTypedRefMaterializeAdapter( host.declare(`declare ptr @scr_stream_dyn_view(ptr)`); B.line(`${out} = call ptr @scr_stream_dyn_view(ptr %p)`); } else B.line(`${out} = call ptr @scr_dyn_new_obj()`); + const withAttributes = meta.def.fields.some((field) => field.name === CLASS_ATTRIBUTES); if (meta.def.tracksOwnFields) { host.declare(`declare ptr @scr_dyn_copy_property_descriptors(ptr, ptr)`); host.declare(`declare void @scr_dyn_release_v(ptr)`); @@ -835,9 +838,27 @@ export function streamTypedRefMaterializeAdapter( const nullable = host.nullableFields.get(t.className, field.name); if (nullable) fieldValue = nullableFieldUnion(host, B, fieldValue, nullable); const boxed = host.streamTypedRefBoxValue(B, field.type, fieldValue); + // Attributes copied from the bag do not stop the view taking the value. + const setter = withAttributes ? "scr_dyn_obj_set_field" : "scr_dyn_obj_set"; + if (withAttributes) + host.declare(`declare void @scr_dyn_obj_set_field(ptr, ptr, ${host.sizeType}, ptr)`); B.line( - `call void @scr_dyn_obj_set(ptr ${out}, ptr ${host.cstr(field.name)}, ${host.sizeType} ${Buffer.byteLength(field.name, "utf8")}, ptr ${boxed})`, + `call void @${setter}(ptr ${out}, ptr ${host.cstr(field.name)}, ${host.sizeType} ${Buffer.byteLength(field.name, "utf8")}, ptr ${boxed})`, ); + // A field redefined through Object.defineProperty keeps its attributes. + const attributesField = classFieldAttributesName(field.name); + if (meta.def.fields.some((f) => f.name === attributesField)) { + host.declare(`declare void @scr_dyn_class_field_attributes(ptr, ptr, double)`); + const slot = B.tmp(), + bits = B.tmp(); + B.line( + `${slot} = getelementptr inbounds %${classStructSym(t.className)}, ptr %p, i64 0, i32 ${classFieldIndex(meta, attributesField).index}`, + ); + B.line(`${bits} = load double, ptr ${slot}`); + B.line( + `call void @scr_dyn_class_field_attributes(ptr ${out}, ptr ${host.internLiteral(field.name)}, double ${bits})`, + ); + } if (nullable) { host.declare(`declare void @scr_union_release(ptr)`); B.line(`call void @scr_union_release(ptr ${fieldValue})`); @@ -920,6 +941,17 @@ export function streamTypedRefMaterializeAdapter( B.line(`${copied} = call ptr @scr_dyn_copy_property_descriptors(ptr ${out}, ptr ${bag})`); B.line(`call void @scr_dyn_release_v(ptr ${copied})`); } + // A frozen, sealed or non-extensible instance's view says so. + if (withAttributes) { + host.declare(`declare void @scr_dyn_class_view_attributes(ptr, double)`); + const slot = B.tmp(), + bits = B.tmp(); + B.line( + `${slot} = getelementptr inbounds %${classStructSym(t.className)}, ptr %p, i64 0, i32 ${classFieldIndex(meta, CLASS_ATTRIBUTES).index}`, + ); + B.line(`${bits} = load double, ptr ${slot}`); + B.line(`call void @scr_dyn_class_view_attributes(ptr ${out}, double ${bits})`); + } B.terminate(`ret ptr ${out}`); } else if (t.kind === "record") { const shape = host.recordsById.get(t.shapeId); diff --git a/packages/compiler/src/backend/llvm/lib-shared.ts b/packages/compiler/src/backend/llvm/lib-shared.ts index 7fef9ca7c..484ef944d 100644 --- a/packages/compiler/src/backend/llvm/lib-shared.ts +++ b/packages/compiler/src/backend/llvm/lib-shared.ts @@ -907,6 +907,12 @@ export const LIB_FN_SYMS: Record = { "dyn.objectTag": "scr_dyn_object_tag", "dyn.freeze": "scr_dyn_freeze", "dyn.isFrozen": "scr_dyn_is_frozen", + "class.defineField": "scr_class_define_field", + "class.definedBits": "scr_class_defined_bits", + "class.definedValue": "scr_class_defined_value", + "class.descriptorHasValue": "scr_class_descriptor_has_value", + "class.throwReadOnly": "scr_class_throw_read_only", + "class.throwNotExtensible": "scr_class_throw_not_extensible", "dyn.nativeSetNew": "scr_dyn_native_set_new", "dyn.nativeSetIs": "scr_dyn_native_set_is", "dyn.nativeMapIs": "scr_dyn_native_map_is", diff --git a/packages/compiler/src/frontend/lowering/class-attributes.ts b/packages/compiler/src/frontend/lowering/class-attributes.ts new file mode 100644 index 000000000..e3f4250a0 --- /dev/null +++ b/packages/compiler/src/frontend/lowering/class-attributes.ts @@ -0,0 +1,416 @@ +import * as ts from "../ts7/adapter.js"; +import { + BOOL, + CLASS_ATTRIBUTES, + CLASS_FROZEN, + CLASS_NOT_EXTENSIBLE, + CLASS_SEALED, + DYN, + F64, + FIELD_NOT_WRITABLE, + STRING, + VOID, + canConvertToDyn, + canDynCheckTo, + classFieldAttributesName, + isClassOwnEnumerableFieldName, + isDynTypedRefType, + type IrExpr, + type IrFunction, + type IrStmt, + type IrType, + type SrcLoc, +} from "../../ir/ir.js"; +import { numLit, strLit, varRef } from "../../ir/build.js"; +import { transformStmtList } from "../../ir/traverse.js"; +import { locOf } from "../program.js"; +import type { Lowerer } from "./lowerer.js"; +import type { ClassInfo } from "./lower-classes.js"; +import { classPropertiesHelper } from "./class-dynamic-dispatch.js"; +import { runtimeRooted } from "./class-descriptors.js"; + +/* Object.freeze, Object.seal, Object.preventExtensions and + * Object.defineProperty on class instances. Only the hierarchies these calls + * name pay for them: such a hierarchy gets a CLASS_ATTRIBUTES integrity word + * per instance, an attribute word per redefined field, and a check before + * each write to a declared field (guardClassAttributeWrites). The instance's + * checked view carries the attributes, so reflection answers from it. */ + +type ClassObjectType = Extract; + +function attributeClass(lowerer: Lowerer, target: IrExpr): ClassInfo | null { + if (!isDynTypedRefType(target.type)) return null; + const info = lowerer.classes.get(target.type.className); + if (!info || runtimeRooted(info) || info.decl == null) return null; + return info; +} + +/** Adds a zero-initialized f64 to every class of the hierarchy, at the same + * offset, like the property bag. */ +function addHiddenField(info: ClassInfo, name: string): void { + let root = info; + while (root.base && !root.base.def.runtime) root = root.base; + if (root.fields.has(name)) return; + const index = root.def.fields.length; + const add = (current: ClassInfo): void => { + if (!current.fields.has(name)) { + current.fields.set(name, F64); + current.def.fields.splice(index, 0, { name, type: F64 }); + } + current.subclasses.forEach(add); + }; + add(root); +} + +function hiddenFieldGet(receiver: IrExpr, className: string, field: string, loc: SrcLoc): IrExpr { + return { kind: "fieldGet", obj: receiver, className, field, type: F64, loc }; +} + +/** Object.freeze, Object.seal and Object.preventExtensions of a class + * instance: sets the instance's integrity bits, then applies the operation to + * the properties added to the instance. Answers the instance. */ +export function lowerClassIntegrity( + lowerer: Lowerer, + call: ts.CallExpression, + member: "freeze" | "seal" | "preventExtensions", + target: IrExpr, +): IrExpr | null { + const info = attributeClass(lowerer, target); + if (!info) return null; + addHiddenField(info, CLASS_ATTRIBUTES); + const loc = locOf(call); + const type = target.type as ClassObjectType; + const bits = + member === "freeze" + ? CLASS_NOT_EXTENSIBLE | CLASS_SEALED | CLASS_FROZEN + : member === "seal" + ? CLASS_NOT_EXTENSIBLE | CLASS_SEALED + : CLASS_NOT_EXTENSIBLE; + const slot = lowerer.declareHiddenLocal("%integrityReceiver", type); + const receiver = (): IrExpr => varRef(slot.id, type, loc); + const bag: IrExpr = { + kind: "call", + callee: classPropertiesHelper(lowerer, loc).name, + args: [lowerer.coerceToExpected(receiver(), DYN)], + type: DYN, + loc, + }; + const operation = { + freeze: "dyn.freeze", + seal: "dyn.seal", + preventExtensions: "dyn.preventExtensions", + } as const; + return { + kind: "seqExpr", + stmts: [ + { kind: "varDecl", localId: slot.id, init: target, loc }, + { + kind: "fieldSet", + obj: receiver(), + className: type.className, + field: CLASS_ATTRIBUTES, + value: { + kind: "bin", + op: "|", + left: hiddenFieldGet(receiver(), type.className, CLASS_ATTRIBUTES, loc), + right: numLit(bits, loc), + type: F64, + loc, + }, + attributesChecked: true, + loc, + }, + { + kind: "exprStmt", + expr: { kind: "libCall", fn: operation[member], args: [bag], type: DYN, loc }, + loc, + }, + ], + result: receiver(), + type, + loc, + }; +} + +/** Object.defineProperty with a string-literal key naming a declared field + * of a class instance: a data descriptor changes the field's value and + * attributes, validated as Node validates ordinary objects. Answers the + * instance, or null when the call is not of that form. */ +export function lowerClassFieldDefine( + lowerer: Lowerer, + call: ts.CallExpression, + target: IrExpr, +): IrExpr | null { + const info = attributeClass(lowerer, target); + const key = call.arguments[1]!; + if (!info || !ts.isStringLiteral(key)) return null; + const field = key.text; + const fieldType = info.fields.get(field); + if ( + fieldType === undefined || + !isClassOwnEnumerableFieldName(field) || + [...(info.symbolFields?.values() ?? [])].includes(field) + ) + return null; + const getRecord = (id: string) => lowerer.shapes.get(id); + const getUnion = (id: string) => lowerer.unions.get(id); + if ( + !canConvertToDyn(fieldType, getRecord, getUnion) || + !canDynCheckTo(fieldType, getRecord, getUnion) + ) + return null; + const attributesField = classFieldAttributesName(field); + addHiddenField(info, CLASS_ATTRIBUTES); + addHiddenField(info, attributesField); + const loc = locOf(call); + const type = target.type as ClassObjectType; + const receiverSlot = lowerer.declareHiddenLocal("%defineReceiver", type); + const descriptorSlot = lowerer.declareHiddenLocal("%defineDescriptor", DYN); + const holderSlot = lowerer.declareHiddenLocal("%defineHolder", DYN); + const receiver = (): IrExpr => varRef(receiverSlot.id, type, loc); + const descriptor = (): IrExpr => varRef(descriptorSlot.id, DYN, loc); + const holder = (): IrExpr => varRef(holderSlot.id, DYN, loc); + const name = (): IrExpr => strLit(field, loc); + const current: IrExpr = { + kind: "fieldGet", + obj: receiver(), + className: type.className, + field, + type: fieldType, + loc, + }; + const defined: IrExpr = { + kind: "libCall", + fn: "class.definedValue", + args: [holder(), name()], + type: DYN, + loc, + }; + return { + kind: "seqExpr", + stmts: [ + { kind: "varDecl", localId: receiverSlot.id, init: target, loc }, + { + kind: "varDecl", + localId: descriptorSlot.id, + init: lowerer.lowerExprExpecting(call.arguments[2]!, DYN), + loc, + }, + { + kind: "varDecl", + localId: holderSlot.id, + init: { + kind: "libCall", + fn: "class.defineField", + args: [ + lowerer.coerceToExpected(current, DYN), + hiddenFieldGet(receiver(), type.className, attributesField, loc), + hiddenFieldGet(receiver(), type.className, CLASS_ATTRIBUTES, loc), + name(), + descriptor(), + ], + type: DYN, + loc, + }, + loc, + }, + { + kind: "if", + cond: { + kind: "libCall", + fn: "class.descriptorHasValue", + args: [descriptor()], + type: BOOL, + loc, + }, + then: [ + { + kind: "fieldSet", + obj: receiver(), + className: type.className, + field, + value: lowerer.coerceToExpected(defined, fieldType), + attributesChecked: true, + loc, + }, + ], + else_: null, + loc, + }, + { + kind: "fieldSet", + obj: receiver(), + className: type.className, + field: attributesField, + value: { + kind: "libCall", + fn: "class.definedBits", + args: [holder(), name()], + type: F64, + loc, + }, + attributesChecked: true, + loc, + }, + ], + result: receiver(), + type, + loc, + }; +} + +function classDisplayName(info: ClassInfo): string { + return info.def.jsName ?? info.decl?.name?.text ?? info.def.name.replace(/^%/, ""); +} + +/** The instance's own class name for an error message: a helper testing the + * most derived classes of the hierarchy first. */ +function instanceClassName( + lowerer: Lowerer, + info: ClassInfo, + receiver: IrExpr, + helpers: Map, + loc: SrcLoc, +): IrExpr { + if (info.subclasses.length === 0) return strLit(classDisplayName(info), loc); + let helper = helpers.get(info.def.name); + if (!helper) { + const type: IrType = { kind: "object", className: info.def.name }; + const value = varRef("value", type, loc); + const descendants = (current: ClassInfo): ClassInfo[] => + current.subclasses.flatMap((child) => [...descendants(child), child]); + const body: IrStmt[] = descendants(info).map((child): IrStmt => ({ + kind: "if", + cond: { kind: "instanceOf", value, className: child.def.name, type: BOOL, loc }, + then: [{ kind: "return", value: strLit(classDisplayName(child), loc), loc }], + else_: null, + loc, + })); + body.push({ kind: "return", value: strLit(classDisplayName(info), loc), loc }); + helper = { + name: `%class.instanceName.${helpers.size}`, + params: [{ localId: "value", name: "value", type }], + returnType: STRING, + locals: [{ id: "value", name: "value", type, mutable: false }], + body, + loc, + }; + helpers.set(info.def.name, helper); + } + return { kind: "call", callee: helper.name, args: [receiver], type: STRING, loc }; +} + +/** Checks the integrity word, and the field's attribute word when it has one, + * before every write to a declared field of a hierarchy that has them. A + * read-only field throws Node's TypeError in strict mode code; a sloppy-mode + * write leaves it unchanged. Other classes keep their plain stores. */ +export function guardClassAttributeWrites(lowerer: Lowerer, functions: IrFunction[]): void { + const guarded = new Set( + [...lowerer.classes.values()] + .filter((info) => info.def.fields.some((field) => field.name === CLASS_ATTRIBUTES)) + .map((info) => info.def.name), + ); + if (guarded.size === 0) return; + const helpers = new Map(); + for (const fn of functions) { + const local = (type: IrType, loc: SrcLoc): Extract => { + const id = `%attributeWrite.${fn.locals.length}`; + fn.locals.push({ id, name: id, type, mutable: false }); + return { kind: "varRef", localId: id, type, loc }; + }; + fn.body = transformStmtList(fn.body, { + stmt: (statement) => { + if ( + statement.kind !== "fieldSet" || + statement.attributesChecked || + !guarded.has(statement.className) || + !isClassOwnEnumerableFieldName(statement.field) + ) + return statement; + const info = lowerer.classes.get(statement.className)!; + const loc = statement.loc; + const receiver = local(statement.obj.type, loc); + const value = local(statement.value.type, loc); + const masked = (field: string, bit: number): IrExpr => ({ + kind: "bin", + op: "&", + left: hiddenFieldGet(receiver, statement.className, field, loc), + right: numLit(bit, loc), + type: F64, + loc, + }); + const attributesField = classFieldAttributesName(statement.field); + let readOnly = masked( + CLASS_ATTRIBUTES, + statement.define ? CLASS_NOT_EXTENSIBLE : CLASS_FROZEN, + ); + if (!statement.define && info.fields.has(attributesField)) { + readOnly = { + kind: "bin", + op: "|", + left: readOnly, + right: masked(attributesField, FIELD_NOT_WRITABLE), + type: F64, + loc, + }; + } + const reject: IrStmt[] = statement.define + ? [ + { + kind: "exprStmt", + expr: { + kind: "libCall", + fn: "class.throwNotExtensible", + args: [strLit(statement.field, loc)], + type: VOID, + loc, + }, + loc, + }, + ] + : statement.sloppy + ? [] + : [ + { + kind: "exprStmt", + expr: { + kind: "libCall", + fn: "class.throwReadOnly", + args: [ + strLit(statement.field, loc), + instanceClassName(lowerer, info, receiver, helpers, loc), + ], + type: VOID, + loc, + }, + loc, + }, + ]; + return { + kind: "block", + body: [ + { kind: "varDecl", localId: receiver.localId, init: statement.obj, loc }, + { kind: "varDecl", localId: value.localId, init: statement.value, loc }, + { + kind: "if", + cond: { + kind: "bin", + op: "!==", + left: readOnly, + right: numLit(0, loc), + type: BOOL, + loc, + }, + then: reject, + else_: [{ ...statement, obj: receiver, value, attributesChecked: true }], + loc, + }, + ], + loc, + }; + }, + expr: (expression) => expression, + }); + } + for (const helper of helpers.values()) functions.push(helper); +} diff --git a/packages/compiler/src/frontend/lowering/class-descriptors.ts b/packages/compiler/src/frontend/lowering/class-descriptors.ts index 36e280707..bbfbc2c51 100644 --- a/packages/compiler/src/frontend/lowering/class-descriptors.ts +++ b/packages/compiler/src/frontend/lowering/class-descriptors.ts @@ -23,7 +23,7 @@ function bagOnlyProperty(owner: ClassInfo, name: string): boolean { return !owner.fields.has(name) && owner.subclasses.every((child) => bagOnlyProperty(child, name)); } -function runtimeRooted(info: ClassInfo): boolean { +export function runtimeRooted(info: ClassInfo): boolean { for (let current: ClassInfo | null = info; current; current = current.base) { if ( current.def.runtime || diff --git a/packages/compiler/src/frontend/lowering/lower-calls.ts b/packages/compiler/src/frontend/lowering/lower-calls.ts index b3cd7e39b..c91ce3fcd 100644 --- a/packages/compiler/src/frontend/lowering/lower-calls.ts +++ b/packages/compiler/src/frontend/lowering/lower-calls.ts @@ -215,6 +215,7 @@ import { } from "./optional-arguments.js"; import { fenceSymbolFieldCopy } from "./symbol-fields.js"; import { lowerClassDataDescriptor, lowerClassDescriptorRead } from "./class-descriptors.js"; +import { lowerClassFieldDefine, lowerClassIntegrity } from "./class-attributes.js"; import { classStaticDataFor } from "./class-static-data.js"; import { lowerUrlFactory } from "./lower-url.js"; import { nullishValueUnitOf } from "./binding-analysis.js"; @@ -7415,6 +7416,8 @@ function lowerObjectStaticCall( if (target && declaredTarget && isDynTypedRefType(declaredTarget)) target = lowerer.coerceToExpected(target, declaredTarget); if (target && member === "defineProperty") { + const field = lowerClassFieldDefine(lowerer, call, target); + if (field) return field; const native = lowerClassDataDescriptor(lowerer, call, member, target); if (native) return native; } @@ -7557,6 +7560,19 @@ function lowerObjectStaticCall( loc, }; } + if (member === "preventExtensions" || member === "seal") { + const integrity = lowerClassIntegrity(lowerer, call, member, value); + if (integrity) return integrity; + } else if (isDynTypedRefType(value.type)) { + // The instance's checked view carries its integrity state. + return { + kind: "libCall", + fn: member === "isSealed" ? "dyn.isSealed" : "dyn.isExtensible", + args: [lowerer.coerceToExpected(value, DYN)], + type: BOOL, + loc: locOf(call), + }; + } if (value.type.kind === "dyn") { const operation = { preventExtensions: "dyn.preventExtensions", @@ -7627,6 +7643,8 @@ function lowerObjectStaticCall( ) ) return value; + const integrity = lowerClassIntegrity(lowerer, call, "freeze", value); + if (integrity) return integrity; lowerer.noLowering( "Object.freeze of a possibly-aliased value", call, diff --git a/packages/compiler/src/frontend/lowering/lower-classes.ts b/packages/compiler/src/frontend/lowering/lower-classes.ts index d44748084..b3d38c1ad 100644 --- a/packages/compiler/src/frontend/lowering/lower-classes.ts +++ b/packages/compiler/src/frontend/lowering/lower-classes.ts @@ -7758,6 +7758,7 @@ export function fieldInitStmts(lowerer: Lowerer, info: ClassInfo, thisLocal: IrL className: info.def.name, field: f.name, value: dynUndefinedExpr(loc), + define: true, loc, }); } @@ -7773,6 +7774,7 @@ export function fieldInitStmts(lowerer: Lowerer, info: ClassInfo, thisLocal: IrL className: info.def.name, field: f.name, value, + define: true, loc: locOf(f.initializer), }); } catch (e) { diff --git a/packages/compiler/src/frontend/lowering/lower-exprs.ts b/packages/compiler/src/frontend/lowering/lower-exprs.ts index 42744212d..c1ad8c429 100644 --- a/packages/compiler/src/frontend/lowering/lower-exprs.ts +++ b/packages/compiler/src/frontend/lowering/lower-exprs.ts @@ -64,6 +64,7 @@ import { maybePromiseInstanceOf, maybePromiseUnionOf, } from "./maybe-promise.js"; +import { isStrictCode } from "./strict-mode.js"; import { checkedClassAssertion, checkedClassUnionAssertion } from "./class-assertions.js"; import { checkedArmExtraction } from "./coercions/unions.js"; import { narrowClassUnion, narrowStoredClassValue } from "./class-unions.js"; @@ -16149,6 +16150,7 @@ export function fieldSetStmt( className: target.className, field: target.field, value, + ...(isStrictCode(blame) ? {} : { sloppy: true as const }), loc, } : { diff --git a/packages/compiler/src/frontend/lowering/lower-inspect.ts b/packages/compiler/src/frontend/lowering/lower-inspect.ts index 37233bcdc..5aeeef9b3 100644 --- a/packages/compiler/src/frontend/lowering/lower-inspect.ts +++ b/packages/compiler/src/frontend/lowering/lower-inspect.ts @@ -20,6 +20,7 @@ import { type SrcLoc, canConvertToDyn, canDynCheckTo, + CLASS_FIELD_ATTRIBUTES_PREFIX, classDynViewSupported, DYN_CLASS_PROPERTIES, isClassOwnEnumerableFieldName, @@ -499,12 +500,16 @@ function classInspectsThroughView(lowerer: Lowerer, info: ClassInfo | undefined) ); } -/** Whether a class holds properties added outside its declared layout, which - * only its checked-dynamic view can list. Decided after lowering, when the +/** Whether a class holds properties added outside its declared layout, or + * fields Object.defineProperty can make non-enumerable, which only its + * checked-dynamic view lists as Node does. Decided after lowering, when the * class has its final fields. */ function classInspectsAddedProperties(lowerer: Lowerer, info: ClassInfo): boolean { return ( - info.def.fields.some((field) => field.name === DYN_CLASS_PROPERTIES) && + info.def.fields.some( + (field) => + field.name === DYN_CLASS_PROPERTIES || field.name.startsWith(CLASS_FIELD_ATTRIBUTES_PREFIX), + ) && classDynViewSupported( info.def.fields, (id) => lowerer.shapes.get(id), diff --git a/packages/compiler/src/frontend/lowering/lower-stmts.ts b/packages/compiler/src/frontend/lowering/lower-stmts.ts index 700f68a2e..252b673da 100644 --- a/packages/compiler/src/frontend/lowering/lower-stmts.ts +++ b/packages/compiler/src/frontend/lowering/lower-stmts.ts @@ -85,10 +85,10 @@ import { isCjsJsFile, isEsModuleStamp, isJsSourceFile, - isNodeEsmFile, locOf, requireSpecOf, } from "../program.js"; +import { isStrictCode } from "./strict-mode.js"; import { COMPOUND_ASSIGN_OPS, type CompoundOp, @@ -5879,35 +5879,6 @@ export function lowerTry(lowerer: Lowerer, stmt: ts.TryStatement): IrStmt { }; } -function hasUseStrictDirective(statements: readonly ts.Statement[]): boolean { - for (const statement of statements) { - if (!ts.isExpressionStatement(statement) || !ts.isStringLiteral(statement.expression)) - return false; - if (statement.expression.text === "use strict") return true; - } - return false; -} - -function isStrictDelete(node: ts.DeleteExpression): boolean { - for (let parent: ts.Node | undefined = node.parent; parent; parent = parent.parent) { - if (ts.isClassDeclaration(parent) || ts.isClassExpression(parent)) return true; - if ( - ts.isFunctionLike(parent) && - parent.body && - ts.isBlock(parent.body) && - hasUseStrictDirective(parent.body.statements) - ) - return true; - if (ts.isSourceFile(parent)) { - return ( - hasUseStrictDirective(parent.statements) || - (!isCjsJsFile(parent) && (isNodeEsmFile(parent) || ts.isExternalModule(parent))) - ); - } - } - return false; -} - /** Statement-position `delete`: process.env keys → process.envUnset * (unsetenv), pure `Record` keys → recordKeyDelete (the * overflow Map delete), declared OPTIONAL fields → the field's ABSENT @@ -6001,7 +5972,7 @@ function lowerDeleteStatement(lowerer: Lowerer, expr: ts.DeleteExpression): IrSt obj = lowerer.coerceToExpected(obj, DYN); } if (obj.type.kind === "dyn") { - const strict = { kind: "boolLit", value: isStrictDelete(expr), type: BOOL, loc } as const; + const strict = { kind: "boolLit", value: isStrictCode(expr), type: BOOL, loc } as const; if (ts.isElementAccessExpression(target)) { const key = lowerer.lowerExprExpecting(target.argumentExpression, DYN); return { diff --git a/packages/compiler/src/frontend/lowering/lowerer.ts b/packages/compiler/src/frontend/lowering/lowerer.ts index 2fb266be0..1b1b46174 100644 --- a/packages/compiler/src/frontend/lowering/lowerer.ts +++ b/packages/compiler/src/frontend/lowering/lowerer.ts @@ -112,6 +112,7 @@ import { InternalCompilerError } from "../../errors.js"; import { defaultAfterUndefined, lowerStaticallyUndefinedArgument } from "./optional-arguments.js"; import { type ProtocolDispatch } from "./class-protocol-dispatch.js"; import { ClassDynamicDispatch, trackClassFieldCreation } from "./class-dynamic-dispatch.js"; +import { guardClassAttributeWrites } from "./class-attributes.js"; import { ClassProtocols } from "./class-protocols.js"; import { earlyReadVarSymbols } from "./var-hoisting.js"; import { recordTextCodecClass } from "../../ir/ir.js"; @@ -6772,6 +6773,7 @@ export class Lowerer { if (this.diags.length === 0) fuseArrayPipelines(this, functions); finalizeClassMethodValues(this, functions); trackClassFieldCreation(this, functions); + guardClassAttributeWrites(this, functions); // Globals typed by a class that never REGISTERED (a JS class whose // collection fenced — Symbol-keyed fields, an unsupported base): the diff --git a/packages/compiler/src/frontend/lowering/strict-mode.ts b/packages/compiler/src/frontend/lowering/strict-mode.ts new file mode 100644 index 000000000..a97e32f9e --- /dev/null +++ b/packages/compiler/src/frontend/lowering/strict-mode.ts @@ -0,0 +1,34 @@ +import * as ts from "../ts7/adapter.js"; +import { isCjsJsFile, isNodeEsmFile } from "../program.js"; + +function hasUseStrictDirective(statements: readonly ts.Statement[]): boolean { + for (const statement of statements) { + if (!ts.isExpressionStatement(statement) || !ts.isStringLiteral(statement.expression)) + return false; + if (statement.expression.text === "use strict") return true; + } + return false; +} + +/** Whether `node` runs in strict mode code: inside a class body, a function + * with a "use strict" directive, a module, or a script that starts with + * "use strict". CommonJS JavaScript is sloppy otherwise. */ +export function isStrictCode(node: ts.Node): boolean { + for (let parent: ts.Node | undefined = node.parent; parent; parent = parent.parent) { + if (ts.isClassDeclaration(parent) || ts.isClassExpression(parent)) return true; + if ( + ts.isFunctionLike(parent) && + parent.body && + ts.isBlock(parent.body) && + hasUseStrictDirective(parent.body.statements) + ) + return true; + if (ts.isSourceFile(parent)) { + return ( + hasUseStrictDirective(parent.statements) || + (!isCjsJsFile(parent) && (isNodeEsmFile(parent) || ts.isExternalModule(parent))) + ); + } + } + return false; +} diff --git a/packages/compiler/src/frontend/type-mapper.ts b/packages/compiler/src/frontend/type-mapper.ts index 41d18a6a2..617c0efee 100644 --- a/packages/compiler/src/frontend/type-mapper.ts +++ b/packages/compiler/src/frontend/type-mapper.ts @@ -6,6 +6,7 @@ import type { IrRecordShape, IrType, IrUnionDef, IrUnionDiscriminant } from "../ import { BYTES_ELEMENT_NAME, arrayOf, + isDynTypedRefType, BOOL, bytesOf, canConvertToDyn, @@ -1511,6 +1512,10 @@ function mapTypeInner(type: ts.Type, ctx: TypeMapperCtx): IrType | null { contextResolutions++; return DYN; } + // Readonly over a program class (Object.freeze's result type) is the + // same instance. + const readonlyClass = readonlyClassType(type, ctx); + if (readonlyClass !== null) return readonlyClass; if (resolveTypeParam && type.flags & ts.TypeFlags.TypeParameter) { const bound = resolveTypeParam(type); if (bound) { @@ -5111,6 +5116,19 @@ function mentionsMappedParams( return false; } +function readonlyClassType(type: ts.Type, ctx: TypeMapperCtx): IrType | null { + const alias = type.getAliasSymbol(); + if (alias?.name !== "Readonly") return null; + const args = type.getAliasTypeArguments(); + if (args.length !== 1 || !args[0]) return null; + const isLibAlias = ctx.checker + .declarationsOf(alias) + .some((d) => ts.isTypeAliasDeclaration(d) && ctx.isStdlibFile(d.getSourceFile())); + if (!isLibAlias) return null; + const mapped = mapType(args[0], ctx); + return mapped !== null && isDynTypedRefType(mapped) ? mapped : null; +} + function mapGenericUtilityAlias(widened: ts.Type, ctx: TypeMapperCtx): IrType | null { const { resolveTypeParam, shapes, unions } = ctx; if (!resolveTypeParam) return null; diff --git a/packages/compiler/src/ir/builtin-effects.ts b/packages/compiler/src/ir/builtin-effects.ts index fc4c383ff..c4fa209c8 100644 --- a/packages/compiler/src/ir/builtin-effects.ts +++ b/packages/compiler/src/ir/builtin-effects.ts @@ -577,6 +577,9 @@ export const MAY_THROW_LIB_FNS: ReadonlySet = new Set([ "dyn.mapSeedEntry", "dyn.freeze", "dyn.isFrozen", + "class.defineField", + "class.throwReadOnly", + "class.throwNotExtensible", "dyn.toString", "dyn.valueOf", "string.fromCodePoint", diff --git a/packages/compiler/src/ir/builtin-signatures.ts b/packages/compiler/src/ir/builtin-signatures.ts index 15b8d1627..cb41dec6a 100644 --- a/packages/compiler/src/ir/builtin-signatures.ts +++ b/packages/compiler/src/ir/builtin-signatures.ts @@ -202,6 +202,23 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ "dyn.in": { argTypes: [DYN, STRING], result: BOOL }, "dyn.freeze": { argTypes: [DYN], result: DYN }, "dyn.isFrozen": { argTypes: [DYN], result: BOOL }, + /** Object.defineProperty on a declared class field (scr_json.c): the + * field's current value (borrowed), its FIELD_* attribute word, the + * instance's CLASS_* integrity word, the field name and the descriptor. + * Validates as Node does (TypeError "Cannot redefine property: f") and + * answers a one-property holder of the defined value and attributes (+1), + * read back with class.definedBits and class.definedValue. */ + "class.defineField": { argTypes: [DYN, F64, F64, STRING, DYN], result: DYN }, + "class.definedBits": { argTypes: [DYN, STRING], result: F64 }, + "class.definedValue": { argTypes: [DYN, STRING], result: DYN }, + /** Whether a property descriptor has a `value` member. */ + "class.descriptorHasValue": { argTypes: [DYN], result: BOOL }, + /** Throws Node's TypeError for a write to a read-only class field: the + * field name and the instance's class name. */ + "class.throwReadOnly": { argTypes: [STRING, STRING], result: VOID }, + /** Throws Node's TypeError for a class field initializer on a + * non-extensible instance: the field name. */ + "class.throwNotExtensible": { argTypes: [STRING], result: VOID }, "dyn.nativeSetNew": { argTypes: [DYN], result: DYN }, "dyn.nativeSetIs": { argTypes: [DYN], result: BOOL }, "dyn.nativeMapIs": { argTypes: [DYN], result: BOOL }, diff --git a/packages/compiler/src/ir/ir.ts b/packages/compiler/src/ir/ir.ts index 87a4de606..aee26d5ba 100644 --- a/packages/compiler/src/ir/ir.ts +++ b/packages/compiler/src/ir/ir.ts @@ -1896,8 +1896,23 @@ export type IrStmt = | { kind: "return"; value: IrExpr | null; awaited?: true; loc: SrcLoc } /** Field write `obj.f = v` — statement-only, like `assign`/`arraySet`. * Evaluation order: obj, then value. The old value is released; ownership - * of a refcounted new value MOVES into the object. */ - | { kind: "fieldSet"; obj: IrExpr; className: string; field: string; value: IrExpr; loc: SrcLoc } + * of a refcounted new value MOVES into the object. `sloppy` marks a write + * in sloppy-mode code, which skips a read-only field instead of throwing. + * `define` marks a class field initializer, which defines the property + * (a non-extensible instance rejects it). `attributesChecked` marks a + * write that already honors the instance's property attributes + * (CLASS_ATTRIBUTES). */ + | { + kind: "fieldSet"; + obj: IrExpr; + className: string; + field: string; + value: IrExpr; + sloppy?: true; + define?: true; + attributesChecked?: true; + loc: SrcLoc; + } /** Record field write `r.f = v` — mirrors `fieldSet` exactly (evaluation * order obj then value; old value released; refcounted new value moved * in), with a shape id in place of a class name. */ @@ -4048,6 +4063,27 @@ export function isClassOwnEnumerableFieldName(name: string): boolean { /** Compiler-owned storage for properties added through untyped references. */ export const DYN_CLASS_PROPERTIES = "%dynProperties"; +/** Compiler-owned integrity state of a class instance whose hierarchy is + * passed to Object.freeze, Object.seal, Object.preventExtensions or + * Object.defineProperty: an f64 holding CLASS_NOT_EXTENSIBLE, CLASS_SEALED + * and CLASS_FROZEN bits. Other hierarchies have no such field. */ +export const CLASS_ATTRIBUTES = "%attributes"; +export const CLASS_NOT_EXTENSIBLE = 1; +export const CLASS_SEALED = 2; +export const CLASS_FROZEN = 4; + +/** Compiler-owned attributes of one declared field that Object.defineProperty + * changes: an f64 holding FIELD_NOT_WRITABLE, FIELD_NOT_ENUMERABLE and + * FIELD_NOT_CONFIGURABLE bits (zero is an ordinary data property). */ +export const CLASS_FIELD_ATTRIBUTES_PREFIX = "%attr:"; +export const FIELD_NOT_WRITABLE = 1; +export const FIELD_NOT_ENUMERABLE = 2; +export const FIELD_NOT_CONFIGURABLE = 4; + +export function classFieldAttributesName(field: string): string { + return CLASS_FIELD_ATTRIBUTES_PREFIX + field; +} + /** A class capsule can always preserve its exact native identity. Its * optional property view additionally needs converters in both directions; * fields such as Maps may remain opaque without preventing the round trip. */ diff --git a/packages/compiler/test/class-attributes.test.ts b/packages/compiler/test/class-attributes.test.ts new file mode 100644 index 000000000..1a6fea654 --- /dev/null +++ b/packages/compiler/test/class-attributes.test.ts @@ -0,0 +1,84 @@ +import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { expect, test } from "vitest"; +import { compile, deserializeModule } from "../src/index.js"; +import type { IrModule } from "../src/ir/ir.js"; + +async function lowerModule(source: string, file = "main.ts"): Promise { + const dir = await mkdtemp(join(tmpdir(), "scriptc-class-attributes-")); + try { + const entry = join(dir, file); + const outPath = join(dir, "main.ir.json"); + await writeFile(entry, source); + const result = await compile(entry, { outDir: dir, outPath, outputKind: "ir" }); + if (!result.ok) + throw new Error(result.diagnostics.map((d) => `${d.code}: ${d.message}`).join("\n")); + return deserializeModule(await readFile(outPath, "utf8")); + } finally { + await rm(dir, { recursive: true, force: true }); + } +} + +function fieldsOf(mod: IrModule, className: string): string[] { + const cls = (mod.classes ?? []).find( + (c) => c.name === className || c.name.endsWith(`.${className}`), + ); + if (!cls) throw new Error(`no class ${className}`); + return cls.fields.map((f) => f.name); +} + +function functionsCalling(mod: IrModule, fn: string): string[] { + return mod.functions + .filter((f) => JSON.stringify(f.body).includes(`"fn":"${fn}"`)) + .map((f) => f.name); +} + +test("only hierarchies passed to an integrity call get attribute state and write checks", async () => { + const mod = await lowerModule(` +export {}; +class Point { x = 1; } +class Other { x = 1; } +function movePoint(p: Point): void { p.x = 2; } +function moveOther(o: Other): void { o.x = 2; } +const p = new Point(); +Object.freeze(p); +movePoint(p); +moveOther(new Other()); +`); + expect(fieldsOf(mod, "Point")).toContain("%attributes"); + expect(fieldsOf(mod, "Other")).not.toContain("%attributes"); + const checked = functionsCalling(mod, "class.throwReadOnly"); + expect(checked).toContain("movePoint"); + expect(checked).not.toContain("moveOther"); +}); + +test("a redefined field gets its own attribute word", async () => { + const mod = await lowerModule(` +export {}; +class Config { mode = "dev"; level = 1; } +const c = new Config(); +Object.defineProperty(c, "mode", { writable: false }); +`); + const fields = fieldsOf(mod, "Config"); + expect(fields).toContain("%attributes"); + expect(fields).toContain("%attr:mode"); + expect(fields).not.toContain("%attr:level"); +}); + +test("sloppy-mode writes skip a read-only field without throwing", async () => { + const mod = await lowerModule( + ` +class Box { constructor() { this.x = 1; } } +const b = new Box(); +Object.freeze(b); +b.x = 2; +`, + "main.cjs", + ); + expect(fieldsOf(mod, "Box")).toContain("%attributes"); + // Class bodies are strict mode code; the top-level write is not. + const throwing = functionsCalling(mod, "class.throwReadOnly"); + expect(throwing.length).toBeGreaterThan(0); + expect(throwing.every((name) => name.includes("constructor"))).toBe(true); +}); diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6004-class-instance-integrity.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6004-class-instance-integrity.ts.json new file mode 100644 index 000000000..4d21236c0 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6004-class-instance-integrity.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6004-class-instance-integrity.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6005-class-instance-integrity-sloppy.cjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6005-class-instance-integrity-sloppy.cjs.json new file mode 100644 index 000000000..1101d2180 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6005-class-instance-integrity-sloppy.cjs.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6005-class-instance-integrity-sloppy.cjs" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_dyn.h b/packages/runtime/src/scr_dyn.h index 1ab857e57..92a5b648a 100644 --- a/packages/runtime/src/scr_dyn.h +++ b/packages/runtime/src/scr_dyn.h @@ -882,7 +882,17 @@ double scr_dyn_arr_len(const ScrDyn *d); ScrDyn *scr_dyn_arr_at(const ScrDyn *d, double i); ScrDyn *scr_dyn_arr_named_get(const ScrDyn *d, const ScrStr *key); void scr_dyn_obj_set(ScrDyn *obj, const char *key, size_t key_len, ScrDyn *value); +void scr_dyn_obj_set_field(ScrDyn *obj, const char *key, size_t key_len, ScrDyn *value); ScrDyn *scr_dyn_define_property(ScrDyn *target, ScrDyn *key, ScrDyn *descriptor); +void scr_dyn_class_field_attributes(ScrDyn *view, ScrStr *name, double bits); +void scr_dyn_class_view_attributes(ScrDyn *view, double bits); +ScrDyn *scr_class_define_field(ScrDyn *current, double field_bits, double object_bits, ScrStr *name, + ScrDyn *descriptor); +double scr_class_defined_bits(ScrDyn *holder, ScrStr *name); +ScrDyn *scr_class_defined_value(ScrDyn *holder, ScrStr *name); +bool scr_class_descriptor_has_value(ScrDyn *descriptor); +void scr_class_throw_read_only(ScrStr *field, ScrStr *owner); +void scr_class_throw_not_extensible(ScrStr *field); ScrDyn *scr_dyn_get_own_property_descriptor(ScrDyn *target, ScrDyn *key); ScrDyn *scr_dyn_array_proto_call(ScrDyn *target, ScrStr *method, ScrDyn *args); ScrDyn *scr_dyn_array_prototype(void); /* +1, installs native method adapters */ diff --git a/packages/runtime/src/scr_json.c b/packages/runtime/src/scr_json.c index 7accb818b..5c8d8858c 100644 --- a/packages/runtime/src/scr_json.c +++ b/packages/runtime/src/scr_json.c @@ -1585,8 +1585,12 @@ static void scr_dyn_obj_put(ScrDyn *obj, const char *key, size_t key_len, ScrDyn } if (obj->non_extensible) { scr_dyn_release(value); - static const char message[] = "Cannot add property, object is not extensible"; - scr_throw_error_msg(SCR_ERR_TYPE, message, sizeof message - 1); + ScrJsonBuf buffer; + scr_jb_init(&buffer); + scr_jb_puts(&buffer, "Cannot add property "); + scr_jb_write(&buffer, key, key_len); + scr_jb_puts(&buffer, ", object is not extensible"); + scr_throw_error(SCR_ERR_TYPE, scr_jb_finish(&buffer)); return; } if (obj->v.obj.len == obj->v.obj.cap) { @@ -1922,9 +1926,40 @@ ScrDyn *scr_dyn_set_prototype(ScrDyn *object, ScrDyn *prototype) { return scr_dyn_retain(object); } +/* A class instance's integrity state lives in its native layout, which only + * statically typed calls can reach (the compiler adds the state to the + * hierarchies they name). Through an untyped value the operation throws. */ +static bool scr_dyn_class_integrity_fence(const ScrDyn *object, const char *operation) { + if (object->kind != SCR_DYN_TYPED_REF) return false; + ScrJsonBuf buffer; + scr_jb_init(&buffer); + scr_jb_puts(&buffer, operation); + scr_jb_puts(&buffer, " of a class instance held in an untyped value is not supported yet"); + ScrStr *message = scr_jb_finish(&buffer); + scr_throw_error_msg_code(SCR_ERR_ERROR, message->data, message->len, "SC2020"); + scr_str_release(message); + return true; +} + +/* Integrity queries on a class instance answer from its checked view, which + * carries the instance's attributes (scr_dyn_class_view_attributes). */ +static bool scr_dyn_typed_ref_view_answer(ScrDyn *object, bool (*query)(const ScrDyn *), + bool *answer) { + if (object->kind != SCR_DYN_TYPED_REF) return false; + ScrDyn *view = scr_dyn_typed_ref_materialize(object); + *answer = scr_exc_pending() ? false : query(view); + scr_dyn_release(view); + return true; +} + +static bool scr_dyn_is_extensible_view(const ScrDyn *view) { + return (view->kind == SCR_DYN_OBJ || view->kind == SCR_DYN_ARR) && !view->non_extensible; +} + ScrDyn *scr_dyn_prevent_extensions(ScrDyn *object) { if (scr_dyn_class_reflection_fence(object)) return NULL; if (object->kind == SCR_DYN_PROXY) { scr_dyn_proxy_unsupported("Object.preventExtensions"); return NULL; } + if (scr_dyn_class_integrity_fence(object, "Object.preventExtensions")) return NULL; if (object->kind == SCR_DYN_OBJ || object->kind == SCR_DYN_ARR) { object->non_extensible = true; if (object->kind == SCR_DYN_ARR && object->v.arr.properties) @@ -1936,12 +1971,15 @@ ScrDyn *scr_dyn_prevent_extensions(ScrDyn *object) { bool scr_dyn_is_extensible(ScrDyn *object) { if (scr_dyn_class_reflection_fence(object)) return false; if (object->kind == SCR_DYN_PROXY) { scr_dyn_proxy_unsupported("Object.isExtensible"); return false; } - return (object->kind == SCR_DYN_OBJ || object->kind == SCR_DYN_ARR) && !object->non_extensible; + bool answer; + if (scr_dyn_typed_ref_view_answer(object, scr_dyn_is_extensible_view, &answer)) return answer; + return scr_dyn_is_extensible_view(object); } ScrDyn *scr_dyn_seal(ScrDyn *object) { if (scr_dyn_class_reflection_fence(object)) return NULL; if (object->kind == SCR_DYN_PROXY) { scr_dyn_proxy_unsupported("Object.seal"); return NULL; } + if (scr_dyn_class_integrity_fence(object, "Object.seal")) return NULL; if (object->symbol_properties) scr_dyn_release(scr_dyn_seal(object->symbol_properties)); if (object->kind == SCR_DYN_OBJ) { object->non_extensible = true; @@ -1958,6 +1996,8 @@ ScrDyn *scr_dyn_seal(ScrDyn *object) { bool scr_dyn_is_sealed(const ScrDyn *object) { if (scr_dyn_class_reflection_fence(object)) return false; if (object->kind == SCR_DYN_PROXY) { scr_dyn_proxy_unsupported("Object.isSealed"); return false; } + bool answer; + if (scr_dyn_typed_ref_view_answer((ScrDyn *)object, scr_dyn_is_sealed, &answer)) return answer; if (object->symbol_properties && !scr_dyn_is_sealed(object->symbol_properties)) return false; if (object->kind == SCR_DYN_ARR) return object->non_extensible && (object->v.arr.len == 0 || object->v.arr.sealed) && @@ -4243,6 +4283,25 @@ void scr_dyn_obj_set(ScrDyn *obj, const char *key, size_t key_len, ScrDyn *value scr_dyn_obj_put(obj, key, key_len, value); } +/* A class instance's view takes its native field values whatever the + * attributes copied into it (a frozen instance's view is built the same way + * as any other). Consumes value. */ +void scr_dyn_obj_set_field(ScrDyn *obj, const char *key, size_t key_len, ScrDyn *value) { + ScrDynEntry *existing = obj->v.obj.index ? scr_dyn_find_entry(obj, key, key_len) + : scr_dyn_find_linear_entry(obj, key, key_len); + if (existing && !existing->accessor) { + scr_dyn_region_store(obj, value); + ScrDyn *old = existing->value; + existing->value = value; + scr_dyn_release(old); + return; + } + bool non_extensible = obj->non_extensible; + obj->non_extensible = false; + scr_dyn_obj_put(obj, key, key_len, value); + obj->non_extensible = non_extensible; +} + static ScrStr *scr_dyn_property_key(const ScrDyn *key) { return scr_dyn_string_coerce_js(key); } @@ -6036,9 +6095,12 @@ ScrDyn *scr_dyn_define_property(ScrDyn *target, ScrDyn *key, ScrDyn *descriptor) (!accessor && ((!prior->writable && writable) || (!prior->writable && value && !scr_dyn_property_same_value(prior->value, value)))))) { scr_dyn_descriptor_fields_drop(fields); + ScrJsonBuf buffer; + scr_jb_init(&buffer); + scr_jb_puts(&buffer, "Cannot redefine property: "); + scr_jb_write(&buffer, name->data, name->len); scr_str_release(name); - static const char msg[] = "Cannot redefine property"; - scr_throw_error_msg(SCR_ERR_TYPE, msg, sizeof msg - 1); + scr_throw_error(SCR_ERR_TYPE, scr_jb_finish(&buffer)); return NULL; } if (prior) { @@ -13864,6 +13926,7 @@ ScrStr *scr_dyn_to_string_method(const ScrDyn *d, const ScrStr *enc, const ScrSt ScrDyn *scr_dyn_freeze(ScrDyn *value) { if (scr_dyn_class_reflection_fence(value)) return NULL; + if (scr_dyn_class_integrity_fence(value, "Object.freeze")) return NULL; if (value->symbol_properties) scr_dyn_release(scr_dyn_freeze(value->symbol_properties)); if (value->kind == SCR_DYN_ARR) { value->non_extensible = true; @@ -13891,6 +13954,8 @@ ScrDyn *scr_dyn_freeze(ScrDyn *value) { bool scr_dyn_is_frozen(const ScrDyn *value) { if (scr_dyn_class_reflection_fence(value)) return false; + bool answer; + if (scr_dyn_typed_ref_view_answer((ScrDyn *)value, scr_dyn_is_frozen, &answer)) return answer; if (value->symbol_properties && !scr_dyn_is_frozen(value->symbol_properties)) return false; if (value->kind == SCR_DYN_ARR) return scr_dyn_is_sealed(value) && (value->v.arr.len == 0 || value->v.arr.frozen) && @@ -13944,3 +14009,127 @@ ScrDyn *scr_caught_to_dyn(const ScrCaught *c) { return scr_dyn_new_obj(); } } + +/* ── class instance attributes ─────────────────────────────────────────── + * A hierarchy passed to Object.freeze, Object.seal, Object.preventExtensions + * or Object.defineProperty keeps an integrity state per instance and an + * attribute word per redefined field (CLASS_* and FIELD_* bits in the + * compiler's ir.ts). The checked view of an instance carries them, so + * reflection over the view answers as Node does. */ +enum { + SCR_CLASS_NOT_EXTENSIBLE = 1, + SCR_CLASS_SEALED = 2, + SCR_CLASS_FROZEN = 4, + SCR_FIELD_NOT_WRITABLE = 1, + SCR_FIELD_NOT_ENUMERABLE = 2, + SCR_FIELD_NOT_CONFIGURABLE = 4, +}; + +void scr_dyn_class_field_attributes(ScrDyn *view, ScrStr *name, double bits) { + unsigned b = (unsigned)bits; + if (b == 0 || view->kind != SCR_DYN_OBJ) return; + ScrDynEntry *entry = scr_dyn_entry(view, name); + if (!entry) return; + if (b & SCR_FIELD_NOT_WRITABLE) entry->writable = false; + if (b & SCR_FIELD_NOT_ENUMERABLE) entry->enumerable = false; + if (b & SCR_FIELD_NOT_CONFIGURABLE) entry->configurable = false; +} + +void scr_dyn_class_view_attributes(ScrDyn *view, double bits) { + unsigned b = (unsigned)bits; + if (b == 0 || view->kind != SCR_DYN_OBJ) return; + view->non_extensible = true; + for (size_t i = 0; i < view->v.obj.len; i++) { + ScrDynEntry *entry = &view->v.obj.entries[i]; + if (b & (SCR_CLASS_SEALED | SCR_CLASS_FROZEN)) entry->configurable = false; + if ((b & SCR_CLASS_FROZEN) && !entry->accessor) entry->writable = false; + } +} + +/* Object.defineProperty on a declared class field. The descriptor applies to + * a one-property object holding the field's current value and attributes, + * so validation and errors match ordinary objects. Answers that object (+1), + * or NULL with a pending exception. */ +ScrDyn *scr_class_define_field(ScrDyn *current, double field_bits, double object_bits, ScrStr *name, + ScrDyn *descriptor) { + unsigned f = (unsigned)field_bits, o = (unsigned)object_bits; + ScrDyn *holder = scr_dyn_new_obj(); + scr_dyn_obj_set(holder, name->data, name->len, scr_dyn_retain(current)); + ScrDynEntry *entry = scr_dyn_entry(holder, name); + entry->writable = !(f & SCR_FIELD_NOT_WRITABLE) && !(o & SCR_CLASS_FROZEN); + entry->enumerable = !(f & SCR_FIELD_NOT_ENUMERABLE); + entry->configurable = + !(f & SCR_FIELD_NOT_CONFIGURABLE) && !(o & (SCR_CLASS_SEALED | SCR_CLASS_FROZEN)); + ScrDyn *key = scr_dyn_new_str(name); + ScrDyn *defined = scr_dyn_define_property(holder, key, descriptor); + scr_dyn_release(key); + if (!defined) { + scr_dyn_release(holder); + return NULL; + } + scr_dyn_release(defined); + entry = scr_dyn_entry(holder, name); + if (entry->accessor) { + scr_dyn_release(holder); + ScrJsonBuf buffer; + scr_jb_init(&buffer); + scr_jb_puts(&buffer, "Object.defineProperty with an accessor descriptor for the declared field '"); + scr_jb_write(&buffer, name->data, name->len); + scr_jb_puts(&buffer, "' is not supported yet"); + ScrStr *message = scr_jb_finish(&buffer); + scr_throw_error_msg_code(SCR_ERR_ERROR, message->data, message->len, "SC2020"); + scr_str_release(message); + return NULL; + } + return holder; +} + +/* The field attribute word of a scr_class_define_field result. */ +double scr_class_defined_bits(ScrDyn *holder, ScrStr *name) { + const ScrDynEntry *entry = scr_dyn_entry(holder, name); + unsigned bits = 0; + if (!entry->writable) bits |= SCR_FIELD_NOT_WRITABLE; + if (!entry->enumerable) bits |= SCR_FIELD_NOT_ENUMERABLE; + if (!entry->configurable) bits |= SCR_FIELD_NOT_CONFIGURABLE; + return (double)bits; +} + +/* The value a scr_class_define_field result holds (+1). */ +ScrDyn *scr_class_defined_value(ScrDyn *holder, ScrStr *name) { + return scr_dyn_retain(scr_dyn_entry(holder, name)->value); +} + +/* Whether a property descriptor names a value (Node's ToPropertyDescriptor + * reads `value` with [[HasProperty]]). */ +bool scr_class_descriptor_has_value(ScrDyn *descriptor) { + if (descriptor->kind == SCR_DYN_TYPED_REF) { + ScrDyn *view = scr_dyn_typed_ref_materialize(descriptor); + bool has = !scr_exc_pending() && scr_class_descriptor_has_value(view); + scr_dyn_release(view); + return has; + } + return descriptor->kind == SCR_DYN_OBJ && scr_dyn_obj_get(descriptor, "value", 5) != NULL; +} + +/* Node's TypeError for a write to a read-only field of a class instance. */ +void scr_class_throw_read_only(ScrStr *field, ScrStr *owner) { + ScrJsonBuf buffer; + scr_jb_init(&buffer); + scr_jb_puts(&buffer, "Cannot assign to read only property '"); + scr_jb_write(&buffer, field->data, field->len); + scr_jb_puts(&buffer, "' of object '#<"); + scr_jb_write(&buffer, owner->data, owner->len); + scr_jb_puts(&buffer, ">'"); + scr_throw_error(SCR_ERR_TYPE, scr_jb_finish(&buffer)); +} + +/* Node's TypeError for a class field initializer on a non-extensible + * instance (a base constructor froze or sealed `this`). */ +void scr_class_throw_not_extensible(ScrStr *field) { + ScrJsonBuf buffer; + scr_jb_init(&buffer); + scr_jb_puts(&buffer, "Cannot define property "); + scr_jb_write(&buffer, field->data, field->len); + scr_jb_puts(&buffer, ", object is not extensible"); + scr_throw_error(SCR_ERR_TYPE, scr_jb_finish(&buffer)); +} diff --git a/tests/corpus/6004-class-instance-integrity.ts b/tests/corpus/6004-class-instance-integrity.ts new file mode 100644 index 000000000..7df516802 --- /dev/null +++ b/tests/corpus/6004-class-instance-integrity.ts @@ -0,0 +1,169 @@ +export {}; +import { inspect } from "node:util"; + +const message = (run: () => void): string => { + try { + run(); + return "no error"; + } catch (e) { + return `${e instanceof TypeError ? "TypeError" : "Error"}: ${(e as Error).message}`; + } +}; + +class Point { + x = 1; + y = 2; + label = "p"; + move(dx: number): void { + this.x += dx; + } +} + +const p = new Point(); +console.log(Object.isFrozen(p), Object.isSealed(p), Object.isExtensible(p)); +const same = Object.freeze(p); +console.log(same === p, Object.isFrozen(p), Object.isSealed(p), Object.isExtensible(p)); +console.log(message(() => { + p.x = 5; +})); +console.log(message(() => p.move(1))); +console.log(p.x, Object.keys(p)); +const q = new Point(); +q.x = 9; +console.log(q.x, Object.isFrozen(q)); + +class Point3 extends Point { + z = 3; +} +const p3 = new Point3(); +Object.freeze(p3); +console.log(message(() => { + p3.z = 1; +})); +const asBase: Point = new Point3(); +Object.freeze(asBase); +console.log(message(() => { + asBase.y = 0; +})); +console.log(asBase.y); + +class Counter { + n = 0; +} +const c = Object.seal(new Counter()); +c.n = 5; +console.log(c.n, Object.isSealed(c), Object.isFrozen(c), Object.isExtensible(c)); +const d = new Counter(); +Object.preventExtensions(d); +d.n = 2; +console.log(d.n, Object.isExtensible(d), Object.isSealed(d), Object.isFrozen(d)); +const loose: any = d; +console.log(message(() => { + loose.extra = 1; +})); + +class Config { + mode = "dev"; + level = 1; +} +const cfg = new Config(); +Object.defineProperty(cfg, "mode", { value: "prod", writable: false }); +console.log(cfg.mode, Object.keys(cfg), Object.values(cfg)); +console.log(message(() => { + cfg.mode = "x"; +})); +Object.defineProperty(cfg, "level", { enumerable: false }); +console.log(Object.keys(cfg), Object.entries(cfg), cfg.level); +Object.defineProperty(cfg, "level", { configurable: false }); +console.log(message(() => Object.defineProperty(cfg, "level", { enumerable: true }))); +console.log(JSON.stringify(Object.getOwnPropertyDescriptor(cfg, "mode"))); +console.log(JSON.stringify(Object.getOwnPropertyDescriptor(cfg, "level"))); +cfg.level = 7; +console.log(cfg.level); + +const fz = Object.freeze(new Config()); +console.log(message(() => Object.defineProperty(fz, "level", { value: 2 }))); +console.log(message(() => Object.defineProperty(fz, "level", { value: 1 }))); +console.log(fz.level, JSON.stringify(Object.getOwnPropertyDescriptor(fz, "level"))); + +class Frozen { + a = 1; + constructor() { + Object.freeze(this); + } +} +const frozenInCtor = new Frozen(); +console.log(frozenInCtor.a, Object.isFrozen(frozenInCtor)); +console.log(message(() => { + frozenInCtor.a = 2; +})); + +class Extended extends Frozen { + b = 2; +} +console.log(message(() => new Extended())); + +class Empty {} +const empty = new Empty(); +console.log(Object.isExtensible(empty), Object.isSealed(empty), Object.isFrozen(empty)); +Object.preventExtensions(empty); +console.log(Object.isExtensible(empty), Object.isSealed(empty), Object.isFrozen(empty)); + +class Totals { + count = 1; + items: string[] = []; +} +const t = new Totals(); +Object.freeze(t); +console.log(message(() => { + t.count += 1; +})); +console.log(message(() => { + t.count++; +})); +console.log(message(() => { + ({ value: t.count } = { value: 5 }); +})); +t.items.push("still mutable"); +console.log(t.count, t.items, Object.isFrozen(t.items)); +console.log(JSON.stringify(Object.getOwnPropertyDescriptor(t, "count"))); + +class Holder { + value: T; + constructor(value: T) { + this.value = value; + } +} +const h = Object.freeze(new Holder("text")); +console.log(h.value, Object.isFrozen(h)); +const h2 = new Holder(4); +Object.seal(h2); +h2.value = 5; +console.log(h2.value, Object.isSealed(h2)); + +class Settings { + mode = "dev"; + level = 1; +} +const settings = new Settings(); +console.log(settings); +Object.defineProperty(settings, "level", { enumerable: false }); +console.log(settings, inspect(settings)); +console.log([settings], { settings }); + +class Pair { + x = 1; + y = 2; +} +const pair = new Pair(); +Object.freeze(pair); +console.log(pair, Object.isFrozen(pair)); +const sealed = new Pair(); +(sealed as any).extra = "added"; +Object.seal(sealed); +console.log(sealed, Object.isSealed(sealed)); + +// JSON.stringify reads the same view: non-enumerable fields are skipped. +console.log(JSON.stringify(cfg), JSON.stringify(settings)); +console.log(JSON.stringify({ settings, pair, sealed })); +console.log(JSON.stringify([fz, t])); diff --git a/tests/corpus/6005-class-instance-integrity-sloppy.cjs b/tests/corpus/6005-class-instance-integrity-sloppy.cjs new file mode 100644 index 000000000..92c959d34 --- /dev/null +++ b/tests/corpus/6005-class-instance-integrity-sloppy.cjs @@ -0,0 +1,40 @@ +class Box { + constructor() { + this.x = 1; + } + bump() { + this.x = 2; + } +} + +const b = new Box(); +Object.freeze(b); +b.x = 5; +console.log(b.x, Object.isFrozen(b)); + +function strictWrite() { + "use strict"; + try { + b.x = 6; + } catch (e) { + console.log(e instanceof TypeError, e.message); + } +} +strictWrite(); + +try { + b.bump(); +} catch (e) { + console.log("class body:", e.message); +} +console.log(b.x); + +const s = new Box(); +Object.seal(s); +s.x = 3; +console.log(s.x, Object.isSealed(s), Object.isFrozen(s)); + +const plain = new Box(); +Object.defineProperty(plain, "x", { writable: false }); +plain.x = 9; +console.log(plain.x, JSON.stringify(Object.getOwnPropertyDescriptor(plain, "x"))); diff --git a/tests/harness/corpus-costs.json b/tests/harness/corpus-costs.json index 733f4b967..1d4f04d42 100644 --- a/tests/harness/corpus-costs.json +++ b/tests/harness/corpus-costs.json @@ -2226,6 +2226,8 @@ "6001-json-stringify-js-class-instances.js": 4668, "6002-inspect-class-added-properties.ts": 6044, "6003-inspect-js-class-properties.js": 2752, + "6004-class-instance-integrity.ts": 10548, + "6005-class-instance-integrity-sloppy.cjs": 7466, "601-closures-loops.ts": 1788, "6010-match-metadata.ts": 3990, "6011-match-metadata-js.js": 3660, @@ -5311,6 +5313,8 @@ "6001-json-stringify-js-class-instances.js": 7469, "6002-inspect-class-added-properties.ts": 9670, "6003-inspect-js-class-properties.js": 4403, + "6004-class-instance-integrity.ts": 16877, + "6005-class-instance-integrity-sloppy.cjs": 11946, "601-closures-loops.ts": 2297, "6010-match-metadata.ts": 5450, "6011-match-metadata-js.js": 4890, From 48259bd7cd9d79b71bad2e62c4d7a7b0ef51f125 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Mon, 19 Oct 2026 05:41:16 -0500 Subject: [PATCH 12/83] Refuse literal accessor descriptors for declared class fields at compile time Object.defineProperty with an object-literal descriptor that declares get or set for a declared class field is refused at compile time with SC2020, naming the field. JavaScript sources report it as a deferred fence, so coverage lists the call. Other accessor descriptors still throw SC2020 when the call runs. The native descriptor test now compiles a data descriptor change on a declared field and compares the output with Node, and checks that an accessor descriptor stays fenced. --- .../builtin-apis/object-freezing.mdx | 2 +- .../src/frontend/lowering/class-attributes.ts | 28 +++++++++++++- .../test/native-class-descriptors.test.ts | 38 ++++++++++++++++++- .../class-field-accessor-descriptor.ts.json | 6 +++ .../class-field-accessor-descriptor.ts | 14 +++++++ .../class-field-accessor-descriptor.ts.txt | 6 +++ 6 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/class-field-accessor-descriptor.ts.json create mode 100644 tests/diagnostics/class-field-accessor-descriptor.ts create mode 100644 tests/harness/__snapshots__/class-field-accessor-descriptor.ts.txt diff --git a/docs/content/limitations/builtin-apis/object-freezing.mdx b/docs/content/limitations/builtin-apis/object-freezing.mdx index 473f21b0e..00070e773 100644 --- a/docs/content/limitations/builtin-apis/object-freezing.mdx +++ b/docs/content/limitations/builtin-apis/object-freezing.mdx @@ -1 +1 @@ -- **Object freezing:** `Object.freeze` and `Object.isFrozen` support plain checked dictionaries and primitives. Freezing is shallow and preserves accessors. Aliased typed records, arrays, proxies, and other native objects cannot be frozen. Class instances support `Object.freeze`, `Object.seal`, `Object.preventExtensions`, `Object.isFrozen`, `Object.isSealed`, and `Object.isExtensible` when the static type of the argument names the class. A write to a read-only field throws Node's `TypeError` in strict mode code and leaves the field unchanged in sloppy mode code. `Object.defineProperty` with a string-literal key changes the value, `writable`, `enumerable`, and `configurable` attributes of a declared field; an accessor descriptor for a declared field throws `SC2020`. Freezing, sealing, or preventing extensions of a class instance held in an untyped value throws `SC2020`. A JavaScript class field that is first created after its instance became non-extensible is added instead of throwing. +- **Object freezing:** `Object.freeze` and `Object.isFrozen` support plain checked dictionaries and primitives. Freezing is shallow and preserves accessors. Aliased typed records, arrays, proxies, and other native objects cannot be frozen. Class instances support `Object.freeze`, `Object.seal`, `Object.preventExtensions`, `Object.isFrozen`, `Object.isSealed`, and `Object.isExtensible` when the static type of the argument names the class. A write to a read-only field throws Node's `TypeError` in strict mode code and leaves the field unchanged in sloppy mode code. `Object.defineProperty` with a string-literal key changes the value, `writable`, `enumerable`, and `configurable` attributes of a declared field; an object-literal accessor descriptor (`get` or `set`) for a declared field is refused at compile time with `SC2020`, and another accessor descriptor for a declared field throws `SC2020` when the call runs. Freezing, sealing, or preventing extensions of a class instance held in an untyped value throws `SC2020`. A JavaScript class field that is first created after its instance became non-extensible is added instead of throwing. diff --git a/packages/compiler/src/frontend/lowering/class-attributes.ts b/packages/compiler/src/frontend/lowering/class-attributes.ts index e3f4250a0..ae4dd4900 100644 --- a/packages/compiler/src/frontend/lowering/class-attributes.ts +++ b/packages/compiler/src/frontend/lowering/class-attributes.ts @@ -24,7 +24,7 @@ import { import { numLit, strLit, varRef } from "../../ir/build.js"; import { transformStmtList } from "../../ir/traverse.js"; import { locOf } from "../program.js"; -import type { Lowerer } from "./lowerer.js"; +import { type Lowerer, PoisonError } from "./lowerer.js"; import type { ClassInfo } from "./lower-classes.js"; import { classPropertiesHelper } from "./class-dynamic-dispatch.js"; import { runtimeRooted } from "./class-descriptors.js"; @@ -132,10 +132,26 @@ export function lowerClassIntegrity( }; } +/** An object literal with a get or set property. */ +function literalAccessorDescriptor(node: ts.Expression): boolean { + while (ts.isParenthesizedExpression(node)) node = node.expression; + return ( + ts.isObjectLiteralExpression(node) && + node.properties.some( + (property) => + property.name !== undefined && + (ts.isIdentifier(property.name) || ts.isStringLiteral(property.name)) && + (property.name.text === "get" || property.name.text === "set"), + ) + ); +} + /** Object.defineProperty with a string-literal key naming a declared field * of a class instance: a data descriptor changes the field's value and * attributes, validated as Node validates ordinary objects. Answers the - * instance, or null when the call is not of that form. */ + * instance, or null when the call is not of that form. An object-literal + * accessor descriptor is refused at compile time; other accessor + * descriptors throw SC2020 when the call runs. */ export function lowerClassFieldDefine( lowerer: Lowerer, call: ts.CallExpression, @@ -152,6 +168,14 @@ export function lowerClassFieldDefine( [...(info.symbolFields?.values() ?? [])].includes(field) ) return null; + if (literalAccessorDescriptor(call.arguments[2]!)) { + lowerer.pushDiag({ + code: "SC2020", + message: `Object.defineProperty with an accessor descriptor for the declared field '${field}' is not supported yet`, + loc: locOf(call), + }); + throw new PoisonError(); + } const getRecord = (id: string) => lowerer.shapes.get(id); const getUnion = (id: string) => lowerer.unions.get(id); if ( diff --git a/packages/compiler/test/native-class-descriptors.test.ts b/packages/compiler/test/native-class-descriptors.test.ts index d4aff3949..bd0a06dac 100644 --- a/packages/compiler/test/native-class-descriptors.test.ts +++ b/packages/compiler/test/native-class-descriptors.test.ts @@ -12,7 +12,7 @@ function fixture(source: string) { } test.each([ - "Object.defineProperty(value, 'x', {value: 2, writable: false});", + "Object.defineProperty(value, 'x', {get() { return 2; }});", "Object.defineProperties(value, {x: {value: 2}});", ])("keeps unsafe native descriptor changes fenced: %s", (operation) => { const { dir, entry } = fixture( @@ -30,6 +30,42 @@ test.each([ } }); +test("data descriptor changes on native fields match Node", async () => { + const { dir, entry } = fixture(` +class Value { constructor() { this.x = 1; } } +const value = new Value(); +Object.defineProperty(value, 'x', { value: 2, writable: false }); +console.log(value.x, JSON.stringify(Object.getOwnPropertyDescriptor(value, 'x'))); +value.x = 3; +console.log(value.x, Object.keys(value)); +`); + try { + const result = await compile(entry, { + outDir: dir, + outPath: join(dir, "program"), + sanitize: process.env["SCRIPTC_SAN"] === "1", + }); + if (!result.ok) throw new Error(JSON.stringify(result.diagnostics)); + const run = spawnSync(result.binaryPath, { encoding: "utf8", timeout: 30_000 }); + const oracle = spawnSync(process.execPath, [entry], { encoding: "utf8", timeout: 30_000 }); + expect(run.error).toBeUndefined(); + expect(oracle.status).toBe(0); + expect({ + status: run.status, + signal: run.signal, + stdout: run.stdout, + stderr: run.stderr, + }).toEqual({ + status: oracle.status, + signal: oracle.signal, + stdout: oracle.stdout, + stderr: oracle.stderr, + }); + } finally { + rmSync(dir, { recursive: true, force: true }); + } +}); + test("JS overrides with different return types match Node", async () => { const { dir, entry } = fixture(` class Base { copy(value = 1) { return value; } } diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/class-field-accessor-descriptor.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/class-field-accessor-descriptor.ts.json new file mode 100644 index 000000000..3d6639169 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/class-field-accessor-descriptor.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/diagnostics/class-field-accessor-descriptor.ts" + ], + "diags": [] +} diff --git a/tests/diagnostics/class-field-accessor-descriptor.ts b/tests/diagnostics/class-field-accessor-descriptor.ts new file mode 100644 index 000000000..66078f6c2 --- /dev/null +++ b/tests/diagnostics/class-field-accessor-descriptor.ts @@ -0,0 +1,14 @@ +// Object.defineProperty can change a declared class field's value and +// attributes, but not turn it into an accessor: a literal get or set +// descriptor is refused at compile time, naming the field. +class Value { + x = 1; +} + +const value = new Value(); +Object.defineProperty(value, "x", { + get() { + return 2; + }, +}); +console.log(value.x); diff --git a/tests/harness/__snapshots__/class-field-accessor-descriptor.ts.txt b/tests/harness/__snapshots__/class-field-accessor-descriptor.ts.txt new file mode 100644 index 000000000..986e0d023 --- /dev/null +++ b/tests/harness/__snapshots__/class-field-accessor-descriptor.ts.txt @@ -0,0 +1,6 @@ +class-field-accessor-descriptor.ts:9:1 - error SC2020: Object.defineProperty with an accessor descriptor for the declared field 'x' is not supported yet + + 8 | const value = new Value(); + 9 | Object.defineProperty(value, "x", { + | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 10 | get() { \ No newline at end of file From 809f2cbea1acd7ac3fa9ae9eb00ae668435f6d1f Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Mon, 19 Oct 2026 07:55:06 -0500 Subject: [PATCH 13/83] Merge main --- docs/content/docs/limitations.mdx | 2 + .../builtin-apis/asynclocalstorage.mdx | 1 + .../builtin-apis/iterator-helpers.mdx | 2 +- .../limitations/builtin-apis/test-runner.mdx | 2 + .../generated/node-v24-backlog.json | 21 +- .../generated/node-v24-internal.json | 25 +- internal/compatibility/static-support.json | 4 +- .../ambient/scriptc-node-fallback.d.ts | 53 ++- .../backend/llvm/checked-value-lifetimes.ts | 7 +- .../src/backend/llvm/dyn-handle-tags.test.ts | 39 ++ packages/compiler/src/backend/llvm/dyn.ts | 4 +- packages/compiler/src/backend/llvm/emitter.ts | 17 + .../compiler/src/backend/llvm/expr-records.ts | 12 +- .../compiler/src/backend/llvm/lib-dispatch.ts | 8 +- .../compiler/src/backend/llvm/lib-process.ts | 20 +- .../compiler/src/backend/llvm/lib-shared.ts | 2 + .../src/backend/llvm/reference-effects.ts | 1 + .../src/backend/llvm/reference-writes.ts | 1 + packages/compiler/src/backend/llvm/shapes.ts | 70 +-- packages/compiler/src/backend/may-throw.ts | 4 +- .../src/frontend/lowering/containers/bytes.ts | 16 + .../lowering/containers/iterator-helpers.ts | 434 +++++++++++++++--- .../lowering/containers/match-row-uses.ts | 299 ++++++++++++ .../lowering/containers/string-and-regexp.ts | 3 +- .../frontend/lowering/iterator-adapters.ts | 348 +++++++++++++- .../src/frontend/lowering/lower-calls.ts | 92 +++- .../src/frontend/lowering/lower-classes.ts | 12 +- .../src/frontend/lowering/lower-exprs.ts | 17 +- .../src/frontend/lowering/lower-inspect.ts | 12 + .../src/frontend/lowering/lower-stmts.ts | 22 +- .../src/frontend/lowering/lower-test.ts | 224 +++++++-- .../compiler/src/frontend/lowering/lowerer.ts | 91 ++-- packages/compiler/src/frontend/type-mapper.ts | 5 +- .../compiler/src/ir/builtin-signatures.ts | 3 + .../src/ir/function-references.test.ts | 1 + packages/compiler/src/ir/int32-slots.ts | 1 + .../compiler/src/ir/intrinsic-signatures.ts | 6 + packages/compiler/src/ir/ir.ts | 19 +- packages/compiler/src/ir/runtime-features.ts | 363 ++++++++------- .../compiler/test/match-row-emission.test.ts | 69 +++ .../corpus/5815-als-callback-context.ts.json | 6 + .../tests/corpus/6010-match-metadata.ts.json | 6 + .../corpus/6011-match-metadata-js.js.json | 6 + .../corpus/6071-iterator-sources.ts.json | 6 + .../corpus/6072-iterator-sources-js.js.json | 6 + .../6073-stored-collection-helpers.ts.json | 6 + .../6074-stored-collection-helpers-js.js.json | 6 + .../tests/corpus/6075-print-iterators.ts.json | 6 + .../corpus/6076-print-iterators-js.js.json | 6 + .../6200-http-agent-free-socket-data.ts.json | 6 + .../string-iterator-helpers.ts.json | 6 - packages/runtime/src/scr_alloc.c | 42 +- packages/runtime/src/scr_alloc.test.c | 52 ++- packages/runtime/src/scr_array.c | 6 + packages/runtime/src/scr_array.h | 12 +- packages/runtime/src/scr_async.c | 113 ++++- packages/runtime/src/scr_async.h | 12 + packages/runtime/src/scr_async_dyn.c | 1 + packages/runtime/src/scr_dyn.h | 149 +++--- packages/runtime/src/scr_generator.h | 14 + packages/runtime/src/scr_http.c | 7 + packages/runtime/src/scr_inspect.c | 98 ++++ packages/runtime/src/scr_json.c | 56 +++ packages/runtime/src/scr_message.test.c | 2 +- packages/runtime/src/scr_regex.c | 184 +++++++- packages/runtime/src/scr_regex.h | 14 +- packages/runtime/src/scr_test.c | 20 + packages/runtime/src/scr_test.h | 6 + scripts/zero-cost-gate.mjs | 5 +- tests/corpus/5815-als-callback-context.ts | 76 +++ tests/corpus/6010-match-metadata.ts | 40 ++ tests/corpus/6011-match-metadata-js.js | 31 ++ tests/corpus/6071-iterator-sources.ts | 70 +++ tests/corpus/6072-iterator-sources-js.js | 70 +++ .../corpus/6073-stored-collection-helpers.ts | 60 +++ .../6074-stored-collection-helpers-js.js | 57 +++ tests/corpus/6075-print-iterators.ts | 19 + tests/corpus/6076-print-iterators-js.js | 17 + .../6110-cjs-speculative-wrapper-fences.cjs | 3 +- .../6200-http-agent-free-socket-data.ts | 57 +++ .../collection-iterator-helpers.ts | 18 +- tests/diagnostics/es2025-fences.ts | 6 +- tests/diagnostics/string-iterator-helpers.ts | 3 - .../node-test/cases/test-name-forms.ts | 55 +++ .../collection-iterator-helpers.ts.txt | 22 +- .../__snapshots__/es2025-fences.ts.txt | 8 +- .../string-iterator-helpers.ts.txt | 8 - tests/harness/corpus-costs.json | 34 ++ 88 files changed, 3278 insertions(+), 567 deletions(-) create mode 100644 docs/content/limitations/builtin-apis/asynclocalstorage.mdx create mode 100644 packages/compiler/src/backend/llvm/dyn-handle-tags.test.ts create mode 100644 packages/compiler/src/frontend/lowering/containers/match-row-uses.ts create mode 100644 packages/compiler/test/match-row-emission.test.ts create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5815-als-callback-context.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6010-match-metadata.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6011-match-metadata-js.js.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6071-iterator-sources.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6072-iterator-sources-js.js.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6073-stored-collection-helpers.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6074-stored-collection-helpers-js.js.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6075-print-iterators.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6076-print-iterators-js.js.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6200-http-agent-free-socket-data.ts.json delete mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/string-iterator-helpers.ts.json create mode 100644 tests/corpus/5815-als-callback-context.ts create mode 100644 tests/corpus/6010-match-metadata.ts create mode 100644 tests/corpus/6011-match-metadata-js.js create mode 100644 tests/corpus/6071-iterator-sources.ts create mode 100644 tests/corpus/6072-iterator-sources-js.js create mode 100644 tests/corpus/6073-stored-collection-helpers.ts create mode 100644 tests/corpus/6074-stored-collection-helpers-js.js create mode 100644 tests/corpus/6075-print-iterators.ts create mode 100644 tests/corpus/6076-print-iterators-js.js create mode 100644 tests/corpus/6200-http-agent-free-socket-data.ts delete mode 100644 tests/diagnostics/string-iterator-helpers.ts create mode 100644 tests/fixtures/node-test/cases/test-name-forms.ts delete mode 100644 tests/harness/__snapshots__/string-iterator-helpers.ts.txt diff --git a/docs/content/docs/limitations.mdx b/docs/content/docs/limitations.mdx index ae166836f..d261b6385 100644 --- a/docs/content/docs/limitations.mdx +++ b/docs/content/docs/limitations.mdx @@ -64,6 +64,8 @@ title: "Limitations" ../limitations/builtin-apis/eventemitter.mdx +../limitations/builtin-apis/asynclocalstorage.mdx + ../limitations/builtin-apis/streams-and-readline.mdx ../limitations/builtin-apis/commonjs-metadata.mdx diff --git a/docs/content/limitations/builtin-apis/asynclocalstorage.mdx b/docs/content/limitations/builtin-apis/asynclocalstorage.mdx new file mode 100644 index 000000000..020382915 --- /dev/null +++ b/docs/content/limitations/builtin-apis/asynclocalstorage.mdx @@ -0,0 +1 @@ +- **AsyncLocalStorage:** timer, interval, `setImmediate`, `process.nextTick` and `queueMicrotask` callbacks run with the stores that were active when they were scheduled, as in Node. Completion callbacks of callback-style I/O APIs, such as `fs.readFile` and `crypto.randomBytes`, run with no store, where Node runs them with the stores that were active when the operation started. diff --git a/docs/content/limitations/builtin-apis/iterator-helpers.mdx b/docs/content/limitations/builtin-apis/iterator-helpers.mdx index 01f779cb6..b4c4d8200 100644 --- a/docs/content/limitations/builtin-apis/iterator-helpers.mdx +++ b/docs/content/limitations/builtin-apis/iterator-helpers.mdx @@ -1 +1 @@ -- **Iterator helpers:** `map`, `filter`, `take`, `drop`, `flatMap`, `toArray`, `forEach`, `reduce`, `some`, `every` and `find` compile on generators, on array, Map and Set iterators, and on the objects the helpers return. Helper objects support `.next()`, `for...of`, spreads, `Array.from` and `yield*`, pull their source lazily, and close it (run its `return()`) at the same points as Node: when `take` reaches its count, when a short-circuiting method finds its answer, when a callback throws, and when the loop consuming the helper exits early. `flatMap` callbacks return arrays or generators. Iterator helpers on string iterators, `Iterator.from`, the `Iterator` global, calling `return()` on a helper object in TypeScript, printing helper objects, and helper calls on untyped iterators in JavaScript are unsupported. +- **Iterator helpers:** `map`, `filter`, `take`, `drop`, `flatMap`, `toArray`, `forEach`, `reduce`, `some`, `every` and `find` compile on generators, on array, typed array, string, Map and Set iterators, and on the objects the helpers return. Helper objects support `.next()`, `.return()`, `for...of`, spreads, `Array.from` and `yield*`, pull their source lazily, and close it (run its `return()`) at the same points as Node: when `take` reaches its count, when a short-circuiting method finds its answer, when a callback throws, and when the loop consuming the helper exits early. In TypeScript, `h.return!()` and `h.return?.()` compile when `h` is a helper call or a `const` initialized with one. `flatMap` callbacks return arrays, generators, Sets or Maps; a string result throws Node's TypeError. `Iterator.from` accepts arrays, strings, typed arrays, Sets, Maps and iterator objects such as generators. In JavaScript, untyped array, string, Map and Set iterators answer the same methods. `console.log`, `util.inspect` and `Object.prototype.toString` print iterator objects as Node does: `Object [Iterator Helper] {}`, `Object [Generator] {}`, `Object [Array Iterator] {}`, and Map and Set iterators with the entries they have left. `Iterator.from` over an object with its own `next` method or over an untyped value, the `Iterator` global as a value, and `String()` and template literals of typed iterator objects are unsupported. diff --git a/docs/content/limitations/builtin-apis/test-runner.mdx b/docs/content/limitations/builtin-apis/test-runner.mdx index 77d3d9a84..70143a0c3 100644 --- a/docs/content/limitations/builtin-apis/test-runner.mdx +++ b/docs/content/limitations/builtin-apis/test-runner.mdx @@ -4,3 +4,5 @@ - **Suite and hook contexts:** a `describe` body may take its `SuiteContext`, which has `name`, `fullName`, `filePath`, `attempt`, `passed` and `diagnostic`. Hooks may take their context, as in Node: the suite's for `before` and `after`, the test's for `beforeEach`, `afterEach`, `t.before` and `t.after`. The `SuiteContext` `signal` and `log` members, and hook `done` callbacks, throw `SC2020`. - **Module-level hooks:** `before` hooks outside any suite run at their `before()` call, and the first top-level test waits for the async ones; `after` hooks outside any suite run after the last top-level test. Both get the root's context, named ``. As in Node, a throwing root `before` fails each top-level test with its error, without running its body, and cancels the tests and suites inside each top-level suite. A throwing root `after` reports the entry file as a failed result, and the exit code is unchanged. - **Timeouts:** when a test's `{ timeout }` elapses, its running subtests are cancelled, as in Node. Subtests do not take their parent's `timeout` as their own, where Node's do, and the subtests of a timed-out test with `{ concurrency }` above 1 run to completion before the test reports. A body that keeps running after its timeout and calls `t.test` again runs that subtest, where Node reports it as not started. +- **Thrown values:** a test that throws an object or array that is not an `Error`, such as `throw { code: 42 }`, reports `[object Object]` in the failing section, where Node prints the value as `util.inspect` renders it. +- **Test names:** a test without a name, or with an empty one, takes its function's name or ``, as in Node, when the function's name is known at compile time: a function expression, a function declaration, or a `const` bound to an arrow or anonymous function. A name computed at run time, with a function the program received as a parameter, is used as given, so an empty one stays empty where Node uses the function's name. `test(fn)` and `test("", fn)` with such a function throw `SC2020`. diff --git a/internal/compatibility/generated/node-v24-backlog.json b/internal/compatibility/generated/node-v24-backlog.json index 7d0fcf75c..a0d7bec4b 100644 --- a/internal/compatibility/generated/node-v24-backlog.json +++ b/internal/compatibility/generated/node-v24-backlog.json @@ -880,7 +880,8 @@ "confidence": "high", "source": "compiler-dedicated:AsyncLocalStorage", "tests": [ - "tests/corpus/2213-async-local-storage.cjs" + "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts" ] } } @@ -932,7 +933,8 @@ "confidence": "high", "source": "compiler-dedicated:asyncLocalStorage.disable", "tests": [ - "tests/corpus/2213-async-local-storage.cjs" + "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts" ] } } @@ -959,7 +961,8 @@ "confidence": "high", "source": "compiler-dedicated:asyncLocalStorage.getStore", "tests": [ - "tests/corpus/2213-async-local-storage.cjs" + "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts" ] } } @@ -986,7 +989,8 @@ "confidence": "high", "source": "compiler-dedicated:asyncLocalStorage.enterWith", "tests": [ - "tests/corpus/2213-async-local-storage.cjs" + "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts" ] } } @@ -1013,7 +1017,8 @@ "confidence": "high", "source": "compiler-dedicated:asyncLocalStorage.run", "tests": [ - "tests/corpus/2213-async-local-storage.cjs" + "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts" ] } } @@ -1040,7 +1045,8 @@ "confidence": "high", "source": "compiler-dedicated:asyncLocalStorage.exit", "tests": [ - "tests/corpus/2213-async-local-storage.cjs" + "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts" ] } } @@ -1771,7 +1777,8 @@ "confidence": "high", "source": "compiler-dedicated:AsyncLocalStorage", "tests": [ - "tests/corpus/2213-async-local-storage.cjs" + "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts" ] } } diff --git a/internal/compatibility/generated/node-v24-internal.json b/internal/compatibility/generated/node-v24-internal.json index c87f98fec..cfbde8940 100644 --- a/internal/compatibility/generated/node-v24-internal.json +++ b/internal/compatibility/generated/node-v24-internal.json @@ -1953,7 +1953,8 @@ "status": "partial", "evidence": "derived:descendants", "tests": [ - "tests/corpus/2213-async-local-storage.cjs" + "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts" ] }, "anchorSource": "chapter" @@ -2002,7 +2003,8 @@ "status": "partial", "evidence": "compiler-dedicated:AsyncLocalStorage", "tests": [ - "tests/corpus/2213-async-local-storage.cjs" + "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts" ] }, "anchorSource": "exact" @@ -2074,7 +2076,8 @@ "status": "partial", "evidence": "compiler-dedicated:asyncLocalStorage.disable", "tests": [ - "tests/corpus/2213-async-local-storage.cjs" + "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts" ] }, "anchorSource": "exact" @@ -2100,7 +2103,8 @@ "status": "partial", "evidence": "compiler-dedicated:asyncLocalStorage.getStore", "tests": [ - "tests/corpus/2213-async-local-storage.cjs" + "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts" ] }, "anchorSource": "exact" @@ -2126,7 +2130,8 @@ "status": "partial", "evidence": "compiler-dedicated:asyncLocalStorage.enterWith", "tests": [ - "tests/corpus/2213-async-local-storage.cjs" + "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts" ] }, "anchorSource": "exact" @@ -2152,7 +2157,8 @@ "status": "partial", "evidence": "compiler-dedicated:asyncLocalStorage.run", "tests": [ - "tests/corpus/2213-async-local-storage.cjs" + "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts" ] }, "anchorSource": "exact" @@ -2178,7 +2184,8 @@ "status": "partial", "evidence": "compiler-dedicated:asyncLocalStorage.exit", "tests": [ - "tests/corpus/2213-async-local-storage.cjs" + "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts" ] }, "anchorSource": "exact" @@ -2481,6 +2488,7 @@ "evidence": "derived:descendants", "tests": [ "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts", "tests/harness/node-test.test.ts" ] }, @@ -3042,7 +3050,8 @@ "status": "partial", "evidence": "compiler-dedicated:AsyncLocalStorage", "tests": [ - "tests/corpus/2213-async-local-storage.cjs" + "tests/corpus/2213-async-local-storage.cjs", + "tests/corpus/5815-als-callback-context.ts" ] }, "anchorSource": "exact" diff --git a/internal/compatibility/static-support.json b/internal/compatibility/static-support.json index 584492bf1..2e5eb1c4c 100644 --- a/internal/compatibility/static-support.json +++ b/internal/compatibility/static-support.json @@ -36,7 +36,7 @@ "syntax.for-await-readable-streams": ["tests/corpus/2861-readable-iterator-close.ts", "tests/corpus/2862-web-readable-async-iteration.ts"], "syntax.for-await-class-async-iterators": ["tests/corpus/2863-class-async-iterator.ts"], "node-builtin.assert": ["tests/corpus/1600-assert-passing.ts", "tests/corpus/1609-assert-async.ts"], - "node-builtin.async_hooks": ["tests/corpus/2213-async-local-storage.cjs"], + "node-builtin.async_hooks": ["tests/corpus/2213-async-local-storage.cjs", "tests/corpus/5815-als-callback-context.ts"], "node-builtin.buffer": ["tests/corpus/1402-buffer-encodings.ts", "tests/corpus/1660-buffer-read-write-num.ts", "tests/corpus/2902-bigint-bytes.ts", "tests/corpus/2904-bigint-errors.ts"], "node-builtin.child_process": ["tests/corpus/1361-spawn-events.ts", "tests/corpus/1565-spawn-pipe-streams.ts", "tests/corpus/2911-execfile-callback.ts", "tests/corpus/2939-child-shell-text.ts", "tests/corpus/2946-child-fork-ipc/main.ts", "tests/corpus/3127-spawn-runtime-options.ts", "tests/corpus/3130-child-generic-streams.ts", "tests/corpus/3131-child-rest-listeners.js", "tests/corpus/3132-child-generic-stream-completion.ts"], "node-builtin.cluster": ["tests/corpus/957-builtins-namespace.ts"], @@ -395,7 +395,7 @@ { "symbols": ["crypto.hkdf", "crypto.hkdfSync", "crypto.scrypt", "crypto.scryptSync"], "status": "partial", "evidence": ["tests/corpus/node24-crypto-kdf.ts", "tests/corpus/node24-crypto-kdf-errors.ts", "tests/corpus/node24-crypto-kdf-options.ts", "tests/corpus/5438-crypto-kdf-digests.ts"] }, { "symbols": ["EventEmitter", "emitter.on", "emitter.addListener", "emitter.once", "emitter.prependListener", "emitter.prependOnceListener", "emitter.off", "emitter.removeListener", "emitter.removeAllListeners", "emitter.emit", "emitter.listenerCount", "emitter.listeners", "emitter.rawListeners", "emitter.eventNames", "emitter.setMaxListeners", "emitter.getMaxListeners", "events.once"], "status": "partial", "evidence": ["tests/corpus/1677-emitter-listeners.ts", "tests/corpus/2620-ee-override-once-order.ts"] }, { "symbols": ["events.getEventListeners", "events.getMaxListeners", "events.listenerCount", "events.setMaxListeners", "events.defaultMaxListeners"], "status": "partial", "evidence": ["tests/corpus/5885-events-max-listener-helpers.ts", "tests/corpus/5846-events-esm-default-max.ts", "tests/corpus/5847-events-cjs-default-max.cjs"] }, - { "symbols": ["AsyncLocalStorage", "asyncLocalStorage.run", "asyncLocalStorage.exit", "asyncLocalStorage.getStore", "asyncLocalStorage.enterWith", "asyncLocalStorage.disable"], "status": "partial", "evidence": ["tests/corpus/2213-async-local-storage.cjs"] }, + { "symbols": ["AsyncLocalStorage", "asyncLocalStorage.run", "asyncLocalStorage.exit", "asyncLocalStorage.getStore", "asyncLocalStorage.enterWith", "asyncLocalStorage.disable"], "status": "partial", "evidence": ["tests/corpus/2213-async-local-storage.cjs", "tests/corpus/5815-als-callback-context.ts"] }, { "symbols": ["StringDecoder", "stringDecoder.write", "stringDecoder.end"], "status": "partial", "evidence": ["tests/corpus/1662-string-decoder-encodings.ts", "tests/corpus/node24-string-decoder.ts", "tests/corpus/5882-string-decoder-runtime-encoding.ts"] }, { "symbols": ["setImmediate", "setInterval", "setTimeout", "clearImmediate", "clearInterval", "clearTimeout", "timersPromises.setTimeout", "timersPromises.setImmediate", "timersPromises.setInterval", "immediate.hasRef", "immediate.ref", "immediate.unref", "timeout.hasRef", "timeout.ref", "timeout.unref", "timeout.refresh"], "status": "partial", "evidence": ["tests/corpus/1804-timers-module.ts", "tests/corpus/1805-timer-callback-args.ts", "tests/corpus/2093-timers-promises.ts", "tests/corpus/2852-timers-promises-interval.ts", "tests/corpus/5830-timers-promises-signal.ts"] }, { "symbols": ["timersPromises.scheduler.wait", "timersPromises.scheduler.yield"], "status": "partial", "evidence": ["tests/corpus/5839-timers-promises-scheduler.ts", "tests/corpus/5889-timers-scheduler-signal.ts"] }, diff --git a/packages/compiler/ambient/scriptc-node-fallback.d.ts b/packages/compiler/ambient/scriptc-node-fallback.d.ts index 9655b7580..3e3f66a2a 100644 --- a/packages/compiler/ambient/scriptc-node-fallback.d.ts +++ b/packages/compiler/ambient/scriptc-node-fallback.d.ts @@ -3405,8 +3405,10 @@ declare module "node:test" { readonly tags: readonly string[]; readonly assert: TestContextAssert; readonly mock: MockTracker; - test(name: string, fn?: TestFn): Promise; - test(name: string, options: TestOptions, fn?: TestFn): Promise; + test(name?: string, fn?: TestFn): Promise; + test(name?: string, options?: TestOptions, fn?: TestFn): Promise; + test(options?: TestOptions, fn?: TestFn): Promise; + test(fn?: TestFn): Promise; skip(message?: string): void; todo(message?: string): void; diagnostic(message: string): void; @@ -3436,23 +3438,44 @@ declare module "node:test" { match(value: string, regExp: RegExp, message?: string): void; doesNotMatch(value: string, regExp: RegExp, message?: string): void; } - function test(name: string, fn?: TestFn): Promise; - function test(name: string, options: TestOptions, fn?: TestFn): Promise; + /* The name, options and function are each optional, as in Node: a test + * without a name takes the function's name, or "". */ + function test(name?: string, fn?: TestFn): Promise; + function test(name?: string, options?: TestOptions, fn?: TestFn): Promise; + function test(options?: TestOptions, fn?: TestFn): Promise; + function test(fn?: TestFn): Promise; namespace test { - function skip(name: string, fn?: TestFn): Promise; - function skip(name: string, options: TestOptions, fn?: TestFn): Promise; - function todo(name: string, fn?: TestFn): Promise; - function todo(name: string, options: TestOptions, fn?: TestFn): Promise; - function only(name: string, fn?: TestFn): Promise; - function only(name: string, options: TestOptions, fn?: TestFn): Promise; + function skip(name?: string, fn?: TestFn): Promise; + function skip(name?: string, options?: TestOptions, fn?: TestFn): Promise; + function skip(options?: TestOptions, fn?: TestFn): Promise; + function skip(fn?: TestFn): Promise; + function todo(name?: string, fn?: TestFn): Promise; + function todo(name?: string, options?: TestOptions, fn?: TestFn): Promise; + function todo(options?: TestOptions, fn?: TestFn): Promise; + function todo(fn?: TestFn): Promise; + function only(name?: string, fn?: TestFn): Promise; + function only(name?: string, options?: TestOptions, fn?: TestFn): Promise; + function only(options?: TestOptions, fn?: TestFn): Promise; + function only(fn?: TestFn): Promise; } const it: typeof test; - function describe(name: string, fn?: SuiteFn): void; - function describe(name: string, options: TestOptions, fn?: SuiteFn): void; + function describe(name?: string, fn?: SuiteFn): void; + function describe(name?: string, options?: TestOptions, fn?: SuiteFn): void; + function describe(options?: TestOptions, fn?: SuiteFn): void; + function describe(fn?: SuiteFn): void; namespace describe { - function skip(name: string, fn?: SuiteFn): void; - function todo(name: string, fn?: SuiteFn): void; - function only(name: string, fn?: SuiteFn): void; + function skip(name?: string, fn?: SuiteFn): void; + function skip(name?: string, options?: TestOptions, fn?: SuiteFn): void; + function skip(options?: TestOptions, fn?: SuiteFn): void; + function skip(fn?: SuiteFn): void; + function todo(name?: string, fn?: SuiteFn): void; + function todo(name?: string, options?: TestOptions, fn?: SuiteFn): void; + function todo(options?: TestOptions, fn?: SuiteFn): void; + function todo(fn?: SuiteFn): void; + function only(name?: string, fn?: SuiteFn): void; + function only(name?: string, options?: TestOptions, fn?: SuiteFn): void; + function only(options?: TestOptions, fn?: SuiteFn): void; + function only(fn?: SuiteFn): void; } const suite: typeof describe; function before(fn: HookFn): void; diff --git a/packages/compiler/src/backend/llvm/checked-value-lifetimes.ts b/packages/compiler/src/backend/llvm/checked-value-lifetimes.ts index c359f6175..5f8119079 100644 --- a/packages/compiler/src/backend/llvm/checked-value-lifetimes.ts +++ b/packages/compiler/src/backend/llvm/checked-value-lifetimes.ts @@ -42,5 +42,10 @@ export function borrowsJsonInputs(fn: string): boolean { * loop. Runtime steps borrow that state but may invoke indexed getters, so * this permits stable-local borrowing without preserving reference edges. */ export function borrowsIteratorInputs(fn: string): boolean { - return fn === "dyn.iteratorCanStep" || fn === "dyn.iteratorStep" || fn === "dyn.iteratorStepDone"; + return ( + fn === "dyn.iteratorCanStep" || + fn === "dyn.iteratorStep" || + fn === "dyn.iteratorStepDone" || + fn === "dyn.collectionIteratorIndex" + ); } diff --git a/packages/compiler/src/backend/llvm/dyn-handle-tags.test.ts b/packages/compiler/src/backend/llvm/dyn-handle-tags.test.ts new file mode 100644 index 000000000..8aa69b386 --- /dev/null +++ b/packages/compiler/src/backend/llvm/dyn-handle-tags.test.ts @@ -0,0 +1,39 @@ +import { readFileSync } from "node:fs"; +import { expect, test } from "vitest"; +import { DYN_HANDLE_KINDS } from "../../ir/ir.js"; +import { DYN_HANDLE_TAG_NUM } from "./dyn.js"; + +// ScrDynHandleTag in scr_dyn.h lists its tags by name, each with an explicit +// value, so changes that add tags edit different lines. These checks stand +// in for what the enum's order used to guarantee. +const header = readFileSync(new URL("../../../../runtime/src/scr_dyn.h", import.meta.url), "utf8"); +const body = /typedef enum \{\n([\s\S]*?)\} ScrDynHandleTag;/.exec(header)![1]!; +const tags = [...body.matchAll(/^\s*(SCR_DYNH_[A-Z0-9_]+) = (\d+),/gm)].map(([, name, value]) => ({ + name: name!, + value: Number(value), +})); +const capacity = Number(/#define SCR_DYNH_CAPACITY (\d+)/.exec(header)![1]); + +test("every ScrDynHandleTag has an explicit value, in name order", () => { + const lines = body.split("\n").filter((line) => line.trim().startsWith("SCR_DYNH_")); + expect(tags.length).toBe(lines.length); + expect(tags.map((tag) => tag.name)).toEqual(tags.map((tag) => tag.name).toSorted()); +}); + +test("ScrDynHandleTag values are unique and below SCR_DYNH_CAPACITY", () => { + const owners = new Map(); + for (const { name, value } of tags) { + expect(owners.get(value), `${name} = ${value}`).toBeUndefined(); + owners.set(value, name); + expect(value, name).toBeLessThan(capacity); + } +}); + +test("the backend's handle tag numbers match scr_dyn.h", () => { + const values = new Map(tags.map((tag) => [tag.name, tag.value])); + for (const [kind, num] of Object.entries(DYN_HANDLE_TAG_NUM)) { + const tag = DYN_HANDLE_KINDS.get(kind)?.tag; + expect(tag, kind).toBeDefined(); + expect(values.get(tag!), `${kind} (${tag})`).toBe(num); + } +}); diff --git a/packages/compiler/src/backend/llvm/dyn.ts b/packages/compiler/src/backend/llvm/dyn.ts index 9b11d6240..154b80c02 100644 --- a/packages/compiler/src/backend/llvm/dyn.ts +++ b/packages/compiler/src/backend/llvm/dyn.ts @@ -72,8 +72,8 @@ export function recordDynKeepsIdentity(shape: IrRecordShape): boolean { } /** ScrDynKind values (scr_runtime.h). */ -/** ScrDynHandleTag numeric values (scr_runtime.h's enum order). */ -const DYN_HANDLE_TAG_NUM: Record = { +/** ScrDynHandleTag values (scr_dyn.h's explicit values; dyn-handle-tags.test.ts checks them). */ +export const DYN_HANDLE_TAG_NUM: Record = { httpReq: 0, httpRes: 1, netSocket: 2, diff --git a/packages/compiler/src/backend/llvm/emitter.ts b/packages/compiler/src/backend/llvm/emitter.ts index 588e8c26a..48012704b 100644 --- a/packages/compiler/src/backend/llvm/emitter.ts +++ b/packages/compiler/src/backend/llvm/emitter.ts @@ -968,6 +968,14 @@ export class LlEmitter { /** The program calls mock.timers members (moduleUsesMockTimers): timer * calls emit scr_test.c's mock-aware entry points. */ readonly mockTimers: boolean; + private schedulesInContextValue: boolean | undefined = undefined; + /** Whether timer and tick scheduling goes through the *_als entry + * points (RuntimeFeatures asyncContext). */ + get schedulesInContext(): boolean { + if (this.schedulesInContextValue === undefined) + this.schedulesInContextValue = moduleRuntimeFeatures(this.mod).asyncContext; + return this.schedulesInContextValue; + } /** Emitted ref-kind resolve thunks for new Promise, interned per inner * typeKey → thunk symbol. */ readonly resolveThunks = new Map(); @@ -4636,6 +4644,9 @@ export class LlEmitter { // An iterator helper's return() before its first next() closes the // source generator in its first argument slot. const closesSource = fn.generator.closesSource === true; + if (fn.generator.tag !== undefined) this.declare(`declare void @scr_gen_set_tag(ptr, ptr)`); + if (fn.generator.inheritsTag === true) + this.declare(`declare void @scr_gen_copy_tag(ptr, ptr)`); if (closesSource) { const field = lifted ? 1 : 0; this.declare(`declare void @scr_gen_close(ptr)`); @@ -4668,6 +4679,12 @@ export class LlEmitter { settleAsync === null ? ` %gg = call ptr @${genNew}(ptr @${mangleTrampoline(fn.name)}, ptr %ap, ptr @${mangleGenDrop(fn.name)})` : ` %gg = call ptr @scr_async_gen_new(ptr @${mangleTrampoline(fn.name)}, ptr %ap, ptr @${mangleGenDrop(fn.name)}, ptr @${settleAsync})`, + ...(fn.generator.tag !== undefined + ? [` call void @scr_gen_set_tag(ptr %gg, ptr ${this.cstr(fn.generator.tag)})`] + : []), + ...(fn.generator.inheritsTag === true + ? [` call void @scr_gen_copy_tag(ptr %gg, ptr %a${lifted ? 1 : 0})`] + : []), ...(closesSource ? [ ` call void @scr_gen_set_unstarted_return(ptr %gg, ptr @${mangleGenSourceClose(fn.name)})`, diff --git a/packages/compiler/src/backend/llvm/expr-records.ts b/packages/compiler/src/backend/llvm/expr-records.ts index 3318ae774..0cbfe15dd 100644 --- a/packages/compiler/src/backend/llvm/expr-records.ts +++ b/packages/compiler/src/backend/llvm/expr-records.ts @@ -147,7 +147,9 @@ function directRegexOperandMethod(method: string): boolean { case "match": case "search": case "matchAll": + case "matchAllCaptures": case "matchAllInto": + case "matchAllCapturesInto": case "replace": case "replaceAll": case "split": @@ -192,7 +194,7 @@ export function emitRegexIntrinsic( } else { values = preservesRegexInputs(method) ? emitBorrowedInputs(host, inputs) - : method === "matchAllInto" + : method === "matchAllInto" || method === "matchAllCapturesInto" ? inputs.map((a) => emitBorrowedInput(host, a)) : inputs.map((a) => host.emitExpr(a)); } @@ -216,6 +218,9 @@ export function emitRegexIntrinsic( // Every match drained eagerly into a fresh +1 string[][]; throws // Node's TypeError on a non-global regex (catchable). return fallible("scr_regex_match_all", `ptr ${r.name}, ptr ${args[0]!.name}`); + case "matchAllCaptures": + // matchAll whose rows carry no index, input or groups properties. + return fallible("scr_regex_match_all_captures", `ptr ${r.name}, ptr ${args[0]!.name}`); case "matchAllInto": // matchAll's companion-index form: args[1] (a number[]) also // receives each match's UTF-16 start index. @@ -223,6 +228,11 @@ export function emitRegexIntrinsic( "scr_regex_match_all_into", `ptr ${r.name}, ptr ${args[0]!.name}, ptr ${args[1]!.name}`, ); + case "matchAllCapturesInto": + return fallible( + "scr_regex_match_all_captures_into", + `ptr ${r.name}, ptr ${args[0]!.name}, ptr ${args[1]!.name}`, + ); case "replaceAll": return fallible( "scr_regex_replace_all", diff --git a/packages/compiler/src/backend/llvm/lib-dispatch.ts b/packages/compiler/src/backend/llvm/lib-dispatch.ts index f21ded106..034f0b6f1 100644 --- a/packages/compiler/src/backend/llvm/lib-dispatch.ts +++ b/packages/compiler/src/backend/llvm/lib-dispatch.ts @@ -526,7 +526,13 @@ export function emitGenericLibCall(host: LlvmEmitterContext, e: LibCallExpr): Ll if (MAY_THROW_LIB_FNS.has(e.fn) && LIB_FN_SYMS[e.fn] === undefined) { throw new LlvmUnsupportedError(`libCall:${e.fn}`, e.loc); } - const sym = mockTimerSym(host, e) ?? LIB_FN_SYMS[e.fn]; + // queueMicrotask's checked-dynamic form carries the AsyncLocalStorage + // context like the static one (lib-process.ts). + const sym = + mockTimerSym(host, e) ?? + (e.fn === "timers.queueMicrotaskDyn" && host.schedulesInContext + ? "scr_queue_microtask_dyn_als" + : LIB_FN_SYMS[e.fn]); if (sym === undefined) throw new LlvmUnsupportedError(`libCall:${e.fn}`, e.loc); const args = READ_ONLY_STRING_LIB_FNS.has(e.fn) ? emitBorrowedInputs(host, e.args) diff --git a/packages/compiler/src/backend/llvm/lib-process.ts b/packages/compiler/src/backend/llvm/lib-process.ts index 546c9a209..3c35ae6dc 100644 --- a/packages/compiler/src/backend/llvm/lib-process.ts +++ b/packages/compiler/src/backend/llvm/lib-process.ts @@ -374,16 +374,18 @@ export function emitAsyncContextLibCall(host: LlvmEmitterContext, e: LibCallExpr host.usesTimers = true; const cb = host.emitExpr(e.args[0]!); host.moveTemp(cb); + // Programs that enter AsyncLocalStorage stores schedule through the + // *_als entry points, which carry the active stores into the callback. const sym = - mockTimerSym(host, e) ?? - { - "timers.setTimeout": "scr_set_timeout", - "timers.setInterval": "scr_set_interval", - "timers.setTimeoutHandle": "scr_set_timeout_handle", - "timers.setImmediate": "scr_set_immediate", - "process.nextTick": "scr_next_tick", - "timers.queueMicrotask": "scr_queue_microtask", - }[e.fn]; + (mockTimerSym(host, e) ?? + { + "timers.setTimeout": "scr_set_timeout", + "timers.setInterval": "scr_set_interval", + "timers.setTimeoutHandle": "scr_set_timeout_handle", + "timers.setImmediate": "scr_set_immediate", + "process.nextTick": "scr_next_tick", + "timers.queueMicrotask": "scr_queue_microtask", + }[e.fn]!) + (host.schedulesInContext ? "_als" : ""); const rest = e.args.slice(1).map((a) => host.emitExpr(a)); const argList = [`ptr ${cb.name}`, ...rest.map((a) => `${host.llType(a.type)} ${a.name}`)].join( ", ", diff --git a/packages/compiler/src/backend/llvm/lib-shared.ts b/packages/compiler/src/backend/llvm/lib-shared.ts index 7b174c6f5..14148af0c 100644 --- a/packages/compiler/src/backend/llvm/lib-shared.ts +++ b/packages/compiler/src/backend/llvm/lib-shared.ts @@ -890,6 +890,7 @@ export const LIB_FN_SYMS: Record = { "dyn.iteratorCanStep": "scr_dyn_iterator_can_step", "dyn.iteratorStep": "scr_dyn_iterator_step", "dyn.iteratorStepDone": "scr_dyn_iterator_step_done", + "dyn.collectionIteratorIndex": "scr_dyn_collection_iterator_index", "dyn.mapSeedEntries": "scr_dyn_map_seed_entries", "dyn.mapSeedEntry": "scr_dyn_map_seed_entry", "dyn.arrLen": "scr_dyn_arr_len", @@ -1414,6 +1415,7 @@ export const LIB_FN_SYMS: Record = { "regex.groupEquals": "scr_regex_group_eq", "regex.groupNumber": "scr_regex_group_number", "regex.groupParseInt": "scr_regex_group_parse_int", + "regex.resultProp": "scr_regex_result_prop", // queueMicrotask's checked-dynamic form (borrowed dyn; a non-function // throws synchronously), the minted setImmediate value, and the // timers/promises immediate — all mark the loop live. diff --git a/packages/compiler/src/backend/llvm/reference-effects.ts b/packages/compiler/src/backend/llvm/reference-effects.ts index 88875fce4..e7b044187 100644 --- a/packages/compiler/src/backend/llvm/reference-effects.ts +++ b/packages/compiler/src/backend/llvm/reference-effects.ts @@ -29,6 +29,7 @@ export function preservesRegexInputs(method: string): boolean { case "match": case "search": case "matchAll": + case "matchAllCaptures": case "replace": case "replaceAll": case "split": diff --git a/packages/compiler/src/backend/llvm/reference-writes.ts b/packages/compiler/src/backend/llvm/reference-writes.ts index 9397b7aee..63e4993e8 100644 --- a/packages/compiler/src/backend/llvm/reference-writes.ts +++ b/packages/compiler/src/backend/llvm/reference-writes.ts @@ -104,6 +104,7 @@ const INERT_LIB_CALLS = new Set([ "regex.groupEquals", "regex.groupNumber", "regex.groupParseInt", + "regex.resultProp", "process.cwd", "process.argv", "process.execArgv", diff --git a/packages/compiler/src/backend/llvm/shapes.ts b/packages/compiler/src/backend/llvm/shapes.ts index 951ed61c9..d49df20b2 100644 --- a/packages/compiler/src/backend/llvm/shapes.ts +++ b/packages/compiler/src/backend/llvm/shapes.ts @@ -61,64 +61,64 @@ import { LlvmUnsupportedError } from "./unsupported.js"; /** What the tables need from the emitter: the extern-declaration ledger * and the module-wide cycle/shape indexes. */ export interface ShapeHost { - declare(decl: string): void; /** The hierarchy root of a class (a program class or a runtime base), * or undefined for classes outside the class graph. */ classRootOf?(className: string): string | undefined; - /** Request the shared OOM abort helper (@sc_oom) — emitted once. */ - needOom(): void; + /** Byte offset from an object pointer back to ScrCycHdr.buf. */ + readonly cycleBufOffset: number; + declare(decl: string): void; + /** Inline the small-object allocator's fast paths (llvm/alloc.ts). */ + readonly inlineAlloc?: boolean; + /** Record and class fields that store a scalar union's tag and bits + * inline (inline-unions.ts): they own nothing. */ + readonly inlineUnions: InlineUnionFields; /** The immutable ScrMapOps descriptor (scr_map.h) of a Map or Set type: * a module constant holding the LLVM operands { key retain, key release, * key trace, value retain, value release, value trace, union key kinds }, * or `null` when every operand is null. */ mapOps(operands: readonly string[]): string; - /** The target C ABI's `size_t` / refcount integer type. */ - readonly sizeType: "i32" | "i64"; - /** Byte offset from an object pointer back to ScrCycHdr.buf. */ - readonly cycleBufOffset: number; - readonly tracedShapes: Set; - readonly tracedUnions: Set; + /** Request the shared OOM abort helper (@sc_oom) — emitted once. */ + needOom(): void; + /** The next collector statistics index for an emitted ScrCycType + * descriptor (cycTypeDescriptor), counting from FIRST_EMITTED_CYC_KIND + * in emission order. + * Absent: every descriptor gets 0, which keeps no statistics. */ + nextCycKind?(): number; /** Unions represented as a nullable arm pointer (nullable-unions.ts): * their RC and trace entry points are the arm's NULL-tolerant ones. */ readonly nullableUnions: NullableUnions; - /** Record and class fields that store a scalar union's tag and bits - * inline (inline-unions.ts): they own nothing. */ - readonly inlineUnions: InlineUnionFields; - /** The zeroed `%ScrUnion` an inline store reads for a NULL dummy. */ - unionNoneRef(): string; - readonly recordsById: Map; - readonly recordCloneShapes: ReadonlySet; - /** The program constructs lifetime regions: the ProgramLifetime classes - * allocate in regions and count references in their region chunk instead - * of being empty (llvm/regions.ts). */ - regions?: RegionHooks | null; - /** The alias tag of a runtime word that lifetime-region code reads and - * writes outside every field: the chunk count, the chunk owner, the class - * cursors and scr_region_counting (", !tbaa !N", or "" in debug builds). */ - regionAliasTag?(word: "count" | "owner" | "cursor" | "counting"): string; /** Emit the live-object audit notes (`scr_obj_alloc_note` / * `scr_obj_free_note`) in emitted new/free helpers. Only runtimes built * with SCR_RC_AUDIT (the sanitized lane) count them; release and dev * runtime packs define them empty, so plain builds omit the calls. * Absent means emit. */ readonly objectAudit?: boolean; - /** Inline the small-object allocator's fast paths (llvm/alloc.ts). */ - readonly inlineAlloc?: boolean; - /** Worker executables: the inline allocator state is thread-local. */ - readonly threadLocalAlloc?: boolean; /** Inline RC fast-path helpers requested so far (":"); the * emitter defines each once through {@link emitInlineRcHelpers}. Null * keeps every retain/release a runtime call. */ readonly rcHelpers: Set | null; - /** Which fast paths are inlined (see {@link InlineRcMode}). */ - readonly rcMode?: InlineRcMode; /** Define the fast-path helpers `noinline` (see {@link inlineRcOption}). */ readonly rcHelpersNoinline?: boolean; - /** The next collector statistics index for an emitted ScrCycType - * descriptor (cycTypeDescriptor), counting from FIRST_EMITTED_CYC_KIND - * in emission order. - * Absent: every descriptor gets 0, which keeps no statistics. */ - nextCycKind?(): number; + /** Which fast paths are inlined (see {@link InlineRcMode}). */ + readonly rcMode?: InlineRcMode; + readonly recordCloneShapes: ReadonlySet; + readonly recordsById: Map; + /** The alias tag of a runtime word that lifetime-region code reads and + * writes outside every field: the chunk count, the chunk owner, the class + * cursors and scr_region_counting (", !tbaa !N", or "" in debug builds). */ + regionAliasTag?(word: "count" | "owner" | "cursor" | "counting"): string; + /** The program constructs lifetime regions: the ProgramLifetime classes + * allocate in regions and count references in their region chunk instead + * of being empty (llvm/regions.ts). */ + regions?: RegionHooks | null; + /** The target C ABI's `size_t` / refcount integer type. */ + readonly sizeType: "i32" | "i64"; + /** Worker executables: the inline allocator state is thread-local. */ + readonly threadLocalAlloc?: boolean; + readonly tracedShapes: Set; + readonly tracedUnions: Set; + /** The zeroed `%ScrUnion` an inline store reads for a NULL dummy. */ + unionNoneRef(): string; } /** What class emission asks of lifetime regions (LifetimeRegions in diff --git a/packages/compiler/src/backend/may-throw.ts b/packages/compiler/src/backend/may-throw.ts index f07bbfa78..b72dc545d 100644 --- a/packages/compiler/src/backend/may-throw.ts +++ b/packages/compiler/src/backend/may-throw.ts @@ -297,7 +297,9 @@ export function computeMayThrow( rec.method === "replaceAll" || rec.method === "split" || rec.method === "matchAll" || - rec.method === "matchAllInto" + rec.method === "matchAllCaptures" || + rec.method === "matchAllInto" || + rec.method === "matchAllCapturesInto" ) { f.throws = true; } diff --git a/packages/compiler/src/frontend/lowering/containers/bytes.ts b/packages/compiler/src/frontend/lowering/containers/bytes.ts index c41bf5163..91cf0b263 100644 --- a/packages/compiler/src/frontend/lowering/containers/bytes.ts +++ b/packages/compiler/src/frontend/lowering/containers/bytes.ts @@ -35,6 +35,7 @@ import { } from "../optional-arguments.js"; import { lowerCheckedArrayFrom } from "./array-construction.js"; import { lowerToNumberArgument } from "../lower-exprs.js"; +import { lowerBytesIteratorValue } from "../iterator-adapters.js"; /** TypedArray.prototype.sort and toSorted. The receiver and comparator * expressions are evaluated before entering the helper (buildBytesSortFn). @@ -462,6 +463,21 @@ export function lowerBytesMethodCall( if (name === "sort" || name === "toSorted") { return lowerBytesSortCall(lowerer, call, access, receiverIr); } + // values()/keys()/entries() as iterator objects (for...of heads and + // stored numeric iterators lower before this). + if ((name === "values" || name === "keys" || name === "entries") && nArgs === 0) { + const type = lowerer.mapTypeOf(lowerer.typeOf(call)); + if (type?.kind === "generator") { + const value = lowerBytesIteratorValue( + lowerer, + lowerer.lowerExpr(access.expression), + name, + type, + loc, + ); + if (value) return value; + } + } if (name === "toReversed") { if (nArgs !== 0) { lowerer.noLowering(`.toReversed with ${nArgs} arguments on typed arrays`, call); diff --git a/packages/compiler/src/frontend/lowering/containers/iterator-helpers.ts b/packages/compiler/src/frontend/lowering/containers/iterator-helpers.ts index 7cb3753ac..79b91af64 100644 --- a/packages/compiler/src/frontend/lowering/containers/iterator-helpers.ts +++ b/packages/compiler/src/frontend/lowering/containers/iterator-helpers.ts @@ -5,6 +5,7 @@ import type { Lowerer } from "../lowerer.js"; import { BOOL, CAUGHT, + DYN, F64, type IrMapIntrinsicMethod, type IrSetIntrinsicMethod, @@ -28,12 +29,19 @@ import { concatStrings, throwTypeError } from "./dynamic-receivers.js"; import { type GenType, generatorLoopBinding, + resultRecordOf, genStep, genValue, stepDrivable, } from "../lower-generators.js"; import { exitBeforeReturns } from "./for-of.js"; -import { iteratorSlotAdapter } from "../iterator-adapters.js"; +import { + iteratorSlotAdapter, + lowerArrayIteratorValue, + lowerBytesIteratorValue, + lowerCollectionIteratorValue, + lowerStringIteratorValue, +} from "../iterator-adapters.js"; import { lowerArrayCallback } from "./callback-arguments.js"; /* ── iterator helpers (ES2025) ─────────────────────────────────────────── */ @@ -150,15 +158,45 @@ export function lowerGeneratorHelperCall( if (call.arguments.length !== 1 || argNode === undefined || ts.isSpreadElement(argNode)) { lowerer.noLowering(`.${name} with ${call.arguments.length} arguments`, call); } + // A stage on m.keys()/values()/entries() (or a Set's) walks the + // collection inside the helper's own generator. + const collection = collectionIteratorCall(lowerer, access.expression); + if (collection !== null) { + const coll = lowerer.lowerExpr(collection.receiver); + if (coll.type.kind !== "map" && coll.type.kind !== "set") + lowerer.badType(collection.receiver, lowerer.typeOf(collection.receiver)); + const source: ChainSource = { kind: "collection", type: coll.type, proj: collection.proj }; + const elem = collectionSeed(lowerer, source); + const { stage, out } = lowerStage(lowerer, { name, argNode, access }, elem, loc); + return lowerHelperObject(lowerer, call, coll, source, elem, stage, out, loc); + } const src = lowerHelperSource(lowerer, access.expression); if (name !== "take" && name !== "drop") { const { stage, out } = lowerStage(lowerer, { name, argNode, access }, src.elem, loc); - return lowerHelperObject(lowerer, call, src.gen, src.elem, stage, out, loc); + return lowerHelperObject( + lowerer, + call, + src.gen, + { kind: "generator", type: src.gen.type, elem: src.elem }, + src.elem, + stage, + out, + loc, + ); } // The budget check closes the source, so the source is bound first. const bound = bindSource(lowerer, src.gen, loc); const { stage, out } = lowerStage(lowerer, { name, argNode, access }, src.elem, loc, bound.ref); - const helper = lowerHelperObject(lowerer, call, bound.ref, src.elem, stage, out, loc); + const helper = lowerHelperObject( + lowerer, + call, + bound.ref, + { kind: "generator", type: src.gen.type, elem: src.elem }, + src.elem, + stage, + out, + loc, + ); return { kind: "seqExpr", stmts: [bound.decl], result: helper, type: helper.type, loc }; } // The stages before the terminal, back to the first receiver that is @@ -189,6 +227,24 @@ export function lowerGeneratorHelperCall( collectionSeed(lowerer, source), ); } + // Iterator.from(arr) walks the array by index. + const arrayRoot = arrayIteratorCall(lowerer, cur); + if (arrayRoot !== null) { + const items = lowerer.lowerExpr(arrayRoot.receiver); + if (items.type.kind !== "array") + lowerer.badType(arrayRoot.receiver, lowerer.typeOf(arrayRoot.receiver)); + const elem = + arrayRoot.proj === "entries" ? entryPairType(lowerer, items.type.elem) : items.type.elem; + return lowerFusedChain( + lowerer, + call, + name, + stages, + { kind: "array", type: items.type, proj: arrayRoot.proj }, + items, + elem, + ); + } const src = lowerHelperSource(lowerer, cur); return lowerFusedChain( lowerer, @@ -201,6 +257,125 @@ export function lowerGeneratorHelperCall( ); } +/** `h.return!()` or `h.return?.()` on a helper object. The lib declares + * return() as optional on iterator objects, and a helper object always + * has one, so both spellings are the call: the helper completes and closes + * its source, and the result is `{ value: undefined, done: true }`. The + * receiver must be provably a helper object — a helper stage call, or a + * const binding initialized with one — since other iterator objects (the + * array, string, Map and Set iterators) have no return(). Null otherwise, + * and for the plain `h.return()` spelling (lowerGenMethodCall's). */ +export function lowerHelperReturnCall(lowerer: Lowerer, call: ts.CallExpression): IrExpr | null { + let callee: ts.Expression = call.expression; + const nonNull = ts.isNonNullExpression(callee); + if (nonNull) callee = (callee as ts.NonNullExpression).expression; + if (!nonNull && call.questionDotToken === undefined) return null; + if (!ts.isPropertyAccessExpression(callee) || callee.questionDotToken) return null; + if (callee.name.text !== "return" || call.arguments.length !== 0) return null; + if (!isHelperObject(lowerer, callee.expression) || !lowerer.isStdlibMember(callee)) return null; + const callT = lowerer.mapTypeOf(lowerer.typeOf(call)); + const gen = lowerer.lowerExpr(callee.expression); + if (gen.type.kind !== "generator" || gen.type.async) return null; + const loc = locOf(call); + const recT = resultRecordOf(lowerer, gen.type); + const resume: IrExpr = { kind: "genResume", mode: "return", gen, arg: null, type: recT, loc }; + // `?.` types the call `IteratorResult | undefined`, which has no mapping + // as a whole; the result is never undefined, so it is the record itself. + if (callT === null || typeEquals(callT, recT)) return resume; + const result = lowerer.coerceToExpected(resume, callT); + if (!typeEquals(result.type, callT)) lowerer.badType(call, lowerer.typeOf(call)); + return result; +} + +/** Whether `expr` is a helper stage call, or a const binding initialized + * with one. */ +function isHelperObject(lowerer: Lowerer, expr: ts.Expression): boolean { + while (ts.isParenthesizedExpression(expr)) expr = expr.expression; + if (isStageCall(lowerer, expr)) return true; + if (!ts.isIdentifier(expr)) return false; + const symbol = lowerer.resolveValueSymbol(expr); + const decl = symbol ? lowerer.checker.valueDeclarationOf(symbol) : undefined; + if ( + !decl || + !ts.isVariableDeclaration(decl) || + decl.initializer === undefined || + !ts.isVariableDeclarationList(decl.parent) || + (decl.parent.flags & ts.NodeFlags.Const) === 0 + ) + return false; + let init: ts.Expression = decl.initializer; + while (ts.isParenthesizedExpression(init)) init = init.expression; + return isStageCall(lowerer, init); +} + +/** `Iterator.from(value)` over a typed built-in iterable: the value's own + * iterator, which already inherits from Iterator.prototype, so Node returns + * it rather than a wrapper. A generator (including a helper object or an + * array iterator) is itself; an array, string or typed array gives its + * values iterator; a Set its values and a Map its entries, as a generator + * over the live collection. The result converts into the call's + * IteratorObject type. Null for other arguments (objects with their own + * next method, untyped values), which keep the Iterator fence. */ +export function lowerIteratorFromCall( + lowerer: Lowerer, + call: ts.CallExpression, + access: ts.PropertyAccessExpression, +): IrExpr | null { + if (access.name.text !== "from" || call.arguments.length !== 1) return null; + const argNode = call.arguments[0]!; + if (ts.isSpreadElement(argNode)) return null; + const resultT = lowerer.mapTypeOf(lowerer.typeOf(call)); + const argT = lowerer.mapTypeOf(lowerer.typeOf(argNode)); + if (resultT?.kind !== "generator" || resultT.async || argT === null) return null; + const iterableKinds = ["generator", "array", "string", "bytes", "set", "map"]; + if (!iterableKinds.includes(argT.kind) || (argT.kind === "generator" && argT.async)) return null; + const loc = locOf(call); + const value = lowerer.lowerExpr(argNode); + const own = (yieldT: IrType): GenType => ({ + kind: "generator", + yieldT, + retT: VOID, + nextT: UNDEFINED_T, + }); + let iterator: IrExpr | null = null; + const t = value.type; + if (t.kind === "generator") iterator = value; + else if (t.kind === "array") + iterator = lowerArrayIteratorValue(lowerer, value, "values", own(t.elem), loc); + else if (t.kind === "string") + iterator = lowerStringIteratorValue(lowerer, value, own(STRING), loc); + else if (t.kind === "bytes") + iterator = lowerBytesIteratorValue(lowerer, value, "values", own(F64), loc); + else if (t.kind === "set" || t.kind === "map") { + const elem = t.kind === "set" ? t.elem : entryPairTypeOf(lowerer, t.key, t.value); + iterator = lowerCollectionIteratorValue( + lowerer, + value, + t.kind === "set" ? "values" : "entries", + own(elem), + loc, + ); + } + if (iterator === null) lowerer.badType(argNode, lowerer.typeOf(argNode)); + const result = lowerer.coerceToExpected(iterator, resultT); + if (!typeEquals(result.type, resultT)) lowerer.badType(call, lowerer.typeOf(call)); + return result; +} + +/** The interned [K, V] pair a Map's entries yield. */ +function entryPairTypeOf(lowerer: Lowerer, keyT: IrType, valueT: IrType): IrType { + return { + kind: "record", + shapeId: lowerer.shapes.intern( + [ + { name: "0", type: keyT }, + { name: "1", type: valueT }, + ], + true, + ), + }; +} + const ITER_STAGES = new Set(["map", "filter", "take", "drop", "flatMap"]); const ITER_TERMINALS = new Set(["toArray", "forEach", "reduce", "some", "every", "find"]); /** Native iterators that ride checked runtime values and adapt into @@ -353,12 +528,30 @@ export function lowerForOfHelperChain( } } -/** `arr.values()` or `arr.entries()` on an array value, as the root of a - * helper chain. */ +/** The argument of an `Iterator.from(x)` call, or null. As a chain's root, + * the call is the argument's own iterator, so the chain can walk the + * argument directly. */ +function iteratorFromArgument(lowerer: Lowerer, expr: ts.Expression): ts.Expression | null { + if (!ts.isCallExpression(expr) || !ts.isPropertyAccessExpression(expr.expression)) return null; + if (expr.questionDotToken || expr.expression.questionDotToken || expr.arguments.length !== 1) + return null; + const arg = expr.arguments[0]!; + if (ts.isSpreadElement(arg) || expr.expression.name.text !== "from") return null; + if (!lowerer.isStdlibGlobal(expr.expression.expression, "Iterator")) return null; + return lowerer.isStdlibMember(expr.expression) ? arg : null; +} + +/** `arr.values()`, `arr.entries()` or `Iterator.from(arr)` on an array value, + * as the root of a helper chain. */ function arrayIteratorCall( lowerer: Lowerer, expr: ts.Expression, ): { receiver: ts.Expression; proj: "values" | "entries" } | null { + const fromArg = iteratorFromArgument(lowerer, expr); + if (fromArg !== null) + return lowerer.mapTypeOf(lowerer.typeOf(fromArg))?.kind === "array" + ? { receiver: fromArg, proj: "values" } + : null; if (!ts.isCallExpression(expr) || !ts.isPropertyAccessExpression(expr.expression)) return null; if (expr.questionDotToken || expr.expression.questionDotToken || expr.arguments.length !== 0) return null; @@ -401,6 +594,17 @@ function collectionIteratorCall( lowerer: Lowerer, expr: ts.Expression, ): { receiver: ts.Expression; proj: "keys" | "values" | "entries" } | null { + // Iterator.from(set) is the Set's values iterator, Iterator.from(map) the + // Map's entries iterator. + const fromArg = iteratorFromArgument(lowerer, expr); + if (fromArg !== null) { + const kind = lowerer.mapTypeOf(lowerer.typeOf(fromArg))?.kind; + return kind === "set" + ? { receiver: fromArg, proj: "values" } + : kind === "map" + ? { receiver: fromArg, proj: "entries" } + : null; + } if (!ts.isCallExpression(expr) || !ts.isPropertyAccessExpression(expr.expression)) return null; if (expr.questionDotToken || expr.expression.questionDotToken || expr.arguments.length !== 0) return null; @@ -480,6 +684,11 @@ function lowerHelperSource( lowerer: Lowerer, expr: ts.Expression, ): { gen: IrExpr & { type: GenType }; elem: IrType } { + // Iterator.from(gen) is the generator itself; the call's IteratorObject + // type would add an adapter generator over it. + const fromArg = iteratorFromArgument(lowerer, expr); + if (fromArg !== null && lowerer.mapTypeOf(lowerer.typeOf(fromArg))?.kind === "generator") + return lowerHelperSource(lowerer, fromArg); const tsType = lowerer.typeOf(expr); const mapped = lowerer.mapTypeOf(tsType); const arg = lowerer.checker.getTypeArguments(tsType as ts.TypeReference)[0]; @@ -505,6 +714,14 @@ function lowerHelperSource( ) lowerer.badType(expr, tsType); const target: GenType = { kind: "generator", yieldT, retT: VOID, nextT: UNDEFINED_T }; + // m.keys()/values()/entries() (or a Set's) on a typed collection: a + // generator over the collection with typed reads. + const collection = collectionIteratorCall(lowerer, expr); + if (collection !== null) { + const coll = lowerer.lowerExpr(collection.receiver); + const walk = lowerCollectionIteratorValue(lowerer, coll, collection.proj, target, locOf(expr)); + if (walk !== null) return { gen: walk as IrExpr & { type: GenType }, elem: yieldT }; + } const adapted = iteratorSlotAdapter(lowerer, lowerer.lowerExpr(expr), target); if (adapted === null || adapted.type.kind !== "generator") lowerer.badType(expr, tsType); return { gen: adapted as IrExpr & { type: GenType }, elem: yieldT }; @@ -577,10 +794,17 @@ function lowerStage( if (ret.kind === "array") return { stage: { kind: "flatMap", fn }, out: ret.elem }; if (ret.kind === "generator" && ret.async !== true && stepDrivable(ret)) return { stage: { kind: "flatMap", fn }, out: ret.yieldT }; + // A Set flattens its values and a Map its entries, walked live. A string + // is rejected at run time with Node's TypeError (strings are not + // iterator objects). + if (ret.kind === "set") return { stage: { kind: "flatMap", fn }, out: ret.elem }; + if (ret.kind === "map") + return { stage: { kind: "flatMap", fn }, out: entryPairTypeOf(lowerer, ret.key, ret.value) }; + if (ret.kind === "string") return { stage: { kind: "flatMap", fn }, out: STRING }; lowerer.noLowering( `.flatMap over a callback returning '${lowerer.fmt(ret)}'`, s.argNode, - "callbacks returning an array or a generator are the supported forms", + "callbacks returning an array, a generator, a Set or a Map are the supported forms", ); } @@ -749,7 +973,8 @@ function lowerFusedChain( function lowerHelperObject( lowerer: Lowerer, call: ts.CallExpression, - src: IrExpr & { type: GenType }, + src: IrExpr, + source: (ChainSource & { kind: "generator" }) | (ChainSource & { kind: "collection" }), srcElem: IrType, stage: StageIr, out: IrType, @@ -773,21 +998,23 @@ function lowerHelperObject( "the helper's element type must be representable — annotate the callback's types", ); } - if (!stepDrivable(src.type)) { + if (source.kind === "generator" && !stepDrivable(source.type)) { lowerer.noLowering( - `.${stage.kind} over an iterator yielding '${lowerer.fmt(src.type.yieldT)}'`, + `.${stage.kind} over an iterator yielding '${lowerer.fmt(source.type.yieldT)}'`, call, "helpers over iterators of numbers, booleans, strings, arrays, records and class instances are the supported forms", ); } const arg = "budget" in stage ? stage.budget : stage.fn; - const key = `iterHelper:${stage.kind}:${typeKey(src.type)}:${typeKey(srcElem)}:${typeKey(arg.type)}:${typeKey(outT)}`; + const srcKey = + source.kind === "collection" ? `${source.proj}:${typeKey(source.type)}` : typeKey(source.type); + const key = `iterHelper:${stage.kind}:${srcKey}:${typeKey(srcElem)}:${typeKey(arg.type)}:${typeKey(outT)}`; let name = lowerer.arrHofHelpers.get(key); if (!name) { name = `%iter.${stage.kind}.${lowerer.arrHofHelpers.size}`; lowerer.arrHofHelpers.set(key, name); lowerer.liftedFns.push( - buildHelperGenFn(lowerer, name, src.type, srcElem, stage, arg.type, outT, resultType, loc), + buildHelperGenFn(lowerer, name, source, srcElem, stage, arg.type, outT, resultType, loc), ); } return { kind: "call", callee: name, args: [src, arg], type: outT, loc }; @@ -1079,6 +1306,107 @@ function driveGenerator( ]; } +/** Statements that walk the Map or Set in local `collId` from inside a + * helper's generator: each pass runs `pre` (which may `break`), takes the + * next live entry index from the runtime's native iterator, which keeps + * the walk live and ends it when exhausted or released, and runs `body` + * over the projected element read with the typed intrinsics. */ +function driveCollectionNative( + lowerer: Lowerer, + frame: ChainFrame, + collId: string, + source: ChainSource & { kind: "collection" }, + pre: IrStmt[], + body: (value: IrExpr) => IrStmt[], + loc: SrcLoc, +): IrStmt[] { + const t = source.type; + const coll = (): IrExpr => varRef(collId, t, loc); + const nativeId = frame.local("native", DYN, false); + const iId = frame.local("i", F64, false); + const read = (method: "iterKey" | "iterValue", type: IrType): IrExpr => + t.kind === "map" + ? { kind: "mapIntrinsic", method, receiver: coll(), args: [varRef(iId, F64, loc)], type, loc } + : { + kind: "setIntrinsic", + method: "iterKey", + receiver: coll(), + args: [varRef(iId, F64, loc)], + type, + loc, + }; + const keyT = t.kind === "map" ? t.key : t.elem; + const valueT = t.kind === "map" ? t.value : t.elem; + const seedT = collectionSeed(lowerer, source); + const seed: IrExpr = + source.proj === "entries" + ? { + kind: "recordLit", + fields: [ + { name: "0", value: read("iterKey", keyT) }, + { name: "1", value: read("iterValue", valueT) }, + ], + type: seedT, + loc, + } + : source.proj === "values" && t.kind === "map" + ? read("iterValue", valueT) + : read("iterKey", keyT); + const vId = frame.local("v", seedT, false); + return [ + { + kind: "varDecl", + localId: nativeId, + init: { + kind: "dynInvoke", + recv: lowerer.coerceToExpected(coll(), DYN), + method: "values", + calleeName: t.kind === "map" ? "map.values" : "set.values", + args: [], + type: DYN, + loc, + }, + loc, + }, + { + kind: "while", + cond: boolLit(true, loc), + body: [ + ...pre, + { + kind: "varDecl", + localId: iId, + init: { + kind: "libCall", + fn: "dyn.collectionIteratorIndex", + args: [varRef(nativeId, DYN, loc)], + type: F64, + loc, + }, + loc, + }, + { + kind: "if", + cond: { + kind: "bin", + op: "<", + left: varRef(iId, F64, loc), + right: numLit(0, loc), + type: BOOL, + loc, + }, + then: [{ kind: "break", loc }], + else_: null, + loc, + }, + { kind: "varDecl", localId: vId, init: seed, loc }, + ...body(varRef(vId, seedT, loc)), + ], + loc, + }, + ]; +} + /** `for (j = 0; j < arr.length && !stop; j++) body(arr[j])` over a live * array local (the array iterator's per-step length read). */ function walkArray( @@ -1245,8 +1573,9 @@ function walkCollection( } /** A flatMap stage's inner walk over the callback's result in local - * `innerId`: an array by index, or a generator driven (and closed on - * early exit) like the source. `stop` ends the walk early. */ + * `innerId`: an array by index, a Set's values or a Map's entries walked + * live, or a generator driven (and closed on early exit) like the source. + * A string result throws Node's TypeError. `stop` ends the walk early. */ function walkInner( lowerer: Lowerer, frame: ChainFrame, @@ -1257,6 +1586,27 @@ function walkInner( loc: SrcLoc, ): IrStmt[] { if (innerT.kind === "array") return [walkArray(frame, innerId, innerT, stop, body, loc)]; + if (innerT.kind === "set" || innerT.kind === "map") { + const source: ChainSource = { + kind: "collection", + type: innerT, + proj: innerT.kind === "set" ? "values" : "entries", + }; + return walkCollection(lowerer, frame, innerId, source, stop ?? boolLit(false, loc), body, loc); + } + if (innerT.kind === "string") { + return [ + throwTypeError( + { + kind: "strLit", + value: "Iterator.prototype.flatMap called on non-object", + type: STRING, + loc, + }, + loc, + ), + ]; + } if (innerT.kind !== "generator") throw new InternalCompilerError("lowerer bug: flatMap over a non-iterable result"); const pre: IrStmt[] = @@ -1768,7 +2118,7 @@ function buildIterChainFn( function buildHelperGenFn( lowerer: Lowerer, name: string, - srcT: GenType, + source: (ChainSource & { kind: "generator" }) | (ChainSource & { kind: "collection" }), srcElem: IrType, stage: StageIr, argT: IrType, @@ -1777,8 +2127,12 @@ function buildHelperGenFn( loc: SrcLoc, ): IrFunction { const frame = new LiftedFrame(loc); - const srcId = frame.param("src", srcT); + const srcId = frame.param("src", source.type); const argId = frame.param(stage.kind === "take" || stage.kind === "drop" ? "n" : "f", argT); + const drive = (pre: IrStmt[], each: (value: IrExpr) => IrStmt[], at: SrcLoc): IrStmt[] => + source.kind === "generator" + ? driveGenerator(lowerer, frame, srcId, source.type, srcElem, pre, each, at) + : driveCollectionNative(lowerer, frame, srcId, source, pre, each, at); const yieldOf = (value: IrExpr): IrStmt => ({ kind: "exprStmt", expr: { @@ -1804,12 +2158,7 @@ function buildHelperGenFn( const n = (): IrExpr => varRef(argId, F64, loc); if (stage.kind === "map") { body.push( - ...driveGenerator( - lowerer, - frame, - srcId, - srcT, - srcElem, + ...drive( [], (value) => { const mId = frame.local("m", fnT!.ret, false); @@ -1824,12 +2173,7 @@ function buildHelperGenFn( ); } else if (stage.kind === "filter") { body.push( - ...driveGenerator( - lowerer, - frame, - srcId, - srcT, - srcElem, + ...drive( [], (value) => { const okId = frame.local("ok", BOOL, false); @@ -1860,28 +2204,12 @@ function buildHelperGenFn( loc, }, ]; - body.push( - ...driveGenerator( - lowerer, - frame, - srcId, - srcT, - srcElem, - pre, - (value) => [yieldOf(value)], - loc, - ), - ); + body.push(...drive(pre, (value) => [yieldOf(value)], loc)); } else if (stage.kind === "drop") { const droppedId = frame.local("dropped", F64, true); body.push({ kind: "varDecl", localId: droppedId, init: numLit(0, loc), loc }); body.push( - ...driveGenerator( - lowerer, - frame, - srcId, - srcT, - srcElem, + ...drive( [], (value) => [ { @@ -1905,12 +2233,7 @@ function buildHelperGenFn( ); } else { body.push( - ...driveGenerator( - lowerer, - frame, - srcId, - srcT, - srcElem, + ...drive( [], (value) => { const innerT = fnT!.ret; @@ -1932,7 +2255,14 @@ function buildHelperGenFn( returnType: VOID, locals: frame.locals, body, - generator: { yieldT: outT.yieldT, nextT: outT.nextT, resultType, closesSource: true }, + generator: { + yieldT: outT.yieldT, + nextT: outT.nextT, + resultType, + tag: "Iterator Helper", + // A native Map or Set iterator has no return() to call. + ...(source.kind === "generator" ? { closesSource: true as const } : {}), + }, loc, }; } diff --git a/packages/compiler/src/frontend/lowering/containers/match-row-uses.ts b/packages/compiler/src/frontend/lowering/containers/match-row-uses.ts new file mode 100644 index 000000000..387ff81d3 --- /dev/null +++ b/packages/compiler/src/frontend/lowering/containers/match-row-uses.ts @@ -0,0 +1,299 @@ +import * as ts from "../../ts7/adapter.js"; +import type { Lowerer } from "../lowerer.js"; +import { identifiersNamed } from "../identifier-uses.js"; +import { bindingNeverReassigned } from "../binding-analysis.js"; +import { matchCallbackParamGroupsOf, matchResultNamedGroupsOf } from "../lower-exprs.js"; + +/** Whether the rows a matchAll drain produces are read only through their + * elements: element reads and `in` tests with number keys, `.length`, the compile-time + * `.groups` projection, `{ groups }` destructuring and, on a for-of binding + * over the drain itself (`companion`), the companion-index `.index`. Rows + * reach these reads through for-of loops, spreads, `Array.from`, stored + * const bindings, `filter`/`slice`/`toReversed` copies, `find`/`findLast`/ + * `at` picks and inline callbacks of the array methods below. No such read + * can observe a row's `index`, `input` or `groups` properties, so the + * drain leaves them out. False for any other use. */ +export function matchAllRowsReadByElements( + lowerer: Lowerer, + drain: ts.Expression, + companion: boolean, +): boolean { + const parent = outerValue(drain).parent; + if ( + ts.isCallExpression(parent) && + parent.arguments[0] === outerValue(drain) && + ts.isPropertyAccessExpression(parent.expression) && + parent.expression.name.text === "from" && + lowerer.isStdlibGlobal(parent.expression.expression, "Array") + ) { + const mapper = parent.arguments[1]; + if (mapper === undefined) return rowsReadByElements(lowerer, parent, 0); + return parent.arguments.length === 2 && callbackReadsByElements(lowerer, mapper, 2); + } + if (ts.isForOfStatement(parent) && parent.expression === outerValue(drain)) + return forOfReadsByElements(lowerer, parent, companion); + return rowsReadByElements(lowerer, drain, 0); +} + +/** matchAllRowsReadByElements for a stored drain, `const rows = + * s.matchAll(re)`: a const-binding for-of loop over `rows` in the same + * function walks the companion indices, so its binding may also read + * `.index`. */ +export function storedMatchAllRowsReadByElements( + lowerer: Lowerer, + decl: ts.VariableDeclaration, +): boolean { + const scope = enclosingFunction(decl); + return constBindingReadsByElements(lowerer, decl, (use) => { + const parent = outerValue(use).parent; + if ( + ts.isForOfStatement(parent) && + parent.expression === outerValue(use) && + ts.isVariableDeclarationList(parent.initializer) && + (parent.initializer.flags & ts.NodeFlags.Const) !== 0 && + enclosingFunction(parent) === scope + ) + return forOfReadsByElements(lowerer, parent, true); + return rowsReadByElements(lowerer, use, 1); + }); +} + +function enclosingFunction(node: ts.Node): ts.Node | undefined { + let at = node.parent; + while (at !== undefined && !ts.isFunctionLike(at)) at = at.parent; + return at; +} + +/** Array methods whose first argument is a callback over the rows. */ +const ROW_CALLBACK_METHODS = new Set([ + "map", + "forEach", + "filter", + "some", + "every", + "find", + "findLast", + "findIndex", + "findLastIndex", + "flatMap", +]); + +/** Array methods whose result holds rows of the receiver. */ +const ROW_SUBSET_METHODS = new Set(["filter", "slice", "toReversed"]); + +/** Array methods that return one row of the receiver. */ +const ROW_PICK_METHODS = new Set(["find", "findLast", "at"]); + +/** Bounds the walk through stored bindings and copies. */ +const MAX_DEPTH = 8; + +function outerValue(node: ts.Node): ts.Node { + let ref = node; + while (ts.isParenthesizedExpression(ref.parent) || ts.isNonNullExpression(ref.parent)) + ref = ref.parent; + return ref; +} + +/** `node` evaluates to an array of rows. */ +function rowsReadByElements(lowerer: Lowerer, node: ts.Node, depth: number): boolean { + if (depth > MAX_DEPTH) return false; + const ref = outerValue(node); + const parent = ref.parent; + if (ts.isSpreadElement(parent)) { + const literal = parent.parent; + return ( + ts.isArrayLiteralExpression(literal) && + literal.elements.length === 1 && + rowsReadByElements(lowerer, literal, depth + 1) + ); + } + if (ts.isForOfStatement(parent) && parent.expression === ref) + return forOfReadsByElements(lowerer, parent, false); + if (ts.isVariableDeclaration(parent) && parent.initializer === ref) + return constBindingReadsByElements(lowerer, parent, (use) => + rowsReadByElements(lowerer, use, depth + 1), + ); + if (ts.isElementAccessExpression(parent) && parent.expression === ref) + return numberKeyRead(lowerer, parent) && rowReadByElements(lowerer, parent, depth + 1); + if (!ts.isPropertyAccessExpression(parent) || parent.expression !== ref) return false; + if (parent.questionDotToken !== undefined || !lowerer.isStdlibMember(parent)) return false; + const method = parent.name.text; + const call = parent.parent; + if (!ts.isCallExpression(call) || call.expression !== parent) + return method === "length" && isValueRead(parent); + if (ROW_CALLBACK_METHODS.has(method)) { + if (call.arguments.length !== 1 || !callbackReadsByElements(lowerer, call.arguments[0]!, 2)) + return false; + } else if (ROW_SUBSET_METHODS.has(method) || ROW_PICK_METHODS.has(method)) { + if (call.arguments.some((a) => ts.isSpreadElement(a))) return false; + } else { + return false; + } + if (ROW_SUBSET_METHODS.has(method)) return rowsReadByElements(lowerer, call, depth + 1); + if (ROW_PICK_METHODS.has(method)) return rowReadByElements(lowerer, call, depth + 1); + return true; +} + +/** `node` evaluates to one row. */ +function rowReadByElements(lowerer: Lowerer, node: ts.Node, depth: number): boolean { + if (depth > MAX_DEPTH) return false; + const ref = outerValue(node); + const parent = ref.parent; + if (ts.isExpressionStatement(parent)) return true; + if (ts.isElementAccessExpression(parent) && parent.expression === ref) + return numberKeyRead(lowerer, parent); + // `1 in m`: a number key tests an element. + if ( + ts.isBinaryExpression(parent) && + parent.operatorToken.kind === ts.SyntaxKind.InKeyword && + parent.right === ref + ) + return lowerer.mapTypeOf(lowerer.typeOf(parent.left))?.kind === "f64"; + if (ts.isPropertyAccessExpression(parent) && parent.expression === ref) { + if (parent.questionDotToken !== undefined || !isValueRead(parent)) return false; + if (parent.name.text === "length") return true; + return parent.name.text === "groups" && groupsProjected(lowerer, ref as ts.Expression); + } + if (ts.isVariableDeclaration(parent) && parent.initializer === ref) { + if (ts.isObjectBindingPattern(parent.name)) + return groupsPattern(parent.name) && groupsProjected(lowerer, ref as ts.Expression); + return constBindingReadsByElements(lowerer, parent, (use) => + rowReadByElements(lowerer, use, depth + 1), + ); + } + return false; +} + +/** A for-of loop over rows: its binding is read by elements, or by its + * companion `.index` when the loop walks the drain itself. */ +function forOfReadsByElements( + lowerer: Lowerer, + loop: ts.ForOfStatement, + companion: boolean, +): boolean { + const list = loop.initializer; + if (!ts.isVariableDeclarationList(list) || list.declarations.length !== 1) return false; + const decl = list.declarations[0]!; + if (!ts.isIdentifier(decl.name)) return false; + const symbol = lowerer.resolveValueSymbol(decl.name); + if (!symbol || !bindingNeverReassigned(lowerer, symbol, decl)) return false; + return usesOf(lowerer, decl.name, symbol).every((use) => { + const parent = outerValue(use).parent; + if ( + companion && + ts.isPropertyAccessExpression(parent) && + parent.name.text === "index" && + parent.questionDotToken === undefined && + isValueRead(parent) + ) + return true; + return rowReadByElements(lowerer, use, 1); + }); +} + +/** An inline callback whose first parameter (a row) is read by elements. + * The callback may name at most `maxParams` parameters, which keeps the + * array argument (the rows themselves) out of reach. */ +function callbackReadsByElements( + lowerer: Lowerer, + node: ts.Expression, + maxParams: number, +): boolean { + let fn: ts.Node = node; + while (ts.isParenthesizedExpression(fn)) fn = fn.expression; + if (!ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) return false; + if (fn.parameters.length > maxParams) return false; + const param = fn.parameters[0]; + if (param === undefined) return true; + if (param.dotDotDotToken !== undefined || param.initializer !== undefined) return false; + if (ts.isObjectBindingPattern(param.name)) + return groupsPattern(param.name) && groupsProjectedParam(lowerer, param); + if (!ts.isIdentifier(param.name)) return false; + const symbol = lowerer.checker.getSymbolAtLocation(param.name); + if (!symbol || !bindingNeverReassigned(lowerer, symbol, param)) return false; + return usesOf(lowerer, param.name, symbol).every((use) => rowReadByElements(lowerer, use, 1)); +} + +/** Every use of a non-exported const binding satisfies `read`. */ +function constBindingReadsByElements( + lowerer: Lowerer, + decl: ts.VariableDeclaration, + read: (use: ts.Identifier) => boolean, +): boolean { + if ( + !ts.isIdentifier(decl.name) || + decl.type !== undefined || + !ts.isVariableDeclarationList(decl.parent) || + (decl.parent.flags & ts.NodeFlags.Const) === 0 + ) + return false; + const statement = decl.parent.parent; + if ( + ts.isVariableStatement(statement) && + statement.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword) + ) + return false; + const symbol = lowerer.resolveValueSymbol(decl.name); + if (!symbol) return false; + return usesOf(lowerer, decl.name, symbol).every(read); +} + +function usesOf(lowerer: Lowerer, name: ts.Identifier, symbol: ts.Symbol): ts.Identifier[] { + return identifiersNamed(lowerer, name.getSourceFile(), name.text).filter( + (use) => use !== name && lowerer.resolveValueSymbol(use) === symbol, + ); +} + +/** `rows[i]` or `rows?.[i]` with a number key, read and not written. */ +function numberKeyRead(lowerer: Lowerer, access: ts.ElementAccessExpression): boolean { + if (!isValueRead(access)) return false; + const key = lowerer.mapTypeOf(lowerer.typeOf(access.argumentExpression)); + return key?.kind === "f64"; +} + +/** `m.groups` lowers to the compile-time projection over the row's + * elements (lowerMatchGroupsRead). */ +function groupsProjected(lowerer: Lowerer, row: ts.Expression): boolean { + const groups = matchResultNamedGroupsOf(lowerer, row); + return groups !== null && groups.length > 0; +} + +/** `({ groups }) => ...` lowers through the same projection. */ +function groupsProjectedParam(lowerer: Lowerer, param: ts.ParameterDeclaration): boolean { + const groups = matchCallbackParamGroupsOf(lowerer, param); + return groups !== null && groups.length > 0; +} + +/** An object pattern that binds only `groups`. */ +function groupsPattern(pattern: ts.ObjectBindingPattern): boolean { + return ( + pattern.elements.length > 0 && + pattern.elements.every((el) => { + if (el.dotDotDotToken !== undefined) return false; + const prop = el.propertyName; + if (prop === undefined) return ts.isIdentifier(el.name) && el.name.text === "groups"; + return (ts.isIdentifier(prop) || ts.isStringLiteral(prop)) && prop.text === "groups"; + }) + ); +} + +/** A read whose value is only consumed: not an assignment, update or + * delete target. */ +function isValueRead(access: ts.Expression): boolean { + const ref = outerValue(access); + const parent = ref.parent; + if ( + ts.isBinaryExpression(parent) && + parent.left === ref && + parent.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && + parent.operatorToken.kind <= ts.SyntaxKind.LastAssignment + ) + return false; + return !( + (ts.isPrefixUnaryExpression(parent) && + (parent.operator === ts.SyntaxKind.PlusPlusToken || + parent.operator === ts.SyntaxKind.MinusMinusToken)) || + ts.isPostfixUnaryExpression(parent) || + ts.isDeleteExpression(parent) + ); +} diff --git a/packages/compiler/src/frontend/lowering/containers/string-and-regexp.ts b/packages/compiler/src/frontend/lowering/containers/string-and-regexp.ts index 361441636..dd70eb197 100644 --- a/packages/compiler/src/frontend/lowering/containers/string-and-regexp.ts +++ b/packages/compiler/src/frontend/lowering/containers/string-and-regexp.ts @@ -1,3 +1,4 @@ +import { matchAllRowsReadByElements } from "./match-row-uses.js"; import { dynUndefinedExpr, nodeThrowExpr, numLit, strLit, varRef } from "../../../ir/build.js"; import * as ts from "../../ts7/adapter.js"; import { @@ -869,7 +870,7 @@ export function lowerRegexMethodCall( const re = lowerer.lowerExprExpecting(arg0, { kind: "regex" }); return { kind: "regexIntrinsic", - method: "matchAll", + method: matchAllRowsReadByElements(lowerer, call, false) ? "matchAllCaptures" : "matchAll", receiver, args: [re], type: arrayOf(arrayOf(STRING)), diff --git a/packages/compiler/src/frontend/lowering/iterator-adapters.ts b/packages/compiler/src/frontend/lowering/iterator-adapters.ts index fb59c4cee..898c46f20 100644 --- a/packages/compiler/src/frontend/lowering/iterator-adapters.ts +++ b/packages/compiler/src/frontend/lowering/iterator-adapters.ts @@ -258,13 +258,350 @@ function dynIteratorAdapter( returnType: expected.retT, locals, body, - generator: { yieldT: expected.yieldT, nextT: expected.nextT, resultType }, + generator: { yieldT: expected.yieldT, nextT: expected.nextT, resultType, tag: "!" }, loc, }); } return { kind: "call", callee: name, args: [expr], type: expected, loc }; } +/** `m.keys()`, `m.values()` or `m.entries()` (or a Set's) as a source + * generator for an iterator helper object: an interned generator over the + * typed collection that steps the runtime's native iterator by entry + * index and reads each key or value with the typed intrinsics, so values + * keep their types without a checked conversion per step. The native + * iterator owns the live-walk bracket: it ends the iteration when the + * collection is exhausted or when the generator, done or abandoned, + * releases it. `type` yields the key, the value, or the interned [key, + * value] pair. Null when `type` does not match. */ +export function lowerCollectionIteratorValue( + lowerer: Lowerer, + receiver: IrExpr, + method: "keys" | "values" | "entries", + type: IrType, + loc: SrcLoc, +): IrExpr | null { + const collT = receiver.type; + if ((collT.kind !== "map" && collT.kind !== "set") || type.kind !== "generator") return null; + if (type.retT.kind !== "void" || type.nextT.kind !== "undefinedT") return null; + const keyT = collT.kind === "map" ? collT.key : collT.elem; + const valueT = collT.kind === "map" ? collT.value : collT.elem; + const resultType = genResultRecord(type.yieldT, type.retT, lowerer.shapes, lowerer.unions); + if (!resultType) return null; + const key = `collIter:${method}:${typeKey(collT)}:${typeKey(type)}`; + let name = lowerer.valueHelpers.get(key); + if (!name) { + const coll = varRef("coll.0", collT, loc); + const index = varRef("index.0", F64, loc); + const native = varRef("native.0", DYN, loc); + const read = (m: "iterKey" | "iterValue", t: IrType): IrExpr => + collT.kind === "map" + ? { kind: "mapIntrinsic", method: m, receiver: coll, args: [index], type: t, loc } + : { kind: "setIntrinsic", method: "iterKey", receiver: coll, args: [index], type: t, loc }; + let yielded: IrExpr; + if (method === "keys" || (collT.kind === "set" && method === "values")) + yielded = read("iterKey", keyT); + else if (method === "values") yielded = read("iterValue", valueT); + else + yielded = { + kind: "recordLit", + fields: [ + { name: "0", value: read("iterKey", keyT) }, + { name: "1", value: read("iterValue", valueT) }, + ], + type: type.yieldT, + loc, + }; + if (!typeEquals(yielded.type, type.yieldT)) return null; + name = `%coll.iter.${lowerer.valueHelpers.size}`; + lowerer.valueHelpers.set(key, name); + lowerer.liftedFns.push({ + name, + params: [{ localId: "coll.0", name: "coll", type: collT }], + returnType: VOID, + locals: [ + { id: "coll.0", name: "coll", type: collT, mutable: false }, + { id: "native.0", name: "native", type: DYN, mutable: false }, + { id: "index.0", name: "index", type: F64, mutable: true }, + ], + body: [ + { + kind: "varDecl", + localId: "native.0", + init: { + kind: "dynInvoke", + recv: lowerer.coerceToExpected(coll, DYN), + method: "values", + calleeName: collT.kind === "map" ? "map.values" : "set.values", + args: [], + type: DYN, + loc, + }, + loc, + }, + { + kind: "while", + cond: { kind: "boolLit", value: true, type: BOOL, loc }, + body: [ + { + kind: "varDecl", + localId: "index.0", + init: { + kind: "libCall", + fn: "dyn.collectionIteratorIndex", + args: [native], + type: F64, + loc, + }, + loc, + }, + { + kind: "if", + cond: { + kind: "bin", + op: "<", + left: index, + right: { kind: "numLit", value: 0, type: F64, loc }, + type: BOOL, + loc, + }, + then: [{ kind: "return", value: null, loc }], + else_: null, + loc, + }, + { kind: "exprStmt", expr: { kind: "yieldExpr", value: yielded, type: VOID, loc }, loc }, + ], + loc, + }, + ], + generator: { + yieldT: type.yieldT, + nextT: UNDEFINED_T, + resultType, + tag: "!", + }, + loc, + }); + } + return { kind: "call", callee: name, args: [receiver], type, loc }; +} + +/** `ta.values()`, `ta.keys()` or `ta.entries()` on a typed array as a + * value: an interned generator over the indices, reading the length and + * the element at each step, as the typed array iterator does. Null when + * `type` is not the matching number (or [number, number]) generator. */ +export function lowerBytesIteratorValue( + lowerer: Lowerer, + receiver: IrExpr, + method: "values" | "keys" | "entries", + type: IrType, + loc: SrcLoc, +): IrExpr | null { + const bytesT = receiver.type; + if (bytesT.kind !== "bytes" || type.kind !== "generator") return null; + if (type.retT.kind !== "void" || type.nextT.kind !== "undefinedT") return null; + const pair = type.yieldT.kind === "record" ? lowerer.shapes.get(type.yieldT.shapeId) : undefined; + const pairOk = + pair?.tuple === true && + pair.fields.length === 2 && + pair.fields.every((f) => (f.name === "0" || f.name === "1") && f.type.kind === "f64"); + if (method === "entries" ? !pairOk : type.yieldT.kind !== "f64") return null; + const resultType = genResultRecord(type.yieldT, type.retT, lowerer.shapes, lowerer.unions); + if (!resultType) return null; + const key = `bytesIter:${method}:${typeKey(bytesT)}:${typeKey(type)}`; + let name = lowerer.valueHelpers.get(key); + if (!name) { + name = `%bytes.iter.${lowerer.valueHelpers.size}`; + lowerer.valueHelpers.set(key, name); + const list = varRef("list.0", bytesT, loc); + const index = varRef("index.0", F64, loc); + const element: IrExpr = { + kind: "bytesIntrinsic", + method: "get", + receiver: list, + args: [index], + type: F64, + loc, + }; + const yielded: IrExpr = + method === "keys" + ? index + : method === "values" + ? element + : { + kind: "recordLit", + fields: [ + { name: "0", value: index }, + { name: "1", value: element }, + ], + type: type.yieldT, + loc, + }; + lowerer.liftedFns.push({ + name, + params: [{ localId: "list.0", name: "list", type: bytesT }], + returnType: VOID, + locals: [ + { id: "list.0", name: "list", type: bytesT, mutable: false }, + { id: "index.0", name: "index", type: F64, mutable: true }, + ], + body: [ + { + kind: "varDecl", + localId: "index.0", + init: { kind: "numLit", value: 0, type: F64, loc }, + loc, + }, + { + kind: "while", + cond: { + kind: "bin", + op: "<", + left: index, + right: { + kind: "bytesIntrinsic", + method: "length", + receiver: list, + args: [], + type: F64, + loc, + }, + type: BOOL, + loc, + }, + body: [ + { kind: "exprStmt", expr: { kind: "yieldExpr", value: yielded, type: VOID, loc }, loc }, + { + kind: "assign", + localId: "index.0", + value: { + kind: "bin", + op: "+", + left: index, + right: { kind: "numLit", value: 1, type: F64, loc }, + type: F64, + loc, + }, + loc, + }, + ], + loc, + }, + { kind: "return", value: null, loc }, + ], + generator: { yieldT: type.yieldT, nextT: UNDEFINED_T, resultType, tag: "Array Iterator" }, + loc, + }); + } + return { kind: "call", callee: name, args: [receiver], type, loc }; +} + +/** `s[Symbol.iterator]()` on a string value: an interned generator over + * its code points, the string iterator's steps (a lone surrogate is one + * step). Null when `type` is not the plain string-yielding generator. */ +export function lowerStringIteratorValue( + lowerer: Lowerer, + receiver: IrExpr, + type: IrType, + loc: SrcLoc, +): IrExpr | null { + if (receiver.type.kind !== "string" || type.kind !== "generator") return null; + if ( + type.yieldT.kind !== "string" || + type.retT.kind !== "void" || + type.nextT.kind !== "undefinedT" + ) + return null; + const resultType = genResultRecord(type.yieldT, type.retT, lowerer.shapes, lowerer.unions); + if (!resultType) return null; + const key = `strIter:${typeKey(type)}`; + let name = lowerer.valueHelpers.get(key); + if (!name) { + name = `%str.iter.${lowerer.valueHelpers.size}`; + lowerer.valueHelpers.set(key, name); + const text = varRef("text.0", STRING, loc); + const index = varRef("index.0", F64, loc); + const point = varRef("point.0", STRING, loc); + lowerer.liftedFns.push({ + name, + params: [{ localId: "text.0", name: "text", type: STRING }], + returnType: VOID, + locals: [ + { id: "text.0", name: "text", type: STRING, mutable: false }, + { id: "index.0", name: "index", type: F64, mutable: true }, + { id: "point.0", name: "point", type: STRING, mutable: false }, + ], + body: [ + { + kind: "varDecl", + localId: "index.0", + init: { kind: "numLit", value: 0, type: F64, loc }, + loc, + }, + { + kind: "while", + cond: { + kind: "bin", + op: "<", + left: index, + right: { + kind: "strIntrinsic", + method: "length", + receiver: text, + args: [], + type: F64, + loc, + }, + type: BOOL, + loc, + }, + body: [ + { + kind: "varDecl", + localId: "point.0", + init: { + kind: "strIntrinsic", + method: "cpAt", + receiver: text, + args: [index], + type: STRING, + loc, + }, + loc, + }, + { + kind: "assign", + localId: "index.0", + value: { + kind: "bin", + op: "+", + left: index, + right: { + kind: "strIntrinsic", + method: "length", + receiver: point, + args: [], + type: F64, + loc, + }, + type: F64, + loc, + }, + loc, + }, + { kind: "exprStmt", expr: { kind: "yieldExpr", value: point, type: VOID, loc }, loc }, + ], + loc, + }, + { kind: "return", value: null, loc }, + ], + generator: { yieldT: type.yieldT, nextT: UNDEFINED_T, resultType, tag: "String Iterator" }, + loc, + }); + } + return { kind: "call", callee: name, args: [receiver], type, loc }; +} + /** A generator flowing into a slot with other channel types (a * `Generator` returned through an * `IterableIterator` annotation). The interned adapter generator @@ -419,7 +756,12 @@ function generatorRetypeAdapter( loc, }, ], - generator: { yieldT: expected.yieldT, nextT: expected.nextT, resultType: toResult }, + generator: { + yieldT: expected.yieldT, + nextT: expected.nextT, + resultType: toResult, + inheritsTag: true, + }, loc, }); } @@ -690,7 +1032,7 @@ export function lowerArrayIteratorValue( }, { kind: "return", value: null, loc }, ], - generator: { yieldT: type.yieldT, nextT: UNDEFINED_T, resultType }, + generator: { yieldT: type.yieldT, nextT: UNDEFINED_T, resultType, tag: "Array Iterator" }, loc, }); } diff --git a/packages/compiler/src/frontend/lowering/lower-calls.ts b/packages/compiler/src/frontend/lowering/lower-calls.ts index 23b8b69d2..b3cd7e39b 100644 --- a/packages/compiler/src/frontend/lowering/lower-calls.ts +++ b/packages/compiler/src/frontend/lowering/lower-calls.ts @@ -94,6 +94,8 @@ import { lowerDynArrayFilterCall, lowerDynArrayFlatMapCall } from "./containers/ import { lowerGroupByStaticCall } from "./containers/collection-grouping.js"; import { lowerGeneratorHelperCall, + lowerHelperReturnCall, + lowerIteratorFromCall, lowerIteratorHelperCall, } from "./containers/iterator-helpers.js"; import { @@ -223,6 +225,7 @@ import { completeArgs, } from "./call-arguments.js"; import { isThisParameter, type ParamShape } from "./call-signatures.js"; +import { lowerStringIteratorValue } from "./iterator-adapters.js"; import { checkedArmExtraction } from "./coercions/unions.js"; import { maybePromiseReturnStmt, promiseArmedUnionOf } from "./maybe-promise.js"; import { @@ -365,6 +368,9 @@ export function lowerCall(lowerer: Lowerer, expr: ts.CallExpression): IrExpr { // machinery (the member always exists on a POSIX target, so the // optional call IS the call; `process.getuid` itself has no value // lowering for the chain to guard). + // `h.return!()` / `h.return?.()` on a helper object: the method exists. + const helperReturn = lowerHelperReturnCall(lowerer, expr); + if (helperReturn) return helperReturn; const processOptional = lowerer.lowerProcessOptionalMethodCall(expr); if (processOptional) return processOptional; const reqStreamOptional = lowerCompatReqStreamOptionalCall(lowerer, expr); @@ -1616,6 +1622,17 @@ export function lowerCall(lowerer: Lowerer, expr: ts.CallExpression): IrExpr { lowerer.unsupported("SC1090", expr, `method calls like '${expr.expression.getText()}'`); } + // `s[Symbol.iterator]()` on a string: the string iterator as a value. + if ( + ts.isElementAccessExpression(expr.expression) && + !expr.expression.questionDotToken && + !expr.questionDotToken && + expr.arguments.length === 0 + ) { + const iterator = lowerStringIteratorCall(lowerer, expr, expr.expression); + if (iterator) return iterator; + } + // The element spelling of an unsupported native Web method must fence // before lowering the callee into a checked-dynamic keyed read. if (ts.isElementAccessExpression(expr.expression)) { @@ -2783,7 +2800,8 @@ function lowerObjectPrototypeCall( const immediate = immediateObjectTagReceiver(lowerer, receiverNode); const receiver = immediate?.effect ?? lowerer.lowerExpr(receiverNode); const tag = immediate?.tag ?? objectToStringTag(lowerer, receiver.type); - if (tag === null && receiver.type.kind !== "dyn") { + // A generator object answers its tag through its checked capsule. + if (tag === null && receiver.type.kind !== "dyn" && receiver.type.kind !== "generator") { lowerer.noLowering( "Object.prototype.toString.call over a " + lowerer.fmt(receiver.type) + " receiver", receiverNode, @@ -2803,7 +2821,13 @@ function lowerObjectPrototypeCall( } const result: IrExpr = tag === null - ? { kind: "libCall", fn: "dyn.objectTag", args: [ref], type: STRING, loc } + ? { + kind: "libCall", + fn: "dyn.objectTag", + args: [ref.type.kind === "generator" ? lowerer.coerceToExpected(ref, DYN) : ref], + type: STRING, + loc, + } : { kind: "strLit", value: "[object " + tag + "]", type: STRING, loc }; return { kind: "seqExpr", stmts, result, type: STRING, loc }; } @@ -3157,10 +3181,32 @@ function lowerOptionalStringNumber( return { kind: "call", callee: helper, args: [arg], type: F64, loc }; } -/** Iterator types with checked runtime receivers: Map/Set iterators' - * helper calls lower in lowerGeneratorHelperCall; the string iterator's - * have no lowering. */ -const UNLOWERED_ITERATOR_TYPES = new Set(["MapIterator", "SetIterator", "StringIterator"]); +/** `s[Symbol.iterator]()` on a string-typed receiver: the generator over + * its code points. Null for other element calls. */ +function lowerStringIteratorCall( + lowerer: Lowerer, + call: ts.CallExpression, + access: ts.ElementAccessExpression, +): IrExpr | null { + let key = access.argumentExpression; + while (ts.isParenthesizedExpression(key)) key = key.expression; + if ( + !ts.isPropertyAccessExpression(key) || + lowerer.stdlibGlobalMember(key, "Symbol") !== "iterator" + ) + return null; + if (lowerer.mapTypeOf(lowerer.typeOf(access.expression))?.kind !== "string") return null; + const type = lowerer.mapTypeOf(lowerer.typeOf(call)); + if (type?.kind !== "generator") return null; + const receiver = lowerer.lowerExpr(access.expression); + const value = lowerStringIteratorValue(lowerer, receiver, type, locOf(call)); + if (!value) lowerer.unsupported("SC1090", call, "this string iterator type"); + return value; +} + +/** Iterator types with checked runtime receivers, whose helper calls lower + * in lowerGeneratorHelperCall. */ +const CHECKED_ITERATOR_TYPES = new Set(["MapIterator", "SetIterator"]); const ITERATOR_HELPER_METHODS = new Set([ "map", "filter", @@ -3220,25 +3266,17 @@ function lowerDynReceiverMethodCall( const recvSymbol = recvTs.getSymbol(); if ( recvSymbol && - UNLOWERED_ITERATOR_TYPES.has(recvSymbol.name) && + CHECKED_ITERATOR_TYPES.has(recvSymbol.name) && lowerer.isStdlibSymbol(recvSymbol) && ITERATOR_HELPER_METHODS.has(access.name.text) && lowerer.isStdlibMember(access) ) { - if (recvSymbol.name === "MapIterator" || recvSymbol.name === "SetIterator") { - const helper = lowerGeneratorHelperCall(lowerer, call, access); - if (helper) return helper; - lowerer.noLowering( - `collection iterator .${access.name.text}()`, - call, - "stored Map/Set iterators support .next() and iteration; iterator helper methods have no lowering", - ); - } - // The checked runtime's string iterator has no helper methods. + const helper = lowerGeneratorHelperCall(lowerer, call, access); + if (helper) return helper; lowerer.noLowering( - `${recvSymbol.name}.${access.name.text}`, + `collection iterator .${access.name.text}()`, call, - "iterator helpers lower on generators and on array, Map and Set iterators — spread the string first: [...s].values()", + "iterator helpers on Map/Set iterators are called directly on the iterator, without optional chaining", ); } const arrayReceiver = lowerer.checker.isArrayType(recvTs); @@ -6644,10 +6682,10 @@ function recordAssignHelper( return helper; } -/** The `Iterator` global's statics (ES2025 — Iterator.from, and the - * abstract constructor as a value) have no lowering: every member fences - * with the working spelling named instead of the generic-method fence's - * monomorphization wording. */ +/** The `Iterator` global's statics (ES2025): `Iterator.from` over a typed + * built-in iterable lowers (lowerIteratorFromCall); every other member, + * and other arguments, fence with the working spelling named instead of + * the generic-method fence's monomorphization wording. */ function lowerIteratorStaticFence( lowerer: Lowerer, call: ts.CallExpression, @@ -6655,11 +6693,15 @@ function lowerIteratorStaticFence( ): IrExpr | null { if (!lowerer.isStdlibGlobal(access.expression, "Iterator")) return null; if (!lowerer.isStdlibMember(access)) return null; + const from = lowerIteratorFromCall(lowerer, call, access); + if (from) return from; lowerer.noLowering( `Iterator.${access.name.text}`, call, - "iterator helpers apply directly to generators and to array, Map and Set iterators: " + - "arr.values().map(f).take(n).toArray()", + access.name.text === "from" + ? "Iterator.from accepts arrays, strings, typed arrays, Sets, Maps and iterator objects such as generators" + : "iterator helpers apply directly to generators and to array, Map and Set iterators: " + + "arr.values().map(f).take(n).toArray()", ); } diff --git a/packages/compiler/src/frontend/lowering/lower-classes.ts b/packages/compiler/src/frontend/lowering/lower-classes.ts index 55668c4b7..4445358d3 100644 --- a/packages/compiler/src/frontend/lowering/lower-classes.ts +++ b/packages/compiler/src/frontend/lowering/lower-classes.ts @@ -4598,8 +4598,7 @@ function lowerStaticFieldInitsInner(lowerer: Lowerer, info: ClassInfo): IrStmt[] continue; } const f = item.f; - lowerer.stats.statementsTotal++; - lowerer.bumpFileStat(locOf(f.initializer).file, "total"); + lowerer.countStatement(f.initializer, locOf(f.initializer).file); const diagsBefore = lowerer.diags.length; const sinkMark = lowerer.diagSinkMark(); try { @@ -4635,8 +4634,7 @@ function lowerStaticFieldInitsInner(lowerer: Lowerer, info: ClassInfo): IrStmt[] out.push({ kind: "assign", localId: f.globalId, value, loc: locOf(f.initializer) }); } catch (e) { if (!(e instanceof PoisonError)) throw e; - lowerer.stats.statementsFailed++; - lowerer.bumpFileStat(locOf(f.initializer).file, "failed"); + lowerer.countStatementFailed(f.initializer, locOf(f.initializer).file); if (isJsSourceFile(f.initializer.getSourceFile())) { const fence = lowerer.diagSink === null @@ -7764,8 +7762,7 @@ export function fieldInitStmts(lowerer: Lowerer, info: ClassInfo, thisLocal: IrL }); } if (!f.initializer) continue; - lowerer.stats.statementsTotal++; - lowerer.bumpFileStat(locOf(f.initializer).file, "total"); + lowerer.countStatement(f.initializer, locOf(f.initializer).file); const diagsBefore = lowerer.diags.length; const sinkMark = lowerer.diagSinkMark(); try { @@ -7780,8 +7777,7 @@ export function fieldInitStmts(lowerer: Lowerer, info: ClassInfo, thisLocal: IrL }); } catch (e) { if (!(e instanceof PoisonError)) throw e; - lowerer.stats.statementsFailed++; - lowerer.bumpFileStat(locOf(f.initializer).file, "failed"); + lowerer.countStatementFailed(f.initializer, locOf(f.initializer).file); if (isJsSourceFile(f.initializer.getSourceFile())) { const fence = lowerer.diagSink === null diff --git a/packages/compiler/src/frontend/lowering/lower-exprs.ts b/packages/compiler/src/frontend/lowering/lower-exprs.ts index 47da36a6e..e06466f6f 100644 --- a/packages/compiler/src/frontend/lowering/lower-exprs.ts +++ b/packages/compiler/src/frontend/lowering/lower-exprs.ts @@ -147,6 +147,7 @@ import { } from "../../diagnostics/diagnostic.js"; import { PoisonError, dynFallbackType, jsFuncNameOf, neverTaintedJsType, own } from "./lowerer.js"; import { lowerCollectionSpread } from "./containers/collection-methods.js"; +import { lowerHelperReturnCall } from "./containers/iterator-helpers.js"; import { arrayNumberRead, arrayValueRead, @@ -1912,6 +1913,10 @@ function lowerExprInner(lowerer: Lowerer, expr: ts.Expression): IrExpr { if (ts.isPostfixUnaryExpression(expr)) return lowerIncDec(lowerer, expr, false); if (ts.isBinaryExpression(expr)) return lowerer.lowerBinary(expr); if (ts.isCallExpression(expr)) { + // `h.return?.()` on a helper object: the method exists, so the + // optional call is the call. + const helperReturn = lowerHelperReturnCall(lowerer, expr); + if (helperReturn) return helperReturn; // The tail of an optional chain whose `?.` sits deeper // (`x?.trim().toLowerCase()`): the whole tail short-circuits together. if (isOptionalChainTail(lowerer, expr)) return lowerer.lowerOptionalChain(expr); @@ -2164,13 +2169,11 @@ function lowerExprInner(lowerer: Lowerer, expr: ts.Expression): IrExpr { (lowerer.isStdlibMember(expr) || isJsSourceFile(expr.getSourceFile())) ) { const recv = represented; - const read: IrExpr = { - kind: "dynKeyGet", - value: lowerer.coerceToExpected(recv, DYN), - key: { kind: "strLit", value: expr.name.text, type: STRING, loc }, - type: DYN, - loc, - }; + const key: IrExpr = { kind: "strLit", value: expr.name.text, type: STRING, loc }; + // A plain slice answers from its match source without a dynamic copy. + const read: IrExpr = typeEquals(recv.type, arrayOf(STRING)) + ? { kind: "libCall", fn: "regex.resultProp", args: [recv, key], type: DYN, loc } + : { kind: "dynKeyGet", value: lowerer.coerceToExpected(recv, DYN), key, type: DYN, loc }; return lowerer.maybeNarrow(read, expr); } } diff --git a/packages/compiler/src/frontend/lowering/lower-inspect.ts b/packages/compiler/src/frontend/lowering/lower-inspect.ts index 1d67cef92..2551031eb 100644 --- a/packages/compiler/src/frontend/lowering/lower-inspect.ts +++ b/packages/compiler/src/frontend/lowering/lower-inspect.ts @@ -253,6 +253,10 @@ function inspectSupport( visiting.delete(t.className); return null; } + // Generator objects print through their checked capsule: Node's + // `Object [Generator] {}`, or the tag of the iterator they implement. + case "generator": + return null; default: return `'${lowerer.fmt(t)}' values have no inspect lowering`; } @@ -343,6 +347,14 @@ export function inspectExpr( return { kind: "libCall", fn: "insp.buffer", args: [value], type: STRING, loc }; case "dyn": return { kind: "libCall", fn: "insp.dyn", args: [value, recurse, depth], type: STRING, loc }; + case "generator": + return { + kind: "libCall", + fn: "insp.dyn", + args: [lowerer.coerceToExpected(value, DYN), recurse, depth], + type: STRING, + loc, + }; case "object": if (eventTargetRootOf(lowerer.classes.get(t.className)) !== null) { return { diff --git a/packages/compiler/src/frontend/lowering/lower-stmts.ts b/packages/compiler/src/frontend/lowering/lower-stmts.ts index 7fb1f69a9..4ed68ab1f 100644 --- a/packages/compiler/src/frontend/lowering/lower-stmts.ts +++ b/packages/compiler/src/frontend/lowering/lower-stmts.ts @@ -1,3 +1,7 @@ +import { + matchAllRowsReadByElements, + storedMatchAllRowsReadByElements, +} from "./containers/match-row-uses.js"; import { dynUndefinedExpr, numLit, varRef } from "../../ir/build.js"; import { lowerCollectionSpread } from "./containers/collection-methods.js"; import { lowerEventObjectPropertyWrite } from "./lower-event-target.js"; @@ -690,10 +694,7 @@ export function lowerStmts(lowerer: Lowerer, stmts: readonly ts.Statement[]): Ir entry.index = i; if (unreachable && skipsWhenUnreachable(stmt)) continue; if (inFunction && isAbruptStatement(stmt)) unreachable = true; - if (!lowerer.suppressStats) { - lowerer.stats.statementsTotal++; - if (sf !== undefined) lowerer.bumpFileStat(sf.fileName, "total"); - } + if (!lowerer.suppressStats) lowerer.countStatement(stmt, sf?.fileName); const diagsBefore = lowerer.diags.length; const sinkMark = lowerer.diagSinkMark(); try { @@ -758,10 +759,7 @@ export function lowerStmts(lowerer: Lowerer, stmts: readonly ts.Statement[]): Ir } else if (!(e instanceof PoisonError)) { throw e; } - if (!lowerer.suppressStats) { - lowerer.stats.statementsFailed++; - if (sf !== undefined) lowerer.bumpFileStat(sf.fileName, "failed"); - } + if (!lowerer.suppressStats) lowerer.countStatementFailed(stmt, sf?.fileName); lowerer.noteBlockedBindings(stmt); // The runtime-fence conversion (JS sources, direct diagnostics // only — a capture wrapper in force means collection owns the @@ -1892,7 +1890,9 @@ function lowerMatchAllDrainDecl( localId: local.id, init: { kind: "regexIntrinsic", - method: "matchAllInto", + method: storedMatchAllRowsReadByElements(lowerer, decl) + ? "matchAllCapturesInto" + : "matchAllInto", receiver, args: [re, { kind: "varRef", localId: idxs.id, type: idxsT, loc }], type: rowsT, @@ -9568,7 +9568,9 @@ function lowerForOfMatchAll( }); rowsInit = { kind: "regexIntrinsic", - method: "matchAllInto", + method: matchAllRowsReadByElements(lowerer, call, true) + ? "matchAllCapturesInto" + : "matchAllInto", receiver, args: [re, { kind: "varRef", localId: idxs.id, type: idxsT, loc }], type: rowsT, diff --git a/packages/compiler/src/frontend/lowering/lower-test.ts b/packages/compiler/src/frontend/lowering/lower-test.ts index e36518559..c31e674af 100644 --- a/packages/compiler/src/frontend/lowering/lower-test.ts +++ b/packages/compiler/src/frontend/lowering/lower-test.ts @@ -90,12 +90,14 @@ function atStringOf(expr: ts.CallExpression): string { return `${sf.fileName}:${pos.line + 1}:${pos.character + 1}`; } -/** One parsed { skip?, todo?, only? } options literal. `mode` is the - * runtime literal (0 run / 1 skip / 2 todo), `msg` the directive message - * expression ("" = none). */ +/** One parsed { skip?, todo?, only? } options literal. `skip` and `todo` + * are whether each is set (a string or true), and `skipMsg` and `todoMsg` + * the string messages (directiveOf settles them). */ interface TestOptions { - mode: number; - msg: IrExpr | null; + skip: boolean; + skipMsg: IrExpr | null; + todo: boolean; + todoMsg: IrExpr | null; only: boolean; /** The { plan } count, -1 without one. */ plan: number; @@ -109,8 +111,10 @@ interface TestOptions { } const noOptions = (): TestOptions => ({ - mode: 0, - msg: null, + skip: false, + skipMsg: null, + todo: false, + todoMsg: null, only: false, plan: -1, timeout: null, @@ -118,6 +122,23 @@ const noOptions = (): TestOptions => ({ pre: [], }); +/** The runtime mode (0 run / 1 skip / 2 todo) and directive message of a + * test or suite, as Node's Test constructor settles them: the skip and todo + * method twins override the options of the same name, skip wins over + * todo, and the message is the skip string, else the todo string. */ +function directiveOf(opts: TestOptions, methodMode: number, loc: SrcLoc): [IrExpr, IrExpr] { + if (methodMode === 1) { + opts.skip = true; + opts.skipMsg = null; + } + if (methodMode === 2) { + opts.todo = true; + opts.todoMsg = null; + } + const mode = opts.skip ? 1 : opts.todo ? 2 : 0; + return [numLit(mode, loc), opts.skipMsg ?? opts.todoMsg ?? strLit("", loc)]; +} + /** Flags 8 and 16: a { timeout } and a { concurrency } are given. */ const optionFlags = (opts: TestOptions): number => (opts.only ? 4 : 0) | (opts.timeout ? 8 : 0) | (opts.concurrency ? 16 : 0); @@ -168,15 +189,33 @@ function lowerTestOptions( // A shorthand entry's value is the binding of the same name. const init: ts.Expression = ts.isPropertyAssignment(prop) ? prop.initializer : key; if (name === "skip" || name === "todo") { - const mode = name === "skip" ? 1 : 2; const t = lowerer.typeOf(init); if (t.isStringLiteralType() || lowerer.mapTypeOf(t)?.kind === "string") { - out.mode = mode; - out.msg = optionValue(lowerer, out, lowerer.lowerExprExpecting(init, STRING), locOf(init)); - } else if (init.kind === ts.SyntaxKind.TrueKeyword) { - out.mode = mode; - } else if (init.kind === ts.SyntaxKind.FalseKeyword) { - // explicit false: run normally + const msg = optionValue( + lowerer, + out, + lowerer.lowerExprExpecting(init, STRING), + locOf(init), + ); + if (name === "skip") { + out.skip = true; + out.skipMsg = msg; + } else { + out.todo = true; + out.todoMsg = msg; + } + } else if ( + init.kind === ts.SyntaxKind.TrueKeyword || + init.kind === ts.SyntaxKind.FalseKeyword + ) { + const on = init.kind === ts.SyntaxKind.TrueKeyword; + if (name === "skip") { + out.skip = on; + out.skipMsg = null; + } else { + out.todo = on; + out.todoMsg = null; + } } else { lowerer.noLowering( `${what} with a non-literal ${name} value`, @@ -284,6 +323,116 @@ function lowerBodyArg( /** The shared registration lowering behind test/it and their skip/todo/ * only method twins — and, with `sub`, behind t.test. Argument shapes: * (name), (name, fn), (name, options, fn). */ +/** The `name` a function value has in JavaScript, when the compiler can + * tell: "" for an anonymous function expression, the declared or binding + * name otherwise; null for other expressions. */ +function staticFunctionName(lowerer: Lowerer, node: ts.Expression): string | null { + while (ts.isParenthesizedExpression(node)) node = node.expression; + if (ts.isArrowFunction(node)) return ""; + if (ts.isFunctionExpression(node)) return node.name?.text ?? ""; + if (!ts.isIdentifier(node)) return null; + const symbol = lowerer.resolveValueSymbol(node); + if (!symbol) return null; + for (const decl of lowerer.checker.declarationsOf(symbol)) { + if (ts.isFunctionDeclaration(decl)) return decl.name?.text ?? "default"; + if (ts.isVariableDeclaration(decl) && ts.isIdentifier(decl.name) && decl.initializer) { + let init = decl.initializer; + while (ts.isParenthesizedExpression(init)) init = init.expression; + if (ts.isFunctionExpression(init) && init.name) return init.name.text; + if (ts.isArrowFunction(init) || ts.isFunctionExpression(init)) return decl.name.text; + } + } + return null; +} + +/** Node's (name?, options?, fn?) argument forms (`Test#createSubtest`): a + * leading function is the body, a leading object the options, otherwise + * the first argument is the name and a following object the options. */ +function splitTestArgs( + lowerer: Lowerer, + expr: ts.CallExpression, + what: string, +): { nameNode?: ts.Expression; optionsNode?: ts.Expression; fnNode?: ts.Expression } { + const args = expr.arguments; + const isFn = (a: ts.Expression): boolean => + lowerer.checker.getCallSignatures(lowerer.typeOf(a)).length > 0; + const isOptions = (a: ts.Expression): boolean => ts.isObjectLiteralExpression(a); + const unsupported = (): never => + lowerer.noLowering( + `${what} with these arguments`, + expr, + "the supported forms are (name?, options?, fn?) with a string name, an options object literal, and a function", + ); + if (args.length > 3 || args.some(ts.isSpreadElement)) unsupported(); + const out: { nameNode?: ts.Expression; optionsNode?: ts.Expression; fnNode?: ts.Expression } = {}; + let i = 0; + const next = args[i]; + if (next !== undefined && !isFn(next) && !isOptions(next)) { + out.nameNode = next; + i++; + } + const options = args[i]; + if (options !== undefined && isOptions(options)) { + out.optionsNode = options; + i++; + } + const fn = args[i]; + if (fn !== undefined && isFn(fn)) { + out.fnNode = fn; + i++; + } + if (i < args.length) unsupported(); + return out; +} + +/** The test's name, as Node's Test constructor completes it: a missing or + * empty name takes the function's name, or "". */ +function lowerTestName( + lowerer: Lowerer, + expr: ts.CallExpression, + what: string, + nameNode: ts.Expression | undefined, + fnNode: ts.Expression | undefined, + loc: SrcLoc, +): IrExpr { + const fallback = (): string => { + const fnName = fnNode ? staticFunctionName(lowerer, fnNode) : ""; + if (fnName === null) { + lowerer.noLowering( + `${what} without a name, with a function whose name is not known at compile time`, + fnNode ?? expr, + "give the test a name, or pass a function expression or a declared function", + ); + } + return fnName || ""; + }; + if (!nameNode) return strLit(fallback(), loc); + const name = lowerer.lowerExprExpecting(nameNode, STRING); + if (name.kind === "strLit") return name.value === "" ? strLit(fallback(), loc) : name; + // A name computed at run time: when the function's own name is not known + // at compile time (a function passed in as a parameter, say), the name + // is used as given, and an empty one stays empty (the limitations page + // notes it). Refusing here would refuse every helper that registers + // tests for its callers. + if (fnNode && staticFunctionName(lowerer, fnNode) === null) return name; + const slot = lowerer.declareHiddenLocal("%testName", STRING); + const value = varRef(slot.id, STRING, loc); + return { + kind: "seqExpr", + stmts: [{ kind: "varDecl", localId: slot.id, init: name, loc }], + result: { + kind: "ternary", + cond: { kind: "strEq", negated: false, left: value, right: strLit("", loc), type: BOOL, loc }, + then: strLit(fallback(), loc), + else_: value, + type: STRING, + loc, + }, + type: STRING, + loc, + }; +} + function lowerRegistration( lowerer: Lowerer, expr: ts.CallExpression, @@ -293,21 +442,11 @@ function lowerRegistration( sub: IrExpr | null, // the lowered TestContext receiver (t.test) ): IrExpr { if (sub === null) requireStatementPosition(lowerer, expr, what); - const args = expr.arguments; - if (args.length < 1 || args.length > 3) { - lowerer.noLowering( - `${what} with ${args.length} arguments`, - expr, - "the supported forms are (name), (name, fn), and (name, options, fn)", - ); - } - let name = lowerer.lowerExprExpecting(args[0]!, STRING); + const { nameNode, optionsNode, fnNode } = splitTestArgs(lowerer, expr, what); + let name = lowerTestName(lowerer, expr, what, nameNode, fnNode, loc); let opts = noOptions(); - let fnNode: ts.Expression | undefined; - if (args.length === 2) fnNode = args[1]; - if (args.length === 3) { - opts = lowerTestOptions(lowerer, args[1]!, what, false); - fnNode = args[2]; + if (optionsNode) { + opts = lowerTestOptions(lowerer, optionsNode, what, false); // The name runs before the option values. if (opts.pre.length > 0 && name.kind !== "strLit") { const slot = lowerer.declareHiddenLocal("%testName", STRING); @@ -315,11 +454,9 @@ function lowerRegistration( name = { kind: "varRef", localId: slot.id, type: STRING, loc }; } } - if (methodMode === 1 || methodMode === 2) opts.mode = methodMode; if (methodMode === 3) opts.only = true; const at = strLit(atStringOf(expr), loc); - const mode = numLit(opts.mode, loc); - const msg = opts.msg ?? strLit("", loc); + const [mode, msg] = directiveOf(opts, methodMode, loc); const flagBits = optionFlags(opts); const planTimeout = [ numLit(opts.plan, loc), @@ -387,27 +524,21 @@ function lowerSuite( methodMode: number, ): IrExpr { requireStatementPosition(lowerer, expr, what); - const args = expr.arguments; - if (args.length < 2 || args.length > 3) { - lowerer.noLowering( - `${what} with ${args.length} arguments`, - expr, - "the supported forms are (name, fn) and (name, options, fn)", - ); - } - let name = lowerer.lowerExprExpecting(args[0]!, STRING); + const split = splitTestArgs(lowerer, expr, what); + let name = lowerTestName(lowerer, expr, what, split.nameNode, split.fnNode, loc); let opts = noOptions(); - let fnNode = args[1]!; - if (args.length === 3) { - opts = lowerTestOptions(lowerer, args[1]!, what, true); - fnNode = args[2]!; + if (split.optionsNode) { + opts = lowerTestOptions(lowerer, split.optionsNode, what, true); if (opts.pre.length > 0 && name.kind !== "strLit") { const slot = lowerer.declareHiddenLocal("%testName", STRING); opts.pre.unshift({ kind: "varDecl", localId: slot.id, init: name, loc }); name = { kind: "varRef", localId: slot.id, type: STRING, loc }; } } - if (methodMode === 1 || methodMode === 2) opts.mode = methodMode; + const fnNode = split.fnNode; + if (!fnNode) { + lowerer.noLowering(`${what} without a body`, expr, "give the suite a body function"); + } const cb = lowerer.lowerExpr(fnNode); // The body may take the SuiteContext (flag 2, as test bodies take theirs). if ( @@ -429,8 +560,7 @@ function lowerSuite( ); } const at = strLit(atStringOf(expr), loc); - const mode = numLit(opts.mode, loc); - const msg = opts.msg ?? strLit("", loc); + const [mode, msg] = directiveOf(opts, methodMode, loc); // describe.only and { only: true } mark the suite (flag 4, as for tests). const flags = numLit( optionFlags(opts) | (methodMode === 3 ? 4 : 0) | (cb.type.params.length === 1 ? 2 : 0), diff --git a/packages/compiler/src/frontend/lowering/lowerer.ts b/packages/compiler/src/frontend/lowering/lowerer.ts index 6493bc5f3..2fb266be0 100644 --- a/packages/compiler/src/frontend/lowering/lowerer.ts +++ b/packages/compiler/src/frontend/lowering/lowerer.ts @@ -845,7 +845,25 @@ type RuntimeFenceTarget = /** The Lowerer's pass configuration (see lowerToIr). */ export interface LowererMode { + /** Symbols whose deferred diagnostics reachable emit already flushed — + * the remainder must not report them a second time. */ + alreadyFlushed?: ReadonlySet; + /** LowerOptions.externalTypes, threaded through every lowering pass. */ + externalTypes?: ReadonlyMap; + /** LowerOptions.externalTypeSpecifiersByFile, shared by every pass. */ + externalTypeSpecifiersByFile?: ReadonlyMap; + /** Program-validated ambient declaration symbols for each FFI name. + * Undefined in discovery's legacy call-local validation path. */ + ffiBindingSymbols?: ReadonlyMap>; + /** The build's outbound native FFI declarations. */ + ffiImports?: readonly IrFfiImport[]; + /** Statically resolved child_process.fork program roots, in stable target-id order. */ + forkTargets?: readonly ts.SourceFile[]; frontendServices?: FrontendServices | undefined; + /** LoadResult.inlineRequireCycleCalls. */ + inlineRequireCycleCalls?: ReadonlySet | undefined; + /** LowerOptions.libraryCallbacks (see there). */ + libraryCallbacks?: boolean; projectCheckJs?: boolean; /** Names of bodies a prior reachability pass reached; null lowers everything. */ reachable?: ReadonlySet | null; @@ -853,35 +871,17 @@ export interface LowererMode { * always-reachable init bodies and module building, and report deferred * collection diagnostics nothing flushed. */ remainder?: boolean; - /** Symbols whose deferred diagnostics reachable emit already flushed — - * the remainder must not report them a second time. */ - alreadyFlushed?: ReadonlySet; - /** The build's target platform (LowerOptions.targetPlatform — lowerToIr - * passes it to every pass). Defaults to the host. */ - targetPlatform?: string; /** Node's startup refusal (preflight's resolution walk / named-import * link checks): %main opens with exactly this throw, before any * module init — Node refuses the whole graph before anything evaluates, * so nothing runs. */ startupCrash?: StartupCrash | null; - /** LoadResult.inlineRequireCycleCalls. */ - inlineRequireCycleCalls?: ReadonlySet | undefined; - /** The build's outbound native FFI declarations. */ - ffiImports?: readonly IrFfiImport[]; - /** LowerOptions.libraryCallbacks (see there). */ - libraryCallbacks?: boolean; - /** Statically resolved child_process.fork program roots, in stable target-id order. */ - forkTargets?: readonly ts.SourceFile[]; - workerTargets?: readonly ts.SourceFile[]; - /** Program-validated ambient declaration symbols for each FFI name. - * Undefined in discovery's legacy call-local validation path. */ - ffiBindingSymbols?: ReadonlyMap>; - /** LowerOptions.externalTypes, threaded through every lowering pass. */ - externalTypes?: ReadonlyMap; - /** LowerOptions.externalTypeSpecifiersByFile, shared by every pass. */ - externalTypeSpecifiersByFile?: ReadonlyMap; + /** The build's target platform (LowerOptions.targetPlatform — lowerToIr + * passes it to every pass). Defaults to the host. */ + targetPlatform?: string; /** LowerOptions.tierMode, shared by every pass. */ tierMode?: TierMode; + workerTargets?: readonly ts.SourceFile[]; } function directExternalTypeSpecifiersByFile( @@ -2925,6 +2925,32 @@ export class Lowerer { * pass skips it (attribution describes the build). */ readonly statsByFile = new Map(); + /** Statements and field initializers already in the coverage totals, and + * those already counted as failed. Speculative lowering can lower one + * function body several times: each receiver probe of + * `(function () { ... }).call(this)` lowers it. A source statement counts + * once however many times it lowers. */ + private readonly countedStatements = new Set(); + private readonly failedStatements = new Set(); + + /** Adds `node` to the coverage totals unless it is already counted (the + * caller checks suppressStats). */ + countStatement(node: ts.Node, file: string | undefined): void { + if (this.countedStatements.has(node)) return; + this.countedStatements.add(node); + this.stats.statementsTotal++; + if (file !== undefined) this.bumpFileStat(file, "total"); + } + + /** Counts `node` as failed unless it already is (the caller checks + * suppressStats). */ + countStatementFailed(node: ts.Node, file: string | undefined): void { + if (this.failedStatements.has(node)) return; + this.failedStatements.add(node); + this.stats.statementsFailed++; + if (file !== undefined) this.bumpFileStat(file, "failed"); + } + /** Bumps the per-file attribution counter (no-op unless provenance is * active and this is the emit/discovery lane — mirror the CALLER's * suppressStats guard, this method only gates remainder). */ @@ -2942,28 +2968,29 @@ export class Lowerer { readonly moduleOrder: ts.SourceFile[], mode: LowererMode = {}, ) { + // The mode's options, sorted by name; the values derived from them follow. + this.alreadyFlushed = mode.alreadyFlushed ?? new Set(); + this.externalTypes = mode.externalTypes ?? new Map(); + this.ffiBindingSymbols = mode.ffiBindingSymbols ?? null; + this.ffiImports = mode.ffiImports ?? []; + this.forkTargets = mode.forkTargets ?? []; this.frontendServices = mode.frontendServices; + this.inlineRequireCycleCalls = mode.inlineRequireCycleCalls ?? new Set(); + this.libraryCallbacks = mode.libraryCallbacks ?? false; + this.projectCheckJs = mode.projectCheckJs ?? false; this.reachable = mode.reachable ?? null; this.remainder = mode.remainder ?? false; - this.tierMode = mode.tierMode ?? "auto"; - this.alreadyFlushed = mode.alreadyFlushed ?? new Set(); - this.targetPlatform = mode.targetPlatform ?? process.platform; this.startupCrash = mode.startupCrash ?? null; - this.inlineRequireCycleCalls = mode.inlineRequireCycleCalls ?? new Set(); - this.projectCheckJs = mode.projectCheckJs ?? false; - this.ffiImports = mode.ffiImports ?? []; - this.libraryCallbacks = mode.libraryCallbacks ?? false; + this.targetPlatform = mode.targetPlatform ?? process.platform; + this.tierMode = mode.tierMode ?? "auto"; this.workerTargets = mode.workerTargets ?? []; this.workerTargets.forEach((sf, id) => { this.workerTargetIdByPath.set(tsgoPath(resolve(sf.fileName)), id); }); - this.forkTargets = mode.forkTargets ?? []; this.forkTargets.forEach((sf, id) => { this.forkTargetIdByPath.set(tsgoPath(resolve(sf.fileName)), id); }); this.ffiImportsByName = new Map(this.ffiImports.map((entry) => [entry.name, entry])); - this.ffiBindingSymbols = mode.ffiBindingSymbols ?? null; - this.externalTypes = mode.externalTypes ?? new Map(); this.externalTypeSpecifiersByFile = mode.externalTypeSpecifiersByFile ?? directExternalTypeSpecifiersByFile(this.externalTypes); this.checker = program.getTypeChecker(); diff --git a/packages/compiler/src/frontend/type-mapper.ts b/packages/compiler/src/frontend/type-mapper.ts index d61464b07..41d18a6a2 100644 --- a/packages/compiler/src/frontend/type-mapper.ts +++ b/packages/compiler/src/frontend/type-mapper.ts @@ -3267,8 +3267,9 @@ function mapTypeInner(type: ts.Type, ctx: TypeMapperCtx): IrType | null { // Native collection iterators use checked runtime iterator objects. if (isStdlibInterface("MapIterator") || isStdlibInterface("SetIterator")) return DYN; // Array iterators (`list.values()` and friends) are generators over the - // live array: each step reads the current length and element. - if (isStdlibInterface("ArrayIterator")) { + // live array: each step reads the current length and element. String + // iterators (`s[Symbol.iterator]()`) are generators over the code points. + if (isStdlibInterface("ArrayIterator") || isStdlibInterface("StringIterator")) { const arg = checker.getTypeArguments(widened as ts.TypeReference)[0]; const yieldT = arg ? mapType(arg, ctx) : null; if (!yieldT || yieldT.kind === "void" || isUnitType(yieldT)) return null; diff --git a/packages/compiler/src/ir/builtin-signatures.ts b/packages/compiler/src/ir/builtin-signatures.ts index c034f3a2e..13c1e8871 100644 --- a/packages/compiler/src/ir/builtin-signatures.ts +++ b/packages/compiler/src/ir/builtin-signatures.ts @@ -182,6 +182,7 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ "dyn.iteratorCanStep": { argTypes: [DYN, DYN], result: BOOL }, "dyn.iteratorStep": { argTypes: [DYN], result: DYN }, "dyn.iteratorStepDone": { argTypes: [DYN], result: BOOL }, + "dyn.collectionIteratorIndex": { argTypes: [DYN], result: F64 }, "dyn.mapSeedEntries": { argTypes: [DYN], result: DYN }, "dyn.mapSeedEntry": { argTypes: [DYN], result: DYN }, /** The for-of-over-dyn pack accessors — the emitted index loop drives @@ -3427,6 +3428,8 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ "regex.groupEquals": { argTypes: [arrayOf(STRING), F64, STRING], result: BOOL }, "regex.groupNumber": { argTypes: [arrayOf(STRING), F64], result: F64 }, "regex.groupParseInt": { argTypes: [arrayOf(STRING), F64, F64], result: F64 }, + // `.index` / `.input` (args[1]) of a match result. Borrowed; never throws. + "regex.resultProp": { argTypes: [arrayOf(STRING), STRING], result: DYN }, // node:events EventEmitter: receivers are emitter-hierarchy objects and // the chaining forms (on/off/removeAll/setMax) return the receiver's // own class — program-dependent object types, checked in the libCall diff --git a/packages/compiler/src/ir/function-references.test.ts b/packages/compiler/src/ir/function-references.test.ts index ab4b05c8b..117bba24a 100644 --- a/packages/compiler/src/ir/function-references.test.ts +++ b/packages/compiler/src/ir/function-references.test.ts @@ -143,6 +143,7 @@ const FUNCTION_FIELDS: Record>, Role> = { "captures[].localId": "other", "classCaptures[].name": "other", "classCaptures[].localId": "other", + "generator.tag": "other", }; const MODULE_FIELDS: Record>, Role> = { diff --git a/packages/compiler/src/ir/int32-slots.ts b/packages/compiler/src/ir/int32-slots.ts index 7ea3d6bde..c4180fd5b 100644 --- a/packages/compiler/src/ir/int32-slots.ts +++ b/packages/compiler/src/ir/int32-slots.ts @@ -373,6 +373,7 @@ const DYN_READ_ONLY = new Set([ "dyn.iteratorResult", "dyn.iteratorStep", "dyn.iteratorStepDone", + "dyn.collectionIteratorIndex", "dyn.mapSeedEntries", "dyn.mapSeedEntry", "dyn.numberConstructor", diff --git a/packages/compiler/src/ir/intrinsic-signatures.ts b/packages/compiler/src/ir/intrinsic-signatures.ts index 3c224f8bc..eda569883 100644 --- a/packages/compiler/src/ir/intrinsic-signatures.ts +++ b/packages/compiler/src/ir/intrinsic-signatures.ts @@ -53,11 +53,17 @@ export const REGEX_INTRINSIC_SIGS: Record< matchGroups: { receiver: STRING, argTypes: [REGEX, F64, F64], result: VOID }, lastIndex: { receiver: REGEX, argTypes: [], result: F64 }, matchAll: { receiver: STRING, argTypes: [REGEX], result: arrayOf(arrayOf(STRING)) }, + matchAllCaptures: { receiver: STRING, argTypes: [REGEX], result: arrayOf(arrayOf(STRING)) }, matchAllInto: { receiver: STRING, argTypes: [REGEX, arrayOf(F64)], result: arrayOf(arrayOf(STRING)), }, + matchAllCapturesInto: { + receiver: STRING, + argTypes: [REGEX, arrayOf(F64)], + result: arrayOf(arrayOf(STRING)), + }, search: { receiver: STRING, argTypes: [REGEX], result: F64 }, source: { receiver: REGEX, argTypes: [], result: STRING }, flags: { receiver: REGEX, argTypes: [], result: STRING }, diff --git a/packages/compiler/src/ir/ir.ts b/packages/compiler/src/ir/ir.ts index 343f52617..d2e6129cd 100644 --- a/packages/compiler/src/ir/ir.ts +++ b/packages/compiler/src/ir/ir.ts @@ -1712,6 +1712,15 @@ export interface IrFunction { * iterator helper's source): `.return()` before the first `.next()` * closes it. */ closesSource?: true; + /** The @@toStringTag the object prints with (util.inspect, + * Object.prototype.toString) when it is not a generator object: + * "Iterator Helper", "Array Iterator", "String Iterator". A tag that + * starts with '!' marks a generator standing in for an object of + * another class, which those refuse to print. */ + tag?: string; + /** The first parameter is a generator this one adapts: it prints with + * that generator's tag. */ + inheritsTag?: true; }; /** Source function name for native error stack frames. */ sourceName?: string; @@ -2431,13 +2440,21 @@ export type IrRegexIntrinsicMethod = * unobservable across the lowered surface (strings are immutable; the * spec clones the regex at the call, so lastIndex games can't reach the * drain either). Non-global regexes THROW Node's exact TypeError - * (catchable — replaceAll's stance). */ + * (catchable — replaceAll's stance). Each row carries its index, input + * and groups properties. */ | "matchAll" + /** matchAll whose rows are read only through their elements + * (matchAllRowsReadByElements): the rows carry no index, input or + * groups properties. Same result and throw contract as matchAll. */ + | "matchAllCaptures" /** matchAll's companion-index form (the for-of-over-matchAll desugar): * args[1] is a number[] the drain ALSO fills with each match's UTF-16 * start index — the row's `.index`, always present (every drained row * matched). Same result and throw contract as matchAll. */ | "matchAllInto" + /** matchAllInto whose rows are read only through their elements and the + * companion index: the rows carry no index, input or groups properties. */ + | "matchAllCapturesInto" /** `s.search(re)` — the first match's UTF-16 index, or -1. Symbol.search * neither reads nor writes lastIndex (a fresh exec from position 0), so * no g/y fence applies: /g is irrelevant and /y anchors at 0 — exactly diff --git a/packages/compiler/src/ir/runtime-features.ts b/packages/compiler/src/ir/runtime-features.ts index c33c83e8e..482dc7fae 100644 --- a/packages/compiler/src/ir/runtime-features.ts +++ b/packages/compiler/src/ir/runtime-features.ts @@ -24,6 +24,11 @@ export interface RuntimeFeatures { assert: boolean; dynInvoke: boolean; dynAsync: boolean; + /** The program enters AsyncLocalStorage stores (store.run, enterWith, + * a diagnostics_channel store binding): its timer, immediate, tick and + * queueMicrotask scheduling goes through the runtime's *_als entry + * points, which carry the active stores into each callback. */ + asyncContext: boolean; inspect: boolean; childProcess: boolean; net: boolean; @@ -108,6 +113,16 @@ const DYN_ASYNC_LIB_FNS: ReadonlySet = new Set([ "dc.chanRunStores", ]); +/** The libCalls that enter AsyncLocalStorage stores (RuntimeFeatures + * asyncContext). */ +const ASYNC_CONTEXT_LIB_FNS: ReadonlySet = new Set([ + "als.run", + "als.exitRun", + "als.enterWith", + "dc.chanBindStore", + "dc.chanRunStores", +]); + const LEGACY_URL_LIB_FNS: ReadonlySet = new Set([ "url.legacyParse", "url.legacyFormat", @@ -123,6 +138,178 @@ const UTIL_CALLABLE_LIB_FNS: ReadonlySet = new Set([ "util.debuglog", ]); +/** Entry points that link the regex unit: the regex and glob matchers (fs.glob's walk compiles its patterns with the glob matcher), and readline's terminal unit (scr_readline_tty.c), which measures with the regex unit's stripVTControlCharacters. */ +const REGEX_LIB_FNS: ReadonlySet = new Set([ + "dyn.nativeRegexIs", + "fs.callbackGlob", + "fs.globSync", + "fs.globTypesSync", + "fsp.globError", + "fsp.globStrings", + "fsp.globTypes", + "path.matchesGlob", + "path.win32MatchesGlob", + "regexp.escape", + "rl.cursorPos", + "rl.keypressStdin", + "rl.keypressStream", + "stdin.onKeypress", + "util.stripVTControlCharacters", +]); + +/** TextDecoder entry points over the legacy encodings. */ +const LEGACY_TEXT_DECODER_LIB_FNS: ReadonlySet = new Set([ + "text.decodeLegacy", + "text.decodeLegacyOptions", + "text.decodeStream", +]); + +/** Signal-taking readline, stream and timers/promises entry points, which register abort listeners. */ +const ABORT_SIGNAL_LIB_FNS: ReadonlySet = new Set([ + "http.clientSignal", + "rl.createSignal", + "rl.questionAsyncSignal", + "sp.finishedOptsSignal", + "sp.pipelineOptsSignal", + "stream.addAbortSignal", + "stream.addAbortSignalFn", + "stream.finishedOptsSignal", + "tp.schedulerWaitSignal", + "tp.setImmediateSignal", + "tp.setTimeoutSignal", +]); + +/** Entry points that link the stream unit (with STREAM_LIB_PREFIXES). */ +const STREAM_LIB_FNS: ReadonlySet = new Set([ + "console.bind", + "crypto.hashStream", + "crypto.hashStreamPiped", + "fileHandle.createReadStream", + "fileHandle.createWriteStream", + "fs.createReadStream", + "fs.createWriteStream", + "sp.finished", + "sp.pipeline", + "sp.pipelineStages", + "stream.addAbortSignal", + "stream.addAbortSignalFn", + "stream.destroy", + "stream.destroyErr", + "stream.errored", + "stream.prop", + "stream.workerStdio", +]); + +/** libCall name prefixes that link the stream unit. */ +const STREAM_LIB_PREFIXES: readonly string[] = [ + "duplex.", + "fs.fsStream", + "passthrough.", + "readable.", + "sc.", + "sp.finishedOpts", + "sp.pipelineOpts", + "stream.finishedOpts", + "stream.pipelineStages", + "stream.set", + "transform.", + "writable.", + "zlib.stream", +]; + +/** The Readable operators and Readable.from over a compiled generator or a checked-dynamic iterator (scr_stream_ops.c), which await through the dyn-async unit (with READABLE_DYN_ASYNC_PREFIXES). */ +const READABLE_DYN_ASYNC_LIB_FNS: ReadonlySet = new Set([ + "readable.fromAsyncGen", + "readable.fromAsyncIter", + "readable.fromDone", + "readable.fromDyn", + "readable.fromIter", +]); + +/** libCall name prefixes of the Readable entry points that await through the dyn-async unit. */ +const READABLE_DYN_ASYNC_PREFIXES: readonly string[] = ["readable.fromGen", "readable.op"]; + +/** Entry points that link the dyn-invoke unit. */ +const DYN_INVOKE_LIB_FNS: ReadonlySet = new Set([ + "dyn.arrayProtoCall", + "dyn.arrayPrototype", + "dyn.builtinFunction", + "dyn.builtinMethod", + "dyn.defineProperty", + "dyn.defineProps", + "dyn.definePrototypeProps", + "dyn.functionApply", + "dyn.globalConstructor", + "dyn.objCreateWithProperties", + "dyn.objectTag", +]); + +/** Member names whose constant-key dynamic reads answer the inherited Object.prototype member through the dyn-invoke unit. Mirrors expr-dynamic.ts INHERITED_OBJECT_MEMBERS (the IR layer does not import the backend). */ +const INHERITED_MEMBER_NAMES: ReadonlySet = new Set([ + "constructor", + "hasOwnProperty", + "propertyIsEnumerable", + "toString", + "valueOf", +]); + +/** Entry points that link the child-process unit (with CHILD_PROCESS_LIB_PREFIXES): the fork IPC channel on the process object. */ +const CHILD_PROCESS_LIB_FNS: ReadonlySet = new Set([ + "process.connected", + "process.disconnect", + "process.forkTarget", + "process.onDisconnect", + "process.onMessage", + "process.send", + "process.sendCb", +]); + +/** libCall name prefixes that link the child-process unit. */ +const CHILD_PROCESS_LIB_PREFIXES: readonly string[] = ["child.", "cp.", "spawnRes.", "writer."]; + +/** Entry points that link the symbol unit, besides the sym. and disposable. families. */ +const SYMBOL_LIB_FNS: ReadonlySet = new Set([ + "dyn.usingDispose", + "dyn.usingMethod", + "util.isDeepStrictEqual", +]); + +/** Entry points that link the querystring unit. */ +const QS_LIB_FNS: ReadonlySet = new Set(["qs.parse", "qs.stringify", "qs.unescape"]); + +/** Entry points that link the parseArgs unit. */ +const PARSE_ARGS_LIB_FNS: ReadonlySet = new Set([ + "util.isDeepStrictEqual", + "util.parseArgs", + "util.styleText", +]); + +/** String methods whose Unicode tables live in the regex unit's libunicode. */ +const CASE_MAPPING_STR_METHODS: ReadonlySet = new Set([ + "normalize", + "toLowerCase", + "toUpperCase", +]); + +/** Array methods that link the copying unit. */ +const COPYING_ARR_METHODS: ReadonlySet = new Set([ + "toReversed", + "toSpliced", + "with", + "withUndefined", +]); + +/** Typed-array methods that link the copying unit. */ +const COPYING_BYTES_METHODS: ReadonlySet = new Set([ + "join", + "toArray", + "toReversed", + "with", +]); + +const startsWithAny = (fn: string, prefixes: readonly string[]): boolean => + prefixes.some((prefix) => fn.startsWith(prefix)); + /** Inspect types and expressions together in one traversal. This is a fresh * snapshot: callers may mutate an IR module between compilation passes. */ /** The checked-dynamic undefined value (dynUndefinedExpr). */ @@ -208,6 +395,7 @@ function scanRuntimeFeatures(mod: IrModule, stopAt?: keyof RuntimeFeatures): Run mod.functions.some( (fn) => fn.async === true && fn.generator === undefined && fn.returnType.kind === "dyn", ), + asyncContext: false, inspect: false, childProcess: false, net: false, @@ -239,130 +427,33 @@ function scanRuntimeFeatures(mod: IrModule, stopAt?: keyof RuntimeFeatures): Run const expr = (node: IrExpr): boolean => { if (node.kind === "libCall") { const fn = node.fn; - if ( - fn === "regexp.escape" || - fn === "dyn.nativeRegexIs" || - fn === "util.stripVTControlCharacters" || - fn === "path.matchesGlob" || - fn === "path.win32MatchesGlob" || - // fs.glob's walk compiles its patterns with the glob matcher. - fn === "fs.globSync" || - fn === "fs.globTypesSync" || - fn === "fs.callbackGlob" || - fn === "fsp.globStrings" || - fn === "fsp.globTypes" || - fn === "fsp.globError" - ) - features.regex = true; - if ( - fn === "text.decodeLegacy" || - fn === "text.decodeLegacyOptions" || - fn === "text.decodeStream" - ) - features.legacyTextDecoder = true; + if (REGEX_LIB_FNS.has(fn)) features.regex = true; + if (LEGACY_TEXT_DECODER_LIB_FNS.has(fn)) features.legacyTextDecoder = true; if (fn.startsWith("fetch.")) features.fetch = true; if (fn.startsWith("event.") || fn === "events.addAbortListener") features.eventTarget = true; - // Signal-taking readline, stream and timers/promises entry points - // register abort listeners. - if ( - fn === "rl.createSignal" || - fn === "rl.questionAsyncSignal" || - fn === "http.clientSignal" || - fn === "tp.setTimeoutSignal" || - fn === "tp.schedulerWaitSignal" || - fn === "tp.setImmediateSignal" || - fn === "stream.addAbortSignal" || - fn === "stream.addAbortSignalFn" || - fn === "sp.finishedOptsSignal" || - fn === "sp.pipelineOptsSignal" || - fn === "stream.finishedOptsSignal" - ) { - features.eventTarget = true; - } + if (ABORT_SIGNAL_LIB_FNS.has(fn)) features.eventTarget = true; if (PROCESS_EVENT_LIB_FNS.has(fn)) features.processEvents = true; - // readline's terminal unit (scr_readline_tty.c) measures with the - // regex unit's stripVTControlCharacters; with a completer it prints - // completion errors through the inspect unit. - if ( - fn === "rl.cursorPos" || - fn === "rl.keypressStdin" || - fn === "rl.keypressStream" || - fn === "stdin.onKeypress" - ) - features.regex = true; + // With a completer, readline's terminal unit prints completion errors + // through the inspect unit. if (fn === "rl.terminal") { features.regex = true; if (!isDynUndefined(node.args[3]!)) features.inspect = true; } if (fn.startsWith("emitter.")) features.emitter = true; - if ( - fn.startsWith("readable.") || - fn.startsWith("writable.") || - fn.startsWith("duplex.") || - fn.startsWith("transform.") || - fn.startsWith("passthrough.") || - fn.startsWith("sc.") || - fn.startsWith("stream.set") || - fn === "stream.destroy" || - fn === "stream.destroyErr" || - fn === "stream.prop" || - fn === "stream.workerStdio" || - fn === "stream.errored" || - fn === "sp.finished" || - fn === "sp.pipeline" || - fn === "sp.pipelineStages" || - fn.startsWith("stream.pipelineStages") || - fn === "console.bind" || - fn === "crypto.hashStream" || - fn === "crypto.hashStreamPiped" || - fn === "stream.addAbortSignal" || - fn === "stream.addAbortSignalFn" || - fn.startsWith("sp.finishedOpts") || - fn.startsWith("sp.pipelineOpts") || - fn.startsWith("stream.finishedOpts") || - fn.startsWith("zlib.stream") || - fn === "fs.createReadStream" || - fn === "fs.createWriteStream" || - fn === "fileHandle.createReadStream" || - fn === "fileHandle.createWriteStream" || - fn.startsWith("fs.fsStream") - ) { + if (STREAM_LIB_FNS.has(fn) || startsWithAny(fn, STREAM_LIB_PREFIXES)) { // the stream unit emits through the emitter unit, also when the // program constructs no stream (a pipeline of functions) features.stream = true; features.emitter = true; } if (fn.startsWith("zlib.")) features.zlib = true; - // The Readable operators and Readable.from over a compiled generator - // or a checked-dynamic iterator (scr_stream_ops.c) await through the - // dyn-async unit. - if ( - fn.startsWith("readable.op") || - fn.startsWith("readable.fromGen") || - fn === "readable.fromDyn" || - fn === "readable.fromIter" || - fn === "readable.fromAsyncIter" || - fn === "readable.fromAsyncGen" || - fn === "readable.fromDone" - ) + if (READABLE_DYN_ASYNC_LIB_FNS.has(fn) || startsWithAny(fn, READABLE_DYN_ASYNC_PREFIXES)) features.dynAsync = true; if (fn.startsWith("dc.")) features.dc = true; if (fn.startsWith("assert.")) features.assert = true; - if ( - fn === "dyn.defineProps" || - fn === "dyn.definePrototypeProps" || - fn === "dyn.defineProperty" || - fn === "dyn.objCreateWithProperties" || - fn === "dyn.arrayProtoCall" || - fn === "dyn.arrayPrototype" || - fn === "dyn.functionApply" || - fn === "dyn.builtinMethod" || - fn === "dyn.globalConstructor" || - fn === "dyn.objectTag" || - fn === "dyn.builtinFunction" - ) - features.dynInvoke = true; + if (DYN_INVOKE_LIB_FNS.has(fn)) features.dynInvoke = true; if (DYN_ASYNC_LIB_FNS.has(fn)) features.dynAsync = true; + if (ASYNC_CONTEXT_LIB_FNS.has(fn)) features.asyncContext = true; if ( fn.startsWith("insp.") || fn.startsWith("console.") || @@ -370,19 +461,7 @@ function scanRuntimeFeatures(mod: IrModule, stopAt?: keyof RuntimeFeatures): Run fn === "util.styleText" ) features.inspect = true; - if ( - fn.startsWith("cp.") || - fn.startsWith("child.") || - fn.startsWith("writer.") || - fn.startsWith("spawnRes.") || - fn === "process.forkTarget" || - fn === "process.connected" || - fn === "process.send" || - fn === "process.sendCb" || - fn === "process.disconnect" || - fn === "process.onMessage" || - fn === "process.onDisconnect" - ) + if (CHILD_PROCESS_LIB_FNS.has(fn) || startsWithAny(fn, CHILD_PROCESS_LIB_PREFIXES)) features.childProcess = true; if (fn.startsWith("net.")) features.net = true; if (fn.startsWith("http.")) { @@ -395,18 +474,12 @@ function scanRuntimeFeatures(mod: IrModule, stopAt?: keyof RuntimeFeatures): Run features.tls = true; } if (fn.startsWith("http2.") && !HTTP2_LEGACY_FNS.has(fn)) features.http2 = true; - if ( - fn.startsWith("sym.") || - fn === "util.isDeepStrictEqual" || - fn === "dyn.usingMethod" || - fn === "dyn.usingDispose" || - fn.startsWith("disposable.") - ) + if (fn.startsWith("sym.") || fn.startsWith("disposable.") || SYMBOL_LIB_FNS.has(fn)) features.symbol = true; if (fn === "process.hrtimeValue") features.bigint = true; if (fn.startsWith("bigint.")) features.bigint = true; if (fn.startsWith("sp.") || fn === "url.searchParams") features.searchParams = true; - if (fn === "qs.parse" || fn === "qs.stringify" || fn === "qs.unescape") features.qs = true; + if (QS_LIB_FNS.has(fn)) features.qs = true; // scr_url_legacy.c lowercases and normalizes hostnames (the regex // unit's libunicode), parses and stringifies queries, and emits its // deprecation warnings through the dyn-async unit. @@ -415,8 +488,7 @@ function scanRuntimeFeatures(mod: IrModule, stopAt?: keyof RuntimeFeatures): Run features.qs = true; features.dynAsync = true; } - if (fn === "util.parseArgs" || fn === "util.isDeepStrictEqual" || fn === "util.styleText") - features.parseArgs = true; + if (PARSE_ARGS_LIB_FNS.has(fn)) features.parseArgs = true; if (fn === "util.styleText") features.processEvents = true; // scr_util_callable.c: dyn wrappers over the descriptor, inspect, // regex (NODE_DEBUG matching) and terminal-color units. @@ -453,30 +525,13 @@ function scanRuntimeFeatures(mod: IrModule, stopAt?: keyof RuntimeFeatures): Run features.regex = true; break; case "strIntrinsic": - if ( - node.method === "toLowerCase" || - node.method === "toUpperCase" || - node.method === "normalize" - ) - features.regex = true; + if (CASE_MAPPING_STR_METHODS.has(node.method)) features.regex = true; break; case "arrIntrinsic": - if ( - node.method === "toReversed" || - node.method === "toSpliced" || - node.method === "with" || - node.method === "withUndefined" - ) - features.copying = true; + if (COPYING_ARR_METHODS.has(node.method)) features.copying = true; break; case "bytesIntrinsic": - if ( - node.method === "toReversed" || - node.method === "with" || - node.method === "join" || - node.method === "toArray" - ) - features.copying = true; + if (COPYING_BYTES_METHODS.has(node.method)) features.copying = true; break; case "dynInvoke": features.dynInvoke = true; @@ -884,13 +939,3 @@ export function moduleUsesTls(mod: IrModule): boolean { export function moduleUsesTlsCa(mod: IrModule): boolean { return scanRuntimeFeatures(mod, "tlsCa").tlsCa; } - -/** Mirrors expr-dynamic.ts INHERITED_OBJECT_MEMBERS (the IR layer does not - * import the backend). */ -const INHERITED_MEMBER_NAMES: ReadonlySet = new Set([ - "constructor", - "valueOf", - "toString", - "hasOwnProperty", - "propertyIsEnumerable", -]); diff --git a/packages/compiler/test/match-row-emission.test.ts b/packages/compiler/test/match-row-emission.test.ts new file mode 100644 index 000000000..39e07bc1c --- /dev/null +++ b/packages/compiler/test/match-row-emission.test.ts @@ -0,0 +1,69 @@ +import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, expect, test } from "vitest"; +import { compile } from "../src/index.js"; + +const directories: string[] = []; +afterEach(async () => { + await Promise.all(directories.splice(0).map((dir) => rm(dir, { recursive: true, force: true }))); +}); + +async function emit(source: string): Promise { + const outDir = await mkdtemp(join(tmpdir(), "scriptc-match-rows-")); + directories.push(outDir); + const entry = join(outDir, "main.ts"); + await writeFile(entry, source); + const outPath = join(outDir, "program.ll"); + const result = await compile(entry, { entry, outDir, outPath, outputKind: "llvm" }); + expect(result.ok, JSON.stringify(result)).toBe(true); + return readFile(outPath, "utf8"); +} + +const calls = (llvm: string, symbol: string): number => + llvm.split("\n").filter((line) => line.includes("call ") && line.includes(`@${symbol}(`)).length; + +test("rows read only through their elements carry no match metadata", async () => { + const llvm = await emit(` +const text = "k1=v1; k2=v2;"; +const re = /(?k\\d)=(?v\\d);/g; +for (const m of text.matchAll(re)) console.log(m.index, m[1], m.groups!.val, 1 in m); +console.log([...text.matchAll(re)].map((m) => m[2]).join(",")); +console.log([...text.matchAll(re)].filter((m) => m.length > 2).find((m) => m[1] === "k2")?.[2]); +console.log(Array.from(text.matchAll(re), ({ groups }) => groups!.key).join(",")); +const rows = [...text.matchAll(re)]; +for (const row of rows.slice(1)) console.log(row[0]); +`); + expect(calls(llvm, "scr_regex_match_all_captures_into")).toBe(1); + expect(calls(llvm, "scr_regex_match_all_captures")).toBe(4); + expect(calls(llvm, "scr_regex_match_all")).toBe(0); + expect(calls(llvm, "scr_regex_match_all_into")).toBe(0); +}); + +test("rows whose properties are read or that escape keep match metadata", async () => { + const llvm = await emit(` +const text = "k1=v1; k2=v2;"; +const re = /(?k\\d)=(?v\\d);/g; +for (const m of text.matchAll(re)) console.log(m.input.length); +console.log([...text.matchAll(re)].map((m) => m.index).join(",")); +function first(m: RegExpExecArray): string { + return m[1]!; +} +console.log([...text.matchAll(re)].map(first).join(",")); +console.log([...text.matchAll(re)].map((m) => m)[0]![1]); +`); + expect(calls(llvm, "scr_regex_match_all_into")).toBe(1); + expect(calls(llvm, "scr_regex_match_all")).toBe(3); + expect(calls(llvm, "scr_regex_match_all_captures")).toBe(0); + expect(calls(llvm, "scr_regex_match_all_captures_into")).toBe(0); +}); + +test(".index and .input on a match slice read its match source", async () => { + const llvm = await emit(` +const rows = [..."a1b2".matchAll(/[a-z](\\d)/g)]; +console.log(rows[1]!.index, rows[0]!.input); +const m = /b/.exec("ab")!; +console.log(m.index); +`); + expect(calls(llvm, "scr_regex_result_prop")).toBe(3); +}); diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5815-als-callback-context.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5815-als-callback-context.ts.json new file mode 100644 index 000000000..f192a6b22 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5815-als-callback-context.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/5815-als-callback-context.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6010-match-metadata.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6010-match-metadata.ts.json new file mode 100644 index 000000000..d2b199768 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6010-match-metadata.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6010-match-metadata.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6011-match-metadata-js.js.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6011-match-metadata-js.js.json new file mode 100644 index 000000000..437b28911 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6011-match-metadata-js.js.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6011-match-metadata-js.js" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6071-iterator-sources.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6071-iterator-sources.ts.json new file mode 100644 index 000000000..2745f7978 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6071-iterator-sources.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6071-iterator-sources.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6072-iterator-sources-js.js.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6072-iterator-sources-js.js.json new file mode 100644 index 000000000..e89c895e4 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6072-iterator-sources-js.js.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6072-iterator-sources-js.js" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6073-stored-collection-helpers.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6073-stored-collection-helpers.ts.json new file mode 100644 index 000000000..6d9eeff70 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6073-stored-collection-helpers.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6073-stored-collection-helpers.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6074-stored-collection-helpers-js.js.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6074-stored-collection-helpers-js.js.json new file mode 100644 index 000000000..fbbaf0df4 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6074-stored-collection-helpers-js.js.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6074-stored-collection-helpers-js.js" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6075-print-iterators.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6075-print-iterators.ts.json new file mode 100644 index 000000000..04adc830d --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6075-print-iterators.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6075-print-iterators.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6076-print-iterators-js.js.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6076-print-iterators-js.js.json new file mode 100644 index 000000000..255e32494 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6076-print-iterators-js.js.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6076-print-iterators-js.js" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6200-http-agent-free-socket-data.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6200-http-agent-free-socket-data.ts.json new file mode 100644 index 000000000..e8df3e384 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6200-http-agent-free-socket-data.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6200-http-agent-free-socket-data.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/string-iterator-helpers.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/string-iterator-helpers.ts.json deleted file mode 100644 index 0a716bec5..000000000 --- a/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/string-iterator-helpers.ts.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "order": [ - "/tests/diagnostics/string-iterator-helpers.ts" - ], - "diags": [] -} diff --git a/packages/runtime/src/scr_alloc.c b/packages/runtime/src/scr_alloc.c index 2212a1a36..246d27f0a 100644 --- a/packages/runtime/src/scr_alloc.c +++ b/packages/runtime/src/scr_alloc.c @@ -638,6 +638,7 @@ static _Thread_local ScrSaKeep *scr_sa_keep; /* while the worker's memory is kep static void scr_sa_keep_begin(unsigned i); static void scr_sa_keep_drained(ScrSaKeep *k, ScrSaBlock *b, unsigned c); static bool scr_sa_drain(void); +static bool scr_sa_drain_some(bool one); #endif /* Moves every block other threads freed into this arena's free lists. */ @@ -672,8 +673,8 @@ static bool scr_sa_reserve(void) { if (claimed == UINT32_MAX) { #ifdef SCR_SA_TRIM /* Arenas held by discards that wait (see scr_sa_thread_park): this - * thread discards them and claims one. */ - if (scr_sa_drain()) { + * thread discards one of them and claims it. */ + if (scr_sa_drain_some(true)) { scr_sa_refused = false; claimed = atomic_load_explicit(&scr_sa_claimed, memory_order_relaxed); continue; @@ -934,6 +935,12 @@ static void scr_sa_trim(void) { * interrupts every running thread of the process to flush its TLB, and * glibc's trim locks the arenas other threads allocate from, so discards * wait for the last worker of a group, which hands them to one helper; + * - the helper waits SCR_SA_DISCARD_DELAY_MS (100 by default) and stops if + * a worker started meanwhile: a program that starts its next workers or + * exits within that time does not return pages it would fault back in or + * never touch again, and the last worker of the next group hands the + * discards to a new helper; a thread that finds every arena claimed + * discards one waiting arena and claims it; * - the helper then returns the pages * it carved (MADV_DONTNEED; the start of the first page, which it may * share with older blocks, is cleared), frees the recorded system blocks, @@ -1276,11 +1283,13 @@ static void scr_sa_defer_discard(unsigned i, ScrSaKeep *k) { pthread_mutex_unlock(&scr_sa_pending_lock); } -/* Runs the waiting discards on the calling thread, which ends with no arena; - * returns whether there were any. */ -static bool scr_sa_drain(void) { +/* Runs the waiting discards on the calling thread, which ends with no arena: + * all of them, or only the first one when `one` is set; returns whether + * there were any. */ +static bool scr_sa_drain_some(bool one) { bool any = false; for (;;) { + if (one && any) return true; pthread_mutex_lock(&scr_sa_pending_lock); uint32_t pending = scr_sa_pending; unsigned i = pending ? (unsigned)__builtin_ctz(pending) : 0; @@ -1298,9 +1307,30 @@ static bool scr_sa_drain(void) { } } +static bool scr_sa_drain(void) { return scr_sa_drain_some(false); } + +/* SCR_SA_DISCARD_DELAY_MS: how long the trim helper waits after the last + * worker parks before it returns their memory (default 100). */ +static long scr_sa_discard_delay_ms(void) { + const char *delay = getenv("SCR_SA_DISCARD_DELAY_MS"); + if (!delay) return 100; + long ms = strtol(delay, NULL, 10); + return ms < 0 ? 0 : ms; +} + +/* Waits out the delay first, and stops if a worker starts meanwhile: the + * discards then wait for the last worker of that group. A program that exits + * within the delay does not spend its last milliseconds returning pages, and + * one that starts its next workers within it does not return pages while + * they start. */ static void *scr_sa_drain_main(void *arg) { (void)arg; - scr_sa_drain(); + for (long left = scr_sa_discard_delay_ms(); left > 0; left -= 5) { + struct timespec pause = {0, (left < 5 ? left : 5) * 1000000L}; + nanosleep(&pause, NULL); + if (atomic_load_explicit(&scr_sa_workers_live, memory_order_acquire) != 0) return NULL; + } + if (atomic_load_explicit(&scr_sa_workers_live, memory_order_acquire) == 0) scr_sa_drain(); return NULL; } diff --git a/packages/runtime/src/scr_alloc.test.c b/packages/runtime/src/scr_alloc.test.c index f0efdc935..aa6b0b60b 100644 --- a/packages/runtime/src/scr_alloc.test.c +++ b/packages/runtime/src/scr_alloc.test.c @@ -627,7 +627,7 @@ static void *small_worker(void *arg) { /* Discards wait while another worker runs, and the last worker to park * starts them. When every arena is held by a discard that waits, a worker - * that needs one runs them itself. */ + * that needs one discards one of them and claims it. */ static void check_deferred_discard(void) { size_t page = (size_t)sysconf(_SC_PAGESIZE); atomic_store(&discard_step, 0); @@ -656,6 +656,55 @@ static void check_deferred_discard(void) { discard_in_old_arena = true; } +/* A plain thread that holds an arena until discard_step reaches 3. */ +static void *holding_thread(void *arg) { + (void)arg; + void *p = scr_mem_alloc(DISCARD_SIZE); + assert(p && owned(p)); + atomic_store(&discard_step, 1); + wait_step(3); + scr_mem_free(p); + scr_sa_thread_park(); + return NULL; +} + +/* The helper waits SCR_SA_DISCARD_DELAY_MS before it returns the pages, and + * stops if a worker starts meanwhile; the last worker of that group starts + * the discards again. A plain thread holds the arena whose slice the earlier + * checks filled, so the worker carves a slice of its own. */ +static void check_discard_delay(void) { + size_t page = (size_t)sysconf(_SC_PAGESIZE); + setenv("SCR_SA_DISCARD_DELAY_MS", "300", 1); + atomic_store(&discard_step, 0); + pthread_t plain; + assert(pthread_create(&plain, NULL, holding_thread, NULL) == 0); + wait_step(1); + discard_shares = false; + discard_in_old_arena = false; + pthread_t t; + assert(pthread_create(&t, NULL, discard_worker, NULL) == 0); + assert(pthread_join(t, NULL) == 0); + atomic_store(&discard_step, 0); + pthread_t holder; + assert(pthread_create(&holder, NULL, holding_worker, NULL) == 0); + wait_step(1); + /* Past the delay: the helper stopped when the holder started. */ + struct timespec pause = {0, 600000000L}; + nanosleep(&pause, NULL); + uintptr_t first = (discard_lo + page - 1) & ~(uintptr_t)(page - 1); + size_t total = (discard_hi - first) / page; + assert(resident_pages(first, discard_hi) > total / 2); + for (size_t i = 0; i < DISCARD_BLOCKS; i += 4099) verify(discard_blocks[i], block_size, (unsigned char)i); + setenv("SCR_SA_DISCARD_DELAY_MS", "0", 1); + atomic_store(&discard_step, 3); + assert(pthread_join(holder, NULL) == 0); + assert(pthread_join(plain, NULL) == 0); + scr_sa_wait_trims(); + unsetenv("SCR_SA_DISCARD_DELAY_MS"); + assert(resident_pages(first, discard_hi) < total / 2); + discard_in_old_arena = true; +} + static void check_discard(void) { discard_blocks = malloc(DISCARD_BLOCKS * sizeof *discard_blocks); assert(discard_blocks); @@ -750,6 +799,7 @@ static void check_discard(void) { free(trim_blocks); check_deferred_discard(); + check_discard_delay(); for (unsigned i = 0; i < DISCARD_OLD; i++) scr_mem_free(old_blocks[i]); free(discard_blocks); } diff --git a/packages/runtime/src/scr_array.c b/packages/runtime/src/scr_array.c index 4ef24e906..1f4efc321 100644 --- a/packages/runtime/src/scr_array.c +++ b/packages/runtime/src/scr_array.c @@ -40,6 +40,8 @@ static void scr_arr_oom(void) { /* Readers of the out-of-line storage see an all-empty one until the first * sparse entry, noncanonical property or metadata allocates it. */ static const ScrArrExt scr_arr_no_ext; + +SCR_TL bool (*scr_arr_match_copy)(ScrArr *a, ScrDyn *target) = NULL; #define SCR_AX(a) ((const ScrArrExt *)((a)->ext ? (a)->ext : &scr_arr_no_ext)) ScrArrExt *scr_arr_ext(ScrArr *a) { @@ -4423,6 +4425,10 @@ ScrStr *scr_str_raw(ScrArr *raw, ScrArr *subs) { } void scr_arr_copy_metadata(ScrArr *a, ScrDyn *target) { + const ScrDyn *metadata = SCR_AX(a)->metadata; + if (metadata && metadata->kind == SCR_DYN_HANDLE && metadata->v.handle.tag == SCR_DYNH_MATCH_SOURCE && + scr_arr_match_copy(a, target)) + return; if (!SCR_AX(a)->metadata) return; for (size_t i = 0; i < SCR_AX(a)->metadata->v.obj.len; i++) { ScrDynEntry *entry = &SCR_AX(a)->metadata->v.obj.entries[i]; diff --git a/packages/runtime/src/scr_array.h b/packages/runtime/src/scr_array.h index d02c2097e..5289f8c13 100644 --- a/packages/runtime/src/scr_array.h +++ b/packages/runtime/src/scr_array.h @@ -105,7 +105,12 @@ typedef struct ScrArrExt { struct ScrArrProp *props; /* noncanonical numeric properties */ size_t prop_len; size_t prop_cap; - struct ScrDyn *metadata; /* heterogeneous named properties of match results */ + /* Heterogeneous named properties of match results. Until a result's + * metadata is first needed, this is the match source handle that the + * results of one exec or matchAll call share (SCR_DYNH_MATCH_SOURCE, + * scr_regex.c), and match_index is the result's UTF-16 start index. */ + struct ScrDyn *metadata; + double match_index; } ScrArrExt; /* The LLVM backend mirrors ScrArr as %ScrArr: its inline retain/release read * `cyc` (field 4, i8) to decide mark-live / candidate buffering exactly like @@ -207,6 +212,11 @@ static inline ScrArr *scr_arr_retain(ScrArr *a) { * the candidate-root buffer like every headered type. */ void scr_arr_release(ScrArr *a); void scr_arr_copy_metadata(ScrArr *a, struct ScrDyn *target); +/* Installed by scr_regex.c when it records a pending match source; this + * unit links without the regex runtime. Writes the pending metadata of + * `a` into its dynamic copy or materializes it, answering whether it + * wrote. */ +extern SCR_TL bool (*scr_arr_match_copy)(ScrArr *a, struct ScrDyn *target); /* The array's out-of-line storage, allocated (zeroed) on first use. */ ScrArrExt *scr_arr_ext(ScrArr *a); /* Moves inline dense slots (kept in the header block of a small array) to diff --git a/packages/runtime/src/scr_async.c b/packages/runtime/src/scr_async.c index f630ee5f9..583b1f6d0 100644 --- a/packages/runtime/src/scr_async.c +++ b/packages/runtime/src/scr_async.c @@ -694,6 +694,26 @@ static inline void *scr_fiber_region_capture(void) { return regions != NULL ? regions->capture() : NULL; } +/* enterWith on the main stack lasts until that code returns to the loop, + * so the store does not reach callbacks scheduled without it: the loop + * clears the main slot when the main module's body returns, and an + * enterWith inside a callback the loop runs puts a tick that clears it at + * the front of the tick queue, so it runs as soon as that callback + * returns. */ +static SCR_TL bool scr_loop_started = false; + +static void scr_als_main_clear(void) { + scr_als_ctx_release(scr_als_main_slot); + scr_als_main_slot = NULL; +} + +static void scr_next_tick_raw_first(void (*fn)(void)); + +void scr_als_entered_with(void) { + if (scr_loop_started && SCR_ALS_SLOT() == &scr_als_main_slot) + scr_next_tick_raw_first(scr_als_main_clear); +} + static SCR_TL ScrCtx scr_loop_ctx; /* the main stack (scheduler home) */ static SCR_TL ScrFiber *scr_current = NULL; /* A finished fiber whose last switch went to the loop (scr_resume_fiber). */ @@ -1168,6 +1188,17 @@ void scr_next_tick_raw(void (*fn)(void)) { scr_nt_tail = t; } +/* A raw tick at the front of the queue: it runs before the ticks already + * queued. */ +static void scr_next_tick_raw_first(void (*fn)(void)) { + ScrNtick *t = calloc(1, sizeof *t); + if (!t) scr_oom(); + t->raw = fn; + t->next = scr_nt_head; + scr_nt_head = t; + if (scr_nt_tail == NULL) scr_nt_tail = t; +} + void scr_nticks_teardown(void) { while (scr_nt_head != NULL) { ScrNtick *t = scr_nt_head; @@ -3731,6 +3762,54 @@ void scr_queue_microtask(ScrClosure *cb) { scr_ready_push(f); } +/* ── scheduling in an AsyncLocalStorage context ─────────────────────── + * Programs that enter AsyncLocalStorage stores schedule their timer, + * interval, setImmediate, process.nextTick and queueMicrotask callbacks + * through these entry points (the emitter picks them when the program + * uses the store API), so each callback runs with the stores that were + * active when it was scheduled, as Node's async resources capture them. + * With a snapshot active, the callback is wrapped in a closure that + * installs it around the call; other programs keep the plain entry + * points. */ +static void *scr_als_ctx_retain_v(void *c) { return scr_als_ctx_retain(c); } +static void scr_als_ctx_release_v(void *c) { scr_als_ctx_release(c); } + +static void scr_als_bound_fire(ScrClosure *self) { + ScrClosure *cb = scr_box_get_ref(self->caps[0]); + ScrAlsCtx **slot = SCR_ALS_SLOT(); + ScrAlsCtx *prev = *slot; + *slot = scr_box_get_ref(self->caps[1]); /* the slot owns this reference */ + ((void (*)(ScrClosure *))cb->fn)(cb); + /* An enterWith inside the callback replaced the slot's snapshot; either + * way the slot's reference is released here. */ + slot = SCR_ALS_SLOT(); + scr_als_ctx_release(*slot); + *slot = prev; + scr_closure_release(cb); +} + +static ScrClosure *scr_als_bind(ScrClosure *cb /*moves*/) { + ScrAlsCtx *active = *SCR_ALS_SLOT(); + if (active == NULL) return cb; + ScrClosure *bound = scr_closure_new((void *)scr_als_bound_fire, 2); + bound->caps[0] = scr_box_new(SCR_BOX_FUNC); + scr_box_set_ref(bound->caps[0], cb); + bound->caps[1] = scr_box_new_obj(scr_als_ctx_retain_v, scr_als_ctx_release_v, NULL); + scr_box_set_ref(bound->caps[1], scr_als_ctx_retain(active)); + return bound; +} + +void scr_set_timeout_als(ScrClosure *cb, double ms) { scr_set_timeout(scr_als_bind(cb), ms); } +double scr_set_interval_als(ScrClosure *cb, double ms) { + return scr_set_interval(scr_als_bind(cb), ms); +} +double scr_set_timeout_handle_als(ScrClosure *cb, double ms) { + return scr_set_timeout_handle(scr_als_bind(cb), ms); +} +double scr_set_immediate_als(ScrClosure *cb) { return scr_set_immediate(scr_als_bind(cb)); } +void scr_next_tick_als(ScrClosure *cb) { scr_next_tick(scr_als_bind(cb)); } +void scr_queue_microtask_als(ScrClosure *cb) { scr_queue_microtask(scr_als_bind(cb)); } + /* The checked-dynamic argument form (JS files — common.mustCall wrappers * and the suite's invalid-input probes): a non-function throws Node's * ERR_INVALID_ARG_TYPE synchronously; a function value is called with @@ -3743,16 +3822,28 @@ static void scr_micro_dyn_fire(ScrClosure *c) { scr_dyn_release(fn); } -void scr_queue_microtask_dyn(const ScrDyn *cb) { +/* The closure that calls a dyn function value, or NULL with Node's + * ERR_INVALID_ARG_TYPE pending for a non-function. */ +static ScrClosure *scr_micro_dyn_closure(const ScrDyn *cb) { if (cb->kind != SCR_DYN_FUNC) { static const char msg[] = "The \"callback\" argument must be of type function."; scr_throw_error_msg_code(SCR_ERR_TYPE, msg, sizeof msg - 1, "ERR_INVALID_ARG_TYPE"); - return; + return NULL; } ScrClosure *c = scr_closure_new((void *)scr_micro_dyn_fire, 1); c->caps[0] = scr_box_new_obj(scr_dyn_retain_v, scr_dyn_release_v, NULL); scr_box_set_ref(c->caps[0], scr_dyn_retain((ScrDyn *)cb)); - scr_queue_microtask(c); + return c; +} + +void scr_queue_microtask_dyn(const ScrDyn *cb) { + ScrClosure *c = scr_micro_dyn_closure(cb); + if (c) scr_queue_microtask(c); +} + +void scr_queue_microtask_dyn_als(const ScrDyn *cb) { + ScrClosure *c = scr_micro_dyn_closure(cb); + if (c) scr_queue_microtask_als(c); } /* ── the event hooks (scr_events.c) ────────────────────────────────── @@ -4520,6 +4611,9 @@ static SCR_TL bool scr_loop_cjs_entry = false; void scr_loop_commonjs_entry(void) { scr_loop_cjs_entry = true; } bool scr_loop_run(ScrPromise *top_level) { + /* The main module's body has returned: an enterWith it made ends here. */ + scr_loop_started = true; + if (scr_als_main_slot != NULL) scr_als_main_clear(); bool first_checkpoint = !scr_loop_cjs_entry; bool rejection_failed; for (;;) { @@ -4892,6 +4986,7 @@ struct ScrGen { void (*drop_args)(void *); /* scr_gen_set_unstarted_return's hook; NULL for most generators. */ void (*unstarted_return)(void *); + const char *tag; /* scr_gen_set_tag's; NULL is the default */ bool is_async; void (*settle_async)(ScrGen *, ScrPromise *); ScrAsyncGenRequest *requests_head; @@ -5453,6 +5548,18 @@ void scr_gen_resume(ScrGen *g) { void scr_gen_set_unstarted_return(ScrGen *g, void (*hook)(void *)) { g->unstarted_return = hook; } +void scr_gen_set_tag(ScrGen *g, const char *tag) { g->tag = tag; } + +void scr_gen_copy_tag(ScrGen *g, const ScrGen *from) { g->tag = from->tag; } + +const char *scr_gen_tag(const ScrGen *g) { + return g->tag ? g->tag : g->is_async ? "AsyncGenerator" : "Generator"; +} + +__attribute__((constructor)) static void scr_gen_tag_reader_init(void) { + scr_gen_tag_reader = scr_gen_tag; +} + void scr_gen_close(ScrGen *g) { scr_gen_slot_reset(&g->ret); scr_gen_resume_return(g); diff --git a/packages/runtime/src/scr_async.h b/packages/runtime/src/scr_async.h index efa98e942..96421ca1c 100644 --- a/packages/runtime/src/scr_async.h +++ b/packages/runtime/src/scr_async.h @@ -158,6 +158,8 @@ ScrAlsCtx **scr_als_slot(void); #endif ScrAlsCtx *scr_als_ctx_retain(ScrAlsCtx *c); void scr_als_ctx_release(ScrAlsCtx *c); +/* After an enterWith: on the main stack, queues the tick that ends it. */ +void scr_als_entered_with(void); double scr_als_new(void); ScrDyn *scr_als_get(double id); /* +1; the undefined value when unset */ ScrAlsCtx *scr_als_enter(double id, ScrDyn *value); /* borrows value; prev out (owned) */ @@ -409,6 +411,7 @@ double scr_set_immediate(ScrClosure *cb); /* cb ownership moves in */ * synchronously on a non-function; borrowed. */ void scr_queue_microtask(ScrClosure *cb); void scr_queue_microtask_dyn(const ScrDyn *cb); +void scr_queue_microtask_dyn_als(const ScrDyn *cb); /* setImmediate AS A dyn VALUE (the traceCallback shape): a minted dyn * callable that schedules args[0](args[1..]) on the immediate queue and * answers undefined; a non-function first argument throws Node's @@ -428,6 +431,15 @@ bool scr_immediate_has_ref(double handle); * teardown (they must never run yet must not leak). cb ownership moves * in. */ void scr_next_tick(ScrClosure *cb); +/* The scheduling entry points of programs that enter AsyncLocalStorage + * stores: each callback runs with the stores active when it was scheduled + * (scr_async.c). */ +void scr_set_timeout_als(ScrClosure *cb, double ms); +double scr_set_interval_als(ScrClosure *cb, double ms); +double scr_set_timeout_handle_als(ScrClosure *cb, double ms); +double scr_set_immediate_als(ScrClosure *cb); +void scr_next_tick_als(ScrClosure *cb); +void scr_queue_microtask_als(ScrClosure *cb); /* A successful process.stdout/stderr.write completion on that same queue. * The callback and its program-shaped Error | null adapter both MOVE into * the entry; the adapter is invoked with NULL (Node's success argument). */ diff --git a/packages/runtime/src/scr_async_dyn.c b/packages/runtime/src/scr_async_dyn.c index ffcb85050..7611378fb 100644 --- a/packages/runtime/src/scr_async_dyn.c +++ b/packages/runtime/src/scr_async_dyn.c @@ -120,6 +120,7 @@ void scr_als_restore(ScrAlsCtx *prev) { void scr_als_enter_with(double id, ScrDyn *value) { ScrAlsCtx *prev = scr_als_enter(id, value); scr_als_ctx_release(prev); /* no restore point — Node's enterWith */ + scr_als_entered_with(); } void scr_als_disable(double id) { diff --git a/packages/runtime/src/scr_dyn.h b/packages/runtime/src/scr_dyn.h index 33dce0fe6..140b16d16 100644 --- a/packages/runtime/src/scr_dyn.h +++ b/packages/runtime/src/scr_dyn.h @@ -117,76 +117,82 @@ typedef enum { /* The handle-type tags the checked-dynamic tree can carry. The set is deliberately the * HANDLE kinds whose member surfaces already have complete static * lowerings (the http/net receiver surface); new kinds join by adding a - * tag + an ops registration in their owning unit. */ + * tag + an ops registration in their owning unit. + * + * Tags are sorted by name and every value is explicit and permanent: the + * LLVM backend hardcodes some of them (DYN_HANDLE_TAG_NUM in + * packages/compiler/src/backend/llvm/dyn.ts). A new tag goes in its sorted + * place with the next value after the largest one in use, below + * SCR_DYNH_CAPACITY. packages/compiler/src/backend/llvm/dyn-handle-tags.test.ts + * checks that values are unique and in range and that the backend's + * numbers match. */ typedef enum { - SCR_DYNH_HTTP_REQ, /* ScrHttpReq — IncomingMessage (server req & client res) */ - SCR_DYNH_HTTP_RES, /* ScrHttpRes — ServerResponse */ - SCR_DYNH_NET_SOCKET, /* ScrNetSocket — net.Socket */ - SCR_DYNH_NET_SERVER, /* ScrNetServer — net.Server (http.Server rides the same handle) */ - SCR_DYNH_H2_SESSION, /* ScrH2Session — Http2Session (client & server) */ - SCR_DYNH_H2_STREAM, /* ScrH2Stream — Http2Stream (client & server) */ - SCR_DYNH_HTTP_CLIENT, /* ScrHttpClientReq — http.ClientRequest */ - SCR_DYNH_HTTP_AGENT, /* ScrHttpAgent — http.Agent / https.Agent */ - SCR_DYNH_ABORT_SIGNAL, /* native static-fetch AbortSignal */ - SCR_DYNH_WEB_STREAM, /* native WHATWG ReadableStream */ - SCR_DYNH_WEB_READER, /* native ReadableStreamDefaultReader */ - SCR_DYNH_WEB_CONTROLLER, /* native ReadableStreamDefaultController */ - SCR_DYNH_FETCH_RESPONSE, /* native static-fetch Response */ - SCR_DYNH_FETCH_HEADERS, /* native static-fetch response Headers */ - SCR_DYNH_EVENT, /* ScrEvent — new Event() and AbortSignal's abort events */ - SCR_DYNH_ABORT_CONTROLLER, /* native static-fetch AbortController */ - SCR_DYNH_CHILD, /* child_process.ChildProcess */ - SCR_DYNH_FILE_HANDLE, /* fs/promises.FileHandle */ - SCR_DYNH_ARRAY_BUFFER, /* fixed-length ArrayBuffer storage */ - SCR_DYNH_SEGMENTER, /* default Unicode grapheme segmentation */ - SCR_DYNH_SEGMENTS, /* iterable immutable segmentation result */ - SCR_DYNH_BUILTIN_MODULE, /* native builtin export object */ - SCR_DYNH_GLOBAL, /* native global object identity */ - SCR_DYNH_WEAK_MAP, /* native weak-key metadata */ - SCR_DYNH_WEAK_SET, /* native weak membership */ - SCR_DYNH_SET, /* native Set, shared backing map */ - SCR_DYNH_REGEXP, /* native RegExp, shared compiled pattern */ - SCR_DYNH_STDIO, /* stable process stdin/stdout/stderr values */ - SCR_DYNH_URL, /* native WHATWG URL */ - SCR_DYNH_MAP, /* native Map, shared backing */ - SCR_DYNH_DATE, /* JavaScript Date with stable native identity */ - SCR_DYNH_ITERATOR, /* live native array/string/byte iterator */ - SCR_DYNH_CONSOLE, /* stored global console value */ - SCR_DYNH_SEARCH_PARAMS, /* live WHATWG URLSearchParams */ - SCR_DYNH_FETCH_REQUEST, - SCR_DYNH_WORKER, - SCR_DYNH_MESSAGE_PORT, - SCR_DYNH_SHARED_ARRAY_BUFFER, - SCR_DYNH_CONSOLE_INSTANCE, /* node:console Console instance */ - SCR_DYNH_EVENT_TARGET, /* ScrEventTarget — new EventTarget() */ - SCR_DYNH_CUSTOM_EVENT, /* ScrEvent with detail — new CustomEvent() */ - SCR_DYNH_BLOB, /* ScrBlob — new Blob() and Blob#slice() */ - SCR_DYNH_FILE, /* ScrBlob with a name — new File() */ - SCR_DYNH_KEY_OBJECT, /* node:crypto secret KeyObject (scr_lib.c) */ - SCR_DYNH_DIR, /* fs.Dir (scr_lib.c) */ - SCR_DYNH_CIPHER, /* node:crypto Cipheriv (scr_cipher.c) */ - SCR_DYNH_DECIPHER, /* node:crypto Decipheriv (scr_cipher.c) */ - SCR_DYNH_NET_SOCKET_ADDRESS, /* net.SocketAddress (scr_net.c) */ - SCR_DYNH_NET_BLOCK_LIST, /* net.BlockList (scr_net.c) */ - SCR_DYNH_DISPOSABLE_STACK, /* DisposableStack (scr_symbol.c) */ - SCR_DYNH_MESSAGE_EVENT, /* ScrEvent with data — BroadcastChannel's 'message' */ - SCR_DYNH_BROADCAST_CHANNEL, /* worker_threads BroadcastChannel (scr_worker_events.c) */ - SCR_DYNH_HASH, /* node:crypto Hash (scr_lib.c) */ - SCR_DYNH_HMAC, /* node:crypto Hmac (scr_lib.c) */ - SCR_DYNH_ITERATOR_HELPER, /* an iterator helper object over a checked iterator (scr_json.c) */ - SCR_DYNH_COUNT, + SCR_DYNH_ABORT_CONTROLLER = 15, /* native static-fetch AbortController */ + SCR_DYNH_ABORT_SIGNAL = 8, /* native static-fetch AbortSignal */ + SCR_DYNH_ARRAY_BUFFER = 18, /* fixed-length ArrayBuffer storage */ + SCR_DYNH_BLOB = 41, /* ScrBlob — new Blob() and Blob#slice() */ + SCR_DYNH_BROADCAST_CHANNEL = 51, /* worker_threads BroadcastChannel (scr_worker_events.c) */ + SCR_DYNH_BUILTIN_MODULE = 21, /* native builtin export object */ + SCR_DYNH_CHILD = 16, /* child_process.ChildProcess */ + SCR_DYNH_CIPHER = 45, /* node:crypto Cipheriv (scr_cipher.c) */ + SCR_DYNH_CONSOLE = 32, /* stored global console value */ + SCR_DYNH_CONSOLE_INSTANCE = 38, /* node:console Console instance */ + SCR_DYNH_CUSTOM_EVENT = 40, /* ScrEvent with detail — new CustomEvent() */ + SCR_DYNH_DATE = 30, /* JavaScript Date with stable native identity */ + SCR_DYNH_DECIPHER = 46, /* node:crypto Decipheriv (scr_cipher.c) */ + SCR_DYNH_DIR = 44, /* fs.Dir (scr_lib.c) */ + SCR_DYNH_DISPOSABLE_STACK = 49, /* DisposableStack (scr_symbol.c) */ + SCR_DYNH_EVENT = 14, /* ScrEvent — new Event() and AbortSignal's abort events */ + SCR_DYNH_EVENT_TARGET = 39, /* ScrEventTarget — new EventTarget() */ + SCR_DYNH_FETCH_HEADERS = 13, /* native static-fetch response Headers */ + SCR_DYNH_FETCH_REQUEST = 34, + SCR_DYNH_FETCH_RESPONSE = 12, /* native static-fetch Response */ + SCR_DYNH_FILE = 42, /* ScrBlob with a name — new File() */ + SCR_DYNH_FILE_HANDLE = 17, /* fs/promises.FileHandle */ + SCR_DYNH_GLOBAL = 22, /* native global object identity */ + SCR_DYNH_H2_SESSION = 4, /* ScrH2Session — Http2Session (client & server) */ + SCR_DYNH_H2_STREAM = 5, /* ScrH2Stream — Http2Stream (client & server) */ + SCR_DYNH_HASH = 52, /* node:crypto Hash (scr_lib.c) */ + SCR_DYNH_HMAC = 53, /* node:crypto Hmac (scr_lib.c) */ + SCR_DYNH_HTTP_AGENT = 7, /* ScrHttpAgent — http.Agent / https.Agent */ + SCR_DYNH_HTTP_CLIENT = 6, /* ScrHttpClientReq — http.ClientRequest */ + SCR_DYNH_HTTP_REQ = 0, /* ScrHttpReq — IncomingMessage (server req & client res) */ + SCR_DYNH_HTTP_RES = 1, /* ScrHttpRes — ServerResponse */ + SCR_DYNH_ITERATOR = 31, /* live native array/string/byte iterator */ + SCR_DYNH_ITERATOR_HELPER = 54, /* an iterator helper object over a checked iterator (scr_json.c) */ + SCR_DYNH_KEY_OBJECT = 43, /* node:crypto secret KeyObject (scr_lib.c) */ + SCR_DYNH_MAP = 29, /* native Map, shared backing */ + SCR_DYNH_MATCH_SOURCE = 55, /* a match result's pending metadata (scr_regex.c) */ + SCR_DYNH_MESSAGE_EVENT = 50, /* ScrEvent with data — BroadcastChannel's 'message' */ + SCR_DYNH_MESSAGE_PORT = 36, + SCR_DYNH_NET_BLOCK_LIST = 48, /* net.BlockList (scr_net.c) */ + SCR_DYNH_NET_SERVER = 3, /* ScrNetServer — net.Server (http.Server rides the same handle) */ + SCR_DYNH_NET_SOCKET = 2, /* ScrNetSocket — net.Socket */ + SCR_DYNH_NET_SOCKET_ADDRESS = 47, /* net.SocketAddress (scr_net.c) */ + SCR_DYNH_REGEXP = 26, /* native RegExp, shared compiled pattern */ + SCR_DYNH_SEARCH_PARAMS = 33, /* live WHATWG URLSearchParams */ + SCR_DYNH_SEGMENTER = 19, /* default Unicode grapheme segmentation */ + SCR_DYNH_SEGMENTS = 20, /* iterable immutable segmentation result */ + SCR_DYNH_SET = 25, /* native Set, shared backing map */ + SCR_DYNH_SHARED_ARRAY_BUFFER = 37, + SCR_DYNH_STDIO = 27, /* stable process stdin/stdout/stderr values */ + SCR_DYNH_URL = 28, /* native WHATWG URL */ + SCR_DYNH_WEAK_MAP = 23, /* native weak-key metadata */ + SCR_DYNH_WEAK_SET = 24, /* native weak membership */ + SCR_DYNH_WEB_CONTROLLER = 11, /* native ReadableStreamDefaultController */ + SCR_DYNH_WEB_READER = 10, /* native ReadableStreamDefaultReader */ + SCR_DYNH_WEB_STREAM = 9, /* native WHATWG ReadableStream */ + SCR_DYNH_WORKER = 35, } ScrDynHandleTag; /* Every per-tag table (scr_dynh_ops in scr_json.c, the message clone hooks - * in scr_message.c) is sized by this fixed capacity, never by - * SCR_DYNH_COUNT. Appending a tag for a link-gated unit therefore changes - * no always-linked storage or code: a program that never boxes the new - * handle links byte-identical objects. Raising the capacity is a - * program-wide layout change; do it in a change of its own, measured as - * neutral, and only when the enum outgrows it. */ + * in scr_message.c) is sized by this fixed capacity, never by the number of + * tags. Adding a tag for a link-gated unit therefore changes no + * always-linked storage or code: a program that never boxes the new handle + * links byte-identical objects. Raising the capacity is a program-wide + * layout change; do it in a change of its own, measured as neutral, and + * only when the tags outgrow it. */ #define SCR_DYNH_CAPACITY 128 -_Static_assert(SCR_DYNH_COUNT <= SCR_DYNH_CAPACITY, - "ScrDynHandleTag outgrew SCR_DYNH_CAPACITY: raise it in its own change"); typedef struct ScrDyn ScrDyn; typedef struct ScrBytes ScrBytes; /* full definition below (C11 repeat) */ @@ -713,6 +719,16 @@ ScrDyn *scr_dyn_iterator_result(ScrDyn *value); bool scr_dyn_iterator_can_step(const ScrDyn *iterator, const ScrDyn *next); ScrDyn *scr_dyn_iterator_step(const ScrDyn *iterator); bool scr_dyn_iterator_step_done(const ScrDyn *iterator); +/* A native Map or Set iterator stepped by the compiler's typed walk: the + * next live entry index of its collection, or -1 once the collection is + * exhausted (the iterator then ends its iteration). */ +double scr_dyn_collection_iterator_index(const ScrDyn *iterator); +/* For util.inspect: a native iterator's kind (0 array or typed array, + * 1 string, 2 Map, 3 Set, 4 a handle's), its selection (0 values, 1 keys, + * 2 entries), its collection (NULL once exhausted, and for kinds other + * than 2 and 3) and the index its next step reads from. */ +void scr_dyn_native_iterator_state(const ScrDyn *iterator, unsigned *kind, unsigned *selection, + ScrMap **collection, double *index); /* The ordinary object snapshot plus a retained typed source. source_access * releases that source when materialize=false and returns a fresh snapshot * when true. */ @@ -892,6 +908,9 @@ ScrDyn *scr_dyn_array_prototype_base(void); /* +1, shared native array identity * non-object kinds throw Node's catchable TypeErrors (strict-mode * wording). All three operands BORROWED (the value is retained in). */ void scr_dyn_key_set(ScrDyn *recv, ScrStr *key, ScrDyn *value); +/* A named (non-index) property of an extensible, unfrozen ARR, stored as + * scr_dyn_key_set stores it. Takes ownership of value. */ +void scr_dyn_arr_named_put(ScrDyn *arr, const char *key, size_t key_len, ScrDyn *value); void scr_dyn_key_set_computed(ScrDyn *recv, ScrDyn *key, ScrDyn *value); void scr_dyn_key_delete(ScrDyn *recv, const ScrStr *key, bool strict); ScrDyn *scr_dyn_global_symbol_get(ScrSym *key); /* borrowed key; +1 value */ diff --git a/packages/runtime/src/scr_generator.h b/packages/runtime/src/scr_generator.h index a92cd3222..c4397e021 100644 --- a/packages/runtime/src/scr_generator.h +++ b/packages/runtime/src/scr_generator.h @@ -63,6 +63,20 @@ ScrGen *scr_async_gen_new(void (*entry)(ScrFiber *, void *), void *argpack, /* Runs when return() reaches the generator before its first resume, with * the packed arguments still alive: an iterator helper closes its source. */ void scr_gen_set_unstarted_return(ScrGen *g, void (*hook)(void *)); +/* The @@toStringTag that util.inspect and Object.prototype.toString print + * for the generator object: "Generator" (or "AsyncGenerator") unless set, + * as the compiler does for iterator helpers ("Iterator Helper") and for + * the array and string iterators it builds as generators. A tag that + * starts with '!' marks a generator standing in for an object of another + * class, which these refuse to print. */ +void scr_gen_set_tag(ScrGen *g, const char *tag); +/* An adapter generator prints with the tag of the generator it adapts. */ +void scr_gen_copy_tag(ScrGen *g, const ScrGen *from); +const char *scr_gen_tag(const ScrGen *g); +/* scr_gen_tag for units that also link without the generator runtime (a + * library links none): the generator runtime sets it at startup, and only + * then can a generator capsule exist. */ +extern const char *(*scr_gen_tag_reader)(const ScrGen *g); ScrGen *scr_gen_retain(ScrGen *g); void scr_gen_release(ScrGen *g); /* NULL-tolerant */ void *scr_gen_retain_v(void *g); diff --git a/packages/runtime/src/scr_http.c b/packages/runtime/src/scr_http.c index f34cc18bb..83a1b3583 100644 --- a/packages/runtime/src/scr_http.c +++ b/packages/runtime/src/scr_http.c @@ -3323,6 +3323,13 @@ static void scr_http_conn_pump(ScrHttpConn *conn) { static void scr_http_conn_data(void *ctx, const char *buf, size_t n) { ScrHttpConn *conn = (ScrHttpConn *)ctx; + if (conn->pool_agent != NULL) { + /* Data on an idle pooled socket: the agent destroys the socket, as + * Node's free-socket read guard does, so the bytes are never read as + * the next request's response. */ + scr_net_sock_destroy(conn->sock); + return; + } if (conn->len + n > conn->cap) { size_t cap = conn->cap ? conn->cap : 8192; while (cap < conn->len + n) cap *= 2; diff --git a/packages/runtime/src/scr_inspect.c b/packages/runtime/src/scr_inspect.c index 4aa703711..f72d7aca8 100644 --- a/packages/runtime/src/scr_inspect.c +++ b/packages/runtime/src/scr_inspect.c @@ -1058,6 +1058,99 @@ static ScrStr *scr_insp_weak_collection(const char *label, double recurse, doubl return ib_take(&out); } +/* A native iterator: the array and string iterators print Node's empty + * `Object [Array Iterator] {}`; the Map and Set iterators print the + * entries their next steps would produce, under Node's labels + * (`[Set Iterator] { 1, 2 }`, `[Map Entries] { [ 'a', 1 ] }`), without + * advancing. NULL for a handle's iterator (kind 4), which keeps the + * unsupported answer. */ +static ScrStr *scr_insp_native_iterator(ScrDyn *d, double recurse, double depth) { + unsigned kind, selection; + ScrMap *map; + double index; + scr_dyn_native_iterator_state(d, &kind, &selection, &map, &index); + if (kind == 0) return scr_str_new("Object [Array Iterator] {}", 26); + if (kind == 1) return scr_str_new("Object [String Iterator] {}", 27); + if (kind != 2 && kind != 3) return NULL; + bool pairs = selection == 2; + const char *label = kind == 3 ? (pairs ? "[Set Entries]" : "[Set Iterator]") + : (pairs ? "[Map Entries]" : "[Map Iterator]"); + if (recurse > depth) { + InspBuf out = {0}; + ib_cstr(&out, "[Object "); + ib_cstr(&out, label); + ib_char(&out, ']'); + return ib_take(&out); + } + size_t remaining = 0; + if (map) { + for (double i = scr_map_iter_skip(map, index); i < scr_map_iter_count(map); i++) + if (scr_map_iter_live(map, i)) remaining++; + } + if (remaining == 0) { + InspBuf out = {0}; + ib_cstr(&out, label); + ib_cstr(&out, " { }"); + return ib_take(&out); + } + ScrStr *base = scr_str_new(label, strlen(label)); + scr_insp_begin(recurse + 1); + scr_map_iter_enter(map); + size_t shown = 0; + for (double i = scr_map_iter_skip(map, index); i < scr_map_iter_count(map) && shown < 100; i++) { + if (!scr_map_iter_live(map, i)) continue; + shown++; + ScrDyn *item; + if (pairs) { + item = scr_dyn_new_arr(); + scr_dyn_arr_push(item, scr_map_dyn_key(map, i)); + scr_dyn_arr_push(item, kind == 3 ? scr_map_dyn_key(map, i) : scr_map_dyn_value(map, i)); + } else { + item = kind == 2 && selection == 0 ? scr_map_dyn_value(map, i) : scr_map_dyn_key(map, i); + } + ScrStr *text = scr_insp_dyn(item, recurse + 1, depth); + scr_dyn_release(item); + scr_insp_entry(text, false); + scr_str_release(text); + } + scr_map_iter_exit(map); + if (remaining > shown) { + ScrStr *more = scr_insp_more_items((double)(remaining - shown)); + scr_insp_entry(more, false); + scr_str_release(more); + } + ScrStr *b0 = scr_str_new("{", 1); + ScrStr *b1 = scr_str_new("}", 1); + ScrStr *out = scr_insp_end(base, b0, b1, recurse + 1, false, false); + scr_str_release(base); + scr_str_release(b0); + scr_str_release(b1); + return out; +} + +/* A compiled generator object: Node's empty `Object [] {}`, with the + * tag the compiler set (scr_gen_set_tag). A generator standing in for an + * object of another class (a tag starting with '!') throws the + * "not supported yet" Error instead. */ +static bool scr_insp_is_generator(const ScrDyn *d) { + const char *key = d->v.typed_ref.type_key; + return !strncmp(key, "generator<", 10) || !strncmp(key, "async-generator<", 16); +} + +static ScrStr *scr_insp_generator(const ScrDyn *d) { + const char *tag = scr_gen_tag_reader(d->v.typed_ref.ptr); + if (tag[0] == '!') { + static const char message[] = "util.inspect of this iterator object is not supported yet"; + scr_throw_error_msg(SCR_ERR_ERROR, message, sizeof message - 1); + return scr_str_new("", 0); + } + InspBuf out = {0}; + ib_cstr(&out, "Object ["); + ib_cstr(&out, tag); + ib_cstr(&out, "] {}"); + return ib_take(&out); +} + static ScrStr *scr_insp_dyn_collection(ScrDyn *d, double recurse, double depth, bool is_map) { ScrMap *map = d->v.handle.ptr; const char *label = is_map ? "Map" : "Set"; @@ -1309,6 +1402,10 @@ ScrStr *scr_insp_dyn(ScrDyn *d, double recurse, double depth) { return scr_insp_weak_collection(d->v.handle.tag == SCR_DYNH_WEAK_MAP ? "WeakMap" : "WeakSet", recurse, depth); if (d->v.handle.tag == SCR_DYNH_DATE) return scr_insp_date(scr_dyn_native_date_value(d)); + if (d->v.handle.tag == SCR_DYNH_ITERATOR) { + ScrStr *iterator = scr_insp_native_iterator(d, recurse, depth); + if (iterator) return iterator; + } const ScrDynHandleOps *ops = scr_dyn_handle_ops_of(d); if (ops->inspect_view) { /* Node's custom inspectors answer the bare class name beyond the @@ -1349,6 +1446,7 @@ ScrStr *scr_insp_dyn(ScrDyn *d, double recurse, double depth) { } case SCR_DYN_TYPED_REF: { + if (scr_insp_is_generator(d)) return scr_insp_generator(d); const char *cls = NULL; ScrDyn *view = scr_event_ref_hooks ? scr_event_ref_hooks->inspect_view(d, &cls) : NULL; if (view) { diff --git a/packages/runtime/src/scr_json.c b/packages/runtime/src/scr_json.c index 0376ece2c..7accb818b 100644 --- a/packages/runtime/src/scr_json.c +++ b/packages/runtime/src/scr_json.c @@ -1678,6 +1678,8 @@ ScrDyn *scr_dyn_symbol_get(const ScrDyn *value, const ScrStr *key) { * later, and it neither grows nor owns heap references. */ static ScrDyn scr_dyn_small_ints[384]; +const char *(*scr_gen_tag_reader)(const ScrGen *g) = NULL; + __attribute__((constructor)) static void scr_dyn_small_ints_init(void) { for (int i = 0; i < 384; i++) { scr_dyn_small_ints[i].rc = SIZE_MAX; @@ -4673,6 +4675,31 @@ bool scr_dyn_iterator_step_done(const ScrDyn *value) { return ((ScrNativeIterator *)value->v.handle.ptr)->step_done; } +void scr_dyn_native_iterator_state(const ScrDyn *value, unsigned *kind, unsigned *selection, + ScrMap **collection, double *index) { + const ScrNativeIterator *iterator = value->v.handle.ptr; + *kind = iterator->kind; + *selection = iterator->selection; + *collection = (iterator->kind == 2 || iterator->kind == 3) && iterator->source + ? iterator->source->v.handle.ptr : NULL; + *index = iterator->index; +} + +double scr_dyn_collection_iterator_index(const ScrDyn *value) { + ScrNativeIterator *iterator = value->v.handle.ptr; + ScrDyn *source = iterator->source; + if (!source) return -1; + ScrMap *map = source->v.handle.ptr; + iterator->index = scr_map_iter_skip(map, iterator->index); + if (iterator->index >= scr_map_iter_count(map)) { + iterator->source = NULL; + scr_map_iter_exit(map); + scr_dyn_release(source); + return -1; + } + return iterator->index++; +} + static int scr_ih_method_index(const char *name, size_t len); static ScrDyn *scr_iterator_helper_method(ScrDyn *self, int index, ScrDyn *const *args, size_t argc); static ScrDyn *scr_ih_method_function(int index); @@ -6202,6 +6229,20 @@ ScrStr *scr_dyn_object_tag(const ScrDyn *d) { } if (d->kind == SCR_DYN_TYPED_REF) { if (scr_dyn_ref_is_error(d)) return scr_str_new("[object Error]", 14); + if (scr_dyn_generator(d)) { + const char *gen_tag = scr_gen_tag_reader(d->v.typed_ref.ptr); + if (gen_tag[0] == '!') { + static const char message[] = "Object.prototype.toString of this iterator is not supported yet"; + scr_throw_error_msg(SCR_ERR_ERROR, message, sizeof message - 1); + return NULL; + } + ScrJsonBuf b; + scr_jb_init(&b); + scr_jb_puts(&b, "[object "); + scr_jb_puts(&b, gen_tag); + scr_jb_putc(&b, ']'); + return scr_jb_finish(&b); + } ScrDyn *materialized = scr_dyn_typed_ref_materialize(d); ScrStr *out = scr_dyn_object_tag(materialized); scr_dyn_release(materialized); @@ -6234,6 +6275,13 @@ ScrStr *scr_dyn_object_tag(const ScrDyn *d) { scr_jb_putc(&b, ']'); return scr_jb_finish(&b); } + if (d->v.handle.tag == SCR_DYNH_ITERATOR && ((const ScrNativeIterator *)d->v.handle.ptr)->kind < 4) { + static const char *const tags[] = { + "[object Array Iterator]", "[object String Iterator]", "[object Map Iterator]", "[object Set Iterator]", + }; + const char *it_tag = tags[((const ScrNativeIterator *)d->v.handle.ptr)->kind]; + return scr_str_new(it_tag, strlen(it_tag)); + } if (scr_dyn_handle_ops_of(d)->to_string_tag) { ScrJsonBuf b; scr_jb_init(&b); @@ -7863,6 +7911,14 @@ static void scr_dyn_object_key_set(ScrDyn *recv, ScrStr *key, ScrDyn *value, con return; } +void scr_dyn_arr_named_put(ScrDyn *arr, const char *key, size_t key_len, ScrDyn *value) { + if (!arr->v.arr.properties) { + scr_dyn_region_store(arr, NULL); /* a table that later holds anything */ + arr->v.arr.properties = scr_dyn_new_obj(); + } + scr_dyn_obj_set(arr->v.arr.properties, key, key_len, value); +} + void scr_dyn_key_set(ScrDyn *recv, ScrStr *key, ScrDyn *value) { if (scr_dyn_class_reflection_fence(recv)) return; if (recv->kind == SCR_DYN_PROXY) { scr_dyn_proxy_set(recv, key, value); return; } diff --git a/packages/runtime/src/scr_message.test.c b/packages/runtime/src/scr_message.test.c index 0c6bd7ae3..a6c940783 100644 --- a/packages/runtime/src/scr_message.test.c +++ b/packages/runtime/src/scr_message.test.c @@ -175,7 +175,7 @@ static void check_documents(void) { /* A link-gated unit's handle crosses threads through the transfer hooks in * its ops: share on the sender, adopt on the receiver, drop with the * message. The test borrows the last capacity slot, which no unit uses, so - * it also proves that slots past SCR_DYNH_COUNT work. */ + * it also proves that slots past the tags in use work. */ #define TRANSFER_TAG ((ScrDynHandleTag)(SCR_DYNH_CAPACITY - 1)) typedef struct { _Atomic size_t rc; diff --git a/packages/runtime/src/scr_regex.c b/packages/runtime/src/scr_regex.c index a2e591b0e..44a91410b 100644 --- a/packages/runtime/src/scr_regex.c +++ b/packages/runtime/src/scr_regex.c @@ -610,6 +610,138 @@ static ScrStr *scr_regex_span(const ScrRegexInput *input, int start, int end) { return scr_str_new((const char *)input->data + start, (size_t)(end - start)); } +/* ── match metadata ───────────────────────────────────────────────────── */ + +/* Results of one exec or matchAll call share their subject and regex + * through one match source handle, which each result's metadata slot + * holds with its start index until the metadata object (index, input, + * groups) is first needed: when the result is converted to a dynamic + * value or its groups are read. Results read only through their elements + * never allocate it, and releasing a result releases the handle with the + * rest of its metadata. */ +typedef struct ScrRegexMatchSource { + size_t rc; + ScrStr *input; + ScrRegex *re; + ScrDyn *input_value; /* `input` as a dynamic value, built on first use */ +} ScrRegexMatchSource; + +static bool scr_regex_copy_pending_metadata(ScrArr *a, ScrDyn *target); + +static void *scr_regex_source_retain_v(void *p) { + ((ScrRegexMatchSource *)p)->rc++; + return p; +} + +static void scr_regex_source_release_v(void *p) { + ScrRegexMatchSource *src = p; + if (--src->rc != 0) return; + scr_str_release(src->input); + scr_regex_release(src->re); + scr_dyn_release(src->input_value); + free(src); +} + +/* The +1 match source handle of one exec or matchAll call. */ +static ScrDyn *scr_regex_source_new(ScrStr *s, ScrRegex *re) { + static const ScrDynHandleOps ops = { + "MatchSource", &scr_regex_source_retain_v, &scr_regex_source_release_v, NULL, NULL, NULL, NULL, + }; + scr_dyn_handle_install(SCR_DYNH_MATCH_SOURCE, &ops); + scr_arr_match_copy = &scr_regex_copy_pending_metadata; + ScrRegexMatchSource *src = malloc(sizeof *src); + if (!src) scr_regex_oom(); + src->rc = 0; + src->input = scr_str_retain(s); + src->re = scr_regex_retain(re); + src->input_value = NULL; + return scr_dyn_new_handle(src, SCR_DYNH_MATCH_SOURCE); +} + +/* Records the match at UTF-16 `index` as the row's pending metadata. */ +static void scr_regex_defer_metadata(ScrArr *row, ScrDyn *source, int index) { + ScrArrExt *x = scr_arr_ext(row); + x->metadata = scr_dyn_retain(source); + x->match_index = (double)index; +} + +/* The pending match source of a result, or NULL once its metadata exists. */ +static ScrRegexMatchSource *scr_regex_pending(const ScrArr *a) { + const ScrDyn *metadata = a->ext ? a->ext->metadata : NULL; + if (!metadata || metadata->kind != SCR_DYN_HANDLE || metadata->v.handle.tag != SCR_DYNH_MATCH_SOURCE) + return NULL; + return metadata->v.handle.ptr; +} + +/* The subject as a dynamic value, shared by the source's results. +1. */ +static ScrDyn *scr_regex_source_input(ScrRegexMatchSource *src) { + if (!src->input_value) src->input_value = scr_dyn_new_str(src->input); + return scr_dyn_retain(src->input_value); +} + +/* groups is a null-prototype object mapping each group name to its first + * participating capture (the row's own string, shared) or undefined, and + * undefined itself for a pattern without named groups. The row holds each + * capture as a string or a present undefined. +1. */ +static ScrDyn *scr_regex_groups_of(const ScrArr *a, uint8_t *bc) { + const char *groupnames = lre_get_groupnames(bc); + if (!groupnames) return scr_dyn_retain(scr_dyn_undefined()); + int capture_count = lre_get_capture_count(bc); + ScrDyn *groups = scr_dyn_new_obj_null_proto(); + const char *name = groupnames; + for (int k = 1; k < capture_count; k++) { + size_t length = strlen(name); + bool matched = (size_t)k < a->len && (size_t)k < a->cap && a->present[k] == SCR_ARR_VALUE; + /* ES2025 duplicates sit in distinct alternatives: at most one + * participates, and it wins over an earlier undefined. */ + if (length && (matched || !scr_dyn_obj_get(groups, name, length))) + scr_dyn_obj_set(groups, name, length, + matched ? scr_dyn_new_str((ScrStr *)(uintptr_t)a->data[k]) + : scr_dyn_retain(scr_dyn_undefined())); + name += length + LRE_GROUP_NAME_TRAILER_LEN; + } + return groups; +} + +static void scr_regex_materialize_metadata(ScrArr *a) { + ScrRegexMatchSource *src = scr_regex_pending(a); + if (!src) return; + ScrArrExt *x = a->ext; + ScrDyn *source = x->metadata; + ScrDyn *props = scr_dyn_new_obj(); + scr_dyn_obj_set(props, "index", 5, scr_dyn_new_num(x->match_index)); + scr_dyn_obj_set(props, "input", 5, scr_regex_source_input(src)); + scr_dyn_obj_set(props, "groups", 6, scr_regex_groups_of(a, scr_regex_bc(src->re))); + x->metadata = props; + scr_dyn_release(source); +} + +static bool scr_regex_copy_pending_metadata(ScrArr *a, ScrDyn *target) { + ScrRegexMatchSource *src = scr_regex_pending(a); + if (target->kind != SCR_DYN_ARR || target->v.arr.properties || target->non_extensible || + target->v.arr.frozen) { + scr_regex_materialize_metadata(a); + return false; + } + scr_dyn_arr_named_put(target, "index", 5, scr_dyn_new_num(a->ext->match_index)); + scr_dyn_arr_named_put(target, "input", 5, scr_regex_source_input(src)); + scr_dyn_arr_named_put(target, "groups", 6, scr_regex_groups_of(a, scr_regex_bc(src->re))); + return true; +} + +ScrDyn *scr_regex_result_prop(ScrArr *a, ScrStr *key) { + ScrRegexMatchSource *src = scr_regex_pending(a); + if (src) { + if (key->len == 5 && memcmp(key->data, "index", 5) == 0) + return scr_dyn_new_num(a->ext->match_index); + if (key->len == 5 && memcmp(key->data, "input", 5) == 0) return scr_regex_source_input(src); + scr_regex_materialize_metadata(a); + } + ScrDyn *metadata = a->ext ? a->ext->metadata : NULL; + ScrDyn *value = metadata ? scr_dyn_obj_get(metadata, key->data, key->len) : NULL; + return scr_dyn_retain(value ? value : scr_dyn_undefined()); +} + /* ECMA-262 AdvanceStringIndex: past a whole surrogate pair under /u, one * code unit otherwise. ASCII cannot contain a surrogate pair. */ static int scr_advance(const ScrRegexInput *input, int i, bool unicode) { @@ -713,12 +845,6 @@ static ScrArr *scr_regex_exec_core(ScrStr *s, ScrRegex *re, uint64_t mask, bool int count = lre_get_capture_count(bc); bool spans = lazy != 0 && !input.wide && (count >= 64 || ((mask | lazy) >> count) == 0); out = scr_arr_new_inline(SCR_ELEM_STR, (size_t)count + spans); - if (metadata) { - ScrDyn *props = scr_dyn_new_obj(); - scr_arr_ext(out)->metadata = props; - scr_dyn_obj_set(props, "index", 5, scr_dyn_new_num((double)scr_capture_index(&input, capture[0]))); - scr_dyn_obj_set(props, "input", 5, scr_dyn_new_str(s)); - } /* A fresh dense array: every slot is written directly, then the * hole-free prefix is the run of materialized captures from 0. Its * storage pointers are fixed (the inline block never moves here), so @@ -750,6 +876,11 @@ static ScrArr *scr_regex_exec_core(ScrStr *s, ScrRegex *re, uint64_t mask, bool while (filled < n && present[filled] == SCR_ARR_VALUE) filled++; out->filled = (uint32_t)filled; out->len = n; + if (metadata) { + ScrDyn *source = scr_regex_source_new(s, re); + scr_regex_defer_metadata(out, source, scr_capture_index(&input, capture[0])); + scr_dyn_release(source); + } } scr_regex_input_dispose(&input); return out; /* +1, or NULL (no match) */ @@ -883,8 +1014,11 @@ double scr_regex_search(ScrStr *s, ScrRegex *re) { * Node's exact TypeError (catchable — replaceAll's stance). Empty matches * advance one unit, unicode-aware (AdvanceStringIndex). `indices`, when * non-NULL, receives each match's UTF-16 start index (the row's .index — - * the companion array the for-of-over-matchAll desugar reads). */ -static ScrArr *scr_regex_match_all_core(ScrStr *s, ScrRegex *re, ScrArr *indices) { + * the companion array the for-of-over-matchAll desugar reads). With + * `metadata`, each row records the match source for its index, input and + * groups properties. */ +static ScrArr *scr_regex_match_all_core(ScrStr *s, ScrRegex *re, ScrArr *indices, + bool metadata) { uint8_t *bc = scr_regex_bc(re); int re_flags = lre_get_flags(bc); if (!(re_flags & LRE_FLAG_GLOBAL)) { @@ -895,6 +1029,7 @@ static ScrArr *scr_regex_match_all_core(ScrStr *s, ScrRegex *re, ScrArr *indices } bool unicode = (re_flags & (LRE_FLAG_UNICODE | LRE_FLAG_UNICODE_SETS)) != 0; int capture_count = lre_get_capture_count(bc); + ScrDyn *source = metadata ? scr_regex_source_new(s, re) : NULL; ScrRegexInput input; scr_regex_input_init(&input, re, bc, s); int len = input.len; @@ -906,30 +1041,51 @@ static ScrArr *scr_regex_match_all_core(ScrStr *s, ScrRegex *re, ScrArr *indices if (scr_exec(&input, bc, pos) != 1) break; int start = scr_capture_index(&input, capture[0]); int end = scr_capture_index(&input, capture[1]); - ScrArr *row = scr_arr_new(SCR_ELEM_STR, (size_t)capture_count); + /* One allocation per row, like an exec result: every slot is written + * directly, then the hole-free prefix is the run of captures from 0. */ + ScrArr *row = scr_arr_new_inline(SCR_ELEM_STR, (size_t)capture_count); + uint64_t *data = row->data; + uint8_t *present = row->present; for (int k = 0; k < capture_count; k++) { const uint8_t *cs = capture[2 * k], *ce = capture[2 * k + 1]; if (cs == NULL || ce == NULL) { - scr_arr_set_undefined(row, (double)row->len); + data[k] = 0; + present[k] = SCR_ARR_UNDEFINED; } else { - scr_arr_push_ref( - row, scr_regex_span(&input, scr_capture_index(&input, cs), scr_capture_index(&input, ce))); + ScrStr *text = + scr_regex_span(&input, scr_capture_index(&input, cs), scr_capture_index(&input, ce)); + data[k] = (uint64_t)(uintptr_t)text; + present[k] = SCR_ARR_VALUE; } } + size_t filled = 0; + while (filled < (size_t)capture_count && present[filled] == SCR_ARR_VALUE) filled++; + row->filled = (uint32_t)filled; + row->len = (size_t)capture_count; + if (source) scr_regex_defer_metadata(row, source, start); scr_arr_push_ref(out, row); if (indices != NULL) scr_arr_push_f64(indices, (double)start); pos = end == start ? scr_advance(&input, end, unicode) : end; } + scr_dyn_release(source); scr_regex_input_dispose(&input); return out; /* +1 (possibly empty — Node's no-match drain is []) */ } ScrArr *scr_regex_match_all(ScrStr *s, ScrRegex *re) { - return scr_regex_match_all_core(s, re, NULL); + return scr_regex_match_all_core(s, re, NULL, true); +} + +ScrArr *scr_regex_match_all_captures(ScrStr *s, ScrRegex *re) { + return scr_regex_match_all_core(s, re, NULL, false); } ScrArr *scr_regex_match_all_into(ScrStr *s, ScrRegex *re, ScrArr *indices) { - return scr_regex_match_all_core(s, re, indices); + return scr_regex_match_all_core(s, re, indices, true); +} + +ScrArr *scr_regex_match_all_captures_into(ScrStr *s, ScrRegex *re, ScrArr *indices) { + return scr_regex_match_all_core(s, re, indices, false); } /* ── replace / replaceAll ─────────────────────────────────────────────── */ diff --git a/packages/runtime/src/scr_regex.h b/packages/runtime/src/scr_regex.h index 014e5aa83..01c545bc5 100644 --- a/packages/runtime/src/scr_regex.h +++ b/packages/runtime/src/scr_regex.h @@ -98,11 +98,23 @@ ScrStr *scr_regex_replace_all(ScrStr *s, ScrRegex *re, ScrStr *rep); ScrArr *scr_regex_split(ScrStr *s, ScrRegex *re); ScrArr *scr_regex_split_limit(ScrStr *s, ScrRegex *re, double limit); /* matchAll drained eagerly: +1 string[][] of honest match slices; throws - * Node's TypeError on a non-global regex (catchable). */ + * Node's TypeError on a non-global regex (catchable). Each row carries its + * index, input and groups properties. */ ScrArr *scr_regex_match_all(ScrStr *s, ScrRegex *re); +/* scr_regex_match_all for rows read only through their elements: the rows + * carry no index, input or groups properties. */ +ScrArr *scr_regex_match_all_captures(ScrStr *s, ScrRegex *re); /* matchAll + the companion-index drain: also pushes each match's UTF-16 * start index (f64) onto `indices` — the .index the for-of-over-matchAll * desugar reads. Same throw/result contract as scr_regex_match_all. */ ScrArr *scr_regex_match_all_into(ScrStr *s, ScrRegex *re, ScrArr *indices); +/* scr_regex_match_all_into for rows read only through their elements and + * the companion index: the rows carry no index, input or groups + * properties. */ +ScrArr *scr_regex_match_all_captures_into(ScrStr *s, ScrRegex *re, ScrArr *indices); +/* `.index` / `.input` of a match result: its pending match source or its + * metadata answers, without building the metadata object; undefined when + * neither has the key. Borrows both; +1. */ +struct ScrDyn *scr_regex_result_prop(ScrArr *a, ScrStr *key); #endif /* SCR_REGEX_H */ diff --git a/packages/runtime/src/scr_test.c b/packages/runtime/src/scr_test.c index 00e609129..43affdd8a 100644 --- a/packages/runtime/src/scr_test.c +++ b/packages/runtime/src/scr_test.c @@ -2279,6 +2279,26 @@ double scr_test_mt_set_immediate(ScrClosure *cb /*moves*/) { ScrMockTimers *m = scr_mt_owner[SCR_MT_IMMEDIATE]; return m ? scr_mt_create(m, false, cb, -1)->handle : scr_set_immediate(cb); } +/* The same calls in a program that enters AsyncLocalStorage stores: a + * mocked timer runs in the context its tick() runs in, as in Node; a real + * one keeps the stores active when it was scheduled. */ +void scr_test_mt_set_timeout_als(ScrClosure *cb /*moves*/, double ms) { + ScrMockTimers *m = scr_mt_owner[SCR_MT_TIMEOUT]; + if (m) (void)scr_mt_create(m, false, cb, ms); + else scr_set_timeout_als(cb, ms); +} +double scr_test_mt_set_timeout_handle_als(ScrClosure *cb /*moves*/, double ms) { + ScrMockTimers *m = scr_mt_owner[SCR_MT_TIMEOUT]; + return m ? scr_mt_create(m, false, cb, ms)->handle : scr_set_timeout_handle_als(cb, ms); +} +double scr_test_mt_set_interval_als(ScrClosure *cb /*moves*/, double ms) { + ScrMockTimers *m = scr_mt_owner[SCR_MT_INTERVAL]; + return m ? scr_mt_create(m, true, cb, ms)->handle : scr_set_interval_als(cb, ms); +} +double scr_test_mt_set_immediate_als(ScrClosure *cb /*moves*/) { + ScrMockTimers *m = scr_mt_owner[SCR_MT_IMMEDIATE]; + return m ? scr_mt_create(m, false, cb, -1)->handle : scr_set_immediate_als(cb); +} void scr_test_mt_clear_immediate(double handle) { ScrMockTimers *m = scr_mt_owner[SCR_MT_IMMEDIATE]; if (m) scr_mt_clear(m, handle); diff --git a/packages/runtime/src/scr_test.h b/packages/runtime/src/scr_test.h index 64a280bf7..b29fca362 100644 --- a/packages/runtime/src/scr_test.h +++ b/packages/runtime/src/scr_test.h @@ -110,6 +110,12 @@ void scr_test_mt_clear_immediate(double handle); void scr_test_mt_immediate_unref(double handle); void scr_test_mt_immediate_ref(double handle); bool scr_test_mt_immediate_has_ref(double handle); +/* The setters of programs that also enter AsyncLocalStorage stores: an + * unmocked call uses the loop's *_als function. */ +void scr_test_mt_set_timeout_als(ScrClosure *cb, double ms); +double scr_test_mt_set_timeout_handle_als(ScrClosure *cb, double ms); +double scr_test_mt_set_interval_als(ScrClosure *cb, double ms); +double scr_test_mt_set_immediate_als(ScrClosure *cb); double scr_test_mt_date_now(void); /* timers/promises scheduler.wait(delay) (+1 promise; NULL after a throw). */ ScrPromise *scr_test_mt_scheduler_wait(double ms); diff --git a/scripts/zero-cost-gate.mjs b/scripts/zero-cost-gate.mjs index f4272ac6a..297e741e5 100644 --- a/scripts/zero-cost-gate.mjs +++ b/scripts/zero-cost-gate.mjs @@ -316,7 +316,9 @@ const definedFunctions = (binary) => * b14377864 (DYNAMIC_TIER_PREFIXES in zero-cost-compare.mjs): JSON.parse * trees, worker messages, regex exec metadata (two-tier §3.3 L5), and a * shared scr_dyn_call/scr_dyn_define_property core not yet traced to its - * typed root. Every other bench workload is fully typed and must link none. + * typed root. bench/thread-slots, added later, links them through worker + * messages: node:worker_threads values are dyn objects. Every other bench + * workload is fully typed and must link none. * Remove a workload here when a leak fix makes its binary clean. */ const DYNAMIC_WORKLOADS = new Set([ "bench/json-records", @@ -326,6 +328,7 @@ const DYNAMIC_WORKLOADS = new Set([ "bench/regex-logs", "bench/atomics-contended", "bench/intern-table", + "bench/thread-slots", "bench/typed-array-scan-workers", "bench/log-summary", "bench/inventory-report", diff --git a/tests/corpus/5815-als-callback-context.ts b/tests/corpus/5815-als-callback-context.ts new file mode 100644 index 000000000..461a2aa95 --- /dev/null +++ b/tests/corpus/5815-als-callback-context.ts @@ -0,0 +1,76 @@ +// AsyncLocalStorage context reaches timer, interval, setImmediate, +// process.nextTick and queueMicrotask callbacks: each runs in the store +// that was active when it was scheduled, including callbacks they schedule +// in turn, an interval on every tick, and a refreshed timer. A cleared +// timer or immediate never runs. enterWith inside a callback lasts until +// the callback returns, also in a callback scheduled outside any store, +// and callbacks scheduled outside any store, or inside exit(), see +// undefined. A store entered at the top level reaches only the callbacks +// scheduled after it. +import { AsyncLocalStorage } from "node:async_hooks"; + +const requests = new AsyncLocalStorage(); +const users = new AsyncLocalStorage(); +const seen: string[] = []; + +function record(where: string): void { + seen.push(`${where}: ${requests.getStore() ?? "-"} ${users.getStore() ?? "-"}`); +} + +requests.run("req-1", () => { + setTimeout(() => { + record("timeout"); + process.nextTick(() => record("tick from timeout")); + queueMicrotask(() => record("microtask from timeout")); + }, 1); + setImmediate(() => { + record("immediate"); + setTimeout(() => record("timeout from immediate"), 1); + }); + process.nextTick(() => record("tick")); + queueMicrotask(() => record("microtask")); + users.run("ada", () => { + setTimeout(() => record("nested timeout"), 2); + setImmediate(() => record("nested immediate")); + }); + let ticks = 0; + const interval = setInterval(() => { + ticks++; + record(`interval ${ticks}`); + if (ticks === 3) clearInterval(interval); + }, 1); + const refreshed = setTimeout(() => record("refreshed timeout"), 3); + refreshed.refresh(); + const cleared = setTimeout(() => record("cleared timeout"), 1); + clearTimeout(cleared); + const clearedImmediate = setImmediate(() => record("cleared immediate")); + clearImmediate(clearedImmediate); + requests.exit(() => { + setTimeout(() => record("timeout in exit"), 1); + queueMicrotask(() => record("microtask in exit")); + }); +}); + +requests.run("req-2", () => { + setTimeout(() => { + users.enterWith("grace"); + record("timeout after enterWith"); + setTimeout(() => record("timeout from enterWith"), 1); + }, 4); + setTimeout(() => record("timeout after the enterWith timeout"), 5); +}); + +setTimeout(() => { + record("timeout outside any store"); + requests.enterWith("entered outside"); +}, 1); +setTimeout(() => record("timeout after one that entered outside any store"), 8); +process.nextTick(() => record("tick outside any store")); + +setTimeout(() => { + record("final"); + for (const line of seen.sort()) console.log(line); +}, 30); + +users.enterWith("top level"); +setTimeout(() => record("timeout after a top-level enterWith"), 6); diff --git a/tests/corpus/6010-match-metadata.ts b/tests/corpus/6010-match-metadata.ts new file mode 100644 index 000000000..8de5f6c4a --- /dev/null +++ b/tests/corpus/6010-match-metadata.ts @@ -0,0 +1,40 @@ +// index and input on matchAll rows and exec results: for-of bindings that +// read only captures and the index, bindings that escape, spreads, +// callbacks, stored drains and closures. +const text = "k1=v10; k22=v3; k3=v400;"; +const re = /(?k\d+)=(?v\d+);/g; + +for (const m of text.matchAll(re)) console.log(m.index, m[1], m.groups!.val); +const kept: RegExpExecArray[] = []; +for (const m of text.matchAll(re)) { + kept.push(m); + console.log(m.index, m.input === text); +} +console.log(kept.map((m) => `${m.index}:${m[0]}`).join(" ")); + +const rows = [...text.matchAll(re)]; +console.log(rows.map((m) => m.index).join(","), rows[1]!.index, rows[2]!.input.length); +console.log(rows.filter((m) => m.index > 0).map((m) => m.groups!.key).join(",")); +console.log(rows.find((m) => m.index === 8)?.[2], rows.at(-1)!.index); +console.log(Array.from(text.matchAll(re), (m) => m.index + m[0].length).join(",")); + +const drained = text.matchAll(/v(\d+)/g); +for (const m of drained) console.log(m.index, m[1]); +const stored = text.matchAll(/k(\d)/g); +const later = (): string => [...stored].map((m) => m.index).join("|"); +console.log(later()); + +function startOf(m: RegExpExecArray): number { + return m.index; +} +console.log([...text.matchAll(/=/g)].map(startOf).join(",")); + +const e = /(?\d+);/.exec(text)!; +console.log(e.index, e.input === text, e[1], e.groups!.n); +const g = text.match(/k\d+/g)!; +console.log(g.join(","), g.length); +const plain = [..."aXbX".matchAll(/X/g)]; +console.log(plain.map((m) => m.index).join(","), plain.length); +console.log([...text.matchAll(re)].map((m) => m[1]).join(","), [...text.matchAll(re)].length); +console.log(Array.from(text.matchAll(re), (m) => m.groups!.key + m.length).join(",")); +for (const m of [...text.matchAll(re)].slice(1)) console.log(m[2], m.groups!.val); diff --git a/tests/corpus/6011-match-metadata-js.js b/tests/corpus/6011-match-metadata-js.js new file mode 100644 index 000000000..b67e11069 --- /dev/null +++ b/tests/corpus/6011-match-metadata-js.js @@ -0,0 +1,31 @@ +// Match metadata on matchAll rows and exec results in JavaScript: index, +// input and groups through callbacks, untyped functions, destructuring, +// Object.keys and spread copies. +const text = "k1=v10; k22=v3; k3=v400;"; +const re = /(?k\d+)=(?v\d+)(?!)?;/g; +const rows = [...text.matchAll(re)]; +console.log(rows.map((m) => m.index).join(","), rows.map((m) => m.input.length).join(",")); +console.log(rows.map(({ index, groups }) => `${index}=${groups.key}`).join(" ")); + +function describe(m) { + return [m.index, m.input === text, m.groups.key, m.groups.bang, Object.keys(m).join("|")]; +} +for (const m of text.matchAll(re)) console.log(describe(m)); +console.log(rows.map((m) => describe(m)[0]).join(",")); + +const first = rows[0]; +const copy = { ...first }; +console.log(Object.keys(copy).join(","), copy.index, copy.groups.val, Object.getPrototypeOf(first.groups) === null); +console.log(JSON.stringify(first), first.length); + +const dup = [..."a1-b".matchAll(/(?[a-z])|(?\d)|-/g)]; +console.log(dup.map((m) => describe2(m)).join(" ")); +function describe2(m) { + return `${m.index}:${m.groups.v}`; +} + +const pick = (m) => m && [m.index, m.groups]; +console.log(pick(/(?\d)(?x)?/.exec("ab3")), pick(/b/.exec("ab")), pick(/z/.exec("ab"))); +console.log(pick("a1".match(/(?\d)/)), Object.keys("a1a".match(/a/g)).join(",")); +const none = [..."ab".matchAll(/[a-z]/g)]; +console.log(none.map((m) => m.groups === undefined && m.index >= 0).join(",")); diff --git a/tests/corpus/6071-iterator-sources.ts b/tests/corpus/6071-iterator-sources.ts new file mode 100644 index 000000000..c63469022 --- /dev/null +++ b/tests/corpus/6071-iterator-sources.ts @@ -0,0 +1,70 @@ +// Iterator objects from strings and typed arrays, Iterator.from over the +// built-in iterables, return() on helper objects, and flatMap over Set, +// Map and string results. +const log: string[] = []; +function* count(label: string, n: number): Generator { + try { + for (let i = 1; i <= n; i++) { + log.push(`${label}${i}`); + yield i; + } + } finally { + log.push(`${label}:closed`); + } +} +function flush(what: unknown): void { + console.log(what, "|", log.join(" ")); + log.length = 0; +} + +// String iterators step by code point; a lone surrogate is one step. +const text = "a\u{1F600}b\uD800"; +const chars = text[Symbol.iterator](); +console.log(chars.next(), chars.next().value?.length, [...chars].length); +console.log(text[Symbol.iterator]().map((c) => c.codePointAt(0)!.toString(16)).toArray()); +console.log("naïve"[Symbol.iterator]().filter((c) => c !== "ï").toArray().join("")); +for (const c of "xyz"[Symbol.iterator]().drop(1)) console.log("char", c); +console.log("hello"[Symbol.iterator]().some((c) => c === "l"), "hello"[Symbol.iterator]().find((c) => c > "h")); + +// Typed array iterators. +const bytes = new Uint8Array([9, 8, 7]); +console.log(bytes.values().map((x) => x * 2).toArray(), bytes.keys().filter((i) => i > 0).toArray()); +console.log(bytes.entries().map(([i, v]) => i * v).toArray()); +const floats = new Float64Array([0.5, 1.5, 2.5]).values().map((x) => x + 1); +console.log(floats.next(), [...floats]); +for (const [i, v] of new Int16Array([-1, 2, -3, 4]).entries().filter(([, v]) => v > 0)) console.log(i, v); + +// Iterator.from returns each built-in iterable's own iterator. +const list = [1, 2]; +const fromList = Iterator.from(list); +list.push(3); +console.log([...fromList], Iterator.from("a\u{1F600}").toArray()); +console.log(Iterator.from(new Set(["p", "q"])).toArray(), Iterator.from(new Map([["k", 1]])).toArray()); +console.log(Iterator.from(new Uint8Array([4, 5])).reduce((a, b) => a + b, 0)); +const fromGen = Iterator.from(count("a", 3)); +flush([fromGen.next(), fromGen.take(1).toArray()]); +flush(Iterator.from(count("b", 9).map((x) => x * 3)).take(2).toArray()); + +// return() on helper objects. +const started = count("c", 3); +started.next(); +const doubled = started.map((x) => x * 2); +flush([doubled.return!(), started.next()]); +const fresh = count("d", 3); +const kept = fresh.filter(() => true); +flush([kept.return?.(), fresh.next(), kept.next()]); +const stepping = count("e", 5).drop(1); +flush([stepping.next(), stepping.return!(), stepping.next()]); +flush(count("f", 4).map((x) => x).take(2).return!()); + +// flatMap over Set and Map results, and a string result's TypeError. +console.log([1, 2].values().flatMap((x) => new Set([x, x * 10])).toArray()); +console.log([1, 2].values().flatMap((x) => new Map([[`k${x}`, x]])).toArray()); +const pairs = new Map([["m", 1], ["n", 2]]); +for (const [k, v] of [0].values().flatMap(() => pairs)) console.log(k, v); +try { + count("g", 3).flatMap((x) => `s${x}`).toArray(); +} catch (e) { + log.push(`${(e as Error).name}: ${(e as Error).message}`); +} +flush("string result"); diff --git a/tests/corpus/6072-iterator-sources-js.js b/tests/corpus/6072-iterator-sources-js.js new file mode 100644 index 000000000..84dd00067 --- /dev/null +++ b/tests/corpus/6072-iterator-sources-js.js @@ -0,0 +1,70 @@ +// The JavaScript form: iterator objects from strings and typed arrays, +// Iterator.from over the built-in iterables, return() on helper objects, +// and flatMap over Set, Map and string results. +const log = []; +function* count(label , n ) { + try { + for (let i = 1; i <= n; i++) { + log.push(`${label}${i}`); + yield i; + } + } finally { + log.push(`${label}:closed`); + } +} +function flush(what ) { + console.log(what, "|", log.join(" ")); + log.length = 0; +} + +// String iterators step by code point; a lone surrogate is one step. +const text = "a\u{1F600}b\uD800"; +const chars = text[Symbol.iterator](); +console.log(chars.next(), chars.next().value.length, [...chars].length); +console.log(text[Symbol.iterator]().map((c) => c.length).toArray()); +console.log("naïve"[Symbol.iterator]().filter((c) => c !== "ï").toArray().join("")); +for (const c of "xyz"[Symbol.iterator]().drop(1)) console.log("char", c); +console.log("hello"[Symbol.iterator]().some((c) => c === "l"), "hello"[Symbol.iterator]().find((c) => c > "h")); + +// Typed array iterators. +const bytes = new Uint8Array([9, 8, 7]); +console.log(bytes.values().map((x) => x * 2).toArray(), bytes.keys().filter((i) => i > 0).toArray()); +console.log(bytes.entries().map(([i, v]) => i * v).toArray()); +const floats = new Float64Array([0.5, 1.5, 2.5]).values().map((x) => x + 1); +console.log(floats.next(), [...floats]); +for (const [i, v] of new Int16Array([-1, 2, -3, 4]).entries().filter(([, v]) => v > 0)) console.log(i, v); + +// Iterator.from returns each built-in iterable's own iterator. +const list = [1, 2]; +const fromList = Iterator.from(list); +list.push(3); +console.log([...fromList], Iterator.from("a\u{1F600}").toArray()); +console.log(Iterator.from(new Set(["p", "q"])).toArray(), Iterator.from(new Map([["k", 1]])).toArray()); +console.log(Iterator.from(new Uint8Array([4, 5])).reduce((a, b) => a + b, 0)); +const fromGen = Iterator.from(count("a", 3)); +flush([fromGen.next(), fromGen.take(1).toArray()]); +flush(Iterator.from(count("b", 9).map((x) => x * 3)).take(2).toArray()); + +// return() on helper objects. +const started = count("c", 3); +started.next(); +const doubled = started.map((x) => x * 2); +flush([doubled.return(), started.next()]); +const fresh = count("d", 3); +const kept = fresh.filter(() => true); +flush([kept.return?.(), fresh.next(), kept.next()]); +const stepping = count("e", 5).drop(1); +flush([stepping.next(), stepping.return(), stepping.next()]); +flush(count("f", 4).map((x) => x).take(2).return()); + +// flatMap over Set and Map results, and a string result's TypeError. +console.log([1, 2].values().flatMap((x) => new Set([x, x * 10])).toArray()); +console.log([1, 2].values().flatMap((x) => new Map([[`k${x}`, x]])).toArray()); +const pairs = new Map ([["m", 1], ["n", 2]]); +for (const [k, v] of [0].values().flatMap(() => pairs)) console.log(k, v); +try { + count("g", 3).flatMap((x) => `s${x}`).toArray(); +} catch (e) { + log.push(`${e.name}: ${e.message}`); +} +flush("string result"); diff --git a/tests/corpus/6073-stored-collection-helpers.ts b/tests/corpus/6073-stored-collection-helpers.ts new file mode 100644 index 000000000..ed739eb85 --- /dev/null +++ b/tests/corpus/6073-stored-collection-helpers.ts @@ -0,0 +1,60 @@ +// Iterator helper objects over Map and Set iterators that are stored and +// consumed later: the helpers observe the collection live, and abandoned +// or returned helpers leave it usable. +const set = new Set([1, 2, 3]); +const doubled = set.values().map((x) => x * 2); +set.add(4); +set.delete(1); +console.log(doubled.next(), [...doubled]); + +const map = new Map([ + ["a", 1], + ["b", 2], + ["c", 3], +]); +const large = map.entries().filter(([, v]) => v > 1); +map.set("d", 4); +map.delete("b"); +console.log([...large].map(([k, v]) => `${k}=${v}`).join(" ")); +const keys = map.keys().map((k, i) => k.toUpperCase() + i); +console.log(keys.next(), keys.return!(), keys.next()); +const rest = map.values().drop(2); +for (const v of rest) console.log("value", v); +const firstTwo = map.keys().take(2); +console.log([...firstTwo], [...firstTwo]); + +// A stage on a stage, and Iterator.from over a Set and a Map. +const chained = set.values().filter((x) => x % 2 === 0).map((x) => `n${x}`); +console.log(chained.toArray()); +const fromSet = Iterator.from(set).map((x) => x + 0.5); +set.add(9); +console.log([...fromSet]); +console.log(Iterator.from(map).map(([k, v]) => k + v).toArray()); + +// A helper left mid-walk does not stop later changes or walks. +const abandoned = set.values().map((x) => x); +abandoned.next(); +for (let i = 0; i < 100; i++) { + set.add(100 + i); + set.delete(100 + i); +} +console.log(set.size, set.values().take(3).toArray(), [...set].length); +const returned = map.values().map((v) => v * 10); +returned.next(); +returned.return!(); +for (let i = 0; i < 50; i++) map.set(`t${i}`, i); +for (let i = 0; i < 50; i++) map.delete(`t${i}`); +console.log(map.size, [...map.keys()].join(",")); + +// Records and strings as elements. +interface Row { + id: number; + tag: string; +} +const rows = new Map([ + [1, { id: 1, tag: "x" }], + [2, { id: 2, tag: "y" }], +]); +const tags = rows.values().map((r) => r.tag.repeat(r.id)); +rows.set(3, { id: 3, tag: "z" }); +console.log([...tags], rows.entries().find(([, r]) => r.tag === "y")?.[0]); diff --git a/tests/corpus/6074-stored-collection-helpers-js.js b/tests/corpus/6074-stored-collection-helpers-js.js new file mode 100644 index 000000000..3a314a3c1 --- /dev/null +++ b/tests/corpus/6074-stored-collection-helpers-js.js @@ -0,0 +1,57 @@ +// The JavaScript form: iterator helper objects over Map and Set iterators +// that are stored and consumed later: the helpers observe the collection live, and abandoned +// or returned helpers leave it usable. +const set = new Set([1, 2, 3]); +const doubled = set.values().map((x) => x * 2); +set.add(4); +set.delete(1); +console.log(doubled.next(), [...doubled]); + +const map = new Map ([ + ["a", 1], + ["b", 2], + ["c", 3], +]); +const large = map.entries().filter(([, v]) => v > 1); +map.set("d", 4); +map.delete("b"); +console.log([...large].map(([k, v]) => `${k}=${v}`).join(" ")); +const keys = map.keys().map((k, i) => k.toUpperCase() + i); +console.log(keys.next(), keys.return(), keys.next()); +const rest = map.values().drop(2); +for (const v of rest) console.log("value", v); +const firstTwo = map.keys().take(2); +console.log([...firstTwo], [...firstTwo]); + +// A stage on a stage, and Iterator.from over a Set and a Map. +const chained = set.values().filter((x) => x % 2 === 0).map((x) => `n${x}`); +console.log(chained.toArray()); +const fromSet = Iterator.from(set).map((x) => x + 0.5); +set.add(9); +console.log([...fromSet]); +console.log(Iterator.from(map).map(([k, v]) => k + v).toArray()); + +// A helper left mid-walk does not stop later changes or walks. +const abandoned = set.values().map((x) => x); +abandoned.next(); +for (let i = 0; i < 100; i++) { + set.add(100 + i); + set.delete(100 + i); +} +console.log(set.size, set.values().take(3).toArray(), [...set].length); +const returned = map.values().map((v) => v * 10); +returned.next(); +returned.return(); +for (let i = 0; i < 50; i++) map.set(`t${i}`, i); +for (let i = 0; i < 50; i++) map.delete(`t${i}`); +console.log(map.size, [...map.keys()].join(",")); + +// Records and strings as elements. + +const rows = new Map ([ + [1, { id: 1, tag: "x" }], + [2, { id: 2, tag: "y" }], +]); +const tags = rows.values().map((r) => r.tag.repeat(r.id)); +rows.set(3, { id: 3, tag: "z" }); +console.log([...tags], rows.entries().find(([, r]) => r.tag === "y")?.[0]); diff --git a/tests/corpus/6075-print-iterators.ts b/tests/corpus/6075-print-iterators.ts new file mode 100644 index 000000000..8b4833264 --- /dev/null +++ b/tests/corpus/6075-print-iterators.ts @@ -0,0 +1,19 @@ +// util.inspect and Object.prototype.toString of iterator objects: generators, +// array, typed array and string iterators and iterator helpers print as +// Object [] {}, and Map and Set iterators print the entries they have +// left. +import { inspect } from "node:util"; +function* g(): Generator { yield 1; } +const s = new Set([1, 2, 3]); +const m = new Map([["a", 1], ["b", 2]]); +const si = s.values(); si.next(); +console.log(g(), [1, 2].values(), "ab"[Symbol.iterator](), new Uint8Array([1]).values()); +console.log(s.values(), s.entries(), m.keys(), m.values(), m.entries(), si); +console.log([1].values().map((x) => x), Iterator.from([1]), g().filter(() => true)); +console.log(inspect({ a: { b: { c: s.values(), d: g() } } })); +console.log(Object.prototype.toString.call(g()), Object.prototype.toString.call([1].values().map((x) => x)), Object.prototype.toString.call(m.keys())); +const done = s.values(); Array.from(done); +console.log(done, new Map().entries(), g()); +const keyed = new Map<{ x: number }, number[]>([[{ x: 1 }, [1, 2]]]); +console.log(keyed.entries(), keyed.keys()); +console.log(new Set(Array.from({ length: 120 }, (_, i) => i)).values()); diff --git a/tests/corpus/6076-print-iterators-js.js b/tests/corpus/6076-print-iterators-js.js new file mode 100644 index 000000000..ad4022b6b --- /dev/null +++ b/tests/corpus/6076-print-iterators-js.js @@ -0,0 +1,17 @@ +// The JavaScript form: util.inspect and Object.prototype.toString of +// iterator objects. +import { inspect } from "node:util"; +function* g() { yield 1; } +const s = new Set([1, 2, 3]); +const m = new Map ([["a", 1], ["b", 2]]); +const si = s.values(); si.next(); +console.log(g(), [1, 2].values(), "ab"[Symbol.iterator](), new Uint8Array([1]).values()); +console.log(s.values(), s.entries(), m.keys(), m.values(), m.entries(), si); +console.log([1].values().map((x) => x), Iterator.from([1]), g().filter(() => true)); +console.log(inspect({ a: { b: { c: s.values(), d: g() } } })); +console.log(Object.prototype.toString.call(g()), Object.prototype.toString.call([1].values().map((x) => x)), Object.prototype.toString.call(m.keys())); +const done = s.values(); Array.from(done); +console.log(done, new Map ().entries(), g()); +const keyed = new Map ([[{ x: 1 }, [1, 2]]]); +console.log(keyed.entries(), keyed.keys()); +console.log(new Set(Array.from({ length: 120 }, (_, i) => i)).values()); diff --git a/tests/corpus/6110-cjs-speculative-wrapper-fences.cjs b/tests/corpus/6110-cjs-speculative-wrapper-fences.cjs index fbb1a65fc..3ae9df3dc 100644 --- a/tests/corpus/6110-cjs-speculative-wrapper-fences.cjs +++ b/tests/corpus/6110-cjs-speculative-wrapper-fences.cjs @@ -5,8 +5,7 @@ // and `typeof module.exports` compile; a statement with no static lowering // (here `module.exports` passed as a value in the class base) compiles to // its runtime fence instead of vanishing. It never runs, so the binary -// matches Node. Coverage counts that one fenced statement once: the class -// base's body is lowered once. +// matches Node. const verbose = process.argv.length > 99; (function () { var root = this; diff --git a/tests/corpus/6200-http-agent-free-socket-data.ts b/tests/corpus/6200-http-agent-free-socket-data.ts new file mode 100644 index 000000000..69e89de49 --- /dev/null +++ b/tests/corpus/6200-http-agent-free-socket-data.ts @@ -0,0 +1,57 @@ +// Bytes that arrive on an idle pooled socket are not read as the next +// response: the agent destroys the socket, and the next request through +// the agent dials a new connection. +import * as http from "node:http"; +import * as net from "node:net"; + +let conns = 0; +const server = net.createServer((sock) => { + const id = ++conns; + let buf = ""; + sock.on("data", (d: Buffer) => { + buf += d.toString(); + while (buf.includes("\r\n\r\n")) { + const head = buf.slice(0, buf.indexOf("\r\n\r\n")); + buf = buf.slice(buf.indexOf("\r\n\r\n") + 4); + const path = head.split(" ")[1]!; + const body = `real ${path} conn${id}`; + sock.write(`HTTP/1.1 200 OK\r\nContent-Length: ${body.length}\r\nKeep-Alive: timeout=5\r\n\r\n${body}`); + if (path === "/poisoned") { + setTimeout(() => sock.write("HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\nPOISON"), 50); + } + } + }); + sock.on("close", () => console.log(`server conn${id} closed`)); + sock.on("error", () => {}); +}); + +const agent = new http.Agent({ keepAlive: true }); +function get(port: number, path: string): Promise { + return new Promise((resolve) => { + const req = http.get({ host: "127.0.0.1", port, path, agent }, (res) => { + let body = ""; + res.on("data", (c: Buffer) => (body += c.toString())); + res.on("end", () => resolve(`${body} reused=${req.reusedSocket}`)); + }); + req.on("error", (e: Error) => resolve(`error ${e.message}`)); + }); +} + +function free(): number { + return Object.keys(agent.freeSockets).length; +} + +server.listen(0, "127.0.0.1", async () => { + const port = (server.address() as net.AddressInfo).port; + console.log(await get(port, "/quiet")); + await new Promise((r) => setTimeout(r, 20)); + console.log("free after /quiet", free()); + console.log(await get(port, "/again")); + console.log(await get(port, "/poisoned")); + await new Promise((r) => setTimeout(r, 200)); + console.log("free after stray bytes", free(), "total", agent.totalSocketCount); + console.log(await get(port, "/next")); + console.log("connections", conns); + agent.destroy(); + server.close(() => console.log("server closed")); +}); diff --git a/tests/diagnostics/collection-iterator-helpers.ts b/tests/diagnostics/collection-iterator-helpers.ts index 127b5c64b..d204e05c3 100644 --- a/tests/diagnostics/collection-iterator-helpers.ts +++ b/tests/diagnostics/collection-iterator-helpers.ts @@ -1,9 +1,13 @@ -// Iterator helpers lower over Map/Set iterators, generators and array -// iterators. These helper forms are refused explicitly. +// Iterator helpers lower over Map/Set iterators, generators and array, +// string and typed array iterators. These helper forms are refused +// explicitly. const set = new Set([1, 2]); -// flatMap callbacks return arrays or generators. -set.values().flatMap((value) => new Set([value, value + 1])); -// return() on a helper object. -const mapped = set.values().map((value) => value * 2); -mapped.return!(); +// return() on an iterator object that is not provably a helper object +// (array, string, Map and Set iterators have no return method). +function close(it: IteratorObject): void { + it.return!(); +} +close(set.values().map((value) => value * 2)); +// throw() on a helper object (helpers have no throw method). +set.values().map((value) => value + 1).throw!(new Error("stop")); // Each helper call must produce a diagnostic. diff --git a/tests/diagnostics/es2025-fences.ts b/tests/diagnostics/es2025-fences.ts index 067da192e..1d141eb84 100644 --- a/tests/diagnostics/es2025-fences.ts +++ b/tests/diagnostics/es2025-fences.ts @@ -1,6 +1,6 @@ // The es2025-lib surface WITHOUT a lowering fences by name: resizable // ArrayBuffers and transfer, SharedArrayBuffer.grow, Atomics.waitAsync, -// first-class iterator objects, and the unlowered forms of the +// Iterator.from over objects with their own next method, and the unlowered forms of the // implemented statics (Promise.try with fewer arguments than parameters, // groupBy's literal-union keys, a set-like non-Set argument). @@ -21,8 +21,8 @@ sab.grow(16); const ia = new Int32Array(4); const waited = Atomics.waitAsync(ia, 0, 0, 1); -// First-class iterator objects. -const it = Iterator.from([1, 2, 3].values()); +// Iterator.from over an object with its own next method. +const it = Iterator.from({ next: (): IteratorResult => ({ value: 1, done: false }) }); // Promise.try with fewer arguments than the callback's parameters. const tried = Promise.try((a: number, b?: number) => a + (b ?? 0), 1); diff --git a/tests/diagnostics/string-iterator-helpers.ts b/tests/diagnostics/string-iterator-helpers.ts deleted file mode 100644 index ed0e2315f..000000000 --- a/tests/diagnostics/string-iterator-helpers.ts +++ /dev/null @@ -1,3 +0,0 @@ -// The string iterator has no iterator-helper lowering; the call is -// refused instead of failing at run time. -const upper = "abc"[Symbol.iterator]().map((c) => c.toUpperCase()); diff --git a/tests/fixtures/node-test/cases/test-name-forms.ts b/tests/fixtures/node-test/cases/test-name-forms.ts new file mode 100644 index 000000000..b2f61dd89 --- /dev/null +++ b/tests/fixtures/node-test/cases/test-name-forms.ts @@ -0,0 +1,55 @@ +// The (name?, options?, fn?) forms of test, it, describe and t.test, and +// Node's default names: a missing or empty name takes the function's name, +// or "". A helper may register tests with computed names and +// functions it receives as parameters. With both skip and todo, skip wins, in either order and +// against the todo method twins. +import { describe, it, test } from "node:test"; + +function declaredTest(): void {} +const boundArrow = (): void => {}; +const emptyName = process.argv.length > 99 ? "never" : ""; + +// A helper that registers tests for its callers: the name is computed and +// the function arrives as a parameter. +function registerCase(label: string, body: () => void): void { + test(`case ${label}`, body); +} +registerCase("one", () => {}); +registerCase("two", declaredTest); + +test(function namedExpression() {}); +test(() => {}); +test(declaredTest); +test(boundArrow); +test("", boundArrow); +test(emptyName, declaredTest); +test({ skip: "options first" }, () => {}); +test({ todo: true }); +test(); +test("name and options", { skip: true }); + +describe(function namedSuite() { + it(() => {}); + it({ todo: "later" }, function insideTodo() {}); +}); +describe({ skip: "whole suite" }, () => { + it("not run", () => {}); +}); + +test("subtests", async (t) => { + await t.test(function namedSub() {}); + await t.test(() => {}); + await t.test({ skip: true }, () => {}); +}); + +test("skip then todo", { skip: "skip message", todo: "todo message" }, () => {}); +test("todo then skip", { todo: "todo message", skip: true }, () => {}); +test.todo("todo twin with skip", { skip: true }, () => {}); +it.skip("skip twin with todo", { todo: true }, () => {}); +// The twins override the option of the same name, and the message is the +// skip string, else the todo string. +test.skip("skip twin with a skip message", { skip: "option message" }, () => {}); +it.todo("todo twin with a todo message", { todo: "option message" }, () => {}); +it.skip("skip twin with a todo message", { todo: "todo message" }, () => {}); +test.todo("todo twin with a skip message", { skip: "skip message" }, () => {}); +test("skip false with a todo message", { skip: false, todo: "todo message" }, () => {}); diff --git a/tests/harness/__snapshots__/collection-iterator-helpers.ts.txt b/tests/harness/__snapshots__/collection-iterator-helpers.ts.txt index 87470b621..9c56aa958 100644 --- a/tests/harness/__snapshots__/collection-iterator-helpers.ts.txt +++ b/tests/harness/__snapshots__/collection-iterator-helpers.ts.txt @@ -1,17 +1,17 @@ -collection-iterator-helpers.ts:5:22 - error SC2020: '.flatMap over a callback returning 'Set'' is part of the standard library types but has no scriptc lowering yet +collection-iterator-helpers.ts:8:3 - error SC2020: 'IteratorObject.return' is part of the standard library types but has no scriptc lowering yet - 4 | // flatMap callbacks return arrays or generators. - 5 | set.values().flatMap((value) => new Set([value, value + 1])); - | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - 6 | // return() on a helper object. + 7 | function close(it: IteratorObject): void { + 8 | it.return!(); + | ^~~~~~~~~ + 9 | } - hint: callbacks returning an array or a generator are the supported forms + hint: the type checker sees the full standard library, but only the supported surface compiles (https://scriptc.dev/limitations) -collection-iterator-helpers.ts:8:1 - error SC2020: 'IteratorObject.return' is part of the standard library types but has no scriptc lowering yet +collection-iterator-helpers.ts:12:1 - error SC2020: 'IteratorObject.throw' is part of the standard library types but has no scriptc lowering yet - 7 | const mapped = set.values().map((value) => value * 2); - 8 | mapped.return!(); - | ^~~~~~~~~~~~~ - 9 | // Each helper call must produce a diagnostic. + 11 | // throw() on a helper object (helpers have no throw method). + 12 | set.values().map((value) => value + 1).throw!(new Error("stop")); + | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 13 | // Each helper call must produce a diagnostic. hint: the type checker sees the full standard library, but only the supported surface compiles (https://scriptc.dev/limitations) \ No newline at end of file diff --git a/tests/harness/__snapshots__/es2025-fences.ts.txt b/tests/harness/__snapshots__/es2025-fences.ts.txt index d7eda27b6..b1ad939fe 100644 --- a/tests/harness/__snapshots__/es2025-fences.ts.txt +++ b/tests/harness/__snapshots__/es2025-fences.ts.txt @@ -45,12 +45,12 @@ es2025-fences.ts:22:16 - error SC2020: 'Atomics.waitAsync' is part of the standa es2025-fences.ts:25:12 - error SC2020: 'Iterator.from' is part of the standard library types but has no scriptc lowering yet - 24 | // First-class iterator objects. - 25 | const it = Iterator.from([1, 2, 3].values()); - | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 24 | // Iterator.from over an object with its own next method. + 25 | const it = Iterator.from({ next: (): IteratorResult => ({ value: 1, done: false }) }); + | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 26 | - hint: iterator helpers apply directly to generators and to array, Map and Set iterators: arr.values().map(f).take(n).toArray() + hint: Iterator.from accepts arrays, strings, typed arrays, Sets, Maps and iterator objects such as generators es2025-fences.ts:28:27 - error SC2020: 'Promise.try over a callback of type '(a: number, b?: number | undefined) => number' with 1 argument' is part of the standard library types but has no scriptc lowering yet diff --git a/tests/harness/__snapshots__/string-iterator-helpers.ts.txt b/tests/harness/__snapshots__/string-iterator-helpers.ts.txt deleted file mode 100644 index ef768934e..000000000 --- a/tests/harness/__snapshots__/string-iterator-helpers.ts.txt +++ /dev/null @@ -1,8 +0,0 @@ -string-iterator-helpers.ts:3:15 - error SC2020: 'StringIterator.map' is part of the standard library types but has no scriptc lowering yet - - 2 | // refused instead of failing at run time. - 3 | const upper = "abc"[Symbol.iterator]().map((c) => c.toUpperCase()); - | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - 4 | - - hint: iterator helpers lower on generators and on array, Map and Set iterators — spread the string first: [...s].values() \ No newline at end of file diff --git a/tests/harness/corpus-costs.json b/tests/harness/corpus-costs.json index 1a4ad1180..8a3f3feb3 100644 --- a/tests/harness/corpus-costs.json +++ b/tests/harness/corpus-costs.json @@ -2092,6 +2092,7 @@ "5812-dyn-box-async-fields.ts": 3522, "5813-mock-timers-module.ts": 3766, "5814-mock-timers-date.ts": 3760, + "5815-als-callback-context.ts": 3110, "5830-timers-promises-signal.ts": 3875, "5831-util-types-brands.ts": 3347, "5832-util-types-brands-dynamic.js": 3250, @@ -2224,6 +2225,8 @@ "6000-json-stringify-class-instances.ts": 7567, "6001-json-stringify-js-class-instances.js": 4668, "601-closures-loops.ts": 1788, + "6010-match-metadata.ts": 3990, + "6011-match-metadata-js.js": 3660, "602-closures-identity-recursion.ts": 2372, "6020-class-extends-function-constructor.js": 4300, "6021-asserted-literal-union-arm.ts": 3400, @@ -2237,6 +2240,12 @@ "6053-class-to-primitive.ts": 4385, "6054-js-class-to-primitive.js": 2181, "6070-iterator-helpers-untyped.js": 4050, + "6071-iterator-sources.ts": 5150, + "6072-iterator-sources-js.js": 5350, + "6073-stored-collection-helpers.ts": 4000, + "6074-stored-collection-helpers-js.js": 4100, + "6075-print-iterators.ts": 3800, + "6076-print-iterators-js.js": 3500, "6100-record-get-or-create.ts": 3771, "6101-record-get-or-create-unchecked/main.ts": 2107, "6103-typeof-builtin-bindings.cjs": 2202, @@ -2257,6 +2266,14 @@ "6154-cjs-require-nested-destructure/main.js": 2800, "6155-cjs-export-table-methods/main.js": 2600, "6156-cjs-function-members-from-ts/main.ts": 3000, + "6200-http-agent-free-socket-data.ts": 3846, + "6230-date-parse-legacy.ts": 2490, + "6231-date-local-fields.ts": 2472, + "6232-date-fields-js.cjs": 2875, + "6233-object-identity-dynamic.cjs": 4636, + "6234-object-prototype-values.cjs": 3149, + "6235-object-identity-typed.ts": 2802, + "6236-builtin-function-values.cjs": 3572, "700-classes-basic.ts": 2530, "701-classes-composition.ts": 2357, "702-classes-this-capture.ts": 2140, @@ -5165,6 +5182,7 @@ "5812-dyn-box-async-fields.ts": 5278, "5813-mock-timers-module.ts": 4227, "5814-mock-timers-date.ts": 17708, + "5815-als-callback-context.ts": 4542, "5830-timers-promises-signal.ts": 4170, "5831-util-types-brands.ts": 4380, "5832-util-types-brands-dynamic.js": 3658, @@ -5297,6 +5315,8 @@ "6000-json-stringify-class-instances.ts": 12107, "6001-json-stringify-js-class-instances.js": 7469, "601-closures-loops.ts": 2297, + "6010-match-metadata.ts": 5450, + "6011-match-metadata-js.js": 4890, "602-closures-identity-recursion.ts": 2427, "6020-class-extends-function-constructor.js": 7500, "6021-asserted-literal-union-arm.ts": 7200, @@ -5310,6 +5330,12 @@ "6053-class-to-primitive.ts": 36270, "6054-js-class-to-primitive.js": 2958, "6070-iterator-helpers-untyped.js": 6900, + "6071-iterator-sources.ts": 5150, + "6072-iterator-sources-js.js": 5350, + "6073-stored-collection-helpers.ts": 4150, + "6074-stored-collection-helpers-js.js": 3600, + "6075-print-iterators.ts": 3450, + "6076-print-iterators-js.js": 3300, "6100-record-get-or-create.ts": 3173, "6101-record-get-or-create-unchecked/main.ts": 3228, "6103-typeof-builtin-bindings.cjs": 1796, @@ -5330,6 +5356,14 @@ "6154-cjs-require-nested-destructure/main.js": 6400, "6155-cjs-export-table-methods/main.js": 6000, "6156-cjs-function-members-from-ts/main.ts": 7000, + "6200-http-agent-free-socket-data.ts": 3452, + "6230-date-parse-legacy.ts": 3133, + "6231-date-local-fields.ts": 3365, + "6232-date-fields-js.cjs": 3637, + "6233-object-identity-dynamic.cjs": 4061, + "6234-object-prototype-values.cjs": 3979, + "6235-object-identity-typed.ts": 3441, + "6236-builtin-function-values.cjs": 4025, "700-classes-basic.ts": 2674, "701-classes-composition.ts": 2606, "702-classes-this-capture.ts": 2403, From a405f856bcebc68018f160f88afa6c905d543647 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Mon, 19 Oct 2026 09:06:15 -0500 Subject: [PATCH 14/83] Iterate class instances whose Symbol.iterator is a generator method A class with a parameterless *[Symbol.iterator]() generator method can now be iterated by for-of, array spread, Array.from, Math.max and Math.min spreads, new Set, new Map, Object.fromEntries and Iterator.from. Each site calls the method once and drives the generator it returns. obj[Symbol.iterator]() and the other well-known symbol methods called through element access now call the class's method, so iterator helpers apply to the result instead of failing on an untyped value. Writes that can replace a well-known symbol method (a symbol-keyed write, Object.defineProperty, a prototype write) now enter the method census, and those classes keep the existing checked or refused paths. --- .../src/frontend/lowering/class-iterables.ts | 35 ++++++++ .../lowering/class-method-mutations.ts | 27 +++++- .../lowering/containers/array-construction.ts | 6 +- .../lowering/containers/collection-methods.ts | 28 ++++++ .../lowering/containers/indexed-objects.ts | 36 +++++++- .../lowering/containers/iterator-helpers.ts | 14 ++- .../src/frontend/lowering/lower-exprs.ts | 4 + .../src/frontend/lowering/lower-stdlib.ts | 6 ++ .../src/frontend/lowering/lower-stmts.ts | 11 +++ .../compiler/src/frontend/lowering/lowerer.ts | 4 + .../src/frontend/lowering/symbol-methods.ts | 30 +++++-- .../test/class-method-mutations.test.ts | 21 +++++ .../6055-class-generator-iterables.ts.json | 6 ++ .../6056-class-symbol-method-calls.ts.json | 6 ++ .../corpus/6055-class-generator-iterables.ts | 86 +++++++++++++++++++ .../corpus/6056-class-symbol-method-calls.ts | 78 +++++++++++++++++ .../6055-class-generator-iterables.ts.json | 1 + .../6056-class-symbol-method-calls.ts.json | 1 + 18 files changed, 386 insertions(+), 14 deletions(-) create mode 100644 packages/compiler/src/frontend/lowering/class-iterables.ts create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6055-class-generator-iterables.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6056-class-symbol-method-calls.ts.json create mode 100644 tests/corpus/6055-class-generator-iterables.ts create mode 100644 tests/corpus/6056-class-symbol-method-calls.ts create mode 100644 tests/harness/corpus-costs/6055-class-generator-iterables.ts.json create mode 100644 tests/harness/corpus-costs/6056-class-symbol-method-calls.ts.json diff --git a/packages/compiler/src/frontend/lowering/class-iterables.ts b/packages/compiler/src/frontend/lowering/class-iterables.ts new file mode 100644 index 000000000..8c58fc0bd --- /dev/null +++ b/packages/compiler/src/frontend/lowering/class-iterables.ts @@ -0,0 +1,35 @@ +import type { IrExpr, IrType } from "../../ir/ir.js"; +import type { Lowerer } from "./lowerer.js"; +import { accessorCall } from "./lower-classes.js"; + +/** The generator a class instance's `[Symbol.iterator]` returns, when that + * method is a generator method taking no arguments: `obj[Symbol.iterator]()`, + * called once, as for-of, spread and Array.from call it. Null for other + * values, and for classes whose iterator the program can replace at run + * time (an instance property, a prototype write). */ +export function classGeneratorIterable(lowerer: Lowerer, value: IrExpr): IrExpr | null { + if (value.type.kind !== "object" || !iteratesThroughGenerator(lowerer, value.type)) return null; + const className = value.type.className; + const ret = lowerer.findMethodOn(lowerer.classes.get(className)!, "sym:iterator")!.sig.ret; + return accessorCall(lowerer, className, "sym:iterator", value, [], ret, value.loc); +} + +/** Whether values of `type` iterate through a generator method (the check + * classGeneratorIterable makes, from the type alone). */ +export function iteratesThroughGenerator(lowerer: Lowerer, type: IrType): boolean { + if (type.kind !== "object") return false; + const info = lowerer.classes.get(type.className); + if (!info || info.def.runtime || info.fields.has("sym:iterator")) return false; + if ( + lowerer.prototypeMethodAccesses.has("sym:iterator") || + lowerer.replacedSymbolMethods.has("sym:iterator") + ) + return false; + const found = lowerer.findMethodOn(info, "sym:iterator"); + return ( + !!found && + found.sig.params.length === 0 && + found.sig.ret.kind === "generator" && + !found.sig.ret.async + ); +} diff --git a/packages/compiler/src/frontend/lowering/class-method-mutations.ts b/packages/compiler/src/frontend/lowering/class-method-mutations.ts index 2a0e3a7eb..cef0bdf5f 100644 --- a/packages/compiler/src/frontend/lowering/class-method-mutations.ts +++ b/packages/compiler/src/frontend/lowering/class-method-mutations.ts @@ -43,9 +43,19 @@ export function collectClassMethodMutations( const jsAssignments: (ts.PropertyAccessExpression | ts.ElementAccessExpression)[] = []; const computedMethods: ts.Expression[] = []; const typedClasses: (ts.ClassDeclaration | ts.ClassExpression)[] = []; + const symbolMethods = new Set(); let unknownKey: ts.Node | undefined; let numericKey: ts.Node | undefined; let unknownOwnField = false; + // `sym:iterator` for `Symbol.iterator`: the name a class method declared + // with a well-known symbol key takes. + const wellKnownMethodName = (key: ts.Expression): string | null => { + while (ts.isParenthesizedExpression(key)) key = key.expression; + if (!ts.isPropertyAccessExpression(key)) return null; + const member = lowerer.stdlibGlobalMember(key, "Symbol"); + if (member === null || !(lowerer.typeOf(key).flags & ts.TypeFlags.ESSymbolLike)) return null; + return `sym:${member}`; + }; // JavaScript method inference and dynamic class factories keep their // existing dispatch contract. The native-call proof applies to typed // declarations (including JavaScript files TypeScript checks), while @@ -556,6 +566,19 @@ export function collectClassMethodMutations( lowerer.prototypeMethodAccesses.set(name, site); } for (const { key, receiver, site } of computedWrites) { + // A class method declared with a well-known symbol key is not a write; + // any other write with one can replace that method. + const symbolName = key ? wellKnownMethodName(unwrap(key)) : null; + if (symbolName) { + const declaration = + (ts.isMethodDeclaration(site) || + ts.isGetAccessorDeclaration(site) || + ts.isSetAccessorDeclaration(site)) && + (ts.isClassDeclaration(site.parent) || ts.isClassExpression(site.parent)); + if (declaration) symbolMethods.add(symbolName); + else lowerer.replacedSymbolMethods.add(symbolName); + continue; + } const type = key ? lowerer.typeOf(key) : undefined; const names = type && (literalValues(type) ?? (key ? literalLoopKeys(unwrap(key)) : null)); if (names) { @@ -586,8 +609,10 @@ export function collectClassMethodMutations( if (String(Number(name)) === name) markName(name, numericKey); } } - if (unknownKey) + if (unknownKey) { for (const method of methods) lowerer.prototypeMethodAccesses.set(method, unknownKey); + for (const method of symbolMethods) lowerer.replacedSymbolMethods.add(method); + } if (!unknownKey && !unknownOwnField) for (const field of arrowFields) { if (!receiverFields.has(field)) lowerer.receiverFreeCallbackFields.add(field); diff --git a/packages/compiler/src/frontend/lowering/containers/array-construction.ts b/packages/compiler/src/frontend/lowering/containers/array-construction.ts index 6072b58b7..5a61f53ce 100644 --- a/packages/compiler/src/frontend/lowering/containers/array-construction.ts +++ b/packages/compiler/src/frontend/lowering/containers/array-construction.ts @@ -44,6 +44,7 @@ import { lowerArrayCallback, callbackArrayElement, } from "./callback-arguments.js"; +import { classGeneratorIterable } from "../class-iterables.js"; /** Build the argument array for a mutating/copying operation while keeping * the receiver's scalar payload ABI. A missing read is represented as the @@ -913,7 +914,7 @@ export function lowerArrayFromCall( // and materializes holes while keeping element reference identity. if ((args.length === 1 || args.length === 2) && !ts.isObjectLiteralExpression(args[0]!)) { const source = lowerer.lowerExpr(args[0]!); - const src = + let src = source.type.kind === "string" && args.length === 2 ? strCharsCall(lowerer, source, loc) : source; @@ -1008,6 +1009,9 @@ export function lowerArrayFromCall( if (src.type.kind === "searchParams" && args.length === 1) { return lowerCheckedArrayFrom(lowerer, lowerer.coerceInto(args[0]!, src, DYN), loc); } + // A class whose [Symbol.iterator] is a generator method iterates the + // generator that method returns. + src = classGeneratorIterable(lowerer, src) ?? src; // A generator (or an iterator adapted into one) drains in order. if (src.type.kind === "generator" && args.length === 1) { const arrT = lowerer.mapTypeOf(lowerer.typeOf(call)); diff --git a/packages/compiler/src/frontend/lowering/containers/collection-methods.ts b/packages/compiler/src/frontend/lowering/containers/collection-methods.ts index a8b782b51..6a5290c0f 100644 --- a/packages/compiler/src/frontend/lowering/containers/collection-methods.ts +++ b/packages/compiler/src/frontend/lowering/containers/collection-methods.ts @@ -42,6 +42,8 @@ import { import { strCharsCall } from "./array-construction.js"; import { arrayElementRead } from "./array-iteration.js"; import { isSearchParamsIterable } from "../builtins/url.js"; +import { classGeneratorIterable } from "../class-iterables.js"; +import { generatorDrain } from "../iterator-adapters.js"; /** Ambient Map method calls. `get`/`set`/`has`/`delete`/`clear` lower to * mapIntrinsic; `forEach` desugars to a direct call of a synthetic loop @@ -1048,6 +1050,14 @@ function setFromSeedValue( } : null; } + // A generator, or a class whose [Symbol.iterator] is a generator method: + // the Set takes every value it yields, in order. + const generator = + classGeneratorIterable(lowerer, source) ?? (source.type.kind === "generator" ? source : null); + if (generator) { + const drained = generatorDrain(lowerer, generator, setT.elem, loc); + return drained ? { kind: "setNew", seed: drained, type: setT, loc } : null; + } let seed: IrExpr; if (source.type.kind === "set" && typeEquals(source.type, setT)) { return { kind: "setIntrinsic", method: "clone", receiver: source, args: [], type: setT, loc }; @@ -1246,6 +1256,24 @@ export function mapFromSeedValue( } : null; } + // A generator of entries, or a class whose [Symbol.iterator] is one: the + // Map takes every [key, value] pair it yields, in order. + const generator = + classGeneratorIterable(lowerer, seed) ?? (seed.type.kind === "generator" ? seed : null); + if (generator) { + const entry: IrType = { + kind: "record", + shapeId: lowerer.shapes.intern( + [ + { name: "0", type: mapT.key }, + { name: "1", type: mapT.value }, + ], + true, + ), + }; + const drained = generatorDrain(lowerer, generator, entry, loc); + return drained ? mapFromSeedValue(lowerer, drained, mapT) : null; + } if (seed.type.kind === "map") { if (!typeEquals(seed.type, mapT)) return null; return { kind: "mapIntrinsic", method: "clone", receiver: seed, args: [], type: mapT, loc }; diff --git a/packages/compiler/src/frontend/lowering/containers/indexed-objects.ts b/packages/compiler/src/frontend/lowering/containers/indexed-objects.ts index 38df368ac..4ba196136 100644 --- a/packages/compiler/src/frontend/lowering/containers/indexed-objects.ts +++ b/packages/compiler/src/frontend/lowering/containers/indexed-objects.ts @@ -28,6 +28,8 @@ import { tryLowerExpression } from "../expressions/try-lower-expression.js"; import { locOf } from "../../program.js"; import { typeKey } from "../../type-mapper.js"; import { type WidthLift } from "../lowerer.js"; +import { classGeneratorIterable, iteratesThroughGenerator } from "../class-iterables.js"; +import { generatorDrain } from "../iterator-adapters.js"; /** Object() and new Object() preserve known object inputs. Primitive * boxing and unproven checked-dynamic values retain their refusal. */ @@ -472,9 +474,37 @@ function lowerTypedObjectFromEntriesCall( if (call.arguments.nodes.length !== 1 || ts.isSpreadElement(call.arguments.nodes[0]!)) return null; const argNode = call.arguments.nodes[0]!; - const argIr = lowerer.mapTypeOf(lowerer.typeOf(argNode)); + let argIr = lowerer.mapTypeOf(lowerer.typeOf(argNode)); + // A generator of entries, or a class whose [Symbol.iterator] is one, + // drains into the entry array the object is built from. + let drained: IrExpr | null = null; + if (argIr?.kind === "generator" || (argIr && iteratesThroughGenerator(lowerer, argIr))) { + const value = lowerer.lowerExpr(argNode); + const generator = + classGeneratorIterable(lowerer, value) ?? (value.type.kind === "generator" ? value : null); + if (generator?.type.kind !== "generator") return null; + // The entries take the result's value type: `[string, V]` for an + // object `{ [k: string]: V }`. + const result = lowerer.mapTypeOf(lowerer.typeOf(call)); + const valueT = + result?.kind === "record" ? lowerer.shapes.get(result.shapeId)?.indexValue : undefined; + if (!valueT) return null; + const entry: IrType = { + kind: "record", + shapeId: lowerer.shapes.intern( + [ + { name: "0", type: STRING }, + { name: "1", type: valueT }, + ], + true, + ), + }; + drained = generatorDrain(lowerer, generator, entry, locOf(call)); + if (!drained) return null; + argIr = drained.type; + } if (argIr?.kind !== "array") return null; - const represented = tryLowerExpression(lowerer, argNode); + const represented = drained ?? tryLowerExpression(lowerer, argNode); if (represented && !typeEquals(represented.type, argIr)) return null; // `Object.fromEntries(rows)` over a `string[][]` VALUE — the env-line // idiom (`envArray.map((env) => env.split('='))`). The checker has no @@ -522,7 +552,7 @@ function lowerTypedObjectFromEntriesCall( // Enumeration/filtering may keep the array in checked-dynamic storage. // Validate its tuple layout before calling a helper with a native-array // ABI; opaque tuple values themselves retain their original references. - const receiver = lowerer.lowerExprExpecting(argNode, argIr); + const receiver = drained ?? lowerer.lowerExprExpecting(argNode, argIr); const key = `obj.fromEntries:${argIr.elem.shapeId}:${resultT.shapeId}`; let helper = lowerer.arrHofHelpers.get(key); if (!helper) { diff --git a/packages/compiler/src/frontend/lowering/containers/iterator-helpers.ts b/packages/compiler/src/frontend/lowering/containers/iterator-helpers.ts index aeefffe48..d439d956e 100644 --- a/packages/compiler/src/frontend/lowering/containers/iterator-helpers.ts +++ b/packages/compiler/src/frontend/lowering/containers/iterator-helpers.ts @@ -34,6 +34,7 @@ import { genValue, stepDrivable, } from "../lower-generators.js"; +import { classGeneratorIterable, iteratesThroughGenerator } from "../class-iterables.js"; import { exitBeforeReturns } from "./for-of.js"; import { iteratorSlotAdapter, @@ -317,9 +318,11 @@ function isHelperObject(lowerer: Lowerer, expr: ts.Expression): boolean { * it rather than a wrapper. A generator (including a helper object or an * array iterator) is itself; an array, string or typed array gives its * values iterator; a Set its values and a Map its entries, as a generator - * over the live collection. The result converts into the call's - * IteratorObject type. Null for other arguments (objects with their own - * next method, untyped values), which keep the Iterator fence. */ + * over the live collection; a class instance whose `[Symbol.iterator]` is + * a generator method gives the generator it returns. The result converts + * into the call's IteratorObject type. Null for other arguments (objects + * with their own next method, untyped values), which keep the Iterator + * fence. */ export function lowerIteratorFromCall( lowerer: Lowerer, call: ts.CallExpression, @@ -332,7 +335,9 @@ export function lowerIteratorFromCall( const argT = lowerer.mapTypeOf(lowerer.typeOf(argNode)); if (resultT?.kind !== "generator" || resultT.async || argT === null) return null; const iterableKinds = ["generator", "array", "string", "bytes", "set", "map"]; - if (!iterableKinds.includes(argT.kind) || (argT.kind === "generator" && argT.async)) return null; + const classIterable = iteratesThroughGenerator(lowerer, argT); + if (!classIterable && !iterableKinds.includes(argT.kind)) return null; + if (argT.kind === "generator" && argT.async) return null; const loc = locOf(call); const value = lowerer.lowerExpr(argNode); const own = (yieldT: IrType): GenType => ({ @@ -344,6 +349,7 @@ export function lowerIteratorFromCall( let iterator: IrExpr | null = null; const t = value.type; if (t.kind === "generator") iterator = value; + else if (classIterable) iterator = classGeneratorIterable(lowerer, value); else if (t.kind === "array") iterator = lowerArrayIteratorValue(lowerer, value, "values", own(t.elem), loc); else if (t.kind === "string") diff --git a/packages/compiler/src/frontend/lowering/lower-exprs.ts b/packages/compiler/src/frontend/lowering/lower-exprs.ts index 09407f71f..654133b5e 100644 --- a/packages/compiler/src/frontend/lowering/lower-exprs.ts +++ b/packages/compiler/src/frontend/lowering/lower-exprs.ts @@ -224,6 +224,7 @@ import { } from "./expressions/optional-chains.js"; import { foldedStringKeyOf } from "./expressions/object-literals.js"; import { tryLowerExpression } from "./expressions/try-lower-expression.js"; +import { classGeneratorIterable } from "./class-iterables.js"; import { classToNumberConversion, classToStringConversion, @@ -6244,6 +6245,9 @@ export function lowerArrayLiteral( loc: locOf(el), }; } + // `[...obj]` over a class whose [Symbol.iterator] is a generator + // method spreads the generator that method returns. + src = classGeneratorIterable(lowerer, src) ?? src; // `[...it]` over a generator (or an iterator adapted into one): // drain it once, in order, into a fresh element array. if (src.type.kind === "generator") { diff --git a/packages/compiler/src/frontend/lowering/lower-stdlib.ts b/packages/compiler/src/frontend/lowering/lower-stdlib.ts index 92d9f0690..01e02c8ff 100644 --- a/packages/compiler/src/frontend/lowering/lower-stdlib.ts +++ b/packages/compiler/src/frontend/lowering/lower-stdlib.ts @@ -42,6 +42,8 @@ import { STATIC_RESPONSE_CALLS, STATIC_RESPONSE_READS, } from "../../compat/fetch-profile.js"; +import { classGeneratorIterable } from "./class-iterables.js"; +import { generatorDrain } from "./iterator-adapters.js"; export function refuseUnsupportedApi( lowerer: Lowerer, @@ -2438,6 +2440,10 @@ export function lowerStdlibMethodCall( src = lowerer.coerceInto(spread.expression, src, inferred); } } + // A generator, or a class whose [Symbol.iterator] is a generator + // method, drains into the number[] the fold reads. + src = classGeneratorIterable(lowerer, src) ?? src; + if (src.type.kind === "generator") src = generatorDrain(lowerer, src, F64, loc) ?? src; if (src.type.kind !== "array" || src.type.elem.kind !== "f64") { lowerer.unsupported( "SC1090", diff --git a/packages/compiler/src/frontend/lowering/lower-stmts.ts b/packages/compiler/src/frontend/lowering/lower-stmts.ts index 36abb9130..766583917 100644 --- a/packages/compiler/src/frontend/lowering/lower-stmts.ts +++ b/packages/compiler/src/frontend/lowering/lower-stmts.ts @@ -217,6 +217,7 @@ import { constituentTypes } from "../ts7/checker.js"; import { npmStaticPackageOfPath } from "../npm-static.js"; import { fenceNodeModuleMutation } from "./lower-node-module.js"; import { fencePerfEntryKeyWalk } from "./builtins/performance.js"; +import { classGeneratorIterable } from "./class-iterables.js"; interface UsingRegistration { acquire: IrStmt[]; @@ -8767,6 +8768,16 @@ export function lowerForOf(lowerer: Lowerer, stmt: ts.ForOfStatement): IrStmt { ); } if (iterable.type.kind === "object") { + // A [Symbol.iterator] generator method: the loop drives the + // generator it returns. + const generator = classGeneratorIterable(lowerer, iterable); + if (generator) + return lowerForOfGenerator( + lowerer, + stmt, + generator as IrExpr & { type: IrType & { kind: "generator" } }, + labels, + ); // Class iterables ([Symbol.iterator]() + next() — classIteratorOf) // drive the same protocol with ordinary method calls. const cit = lowerer.classIteratorOf(iterable.type); diff --git a/packages/compiler/src/frontend/lowering/lowerer.ts b/packages/compiler/src/frontend/lowering/lowerer.ts index d252af250..a4fa362c5 100644 --- a/packages/compiler/src/frontend/lowering/lowerer.ts +++ b/packages/compiler/src/frontend/lowering/lowerer.ts @@ -2584,6 +2584,10 @@ export class Lowerer { /** Named method slots observed before class collection need a callable * ABI and shared prototype descriptors instead of call-site specialization. */ readonly prototypeMethodAccesses = new Map(); + /** Well-known symbol methods (`sym:iterator`) the program may replace + * through a symbol-keyed write: an instance or prototype property, or a + * reflection call. */ + readonly replacedSymbolMethods = new Set(); /** Typed calls through class-backed protocols, by signature; class * dynamic dispatch adds their native arms (class-protocol-dispatch.ts). */ readonly protocolDispatches = new Map(); diff --git a/packages/compiler/src/frontend/lowering/symbol-methods.ts b/packages/compiler/src/frontend/lowering/symbol-methods.ts index f6a8e68e4..b02e95641 100644 --- a/packages/compiler/src/frontend/lowering/symbol-methods.ts +++ b/packages/compiler/src/frontend/lowering/symbol-methods.ts @@ -2,10 +2,11 @@ import * as ts from "../ts7/adapter.js"; import { locOf } from "../program.js"; import type { IrExpr } from "../../ir/ir.js"; import type { Lowerer } from "./lowerer.js"; -import { accessorCall } from "./lower-classes.js"; +import { accessorCall, type ClassInfo } from "./lower-classes.js"; import { classSymbolKeyOf } from "./symbol-fields.js"; -/** A stable symbol method uses the same completed arguments and virtual +/** A stable symbol method (a module-scope symbol, or a well-known symbol + * such as `Symbol.iterator`) uses the same completed arguments and virtual * dispatch as a string-named class method. Runtime-selected keys stay on * the checked-property path. */ export function lowerClassSymbolMethodCall( @@ -15,12 +16,13 @@ export function lowerClassSymbolMethodCall( const access = call.expression; if (!ts.isElementAccessExpression(access) || access.questionDotToken || call.questionDotToken) return null; - const key = classSymbolKeyOf(lowerer, access.argumentExpression); - if (!key) return null; const type = lowerer.mapTypeOf(lowerer.typeOf(access.expression)); if (type?.kind !== "object") return null; const info = lowerer.classes.get(type.className); - const name = info?.symbolMethods?.get(key.identity); + const key = classSymbolKeyOf(lowerer, access.argumentExpression); + const name = key + ? info?.symbolMethods?.get(key.identity) + : wellKnownSymbolMethod(lowerer, info, access.argumentExpression); if (!name) return null; const method = lowerer.findMethodOn(info ?? null, name); if (!method) return null; @@ -29,3 +31,21 @@ export function lowerClassSymbolMethodCall( const args = lowerer.completeArgs(call.arguments.nodes, method.sig.params, loc, call); return accessorCall(lowerer, type.className, name, receiver, args, method.sig.ret, loc); } + +/** The `sym:` method a well-known symbol key such as `Symbol.iterator` + * names, when the class declares it as a method and the program cannot + * replace it at run time (an instance property of that name, a prototype + * write). */ +function wellKnownSymbolMethod( + lowerer: Lowerer, + info: ClassInfo | undefined, + key: ts.Expression, +): string | null { + while (ts.isParenthesizedExpression(key)) key = key.expression; + if (!info || info.def.runtime || !ts.isPropertyAccessExpression(key)) return null; + const member = lowerer.stdlibGlobalMember(key, "Symbol"); + if (member === null) return null; + const name = `sym:${member}`; + if (info.fields.has(name) || lowerer.replacedSymbolMethods.has(name)) return null; + return name; +} diff --git a/packages/compiler/test/class-method-mutations.test.ts b/packages/compiler/test/class-method-mutations.test.ts index 2258bd54a..b508a3949 100644 --- a/packages/compiler/test/class-method-mutations.test.ts +++ b/packages/compiler/test/class-method-mutations.test.ts @@ -114,3 +114,24 @@ test("a computed key in an Object.assign source keeps checked dispatch", async ( .replace("LITERAL", "Object.assign(target, { [key]: value })"); expect(await callersOf(source, "%dyn.class.properties")).not.toEqual([]); }); + +const range = ` +class Range { + hi: number; + constructor(hi: number) { this.hi = hi; } + *[Symbol.iterator](): Generator { for (let i = 0; i < this.hi; i++) yield i; } +} +const r = new Range(3); +console.log(r[Symbol.iterator]().next().value); +`; + +test("a well-known symbol method call lowers to the class's method", async () => { + expect(await callersOf(range, "%Range.sym:iterator")).not.toEqual([]); +}); + +test("a reflection call with a well-known symbol key keeps checked dispatch", async () => { + const write = "Object.defineProperty(r, Symbol.iterator, { value: function* () {} });\n"; + // The checked path reads the declared method through its value thunk. + const callers = await callersOf(`${range}${write}`, "%Range.sym:iterator"); + expect(callers.filter((name) => !name.startsWith("%method.value:"))).toEqual([]); +}); diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6055-class-generator-iterables.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6055-class-generator-iterables.ts.json new file mode 100644 index 000000000..661995e3f --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6055-class-generator-iterables.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6055-class-generator-iterables.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6056-class-symbol-method-calls.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6056-class-symbol-method-calls.ts.json new file mode 100644 index 000000000..445389b5e --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6056-class-symbol-method-calls.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6056-class-symbol-method-calls.ts" + ], + "diags": [] +} diff --git a/tests/corpus/6055-class-generator-iterables.ts b/tests/corpus/6055-class-generator-iterables.ts new file mode 100644 index 000000000..50641c261 --- /dev/null +++ b/tests/corpus/6055-class-generator-iterables.ts @@ -0,0 +1,86 @@ +// Classes whose [Symbol.iterator] is a generator method iterate the +// generator it returns, called once: in for-of (an early exit closes it), +// array and Math.max spreads, Array.from, new Set, new Map, +// Object.fromEntries and yield*. +class Counter implements Iterable { + n: number; + constructor(n: number) { + this.n = n; + } + *[Symbol.iterator](): Generator { + for (let i = 0; i < this.n; i++) yield i; + } +} +const c = new Counter(3); +for (const v of c) console.log("of", v); +console.log([...c], Array.from(c), Math.max(...c)); +for (const v of c) { + if (v === 1) break; + console.log("early", v); +} + +class Pairs implements Iterable<[string, number]> { + *[Symbol.iterator](): Generator<[string, number]> { + yield ["a", 1]; + yield ["b", 2]; + } +} +class Nums implements Iterable { + *[Symbol.iterator](): Generator { + yield 1; + yield 2; + yield 2; + } +} +const p = new Pairs(); +const n = new Nums(); +console.log(new Set(n).size); +console.log(new Map(p).get("b")); + +console.log(Object.fromEntries(p)); +function* outer(): Generator { + yield 0; + yield* n; +} +console.log([...outer()]); +function* plain(): Generator { + yield 3; + yield 3; + yield 4; +} +console.log(new Set(plain()).size, [...new Set(n)]); + +class Tracked implements Iterable { + log: string[] = []; + *[Symbol.iterator](): Generator { + try { + for (const word of ["a", "b", "c"]) { + this.log.push("yield " + word); + yield word; + } + } finally { + this.log.push("closed"); + } + } +} +const t = new Tracked(); +for (const w of t) { + if (w === "b") break; + t.log.push("saw " + w); +} +console.log(t.log.join(", ")); +class Tree implements Iterable { + value: number; + children: Tree[]; + constructor(value: number, children: Tree[] = []) { + this.value = value; + this.children = children; + } + *[Symbol.iterator](): Generator { + yield this.value; + for (const child of this.children) yield* child; + } +} +class Leafy extends Tree {} +const tree = new Tree(1, [new Tree(2, [new Tree(3)]), new Leafy(4)]); +console.log([...tree], Array.from(new Leafy(9)), Math.min(...tree)); diff --git a/tests/corpus/6056-class-symbol-method-calls.ts b/tests/corpus/6056-class-symbol-method-calls.ts new file mode 100644 index 000000000..2829dc067 --- /dev/null +++ b/tests/corpus/6056-class-symbol-method-calls.ts @@ -0,0 +1,78 @@ +// Well-known symbol methods called through element access, and +// Iterator.from over a class whose [Symbol.iterator] is a generator method: +// both give the class's own generator, so iterator helpers apply to it. +class Range { + lo: number; + hi: number; + constructor(lo: number, hi: number) { + this.lo = lo; + this.hi = hi; + } + *[Symbol.iterator](): Generator { + for (let i = this.lo; i < this.hi; i++) yield i; + } +} +class Labeled extends Range { + label = "l"; +} +class Words { + text: string; + constructor(text: string) { + this.text = text; + } + *[Symbol.iterator](): Generator { + for (const w of this.text.split(" ")) yield w; + } +} +class Money { + cents: number; + constructor(cents: number) { + this.cents = cents; + } + [Symbol.toPrimitive](hint: string): string | number { + return hint === "number" ? this.cents / 100 : `$${(this.cents / 100).toFixed(2)}`; + } +} +class Handle { + name: string; + constructor(name: string) { + this.name = name; + } + [Symbol.dispose](): void { + console.log("dispose", this.name); + } +} + +const r = new Range(1, 6); +const direct = r[Symbol.iterator](); +console.log(direct.next(), direct.next()); +console.log(r[Symbol.iterator]().map((x) => x + 1).toArray()); +const ranges: Range[] = [new Range(0, 3), new Labeled(5, 7)]; +for (const x of ranges) console.log(x[Symbol.iterator]().toArray()); +console.log(r[Symbol.iterator]().filter((x) => x > 3).toArray()); + +const it = Iterator.from(r); +console.log(it.next().value, it.next().value); +console.log([...it]); +console.log(Iterator.from(r).map((x) => x * 10).toArray()); +console.log(Iterator.from(r).filter((x) => x % 2 === 1).toArray()); +console.log(Iterator.from(r).take(2).toArray(), Iterator.from(r).drop(3).toArray()); +console.log(Iterator.from(r).reduce((a, b) => a + b, 0)); +console.log(Iterator.from(r).some((x) => x > 4), Iterator.from(r).every((x) => x > 0)); +console.log(Iterator.from(r).find((x) => x > 2)); +console.log(Iterator.from(new Words("a bb ccc")).map((w) => w.length).toArray()); +Iterator.from(r).forEach((x) => console.log("each", x)); +for (const x of Iterator.from(r).flatMap((x) => [x, -x]).take(4)) console.log(x); +const g = Iterator.from(r); +for (const x of g) { + if (x === 2) break; +} +console.log(g.next()); + +console.log(Iterator.from(r), r[Symbol.iterator](), Iterator.from(r).map((x) => x)); +console.log(Iterator.from(r).flatMap((x) => new Set([x, x + 10])).toArray()); +console.log(r[Symbol.iterator]().flatMap((x) => new Map([[x, String(x)]])).toArray()); + +const m = new Money(1234); +console.log(m[Symbol.toPrimitive]("number"), m[Symbol.toPrimitive]("default")); +new Handle("h")[Symbol.dispose](); diff --git a/tests/harness/corpus-costs/6055-class-generator-iterables.ts.json b/tests/harness/corpus-costs/6055-class-generator-iterables.ts.json new file mode 100644 index 000000000..68b6f9db4 --- /dev/null +++ b/tests/harness/corpus-costs/6055-class-generator-iterables.ts.json @@ -0,0 +1 @@ +{ "plain": 4921, "san": 6451 } diff --git a/tests/harness/corpus-costs/6056-class-symbol-method-calls.ts.json b/tests/harness/corpus-costs/6056-class-symbol-method-calls.ts.json new file mode 100644 index 000000000..4207a0ed0 --- /dev/null +++ b/tests/harness/corpus-costs/6056-class-symbol-method-calls.ts.json @@ -0,0 +1 @@ +{ "plain": 3468, "san": 5120 } From ee8011cea5560f76041a100016215e5072a788ad Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 12:03:34 -0700 Subject: [PATCH 15/83] Split esbuild CommonJS chunks into their own modules Recognize esbuild's __commonJS helper (keyed and arrow forms, the Object.keys spelling of 0.11) and move every chunk body into a virtual sibling CommonJS module, so require_x() becomes a require of that module and the ordinary CommonJS machinery compiles it. Also accept minified helper declarations that share one var statement, counter-suffixed helper names inside bundled bundles, and treat .cjs files as CommonJS in the interop rewrite. Co-Authored-By: Claude Opus 5.5 --- .../src/frontend/helper-tokens.test.ts | 8 +- .../compiler/src/frontend/helper-tokens.ts | 15 +- .../compiler/src/frontend/input-tracker.ts | 30 +- .../src/frontend/npm-static-bundled-chunks.ts | 586 ++++++++++++++++++ .../src/frontend/npm-static-rewrite-host.ts | 2 +- .../src/frontend/npm-static-rewrite-syntax.ts | 143 +++-- packages/compiler/src/frontend/npm-static.ts | 25 +- packages/compiler/src/frontend/services.ts | 8 + 8 files changed, 761 insertions(+), 56 deletions(-) create mode 100644 packages/compiler/src/frontend/npm-static-bundled-chunks.ts diff --git a/packages/compiler/src/frontend/helper-tokens.test.ts b/packages/compiler/src/frontend/helper-tokens.test.ts index c6355cef9..522b39545 100644 --- a/packages/compiler/src/frontend/helper-tokens.test.ts +++ b/packages/compiler/src/frontend/helper-tokens.test.ts @@ -27,6 +27,8 @@ test.each([ ['"default"', '"default;s3:foo;"'], ['"default"', '"default\\0"'], ["mod", "mod other"], + ["cb[0]", "cb[1]"], + ["cb[1]", "cb[10]"], ])("retains a semantic difference between %s and %s", (a, b) => { expect(helperTokens(a)).not.toBe(helperTokens(b)); }); @@ -44,8 +46,12 @@ test.each([ "'\\8'", "`template`", "/regexp/", - "0", "0x10", + "07", + "1.5", + "1e3", + "1n", + "1_000", "a / b", "\\u0061", "café", diff --git a/packages/compiler/src/frontend/helper-tokens.ts b/packages/compiler/src/frontend/helper-tokens.ts index d7a968755..908cc0d22 100644 --- a/packages/compiler/src/frontend/helper-tokens.ts +++ b/packages/compiler/src/frontend/helper-tokens.ts @@ -1,8 +1,8 @@ /** Token fingerprints for the fixed bundler helpers we recognize. This is * deliberately not a JavaScript parser: regexes, templates, escaped names, - * numbers, and unknown punctuation refuse the helper. None occur in the - * canonical helpers. Every accepted token is represented in the result; - * comments and whitespace alone may disappear. */ + * numbers other than plain decimal integers, and unknown punctuation refuse + * the helper. None occur in the canonical helpers. Every accepted token is + * represented in the result; comments and whitespace alone may disappear. */ const operators = [ ">>>=", "===", @@ -157,6 +157,15 @@ export function helperTokens(text: string): string | null { result += fingerprint("s", token.value); i = token.end; previous = "string"; + } else if (/[0-9]/.test(ch)) { + // Plain decimal integers only (esbuild's `cb[keys(cb)[0]]`): a + // legacy octal, fraction, exponent, radix prefix, BigInt suffix or + // numeric separator refuses rather than aliasing another value. + const start = i++; + while (i < text.length && /[0-9]/.test(text.charAt(i))) i++; + if (/[A-Za-z0-9_$.\\]/.test(text.charAt(i)) || (ch === "0" && i - start > 1)) return null; + previous = text.slice(start, i); + result += fingerprint("n", previous); } else if (/[A-Za-z_$]/.test(ch)) { const start = i++; while (i < text.length && /[A-Za-z0-9_$]/.test(text.charAt(i))) i++; diff --git a/packages/compiler/src/frontend/input-tracker.ts b/packages/compiler/src/frontend/input-tracker.ts index 304a21cfa..b710988a3 100644 --- a/packages/compiler/src/frontend/input-tracker.ts +++ b/packages/compiler/src/frontend/input-tracker.ts @@ -164,8 +164,24 @@ export function markFrontendInputsUnstable(): void { (synchronousTracker ?? activeTracker.getStore())?.markUnstable(); } +/** Compiler-synthesized module sources (a bundle's CommonJS chunks split + * into sibling modules, npm-static-bundled-chunks.ts). They answer like + * regular files but are never recorded: each derives from the content of + * a recorded bundle read, which already keys every cache that sees it. */ +const virtualSources = new Map(); + +export function setVirtualSource(path: string, text: string): void { + virtualSources.set(resolve(path), text); +} + +export function clearVirtualSources(): void { + virtualSources.clear(); +} + export function trackedReadFile(path: string): string | null { path = resolve(path); + const virtual = virtualSources.get(path); + if (virtual !== undefined) return virtual; try { const text = readFileSync(path, "utf8"); record({ op: "file", path, digest: frontendSourceDigest(text) }); @@ -178,6 +194,7 @@ export function trackedReadFile(path: string): string | null { export function trackedFileExists(path: string): boolean { path = resolve(path); + if (virtualSources.has(path)) return true; const kind = pathKind(path); record({ op: "kind", path, kind }); return kind === "file"; @@ -185,6 +202,7 @@ export function trackedFileExists(path: string): boolean { export function trackedDirectoryExists(path: string): boolean { path = resolve(path); + if (virtualSources.has(path)) return false; const kind = pathKind(path); record({ op: "kind", path, kind }); return kind === "directory"; @@ -192,6 +210,7 @@ export function trackedDirectoryExists(path: string): boolean { export function trackedExists(path: string): boolean { path = resolve(path); + if (virtualSources.has(path)) return true; const kind = pathKind(path); record({ op: "kind", path, kind }); return kind !== "missing"; @@ -199,6 +218,7 @@ export function trackedExists(path: string): boolean { export function trackedRealpath(path: string): string | null { path = resolve(path); + if (virtualSources.has(path)) return path; try { const target = systemRealpath(path); record({ op: "realpath", path, target }); @@ -223,9 +243,13 @@ export function trackedAccessibleEntries( } files.sort(); directories.sort(); - const answer = { files, directories }; - record({ op: "entries", path, files, directories }); - return answer; + record({ op: "entries", path, files: [...files], directories }); + for (const virtual of virtualSources.keys()) { + if (dirname(virtual) === path && !files.includes(basename(virtual))) + files.push(basename(virtual)); + } + files.sort(); + return { files, directories }; } catch { // Enumeration can fail even while the path remains a directory. Preserve // the failed operation itself so a permission/ACL repair invalidates the diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts new file mode 100644 index 000000000..e1256e59f --- /dev/null +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts @@ -0,0 +1,586 @@ +/* Bundled CommonJS chunks: esbuild wraps every CommonJS module it bundles + * in a lazily evaluated factory, + * + * var require_x = __commonJS({ "src/x.js"(exports, module) { BODY } }); + * + * and each `require_x()` runs BODY once (later calls answer the cached + * module.exports, a call during BODY answers the partial one). That is + * exactly a CommonJS module that the bundle requires at the call's + * position, so each recognized chunk becomes its own module: a virtual + * sibling file `..cjs` holding BODY, its module + * parameters respelled as the CommonJS globals, and every `require_x()` + * call becomes `require("./..cjs")`. The program then + * compiles chunks with the ordinary CommonJS machinery: evaluation at the + * require's position, the run-once module cache, export tables, and the + * __toESM interop erasure. + * + * Only shapes whose meaning is verified move: the helper must be one of + * the recognized __commonJS spellings, every reference to a factory + * binding must be a direct call, and BODY must not observe the + * difference between a factory and a module (its `this`, `arguments`, + * `__filename`, the module object beyond `module.exports`, a top-level + * return, or a binding of the bundle other than the pure helpers copied + * beside it). Anything else answers a degrade reason. */ +import * as ts from "./ts7/syntax.js"; +import { helperTokens } from "./helper-tokens.js"; + +export interface BundledChunkSplit { + /** The bundle with chunk declarations blanked and calls respelled. */ + text: string; + /** Virtual sibling modules, in declaration order. */ + chunks: { path: string; source: string }[]; +} + +interface Edit { + start: number; + end: number; + text: string; +} + +const CHUNK_SHAPE_DEGRADE = + "its shipped JS bundles CommonJS modules through the __commonJS helper in a shape the " + + "recognizer cannot verify"; + +/** The helper a counter-suffixed bundler name spells (`__commonJS2`). */ +function helperBase(name: string): string { + return /^__[A-Za-z]+\d+$/.test(name) ? name.replace(/\d+$/, "") : name; +} + +function unparen(e: ts.Expression): ts.Expression { + while (ts.isParenthesizedExpression(e)) e = e.expression; + return e; +} + +/** True for an identifier occurrence that reads or writes a binding (as + * opposed to a property name, a label, or a declaration's own name). + * Shorthand property values count: `{ x }` reads x. */ +function isReference(id: ts.Identifier): boolean { + const p = id.parent; + if (p === undefined) return false; + if (ts.isPropertyAccessExpression(p)) return p.expression === id; + if (ts.isPropertyAssignment(p)) return p.initializer === id; + if ( + ts.isMethodDeclaration(p) || + ts.isPropertyDeclaration(p) || + ts.isGetAccessorDeclaration(p) || + ts.isSetAccessorDeclaration(p) + ) + return p.name !== id; + if (ts.isBindingElement(p)) return p.initializer === id; + if (ts.isVariableDeclaration(p) || ts.isParameter(p)) return p.initializer === id; + if ( + ts.isFunctionDeclaration(p) || + ts.isFunctionExpression(p) || + ts.isClassDeclaration(p) || + ts.isClassExpression(p) + ) + return p.name !== id; + if (ts.isLabeledStatement(p)) return p.label !== id; + if (ts.isBreakStatement(p) || ts.isContinueStatement(p)) return false; + if (ts.isMetaProperty(p)) return false; + return true; +} + +/** Names a subtree declares anywhere inside it: variables (destructuring + * included), parameters, function and class names, catch bindings. */ +function declaredNames(root: ts.Node): Set { + const names = new Set(); + // Binding patterns nest BindingElements, which the walk visits too. + ts.walkPreorder(root, (n) => { + if (n.kind >= ts.SyntaxKind.FirstJSDocNode && n.kind <= ts.SyntaxKind.LastJSDocNode) + return "skip"; + if (ts.isVariableDeclaration(n) || ts.isParameter(n) || ts.isBindingElement(n)) { + const name = n.name; + if (name !== undefined && ts.isIdentifier(name)) names.add(name.text); + } else if ( + (ts.isFunctionDeclaration(n) || + ts.isFunctionExpression(n) || + ts.isClassDeclaration(n) || + ts.isClassExpression(n)) && + n.name !== undefined + ) + names.add(n.name.text); + return undefined; + }); + return names; +} + +/** Every binding reference inside a subtree. */ +function references(root: ts.Node): ts.Identifier[] { + const out: ts.Identifier[] = []; + ts.walkPreorder(root, (n) => { + if (n.kind >= ts.SyntaxKind.FirstJSDocNode && n.kind <= ts.SyntaxKind.LastJSDocNode) + return "skip"; + if (ts.isIdentifier(n) && isReference(n)) out.push(n); + return undefined; + }); + return out; +} + +/** True when the reference is written (assignment target, update operand, + * loop target, delete operand). */ +function isWritten(id: ts.Identifier): boolean { + let child: ts.Node = id; + for (let p = child.parent; p !== undefined; child = p, p = p.parent) { + if (ts.isBinaryExpression(p)) { + return ( + p.left === child && + p.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && + p.operatorToken.kind <= ts.SyntaxKind.LastAssignment + ); + } + if (ts.isPrefixUnaryExpression(p) || ts.isPostfixUnaryExpression(p)) { + return ( + p.operator === ts.SyntaxKind.PlusPlusToken || p.operator === ts.SyntaxKind.MinusMinusToken + ); + } + if (ts.isForInStatement(p) || ts.isForOfStatement(p)) return p.initializer === child; + if (ts.isDeleteExpression(p)) return true; + if ( + !( + ts.isParenthesizedExpression(p) || + ts.isArrayLiteralExpression(p) || + ts.isObjectLiteralExpression(p) || + ts.isShorthandPropertyAssignment(p) || + ts.isPropertyAssignment(p) || + ts.isSpreadElement(p) + ) + ) + return false; + } + return false; +} + +/** The recognized __commonJS helper forms, as token templates over the + * helper's own parameter names (CB, MOD) and the own-key reader (KEYS: + * esbuild's `__getOwnPropNames` alias, or `Object.keys` in esbuild 0.11): + * keyed: `(CB, MOD) => function [name]() { return ; }` — the + * factory is the only method of an object literal; + * arrow: `(CB, MOD) => () => ()` — the factory itself. */ +const KEYED_RETURN = "MOD || (0, CB[KEYS(CB)[0]])((MOD = { exports: {} }).exports, MOD), MOD.exports"; +const ARROW_BODY = "(MOD || CB((MOD = { exports: {} }).exports, MOD), MOD.exports)"; + +function template(text: string, cb: string, mod: string, keys: string): string | null { + return helperTokens( + text + .replace(/\bCB\b/g, cb) + .replace(/\bMOD\b/g, mod) + .replace(/\bKEYS\b/g, keys), + ); +} + +type HelperForm = "keyed" | "arrow"; + +/** The helper's form, or null when its initializer is not recognized. + * `ownNames` answers whether an identifier names the verified + * Object.getOwnPropertyNames alias. */ +function helperForm( + init: ts.Expression, + sf: ts.SourceFile, + ownNames: (name: string) => boolean, +): HelperForm | null { + const fn = unparen(init); + if (!ts.isArrowFunction(fn) || fn.parameters.nodes.length !== 2) return null; + const params = fn.parameters.nodes.map((p) => + ts.isIdentifier(p.name) && p.initializer === undefined && p.dotDotDotToken === undefined + ? p.name.text + : null, + ); + const [cb, mod] = params; + if (cb === null || mod === null || cb === undefined || mod === undefined || cb === mod) + return null; + if (ts.isBlock(fn.body)) return null; + const body = unparen(fn.body); + if (ts.isFunctionExpression(body)) { + if (body.parameters.nodes.length !== 0 || body.asteriskToken || body.modifiers?.nodes.length) + return null; + const stmts = body.body.statements.nodes; + const ret = stmts[0]; + if (stmts.length !== 1 || ret === undefined || !ts.isReturnStatement(ret) || !ret.expression) + return null; + const actual = helperTokens(ret.expression.getText(sf)); + if (actual === null) return null; + // The own-key reader: esbuild's alias or the 0.11 spelling. + const keysRef = /\[\s*([A-Za-z_$][\w$]*(?:\.keys)?)\s*\(/.exec(ret.expression.getText(sf)); + const keys = keysRef?.[1]; + if (keys === undefined || (keys !== "Object.keys" && !ownNames(keys))) return null; + return actual === template(KEYED_RETURN, cb, mod, keys) ? "keyed" : null; + } + if (ts.isArrowFunction(body) && body.parameters.nodes.length === 0 && !ts.isBlock(body.body)) { + const actual = helperTokens(body.body.getText(sf)); + return actual !== null && actual === template(ARROW_BODY, cb, mod, "") ? "arrow" : null; + } + return null; +} + +/** A top-level declaration the virtual chunk can copy verbatim: a pure + * helper whose evaluation runs nothing (a function, or a member read of + * Object such as `Object.create` / `Object.prototype.hasOwnProperty`). */ +function copyableInitializer(init: ts.Expression | undefined): boolean { + if (init === undefined) return false; + const e = unparen(init); + if (ts.isArrowFunction(e) || ts.isFunctionExpression(e)) return true; + let walk: ts.Expression = e; + let depth = 0; + while (ts.isPropertyAccessExpression(walk) && ts.isIdentifier(walk.name)) { + walk = walk.expression; + depth++; + } + return depth > 0 && ts.isIdentifier(walk) && walk.text === "Object"; +} + +interface TopLevel { + stmt: ts.Statement; + /** The declarator (or function declaration) itself. */ + node: ts.VariableDeclaration | ts.FunctionDeclaration; +} + +/** True when the file opens with a "use strict" directive prologue. */ +function strictPrologue(stmts: readonly ts.Statement[]): boolean { + for (const s of stmts) { + if (!ts.isExpressionStatement(s) || !ts.isStringLiteral(s.expression)) return false; + if (s.expression.text === "use strict") return true; + } + return false; +} + +/** Splits a CommonJS bundle's __commonJS chunks into virtual modules (see + * the header). Null: no chunks (or not a CommonJS bundle) — serve the file + * unchanged. */ +export function splitBundledChunks( + sf: ts.SourceFile, + filePath: string, +): BundledChunkSplit | { degrade: string } | null { + const source = sf.text; + if (!source.includes("__commonJS")) return null; + // ES-module bundles have no require: their chunks keep the existing + // treatment (the Bun factory recognizer, or the ESM interop refusal). + if (!/\.cjs$/.test(filePath) && ts.isExternalModule(sf)) return null; + + const top = new Map(); + const duplicate = new Set(); + for (const stmt of sf.statements.nodes) { + const add = (name: string, node: TopLevel["node"]): void => { + if (top.has(name)) duplicate.add(name); + else top.set(name, { stmt, node }); + }; + if (ts.isFunctionDeclaration(stmt) && stmt.name !== undefined) add(stmt.name.text, stmt); + else if (ts.isVariableStatement(stmt)) { + for (const d of stmt.declarationList.declarations.nodes) { + if (ts.isIdentifier(d.name)) add(d.name.text, d); + } + } + } + const helperNames = [...top.keys()].filter((n) => helperBase(n) === "__commonJS"); + if (helperNames.length === 0) return null; + + // Every name a recognizer relies on is written nowhere and declared + // once at the top level; `Object` itself is never rebound. + const allRefs = references(sf); + const written = new Set(); + for (const r of allRefs) if (isWritten(r)) written.add(r.text); + const declaredAnywhere = declaredNames(sf); + if (declaredAnywhere.has("Object") || written.has("Object")) return { degrade: CHUNK_SHAPE_DEGRADE }; + const stable = (name: string): boolean => !duplicate.has(name) && !written.has(name); + const ownNames = (name: string): boolean => { + const t = top.get(name); + if (t === undefined || !stable(name) || !ts.isVariableDeclaration(t.node)) return false; + const init = t.node.initializer; + return ( + init !== undefined && + helperTokens(init.getText(sf)) === helperTokens("Object.getOwnPropertyNames") + ); + }; + const forms = new Map(); + for (const name of helperNames) { + const t = top.get(name)!; + const init = ts.isVariableDeclaration(t.node) ? t.node.initializer : undefined; + const form = init !== undefined && stable(name) ? helperForm(init, sf, ownNames) : null; + if (form === null) return { degrade: CHUNK_SHAPE_DEGRADE }; + forms.set(name, form); + } + + // The chunk declarations: `var NAME = HELPER(factory);`, one declarator. + interface Chunk { + name: string; + stmt: ts.VariableStatement; + params: string[]; + body: ts.Block; + path: string; + spec: string; + } + const chunks = new Map(); + const base = filePath.replace(/^.*[\\/]/, ""); + for (const [name, t] of top) { + if (!ts.isVariableDeclaration(t.node) || t.node.initializer === undefined) continue; + const call = unparen(t.node.initializer); + if (!ts.isCallExpression(call) || !ts.isIdentifier(call.expression)) continue; + const form = forms.get(call.expression.text); + if (form === undefined) continue; + const stmt = t.stmt; + if ( + !ts.isVariableStatement(stmt) || + stmt.declarationList.declarations.nodes.length !== 1 || + call.arguments.nodes.length !== 1 || + !stable(name) + ) + return { degrade: CHUNK_SHAPE_DEGRADE }; + let factory: ts.Node = call.arguments.nodes[0]!; + if (form === "keyed") { + if (!ts.isObjectLiteralExpression(factory) || factory.properties.nodes.length !== 1) + return { degrade: CHUNK_SHAPE_DEGRADE }; + factory = factory.properties.nodes[0]!; + if (!ts.isMethodDeclaration(factory) || ts.isComputedPropertyName(factory.name)) + return { degrade: CHUNK_SHAPE_DEGRADE }; + } else { + factory = unparen(factory as ts.Expression); + if (!ts.isArrowFunction(factory) && !ts.isFunctionExpression(factory)) + return { degrade: CHUNK_SHAPE_DEGRADE }; + } + const fn = factory as ts.MethodDeclaration | ts.ArrowFunction | ts.FunctionExpression; + if ( + fn.body === undefined || + !ts.isBlock(fn.body) || + fn.asteriskToken !== undefined || + (fn.modifiers?.nodes.length ?? 0) > 0 || + fn.parameters.nodes.length > 2 + ) + return { degrade: CHUNK_SHAPE_DEGRADE }; + const params: string[] = []; + for (const p of fn.parameters.nodes) { + if (!ts.isIdentifier(p.name) || p.initializer !== undefined || p.dotDotDotToken) + return { degrade: CHUNK_SHAPE_DEGRADE }; + params.push(p.name.text); + } + const file = `${base}.${name}.cjs`; + chunks.set(name, { + name, + stmt, + params, + body: fn.body, + path: filePath.slice(0, filePath.length - base.length) + file, + spec: `./${file}`, + }); + } + if (chunks.size === 0) return null; + + // Every factory reference is a direct zero-argument call. Calls inside a + // chunk body belong to that chunk's text, the rest to the bundle's. + const callEdits: Edit[] = []; + for (const r of allRefs) { + const chunk = chunks.get(r.text); + if (chunk === undefined) continue; + const call = r.parent; + if ( + call === undefined || + !ts.isCallExpression(call) || + call.expression !== r || + call.arguments.nodes.length !== 0 || + call.questionDotToken !== undefined + ) + return { + degrade: `its bundled CommonJS module factory '${r.text}' is used other than as a direct call`, + }; + callEdits.push({ + start: call.getStart(sf), + end: call.getEnd(), + text: `require(${JSON.stringify(chunk.spec)})`, + }); + } + for (const name of chunks.keys()) { + if (declaredAnywhere.has(name) && countDeclarations(sf, name) > 1) + return { degrade: CHUNK_SHAPE_DEGRADE }; + } + + const bundleStrict = strictPrologue(sf.statements.nodes); + const out: BundledChunkSplit["chunks"] = []; + const bundleEdits: Edit[] = [...callEdits]; + for (const chunk of chunks.values()) { + const result = chunkSource(sf, chunk, chunks, top, stable, callEdits, bundleStrict); + if (typeof result !== "string") return result; + out.push({ path: chunk.path, source: result }); + bundleEdits.push({ start: chunk.stmt.getStart(sf), end: chunk.stmt.getEnd(), text: "" }); + } + return { text: applyEdits(source, 0, source.length, bundleEdits), chunks: out }; +} + +function countDeclarations(sf: ts.SourceFile, name: string): number { + let n = 0; + ts.walkPreorder(sf, (node) => { + if ( + (ts.isVariableDeclaration(node) || + ts.isParameter(node) || + ts.isBindingElement(node) || + ts.isFunctionDeclaration(node) || + ts.isFunctionExpression(node) || + ts.isClassDeclaration(node) || + ts.isClassExpression(node)) && + node.name !== undefined && + ts.isIdentifier(node.name) && + node.name.text === name + ) + n++; + return undefined; + }); + return n; +} + +/** Applies non-overlapping edits inside [from, to): replaced spans keep + * their newlines (blanked text stays line-aligned); edits nested in a + * blanked span vanish with it. */ +function applyEdits(source: string, from: number, to: number, edits: Edit[]): string { + const inside = edits + .filter((e) => e.start >= from && e.end <= to) + .sort((a, b) => a.start - b.start || b.end - a.end); + let text = ""; + let at = from; + for (const e of inside) { + if (e.start < at) continue; // nested in an earlier edit + text += source.slice(at, e.start); + const replaced = source.slice(e.start, e.end); + // A respelled span keeps its line breaks after the replacement. + text += + e.text === "" + ? replaced.replace(/[^\r\n]/g, " ") + : e.text + replaced.replace(/[^\n]/g, ""); + at = e.end; + } + return text + source.slice(at, to); +} + +/** The virtual module text for one chunk, or a degrade reason. */ +function chunkSource( + sf: ts.SourceFile, + chunk: { + name: string; + params: string[]; + body: ts.Block; + }, + chunks: ReadonlyMap, + top: ReadonlyMap, + stable: (name: string) => boolean, + callEdits: readonly Edit[], + bundleStrict: boolean, +): string | { degrade: string } { + const why = (reason: string): { degrade: string } => ({ + degrade: `its bundled CommonJS module '${chunk.name}' ${reason}`, + }); + const [exportsParam, moduleParam] = chunk.params; + const body = chunk.body; + const local = declaredNames(body); + for (const p of chunk.params) { + if (local.has(p) || (p !== "exports" && local.has("exports")) || (p !== "module" && local.has("module"))) + return why( + "rebinds its module parameters inside its body (an identifier-minified bundle the recognizer cannot respell)", + ); + } + + /* The factory's own frame must stay unobservable: its receiver, its + * arguments object, and a top-level return have no module equivalent. */ + let frameUse: string | null = null; + /** The innermost frame boundary between a node and the chunk body. */ + const frameOf = (n: ts.Node): "factory" | "arrow" | "function" => { + let arrow = false; + for (let p = n.parent; p !== undefined && p !== body; p = p.parent) { + if ( + ts.isFunctionDeclaration(p) || + ts.isFunctionExpression(p) || + ts.isMethodDeclaration(p) || + ts.isGetAccessorDeclaration(p) || + ts.isSetAccessorDeclaration(p) || + ts.isConstructorDeclaration(p) || + ts.isClassDeclaration(p) || + ts.isClassExpression(p) + ) + return "function"; + if (ts.isArrowFunction(p)) arrow = true; + } + return arrow ? "arrow" : "factory"; + }; + ts.walkPreorder(body, (n) => { + if (frameUse !== null) return "stop"; + if (n.kind >= ts.SyntaxKind.FirstJSDocNode && n.kind <= ts.SyntaxKind.LastJSDocNode) + return "skip"; + let use: string | null = null; + if (n.kind === ts.SyntaxKind.ThisKeyword) use = "reads its factory's receiver (`this`)"; + else if (n.kind === ts.SyntaxKind.SuperKeyword) use = "reads `super` at its top level"; + else if (ts.isIdentifier(n) && n.text === "arguments" && isReference(n)) + use = "reads its factory's arguments object"; + else if (ts.isMetaProperty(n)) use = "reads a meta property at its top level"; + else if (ts.isReturnStatement(n)) { + if (frameOf(n) === "factory") frameUse = "returns from its top level"; + return undefined; + } + if (use !== null && frameOf(n) !== "function") frameUse = use; + return undefined; + }); + if (frameUse !== null) return why(frameUse); + + const edits: Edit[] = callEdits.filter((e) => e.start >= body.pos && e.end <= body.end); + const copied = new Set(); + const copyQueue: string[] = []; + for (const r of references(body)) { + const name = r.text; + if (chunks.has(name)) continue; // a call edit already respells it + if (name === exportsParam || name === moduleParam) { + if (name === moduleParam) { + const p = r.parent; + if (!ts.isPropertyAccessExpression(p) || p.expression !== r || p.name.text !== "exports") + return why("uses its module object beyond module.exports"); + } + const global = name === exportsParam ? "exports" : "module"; + if (name !== global) { + const p = r.parent; + edits.push( + ts.isShorthandPropertyAssignment(p) + ? { start: r.getStart(sf), end: r.getEnd(), text: `${name}: ${global}` } + : { start: r.getStart(sf), end: r.getEnd(), text: global }, + ); + } + continue; + } + if (local.has(name)) continue; + if (name === "exports" || name === "module") + return why(`reads the bundle's own '${name}' binding`); + if (name === "__filename") return why("reads __filename, which names the bundle file"); + if (top.has(name)) { + if (!copied.has(name)) { + copied.add(name); + copyQueue.push(name); + } + } + } + + // The pure helpers BODY reads, closed over their own top-level reads. + const header: string[] = []; + for (let i = 0; i < copyQueue.length; i++) { + const name = copyQueue[i]!; + const t = top.get(name)!; + const node = t.node; + const pure = + stable(name) && + (ts.isFunctionDeclaration(node) || + (ts.isVariableDeclaration(node) && copyableInitializer(node.initializer))); + if (!pure || chunks.has(name)) + return why(`reads the bundle's top-level binding '${name}', which it cannot carry along`); + const own = declaredNames(node); + for (const r of references(node)) { + if (own.has(r.text) || !top.has(r.text) || copied.has(r.text)) continue; + copied.add(r.text); + copyQueue.push(r.text); + } + } + // Declaration order of the bundle: a helper reads only earlier ones. + const ordered = [...copied].sort( + (a, b) => top.get(a)!.node.getStart(sf) - top.get(b)!.node.getStart(sf), + ); + for (const name of ordered) { + const node = top.get(name)!.node; + header.push(ts.isFunctionDeclaration(node) ? node.getText(sf) : `var ${node.getText(sf)};`); + } + + const bodyStrict = strictPrologue(body.statements.nodes); + const text = applyEdits(sf.text, body.getStart(sf) + 1, body.getEnd() - 1, edits); + const prologue = bundleStrict || bodyStrict ? '"use strict";\n' : ""; + return prologue + (header.length > 0 ? header.join("\n") + "\n" : "") + text + "\n"; +} diff --git a/packages/compiler/src/frontend/npm-static-rewrite-host.ts b/packages/compiler/src/frontend/npm-static-rewrite-host.ts index ee36f58d1..5d9675b4a 100644 --- a/packages/compiler/src/frontend/npm-static-rewrite-host.ts +++ b/packages/compiler/src/frontend/npm-static-rewrite-host.ts @@ -134,7 +134,7 @@ function requireTargetEsModuleStamped( } catch { /* unlexable — keep probing */ } - if (src.includes("__toCommonJS(")) return true; + if (/__toCommonJS\d*\(/.test(src)) return true; const fwd = /module\.exports\s*=\s*require\(\s*["']([^"']+)["']\s*\)/.exec(src); if (fwd !== null) return requireTargetEsModuleStamped(parser, file, fwd[1]!, depth + 1); return false; diff --git a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts index 7938471f3..c7e788c1b 100644 --- a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts +++ b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts @@ -24,16 +24,31 @@ export function isBundlerCjsCandidate(source: string): boolean { ); } +/** The helper a bundler-emitted name spells. esbuild suffixes a helper + * that a bundled module declares again with a counter (a bundled esbuild + * dist inside another bundle carries `__export2`, `__toCommonJS2`, …); the + * recognizers below verify each call's shape, so the counter carries no + * meaning beyond the name collision it avoided. */ +export function bundlerHelperBase(name: string): string { + return /^__[A-Za-z]+\d+$/.test(name) ? name.replace(/\d+$/, "") : name; +} + /** The last NAME of a callee: bare identifier or any member chain's final - * name (`tslib_1.__exportStar` → "__exportStar"). */ + * name (`tslib_1.__exportStar` → "__exportStar"), helper counters dropped. */ function calleeNameOf(call: ts.CallExpression): string | null { const callee = call.expression; - if (ts.isIdentifier(callee)) return callee.text; + if (ts.isIdentifier(callee)) return bundlerHelperBase(callee.text); if (ts.isPropertyAccessExpression(callee) && ts.isIdentifier(callee.name)) - return callee.name.text; + return bundlerHelperBase(callee.name.text); return null; } +/** True for an identifier spelling the named helper (counter-suffixed + * spellings included). */ +function isHelperIdent(node: ts.Node, base: string): node is ts.Identifier { + return ts.isIdentifier(node) && bundlerHelperBase(node.text) === base; +} + /** An entry the canonical table can carry. */ interface EntrySpec { /** The export name (may need string-key spelling). */ @@ -194,13 +209,8 @@ interface ToEsmPlan { * __copyProps(…, mod) call copying onto a conditional that tests * isNodeMode/__esModule and defines "default" with value mod. Comments and * formatting are free; the SHAPE is the contract. */ -function recognizedToEsmDecl(stmt: ts.Statement): boolean { - if (!ts.isVariableStatement(stmt)) return false; - const decls = stmt.declarationList.declarations.nodes; - if (decls.length !== 1) return false; - const d = decls[0]!; - if (!ts.isIdentifier(d.name) || d.name.text !== "__toESM" || d.initializer === undefined) - return false; +function recognizedToEsmDecl(d: ts.VariableDeclaration): boolean { + if (!isHelperIdent(d.name, "__toESM") || d.initializer === undefined) return false; let init: ts.Expression = d.initializer; while (ts.isParenthesizedExpression(init)) init = init.expression; if (!ts.isArrowFunction(init)) return false; @@ -218,12 +228,7 @@ function recognizedToEsmDecl(stmt: ts.Statement): boolean { expr = expr.right; } while (ts.isParenthesizedExpression(expr)) expr = expr.expression; - if ( - !ts.isCallExpression(expr) || - !ts.isIdentifier(expr.expression) || - expr.expression.text !== "__copyProps" - ) - return false; + if (!ts.isCallExpression(expr) || !isHelperIdent(expr.expression, "__copyProps")) return false; if (expr.arguments.nodes.length < 2) return false; const from = expr.arguments.nodes[1]!; if (!ts.isIdentifier(from) || from.text !== mod) return false; @@ -234,12 +239,7 @@ function recognizedToEsmDecl(stmt: ts.Statement): boolean { if (!condText.includes(isNodeMode) || !condText.includes("__esModule")) return false; let stamp: ts.Expression = to.whenTrue; while (ts.isParenthesizedExpression(stamp)) stamp = stamp.expression; - if ( - !ts.isCallExpression(stamp) || - !ts.isIdentifier(stamp.expression) || - stamp.expression.text !== "__defProp" - ) - return false; + if (!ts.isCallExpression(stamp) || !isHelperIdent(stamp.expression, "__defProp")) return false; if (stamp.arguments.nodes.length !== 3) return false; const nameArg = stamp.arguments.nodes[1]!; if (!ts.isStringLiteral(nameArg) || nameArg.text !== "default") return false; @@ -278,19 +278,23 @@ function planToEsmInterop(sf: ts.SourceFile, filePath: string, host: CjsRewriteH degrade: reason, }); - const declStmts = sf.statements.nodes.filter( - (s) => - ts.isVariableStatement(s) && - s.declarationList.declarations.nodes.some( - (d) => ts.isIdentifier(d.name) && d.name.text === "__toESM", - ), - ); + // Top-level declarators of the helper, any statement shape (minified + // dists share one `var` among several helpers), by spelling. + const decls = new Map(); + for (const s of sf.statements.nodes) { + if (!ts.isVariableStatement(s)) continue; + for (const d of s.declarationList.declarations.nodes) { + if (!isHelperIdent(d.name, "__toESM")) continue; + const list = decls.get(d.name.text); + if (list === undefined) decls.set(d.name.text, [d]); + else list.push(d); + } + } const refs: ts.Identifier[] = []; const calls: ts.CallExpression[] = []; const collect = (n: ts.Node): void => { - if (ts.isIdentifier(n) && n.text === "__toESM") refs.push(n); - if (ts.isCallExpression(n) && ts.isIdentifier(n.expression) && n.expression.text === "__toESM") - calls.push(n); + if (isHelperIdent(n, "__toESM")) refs.push(n); + if (ts.isCallExpression(n) && isHelperIdent(n.expression, "__toESM")) calls.push(n); }; ts.walkPreorder(sf, collect); if (calls.length === 0 && refs.length === 0) return plan; @@ -298,15 +302,16 @@ function planToEsmInterop(sf: ts.SourceFile, filePath: string, host: CjsRewriteH // a helper that escapes as a VALUE is outside the recognized shape. const callees = new Set(calls.map((c) => c.expression)); const declNames = new Set(); - for (const s of declStmts) { - for (const d of (s as ts.VariableStatement).declarationList.declarations.nodes) { - if (ts.isIdentifier(d.name) && d.name.text === "__toESM") declNames.add(d.name); - } - } + for (const list of decls.values()) for (const d of list) declNames.add(d.name); if (refs.some((r) => !callees.has(r) && !declNames.has(r))) return fail(TO_ESM_ESCAPE_DEGRADE); if (calls.length === 0) return plan; // declared but never called — dead helper - if (declStmts.length !== 1 || !recognizedToEsmDecl(declStmts[0]!)) - return fail(TO_ESM_SHAPE_DEGRADE); + // Every spelling a call uses must be declared exactly once, in the + // recognized shape. + for (const call of calls) { + const list = decls.get((call.expression as ts.Identifier).text); + if (list === undefined || list.length !== 1 || !recognizedToEsmDecl(list[0]!)) + return fail(TO_ESM_SHAPE_DEGRADE); + } /** Module-scope interop bindings: name → whether `.default` IS the * module (pad the access) rather than a member read of its `default`. */ @@ -467,7 +472,10 @@ export function rewriteBundlerCjsSyntax( // ESM bundles can contain closed CommonJS function factories. Recognize // those before refusing interop that needs a dynamic module namespace. - if (ts.isExternalModule(sf)) { + // TypeScript forces every `.cjs` file to be an external module + // (isFileForcedToBeModuleByFormat), yet Node always loads one as + // CommonJS: the extension, not that flag, decides the format there. + if (!/\.cjs$/.test(filePath) && ts.isExternalModule(sf)) { const bundled = rewriteBundledFunctionImports(source, sf); if (bundled !== null) return bundled; return source.includes("__toESM(") @@ -814,21 +822,35 @@ export function rewriteBundlerCjsSyntax( "__reExport", "__toESM", "__toCommonJS", + "__commonJS", "__exportStar", "__createBinding", "__setModuleDefault", "__importStar", "__importDefault", ]); + /* Helper declarators by name. A minified dist shares one `var` among + * several helpers (`var __getProtoOf=…,__hasOwnProp=…;`): each + * declarator is its own candidate, and the statement pads whole only + * once every declarator in it is dead. */ const helperDecls = new Map(); + const helperStmts: ts.VariableStatement[] = []; for (const stmt of sf.statements.nodes) { if (!ts.isVariableStatement(stmt)) continue; const decls = stmt.declarationList.declarations.nodes; - if (decls.length !== 1) continue; - const d = decls[0]!; - if (ts.isIdentifier(d.name) && HELPER_NAMES.has(d.name.text)) { - helperDecls.set(d.name.text, { start: stmt.getStart(sf), end: stmt.getEnd() }); + let anyHelper = false; + for (const d of decls) { + if (!ts.isIdentifier(d.name) || !HELPER_NAMES.has(bundlerHelperBase(d.name.text))) continue; + if (helperDecls.has(d.name.text)) continue; // redeclared: keep the first, never sweep twice + anyHelper = true; + helperDecls.set( + d.name.text, + decls.length === 1 + ? { start: stmt.getStart(sf), end: stmt.getEnd() } + : { start: d.getStart(sf), end: d.getEnd() }, + ); } + if (anyHelper && decls.length > 1) helperStmts.push(stmt); } // every identifier occurrence of a helper name outside its own // declarator name node, position-recorded for the span test @@ -862,8 +884,39 @@ export function rewriteBundlerCjsSyntax( } } } + // A swept declarator of a shared statement pads with its separating + // comma, and the statement pads whole once every declarator is dead: + // padding the bare declarator span would leave a dangling comma. + const declaratorSpans = new Set<{ start: number; end: number }>(); + const declaratorPads: { start: number; end: number }[] = []; + for (const stmt of helperStmts) { + const decls = stmt.declarationList.declarations.nodes; + const deadAt = decls.map((d) => { + if (!ts.isIdentifier(d.name) || !removed.has(d.name.text)) return false; + const span = helperDecls.get(d.name.text)!; + if (span.start !== d.getStart(sf)) return false; + declaratorSpans.add(span); + return true; + }); + if (deadAt.every((x) => x)) { + declaratorPads.push({ start: stmt.getStart(sf), end: stmt.getEnd() }); + continue; + } + // Dead declarators before the last live one take their trailing + // comma; the dead tail after it pads from that declarator's end. + const lastLive = deadAt.lastIndexOf(false); + for (let i = 0; i < decls.length; i++) { + if (!deadAt[i]) continue; + const d = decls[i]!; + declaratorPads.push( + i < lastLive + ? { start: d.getStart(sf), end: decls[i + 1]!.getStart(sf) } + : { start: decls[lastLive]!.getEnd(), end: d.getEnd() }, + ); + } + } neutralize.length = 0; - neutralize.push(...dead); + neutralize.push(...dead.filter((span) => !declaratorSpans.has(span)), ...declaratorPads); } /* ── emission ─────────────────────────────────────────────────────── */ diff --git a/packages/compiler/src/frontend/npm-static.ts b/packages/compiler/src/frontend/npm-static.ts index 3c8fa22e9..e3ae7b600 100644 --- a/packages/compiler/src/frontend/npm-static.ts +++ b/packages/compiler/src/frontend/npm-static.ts @@ -13,7 +13,13 @@ import { registerWorkspacePackage, workspacePackageOfPath, } from "./workspace-registry.js"; -import { trackedExists, trackedReadFile, trackedRealpath } from "./input-tracker.js"; +import { + clearVirtualSources, + setVirtualSource, + trackedExists, + trackedReadFile, + trackedRealpath, +} from "./input-tracker.js"; let activePackages: ReadonlySet = new Set(); let declarationOverloads: ReadonlyMap = new Map(); @@ -34,6 +40,7 @@ export function setNpmStaticPackages>(packages: T): v declarationProperties = new Map(); offenders.clear(); rewriteCache.clear(); + clearVirtualSources(); untypedPkgCache.clear(); realpathProbed.clear(); } @@ -362,7 +369,18 @@ export function npmStaticFsShadow(services: FrontendServices): NpmStaticFsShadow if (hit !== undefined) return hit ?? undefined; let rewritten: string | null = null; try { - const original = trackedReadFile(path); + let original = trackedReadFile(path); + // A bundle's CommonJS chunks move to their own modules first, so + // every later pass (and this shadow, for each chunk) sees plain + // CommonJS module text. + const split = original === null ? null : services.splitCjsChunks(original, path); + if (split !== null && "degrade" in split) { + reportNpmStaticOffender(target.pkg, split.degrade); + original = null; + } else if (split !== null) { + for (const chunk of split.chunks) setVirtualSource(chunk.path, chunk.source); + original = split.text; + } if (original !== null) { const namepaths = services.jsDocNamepaths(path, original); const source = namepaths ?? original; @@ -395,7 +413,8 @@ export function npmStaticFsShadow(services: FrontendServices): NpmStaticFsShadow propertyProjected?.text ?? findWidened?.text ?? classFields?.text ?? - namepaths; + namepaths ?? + (split !== null ? original : null); } } } catch { diff --git a/packages/compiler/src/frontend/services.ts b/packages/compiler/src/frontend/services.ts index ca4dd2b36..7a3b980eb 100644 --- a/packages/compiler/src/frontend/services.ts +++ b/packages/compiler/src/frontend/services.ts @@ -2,6 +2,7 @@ import { Ts7SourceParser, type Ts7SourceKind } from "./ts7/source-parser.js"; import { Ts7Host, type Ts7ApiFactory, type Ts7HostOptions } from "./ts7/program-host.js"; import { rewriteBundlerCjsWithParser } from "./npm-static-rewrite-host.js"; import { isBundlerCjsCandidate } from "./npm-static-rewrite-syntax.js"; +import { splitBundledChunks, type BundledChunkSplit } from "./npm-static-bundled-chunks.js"; import * as syntax from "./npm-static-declaration-syntax.js"; import type { SourceFile } from "./ts7/ast-types.js"; @@ -60,6 +61,13 @@ export class FrontendServices { if (!isBundlerCjsCandidate(source)) return null; return rewriteBundlerCjsWithParser(this.sourceParser(), source, path); } + /** A CommonJS bundle's __commonJS chunks as their own modules (null: no + * chunks; a degrade reason: chunks the recognizer cannot move). */ + splitCjsChunks(source: string, path: string): BundledChunkSplit | { degrade: string } | null { + this.ensureOpen(); + if (!source.includes("__commonJS")) return null; + return splitBundledChunks(this.parse(path, source, "js"), path); + } nullableClassFields(path: string, source: string): syntax.NpmStaticOverloadRewrite | null { return syntax.applyNpmStaticNullableClassFields(this.parse(path, source, "js"), source); } From 6b1f1c59910b88c7ccfea9fd72629ddf63ecdb0b Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 12:07:43 -0700 Subject: [PATCH 16/83] Admit require cycles through esbuild's live export tables The interop rewrite certifies a file whose esbuild export object is installed before anything can run another module's code; the CommonJS cycle rule then treats its tail table as settled at that point. A top-level plain read of a cycle member's function-declaration export is exact as well: Node and the compiled read see the same function. Co-Authored-By: Claude Opus 5.5 --- .../src/frontend/npm-static-rewrite-host.ts | 2 + .../src/frontend/npm-static-rewrite-syntax.ts | 68 ++++++++++++++++ packages/compiler/src/frontend/npm-static.ts | 11 +++ packages/compiler/src/frontend/program.ts | 78 ++++++++++++++++++- packages/compiler/src/frontend/services.ts | 8 +- 5 files changed, 164 insertions(+), 3 deletions(-) diff --git a/packages/compiler/src/frontend/npm-static-rewrite-host.ts b/packages/compiler/src/frontend/npm-static-rewrite-host.ts index 5d9675b4a..d3b2b9ad4 100644 --- a/packages/compiler/src/frontend/npm-static-rewrite-host.ts +++ b/packages/compiler/src/frontend/npm-static-rewrite-host.ts @@ -163,9 +163,11 @@ export function rewriteBundlerCjsWithParser( parser: Ts7SourceParser, source: string, filePath: string, + liveExportTable?: (filePath: string) => void, ): string | { degrade: string } | null { if (!isBundlerCjsCandidate(source)) return null; return rewriteBundlerCjsSyntax(parser.parse(filePath, source, "js"), filePath, { + liveExportTable, requireTargetEsModuleStamped: (from, spec) => requireTargetEsModuleStamped(parser, from, spec), starTargetNames: (from, spec) => { const target = resolveRelativeCjs(from, spec); diff --git a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts index c7e788c1b..49467782f 100644 --- a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts +++ b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts @@ -12,6 +12,65 @@ import { rewriteBundledFunctionImports } from "./npm-static-bundled-cjs.js"; export interface CjsRewriteHost { requireTargetEsModuleStamped(fromFile: string, specifier: string): boolean; starTargetNames(fromFile: string, specifier: string): Set; + /** Told when the rewritten file's tail export table stands for a live + * getter table the original installed before it could run another + * module's code (liveExportTableFirst): a module of a require cycle that + * captured the export object early observes exactly that table. */ + liveExportTable?: ((filePath: string) => void) | undefined; +} + +/** True when esbuild's export object (`module.exports = __toCommonJS(T)` + * over a getter table `__export(T, {...})`) is installed before anything + * that can run another module's code, and nothing else writes the export + * object. Every statement before the installation is then a declaration, + * a directive, or the getter-table call (whose getters run only when + * read), and variable initializers evaluate no calls outside function + * bodies. The getters read live bindings, so the object a cycle partner + * captures early answers every later read exactly like the final table. */ +function liveExportTableFirst( + sf: ts.SourceFile, + installAt: number, + target: string, + exportTables: readonly { target: string }[], +): boolean { + if (!exportTables.some((t) => t.target === target)) return false; + let callsOutsideFunctions = false; + const noCalls = (root: ts.Node): boolean => { + callsOutsideFunctions = false; + ts.walkPreorder(root, (n) => { + if (callsOutsideFunctions) return "stop"; + if (ts.isArrowFunction(n) || ts.isFunctionExpression(n) || ts.isFunctionDeclaration(n)) + return "skip"; + if (ts.isCallExpression(n) || ts.isNewExpression(n) || ts.isTaggedTemplateExpression(n)) + callsOutsideFunctions = true; + return undefined; + }); + return !callsOutsideFunctions; + }; + let sawTable = false; + for (const stmt of sf.statements.nodes) { + if (stmt.getStart(sf) >= installAt) return sawTable; + if (ts.isFunctionDeclaration(stmt) || ts.isEmptyStatement(stmt)) continue; + if (ts.isExpressionStatement(stmt) && ts.isStringLiteral(stmt.expression)) continue; + if (ts.isVariableStatement(stmt)) { + if ( + stmt.declarationList.declarations.nodes.every( + (d) => ts.isIdentifier(d.name) && (d.initializer === undefined || noCalls(d.initializer)), + ) + ) + continue; + return false; + } + if (ts.isExpressionStatement(stmt) && ts.isCallExpression(stmt.expression)) { + const table = exportCallOf(stmt.expression); + if (table !== null && table.target === target) { + sawTable = true; + continue; + } + } + return false; + } + return false; } export function isBundlerCjsCandidate(source: string): boolean { @@ -928,6 +987,15 @@ export function rewriteBundlerCjsSyntax( if (!emitTable) return text; // interop pads only — nothing appends + if ( + esbuildMainTarget !== null && + tablePos !== null && + memberStmts.length === 0 && + definePropEntries.length === 0 && + liveExportTableFirst(sf, tablePos, esbuildMainTarget, exportTables) + ) + host.liveExportTable?.(filePath); + const lines: string[] = [""]; const hasAnyFallback = [...entries.values()].some((s) => s.kind === "any"); if (hasAnyFallback) { diff --git a/packages/compiler/src/frontend/npm-static.ts b/packages/compiler/src/frontend/npm-static.ts index e3ae7b600..f377628d6 100644 --- a/packages/compiler/src/frontend/npm-static.ts +++ b/packages/compiler/src/frontend/npm-static.ts @@ -34,12 +34,22 @@ const offenders = new Map(); * the host probes the same file many times, and the rewrite parses. */ const rewriteCache = new Map(); +/** Rewritten files whose tail export table stands for a live getter table + * installed before the file could run another module's code (the rewrite + * certifies it; require-cycle admission consults it). */ +const liveExportFiles = new Set(); + +export function npmStaticLiveExportTable(path: string): boolean { + return liveExportFiles.has(path.split("\\").join("/")); +} + export function setNpmStaticPackages>(packages: T): void { activePackages = new Set(packages); declarationOverloads = new Map(); declarationProperties = new Map(); offenders.clear(); rewriteCache.clear(); + liveExportFiles.clear(); clearVirtualSources(); untypedPkgCache.clear(); realpathProbed.clear(); @@ -403,6 +413,7 @@ export function npmStaticFsShadow(services: FrontendServices): NpmStaticFsShadow classFields?.text ?? source, path, + (live) => liveExportFiles.add(live.split("\\").join("/")), ); if (answer !== null && typeof answer === "object") { reportNpmStaticOffender(target.pkg, answer.degrade); diff --git a/packages/compiler/src/frontend/program.ts b/packages/compiler/src/frontend/program.ts index bde168c38..d06e966f3 100644 --- a/packages/compiler/src/frontend/program.ts +++ b/packages/compiler/src/frontend/program.ts @@ -68,6 +68,7 @@ import { isNpmStaticPackage, npmStaticActive, npmStaticFsShadow, + npmStaticLiveExportTable, npmStaticPackageOfPath, reportNpmStaticOffender, setNpmStaticDeclarationOverloads, @@ -1438,6 +1439,8 @@ export function requireBindingEarlyUse7( k: number, decl: ts.VariableDeclaration, precedingDecls: readonly ts.VariableDeclaration[], + /** A reference of the binding whose early evaluation is known exact. */ + exactEarlyRead?: (reference: ts.Identifier) => boolean, ): ts.Identifier | null { const checker = program.getTypeChecker(); const bindings = new Set(); @@ -1669,6 +1672,7 @@ export function requireBindingEarlyUse7( const sym = symbolOf(n); if (sym === undefined) return undefined; if (bindings.has(sym)) { + if (exactEarlyRead?.(n) === true) return undefined; hit = n; return "stop"; } @@ -3121,6 +3125,16 @@ function cjsCycleRefusal7( // anything else can change what a module of the cycle observes. let stray: ts.Node | null = null; const settled = new Set(stmts.slice(0, firstEntry).filter(exportStatement)); + // A bundler export table the rewrite certified (npmStaticLiveExportTable) + // stands for the live getter object the original installed before its + // first require: the tail table settles as if it ran there. + const tail = stmts[stmts.length - 1]; + if ( + tail !== undefined && + npmStaticLiveExportTable(m.fileName) && + cjsExportAssignmentOf(tail)?.kind === "table" + ) + settled.add(tail); ts.walkPreorder(m, (n) => { if (stray !== null) return "stop"; if (ts.isStatement(n) && settled.has(n)) return "skip"; @@ -3173,7 +3187,9 @@ function cjsCycleRefusal7( if (req.decl === null) continue; const dep = resolveImport7(program, m, req.spec, "require"); if (dep === null || !members.has(dep)) continue; - const reached = requireBindingEarlyUse7(program, m, stmts.length, req.decl, []); + const reached = requireBindingEarlyUse7(program, m, stmts.length, req.decl, [], (id) => + functionExportRead7(program, id, members), + ); if (reached !== null) { return `the top-level code of ${m.fileName} can use its binding '${reached.text}' of a module of the cycle (${lineOf(reached)}) while the cycle initializes`; } @@ -3183,6 +3199,66 @@ function cjsCycleRefusal7( return null; } +/** True for a plain read `binding.name` (not a call, not a write) whose + * export resolves to a top-level function declaration of a module of the + * cycle that nothing reassigns. Every member settles its exports before + * entering the cycle, so Node reads that same function whenever the read + * runs, and the compiled read resolves to the declaration itself. */ +function functionExportRead7( + program: ts.Program, + id: ts.Identifier, + members: ReadonlySet, +): boolean { + const access = id.parent; + if (!ts.isPropertyAccessExpression(access) || access.expression !== id) return false; + let use: ts.Node = access; + while (ts.isParenthesizedExpression(use.parent)) use = use.parent; + const parent = use.parent; + if ( + (ts.isCallExpression(parent) && parent.expression === use) || + ts.isDeleteExpression(parent) || + ((ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent)) && + (parent.operator === ts.SyntaxKind.PlusPlusToken || + parent.operator === ts.SyntaxKind.MinusMinusToken)) || + (ts.isBinaryExpression(parent) && + parent.left === use && + parent.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && + parent.operatorToken.kind <= ts.SyntaxKind.LastAssignment) + ) + return false; + const checker = program.getTypeChecker(); + const resolved = (sym: ts.Symbol | undefined, depth: number): ts.FunctionDeclaration | null => { + if (sym === undefined || depth > 4) return null; + if ((sym.flags & ts.SymbolFlags.Alias) !== 0) sym = checker.getAliasedSymbol(sym); + for (const d of checker.declarationsOf(sym)) { + if (ts.isFunctionDeclaration(d)) return d; + if (ts.isPropertyAssignment(d) && ts.isIdentifier(d.initializer)) + return resolved(checker.getSymbolAtLocation(d.initializer), depth + 1); + if (ts.isShorthandPropertyAssignment(d)) + return resolved(checker.getShorthandAssignmentValueSymbol(d), depth + 1); + } + return null; + }; + const fn = resolved(checker.getSymbolAtLocation(access.name), 0); + if (fn === null || fn.name === undefined || !members.has(fn.getSourceFile())) return false; + if (!ts.isSourceFile(fn.parent)) return false; + const name = fn.name.text; + let reassigned = false; + ts.walkPreorder(fn.getSourceFile(), (n) => { + if (reassigned) return "stop"; + if ( + ts.isBinaryExpression(n) && + ts.isIdentifier(n.left) && + n.left.text === name && + n.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && + n.operatorToken.kind <= ts.SyntaxKind.LastAssignment + ) + reassigned = true; + return undefined; + }); + return !reassigned; +} + /* ── exact cyclic initialization ───────────────────────────────────────── * Cycles that fail the declaration-only rule still evaluate exactly like * Node when their shape is representable: the guarded %init calls already diff --git a/packages/compiler/src/frontend/services.ts b/packages/compiler/src/frontend/services.ts index 7a3b980eb..40a989a0c 100644 --- a/packages/compiler/src/frontend/services.ts +++ b/packages/compiler/src/frontend/services.ts @@ -56,10 +56,14 @@ export class FrontendServices { this.ensureOpen(); return new Ts7Host(this.createApi, { ...options, cwd: options?.cwd ?? this.cwd }); } - rewriteCjs(source: string, path: string): string | { degrade: string } | null { + rewriteCjs( + source: string, + path: string, + liveExportTable?: (path: string) => void, + ): string | { degrade: string } | null { this.ensureOpen(); if (!isBundlerCjsCandidate(source)) return null; - return rewriteBundlerCjsWithParser(this.sourceParser(), source, path); + return rewriteBundlerCjsWithParser(this.sourceParser(), source, path, liveExportTable); } /** A CommonJS bundle's __commonJS chunks as their own modules (null: no * chunks; a degrade reason: chunks the recognizer cannot move). */ From 17647d8bfd8bd0bc086a1773b0db42428072eadb Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 12:17:13 -0700 Subject: [PATCH 17/83] Add esbuild chunk and CommonJS cycle npm fixtures Real esbuild output (0.25 plain and whitespace/syntax minified, 0.17) of one bundle with lazily and eagerly required chunks, a chunk that is itself an esbuild dist, an external through __toESM, a .cjs bundle in a type-module package, and per-file esbuild CommonJS cycles entered from either side. Split chunks are require-only modules: their whole export table is their surface. Co-Authored-By: Claude Opus 5.5 --- .../compiler/src/frontend/input-tracker.ts | 4 + .../src/frontend/npm-static-rewrite-host.ts | 8 +- .../src/frontend/npm-static-rewrite-syntax.ts | 10 ++ packages/compiler/src/frontend/program.ts | 91 +++++++----- .../npm/cases/2558-bundled-chunks/main.ts | 29 ++++ .../fixtures/npm/cases/2559-cjs-cycle/main.ts | 11 ++ .../2560-cjs-cycle-partner-first/main.ts | 11 ++ .../cases/2561-cjs-cycle-early-read/main.ts | 6 + .../npm/node_modules/gtchunk/dist/index.d.ts | 4 + .../npm/node_modules/gtchunk/dist/index.js | 132 ++++++++++++++++++ .../npm/node_modules/gtchunk/package.json | 9 ++ .../node_modules/gtchunk17/dist/index.d.ts | 4 + .../npm/node_modules/gtchunk17/dist/index.js | 132 ++++++++++++++++++ .../npm/node_modules/gtchunk17/package.json | 9 ++ .../node_modules/gtchunkws/dist/index.d.ts | 4 + .../npm/node_modules/gtchunkws/dist/index.js | 1 + .../npm/node_modules/gtchunkws/package.json | 9 ++ .../npm/node_modules/gtcjsdist/dist/index.cjs | 43 ++++++ .../node_modules/gtcjsdist/dist/index.d.cts | 1 + .../npm/node_modules/gtcjsdist/package.json | 10 ++ .../npm/node_modules/gtcycle/dist/append.d.ts | 1 + .../npm/node_modules/gtcycle/dist/append.js | 30 ++++ .../npm/node_modules/gtcycle/dist/index.d.ts | 4 + .../npm/node_modules/gtcycle/dist/index.js | 44 ++++++ .../npm/node_modules/gtcycle/dist/merge.d.ts | 1 + .../npm/node_modules/gtcycle/dist/merge.js | 30 ++++ .../npm/node_modules/gtcycle/package.json | 6 + .../node_modules/gtcyclebad/dist/index.d.ts | 2 + .../npm/node_modules/gtcyclebad/dist/index.js | 33 +++++ .../node_modules/gtcyclebad/dist/reader.js | 33 +++++ .../npm/node_modules/gtcyclebad/package.json | 6 + 31 files changed, 681 insertions(+), 37 deletions(-) create mode 100644 tests/fixtures/npm/cases/2558-bundled-chunks/main.ts create mode 100644 tests/fixtures/npm/cases/2559-cjs-cycle/main.ts create mode 100644 tests/fixtures/npm/cases/2560-cjs-cycle-partner-first/main.ts create mode 100644 tests/fixtures/npm/cases/2561-cjs-cycle-early-read/main.ts create mode 100644 tests/fixtures/npm/node_modules/gtchunk/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtchunk/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtchunk/package.json create mode 100644 tests/fixtures/npm/node_modules/gtchunk17/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtchunk17/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtchunk17/package.json create mode 100644 tests/fixtures/npm/node_modules/gtchunkws/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtchunkws/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtchunkws/package.json create mode 100644 tests/fixtures/npm/node_modules/gtcjsdist/dist/index.cjs create mode 100644 tests/fixtures/npm/node_modules/gtcjsdist/dist/index.d.cts create mode 100644 tests/fixtures/npm/node_modules/gtcjsdist/package.json create mode 100644 tests/fixtures/npm/node_modules/gtcycle/dist/append.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtcycle/dist/append.js create mode 100644 tests/fixtures/npm/node_modules/gtcycle/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtcycle/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtcycle/dist/merge.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtcycle/dist/merge.js create mode 100644 tests/fixtures/npm/node_modules/gtcycle/package.json create mode 100644 tests/fixtures/npm/node_modules/gtcyclebad/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtcyclebad/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtcyclebad/dist/reader.js create mode 100644 tests/fixtures/npm/node_modules/gtcyclebad/package.json diff --git a/packages/compiler/src/frontend/input-tracker.ts b/packages/compiler/src/frontend/input-tracker.ts index b710988a3..fa2e92568 100644 --- a/packages/compiler/src/frontend/input-tracker.ts +++ b/packages/compiler/src/frontend/input-tracker.ts @@ -174,6 +174,10 @@ export function setVirtualSource(path: string, text: string): void { virtualSources.set(resolve(path), text); } +export function isVirtualSource(path: string): boolean { + return virtualSources.has(resolve(path)); +} + export function clearVirtualSources(): void { virtualSources.clear(); } diff --git a/packages/compiler/src/frontend/npm-static-rewrite-host.ts b/packages/compiler/src/frontend/npm-static-rewrite-host.ts index d3b2b9ad4..524239901 100644 --- a/packages/compiler/src/frontend/npm-static-rewrite-host.ts +++ b/packages/compiler/src/frontend/npm-static-rewrite-host.ts @@ -2,7 +2,12 @@ import { dirname, join, resolve as resolvePath } from "node:path"; import { cjsLexedExportsOfFile, cjsVisibleNames } from "./cjs-syntax.js"; import type { Ts7SourceParser } from "./ts7/source-parser.js"; -import { trackedDirectoryExists, trackedFileExists, trackedReadFile } from "./input-tracker.js"; +import { + isVirtualSource, + trackedDirectoryExists, + trackedFileExists, + trackedReadFile, +} from "./input-tracker.js"; import { resolveExports } from "./resolve.js"; import { packageNameOfSpecifier } from "./workspace-registry.js"; import { isBundlerCjsCandidate, rewriteBundlerCjsSyntax } from "./npm-static-rewrite-syntax.js"; @@ -168,6 +173,7 @@ export function rewriteBundlerCjsWithParser( if (!isBundlerCjsCandidate(source)) return null; return rewriteBundlerCjsSyntax(parser.parse(filePath, source, "js"), filePath, { liveExportTable, + requireOnly: isVirtualSource, requireTargetEsModuleStamped: (from, spec) => requireTargetEsModuleStamped(parser, from, spec), starTargetNames: (from, spec) => { const target = resolveRelativeCjs(from, spec); diff --git a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts index 49467782f..e0534fc2d 100644 --- a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts +++ b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts @@ -17,6 +17,11 @@ export interface CjsRewriteHost { * module's code (liveExportTableFirst): a module of a require cycle that * captured the export object early observes exactly that table. */ liveExportTable?: ((filePath: string) => void) | undefined; + /** True for a module only ever loaded through require() (a bundle's + * CommonJS chunk split into its own module): no ES-module importer + * exists, so Node's lexer-visible name set bounds nothing and every + * name the export plumbing defines is part of its surface. */ + requireOnly?: ((filePath: string) => boolean) | undefined; } /** True when esbuild's export object (`module.exports = __toCommonJS(T)` @@ -711,6 +716,11 @@ export function rewriteBundlerCjsSyntax( } const lexed = cjsLexedExportsOfFile(sf); + if (host.requireOnly?.(filePath) === true) { + for (const t of exportTables) for (const [name] of t.entries) lexed.exports.add(name); + for (const d of definePropEntries) lexed.exports.add(d.name); + for (const m of memberStmts) for (const name of m.names) lexed.exports.add(name); + } let emitTable = sawBundleShape && (lexed.exports.size > 0 || lexed.reexports.length > 0); const starSpecs: string[] = []; diff --git a/packages/compiler/src/frontend/program.ts b/packages/compiler/src/frontend/program.ts index d06e966f3..12cdfd76c 100644 --- a/packages/compiler/src/frontend/program.ts +++ b/packages/compiler/src/frontend/program.ts @@ -3188,7 +3188,7 @@ function cjsCycleRefusal7( const dep = resolveImport7(program, m, req.spec, "require"); if (dep === null || !members.has(dep)) continue; const reached = requireBindingEarlyUse7(program, m, stmts.length, req.decl, [], (id) => - functionExportRead7(program, id, members), + functionExportRead7(id, dep), ); if (reached !== null) { return `the top-level code of ${m.fileName} can use its binding '${reached.text}' of a module of the cycle (${lineOf(reached)}) while the cycle initializes`; @@ -3199,16 +3199,12 @@ function cjsCycleRefusal7( return null; } -/** True for a plain read `binding.name` (not a call, not a write) whose - * export resolves to a top-level function declaration of a module of the - * cycle that nothing reassigns. Every member settles its exports before +/** True for a plain read `binding.name` (not a call, not a write) of the + * cycle member `dep` whose export `name` is a top-level function + * declaration nothing reassigns. Every member settles its exports before * entering the cycle, so Node reads that same function whenever the read * runs, and the compiled read resolves to the declaration itself. */ -function functionExportRead7( - program: ts.Program, - id: ts.Identifier, - members: ReadonlySet, -): boolean { +function functionExportRead7(id: ts.Identifier, dep: ts.SourceFile): boolean { const access = id.parent; if (!ts.isPropertyAccessExpression(access) || access.expression !== id) return false; let use: ts.Node = access; @@ -3226,37 +3222,60 @@ function functionExportRead7( parent.operatorToken.kind <= ts.SyntaxKind.LastAssignment) ) return false; - const checker = program.getTypeChecker(); - const resolved = (sym: ts.Symbol | undefined, depth: number): ts.FunctionDeclaration | null => { - if (sym === undefined || depth > 4) return null; - if ((sym.flags & ts.SymbolFlags.Alias) !== 0) sym = checker.getAliasedSymbol(sym); - for (const d of checker.declarationsOf(sym)) { - if (ts.isFunctionDeclaration(d)) return d; - if (ts.isPropertyAssignment(d) && ts.isIdentifier(d.initializer)) - return resolved(checker.getSymbolAtLocation(d.initializer), depth + 1); - if (ts.isShorthandPropertyAssignment(d)) - return resolved(checker.getShorthandAssignmentValueSymbol(d), depth + 1); + const name = access.name.text; + // The exported value's identifier: a table entry or a member export. + const values: ts.Expression[] = []; + for (const stmt of dep.statements.nodes) { + if (!ts.isExpressionStatement(stmt) || !ts.isBinaryExpression(stmt.expression)) continue; + const e = stmt.expression; + if (e.operatorToken.kind !== ts.SyntaxKind.EqualsToken) continue; + if (isModuleExportsAccess(e.left)) { + if (!ts.isObjectLiteralExpression(e.right)) return false; + for (const p of e.right.properties.nodes) { + if (ts.isSpreadAssignment(p)) return false; + if (ts.isShorthandPropertyAssignment(p) && p.name.text === name) + values.push(p.name as ts.Identifier); + else if ( + ts.isPropertyAssignment(p) && + (ts.isIdentifier(p.name) || ts.isStringLiteral(p.name)) && + p.name.text === name + ) + values.push(p.initializer); + } + } else if ( + ts.isPropertyAccessExpression(e.left) && + e.left.name.text === name && + ((ts.isIdentifier(e.left.expression) && e.left.expression.text === "exports") || + isModuleExportsAccess(e.left.expression)) + ) { + values.push(e.right); } - return null; - }; - const fn = resolved(checker.getSymbolAtLocation(access.name), 0); - if (fn === null || fn.name === undefined || !members.has(fn.getSourceFile())) return false; - if (!ts.isSourceFile(fn.parent)) return false; - const name = fn.name.text; - let reassigned = false; - ts.walkPreorder(fn.getSourceFile(), (n) => { - if (reassigned) return "stop"; - if ( - ts.isBinaryExpression(n) && - ts.isIdentifier(n.left) && - n.left.text === name && - n.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && - n.operatorToken.kind <= ts.SyntaxKind.LastAssignment + } + const value = values.length === 1 ? values[0]! : null; + if (value === null || !ts.isIdentifier(value)) return false; + const fnName = value.text; + if ( + !dep.statements.nodes.some( + (st) => ts.isFunctionDeclaration(st) && st.name?.text === fnName && st.body !== undefined, ) - reassigned = true; + ) + return false; + let rebound = false; + ts.walkPreorder(dep, (n) => { + if (rebound) return "stop"; + if ( + (ts.isBinaryExpression(n) && + ts.isIdentifier(n.left) && + n.left.text === fnName && + n.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && + n.operatorToken.kind <= ts.SyntaxKind.LastAssignment) || + (ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.name.text === fnName) || + (ts.isFunctionDeclaration(n) && n.name?.text === fnName && !ts.isSourceFile(n.parent)) + ) + rebound = true; return undefined; }); - return !reassigned; + return !rebound; } /* ── exact cyclic initialization ───────────────────────────────────────── diff --git a/tests/fixtures/npm/cases/2558-bundled-chunks/main.ts b/tests/fixtures/npm/cases/2558-bundled-chunks/main.ts new file mode 100644 index 000000000..5cbbc3dbc --- /dev/null +++ b/tests/fixtures/npm/cases/2558-bundled-chunks/main.ts @@ -0,0 +1,29 @@ +// esbuild's bundled CommonJS chunks: each package below is real esbuild +// output (0.25 unminified, 0.25 whitespace+syntax minified, 0.17) of one +// source that bundles CommonJS modules through the __commonJS helper. +// Every chunk compiles as its own CommonJS module: it runs once, at its +// first require (the top-level interop imports run while the bundle +// loads; `lazy` requires its chunk only when called), chunk-to-chunk +// requires share one module instance, an esbuild-built dist bundled as a +// chunk keeps its suffixed helpers, and the external dependency passes +// through the __toESM interop. gtcjsdist is a "type": "module" package +// whose CommonJS entry is a .cjs bundle using the node-mode interop. +import { describe, external, lazy, nested } from "gtchunk"; +import { describe as dws, external as ews, lazy as lws, nested as nws } from "gtchunkws"; +import { describe as d17, external as e17, lazy as l17, nested as n17 } from "gtchunk17"; +import { viaNodeMode } from "gtcjsdist"; + +console.log("main start"); +console.log("plain", describe()); +console.log("plain", external()); +console.log("plain", nested()); +console.log("plain", lazy(), lazy()); +console.log("minified", dws()); +console.log("minified", ews()); +console.log("minified", nws()); +console.log("minified", lws(), lws()); +console.log("older", d17()); +console.log("older", e17()); +console.log("older", n17()); +console.log("older", l17(), l17()); +console.log(viaNodeMode()); diff --git a/tests/fixtures/npm/cases/2559-cjs-cycle/main.ts b/tests/fixtures/npm/cases/2559-cjs-cycle/main.ts new file mode 100644 index 000000000..042102b64 --- /dev/null +++ b/tests/fixtures/npm/cases/2559-cjs-cycle/main.ts @@ -0,0 +1,11 @@ +// A require cycle between esbuild's per-file CommonJS outputs (index ↔ +// merge, index ↔ append). Each file installs its live getter table +// before its first require, so a partner that loads while index.js is +// still initializing sees index's partially initialized exports, and both +// partners call back into index from their functions. index.js re-exports +// mergeAll from merge.js through its getter table. +import { run, mergeAll } from "gtcycle"; + +console.log(run([" a ", "", "@x"], ["b", "@keep"])); +console.log(run(["c"], [])); +console.log(mergeAll(["@keep", " d"], ["@drop"]).join("|")); diff --git a/tests/fixtures/npm/cases/2560-cjs-cycle-partner-first/main.ts b/tests/fixtures/npm/cases/2560-cjs-cycle-partner-first/main.ts new file mode 100644 index 000000000..1ed90d432 --- /dev/null +++ b/tests/fixtures/npm/cases/2560-cjs-cycle-partner-first/main.ts @@ -0,0 +1,11 @@ +// The same esbuild CommonJS cycle entered through a partner: merge.js +// loads first, requires index.js, which requires merge.js back while +// merge is still initializing (its getter table is already installed) and +// re-exports mergeAll from it: the re-export reads merge's hoisted +// function while merge is still initializing. +import { mergeAll } from "gtcycle/dist/merge.js"; +import { run, mergeAll as reexported } from "gtcycle"; + +console.log(mergeAll([" x "], ["@keep"]).join(",")); +console.log(reexported === mergeAll); +console.log(run(["y"], ["@z"])); diff --git a/tests/fixtures/npm/cases/2561-cjs-cycle-early-read/main.ts b/tests/fixtures/npm/cases/2561-cjs-cycle-early-read/main.ts new file mode 100644 index 000000000..3efeb9eb1 --- /dev/null +++ b/tests/fixtures/npm/cases/2561-cjs-cycle-early-read/main.ts @@ -0,0 +1,6 @@ +// A require cycle whose partner reads a const export of index.js while +// index is still initializing: Node throws a ReferenceError (the binding +// is in its temporal dead zone), and scriptc refuses the cycle. +import { show } from "gtcyclebad"; + +console.log(show()); diff --git a/tests/fixtures/npm/node_modules/gtchunk/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtchunk/dist/index.d.ts new file mode 100644 index 000000000..bd3d4e582 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunk/dist/index.d.ts @@ -0,0 +1,4 @@ +export declare function describe(): string; +export declare function external(): string; +export declare function nested(): string; +export declare function lazy(): string; diff --git a/tests/fixtures/npm/node_modules/gtchunk/dist/index.js b/tests/fixtures/npm/node_modules/gtchunk/dist/index.js new file mode 100644 index 000000000..7820e3ae4 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunk/dist/index.js @@ -0,0 +1,132 @@ +var __create = Object.create; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __getProtoOf = Object.getPrototypeOf; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __commonJS = (cb, mod) => function __require() { + return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; +}; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( + // If the importer is in node compatibility mode or this is not an ESM + // file that has been converted to a CommonJS file using a Babel- + // compatible transform (i.e. "__esModule" has not been set), then set + // "default" to the CommonJS "module.exports" for node compatibility. + isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, + mod +)); +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); + +// chunk/vendor/settings/counter.js +var require_counter = __commonJS({ + "chunk/vendor/settings/counter.js"(exports2, module2) { + var n = 0; + console.log("counter init"); + module2.exports = { + next() { + n += 1; + return n; + } + }; + } +}); + +// chunk/vendor/settings/index.js +var require_settings = __commonJS({ + "chunk/vendor/settings/index.js"(exports2) { + "use strict"; + console.log("settings init"); + var counter = require_counter(); + exports2.LABEL = "bundled"; + exports2.VERSION = 2; + exports2.bump = function() { + return counter.next(); + }; + } +}); + +// chunk/vendor/prebuilt/index.js +var require_prebuilt = __commonJS({ + "chunk/vendor/prebuilt/index.js"(exports2, module2) { + var __defProp2 = Object.defineProperty; + var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor; + var __getOwnPropNames2 = Object.getOwnPropertyNames; + var __hasOwnProp2 = Object.prototype.hasOwnProperty; + var __export2 = (target, all) => { + for (var name in all) + __defProp2(target, name, { get: all[name], enumerable: true }); + }; + var __copyProps2 = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames2(from)) + if (!__hasOwnProp2.call(to, key) && key !== except) + __defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable }); + } + return to; + }; + var __toCommonJS2 = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod); + var prebuilt_exports = {}; + __export2(prebuilt_exports, { + PREBUILT_TAG: () => PREBUILT_TAG2, + greet: () => greet2 + }); + module2.exports = __toCommonJS2(prebuilt_exports); + function greet2(name) { + return "hello " + name; + } + var PREBUILT_TAG2 = "prebuilt"; + } +}); + +// chunk/vendor/lazy.js +var require_lazy = __commonJS({ + "chunk/vendor/lazy.js"(exports2, module2) { + console.log("lazy init"); + module2.exports = { value: "lazy-value" }; + } +}); + +// chunk/src/index.ts +var index_exports = {}; +__export(index_exports, { + describe: () => describe, + external: () => external, + lazy: () => lazy, + nested: () => nested +}); +module.exports = __toCommonJS(index_exports); +var import_settings = __toESM(require_settings()); +var core = __toESM(require("gtcore")); +var import_gtcore = __toESM(require("gtcore")); +var import_prebuilt = __toESM(require_prebuilt()); +function describe() { + return import_settings.LABEL + "@" + import_settings.default.VERSION + " bump " + (0, import_settings.bump)() + " " + (0, import_settings.bump)(); +} +function external() { + return core.tag + ":" + core.label(1) + ":" + import_gtcore.default.label(2); +} +function nested() { + return (0, import_prebuilt.greet)("chunk") + " " + import_prebuilt.PREBUILT_TAG; +} +function lazy() { + return require_lazy().value; +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + describe, + external, + lazy, + nested +}); diff --git a/tests/fixtures/npm/node_modules/gtchunk/package.json b/tests/fixtures/npm/node_modules/gtchunk/package.json new file mode 100644 index 000000000..579ad6cab --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunk/package.json @@ -0,0 +1,9 @@ +{ + "name": "gtchunk", + "version": "1.0.0", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "dependencies": { + "gtcore": "1.0.0" + } +} diff --git a/tests/fixtures/npm/node_modules/gtchunk17/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtchunk17/dist/index.d.ts new file mode 100644 index 000000000..bd3d4e582 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunk17/dist/index.d.ts @@ -0,0 +1,4 @@ +export declare function describe(): string; +export declare function external(): string; +export declare function nested(): string; +export declare function lazy(): string; diff --git a/tests/fixtures/npm/node_modules/gtchunk17/dist/index.js b/tests/fixtures/npm/node_modules/gtchunk17/dist/index.js new file mode 100644 index 000000000..afda6af56 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunk17/dist/index.js @@ -0,0 +1,132 @@ +var __create = Object.create; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __getProtoOf = Object.getPrototypeOf; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __commonJS = (cb, mod) => function __require() { + return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; +}; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( + // If the importer is in node compatibility mode or this is not an ESM + // file that has been converted to a CommonJS file using a Babel- + // compatible transform (i.e. "__esModule" has not been set), then set + // "default" to the CommonJS "module.exports" for node compatibility. + isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, + mod +)); +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); + +// chunk/vendor/settings/counter.js +var require_counter = __commonJS({ + "chunk/vendor/settings/counter.js"(exports, module2) { + var n = 0; + console.log("counter init"); + module2.exports = { + next() { + n += 1; + return n; + } + }; + } +}); + +// chunk/vendor/settings/index.js +var require_settings = __commonJS({ + "chunk/vendor/settings/index.js"(exports) { + "use strict"; + console.log("settings init"); + var counter = require_counter(); + exports.LABEL = "bundled"; + exports.VERSION = 2; + exports.bump = function() { + return counter.next(); + }; + } +}); + +// chunk/vendor/prebuilt/index.js +var require_prebuilt = __commonJS({ + "chunk/vendor/prebuilt/index.js"(exports, module2) { + var __defProp2 = Object.defineProperty; + var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor; + var __getOwnPropNames2 = Object.getOwnPropertyNames; + var __hasOwnProp2 = Object.prototype.hasOwnProperty; + var __export2 = (target, all) => { + for (var name in all) + __defProp2(target, name, { get: all[name], enumerable: true }); + }; + var __copyProps2 = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames2(from)) + if (!__hasOwnProp2.call(to, key) && key !== except) + __defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable }); + } + return to; + }; + var __toCommonJS2 = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod); + var prebuilt_exports = {}; + __export2(prebuilt_exports, { + PREBUILT_TAG: () => PREBUILT_TAG2, + greet: () => greet2 + }); + module2.exports = __toCommonJS2(prebuilt_exports); + function greet2(name) { + return "hello " + name; + } + var PREBUILT_TAG2 = "prebuilt"; + } +}); + +// chunk/vendor/lazy.js +var require_lazy = __commonJS({ + "chunk/vendor/lazy.js"(exports, module2) { + console.log("lazy init"); + module2.exports = { value: "lazy-value" }; + } +}); + +// chunk/src/index.ts +var src_exports = {}; +__export(src_exports, { + describe: () => describe, + external: () => external, + lazy: () => lazy, + nested: () => nested +}); +module.exports = __toCommonJS(src_exports); +var import_settings = __toESM(require_settings()); +var core = __toESM(require("gtcore")); +var import_gtcore = __toESM(require("gtcore")); +var import_prebuilt = __toESM(require_prebuilt()); +function describe() { + return import_settings.LABEL + "@" + import_settings.default.VERSION + " bump " + (0, import_settings.bump)() + " " + (0, import_settings.bump)(); +} +function external() { + return core.tag + ":" + core.label(1) + ":" + import_gtcore.default.label(2); +} +function nested() { + return (0, import_prebuilt.greet)("chunk") + " " + import_prebuilt.PREBUILT_TAG; +} +function lazy() { + return require_lazy().value; +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + describe, + external, + lazy, + nested +}); diff --git a/tests/fixtures/npm/node_modules/gtchunk17/package.json b/tests/fixtures/npm/node_modules/gtchunk17/package.json new file mode 100644 index 000000000..f01e680c0 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunk17/package.json @@ -0,0 +1,9 @@ +{ + "name": "gtchunk17", + "version": "1.0.0", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "dependencies": { + "gtcore": "1.0.0" + } +} diff --git a/tests/fixtures/npm/node_modules/gtchunkws/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtchunkws/dist/index.d.ts new file mode 100644 index 000000000..bd3d4e582 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunkws/dist/index.d.ts @@ -0,0 +1,4 @@ +export declare function describe(): string; +export declare function external(): string; +export declare function nested(): string; +export declare function lazy(): string; diff --git a/tests/fixtures/npm/node_modules/gtchunkws/dist/index.js b/tests/fixtures/npm/node_modules/gtchunkws/dist/index.js new file mode 100644 index 000000000..f022baf09 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunkws/dist/index.js @@ -0,0 +1 @@ +var __create=Object.create;var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __getProtoOf=Object.getPrototypeOf,__hasOwnProp=Object.prototype.hasOwnProperty;var __commonJS=(cb,mod)=>function(){return mod||(0,cb[__getOwnPropNames(cb)[0]])((mod={exports:{}}).exports,mod),mod.exports};var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:!0})},__copyProps=(to,from,except,desc)=>{if(from&&typeof from=="object"||typeof from=="function")for(let key of __getOwnPropNames(from))!__hasOwnProp.call(to,key)&&key!==except&&__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable});return to};var __toESM=(mod,isNodeMode,target)=>(target=mod!=null?__create(__getProtoOf(mod)):{},__copyProps(isNodeMode||!mod||!mod.__esModule?__defProp(target,"default",{value:mod,enumerable:!0}):target,mod)),__toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:!0}),mod);var require_counter=__commonJS({"chunk/vendor/settings/counter.js"(exports2,module2){var n=0;console.log("counter init");module2.exports={next(){return n+=1,n}}}});var require_settings=__commonJS({"chunk/vendor/settings/index.js"(exports2){"use strict";console.log("settings init");var counter=require_counter();exports2.LABEL="bundled";exports2.VERSION=2;exports2.bump=function(){return counter.next()}}});var require_prebuilt=__commonJS({"chunk/vendor/prebuilt/index.js"(exports2,module2){var __defProp2=Object.defineProperty,__getOwnPropDesc2=Object.getOwnPropertyDescriptor,__getOwnPropNames2=Object.getOwnPropertyNames,__hasOwnProp2=Object.prototype.hasOwnProperty,__export2=(target,all)=>{for(var name in all)__defProp2(target,name,{get:all[name],enumerable:!0})},__copyProps2=(to,from,except,desc)=>{if(from&&typeof from=="object"||typeof from=="function")for(let key of __getOwnPropNames2(from))!__hasOwnProp2.call(to,key)&&key!==except&&__defProp2(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc2(from,key))||desc.enumerable});return to},__toCommonJS2=mod=>__copyProps2(__defProp2({},"__esModule",{value:!0}),mod),prebuilt_exports={};__export2(prebuilt_exports,{PREBUILT_TAG:()=>PREBUILT_TAG2,greet:()=>greet2});module2.exports=__toCommonJS2(prebuilt_exports);function greet2(name){return"hello "+name}var PREBUILT_TAG2="prebuilt"}});var require_lazy=__commonJS({"chunk/vendor/lazy.js"(exports2,module2){console.log("lazy init");module2.exports={value:"lazy-value"}}});var index_exports={};__export(index_exports,{describe:()=>describe,external:()=>external,lazy:()=>lazy,nested:()=>nested});module.exports=__toCommonJS(index_exports);var import_settings=__toESM(require_settings()),core=__toESM(require("gtcore")),import_gtcore=__toESM(require("gtcore")),import_prebuilt=__toESM(require_prebuilt());function describe(){return import_settings.LABEL+"@"+import_settings.default.VERSION+" bump "+(0,import_settings.bump)()+" "+(0,import_settings.bump)()}function external(){return core.tag+":"+core.label(1)+":"+import_gtcore.default.label(2)}function nested(){return(0,import_prebuilt.greet)("chunk")+" "+import_prebuilt.PREBUILT_TAG}function lazy(){return require_lazy().value}0&&(module.exports={describe,external,lazy,nested}); diff --git a/tests/fixtures/npm/node_modules/gtchunkws/package.json b/tests/fixtures/npm/node_modules/gtchunkws/package.json new file mode 100644 index 000000000..c1fc1815a --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunkws/package.json @@ -0,0 +1,9 @@ +{ + "name": "gtchunkws", + "version": "1.0.0", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "dependencies": { + "gtcore": "1.0.0" + } +} diff --git a/tests/fixtures/npm/node_modules/gtcjsdist/dist/index.cjs b/tests/fixtures/npm/node_modules/gtcjsdist/dist/index.cjs new file mode 100644 index 000000000..8836b5691 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcjsdist/dist/index.cjs @@ -0,0 +1,43 @@ +var __create = Object.create; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __getProtoOf = Object.getPrototypeOf; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( + // If the importer is in node compatibility mode or this is not an ESM + // file that has been converted to a CommonJS file using a Babel- + // compatible transform (i.e. "__esModule" has not been set), then set + // "default" to the CommonJS "module.exports" for node compatibility. + isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, + mod +)); +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); + +// cjsdist/src/index.ts +var index_exports = {}; +__export(index_exports, { + viaNodeMode: () => viaNodeMode +}); +module.exports = __toCommonJS(index_exports); +var core = __toESM(require("gtcore"), 1); +var import_gtcore = __toESM(require("gtcore"), 1); +function viaNodeMode() { + return import_gtcore.default.tag + "/" + core.label(7) + "/" + import_gtcore.default.label(8); +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + viaNodeMode +}); diff --git a/tests/fixtures/npm/node_modules/gtcjsdist/dist/index.d.cts b/tests/fixtures/npm/node_modules/gtcjsdist/dist/index.d.cts new file mode 100644 index 000000000..6f77b9943 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcjsdist/dist/index.d.cts @@ -0,0 +1 @@ +export declare function viaNodeMode(): string; diff --git a/tests/fixtures/npm/node_modules/gtcjsdist/package.json b/tests/fixtures/npm/node_modules/gtcjsdist/package.json new file mode 100644 index 000000000..c40f3baed --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcjsdist/package.json @@ -0,0 +1,10 @@ +{ + "name": "gtcjsdist", + "version": "1.0.0", + "type": "module", + "main": "./dist/index.cjs", + "types": "./dist/index.d.cts", + "dependencies": { + "gtcore": "1.0.0" + } +} diff --git a/tests/fixtures/npm/node_modules/gtcycle/dist/append.d.ts b/tests/fixtures/npm/node_modules/gtcycle/dist/append.d.ts new file mode 100644 index 000000000..e33c31f90 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcycle/dist/append.d.ts @@ -0,0 +1 @@ +export declare function appendAll(xs: string[], tail: string): string[]; diff --git a/tests/fixtures/npm/node_modules/gtcycle/dist/append.js b/tests/fixtures/npm/node_modules/gtcycle/dist/append.js new file mode 100644 index 000000000..cdfeb6c6c --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcycle/dist/append.js @@ -0,0 +1,30 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var append_exports = {}; +__export(append_exports, { + appendAll: () => appendAll +}); +module.exports = __toCommonJS(append_exports); +var import_index = require("./index"); +function appendAll(xs, tail) { + return xs.some(import_index.isHandler) ? xs : xs.concat([tail]); +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + appendAll +}); diff --git a/tests/fixtures/npm/node_modules/gtcycle/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtcycle/dist/index.d.ts new file mode 100644 index 000000000..4b86308a9 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcycle/dist/index.d.ts @@ -0,0 +1,4 @@ +export { mergeAll } from "./merge"; +export declare function isHandler(x: string): boolean; +export declare function normalize(xs: string[]): string[]; +export declare function run(a: string[], b: string[]): string; diff --git a/tests/fixtures/npm/node_modules/gtcycle/dist/index.js b/tests/fixtures/npm/node_modules/gtcycle/dist/index.js new file mode 100644 index 000000000..92f6d2a39 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcycle/dist/index.js @@ -0,0 +1,44 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var index_exports = {}; +__export(index_exports, { + isHandler: () => isHandler, + mergeAll: () => import_merge2.mergeAll, + normalize: () => normalize, + run: () => run +}); +module.exports = __toCommonJS(index_exports); +var import_append = require("./append"); +var import_merge = require("./merge"); +var import_merge2 = require("./merge"); +function isHandler(x) { + return x.startsWith("@"); +} +function normalize(xs) { + return xs.map((x) => x.trim()).filter((x) => x.length > 0); +} +function run(a, b) { + return (0, import_append.appendAll)((0, import_merge.mergeAll)(a, b), "@end").join(","); +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + isHandler, + mergeAll, + normalize, + run +}); diff --git a/tests/fixtures/npm/node_modules/gtcycle/dist/merge.d.ts b/tests/fixtures/npm/node_modules/gtcycle/dist/merge.d.ts new file mode 100644 index 000000000..bbe6b6a4d --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcycle/dist/merge.d.ts @@ -0,0 +1 @@ +export declare function mergeAll(a: string[], b: string[]): string[]; diff --git a/tests/fixtures/npm/node_modules/gtcycle/dist/merge.js b/tests/fixtures/npm/node_modules/gtcycle/dist/merge.js new file mode 100644 index 000000000..114e7d8cf --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcycle/dist/merge.js @@ -0,0 +1,30 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var merge_exports = {}; +__export(merge_exports, { + mergeAll: () => mergeAll +}); +module.exports = __toCommonJS(merge_exports); +var import_index = require("./index"); +function mergeAll(a, b) { + return (0, import_index.normalize)(a.concat(b)).filter((x) => !(0, import_index.isHandler)(x) || x === "@keep"); +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + mergeAll +}); diff --git a/tests/fixtures/npm/node_modules/gtcycle/package.json b/tests/fixtures/npm/node_modules/gtcycle/package.json new file mode 100644 index 000000000..7d275beef --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcycle/package.json @@ -0,0 +1,6 @@ +{ + "name": "gtcycle", + "version": "1.0.0", + "main": "./dist/index.js", + "types": "./dist/index.d.ts" +} diff --git a/tests/fixtures/npm/node_modules/gtcyclebad/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtcyclebad/dist/index.d.ts new file mode 100644 index 000000000..0fa366319 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclebad/dist/index.d.ts @@ -0,0 +1,2 @@ +export declare const LIMIT = 3; +export declare function show(): string; diff --git a/tests/fixtures/npm/node_modules/gtcyclebad/dist/index.js b/tests/fixtures/npm/node_modules/gtcyclebad/dist/index.js new file mode 100644 index 000000000..5bd708a37 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclebad/dist/index.js @@ -0,0 +1,33 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var index_exports = {}; +__export(index_exports, { + LIMIT: () => LIMIT, + show: () => show +}); +module.exports = __toCommonJS(index_exports); +var import_reader = require("./reader"); +const LIMIT = 3; +function show() { + return (0, import_reader.describe)(); +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + LIMIT, + show +}); diff --git a/tests/fixtures/npm/node_modules/gtcyclebad/dist/reader.js b/tests/fixtures/npm/node_modules/gtcyclebad/dist/reader.js new file mode 100644 index 000000000..a58bdaa51 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclebad/dist/reader.js @@ -0,0 +1,33 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var reader_exports = {}; +__export(reader_exports, { + captured: () => captured, + describe: () => describe +}); +module.exports = __toCommonJS(reader_exports); +var import_index = require("./index"); +const captured = import_index.LIMIT; +function describe() { + return "limit " + captured; +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + captured, + describe +}); diff --git a/tests/fixtures/npm/node_modules/gtcyclebad/package.json b/tests/fixtures/npm/node_modules/gtcyclebad/package.json new file mode 100644 index 000000000..a6524f8c1 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclebad/package.json @@ -0,0 +1,6 @@ +{ + "name": "gtcyclebad", + "version": "1.0.0", + "main": "./dist/index.js", + "types": "./dist/index.d.ts" +} From 22edb6deb00d31137df32b7b0dceaf80f8960405 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 12:20:28 -0700 Subject: [PATCH 18/83] Test bundled chunks, cycles and refusals Unit tests run each split bundle against its original under Node and pin the refusal reasons; npm-static tests compile the new cases and compare them with Node byte for byte. Co-Authored-By: Claude Opus 5.5 --- .../npm-static-bundled-chunks.test.ts | 160 ++++++++++++++++++ .../src/frontend/npm-static-bundled-chunks.ts | 18 +- .../src/frontend/npm-static-rewrite-syntax.ts | 2 +- .../npm/node_modules/gtchunk/package.json | 1 + .../npm/node_modules/gtchunk17/package.json | 1 + .../npm/node_modules/gtchunkws/package.json | 1 + .../npm/node_modules/gtcjsdist/package.json | 1 + .../npm/node_modules/gtcycle/package.json | 1 + .../npm/node_modules/gtcyclebad/package.json | 1 + tests/harness/npm-cases.ts | 4 +- tests/harness/npm-static.test.ts | 61 ++++++- 11 files changed, 239 insertions(+), 12 deletions(-) create mode 100644 packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts new file mode 100644 index 000000000..91b4c9763 --- /dev/null +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts @@ -0,0 +1,160 @@ +import { spawnSync } from "node:child_process"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { basename, join } from "node:path"; +import { afterAll, expect, test } from "vitest"; +import { nodeFrontendServices } from "./services-node.js"; +import type { BundledChunkSplit } from "./npm-static-bundled-chunks.js"; + +const keyedHelper = `var __getOwnPropNames = Object.getOwnPropertyNames; +var __commonJS = (cb, mod) => function __require() { + return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; +}; +`; + +const dirs: string[] = []; +afterAll(() => { + for (const dir of dirs) rmSync(dir, { recursive: true, force: true }); +}); + +function split(source: string, file = "/pkg/dist/index.js") { + return nodeFrontendServices().splitCjsChunks(source, file); +} + +function run(file: string) { + const result = spawnSync(process.execPath, [file], { encoding: "utf8" }); + return { status: result.status, stdout: result.stdout, stderr: result.stderr }; +} + +/** Runs the original bundle and its split modules side by side. */ +function expectSameBehavior(source: string): BundledChunkSplit { + const dir = mkdtempSync(join(tmpdir(), "scriptc-chunks-")); + dirs.push(dir); + const original = join(dir, "original.js"); + writeFileSync(original, source); + const bundle = join(dir, "index.js"); + const result = split(source, bundle); + expect(result).not.toBeNull(); + expect(result).not.toHaveProperty("degrade"); + const answer = result as BundledChunkSplit; + writeFileSync(bundle, answer.text); + for (const chunk of answer.chunks) writeFileSync(chunk.path, chunk.source); + // Respelled calls and blanked declarations keep the line structure. + expect(answer.text.split("\n")).toHaveLength(source.split("\n").length); + const oracle = run(original); + expect(oracle.status).toBe(0); + expect(run(bundle)).toEqual(oracle); + return answer; +} + +test("chunks run once, at their first require, with shared module instances", () => { + const answer = expectSameBehavior(`"use strict"; +${keyedHelper} +var require_b = __commonJS({ + "b.js"(exports2, module2) { + console.log("b init"); + module2.exports = { n: 1 }; + } +}); +var require_a = __commonJS({ + "a.js"(exports2) { + console.log("a init"); + var b = require_b(); + exports2.value = b.n + 1; + exports2.keys = Object.keys({ exports2 }); + } +}); +console.log("start"); +console.log(require_a().value, require_a().value, require_b().n, require_a().keys); +`); + expect(answer.chunks.map((c) => basename(c.path))).toEqual([ + "index.js.require_b.cjs", + "index.js.require_a.cjs", + ]); + expect(answer.text).not.toContain("require_a()"); +}); + +test("the arrow helper form and minified spellings split the same way", () => { + expectSameBehavior( + `var __commonJS=(cb,mod)=>()=>(mod||cb((mod={exports:{}}).exports,mod),mod.exports);` + + `var require_x=__commonJS((exports,module)=>{module.exports={v:"x"}});` + + `console.log(require_x().v,require_x()===require_x());`, + ); +}); + +test("esbuild 0.11's Object.keys spelling is recognized", () => { + expectSameBehavior(`var __commonJS = (cb, mod) => function __require() { + return mod || (0, cb[Object.keys(cb)[0]])((mod = {exports: {}}).exports, mod), mod.exports; +}; +var require_y = __commonJS({ + "y.js"(exports) { + exports.y = 3; + } +}); +console.log(require_y().y); +`); +}); + +test("pure helpers a chunk reads travel with it", () => { + const answer = expectSameBehavior(`${keyedHelper}var __defProp = Object.defineProperty; +var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); +var require_named = __commonJS({ + "named.js"(exports2, module2) { + module2.exports = __name(function () {}, "renamed"); + } +}); +console.log(require_named().name); +`); + const chunk = answer.chunks[0]!.source; + expect(chunk).toContain("var __name ="); + expect(chunk).toContain("var __defProp = Object.defineProperty;"); +}); + +test.each([ + ["reads its factory's receiver", `console.log(this);`], + ["reads its factory's arguments object", `console.log(arguments.length);`], + ["returns from its top level", `if (exports2) return;`], + ["uses its module object beyond module.exports", `console.log(module2.id);`], + ["reads __filename", `console.log(__filename);`], + ["rebinds its module parameters", `function f(module2) { return module2; }`], + ["reads the bundle's top-level binding 'state'", `console.log(state);`], +])("a chunk that %s keeps the package refused", (reason, statement) => { + const result = split(`${keyedHelper}var state = { n: 1 }; +var require_c = __commonJS({ + "c.js"(exports2, module2) { + ${statement} + } +}); +require_c(); +`); + expect(result).toEqual({ degrade: expect.stringContaining(reason) as string }); +}); + +test("a factory used other than as a direct call keeps the package refused", () => { + const result = + split(`${keyedHelper}var require_d = __commonJS({ "d.js"(exports2) { exports2.d = 1; } }); +var load = require_d; +load(); +`); + expect(result).toEqual({ + degrade: expect.stringContaining("used other than as a direct call") as string, + }); +}); + +test("an unrecognized helper body keeps the package refused", () => { + const result = + split(`var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod); +var require_e = __commonJS((exports) => { exports.e = 1; }); +require_e(); +`); + expect(result).toEqual({ degrade: expect.stringContaining("__commonJS helper") as string }); +}); + +test("files without chunks and ES-module bundles are left alone", () => { + expect(split("exports.x = 1;\n")).toBeNull(); + expect( + split(`${keyedHelper}var require_f = __commonJS({ "f.js"(e) { e.f = 1; } }); +export default require_f(); +`), + ).toBeNull(); +}); diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts index e1256e59f..8b065ccda 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts @@ -157,7 +157,8 @@ function isWritten(id: ts.Identifier): boolean { * keyed: `(CB, MOD) => function [name]() { return ; }` — the * factory is the only method of an object literal; * arrow: `(CB, MOD) => () => ()` — the factory itself. */ -const KEYED_RETURN = "MOD || (0, CB[KEYS(CB)[0]])((MOD = { exports: {} }).exports, MOD), MOD.exports"; +const KEYED_RETURN = + "MOD || (0, CB[KEYS(CB)[0]])((MOD = { exports: {} }).exports, MOD), MOD.exports"; const ARROW_BODY = "(MOD || CB((MOD = { exports: {} }).exports, MOD), MOD.exports)"; function template(text: string, cb: string, mod: string, keys: string): string | null { @@ -255,7 +256,7 @@ export function splitBundledChunks( if (!source.includes("__commonJS")) return null; // ES-module bundles have no require: their chunks keep the existing // treatment (the Bun factory recognizer, or the ESM interop refusal). - if (!/\.cjs$/.test(filePath) && ts.isExternalModule(sf)) return null; + if (!filePath.endsWith(".cjs") && ts.isExternalModule(sf)) return null; const top = new Map(); const duplicate = new Set(); @@ -280,7 +281,8 @@ export function splitBundledChunks( const written = new Set(); for (const r of allRefs) if (isWritten(r)) written.add(r.text); const declaredAnywhere = declaredNames(sf); - if (declaredAnywhere.has("Object") || written.has("Object")) return { degrade: CHUNK_SHAPE_DEGRADE }; + if (declaredAnywhere.has("Object") || written.has("Object")) + return { degrade: CHUNK_SHAPE_DEGRADE }; const stable = (name: string): boolean => !duplicate.has(name) && !written.has(name); const ownNames = (name: string): boolean => { const t = top.get(name); @@ -440,9 +442,7 @@ function applyEdits(source: string, from: number, to: number, edits: Edit[]): st const replaced = source.slice(e.start, e.end); // A respelled span keeps its line breaks after the replacement. text += - e.text === "" - ? replaced.replace(/[^\r\n]/g, " ") - : e.text + replaced.replace(/[^\n]/g, ""); + e.text === "" ? replaced.replace(/[^\r\n]/g, " ") : e.text + replaced.replace(/[^\n]/g, ""); at = e.end; } return text + source.slice(at, to); @@ -469,7 +469,11 @@ function chunkSource( const body = chunk.body; const local = declaredNames(body); for (const p of chunk.params) { - if (local.has(p) || (p !== "exports" && local.has("exports")) || (p !== "module" && local.has("module"))) + if ( + local.has(p) || + (p !== "exports" && local.has("exports")) || + (p !== "module" && local.has("module")) + ) return why( "rebinds its module parameters inside its body (an identifier-minified bundle the recognizer cannot respell)", ); diff --git a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts index e0534fc2d..fb591379b 100644 --- a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts +++ b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts @@ -539,7 +539,7 @@ export function rewriteBundlerCjsSyntax( // TypeScript forces every `.cjs` file to be an external module // (isFileForcedToBeModuleByFormat), yet Node always loads one as // CommonJS: the extension, not that flag, decides the format there. - if (!/\.cjs$/.test(filePath) && ts.isExternalModule(sf)) { + if (!filePath.endsWith(".cjs") && ts.isExternalModule(sf)) { const bundled = rewriteBundledFunctionImports(source, sf); if (bundled !== null) return bundled; return source.includes("__toESM(") diff --git a/tests/fixtures/npm/node_modules/gtchunk/package.json b/tests/fixtures/npm/node_modules/gtchunk/package.json index 579ad6cab..2812a5188 100644 --- a/tests/fixtures/npm/node_modules/gtchunk/package.json +++ b/tests/fixtures/npm/node_modules/gtchunk/package.json @@ -1,6 +1,7 @@ { "name": "gtchunk", "version": "1.0.0", + "description": "esbuild 0.25.10 output (--bundle --platform=node --format=cjs --external:gtcore): bundled CommonJS modules behind the __commonJS helper, including an esbuild-built dist with suffixed helpers", "main": "./dist/index.js", "types": "./dist/index.d.ts", "dependencies": { diff --git a/tests/fixtures/npm/node_modules/gtchunk17/package.json b/tests/fixtures/npm/node_modules/gtchunk17/package.json index f01e680c0..73761f181 100644 --- a/tests/fixtures/npm/node_modules/gtchunk17/package.json +++ b/tests/fixtures/npm/node_modules/gtchunk17/package.json @@ -1,6 +1,7 @@ { "name": "gtchunk17", "version": "1.0.0", + "description": "the gtchunk source bundled by esbuild 0.17.19: chunk factories take (exports, module2)", "main": "./dist/index.js", "types": "./dist/index.d.ts", "dependencies": { diff --git a/tests/fixtures/npm/node_modules/gtchunkws/package.json b/tests/fixtures/npm/node_modules/gtchunkws/package.json index c1fc1815a..5502f80a0 100644 --- a/tests/fixtures/npm/node_modules/gtchunkws/package.json +++ b/tests/fixtures/npm/node_modules/gtchunkws/package.json @@ -1,6 +1,7 @@ { "name": "gtchunkws", "version": "1.0.0", + "description": "the gtchunk source bundled by esbuild 0.25.10 with --minify-whitespace --minify-syntax: helpers share var statements", "main": "./dist/index.js", "types": "./dist/index.d.ts", "dependencies": { diff --git a/tests/fixtures/npm/node_modules/gtcjsdist/package.json b/tests/fixtures/npm/node_modules/gtcjsdist/package.json index c40f3baed..8b416fca1 100644 --- a/tests/fixtures/npm/node_modules/gtcjsdist/package.json +++ b/tests/fixtures/npm/node_modules/gtcjsdist/package.json @@ -1,6 +1,7 @@ { "name": "gtcjsdist", "version": "1.0.0", + "description": "a type-module package whose CommonJS entry is an esbuild 0.25.10 .cjs bundle using the node-mode __toESM interop", "type": "module", "main": "./dist/index.cjs", "types": "./dist/index.d.cts", diff --git a/tests/fixtures/npm/node_modules/gtcycle/package.json b/tests/fixtures/npm/node_modules/gtcycle/package.json index 7d275beef..5b41b8a52 100644 --- a/tests/fixtures/npm/node_modules/gtcycle/package.json +++ b/tests/fixtures/npm/node_modules/gtcycle/package.json @@ -1,6 +1,7 @@ { "name": "gtcycle", "version": "1.0.0", + "description": "esbuild 0.25.10 per-file CommonJS output (no bundling) of three modules that import each other", "main": "./dist/index.js", "types": "./dist/index.d.ts" } diff --git a/tests/fixtures/npm/node_modules/gtcyclebad/package.json b/tests/fixtures/npm/node_modules/gtcyclebad/package.json index a6524f8c1..793c97fc9 100644 --- a/tests/fixtures/npm/node_modules/gtcyclebad/package.json +++ b/tests/fixtures/npm/node_modules/gtcyclebad/package.json @@ -1,6 +1,7 @@ { "name": "gtcyclebad", "version": "1.0.0", + "description": "esbuild 0.25.10 per-file CommonJS output of a cycle whose partner reads a const binding before its declaration runs", "main": "./dist/index.js", "types": "./dist/index.d.ts" } diff --git a/tests/harness/npm-cases.ts b/tests/harness/npm-cases.ts index 4dda0b9ad..8562ac24f 100644 --- a/tests/harness/npm-cases.ts +++ b/tests/harness/npm-cases.ts @@ -19,12 +19,12 @@ export function npmCases(fixturesRoot: string): NpmCase[] { return [ ...globSync(join(fixturesRoot, "npm/cases/*/main.ts")) .sort() - // 2465-2469 and 2556-2557 are the --npm-static bundler-emitted-CJS + // 2465-2469 and 2556-2561 are the --npm-static bundler-emitted-CJS // cases (npm-static.test.ts drives them with the opt-in): their // consumers exercise the static rewrite's surface (lexer-visible // names the shipped .d.ts never declares, the __toESM interop // family), so they stay out of the flagless island lane by design. - .filter((entry) => !/\/(246[5-9]|255[67])-[^/]+\/main\.ts$/.test(entry)) + .filter((entry) => !/\/(246[5-9]|255[6-9]|256[01])-[^/]+\/main\.ts$/.test(entry)) .map((entry) => ({ name: entry.split("/").at(-2)!, entry, diff --git a/tests/harness/npm-static.test.ts b/tests/harness/npm-static.test.ts index 356d2fa6d..b908f0bd9 100644 --- a/tests/harness/npm-static.test.ts +++ b/tests/harness/npm-static.test.ts @@ -76,8 +76,8 @@ async function buildStatic(entry: string, npmStatic: string[] | "auto"): Promise ...globSync( join(fixturesRoot, "npm/node_modules/cryptozoo/**/*.{js,mjs,cjs,json,d.ts}"), ).sort(), - // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2557) - ...globSync(join(fixturesRoot, "npm/node_modules/gt*/**/*.{js,json}")).sort(), + // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2561) + ...globSync(join(fixturesRoot, "npm/node_modules/gt*/**/*.{js,cjs,json}")).sort(), ]; for (const f of inputs) hash.update(f).update(readFileSync(f)); const key = hash @@ -1389,6 +1389,63 @@ describe(`npm-static pilots${sanitize ? " (sanitized)" : ""}`, () => { ]); }, 120_000); + /* ── 2558-2561: esbuild's bundled CommonJS chunks and per-file CommonJS + * cycles ─ every __commonJS chunk compiles as its own CommonJS module + * (evaluated once, at its first require); a require cycle between + * esbuild outputs whose live getter tables install before their first + * require initializes exactly like Node, and a cycle that reads a + * binding in its temporal dead zone stays refused. */ + async function expectNodeParity(entry: string, packages: string[]): Promise { + const { coverage } = analyze(entry, { npmStatic: packages }); + expect(coverage.npmStatic).toEqual(packages.map((pkg) => ({ package: pkg, status: "static" }))); + expect(coverage.preflightFailed).toBe(false); + expect(coverage.diagnostics).toHaveLength(0); + const binary = await buildStatic(entry, packages); + const [nodeRes, nativeRes] = await Promise.all([ + runBinary("node", [entry]), + runBinary(binary, []), + ]); + nativeRes.stderr = comparableStderr(nativeRes.stderr); + expect(nativeRes).toEqual(nodeRes); + expect(nativeRes.exitCode).toBe(0); + } + + // 2558: esbuild 0.25 (plain and whitespace/syntax minified) and 0.17 + // CommonJS bundles of one source, plus a .cjs bundle in a type-module + // package with the node-mode interop. + test("bundled CommonJS chunks 2558-bundled-chunks compile statically and byte-match Node", async () => { + await expectNodeParity(join(fixturesRoot, "npm/cases/2558-bundled-chunks/main.ts"), [ + "gtchunk", + "gtchunkws", + "gtchunk17", + "gtcjsdist", + "gtcore", + ]); + }, 180_000); + + test.each(["2559-cjs-cycle", "2560-cjs-cycle-partner-first"])( + "the esbuild CommonJS cycle %s compiles statically and byte-matches Node", + async (name) => { + await expectNodeParity(join(fixturesRoot, `npm/cases/${name}/main.ts`), ["gtcycle"]); + }, + 180_000, + ); + + // 2561: Node throws a ReferenceError there; the cycle keeps SC1016. + test("a CommonJS cycle that reads a binding before its declaration stays refused", () => { + const entry = join(fixturesRoot, "npm/cases/2561-cjs-cycle-early-read/main.ts"); + const { coverage } = analyze(entry, { npmStatic: ["gtcyclebad"] }); + expect(coverage.npmStatic).toEqual([ + { + package: "gtcyclebad", + status: "refused", + detail: expect.stringMatching( + /circular imports .*can use its binding 'import_index' of a module of the cycle/, + ) as string, + }, + ]); + }, 120_000); + test("published type-only declarations coexist with a static runtime bundle", async () => { const entry = join(fixturesRoot, "npm/cases/2469-bundle-offender/main.ts"); const { coverage } = analyze(entry, { npmStatic: ["gtghost"] }); From bda597034323f12c2757ebede5334021cfb64653 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 12:32:22 -0700 Subject: [PATCH 19/83] Name identifier-minified and ES-module chunk bundles in refusals An identifier-minified esbuild bundle keeps the __commonJS helper's shape but loses the names the recognizers verify; an ES-module bundle of CommonJS chunks has no require to route them through. Both now refuse with a message naming the construct. Co-Authored-By: Claude Opus 5.5 --- .../npm-static-bundled-chunks.test.ts | 22 ++++++++++++++++++ .../src/frontend/npm-static-bundled-chunks.ts | 23 +++++++++++++++++-- .../src/frontend/npm-static-rewrite-syntax.ts | 15 ++++++------ packages/compiler/src/frontend/services.ts | 8 +++++-- 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts index 91b4c9763..3ef430102 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts @@ -4,6 +4,7 @@ import { tmpdir } from "node:os"; import { basename, join } from "node:path"; import { afterAll, expect, test } from "vitest"; import { nodeFrontendServices } from "./services-node.js"; +import { rewriteBundlerCjsExports } from "./npm-static-rewrite.js"; import type { BundledChunkSplit } from "./npm-static-bundled-chunks.js"; const keyedHelper = `var __getOwnPropNames = Object.getOwnPropertyNames; @@ -158,3 +159,24 @@ export default require_f(); `), ).toBeNull(); }); + +test("an identifier-minified bundle names its lost helper names", () => { + // esbuild --minify output: the helper shapes survive, their names do not. + const result = split( + `var c=(r,e)=>()=>(e||r((e={exports:{}}).exports,e),e.exports);` + + `var x=c((t,m)=>{m.exports={v:1}});console.log(x().v);`, + ); + expect(result).toEqual({ degrade: expect.stringContaining("identifier-minified") as string }); +}); + +test("an ES-module bundle of CommonJS chunks names the chunk interop", () => { + const result = rewriteBundlerCjsExports( + `${keyedHelper}var __toESM = (mod) => mod; +var require_g = __commonJS({ "g.js"(exports) { exports.g = 1; } }); +var import_g = __toESM(require_g()); +export const g = import_g.g; +`, + "/pkg/dist/index.js", + ); + expect(result).toEqual({ degrade: expect.stringContaining("bundled CommonJS modules") as string }); +}); diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts index 8b065ccda..c4a12e998 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts @@ -245,6 +245,12 @@ function strictPrologue(stmts: readonly ts.Statement[]): boolean { return false; } +/** A cheap text probe: the helper's name, or its minified body's + * `(m={exports:{}}).exports,m` fragment. */ +export function mayBundleChunks(source: string): boolean { + return source.includes("__commonJS") || /\{\s*exports\s*:\s*\{\s*\}\s*\}\s*\)\s*\.exports\s*,/.test(source); +} + /** Splits a CommonJS bundle's __commonJS chunks into virtual modules (see * the header). Null: no chunks (or not a CommonJS bundle) — serve the file * unchanged. */ @@ -253,7 +259,7 @@ export function splitBundledChunks( filePath: string, ): BundledChunkSplit | { degrade: string } | null { const source = sf.text; - if (!source.includes("__commonJS")) return null; + if (!mayBundleChunks(source)) return null; // ES-module bundles have no require: their chunks keep the existing // treatment (the Bun factory recognizer, or the ESM interop refusal). if (!filePath.endsWith(".cjs") && ts.isExternalModule(sf)) return null; @@ -273,7 +279,6 @@ export function splitBundledChunks( } } const helperNames = [...top.keys()].filter((n) => helperBase(n) === "__commonJS"); - if (helperNames.length === 0) return null; // Every name a recognizer relies on is written nowhere and declared // once at the top level; `Object` itself is never rebound. @@ -293,6 +298,20 @@ export function splitBundledChunks( helperTokens(init.getText(sf)) === helperTokens("Object.getOwnPropertyNames") ); }; + if (helperNames.length === 0) { + // The helper's shape under another name: an identifier-minified bundle, + // whose other interop helpers lost their names as well. + for (const [name, t] of top) { + const init = ts.isVariableDeclaration(t.node) ? t.node.initializer : undefined; + if (init !== undefined && stable(name) && helperForm(init, sf, ownNames) !== null) + return { + degrade: + "its shipped JS is an identifier-minified bundle whose bundler-interop helpers " + + "(__commonJS and the rest) have lost the names the recognizers verify", + }; + } + return null; + } const forms = new Map(); for (const name of helperNames) { const t = top.get(name)!; diff --git a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts index fb591379b..1730e1153 100644 --- a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts +++ b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts @@ -542,13 +542,14 @@ export function rewriteBundlerCjsSyntax( if (!filePath.endsWith(".cjs") && ts.isExternalModule(sf)) { const bundled = rewriteBundledFunctionImports(source, sf); if (bundled !== null) return bundled; - return source.includes("__toESM(") - ? { - degrade: - "its ES-module dist routes an external dependency through the __toESM " + - "bundler-interop helper, which cannot compile natively in ESM output", - } - : null; + if (!source.includes("__toESM(")) return null; + return { + degrade: source.includes("__commonJS") + ? "its ES-module bundle reaches bundled CommonJS modules through the __commonJS and " + + "__toESM bundler-interop helpers, which cannot compile natively in ESM output" + : "its ES-module dist routes an external dependency through the __toESM " + + "bundler-interop helper, which cannot compile natively in ESM output", + }; } // The __toESM interop pass (see the section header): wrapper call sites diff --git a/packages/compiler/src/frontend/services.ts b/packages/compiler/src/frontend/services.ts index 40a989a0c..c6bb24ae5 100644 --- a/packages/compiler/src/frontend/services.ts +++ b/packages/compiler/src/frontend/services.ts @@ -2,7 +2,11 @@ import { Ts7SourceParser, type Ts7SourceKind } from "./ts7/source-parser.js"; import { Ts7Host, type Ts7ApiFactory, type Ts7HostOptions } from "./ts7/program-host.js"; import { rewriteBundlerCjsWithParser } from "./npm-static-rewrite-host.js"; import { isBundlerCjsCandidate } from "./npm-static-rewrite-syntax.js"; -import { splitBundledChunks, type BundledChunkSplit } from "./npm-static-bundled-chunks.js"; +import { + mayBundleChunks, + splitBundledChunks, + type BundledChunkSplit, +} from "./npm-static-bundled-chunks.js"; import * as syntax from "./npm-static-declaration-syntax.js"; import type { SourceFile } from "./ts7/ast-types.js"; @@ -69,7 +73,7 @@ export class FrontendServices { * chunks; a degrade reason: chunks the recognizer cannot move). */ splitCjsChunks(source: string, path: string): BundledChunkSplit | { degrade: string } | null { this.ensureOpen(); - if (!source.includes("__commonJS")) return null; + if (!mayBundleChunks(source)) return null; return splitBundledChunks(this.parse(path, source, "js"), path); } nullableClassFields(path: string, source: string): syntax.NpmStaticOverloadRewrite | null { From 405f643742afb50214b58298717a146a14719d42 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 12:43:47 -0700 Subject: [PATCH 20/83] Judge bundled packages by the files their imports can load Interop the rewrite cannot finish now refuses its package only when the compiled program can load that file: the module order closed over every literal require() and import() and, in package JavaScript, every import and export declaration (a computed specifier keeps every file loadable). A package reached through a leaf subpath and types from its main entry stays static. Auto mode also no longer refuses a whitespace/syntax minified bundle whose interop helpers keep their names: the recognizers judge it instead. Co-Authored-By: Claude Opus 5.5 --- packages/compiler/src/frontend/npm-static.ts | 31 +++++++++-- packages/compiler/src/frontend/pipeline.ts | 10 ++++ packages/compiler/src/frontend/program.ts | 51 +++++++++++++++++++ .../npm/cases/2562-subpath-leaf/main.ts | 10 ++++ .../npm/node_modules/gtsubpath/dist/data.d.ts | 2 + .../npm/node_modules/gtsubpath/dist/data.js | 6 +++ .../npm/node_modules/gtsubpath/dist/detect.js | 11 ++++ .../node_modules/gtsubpath/dist/index.d.ts | 6 +++ .../npm/node_modules/gtsubpath/dist/index.js | 5 ++ .../npm/node_modules/gtsubpath/package.json | 10 ++++ tests/harness/npm-cases.ts | 4 +- tests/harness/npm-static.test.ts | 26 ++++++++-- 12 files changed, 164 insertions(+), 8 deletions(-) create mode 100644 tests/fixtures/npm/cases/2562-subpath-leaf/main.ts create mode 100644 tests/fixtures/npm/node_modules/gtsubpath/dist/data.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtsubpath/dist/data.js create mode 100644 tests/fixtures/npm/node_modules/gtsubpath/dist/detect.js create mode 100644 tests/fixtures/npm/node_modules/gtsubpath/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtsubpath/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtsubpath/package.json diff --git a/packages/compiler/src/frontend/npm-static.ts b/packages/compiler/src/frontend/npm-static.ts index f377628d6..7dffd05f7 100644 --- a/packages/compiler/src/frontend/npm-static.ts +++ b/packages/compiler/src/frontend/npm-static.ts @@ -48,6 +48,7 @@ export function setNpmStaticPackages>(packages: T): v declarationOverloads = new Map(); declarationProperties = new Map(); offenders.clear(); + fileDegrades.clear(); rewriteCache.clear(); liveExportFiles.clear(); clearVirtualSources(); @@ -98,6 +99,21 @@ export function npmStaticOffenders(): ReadonlyMap { return offenders; } +/** Files whose bundler interop the rewrite could not finish this attempt, + * with their package and reason. A file refuses its package only when + * the compiled program can load it (npmStaticLoadableFiles): a package is + * judged by what its imported subpaths reach, not by every file it ships. */ +const fileDegrades = new Map(); + +export function npmStaticFileDegrades(): ReadonlyMap { + return fileDegrades; +} + +function reportFileDegrade(path: string, pkg: string, reason: string): void { + const key = path.split("\\").join("/"); + if (!fileDegrades.has(key)) fileDegrades.set(key, { pkg, reason }); +} + /** The DefinitelyTyped name mangling ("@scope/pkg" → "scope__pkg") — the * @types twin that must hide alongside the package's own declarations. */ function mangledTypesName(name: string): string { @@ -385,7 +401,7 @@ export function npmStaticFsShadow(services: FrontendServices): NpmStaticFsShadow // CommonJS module text. const split = original === null ? null : services.splitCjsChunks(original, path); if (split !== null && "degrade" in split) { - reportNpmStaticOffender(target.pkg, split.degrade); + reportFileDegrade(path, target.pkg, split.degrade); original = null; } else if (split !== null) { for (const chunk of split.chunks) setVirtualSource(chunk.path, chunk.source); @@ -416,7 +432,7 @@ export function npmStaticFsShadow(services: FrontendServices): NpmStaticFsShadow (live) => liveExportFiles.add(live.split("\\").join("/")), ); if (answer !== null && typeof answer === "object") { - reportNpmStaticOffender(target.pkg, answer.degrade); + reportFileDegrade(path, target.pkg, answer.degrade); } else { rewritten = answer ?? @@ -464,6 +480,14 @@ function looksUnminified(source: string): boolean { return source.length / lines.length <= 200; } +/** A whitespace/syntax-minified bundler dist whose interop helpers keep + * their names (`var __toESM=…,__toCommonJS=mod=>…`): the recognizers + * verify those shapes and refuse the package precisely when they cannot, + * so the minification heuristic need not refuse it up front. */ +function namedBundlerHelpers(source: string): boolean { + return /\b__(?:toCommonJS|commonJS|toESM)\d*\s*=\s*(?:\(|[A-Za-z_$][\w$]*\s*=>)/.test(source); +} + function hasTransformMarkers(source: string): boolean { return TRANSFORM_MARKERS.some((m) => source.includes(m)); } @@ -486,7 +510,8 @@ export function npmStaticIneligibleReason( if (jsEntry === null) return "no runtime JS entry resolves"; const source = trackedReadFile(jsEntry); if (source === null) return `its runtime entry ${jsEntry} cannot be read`; - if (!looksUnminified(source)) return "its shipped JS looks minified"; + if (!looksUnminified(source) && !namedBundlerHelpers(source)) + return "its shipped JS looks minified"; if (hasTransformMarkers(source)) { return "its shipped JS carries build-transform markers (bundled/transpiled dist)"; } diff --git a/packages/compiler/src/frontend/pipeline.ts b/packages/compiler/src/frontend/pipeline.ts index 024c69d98..b11fa2b63 100644 --- a/packages/compiler/src/frontend/pipeline.ts +++ b/packages/compiler/src/frontend/pipeline.ts @@ -10,11 +10,13 @@ import { checkPreflight, isNodeTypesPath, locOf, + npmStaticLoadableFiles, requiresOf, resolveNpmImport, type LoadResult, } from "./program.js"; import { + npmStaticFileDegrades, npmStaticIneligibleReason, npmStaticOffenders, npmStaticPackageOfPath, @@ -458,6 +460,14 @@ function loadFrontend( const typecheckRefused = new Set(); while (effective.size > 0) { const reasons = new Map(npmStaticOffenders()); + // Bundler interop the rewrite could not finish refuses its package + // only where the compiled program can load the file. + if (npmStaticFileDegrades().size > 0) { + const loadable = npmStaticLoadableFiles(load.program, load.moduleOrder); + for (const [file, { pkg, reason }] of npmStaticFileDegrades()) { + if (!reasons.has(pkg) && (loadable === null || loadable.has(file))) reasons.set(pkg, reason); + } + } for (const d of preflight) { const pkg = npmStaticPackageOfPath(d.loc.file); if (pkg !== null && !reasons.has(pkg)) diff --git a/packages/compiler/src/frontend/program.ts b/packages/compiler/src/frontend/program.ts index 12cdfd76c..fa02d15f7 100644 --- a/packages/compiler/src/frontend/program.ts +++ b/packages/compiler/src/frontend/program.ts @@ -6023,6 +6023,57 @@ export function orderedImportsOf( return out; } +/** The files a compiled program can load: the module order closed over + * every literal require() and import() call it contains (deferred and + * nested ones included) and, for opted-in package JavaScript, over every + * import and export declaration as well. A package file outside this set + * is reachable only through types, so whatever it spells cannot affect + * the compiled program. Null when a reachable file loads a computed + * specifier: then any file may load. */ +export function npmStaticLoadableFiles( + program: ts.Program, + moduleOrder: readonly ts.SourceFile[], +): Set | null { + const seen = new Set(); + const work: ts.SourceFile[] = []; + const add = (sf: ts.SourceFile | null | undefined): void => { + if (sf === null || sf === undefined || seen.has(sf) || sf.isDeclarationFile) return; + seen.add(sf); + work.push(sf); + }; + for (const sf of moduleOrder) add(sf); + let computed = false; + while (work.length > 0 && !computed) { + const sf = work.pop()!; + const packageJs = npmStaticPackageOfPath(sf.fileName) !== null && isJsSourceFileName(sf.fileName); + const follow = (spec: string, kind: "import" | "require"): void => { + if (canonicalBuiltinModule(spec) !== null) return; + add(resolveImport7(program, sf, spec, kind) ?? npmStaticDepSf7(program, sf, spec, kind)); + }; + ts.walkPreorder(sf, (n) => { + if (computed) return "stop"; + if (ts.isCallExpression(n)) { + const isImport = n.expression.kind === ts.SyntaxKind.ImportKeyword; + const isRequire = ts.isIdentifier(n.expression) && n.expression.text === "require"; + if (!isImport && !isRequire) return undefined; + const arg = n.arguments.nodes[0]; + if (arg !== undefined && ts.isStringLiteralLike(arg)) + follow(arg.text, isImport ? "import" : "require"); + else if (packageJs || isImport) computed = true; + } else if ( + packageJs && + (ts.isImportDeclaration(n) || ts.isExportDeclaration(n)) && + n.moduleSpecifier !== undefined && + ts.isStringLiteral(n.moduleSpecifier) + ) { + follow(n.moduleSpecifier.text, "import"); + } + return undefined; + }); + } + return computed ? null : new Set([...seen].map((sf) => sf.fileName.split("\\").join("/"))); +} + /** The program source file a bare specifier reaches when it names an * opted-in --npm-static package (the shipped-JS entry preflight admitted * as a module edge), else null. No offender reporting here — preflight diff --git a/tests/fixtures/npm/cases/2562-subpath-leaf/main.ts b/tests/fixtures/npm/cases/2562-subpath-leaf/main.ts new file mode 100644 index 000000000..b34359cc7 --- /dev/null +++ b/tests/fixtures/npm/cases/2562-subpath-leaf/main.ts @@ -0,0 +1,10 @@ +// The program reaches gtsubpath only through its leaf data module (and a +// type from its main entry). The main entry reaches a file whose bundler +// interop the recognizer cannot verify, but no compiled code can load it, +// so the package compiles: a package is judged by what its imported +// subpaths reach. +import type { Manager } from "gtsubpath"; +import { managers } from "gtsubpath/dist/data.js"; + +const names: Manager[] = managers.filter((m) => m.lockfile.endsWith(".yaml")); +console.log(managers.length, names.map((m) => m.name).join(",")); diff --git a/tests/fixtures/npm/node_modules/gtsubpath/dist/data.d.ts b/tests/fixtures/npm/node_modules/gtsubpath/dist/data.d.ts new file mode 100644 index 000000000..356c8bbc9 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtsubpath/dist/data.d.ts @@ -0,0 +1,2 @@ +import type { Manager } from "./index.js"; +export declare const managers: Manager[]; diff --git a/tests/fixtures/npm/node_modules/gtsubpath/dist/data.js b/tests/fixtures/npm/node_modules/gtsubpath/dist/data.js new file mode 100644 index 000000000..918a296a8 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtsubpath/dist/data.js @@ -0,0 +1,6 @@ +"use strict"; +exports.managers = [ + { name: "npm", lockfile: "package-lock.json" }, + { name: "pnpm", lockfile: "pnpm-lock.yaml" }, + { name: "yarn", lockfile: "yarn.lock" }, +]; diff --git a/tests/fixtures/npm/node_modules/gtsubpath/dist/detect.js b/tests/fixtures/npm/node_modules/gtsubpath/dist/detect.js new file mode 100644 index 000000000..d129041fe --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtsubpath/dist/detect.js @@ -0,0 +1,11 @@ +// A hand-rolled __toESM the recognizer cannot verify (see gtdrift). +"use strict"; +var __toESM = (mod) => { + var ns = { default: mod }; + for (var key in mod) ns[key] = mod[key]; + return ns; +}; +var import_gtcore = __toESM(require("gtcore")); +exports.detect = function detect(files) { + return import_gtcore.default.label(files.length); +}; diff --git a/tests/fixtures/npm/node_modules/gtsubpath/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtsubpath/dist/index.d.ts new file mode 100644 index 000000000..24b05d86d --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtsubpath/dist/index.d.ts @@ -0,0 +1,6 @@ +export interface Manager { + name: string; + lockfile: string; +} +export declare const managers: Manager[]; +export declare function detect(files: string[]): string; diff --git a/tests/fixtures/npm/node_modules/gtsubpath/dist/index.js b/tests/fixtures/npm/node_modules/gtsubpath/dist/index.js new file mode 100644 index 000000000..a330d28c0 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtsubpath/dist/index.js @@ -0,0 +1,5 @@ +"use strict"; +const detect = require("./detect.js"); +const data = require("./data.js"); +exports.detect = detect.detect; +exports.managers = data.managers; diff --git a/tests/fixtures/npm/node_modules/gtsubpath/package.json b/tests/fixtures/npm/node_modules/gtsubpath/package.json new file mode 100644 index 000000000..f785cb154 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtsubpath/package.json @@ -0,0 +1,10 @@ +{ + "name": "gtsubpath", + "version": "1.0.0", + "description": "a package whose main entry reaches a file with bundler interop the recognizer cannot verify, beside a leaf data module the program imports by subpath", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "dependencies": { + "gtcore": "1.0.0" + } +} diff --git a/tests/harness/npm-cases.ts b/tests/harness/npm-cases.ts index 8562ac24f..4b3b9fdee 100644 --- a/tests/harness/npm-cases.ts +++ b/tests/harness/npm-cases.ts @@ -19,12 +19,12 @@ export function npmCases(fixturesRoot: string): NpmCase[] { return [ ...globSync(join(fixturesRoot, "npm/cases/*/main.ts")) .sort() - // 2465-2469 and 2556-2561 are the --npm-static bundler-emitted-CJS + // 2465-2469 and 2556-2562 are the --npm-static bundler-emitted-CJS // cases (npm-static.test.ts drives them with the opt-in): their // consumers exercise the static rewrite's surface (lexer-visible // names the shipped .d.ts never declares, the __toESM interop // family), so they stay out of the flagless island lane by design. - .filter((entry) => !/\/(246[5-9]|255[6-9]|256[01])-[^/]+\/main\.ts$/.test(entry)) + .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-2])-[^/]+\/main\.ts$/.test(entry)) .map((entry) => ({ name: entry.split("/").at(-2)!, entry, diff --git a/tests/harness/npm-static.test.ts b/tests/harness/npm-static.test.ts index b908f0bd9..a8df98c52 100644 --- a/tests/harness/npm-static.test.ts +++ b/tests/harness/npm-static.test.ts @@ -76,7 +76,7 @@ async function buildStatic(entry: string, npmStatic: string[] | "auto"): Promise ...globSync( join(fixturesRoot, "npm/node_modules/cryptozoo/**/*.{js,mjs,cjs,json,d.ts}"), ).sort(), - // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2561) + // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2562) ...globSync(join(fixturesRoot, "npm/node_modules/gt*/**/*.{js,cjs,json}")).sort(), ]; for (const f of inputs) hash.update(f).update(readFileSync(f)); @@ -1405,8 +1405,11 @@ describe(`npm-static pilots${sanitize ? " (sanitized)" : ""}`, () => { runBinary("node", [entry]), runBinary(binary, []), ]); - nativeRes.stderr = comparableStderr(nativeRes.stderr); - expect(nativeRes).toEqual(nodeRes); + // Node's stderr carries its type-stripping warning; the programs write + // only to stdout. + expect(nativeRes.stdout.toString("utf8")).toBe(nodeRes.stdout.toString("utf8")); + expect(comparableStderr(nativeRes.stderr).toString("utf8")).toBe(""); + expect(nativeRes.exitCode).toBe(nodeRes.exitCode); expect(nativeRes.exitCode).toBe(0); } @@ -1423,6 +1426,14 @@ describe(`npm-static pilots${sanitize ? " (sanitized)" : ""}`, () => { ]); }, 180_000); + // A whitespace/syntax-minified bundle whose helpers keep their names is + // judged by the recognizers, not refused up front as minified. + test("--npm-static=auto opts a minified bundle with named helpers in", () => { + const entry = join(fixturesRoot, "npm/cases/2558-bundled-chunks/main.ts"); + const { coverage } = analyze(entry, { npmStatic: "auto" }); + expect(coverage.npmStatic).toContainEqual({ package: "gtchunkws", status: "static" }); + }, 120_000); + test.each(["2559-cjs-cycle", "2560-cjs-cycle-partner-first"])( "the esbuild CommonJS cycle %s compiles statically and byte-matches Node", async (name) => { @@ -1446,6 +1457,15 @@ describe(`npm-static pilots${sanitize ? " (sanitized)" : ""}`, () => { ]); }, 120_000); + // 2562: a file with unverifiable interop that no compiled code can load + // (the program imports a leaf data module and only a type from the + // main entry) leaves its package static. + test("a package is judged by the files its imported subpaths can load", async () => { + await expectNodeParity(join(fixturesRoot, "npm/cases/2562-subpath-leaf/main.ts"), [ + "gtsubpath", + ]); + }, 180_000); + test("published type-only declarations coexist with a static runtime bundle", async () => { const entry = join(fixturesRoot, "npm/cases/2469-bundle-offender/main.ts"); const { coverage } = analyze(entry, { npmStatic: ["gtghost"] }); From bab3b9800165ccc0d378031d269bb1b4d50f3178 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 12:50:37 -0700 Subject: [PATCH 21/83] Record order baselines for the new npm cases Co-Authored-By: Claude Opus 5.5 --- .../fixtures/npm/cases/2558-bundled-chunks/main.ts.json | 6 ++++++ .../tests/fixtures/npm/cases/2559-cjs-cycle/main.ts.json | 6 ++++++ .../npm/cases/2560-cjs-cycle-partner-first/main.ts.json | 6 ++++++ .../npm/cases/2561-cjs-cycle-early-read/main.ts.json | 6 ++++++ .../tests/fixtures/npm/cases/2562-subpath-leaf/main.ts.json | 6 ++++++ 5 files changed, 30 insertions(+) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2558-bundled-chunks/main.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2559-cjs-cycle/main.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2560-cjs-cycle-partner-first/main.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2561-cjs-cycle-early-read/main.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2562-subpath-leaf/main.ts.json diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2558-bundled-chunks/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2558-bundled-chunks/main.ts.json new file mode 100644 index 000000000..cc5bc69c8 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2558-bundled-chunks/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2558-bundled-chunks/main.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2559-cjs-cycle/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2559-cjs-cycle/main.ts.json new file mode 100644 index 000000000..382eef7b4 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2559-cjs-cycle/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2559-cjs-cycle/main.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2560-cjs-cycle-partner-first/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2560-cjs-cycle-partner-first/main.ts.json new file mode 100644 index 000000000..f609f128e --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2560-cjs-cycle-partner-first/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2560-cjs-cycle-partner-first/main.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2561-cjs-cycle-early-read/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2561-cjs-cycle-early-read/main.ts.json new file mode 100644 index 000000000..be3b2af16 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2561-cjs-cycle-early-read/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2561-cjs-cycle-early-read/main.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2562-subpath-leaf/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2562-subpath-leaf/main.ts.json new file mode 100644 index 000000000..68cde3de1 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2562-subpath-leaf/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2562-subpath-leaf/main.ts" + ], + "diags": [] +} From 506b7994baf64a4b039bd534a1ac7bd82b4a4d39 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 12:59:49 -0700 Subject: [PATCH 22/83] Type chunk records once so the compiler compiles itself Co-Authored-By: Claude Opus 5.5 --- .../src/frontend/npm-static-bundled-chunks.ts | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts index c4a12e998..837e7c498 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts @@ -230,6 +230,16 @@ function copyableInitializer(init: ts.Expression | undefined): boolean { return depth > 0 && ts.isIdentifier(walk) && walk.text === "Object"; } +/** One recognized chunk declaration. */ +interface Chunk { + name: string; + stmt: ts.VariableStatement; + params: string[]; + body: ts.Block; + path: string; + spec: string; +} + interface TopLevel { stmt: ts.Statement; /** The declarator (or function declaration) itself. */ @@ -322,14 +332,6 @@ export function splitBundledChunks( } // The chunk declarations: `var NAME = HELPER(factory);`, one declarator. - interface Chunk { - name: string; - stmt: ts.VariableStatement; - params: string[]; - body: ts.Block; - path: string; - spec: string; - } const chunks = new Map(); const base = filePath.replace(/^.*[\\/]/, ""); for (const [name, t] of top) { @@ -470,12 +472,8 @@ function applyEdits(source: string, from: number, to: number, edits: Edit[]): st /** The virtual module text for one chunk, or a degrade reason. */ function chunkSource( sf: ts.SourceFile, - chunk: { - name: string; - params: string[]; - body: ts.Block; - }, - chunks: ReadonlyMap, + chunk: Chunk, + chunks: ReadonlyMap, top: ReadonlyMap, stable: (name: string) => boolean, callEdits: readonly Edit[], From 40ca92fa13c56e2693da73c4904ce80debf616e9 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 13:01:47 -0700 Subject: [PATCH 23/83] Format Co-Authored-By: Claude Opus 5.5 --- .../compiler/src/frontend/npm-static-bundled-chunks.test.ts | 4 +++- packages/compiler/src/frontend/npm-static-bundled-chunks.ts | 5 ++++- packages/compiler/src/frontend/pipeline.ts | 3 ++- packages/compiler/src/frontend/program.ts | 3 ++- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts index 3ef430102..82926d2b8 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts @@ -178,5 +178,7 @@ export const g = import_g.g; `, "/pkg/dist/index.js", ); - expect(result).toEqual({ degrade: expect.stringContaining("bundled CommonJS modules") as string }); + expect(result).toEqual({ + degrade: expect.stringContaining("bundled CommonJS modules") as string, + }); }); diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts index 837e7c498..cdb9dff64 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts @@ -258,7 +258,10 @@ function strictPrologue(stmts: readonly ts.Statement[]): boolean { /** A cheap text probe: the helper's name, or its minified body's * `(m={exports:{}}).exports,m` fragment. */ export function mayBundleChunks(source: string): boolean { - return source.includes("__commonJS") || /\{\s*exports\s*:\s*\{\s*\}\s*\}\s*\)\s*\.exports\s*,/.test(source); + return ( + source.includes("__commonJS") || + /\{\s*exports\s*:\s*\{\s*\}\s*\}\s*\)\s*\.exports\s*,/.test(source) + ); } /** Splits a CommonJS bundle's __commonJS chunks into virtual modules (see diff --git a/packages/compiler/src/frontend/pipeline.ts b/packages/compiler/src/frontend/pipeline.ts index b11fa2b63..676b2bb3e 100644 --- a/packages/compiler/src/frontend/pipeline.ts +++ b/packages/compiler/src/frontend/pipeline.ts @@ -465,7 +465,8 @@ function loadFrontend( if (npmStaticFileDegrades().size > 0) { const loadable = npmStaticLoadableFiles(load.program, load.moduleOrder); for (const [file, { pkg, reason }] of npmStaticFileDegrades()) { - if (!reasons.has(pkg) && (loadable === null || loadable.has(file))) reasons.set(pkg, reason); + if (!reasons.has(pkg) && (loadable === null || loadable.has(file))) + reasons.set(pkg, reason); } } for (const d of preflight) { diff --git a/packages/compiler/src/frontend/program.ts b/packages/compiler/src/frontend/program.ts index fa02d15f7..908a587ea 100644 --- a/packages/compiler/src/frontend/program.ts +++ b/packages/compiler/src/frontend/program.ts @@ -6045,7 +6045,8 @@ export function npmStaticLoadableFiles( let computed = false; while (work.length > 0 && !computed) { const sf = work.pop()!; - const packageJs = npmStaticPackageOfPath(sf.fileName) !== null && isJsSourceFileName(sf.fileName); + const packageJs = + npmStaticPackageOfPath(sf.fileName) !== null && isJsSourceFileName(sf.fileName); const follow = (spec: string, kind: "import" | "require"): void => { if (canonicalBuiltinModule(spec) !== null) return; add(resolveImport7(program, sf, spec, kind) ?? npmStaticDepSf7(program, sf, spec, kind)); From f42fd146d9eaff8d192d9326ccbd97d7499f0997 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 14:00:23 -0700 Subject: [PATCH 24/83] Lower esbuild's lazy ES modules; select @types-typed packages by default An __esm initializer becomes a run-once closure over its body in the bundle's own scope. Interop bindings it assigns once from require() become that require at the assignment, with their member reads respelled as reads of the required module; a namespace read only through members becomes an object literal of getters over the module's live bindings. Asynchronous initializers, initializer results, and namespaces kept as values (a memoized lazy loader) stay refused with a message naming them. The default selection now also compiles a package typed by a DefinitelyTyped twin that describes its installed release (same major, or major.minor below 1.0), and untyped dependencies of compiled packages. A twin for another release is refused with both versions. Co-Authored-By: Claude Opus 5.5 --- .../src/frontend/npm-static-bundled-chunks.ts | 89 +-- .../frontend/npm-static-bundled-esm.test.ts | 149 +++++ .../src/frontend/npm-static-bundled-esm.ts | 582 ++++++++++++++++++ .../src/frontend/npm-static-rewrite-host.ts | 12 + .../src/frontend/npm-static-rewrite-syntax.ts | 1 + packages/compiler/src/frontend/npm-static.ts | 53 +- packages/compiler/src/frontend/pipeline.ts | 21 +- packages/compiler/src/frontend/services.ts | 10 +- .../node_modules/@types/twinstale/index.d.ts | 1 + .../@types/twinstale/package.json | 5 + .../node_modules/twinstale/index.js | 6 + .../node_modules/twinstale/package.json | 5 + tests/fixtures/npm-static/twinstale-cli.ts | 5 + .../npm/cases/2563-bundled-esm-inits/main.ts | 16 + .../npm/node_modules/gtesm/dist/index.d.ts | 2 + .../npm/node_modules/gtesm/dist/index.js | 90 +++ .../npm/node_modules/gtesm/package.json | 10 + .../npm/node_modules/gtesmws/dist/index.d.ts | 2 + .../npm/node_modules/gtesmws/dist/index.js | 1 + .../npm/node_modules/gtesmws/package.json | 10 + tests/harness/npm-cases.ts | 4 +- tests/harness/npm-static.test.ts | 54 +- 22 files changed, 1064 insertions(+), 64 deletions(-) create mode 100644 packages/compiler/src/frontend/npm-static-bundled-esm.test.ts create mode 100644 packages/compiler/src/frontend/npm-static-bundled-esm.ts create mode 100644 tests/fixtures/npm-static/node_modules/@types/twinstale/index.d.ts create mode 100644 tests/fixtures/npm-static/node_modules/@types/twinstale/package.json create mode 100644 tests/fixtures/npm-static/node_modules/twinstale/index.js create mode 100644 tests/fixtures/npm-static/node_modules/twinstale/package.json create mode 100644 tests/fixtures/npm-static/twinstale-cli.ts create mode 100644 tests/fixtures/npm/cases/2563-bundled-esm-inits/main.ts create mode 100644 tests/fixtures/npm/node_modules/gtesm/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtesm/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtesm/package.json create mode 100644 tests/fixtures/npm/node_modules/gtesmws/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtesmws/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtesmws/package.json diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts index cdb9dff64..28fe4a1b9 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts @@ -31,7 +31,7 @@ export interface BundledChunkSplit { chunks: { path: string; source: string }[]; } -interface Edit { +export interface Edit { start: number; end: number; text: string; @@ -42,11 +42,11 @@ const CHUNK_SHAPE_DEGRADE = "recognizer cannot verify"; /** The helper a counter-suffixed bundler name spells (`__commonJS2`). */ -function helperBase(name: string): string { +export function helperBase(name: string): string { return /^__[A-Za-z]+\d+$/.test(name) ? name.replace(/\d+$/, "") : name; } -function unparen(e: ts.Expression): ts.Expression { +export function unparen(e: ts.Expression): ts.Expression { while (ts.isParenthesizedExpression(e)) e = e.expression; return e; } @@ -54,7 +54,7 @@ function unparen(e: ts.Expression): ts.Expression { /** True for an identifier occurrence that reads or writes a binding (as * opposed to a property name, a label, or a declaration's own name). * Shorthand property values count: `{ x }` reads x. */ -function isReference(id: ts.Identifier): boolean { +export function isReference(id: ts.Identifier): boolean { const p = id.parent; if (p === undefined) return false; if (ts.isPropertyAccessExpression(p)) return p.expression === id; @@ -83,7 +83,7 @@ function isReference(id: ts.Identifier): boolean { /** Names a subtree declares anywhere inside it: variables (destructuring * included), parameters, function and class names, catch bindings. */ -function declaredNames(root: ts.Node): Set { +export function declaredNames(root: ts.Node): Set { const names = new Set(); // Binding patterns nest BindingElements, which the walk visits too. ts.walkPreorder(root, (n) => { @@ -106,7 +106,7 @@ function declaredNames(root: ts.Node): Set { } /** Every binding reference inside a subtree. */ -function references(root: ts.Node): ts.Identifier[] { +export function references(root: ts.Node): ts.Identifier[] { const out: ts.Identifier[] = []; ts.walkPreorder(root, (n) => { if (n.kind >= ts.SyntaxKind.FirstJSDocNode && n.kind <= ts.SyntaxKind.LastJSDocNode) @@ -119,7 +119,7 @@ function references(root: ts.Node): ts.Identifier[] { /** True when the reference is written (assignment target, update operand, * loop target, delete operand). */ -function isWritten(id: ts.Identifier): boolean { +export function isWritten(id: ts.Identifier): boolean { let child: ts.Node = id; for (let p = child.parent; p !== undefined; child = p, p = p.parent) { if (ts.isBinaryExpression(p)) { @@ -247,7 +247,7 @@ interface TopLevel { } /** True when the file opens with a "use strict" directive prologue. */ -function strictPrologue(stmts: readonly ts.Statement[]): boolean { +export function strictPrologue(stmts: readonly ts.Statement[]): boolean { for (const s of stmts) { if (!ts.isExpressionStatement(s) || !ts.isStringLiteral(s.expression)) return false; if (s.expression.text === "use strict") return true; @@ -430,7 +430,7 @@ export function splitBundledChunks( return { text: applyEdits(source, 0, source.length, bundleEdits), chunks: out }; } -function countDeclarations(sf: ts.SourceFile, name: string): number { +export function countDeclarations(sf: ts.SourceFile, name: string): number { let n = 0; ts.walkPreorder(sf, (node) => { if ( @@ -454,7 +454,7 @@ function countDeclarations(sf: ts.SourceFile, name: string): number { /** Applies non-overlapping edits inside [from, to): replaced spans keep * their newlines (blanked text stays line-aligned); edits nested in a * blanked span vanish with it. */ -function applyEdits(source: string, from: number, to: number, edits: Edit[]): string { +export function applyEdits(source: string, from: number, to: number, edits: Edit[]): string { const inside = edits .filter((e) => e.start >= from && e.end <= to) .sort((a, b) => a.start - b.start || b.end - a.end); @@ -472,37 +472,13 @@ function applyEdits(source: string, from: number, to: number, edits: Edit[]): st return text + source.slice(at, to); } -/** The virtual module text for one chunk, or a degrade reason. */ -function chunkSource( - sf: ts.SourceFile, - chunk: Chunk, - chunks: ReadonlyMap, - top: ReadonlyMap, - stable: (name: string) => boolean, - callEdits: readonly Edit[], - bundleStrict: boolean, -): string | { degrade: string } { - const why = (reason: string): { degrade: string } => ({ - degrade: `its bundled CommonJS module '${chunk.name}' ${reason}`, - }); - const [exportsParam, moduleParam] = chunk.params; - const body = chunk.body; - const local = declaredNames(body); - for (const p of chunk.params) { - if ( - local.has(p) || - (p !== "exports" && local.has("exports")) || - (p !== "module" && local.has("module")) - ) - return why( - "rebinds its module parameters inside its body (an identifier-minified bundle the recognizer cannot respell)", - ); - } - - /* The factory's own frame must stay unobservable: its receiver, its - * arguments object, and a top-level return have no module equivalent. */ +/** How code in a factory body (a chunk or a lazy ES-module initializer) + * observes the factory's own frame, or null: its receiver, its arguments + * object, `super`, a meta property, or (when `returns`) a top-level + * return have no module-scope equivalent. */ +export function factoryFrameUse(body: ts.Block, returns: boolean): string | null { let frameUse: string | null = null; - /** The innermost frame boundary between a node and the chunk body. */ + /** The innermost frame boundary between a node and the factory body. */ const frameOf = (n: ts.Node): "factory" | "arrow" | "function" => { let arrow = false; for (let p = n.parent; p !== undefined && p !== body; p = p.parent) { @@ -532,12 +508,43 @@ function chunkSource( use = "reads its factory's arguments object"; else if (ts.isMetaProperty(n)) use = "reads a meta property at its top level"; else if (ts.isReturnStatement(n)) { - if (frameOf(n) === "factory") frameUse = "returns from its top level"; + if (returns && frameOf(n) === "factory") frameUse = "returns from its top level"; return undefined; } if (use !== null && frameOf(n) !== "function") frameUse = use; return undefined; }); + return frameUse; +} + +/** The virtual module text for one chunk, or a degrade reason. */ +function chunkSource( + sf: ts.SourceFile, + chunk: Chunk, + chunks: ReadonlyMap, + top: ReadonlyMap, + stable: (name: string) => boolean, + callEdits: readonly Edit[], + bundleStrict: boolean, +): string | { degrade: string } { + const why = (reason: string): { degrade: string } => ({ + degrade: `its bundled CommonJS module '${chunk.name}' ${reason}`, + }); + const [exportsParam, moduleParam] = chunk.params; + const body = chunk.body; + const local = declaredNames(body); + for (const p of chunk.params) { + if ( + local.has(p) || + (p !== "exports" && local.has("exports")) || + (p !== "module" && local.has("module")) + ) + return why( + "rebinds its module parameters inside its body (an identifier-minified bundle the recognizer cannot respell)", + ); + } + + const frameUse = factoryFrameUse(body, true); if (frameUse !== null) return why(frameUse); const edits: Edit[] = callEdits.filter((e) => e.start >= body.pos && e.end <= body.end); diff --git a/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts b/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts new file mode 100644 index 000000000..9c064cb55 --- /dev/null +++ b/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts @@ -0,0 +1,149 @@ +import { spawnSync } from "node:child_process"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterAll, expect, test } from "vitest"; +import { nodeFrontendServices } from "./services-node.js"; + +const helpers = `var __getOwnPropNames = Object.getOwnPropertyNames; +var __esm = (fn, res) => function __init() { + return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; +}; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __create = Object.create; +var __getProtoOf = Object.getPrototypeOf; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( + isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, + mod +)); +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +`; + +const dirs: string[] = []; +afterAll(() => { + for (const dir of dirs) rmSync(dir, { recursive: true, force: true }); +}); + +function lower(source: string, file = "/pkg/dist/index.js") { + return nodeFrontendServices().lowerEsmInits(source, file); +} + +function run(file: string) { + const result = spawnSync(process.execPath, [file], { encoding: "utf8" }); + return { status: result.status, stdout: result.stdout, stderr: result.stderr }; +} + +function expectSameBehavior(source: string): string { + const dir = mkdtempSync(join(tmpdir(), "scriptc-esm-inits-")); + dirs.push(dir); + const original = join(dir, "original.js"); + const lowered = join(dir, "index.js"); + writeFileSync(original, source); + const result = lower(source, lowered); + expect(typeof result).toBe("string"); + writeFileSync(lowered, result as string); + const oracle = run(original); + expect(oracle.status).toBe(0); + expect(run(lowered)).toEqual(oracle); + return result as string; +} + +const lazyModule = ` +function helper(x) { COUNT++; return "[" + x + "]"; } +var COUNT; +var init_helper = __esm({ + "src/helper.ts"() { + console.log("helper init"); + COUNT = 0; + } +}); +var lazy_exports = {}; +__export(lazy_exports, { + VALUE: () => VALUE, + value: () => value +}); +function value() { + return VALUE + ":" + COUNT + ":" + import_os.default.EOL.length; +} +var path, import_os, VALUE; +var init_lazy = __esm({ + "src/lazy.ts"() { + path = __toESM(require("path")); + import_os = __toESM(require("os")); + init_helper(); + console.log("lazy init"); + VALUE = helper(path.basename("/a/b.txt")); + } +}); +`; + +test("initializers run once, at their first call, and namespaces read live bindings", () => { + const lowered = expectSameBehavior(`"use strict"; +${helpers}${lazyModule} +console.log("start"); +function load() { + const lazy = (init_lazy(), __toCommonJS(lazy_exports)); + return lazy.value() + " " + lazy.VALUE; +} +console.log(load()); +COUNT = 5; +console.log(load(), (init_lazy(), __toCommonJS(lazy_exports)).value()); +`); + expect(lowered).not.toContain("__esm({"); + expect(lowered).not.toContain("import_os"); + expect(lowered).toContain('(require("os")).EOL'); +}); + +test("minified initializers sharing a var statement lower the same way", () => { + expectSameBehavior( + helpers + + `var x_exports={};__export(x_exports,{v:()=>v});var v,import_path,init_x=__esm({"x.ts"(){"use strict";import_path=__toESM(require("path")),v=import_path.default.sep}});` + + `console.log((init_x(),__toCommonJS(x_exports)).v);`, + ); +}); + +test.each([ + [ + "is asynchronous", + `var init_a = __esm({ async "a.ts"() { await 0; } });\ninit_a();`, + ], + [ + "is used other than as a statement call", + `var init_a = __esm({ "a.ts"() {} });\nvar r = init_a();`, + ], + [ + "reads its factory's receiver", + `var init_a = __esm({ "a.ts"() { console.log(this); } });\ninit_a();`, + ], + [ + "stores the namespace of a bundled ES module ('a_exports') in a variable", + `var a_exports = {};\n__export(a_exports, { v: () => v });\nvar v, init_a = __esm({ "a.ts"() { v = 1; } });\nvar ns, load = () => ns ??= __toESM((init_a(), __toCommonJS(a_exports)));\nload();`, + ], + [ + "reads the namespace of a bundled ES module ('a_exports')", + `var a_exports = {};\n__export(a_exports, { v: () => v });\nvar v, init_a = __esm({ "a.ts"() { v = 1; } });\nconsole.log(Object.keys((init_a(), __toCommonJS(a_exports))));`, + ], +])("an initializer that %s keeps the package refused", (reason, code) => { + expect(lower(`${helpers}${code}\n`)).toEqual({ + degrade: expect.stringContaining(reason) as string, + }); +}); + +test("files without __esm initializers and ES-module bundles are left alone", () => { + expect(lower("exports.x = 1;\n")).toBeNull(); + expect(lower(`${helpers}var init_a = __esm({ "a.ts"() {} });\ninit_a();\nexport {};\n`)).toBeNull(); +}); diff --git a/packages/compiler/src/frontend/npm-static-bundled-esm.ts b/packages/compiler/src/frontend/npm-static-bundled-esm.ts new file mode 100644 index 000000000..ae5985e51 --- /dev/null +++ b/packages/compiler/src/frontend/npm-static-bundled-esm.ts @@ -0,0 +1,582 @@ +/* Lazily initialized ES modules in a CommonJS bundle. esbuild scope-hoists + * every ES module it bundles into the bundle's own scope; one that some + * code require()s instead of importing runs from a lazy initializer, + * + * var COUNT, import_fs; + * var init_x = __esm({ "src/x.ts"() { import_fs = __toESM(require("fs")); COUNT = 0; } }); + * + * and the requiring site reads its namespace as + * `(init_x(), __toCommonJS(x_exports))`. `init_x()` runs the body once (a + * call during the body answers immediately). The pass below keeps that + * shape in the bundle's scope and respells only what has no static + * representation: + * + * - the initializer becomes a run-once closure over the same body; + * - an interop binding the body assigns once from require() (`import_fs` + * above) becomes that require at the assignment's position, and its + * member reads become member reads of the required module, exactly + * like the top-level interop erasure (npm-static-rewrite-syntax.ts); + * - a namespace read only through member reads becomes an object + * literal of getters over the module's live bindings (what + * __toCommonJS copies, minus the non-enumerable __esModule mark no + * member read can reach). + * + * The respelled interop reads resolve the required module even if they + * ran before the initializer, where Node would read a member of + * undefined and throw; esbuild only reaches them through the module's + * own code, which runs after its initializer. Shapes outside the + * recognized forms answer a degrade reason. */ +import * as ts from "./ts7/syntax.js"; +import { bareRequireSpecOf } from "./cjs-syntax.js"; +import { helperTokens } from "./helper-tokens.js"; +import { + applyEdits, + countDeclarations, + declaredNames, + factoryFrameUse, + helperBase, + isWritten, + references, + unparen, + type Edit, +} from "./npm-static-bundled-chunks.js"; + +export interface EsmInitHost { + requireTargetEsModuleStamped(fromFile: string, specifier: string): boolean; +} + +const ESM_SHAPE_DEGRADE = + "its shipped JS initializes bundled ES modules through the __esm helper in a shape the " + + "recognizer cannot verify"; + +/* The recognized __esm forms over its parameter names (FN, RES) and the + * own-key reader (KEYS): keyed `(FN, RES) => function [name]() { return + * ; }` with a one-method object argument, and arrow + * `(FN, RES) => () => ()` with a function argument. */ +const KEYED_RETURN = "FN && (RES = (0, FN[KEYS(FN)[0]])(FN = 0)), RES"; +const ARROW_BODY = "(FN && (RES = FN(FN = 0)), RES)"; + +function template(text: string, fn: string, res: string, keys: string): string | null { + return helperTokens( + text + .replace(/\bFN\b/g, fn) + .replace(/\bRES\b/g, res) + .replace(/\bKEYS\b/g, keys), + ); +} + +function esmHelperForm( + init: ts.Expression, + sf: ts.SourceFile, + ownNames: (name: string) => boolean, +): "keyed" | "arrow" | null { + const fn = unparen(init); + if (!ts.isArrowFunction(fn) || fn.parameters.nodes.length !== 2 || ts.isBlock(fn.body)) + return null; + const [p0, p1] = fn.parameters.nodes; + if ( + !p0 || + !p1 || + !ts.isIdentifier(p0.name) || + !ts.isIdentifier(p1.name) || + p0.initializer || + p1.initializer || + p0.name.text === p1.name.text + ) + return null; + const [f, r] = [p0.name.text, p1.name.text]; + const body = unparen(fn.body); + if (ts.isFunctionExpression(body)) { + if (body.parameters.nodes.length !== 0 || body.asteriskToken || body.modifiers?.nodes.length) + return null; + const stmts = body.body.statements.nodes; + const ret = stmts[0]; + if (stmts.length !== 1 || !ret || !ts.isReturnStatement(ret) || !ret.expression) return null; + const text = ret.expression.getText(sf); + const keys = /\[\s*([A-Za-z_$][\w$]*(?:\.keys)?)\s*\(/.exec(text)?.[1]; + if (keys === undefined || (keys !== "Object.keys" && !ownNames(keys))) return null; + return helperTokens(text) === template(KEYED_RETURN, f, r, keys) ? "keyed" : null; + } + if (ts.isArrowFunction(body) && body.parameters.nodes.length === 0 && !ts.isBlock(body.body)) { + const actual = helperTokens(body.body.getText(sf)); + return actual !== null && actual === template(ARROW_BODY, f, r, "") ? "arrow" : null; + } + return null; +} + +/** Pads the given declarators out of their statements (separating commas + * included; a statement whose declarators all go pads whole). */ +function removeDeclarators(sf: ts.SourceFile, dead: ReadonlySet): Edit[] { + const edits: Edit[] = []; + const stmts = new Set(); + for (const d of dead) { + const stmt = d.parent?.parent; + if (stmt !== undefined && ts.isVariableStatement(stmt)) stmts.add(stmt); + } + for (const stmt of stmts) { + const decls = stmt.declarationList.declarations.nodes; + const deadAt = decls.map((d) => dead.has(d)); + if (deadAt.every((x) => x)) { + edits.push({ start: stmt.getStart(sf), end: stmt.getEnd(), text: "" }); + continue; + } + const lastLive = deadAt.lastIndexOf(false); + for (let i = 0; i < decls.length; i++) { + if (!deadAt[i]) continue; + edits.push( + i < lastLive + ? { start: decls[i]!.getStart(sf), end: decls[i + 1]!.getStart(sf), text: "" } + : { start: decls[lastLive]!.getEnd(), end: decls[i]!.getEnd(), text: "" }, + ); + } + } + return edits; +} + +/** The elements of an expression statement's comma chain, in order. */ +function commaElements(e: ts.Expression): ts.Expression[] { + const out: ts.Expression[] = []; + const visit = (x: ts.Expression): void => { + if (ts.isBinaryExpression(x) && x.operatorToken.kind === ts.SyntaxKind.CommaToken) { + visit(x.left); + visit(x.right); + } else out.push(x); + }; + visit(e); + return out; +} + +/** True when a call's result is discarded: an expression statement or a + * non-final comma operand (parentheses included). */ +function resultUnused(call: ts.Expression): boolean { + let child: ts.Node = call; + let p = call.parent; + while (p !== undefined && ts.isParenthesizedExpression(p)) { + child = p; + p = p.parent; + } + if (p === undefined) return false; + if (ts.isExpressionStatement(p)) return true; + if (ts.isBinaryExpression(p) && p.operatorToken.kind === ts.SyntaxKind.CommaToken) { + if (p.left === child) return true; + return resultUnused(p); + } + return false; +} + +/** True when a member access is a plain read (not written, deleted or + * updated). */ +function plainRead(access: ts.Expression): boolean { + const p = access.parent; + if (p === undefined) return true; + if (ts.isDeleteExpression(p)) return false; + if ( + ts.isBinaryExpression(p) && + p.left === access && + p.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && + p.operatorToken.kind <= ts.SyntaxKind.LastAssignment + ) + return false; + if ( + (ts.isPrefixUnaryExpression(p) || ts.isPostfixUnaryExpression(p)) && + (p.operator === ts.SyntaxKind.PlusPlusToken || p.operator === ts.SyntaxKind.MinusMinusToken) + ) + return false; + return true; +} + +/** The `__export(T, { name: () => value, … })` getter table of T, or null. */ +function exportTableOf( + call: ts.CallExpression, +): { target: string; entries: [string, ts.Expression][] } | null { + if (!ts.isIdentifier(call.expression) || helperBase(call.expression.text) !== "__export") + return null; + const [target, table] = call.arguments.nodes; + if ( + call.arguments.nodes.length !== 2 || + !target || + !table || + !ts.isIdentifier(target) || + !ts.isObjectLiteralExpression(table) + ) + return null; + const entries: [string, ts.Expression][] = []; + for (const prop of table.properties.nodes) { + if (!ts.isPropertyAssignment(prop)) return null; + if (!ts.isIdentifier(prop.name) && !ts.isStringLiteral(prop.name)) return null; + const getter = unparen(prop.initializer); + if (!ts.isArrowFunction(getter) || getter.parameters.nodes.length !== 0) return null; + if (ts.isBlock(getter.body)) return null; + entries.push([prop.name.text, getter.body]); + } + return { target: target.text, entries }; +} + +/** Lowers a CommonJS bundle's __esm initializers (see the header). Null: + * nothing to lower. */ +export function lowerBundledEsmInits( + sf: ts.SourceFile, + filePath: string, + host: EsmInitHost, +): string | { degrade: string } | null { + const source = sf.text; + if (!source.includes("__esm")) return null; + if (!filePath.endsWith(".cjs") && ts.isExternalModule(sf)) return null; + + interface Top { + stmt: ts.Statement; + node: ts.VariableDeclaration | ts.FunctionDeclaration; + } + const top = new Map(); + const duplicate = new Set(); + for (const stmt of sf.statements.nodes) { + const add = (name: string, node: Top["node"]): void => { + if (top.has(name)) duplicate.add(name); + else top.set(name, { stmt, node }); + }; + if (ts.isFunctionDeclaration(stmt) && stmt.name !== undefined) add(stmt.name.text, stmt); + else if (ts.isVariableStatement(stmt)) { + for (const d of stmt.declarationList.declarations.nodes) { + if (ts.isIdentifier(d.name)) add(d.name.text, d); + } + } + } + const helpers = [...top.keys()].filter((n) => helperBase(n) === "__esm"); + if (helpers.length === 0) return null; + + const allRefs = references(sf); + const writes = new Map(); + for (const r of allRefs) if (isWritten(r)) writes.set(r.text, (writes.get(r.text) ?? 0) + 1); + const declaredAnywhere = declaredNames(sf); + if (declaredAnywhere.has("Object") || writes.has("Object")) return { degrade: ESM_SHAPE_DEGRADE }; + const stable = (name: string): boolean => !duplicate.has(name) && !writes.has(name); + const ownNames = (name: string): boolean => { + const t = top.get(name); + const init = t !== undefined && ts.isVariableDeclaration(t.node) ? t.node.initializer : undefined; + return ( + init !== undefined && + stable(name) && + helperTokens(init.getText(sf)) === helperTokens("Object.getOwnPropertyNames") + ); + }; + const forms = new Map(); + for (const name of helpers) { + const t = top.get(name)!; + const init = ts.isVariableDeclaration(t.node) ? t.node.initializer : undefined; + const form = init !== undefined && stable(name) ? esmHelperForm(init, sf, ownNames) : null; + if (form === null) return { degrade: ESM_SHAPE_DEGRADE }; + forms.set(name, form); + } + + // The initializers: `NAME = HELPER(factory)` declarators. + interface Init { + name: string; + call: ts.CallExpression; + body: ts.Block; + } + const inits = new Map(); + for (const [name, t] of top) { + if (!ts.isVariableDeclaration(t.node) || t.node.initializer === undefined) continue; + const call = unparen(t.node.initializer); + if (!ts.isCallExpression(call) || !ts.isIdentifier(call.expression)) continue; + const form = forms.get(call.expression.text); + if (form === undefined) continue; + if (call.arguments.nodes.length !== 1 || !stable(name)) return { degrade: ESM_SHAPE_DEGRADE }; + let factory: ts.Node = call.arguments.nodes[0]!; + if (form === "keyed") { + if (!ts.isObjectLiteralExpression(factory) || factory.properties.nodes.length !== 1) + return { degrade: ESM_SHAPE_DEGRADE }; + factory = factory.properties.nodes[0]!; + if (!ts.isMethodDeclaration(factory) || ts.isComputedPropertyName(factory.name)) + return { degrade: ESM_SHAPE_DEGRADE }; + } else { + factory = unparen(factory as ts.Expression); + if (!ts.isArrowFunction(factory) && !ts.isFunctionExpression(factory)) + return { degrade: ESM_SHAPE_DEGRADE }; + } + const fn = factory as ts.MethodDeclaration | ts.ArrowFunction | ts.FunctionExpression; + if (fn.modifiers?.nodes.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword)) + return { + degrade: `its bundled ES module initializer '${name}' is asynchronous (top-level await), which the lazy initialization rewrite does not cover`, + }; + if ( + fn.body === undefined || + !ts.isBlock(fn.body) || + fn.asteriskToken !== undefined || + (fn.modifiers?.nodes.length ?? 0) > 0 || + fn.parameters.nodes.length > 0 + ) + return { degrade: ESM_SHAPE_DEGRADE }; + const frame = factoryFrameUse(fn.body, false); + if (frame !== null) + return { degrade: `its bundled ES module initializer '${name}' ${frame}` }; + inits.set(name, { name, call, body: fn.body }); + } + if (inits.size === 0) return null; + + const edits: Edit[] = []; + // Every initializer reference is a direct call whose result is unused. + for (const r of allRefs) { + if (!inits.has(r.text)) continue; + const call = r.parent; + if ( + call === undefined || + !ts.isCallExpression(call) || + call.expression !== r || + call.arguments.nodes.length !== 0 || + !resultUnused(call) + ) + return { + degrade: `its bundled ES module initializer '${r.text}' is used other than as a statement call`, + }; + } + for (const name of inits.keys()) { + if (countDeclarations(sf, name) !== 1) return { degrade: ESM_SHAPE_DEGRADE }; + } + + /* Interop bindings an initializer assigns once from require(). */ + const refsByName = new Map(); + for (const r of allRefs) { + const list = refsByName.get(r.text); + if (list === undefined) refsByName.set(r.text, [r]); + else list.push(r); + } + const deadDeclarators = new Set(); + for (const init of inits.values()) { + for (const stmt of init.body.statements.nodes) { + if (!ts.isExpressionStatement(stmt)) continue; + for (const el of commaElements(stmt.expression)) { + if ( + !ts.isBinaryExpression(el) || + el.operatorToken.kind !== ts.SyntaxKind.EqualsToken || + !ts.isIdentifier(el.left) + ) + continue; + const name = el.left.text; + const decl = top.get(name)?.node; + if ( + decl === undefined || + !ts.isVariableDeclaration(decl) || + decl.initializer !== undefined || + duplicate.has(name) || + writes.get(name) !== 1 || + countDeclarations(sf, name) !== 1 + ) + continue; + let rhs = unparen(el.right); + let isNodeMode = false; + let interop = false; + if ( + ts.isCallExpression(rhs) && + ts.isIdentifier(rhs.expression) && + helperBase(rhs.expression.text) === "__toESM" + ) { + const [arg, mode] = rhs.arguments.nodes; + if (!arg || rhs.arguments.nodes.length > 2) continue; + if (mode !== undefined) { + if (ts.isNumericLiteral(mode)) isNodeMode = Number(mode.text) !== 0; + else if (mode.kind === ts.SyntaxKind.TrueKeyword) isNodeMode = true; + else if (mode.kind !== ts.SyntaxKind.FalseKeyword) continue; + } + rhs = unparen(arg); + interop = true; + } + const spec = bareRequireSpecOf(rhs); + if (spec === null) continue; + const req = rhs.getText(sf); + const defaultIsModule = + interop && (isNodeMode || !host.requireTargetEsModuleStamped(filePath, spec)); + // Every other reference must be a plain member read. + const uses: Edit[] = []; + let ok = true; + for (const r of refsByName.get(name) ?? []) { + if (r === el.left) continue; + const access = r.parent; + if ( + access === undefined || + !ts.isPropertyAccessExpression(access) || + access.expression !== r || + !plainRead(access) + ) { + ok = false; + break; + } + if (defaultIsModule && access.name.text === "default") + uses.push({ start: access.getStart(sf), end: access.getEnd(), text: `(${req})` }); + else uses.push({ start: r.getStart(sf), end: r.getEnd(), text: req }); + } + if (!ok) continue; + edits.push({ start: el.getStart(sf), end: el.getEnd(), text: req }, ...uses); + deadDeclarators.add(decl); + } + } + } + edits.push(...removeDeclarators(sf, deadDeclarators)); + + /* Namespaces of lazily initialized modules read through members. */ + let mainTarget: string | null = null; + for (const stmt of sf.statements.nodes) { + if (!ts.isExpressionStatement(stmt) || !ts.isBinaryExpression(stmt.expression)) continue; + const e = stmt.expression; + const rhs = e.right; + if ( + e.operatorToken.kind === ts.SyntaxKind.EqualsToken && + ts.isPropertyAccessExpression(e.left) && + ts.isIdentifier(e.left.expression) && + e.left.expression.text === "module" && + e.left.name.text === "exports" && + ts.isCallExpression(rhs) && + ts.isIdentifier(rhs.expression) && + helperBase(rhs.expression.text) === "__toCommonJS" && + rhs.arguments.nodes.length === 1 && + ts.isIdentifier(rhs.arguments.nodes[0]!) + ) + mainTarget = (rhs.arguments.nodes[0] as ts.Identifier).text; + } + const tables = new Map(); + for (const stmt of sf.statements.nodes) { + if (!ts.isExpressionStatement(stmt) || !ts.isCallExpression(stmt.expression)) continue; + const table = exportTableOf(stmt.expression); + if (table !== null && table.target !== mainTarget) { + if (tables.has(table.target)) return { degrade: ESM_SHAPE_DEGRADE }; + tables.set(table.target, { stmt, entries: table.entries }); + } + } + const lowered = new Set(); + for (const r of allRefs) { + const table = tables.get(r.text); + if (table === undefined) continue; + const p = r.parent; + // The table's own declaration and __export call are handled below. + if (p !== undefined && ts.isVariableDeclaration(p) && p.name === r) continue; + if ( + p !== undefined && + ts.isCallExpression(p) && + p.arguments.nodes[0] === r && + ts.isExpressionStatement(p.parent) && + p.parent === table.stmt + ) + continue; + const why = `reads the namespace of a bundled ES module ('${r.text}') in a way the static rewrite cannot follow (only member reads of its exports compile)`; + if ( + p === undefined || + !ts.isCallExpression(p) || + !ts.isIdentifier(p.expression) || + helperBase(p.expression.text) !== "__toCommonJS" || + p.arguments.nodes.length !== 1 + ) + return { degrade: `its shipped JS ${why}` }; + // The namespace expression: `(init_x(), __toCommonJS(T))` or the call. + let ns: ts.Expression = p; + for (;;) { + const up = ns.parent; + if (up !== undefined && ts.isParenthesizedExpression(up)) ns = up; + else if ( + up !== undefined && + ts.isBinaryExpression(up) && + up.operatorToken.kind === ts.SyntaxKind.CommaToken && + up.right === ns + ) + ns = up; + else break; + } + const names = new Set(table.entries.map(([n]) => n)); + const memberRead = (access: ts.Node | undefined, of: ts.Node): boolean => + access !== undefined && + ts.isPropertyAccessExpression(access) && + access.expression === of && + names.has(access.name.text) && + plainRead(access); + const holder = ns.parent; + let readable = memberRead(holder, ns); + if ( + !readable && + holder !== undefined && + ts.isVariableDeclaration(holder) && + holder.initializer === ns && + ts.isIdentifier(holder.name) + ) { + const local = holder.name.text; + const localRefs = (refsByName.get(local) ?? []).filter((x) => x !== holder.name); + readable = + countDeclarations(sf, local) === 1 && + !writes.has(local) && + localRefs.every((x) => memberRead(x.parent, x)); + } + if (!readable) { + // A namespace kept for later (`x ??= __toESM(ns)`, a hand-rolled + // lazy loader) is a value with no static representation. + let outer: ts.Node | undefined = holder; + while ( + outer !== undefined && + (ts.isParenthesizedExpression(outer) || + (ts.isCallExpression(outer) && + ts.isIdentifier(outer.expression) && + helperBase(outer.expression.text) === "__toESM")) + ) + outer = outer.parent; + if ( + outer !== undefined && + ts.isBinaryExpression(outer) && + outer.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && + outer.operatorToken.kind <= ts.SyntaxKind.LastAssignment + ) + return { + degrade: `its shipped JS stores the namespace of a bundled ES module ('${r.text}') in a variable (a memoized lazy loader), a module object the static rewrite cannot represent`, + }; + return { degrade: `its shipped JS ${why}` }; + } + // The getters read top-level bindings: nothing between the use site + // and the top level may rebind one of their names. + const read = new Set(); + for (const [, value] of table.entries) for (const x of references(value)) read.add(x.text); + for (let a = p.parent; a !== undefined && !ts.isSourceFile(a); a = a.parent) { + if (ts.isFunctionLike(a) || ts.isBlock(a) || ts.isClassDeclaration(a) || ts.isClassExpression(a)) { + for (const n of declaredNames(a)) { + if (read.has(n)) return { degrade: `its shipped JS ${why}` }; + } + } + } + const getters = table.entries + .map( + ([n, value]) => + `get ${/^[A-Za-z_$][\w$]*$/.test(n) ? n : JSON.stringify(n)}() { return ${applyEdits(source, value.getStart(sf), value.getEnd(), edits)}; }`, + ) + .join(", "); + edits.push({ start: p.getStart(sf), end: p.getEnd(), text: `{ ${getters} }` }); + lowered.add(r.text); + } + for (const target of lowered) { + const table = tables.get(target)!; + const decl = top.get(target)?.node; + if ( + decl === undefined || + !ts.isVariableDeclaration(decl) || + decl.initializer === undefined || + !ts.isObjectLiteralExpression(unparen(decl.initializer)) || + (unparen(decl.initializer) as ts.ObjectLiteralExpression).properties.nodes.length !== 0 || + writes.has(target) + ) + return { degrade: ESM_SHAPE_DEGRADE }; + edits.push({ start: table.stmt.getStart(sf), end: table.stmt.getEnd(), text: "" }); + edits.push(...removeDeclarators(sf, new Set([decl]))); + } + + /* The run-once closures. */ + for (const init of inits.values()) { + const { call, body } = init; + edits.push( + { + start: call.getStart(sf), + end: body.getStart(sf) + 1, + text: "(() => { let __scriptc_done = false; function __scriptc_init() {", + }, + { + start: body.getEnd() - 1, + end: call.getEnd(), + text: "} return function () { if (__scriptc_done) return; __scriptc_done = true; __scriptc_init(); }; })()", + }, + ); + } + return applyEdits(source, 0, source.length, edits); +} diff --git a/packages/compiler/src/frontend/npm-static-rewrite-host.ts b/packages/compiler/src/frontend/npm-static-rewrite-host.ts index 524239901..9ff14c455 100644 --- a/packages/compiler/src/frontend/npm-static-rewrite-host.ts +++ b/packages/compiler/src/frontend/npm-static-rewrite-host.ts @@ -11,6 +11,7 @@ import { import { resolveExports } from "./resolve.js"; import { packageNameOfSpecifier } from "./workspace-registry.js"; import { isBundlerCjsCandidate, rewriteBundlerCjsSyntax } from "./npm-static-rewrite-syntax.js"; +import { lowerBundledEsmInits } from "./npm-static-bundled-esm.js"; const NODE_REQUIRE_CONDITIONS = new Set(["require", "node", "default"]); @@ -164,6 +165,17 @@ function starTargetNames(parser: Ts7SourceParser, file: string): Set { } } +export function lowerEsmInitsWithParser( + parser: Ts7SourceParser, + source: string, + filePath: string, +): string | { degrade: string } | null { + if (!source.includes("__esm")) return null; + return lowerBundledEsmInits(parser.parse(filePath, source, "js"), filePath, { + requireTargetEsModuleStamped: (from, spec) => requireTargetEsModuleStamped(parser, from, spec), + }); +} + export function rewriteBundlerCjsWithParser( parser: Ts7SourceParser, source: string, diff --git a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts index 1730e1153..08dbc0db5 100644 --- a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts +++ b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts @@ -893,6 +893,7 @@ export function rewriteBundlerCjsSyntax( "__toESM", "__toCommonJS", "__commonJS", + "__esm", "__exportStar", "__createBinding", "__setModuleDefault", diff --git a/packages/compiler/src/frontend/npm-static.ts b/packages/compiler/src/frontend/npm-static.ts index 7dffd05f7..7c9b7f6a1 100644 --- a/packages/compiler/src/frontend/npm-static.ts +++ b/packages/compiler/src/frontend/npm-static.ts @@ -407,6 +407,15 @@ export function npmStaticFsShadow(services: FrontendServices): NpmStaticFsShadow for (const chunk of split.chunks) setVirtualSource(chunk.path, chunk.source); original = split.text; } + // Then its lazily initialized ES modules become run-once closures. + const esm = original === null ? null : services.lowerEsmInits(original, path); + if (esm !== null && typeof esm === "object") { + reportFileDegrade(path, target.pkg, esm.degrade); + original = null; + } else if (esm !== null) { + original = esm; + } + const respelled = split !== null || esm !== null; if (original !== null) { const namepaths = services.jsDocNamepaths(path, original); const source = namepaths ?? original; @@ -441,7 +450,7 @@ export function npmStaticFsShadow(services: FrontendServices): NpmStaticFsShadow findWidened?.text ?? classFields?.text ?? namepaths ?? - (split !== null ? original : null); + (respelled ? original : null); } } } catch { @@ -492,6 +501,48 @@ function hasTransformMarkers(source: string): boolean { return TRANSFORM_MARKERS.some((m) => source.includes(m)); } +/** The installed version of the package owning `file` (the innermost + * node_modules segment), or null when no package.json names one. */ +function installedVersionOf(file: string): string | null { + const norm = file.split("\\").join("/"); + const idx = norm.lastIndexOf("/node_modules/"); + if (idx < 0) return null; + const pkg = npmPackageNameOf(norm); + if (pkg === null) return null; + const text = trackedReadFile(`${norm.slice(0, idx)}/node_modules/${pkg}/package.json`); + if (text === null) return null; + try { + const version = (JSON.parse(text) as { version?: unknown }).version; + return typeof version === "string" ? version : null; + } catch { + return null; + } +} + +/** Why a DefinitelyTyped twin cannot vouch for the runtime package `pkg`, + * or null when it can. DefinitelyTyped versions an @types package by the + * library version it describes (major.minor; 0.x minors break): the twin + * must describe the release that is installed, read from both package + * manifests next to the resolved declaration and JavaScript files. */ +export function definitelyTypedMismatch( + pkg: string, + typesFile: string, + jsFile: string, +): string | null { + const typesVersion = installedVersionOf(typesFile); + const version = installedVersionOf(jsFile); + const twin = npmPackageNameOf(typesFile.split("\\").join("/")) ?? "its @types package"; + if (typesVersion === null || version === null) + return `its declared types come from the separate ${twin} package, whose version cannot be matched against the installed ${pkg}`; + const line = (v: string): string => { + const [major = "", minor = ""] = v.split("."); + return major === "0" ? `0.${minor}` : major; + }; + return line(typesVersion) === line(version) + ? null + : `its declared types come from ${twin}@${typesVersion}, which describes a different release than the installed ${pkg}@${version}`; +} + /** Auto-mode eligibility for one candidate package: `typesFile` is what * the ordinary (types-first) resolution answered for the import, * `jsEntry` what the js-only resolution answers. Returns null when diff --git a/packages/compiler/src/frontend/pipeline.ts b/packages/compiler/src/frontend/pipeline.ts index 676b2bb3e..5c929960a 100644 --- a/packages/compiler/src/frontend/pipeline.ts +++ b/packages/compiler/src/frontend/pipeline.ts @@ -16,6 +16,7 @@ import { type LoadResult, } from "./program.js"; import { + definitelyTypedMismatch, npmStaticFileDegrades, npmStaticIneligibleReason, npmStaticOffenders, @@ -219,13 +220,25 @@ function detectAutoPackages( chosen.push(runtimePkg); continue; } + // Both answers compile the JavaScript with inferred types; the + // declarations only check the importer. A DefinitelyTyped twin + // qualifies when it describes the installed release. A package + // without declarations qualifies as a dependency of a compiled + // package, whose JavaScript importer has no declarations to check. + const refusal = + npm === null + ? owner === null + ? "it ships no own .d.ts declaration surface" + : null + : definitelyTypedMismatch(runtimePkg, npm.typesFile, js!.typesFile); + if (refusal === null) { + chosen.push(runtimePkg); + continue; + } statuses.push({ package: runtimePkg, status: "refused", - detail: - npm === null - ? "it ships no own .d.ts declaration surface" - : `its declared types come from the separate ${npm.packageName} package, not the package itself`, + detail: refusal, hint: npmStaticOptInHint(runtimePkg, owner), }); continue; diff --git a/packages/compiler/src/frontend/services.ts b/packages/compiler/src/frontend/services.ts index c6bb24ae5..aeed52161 100644 --- a/packages/compiler/src/frontend/services.ts +++ b/packages/compiler/src/frontend/services.ts @@ -1,6 +1,6 @@ import { Ts7SourceParser, type Ts7SourceKind } from "./ts7/source-parser.js"; import { Ts7Host, type Ts7ApiFactory, type Ts7HostOptions } from "./ts7/program-host.js"; -import { rewriteBundlerCjsWithParser } from "./npm-static-rewrite-host.js"; +import { lowerEsmInitsWithParser, rewriteBundlerCjsWithParser } from "./npm-static-rewrite-host.js"; import { isBundlerCjsCandidate } from "./npm-static-rewrite-syntax.js"; import { mayBundleChunks, @@ -76,6 +76,14 @@ export class FrontendServices { if (!mayBundleChunks(source)) return null; return splitBundledChunks(this.parse(path, source, "js"), path); } + /** A CommonJS bundle's lazily initialized ES modules (__esm) lowered to + * run-once closures (null: none; a degrade reason: shapes the rewrite + * cannot lower). */ + lowerEsmInits(source: string, path: string): string | { degrade: string } | null { + this.ensureOpen(); + if (!source.includes("__esm")) return null; + return lowerEsmInitsWithParser(this.sourceParser(), source, path); + } nullableClassFields(path: string, source: string): syntax.NpmStaticOverloadRewrite | null { return syntax.applyNpmStaticNullableClassFields(this.parse(path, source, "js"), source); } diff --git a/tests/fixtures/npm-static/node_modules/@types/twinstale/index.d.ts b/tests/fixtures/npm-static/node_modules/@types/twinstale/index.d.ts new file mode 100644 index 000000000..0017b82be --- /dev/null +++ b/tests/fixtures/npm-static/node_modules/@types/twinstale/index.d.ts @@ -0,0 +1 @@ +export default function stale(text: string): string; diff --git a/tests/fixtures/npm-static/node_modules/@types/twinstale/package.json b/tests/fixtures/npm-static/node_modules/@types/twinstale/package.json new file mode 100644 index 000000000..7f7850ce6 --- /dev/null +++ b/tests/fixtures/npm-static/node_modules/@types/twinstale/package.json @@ -0,0 +1,5 @@ +{ + "name": "@types/twinstale", + "version": "2.0.4", + "types": "index.d.ts" +} diff --git a/tests/fixtures/npm-static/node_modules/twinstale/index.js b/tests/fixtures/npm-static/node_modules/twinstale/index.js new file mode 100644 index 000000000..129abfcce --- /dev/null +++ b/tests/fixtures/npm-static/node_modules/twinstale/index.js @@ -0,0 +1,6 @@ +// The 3.x release changed the export: its @types twin still describes 2.x. +'use strict'; + +module.exports = function stale(text) { + return 'stale:' + text; +}; diff --git a/tests/fixtures/npm-static/node_modules/twinstale/package.json b/tests/fixtures/npm-static/node_modules/twinstale/package.json new file mode 100644 index 000000000..9c104d6bf --- /dev/null +++ b/tests/fixtures/npm-static/node_modules/twinstale/package.json @@ -0,0 +1,5 @@ +{ + "name": "twinstale", + "version": "3.1.0", + "main": "index.js" +} diff --git a/tests/fixtures/npm-static/twinstale-cli.ts b/tests/fixtures/npm-static/twinstale-cli.ts new file mode 100644 index 000000000..1938cbf8b --- /dev/null +++ b/tests/fixtures/npm-static/twinstale-cli.ts @@ -0,0 +1,5 @@ +// twinstale's @types twin describes an older major release than the +// installed package, so it cannot vouch for the JavaScript. +import stale from "twinstale"; + +console.log(stale("ready")); diff --git a/tests/fixtures/npm/cases/2563-bundled-esm-inits/main.ts b/tests/fixtures/npm/cases/2563-bundled-esm-inits/main.ts new file mode 100644 index 000000000..02ac60cba --- /dev/null +++ b/tests/fixtures/npm/cases/2563-bundled-esm-inits/main.ts @@ -0,0 +1,16 @@ +// esbuild's lazily initialized ES modules: each package is real esbuild +// output (0.25 plain, and whitespace/syntax minified) of a source whose +// load() require()s an ES module. That module and the module it imports +// run behind the __esm helper: once, at the first load(), assigning +// their interop bindings (a builtin and an external CommonJS package) +// and live exports that the namespace's getters read. +import { describe, load } from "gtesm"; +import { describe as dws, load as lws } from "gtesmws"; + +console.log("main start"); +console.log(describe()); +console.log(load()); +console.log(load()); +console.log(dws()); +console.log(lws()); +console.log(lws()); diff --git a/tests/fixtures/npm/node_modules/gtesm/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtesm/dist/index.d.ts new file mode 100644 index 000000000..af4369397 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtesm/dist/index.d.ts @@ -0,0 +1,2 @@ +export declare function describe(): string; +export declare function load(): string; diff --git a/tests/fixtures/npm/node_modules/gtesm/dist/index.js b/tests/fixtures/npm/node_modules/gtesm/dist/index.js new file mode 100644 index 000000000..241c5e048 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtesm/dist/index.js @@ -0,0 +1,90 @@ +var __create = Object.create; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __getProtoOf = Object.getPrototypeOf; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __esm = (fn, res) => function __init() { + return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; +}; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( + // If the importer is in node compatibility mode or this is not an ESM + // file that has been converted to a CommonJS file using a Babel- + // compatible transform (i.e. "__esModule" has not been set), then set + // "default" to the CommonJS "module.exports" for node compatibility. + isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, + mod +)); +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); + +// src/helper.ts +function helper(x) { + COUNT++; + return "[" + x + "]"; +} +var COUNT; +var init_helper = __esm({ + "src/helper.ts"() { + console.log("helper init"); + COUNT = 0; + } +}); + +// src/lazy.ts +var lazy_exports = {}; +__export(lazy_exports, { + VALUE: () => VALUE, + value: () => value +}); +function value() { + return VALUE + ":" + import_gtcore.default.tag + ":" + COUNT; +} +var path, import_gtcore, VALUE; +var init_lazy = __esm({ + "src/lazy.ts"() { + path = __toESM(require("path")); + import_gtcore = __toESM(require("gtcore")); + init_helper(); + console.log("lazy init"); + VALUE = helper(path.basename("/a/b.txt")); + } +}); + +// src/index.ts +var index_exports = {}; +__export(index_exports, { + describe: () => describe, + load: () => load +}); +module.exports = __toCommonJS(index_exports); + +// src/greet.ts +function greet(name) { + return "hello " + name; +} + +// src/index.ts +function describe() { + return greet("main"); +} +function load() { + const lazy = (init_lazy(), __toCommonJS(lazy_exports)); + return lazy.value() + " " + lazy.VALUE; +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + describe, + load +}); diff --git a/tests/fixtures/npm/node_modules/gtesm/package.json b/tests/fixtures/npm/node_modules/gtesm/package.json new file mode 100644 index 000000000..3913402f9 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtesm/package.json @@ -0,0 +1,10 @@ +{ + "name": "gtesm", + "version": "1.0.0", + "description": "esbuild 0.25.10 output (--bundle --platform=node --format=cjs --external:gtcore) of a source that require()s an ES module: lazily initialized ES modules behind the __esm helper", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "dependencies": { + "gtcore": "1.0.0" + } +} diff --git a/tests/fixtures/npm/node_modules/gtesmws/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtesmws/dist/index.d.ts new file mode 100644 index 000000000..af4369397 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtesmws/dist/index.d.ts @@ -0,0 +1,2 @@ +export declare function describe(): string; +export declare function load(): string; diff --git a/tests/fixtures/npm/node_modules/gtesmws/dist/index.js b/tests/fixtures/npm/node_modules/gtesmws/dist/index.js new file mode 100644 index 000000000..cef009700 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtesmws/dist/index.js @@ -0,0 +1 @@ +var __create=Object.create;var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __getProtoOf=Object.getPrototypeOf,__hasOwnProp=Object.prototype.hasOwnProperty;var __esm=(fn,res)=>function(){return fn&&(res=(0,fn[__getOwnPropNames(fn)[0]])(fn=0)),res};var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:!0})},__copyProps=(to,from,except,desc)=>{if(from&&typeof from=="object"||typeof from=="function")for(let key of __getOwnPropNames(from))!__hasOwnProp.call(to,key)&&key!==except&&__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable});return to};var __toESM=(mod,isNodeMode,target)=>(target=mod!=null?__create(__getProtoOf(mod)):{},__copyProps(isNodeMode||!mod||!mod.__esModule?__defProp(target,"default",{value:mod,enumerable:!0}):target,mod)),__toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:!0}),mod);function helper(x){return COUNT++,"["+x+"]"}var COUNT,init_helper=__esm({"src/helper.ts"(){console.log("helper init");COUNT=0}});var lazy_exports={};__export(lazy_exports,{VALUE:()=>VALUE,value:()=>value});function value(){return VALUE+":"+import_gtcore.default.tag+":"+COUNT}var path,import_gtcore,VALUE,init_lazy=__esm({"src/lazy.ts"(){path=__toESM(require("path")),import_gtcore=__toESM(require("gtcore"));init_helper();console.log("lazy init");VALUE=helper(path.basename("/a/b.txt"))}});var index_exports={};__export(index_exports,{describe:()=>describe,load:()=>load});module.exports=__toCommonJS(index_exports);function greet(name){return"hello "+name}function describe(){return greet("main")}function load(){let lazy=(init_lazy(),__toCommonJS(lazy_exports));return lazy.value()+" "+lazy.VALUE}0&&(module.exports={describe,load}); diff --git a/tests/fixtures/npm/node_modules/gtesmws/package.json b/tests/fixtures/npm/node_modules/gtesmws/package.json new file mode 100644 index 000000000..3dfe90e90 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtesmws/package.json @@ -0,0 +1,10 @@ +{ + "name": "gtesmws", + "version": "1.0.0", + "description": "the gtesm source bundled by esbuild 0.25.10 with --minify-whitespace --minify-syntax", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "dependencies": { + "gtcore": "1.0.0" + } +} diff --git a/tests/harness/npm-cases.ts b/tests/harness/npm-cases.ts index 4b3b9fdee..f8d346154 100644 --- a/tests/harness/npm-cases.ts +++ b/tests/harness/npm-cases.ts @@ -19,12 +19,12 @@ export function npmCases(fixturesRoot: string): NpmCase[] { return [ ...globSync(join(fixturesRoot, "npm/cases/*/main.ts")) .sort() - // 2465-2469 and 2556-2562 are the --npm-static bundler-emitted-CJS + // 2465-2469 and 2556-2563 are the --npm-static bundler-emitted-CJS // cases (npm-static.test.ts drives them with the opt-in): their // consumers exercise the static rewrite's surface (lexer-visible // names the shipped .d.ts never declares, the __toESM interop // family), so they stay out of the flagless island lane by design. - .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-2])-[^/]+\/main\.ts$/.test(entry)) + .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-3])-[^/]+\/main\.ts$/.test(entry)) .map((entry) => ({ name: entry.split("/").at(-2)!, entry, diff --git a/tests/harness/npm-static.test.ts b/tests/harness/npm-static.test.ts index a8df98c52..a1703adf7 100644 --- a/tests/harness/npm-static.test.ts +++ b/tests/harness/npm-static.test.ts @@ -76,7 +76,7 @@ async function buildStatic(entry: string, npmStatic: string[] | "auto"): Promise ...globSync( join(fixturesRoot, "npm/node_modules/cryptozoo/**/*.{js,mjs,cjs,json,d.ts}"), ).sort(), - // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2562) + // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2563) ...globSync(join(fixturesRoot, "npm/node_modules/gt*/**/*.{js,cjs,json}")).sort(), ]; for (const f of inputs) hash.update(f).update(readFileSync(f)); @@ -828,30 +828,44 @@ describe(`npm-static pilots${sanitize ? " (sanitized)" : ""}`, () => { // Types-first resolution answers @types/twinned, but the refusal names the // package that has the code and the flag that compiles it. - test("an @types-typed package is refused under its runtime name with the opt-in remedy", () => { - const { coverage } = analyze(join(pilotRoot, "twinned-cli.ts")); + // The default selection compiles a package typed by a DefinitelyTyped + // twin that describes its installed release, and the untyped + // dependencies of compiled packages. + test("an @types-typed package and its untyped dependency compile by default", async () => { + const entry = join(pilotRoot, "twinned-cli.ts"); + const { coverage } = analyze(entry); + expect(coverage.npmStatic).toEqual([ + { package: "twinned", status: "static" }, + { package: "ms", status: "static" }, + ]); + expect(coverage.preflightFailed).toBe(false); + const binary = await buildStatic(entry, "auto"); + const [nodeRes, nativeRes] = await Promise.all([ + runBinary("node", [entry]), + runBinary(binary, []), + ]); + expect(nativeRes.stdout.toString("utf8")).toBe(nodeRes.stdout.toString("utf8")); + expect(nativeRes.exitCode).toBe(nodeRes.exitCode); + }, 180_000); + + test("an @types twin describing another release is refused with the opt-in remedy", () => { + const { coverage } = analyze(join(pilotRoot, "twinstale-cli.ts")); expect(coverage.npmStatic).toEqual([ { - package: "twinned", + package: "twinstale", status: "refused", detail: - "its declared types come from the separate @types/twinned package, not the package itself", - hint: "to compile it anyway, name it with --npm-static=twinned (or --npm-static=twinned... to include its dependencies)", + "its declared types come from @types/twinstale@2.0.4, which describes a different release than the installed twinstale@3.1.0", + hint: "to compile it anyway, name it with --npm-static=twinstale (or --npm-static=twinstale... to include its dependencies)", }, ]); const diagnostic = coverage.diagnostics.find((d) => d.code === "SC2013"); - expect(diagnostic?.message).toContain("'twinned'"); - expect(diagnostic?.hint).toContain("--npm-static=twinned"); + expect(diagnostic?.message).toContain("'twinstale'"); }, 120_000); - test("a named package's untyped dependency points at the transitive opt-in", () => { + test("a named package's untyped dependency compiles with it", () => { const { coverage } = analyze(join(pilotRoot, "twinned-cli.ts"), { npmStatic: ["twinned"] }); - expect(coverage.npmStatic).toContainEqual({ - package: "ms", - status: "refused", - detail: "it ships no own .d.ts declaration surface", - hint: "to compile it anyway, name it with --npm-static=ms, or use --npm-static=twinned... to include all of twinned's dependencies", - }); + expect(coverage.npmStatic).toContainEqual({ package: "ms", status: "static" }); }, 120_000); test("--npm-static=pkg... compiles the package's dependency closure", async () => { @@ -1457,6 +1471,16 @@ describe(`npm-static pilots${sanitize ? " (sanitized)" : ""}`, () => { ]); }, 120_000); + // 2563: lazily initialized ES modules (__esm) in esbuild 0.25 plain and + // whitespace/syntax-minified bundles. + test("bundled ES module initializers 2563-bundled-esm-inits compile statically and byte-match Node", async () => { + await expectNodeParity(join(fixturesRoot, "npm/cases/2563-bundled-esm-inits/main.ts"), [ + "gtesm", + "gtesmws", + "gtcore", + ]); + }, 180_000); + // 2562: a file with unverifiable interop that no compiled code can load // (the program imports a leaf data module and only a type from the // main entry) leaves its package static. From 0d9a6da8c96556f042690d0d8a294bd19e964f27 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 14:19:07 -0700 Subject: [PATCH 25/83] Select untyped dependencies and @types twins by identity An untyped package another package's JavaScript imports is selected whether resolution answers no declarations or its JavaScript. A DefinitelyTyped twin qualifies by identity (@types/): the declarations only check the program, and the JavaScript compiles with its inferred surface, so a twin that disagrees with the shipped JavaScript fails the build at the use rather than compiling against it. Co-Authored-By: Claude Opus 5.5 --- packages/compiler/src/frontend/npm-static.ts | 65 ++++++------------- packages/compiler/src/frontend/pipeline.ts | 14 ++-- .../node_modules/@types/twinstale/index.d.ts | 2 +- .../node_modules/twinstale/index.js | 5 +- tests/fixtures/npm-static/twinstale-cli.ts | 7 +- tests/harness/npm-static.test.ts | 21 +++--- 6 files changed, 46 insertions(+), 68 deletions(-) diff --git a/packages/compiler/src/frontend/npm-static.ts b/packages/compiler/src/frontend/npm-static.ts index 7c9b7f6a1..4c1bc9ae6 100644 --- a/packages/compiler/src/frontend/npm-static.ts +++ b/packages/compiler/src/frontend/npm-static.ts @@ -501,46 +501,18 @@ function hasTransformMarkers(source: string): boolean { return TRANSFORM_MARKERS.some((m) => source.includes(m)); } -/** The installed version of the package owning `file` (the innermost - * node_modules segment), or null when no package.json names one. */ -function installedVersionOf(file: string): string | null { - const norm = file.split("\\").join("/"); - const idx = norm.lastIndexOf("/node_modules/"); - if (idx < 0) return null; - const pkg = npmPackageNameOf(norm); - if (pkg === null) return null; - const text = trackedReadFile(`${norm.slice(0, idx)}/node_modules/${pkg}/package.json`); - if (text === null) return null; - try { - const version = (JSON.parse(text) as { version?: unknown }).version; - return typeof version === "string" ? version : null; - } catch { - return null; - } -} - -/** Why a DefinitelyTyped twin cannot vouch for the runtime package `pkg`, - * or null when it can. DefinitelyTyped versions an @types package by the - * library version it describes (major.minor; 0.x minors break): the twin - * must describe the release that is installed, read from both package - * manifests next to the resolved declaration and JavaScript files. */ -export function definitelyTypedMismatch( - pkg: string, - typesFile: string, - jsFile: string, -): string | null { - const typesVersion = installedVersionOf(typesFile); - const version = installedVersionOf(jsFile); - const twin = npmPackageNameOf(typesFile.split("\\").join("/")) ?? "its @types package"; - if (typesVersion === null || version === null) - return `its declared types come from the separate ${twin} package, whose version cannot be matched against the installed ${pkg}`; - const line = (v: string): string => { - const [major = "", minor = ""] = v.split("."); - return major === "0" ? `0.${minor}` : major; - }; - return line(typesVersion) === line(version) +/** Why the declarations at `typesFile` cannot stand for the runtime + * package `pkg`, or null when they are its DefinitelyTyped twin + * (`@types/`). The declarations only check the importing + * program; the JavaScript compiles with its inferred types, and an + * inferred surface the program does not typecheck against refuses the + * package afterwards, whatever release the twin describes. */ +export function definitelyTypedMismatch(pkg: string, typesFile: string): string | null { + const owner = npmPackageNameOf(typesFile.split("\\").join("/")); + const twin = `@types/${mangledTypesName(pkg)}`; + return owner === twin ? null - : `its declared types come from ${twin}@${typesVersion}, which describes a different release than the installed ${pkg}@${version}`; + : `its declared types come from ${owner ?? typesFile}, which is not ${pkg}'s DefinitelyTyped package ${twin}`; } /** Auto-mode eligibility for one candidate package: `typesFile` is what @@ -551,12 +523,17 @@ export function npmStaticIneligibleReason( pkgName: string, typesFile: string, jsEntry: string | null, + /** A dependency another package's JavaScript imports: no declarations + * check that importer, so the package need not ship its own. */ + untypedDependency = false, ): string | null { - if (npmPackageNameOf(typesFile) !== pkgName) { - return "its declared types come from a third-party @types package, not the package itself"; - } - if (!/\.d\.(ts|mts|cts)$/.test(typesFile)) { - return "it ships no own .d.ts declaration surface"; + if (!untypedDependency) { + if (npmPackageNameOf(typesFile) !== pkgName) { + return "its declared types come from a third-party @types package, not the package itself"; + } + if (!/\.d\.(ts|mts|cts)$/.test(typesFile)) { + return "it ships no own .d.ts declaration surface"; + } } if (jsEntry === null) return "no runtime JS entry resolves"; const source = trackedReadFile(jsEntry); diff --git a/packages/compiler/src/frontend/pipeline.ts b/packages/compiler/src/frontend/pipeline.ts index 5c929960a..5081fc7be 100644 --- a/packages/compiler/src/frontend/pipeline.ts +++ b/packages/compiler/src/frontend/pipeline.ts @@ -221,16 +221,17 @@ function detectAutoPackages( continue; } // Both answers compile the JavaScript with inferred types; the - // declarations only check the importer. A DefinitelyTyped twin - // qualifies when it describes the installed release. A package - // without declarations qualifies as a dependency of a compiled - // package, whose JavaScript importer has no declarations to check. + // declarations only check the importer, and an inferred surface + // the program does not typecheck against still refuses the package + // below. A DefinitelyTyped twin qualifies; a package without + // declarations qualifies as a dependency of another package, whose + // JavaScript importer has no declarations to check. const refusal = npm === null - ? owner === null + ? owner === null && !sf.fileName.includes("/node_modules/") ? "it ships no own .d.ts declaration surface" : null - : definitelyTypedMismatch(runtimePkg, npm.typesFile, js!.typesFile); + : definitelyTypedMismatch(runtimePkg, npm.typesFile); if (refusal === null) { chosen.push(runtimePkg); continue; @@ -269,6 +270,7 @@ function detectAutoPackages( pkg, typesFile, jsEntry !== null && isJsSourceFileName(jsEntry.typesFile) ? jsEntry.typesFile : null, + mode === "lib" && isJsSourceFileName(typesFile) && fromFile.includes("/node_modules/"), ); if (reason === null) chosen.push(pkg); else diff --git a/tests/fixtures/npm-static/node_modules/@types/twinstale/index.d.ts b/tests/fixtures/npm-static/node_modules/@types/twinstale/index.d.ts index 0017b82be..47f003c12 100644 --- a/tests/fixtures/npm-static/node_modules/@types/twinstale/index.d.ts +++ b/tests/fixtures/npm-static/node_modules/@types/twinstale/index.d.ts @@ -1 +1 @@ -export default function stale(text: string): string; +export function stale(text: string): string; diff --git a/tests/fixtures/npm-static/node_modules/twinstale/index.js b/tests/fixtures/npm-static/node_modules/twinstale/index.js index 129abfcce..6efa90723 100644 --- a/tests/fixtures/npm-static/node_modules/twinstale/index.js +++ b/tests/fixtures/npm-static/node_modules/twinstale/index.js @@ -1,6 +1,7 @@ -// The 3.x release changed the export: its @types twin still describes 2.x. +// The 3.x release renamed its export: its @types twin still describes the +// 2.x name. 'use strict'; -module.exports = function stale(text) { +exports.render = function render(text) { return 'stale:' + text; }; diff --git a/tests/fixtures/npm-static/twinstale-cli.ts b/tests/fixtures/npm-static/twinstale-cli.ts index 1938cbf8b..f6c8a679c 100644 --- a/tests/fixtures/npm-static/twinstale-cli.ts +++ b/tests/fixtures/npm-static/twinstale-cli.ts @@ -1,5 +1,6 @@ -// twinstale's @types twin describes an older major release than the -// installed package, so it cannot vouch for the JavaScript. -import stale from "twinstale"; +// twinstale's @types twin describes an older release than the installed +// package: the program typechecks against the twin, but not against the +// JavaScript's inferred surface (under Node the import is undefined). +import { stale } from "twinstale"; console.log(stale("ready")); diff --git a/tests/harness/npm-static.test.ts b/tests/harness/npm-static.test.ts index a1703adf7..8560973cd 100644 --- a/tests/harness/npm-static.test.ts +++ b/tests/harness/npm-static.test.ts @@ -848,19 +848,16 @@ describe(`npm-static pilots${sanitize ? " (sanitized)" : ""}`, () => { expect(nativeRes.exitCode).toBe(nodeRes.exitCode); }, 180_000); - test("an @types twin describing another release is refused with the opt-in remedy", () => { + // The twin only checks the program; the JavaScript compiles with its + // inferred surface, so a twin naming an export the JavaScript lacks + // (Node's link error) fails the build at the import's use instead of + // compiling against the declaration. + test("an @types twin that disagrees with the shipped JavaScript does not build", () => { const { coverage } = analyze(join(pilotRoot, "twinstale-cli.ts")); - expect(coverage.npmStatic).toEqual([ - { - package: "twinstale", - status: "refused", - detail: - "its declared types come from @types/twinstale@2.0.4, which describes a different release than the installed twinstale@3.1.0", - hint: "to compile it anyway, name it with --npm-static=twinstale (or --npm-static=twinstale... to include its dependencies)", - }, - ]); - const diagnostic = coverage.diagnostics.find((d) => d.code === "SC2013"); - expect(diagnostic?.message).toContain("'twinstale'"); + expect(coverage.npmStatic).toEqual([{ package: "twinstale", status: "static" }]); + expect(coverage.diagnostics).toContainEqual( + expect.objectContaining({ message: expect.stringContaining("'stale'") as string }), + ); }, 120_000); test("a named package's untyped dependency compiles with it", () => { From 4e0f71a7e5dae2f2b266049d6ce8d8473cbd5760 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 14:21:57 -0700 Subject: [PATCH 26/83] Document the new package selection; record the 2563 order baseline Co-Authored-By: Claude Opus 5.5 --- docs/content/docs/dependencies.mdx | 14 ++++++++------ .../src/frontend/npm-static-bundled-esm.test.ts | 9 ++++----- .../src/frontend/npm-static-bundled-esm.ts | 13 +++++++++---- .../npm/cases/2563-bundled-esm-inits/main.ts.json | 6 ++++++ 4 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2563-bundled-esm-inits/main.ts.json diff --git a/docs/content/docs/dependencies.mdx b/docs/content/docs/dependencies.mdx index 3c6958841..e6020fe54 100644 --- a/docs/content/docs/dependencies.mdx +++ b/docs/content/docs/dependencies.mdx @@ -52,10 +52,12 @@ Type-only imports follow Node.js's type stripping. `import type` and `export typ scriptc compiles a package automatically when it: -- ships its own type declarations, and -- ships readable, unminified JavaScript. +- ships its own type declarations, or is typed by its DefinitelyTyped `@types/` package, or is a dependency of another package that has no declarations at all, and +- ships readable JavaScript: unminified, or minified by a bundler whose helpers keep their names. -A package that doesn't meet these requirements, or that uses something scriptc can't compile, gets a package-specific diagnostic that explains why. The usual fixes are to switch to a version or alternative package that compiles, or to replace the call with code you write yourself. A package whose declarations come from a separate `@types/` package doesn't ship its own; its diagnostic names the package and the `--npm-static` option that compiles it anyway. +The declarations check your program; the package's JavaScript compiles with the types scriptc infers from it. A program that doesn't typecheck against the inferred types, for example because an `@types/` package describes another release, fails the build at that use. + +A package that doesn't meet these requirements, or that uses something scriptc can't compile, gets a package-specific diagnostic that explains why. The usual fixes are to switch to a version or alternative package that compiles, or to replace the call with code you write yourself. @@ -63,17 +65,17 @@ A package that doesn't meet these requirements, or that uses something scriptc c ## Explicit package selection -Some packages rely on separately installed declarations, such as an `@types/` package, or on types inferred from their JavaScript. To try compiling one of them, name it with `--npm-static`: +Some packages your program imports directly ship no declarations, so their types can only be inferred from their JavaScript. To try compiling one of them, name it with `--npm-static`: ```console $ scriptc build app.ts --npm-static=three -o app >/dev/null ``` -Naming a package skips the declaration requirement above, but the package still has to compile. The option selects only the packages it names: a dependency of a named package that doesn't ship its own declarations is still refused. Add a trailing `...` to select a package together with every package it depends on, for example `--npm-static=fs-extra...`. `--npm-static=auto` uses the default automatic selection. The option accepts a comma-separated list and can be repeated. +Naming a package skips the declaration requirement above, but the package still has to compile. Add a trailing `...` to select a package together with every package it depends on, for example `--npm-static=fs-extra...`. `--npm-static=auto` uses the default automatic selection. The option accepts a comma-separated list and can be repeated. ## CommonJS packages -CommonJS packages compile with Node.js's module semantics. Each `require()` evaluates its module at the call, once, and later calls return the cached exports. A package can assign `module.exports` and run code before it requires its dependencies. If that code reads a `const` or `let` require binding before its `require` runs, the read throws the same `ReferenceError` as Node.js. A `var` binding that earlier code can reach is reported at build time. A `require()` of a JSON file, such as a package's own `package.json`, returns one object parsed at build time, shared by every module that requires it. A require cycle compiles when each of its modules assigns its exports before requiring anything, as semver's classes do, and uses the other modules only inside functions that its top-level code can't call. Other require cycles are reported at build time. The [limitations](/docs/limitations) page lists the supported `require` forms. +CommonJS packages compile with Node.js's module semantics. Each `require()` evaluates its module at the call, once, and later calls return the cached exports. A package can assign `module.exports` and run code before it requires its dependencies. If that code reads a `const` or `let` require binding before its `require` runs, the read throws the same `ReferenceError` as Node.js. A `var` binding that earlier code can reach is reported at build time. A `require()` of a JSON file, such as a package's own `package.json`, returns one object parsed at build time, shared by every module that requires it. A require cycle compiles when each of its modules assigns its exports before requiring anything, as semver's classes and esbuild's per-file output do, and uses the other modules only inside functions that its top-level code can't call. Bundles built by esbuild compile with their bundled CommonJS modules and lazily initialized ES modules, each evaluated once, at its first use, as under Node.js. Other require cycles are reported at build time. The [limitations](/docs/limitations) page lists the supported `require` forms. ## Unsupported code inside a package diff --git a/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts b/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts index 9c064cb55..0b9ecd042 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts @@ -117,10 +117,7 @@ test("minified initializers sharing a var statement lower the same way", () => { }); test.each([ - [ - "is asynchronous", - `var init_a = __esm({ async "a.ts"() { await 0; } });\ninit_a();`, - ], + ["is asynchronous", `var init_a = __esm({ async "a.ts"() { await 0; } });\ninit_a();`], [ "is used other than as a statement call", `var init_a = __esm({ "a.ts"() {} });\nvar r = init_a();`, @@ -145,5 +142,7 @@ test.each([ test("files without __esm initializers and ES-module bundles are left alone", () => { expect(lower("exports.x = 1;\n")).toBeNull(); - expect(lower(`${helpers}var init_a = __esm({ "a.ts"() {} });\ninit_a();\nexport {};\n`)).toBeNull(); + expect( + lower(`${helpers}var init_a = __esm({ "a.ts"() {} });\ninit_a();\nexport {};\n`), + ).toBeNull(); }); diff --git a/packages/compiler/src/frontend/npm-static-bundled-esm.ts b/packages/compiler/src/frontend/npm-static-bundled-esm.ts index ae5985e51..cf4f6280b 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-esm.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-esm.ts @@ -252,7 +252,8 @@ export function lowerBundledEsmInits( const stable = (name: string): boolean => !duplicate.has(name) && !writes.has(name); const ownNames = (name: string): boolean => { const t = top.get(name); - const init = t !== undefined && ts.isVariableDeclaration(t.node) ? t.node.initializer : undefined; + const init = + t !== undefined && ts.isVariableDeclaration(t.node) ? t.node.initializer : undefined; return ( init !== undefined && stable(name) && @@ -308,8 +309,7 @@ export function lowerBundledEsmInits( ) return { degrade: ESM_SHAPE_DEGRADE }; const frame = factoryFrameUse(fn.body, false); - if (frame !== null) - return { degrade: `its bundled ES module initializer '${name}' ${frame}` }; + if (frame !== null) return { degrade: `its bundled ES module initializer '${name}' ${frame}` }; inits.set(name, { name, call, body: fn.body }); } if (inits.size === 0) return null; @@ -531,7 +531,12 @@ export function lowerBundledEsmInits( const read = new Set(); for (const [, value] of table.entries) for (const x of references(value)) read.add(x.text); for (let a = p.parent; a !== undefined && !ts.isSourceFile(a); a = a.parent) { - if (ts.isFunctionLike(a) || ts.isBlock(a) || ts.isClassDeclaration(a) || ts.isClassExpression(a)) { + if ( + ts.isFunctionLike(a) || + ts.isBlock(a) || + ts.isClassDeclaration(a) || + ts.isClassExpression(a) + ) { for (const n of declaredNames(a)) { if (read.has(n)) return { degrade: `its shipped JS ${why}` }; } diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2563-bundled-esm-inits/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2563-bundled-esm-inits/main.ts.json new file mode 100644 index 000000000..953a75d7d --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2563-bundled-esm-inits/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2563-bundled-esm-inits/main.ts" + ], + "diags": [] +} From 769e9e58cf768ced8e26510d7e42f35deee02355 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 14:29:23 -0700 Subject: [PATCH 27/83] Run the two npm cases the default selection now compiles cjs-lexer-invisible and hoisted-workspace compiled only once their untyped dependencies were selected; both now match Node in both lanes. Co-Authored-By: Claude Opus 5.5 --- tests/harness/npm.test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/harness/npm.test.ts b/tests/harness/npm.test.ts index b6af0dbb6..1f231b3bc 100644 --- a/tests/harness/npm.test.ts +++ b/tests/harness/npm.test.ts @@ -40,7 +40,6 @@ const KNOWN: Record = { "bytes-exit": { build: "SC2004" }, "cjs-esm-named": { build: "SC2013" }, "cjs-lexer-aligned": { build: "SC2013" }, - "cjs-lexer-invisible": { build: "SC2013" }, "commander-calc": { run: "SC1090", argvs: [["--help"]] }, "commander-calc-async": { run: "SC1090", argvs: [["--help"]] }, "create-require": { run: "SC1090" }, @@ -53,7 +52,6 @@ const KNOWN: Record = { "esm-missing-export": { build: "SC1090" }, "fetch-destructure": { run: "SC1031" }, "fs-shims": { run: "SC2020" }, - "hoisted-workspace": { build: "SC2013" }, "import-refusal": { build: "SC2020" }, "island-registry": { build: "SC2011" }, "island-timers": { From 0e02fc28ca8ce822c19e25be8442a71268b1508f Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Mon, 19 Oct 2026 16:59:05 -0500 Subject: [PATCH 28/83] Accept mock.fn() without an original node:test changes, matching Node 24.22: - mock.fn() and t.mock.fn() without an original return undefined and record the arguments of each call, whatever the callers pass. The original and the initial implementation are a function that returns undefined, so restore() brings that function back. - mock.fn() with an explicit function type records F's parameters when F's return type is void or includes undefined. A return type without undefined, such as mock.fn<() => number>(), throws SC2020. mock.fn()'s default type, (...args: any[]) => undefined, maps to the checked-dynamic rest ABI used for program-declared (...args: unknown[]) functions: the calls log records the argument array, and the mock's wrapper spreads it into the implementation. A function whose fixed parameters are all unknown or any, or that has none, now converts to a checked-dynamic rest signature without fixed parameters, such as (...args: unknown[]) => void: each call passes the arguments in order, undefined for the ones the caller left out. This is how () => {} reaches mockImplementation and mockImplementationOnce, and it applies to any such slot in TypeScript programs, which threw SC1090 before. The mock-fn-default fixture and corpus program 5816 compare with Node, plain and with SCRIPTC_SAN=1. --- .../limitations/builtin-apis/test-mocks.mdx | 2 +- .../null-and-optional-values.mdx | 2 +- .../generated/node-v24-backlog.json | 1 + .../generated/node-v24-internal.json | 2 + internal/compatibility/static-support.json | 2 +- .../lowering/coercions/function-adapters.ts | 58 +++++++++++++- .../src/frontend/lowering/lower-mock.ts | 75 ++++++++++++++++--- packages/compiler/src/frontend/mock-shapes.ts | 19 +++-- packages/compiler/src/frontend/type-mapper.ts | 22 +++++- .../5816-fixed-into-unknown-rest.ts.json | 6 ++ tests/corpus/5816-fixed-into-unknown-rest.ts | 39 ++++++++++ tests/diagnostics/maybe-promise-fences.ts | 5 -- tests/diagnostics/mock-fences.ts | 12 ++- .../node-test/cases/mock-fn-default.ts | 72 ++++++++++++++++++ .../__snapshots__/maybe-promise-fences.ts.txt | 9 +-- .../harness/__snapshots__/mock-fences.ts.txt | 60 +++++++-------- .../5816-fixed-into-unknown-rest.ts.json | 1 + 17 files changed, 318 insertions(+), 69 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5816-fixed-into-unknown-rest.ts.json create mode 100644 tests/corpus/5816-fixed-into-unknown-rest.ts create mode 100644 tests/fixtures/node-test/cases/mock-fn-default.ts create mode 100644 tests/harness/corpus-costs/5816-fixed-into-unknown-rest.ts.json diff --git a/docs/content/limitations/builtin-apis/test-mocks.mdx b/docs/content/limitations/builtin-apis/test-mocks.mdx index a0c63cd98..6c52c5639 100644 --- a/docs/content/limitations/builtin-apis/test-mocks.mdx +++ b/docs/content/limitations/builtin-apis/test-mocks.mdx @@ -1,2 +1,2 @@ -- **Test mocks:** `mock.fn`, `mock.method`, `mock.restoreAll` and `mock.reset` compile, on the module's `mock` and on `t.mock`. `mock.method` mocks a function-valued field of a record, such as an object literal or an interface-typed object. Class instances, builtin objects, and object literals whose methods use `this` throw `SC2020`. Calls through the object record it as `this`, including calls of a method read off the object and called on its own, where Node records `undefined`. `object.method === mock` is `false`, because the field holds a wrapper of the mock. `mock.fn()` without an original, `mock.getter`, `mock.setter`, `mock.property`, `mock.module` and mock options throw `SC2020`. +- **Test mocks:** `mock.fn`, `mock.method`, `mock.restoreAll` and `mock.reset` compile, on the module's `mock` and on `t.mock`. `mock.method` mocks a function-valued field of a record, such as an object literal or an interface-typed object. Class instances, builtin objects, and object literals whose methods use `this` throw `SC2020`. Calls through the object record it as `this`, including calls of a method read off the object and called on its own, where Node records `undefined`. `object.method === mock` is `false`, because the field holds a wrapper of the mock. `mock.fn()` without an original returns `undefined` and records the arguments of each call, as in Node; with a function type whose return type does not include `undefined`, such as `mock.fn<() => number>()`, it throws `SC2020`. `mock.getter`, `mock.setter`, `mock.property`, `mock.module` and mock options throw `SC2020`. - **Mock timers:** `mock.timers.enable`, `tick`, `runAll`, `reset`, `setTime` and `[Symbol.dispose]()` compile, on the module's `mock` and on `t.mock`, for the `setTimeout`, `setInterval`, `setImmediate`, `Date`, `scheduler.wait` and `AbortSignal.timeout` APIs. `enable()` takes no options or an object literal whose `apis`, when given, is an array literal. Options and `apis` lists that are not literals, and the `MockTimers` object used as a value, throw `SC2020`. As in Node, the global timer functions, `Date.now()`, `new Date()` and `scheduler.wait` are mocked, and other calls through named and namespace imports of `node:timers` and `node:timers/promises` use real timers. As in Node, once a reset gives `scheduler.wait` back, calling it throws `ERR_INVALID_THIS`. While `scheduler.wait` is mocked, calling it with a `signal` option throws an `SC2020` error. A mock timer's handle converts to a negative number, where Node's mock `Timeout` converts to `NaN`. When two trackers have timers enabled, clearing a timer through the tracker that did not create it leaves the timer queued, where Node removes the entry at the same queue position in the clearing tracker. diff --git a/docs/content/limitations/types-and-class-definitions/null-and-optional-values.mdx b/docs/content/limitations/types-and-class-definitions/null-and-optional-values.mdx index afb8ec9cf..54db5e6fc 100644 --- a/docs/content/limitations/types-and-class-definitions/null-and-optional-values.mdx +++ b/docs/content/limitations/types-and-class-definitions/null-and-optional-values.mdx @@ -1 +1 @@ -- **Null and optional values:** `undefined` and `null` compile as standalone values and union members. Optional record and class fields and optional, default, and rest parameters also compile, including rest parameters typed `unknown[]` or `any[]`, whose functions take any number of arguments. A function value with fixed parameters cannot be stored where such a variadic function type is expected. +- **Null and optional values:** `undefined` and `null` compile as standalone values and union members. Optional record and class fields and optional, default, and rest parameters also compile, including rest parameters typed `unknown[]` or `any[]`, whose functions take any number of arguments. A function value whose fixed parameters are all `unknown` or `any`, or that has none, can be stored where such a variadic function type without fixed parameters is expected: each call passes the arguments in order, `undefined` for the ones the caller left out. Other function values with fixed parameters cannot be stored there. diff --git a/internal/compatibility/generated/node-v24-backlog.json b/internal/compatibility/generated/node-v24-backlog.json index 75e789c61..92d145a3b 100644 --- a/internal/compatibility/generated/node-v24-backlog.json +++ b/internal/compatibility/generated/node-v24-backlog.json @@ -66502,6 +66502,7 @@ "source": "compiler-feature:test.mock.fn", "tests": [ "tests/fixtures/node-test/cases/mock-fn.ts", + "tests/fixtures/node-test/cases/mock-fn-default.ts", "tests/harness/node-test.test.ts" ] } diff --git a/internal/compatibility/generated/node-v24-internal.json b/internal/compatibility/generated/node-v24-internal.json index c93030602..d953c69e4 100644 --- a/internal/compatibility/generated/node-v24-internal.json +++ b/internal/compatibility/generated/node-v24-internal.json @@ -91073,6 +91073,7 @@ "tests/fixtures/node-test/cases/ctx-plan.ts", "tests/fixtures/node-test/cases/get-test-context-cancelled.ts", "tests/fixtures/node-test/cases/get-test-context.ts", + "tests/fixtures/node-test/cases/mock-fn-default.ts", "tests/fixtures/node-test/cases/mock-fn.ts", "tests/fixtures/node-test/cases/mock-method.ts", "tests/fixtures/node-test/cases/mock-timers-date.ts", @@ -92484,6 +92485,7 @@ "evidence": "compiler-feature:test.mock.fn", "tests": [ "tests/fixtures/node-test/cases/mock-fn.ts", + "tests/fixtures/node-test/cases/mock-fn-default.ts", "tests/harness/node-test.test.ts" ] }, diff --git a/internal/compatibility/static-support.json b/internal/compatibility/static-support.json index 71605612e..035d94db5 100644 --- a/internal/compatibility/static-support.json +++ b/internal/compatibility/static-support.json @@ -362,7 +362,7 @@ { "chapter": "test", "parent": "Class: `TestContext`", "symbols": ["context.filePath", "passed", "attempt", "tags", "workerId"], "status": "partial", "evidence": ["tests/fixtures/node-test/cases/ctx-members.ts", "tests/harness/node-test.test.ts"] }, { "chapter": "test", "parent": "Class: `SuiteContext`", "symbols": ["context.name", "context.fullName", "context.filePath", "context.diagnostic", "passed", "attempt"], "status": "partial", "evidence": ["tests/fixtures/node-test/cases/suite-context.ts", "tests/harness/node-test.test.ts"] }, { "chapter": "test", "parent": "Class: `TestContext`", "symbols": ["context.diagnostic", "context.name"], "status": "partial", "evidence": ["tests/harness/node-test.test.ts"] }, - { "chapter": "test", "parent": "Class: `MockTracker`", "symbols": ["mock.fn"], "status": "partial", "evidence": ["tests/fixtures/node-test/cases/mock-fn.ts", "tests/harness/node-test.test.ts"] }, + { "chapter": "test", "parent": "Class: `MockTracker`", "symbols": ["mock.fn"], "status": "partial", "evidence": ["tests/fixtures/node-test/cases/mock-fn.ts", "tests/fixtures/node-test/cases/mock-fn-default.ts", "tests/harness/node-test.test.ts"] }, { "chapter": "test", "parent": "Class: `MockTracker`", "symbols": ["mock.method", "mock.reset", "mock.restoreAll"], "status": "partial", "evidence": ["tests/fixtures/node-test/cases/mock-method.ts", "tests/corpus/5810-mock-method-records.ts", "tests/corpus/5811-mock-tracker-restore.ts", "tests/harness/node-test.test.ts"] }, { "chapter": "test", "parent": "Class: `MockTimers`", "symbols": ["timers.enable", "timers.reset", "timers.tick", "timers.runAll", "timers.setTime"], "status": "partial", "evidence": ["tests/fixtures/node-test/cases/mock-timers.ts", "tests/fixtures/node-test/cases/mock-timers-date.ts", "tests/corpus/5813-mock-timers-module.ts", "tests/corpus/5814-mock-timers-date.ts", "tests/harness/node-test.test.ts"] }, { "chapter": "test", "parent": "Class: `MockTimers`", "signatures": ["`timers[Symbol.dispose]()`"], "status": "partial", "evidence": ["tests/fixtures/node-test/cases/mock-timers-date.ts", "tests/harness/node-test.test.ts"] }, diff --git a/packages/compiler/src/frontend/lowering/coercions/function-adapters.ts b/packages/compiler/src/frontend/lowering/coercions/function-adapters.ts index b99c24a15..6ae52ee40 100644 --- a/packages/compiler/src/frontend/lowering/coercions/function-adapters.ts +++ b/packages/compiler/src/frontend/lowering/coercions/function-adapters.ts @@ -127,15 +127,71 @@ export function cleanFuncAdaptable( return src.ret.kind === "void" && dst.ret.kind === "dyn"; } +/** Adapt a fixed-parameter function to a checked-dynamic rest signature + * without fixed parameters (`() => {}` into mock.fn()'s `(...args: any[]) + * => undefined`): the adapter receives the hidden argument array and + * passes its element i as parameter i, undefined when the caller passed + * fewer arguments, as JS does. Null unless every parameter is + * checked-dynamic, which holds a missing argument as undefined, and the + * result is discarded or converts. */ +function funcIntoDynRestAdapter( + lowerer: Lowerer, + fromT: IrType & { kind: "func" }, + toT: IrType & { kind: "func" }, + loc: SrcLoc, +): string | null { + if (fromT.rest === true || fromT.params.some((p) => p.kind !== "dyn")) return null; + if (toT.ret.kind !== "void" && !lowerer.coercibleValue(fromT.ret, toT.ret)) return null; + const key = `fnadapt:${typeKey(fromT)}:${typeKey(toT)}`; + const existing = lowerer.coercions.retags.get(key); + if (existing) return existing; + const name = `%fn.adapt.${lowerer.coercions.retags.size}`; + lowerer.coercions.retags.set(key, name); + lowerer.freshClosureAdapters.add(name); + const params: IrParam[] = [{ localId: "rest.0", name: "args", type: DYN }]; + const call: IrExpr = { + kind: "callValue", + callee: { kind: "varRef", localId: "f.0", type: fromT, loc }, + receiver: { kind: "libCall", fn: "dyn.this", args: [], type: DYN, loc }, + args: fromT.params.map((_, i): IrExpr => ({ + kind: "dynKeyGet", + value: { kind: "varRef", localId: "rest.0", type: DYN, loc }, + key: { kind: "strLit", value: String(i), type: STRING, loc }, + type: DYN, + loc, + })), + type: fromT.ret, + loc, + }; + let body: IrStmt[]; + if (toT.ret.kind === "void") { + body = [ + { kind: "exprStmt", expr: call, loc }, + { kind: "return", value: null, loc }, + ]; + } else { + const result = lowerer.coerceToExpected(call, toT.ret); + if (!typeEquals(result.type, toT.ret)) + throw new InternalCompilerError("lowerer bug: probed fn-adapter return stopped coercing"); + body = [{ kind: "return", value: result, loc }]; + } + lowerer.liftedFns.push(...buildFunctionAdapter(name, fromT, toT, params, [], body, loc)); + return name; +} + /** Adapt a function value to a destination signature, forwarding and * converting its arguments and result. Unsupported parameter/result pairs - * produce checked traps when called; rest signatures remain refused. */ + * produce checked traps when called; rest signatures remain refused, + * except a fixed-parameter function into a checked-dynamic rest signature + * without fixed parameters (funcIntoDynRestAdapter). */ export function funcCoerceAdapter( lowerer: Lowerer, fromT: IrType & { kind: "func" }, toT: IrType & { kind: "func" }, loc: SrcLoc, ): string | null { + if (toT.rest === true && toT.restAbi === undefined && toT.params.length === 0) + return funcIntoDynRestAdapter(lowerer, fromT, toT, loc); if (fromT.rest === true || toT.rest === true) return null; if (fromT.params.length > toT.params.length) return null; // Piece dispositions beyond coercibleValue, all CHECKER-APPROVED diff --git a/packages/compiler/src/frontend/lowering/lower-mock.ts b/packages/compiler/src/frontend/lowering/lower-mock.ts index 4f446bb89..be68abaca 100644 --- a/packages/compiler/src/frontend/lowering/lower-mock.ts +++ b/packages/compiler/src/frontend/lowering/lower-mock.ts @@ -154,7 +154,12 @@ function mockWrapperFactory( const { fn, call, calls, once, onceEntry, result, args } = mockContextFields(lowerer, ctxType)!; const impl = `${name}.impl`; const ctx = () => varRef("ctx.0", ctxType, loc); + // A checked-dynamic rest function (mock.fn()'s default type) takes its + // arguments as one trailing dyn array, recorded as the call's arguments + // and spread into the implementation. + const dynRest = fn.rest === true && fn.restAbi === undefined; const params = fn.params.map((type, i) => ({ localId: `p${i}.0`, name: `p${i}`, type })); + if (dynRest) params.push({ localId: "rest.0", name: "args", type: DYN }); const paramRefs = () => params.map((p) => varRef(p.localId, p.type, loc)); const argumentsValue = (): IrExpr => fn.rest @@ -219,13 +224,23 @@ function mockWrapperFactory( loc, }; }; - const invoke: IrExpr = { - kind: "callValue", - callee: varRef("f.0", fn, loc), - args: paramRefs(), - type: fn.ret, - loc, - }; + const invoke: IrExpr = dynRest + ? { + kind: "dynCall", + callee: { kind: "dynFrom", value: varRef("f.0", fn, loc), type: DYN, loc }, + calleeName: "implementation", + args: paramRefs(), + spreads: [{ arg: 0, what: "args" }], + type: DYN, + loc, + } + : { + kind: "callValue", + callee: varRef("f.0", fn, loc), + args: paramRefs(), + type: fn.ret, + loc, + }; const tryBody: IrStmt[] = voidReturn ? [ { kind: "exprStmt", expr: invoke, loc }, @@ -629,7 +644,37 @@ function requireUndefinedResultArm( } } -/** mock.fn(original, implementation?) and t.mock.fn(...). */ +/** `%mock.noop.N`: mock.fn()'s original, Node's `function () {}` — it + * returns undefined. Null when the function type's return type cannot hold + * undefined. */ +function mockNoopOriginal( + lowerer: Lowerer, + ctxType: IrType, + fn: FuncType, + loc: SrcLoc, +): IrExpr | null { + const voidReturn = fn.ret.kind === "void" || fn.ret.kind === "undefinedT"; + const undefinedValue = voidReturn ? null : lowerer.wrappedUndefined(fn.ret, loc); + if (!voidReturn && !undefinedValue) return null; + const name = liftOnce(lowerer, ctxType, "noop", (name) => { + const params = fn.params.map((type, i) => ({ localId: `p${i}.0`, name: `p${i}`, type })); + if (fn.rest === true && fn.restAbi === undefined) + params.push({ localId: "rest.0", name: "args", type: DYN }); + lowerer.liftedFns.push({ + name, + params, + returnType: fn.ret, + locals: params.map((p) => local(p.localId, p.name, p.type)), + body: [{ kind: "return", value: undefinedValue, loc }], + loc, + }); + }); + return { kind: "closure", fnName: name, captures: [], type: fn, loc }; +} + +/** mock.fn(original?, implementation?) and t.mock.fn(...). Without an + * original, the mock's original and implementation are a function that + * returns undefined, as in Node. */ export function lowerMockFn( lowerer: Lowerer, call: ts.CallExpression, @@ -658,11 +703,19 @@ export function lowerMockFn( const { fn } = fields; requireUndefinedResultArm(lowerer, fields, call, what); const args = call.arguments.nodes; - if (args.length === 0 || args.length > 2 || args.some(ts.isSpreadElement)) { + if (args.length > 2 || args.some(ts.isSpreadElement)) { lowerer.noLowering( `${what} with ${args.length} arguments`, call, - "the supported forms are mock.fn(original) and mock.fn(original, implementation); mock.fn() and mock options have no lowering", + "the supported forms are mock.fn(), mock.fn(original) and mock.fn(original, implementation); mock options have no lowering", + ); + } + const noop = args.length === 0 ? mockNoopOriginal(lowerer, ctxType!, fn, loc) : null; + if (args.length === 0 && !noop) { + lowerer.noLowering( + `${what}() of a function returning '${lowerer.fmt(fn.ret)}'`, + call, + "mock.fn() without an original returns undefined: pass an original, or use a function type whose return type includes undefined", ); } const stmts: IrStmt[] = []; @@ -670,7 +723,7 @@ export function lowerMockFn( stmts.push({ kind: "varDecl", localId: originalLocal.id, - init: lowerer.lowerExprExpecting(args[0]!, fn), + init: noop ?? lowerer.lowerExprExpecting(args[0]!, fn), loc, }); const implementation = diff --git a/packages/compiler/src/frontend/mock-shapes.ts b/packages/compiler/src/frontend/mock-shapes.ts index 1baf2cd04..50781c272 100644 --- a/packages/compiler/src/frontend/mock-shapes.ts +++ b/packages/compiler/src/frontend/mock-shapes.ts @@ -12,10 +12,11 @@ * object; empty for mock.fn * * A MockFunctionCall is { arguments, error, result, this }: the - * arguments as a tuple of F's parameters, the result as F's return type or - * undefined (a checked-dynamic value for functions returning void or - * undefined, since a record field cannot have a unit type), and error and - * this as checked-dynamic values. */ + * arguments as a tuple of F's parameters (a checked-dynamic array for + * mock.fn()'s `(...args: any[]) => undefined`), the result as F's return + * type or undefined (a checked-dynamic value for functions returning void + * or undefined, since a record field cannot have a unit type), and error + * and this as checked-dynamic values. */ import { DYN, F64, NULL_T, UNDEFINED_T, VOID, typeKey, type IrType } from "../ir/ir.js"; import type { TypeMapperCtx } from "./type-mapper.js"; @@ -34,11 +35,17 @@ function orUndefined(t: IrType, reg: MockShapeRegistries): IrType { } /** The arguments type of a call record: a tuple of the parameters, or the - * rest array itself for a function whose only parameter is a rest array. - * Null for other rest shapes. A function without parameters records the + * rest array itself for a function whose only parameter is a rest array, + * or a checked-dynamic array for a function returning nothing whose only + * parameter is a checked-dynamic rest (mock.fn()'s default, `(...args: + * any[]) => undefined`). Null for other rest shapes. A function without parameters records the * empty tuple, which maps to an array of the unit-only union, as * type-mapper.ts maps `[]`. */ function mockArgumentsType(fn: IrType & { kind: "func" }, reg: MockShapeRegistries): IrType | null { + if (fn.rest && fn.restAbi === undefined) { + const returnsNothing = fn.ret.kind === "void" || fn.ret.kind === "undefinedT"; + return fn.params.length === 0 && returnsNothing ? DYN : null; + } if (fn.rest) { if (fn.params.length !== 1 || fn.params[0]!.kind !== "array") return null; return fn.params[0]!; diff --git a/packages/compiler/src/frontend/type-mapper.ts b/packages/compiler/src/frontend/type-mapper.ts index 123a5d1a0..b4218341b 100644 --- a/packages/compiler/src/frontend/type-mapper.ts +++ b/packages/compiler/src/frontend/type-mapper.ts @@ -4384,10 +4384,13 @@ function mapCallSignature(sig: ts.Signature, ctx: TypeMapperCtx): IrType | null // params). Declaration files keep their existing paths: library // callback slots such as String#replace's `(substring, ...args: any[])` // have intrinsic lowerings that inspect the unmapped type. + // node:test's default mock function type, `(...args: any[]) => + // undefined` (the type parameter default of mock.fn), takes the same + // ABI: mock.fn() without an original records whatever its callers pass. if ( rest && decl !== undefined && - !decl.getSourceFile().isDeclarationFile && + (!decl.getSourceFile().isDeclarationFile || isNodeTestDefaultFunctionParam(decl, ctx)) && (pt?.kind === "dyn" || (!pt && isAnyArrayType(checker.getTypeOfSymbol(p), checker))) ) { dynRest = true; @@ -6364,6 +6367,23 @@ function dynSubsumableUnionArm(arm: IrType, ctx: TypeMapperCtx): boolean { } } +/** A parameter of a function type that is the default of a type parameter + * declared in node:test's standard-library declarations (MockTracker.fn's + * `F extends Function = (...args: any[]) => undefined`). */ +function isNodeTestDefaultFunctionParam(decl: ts.Declaration, ctx: TypeMapperCtx): boolean { + const fnType = decl.parent; + return ( + ts.isParameter(decl) && + fnType !== undefined && + ts.isFunctionTypeNode(fnType) && + fnType.parent !== undefined && + ts.isTypeParameterDeclaration(fnType.parent) && + fnType.parent.defaultType === fnType && + ctx.isStdlibFile(decl.getSourceFile()) && + isDeclaredInAmbientModule(decl, "test") + ); +} + /** `any[]` (or `readonly any[]`): an array type whose element is `any`. */ export function isAnyArrayType(t: ts.Type, checker: ts.TypeChecker): boolean { if (!checker.isArrayType(t)) return false; diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5816-fixed-into-unknown-rest.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5816-fixed-into-unknown-rest.ts.json new file mode 100644 index 000000000..8354f5c48 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5816-fixed-into-unknown-rest.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/5816-fixed-into-unknown-rest.ts" + ], + "diags": [] +} diff --git a/tests/corpus/5816-fixed-into-unknown-rest.ts b/tests/corpus/5816-fixed-into-unknown-rest.ts new file mode 100644 index 000000000..f6d287021 --- /dev/null +++ b/tests/corpus/5816-fixed-into-unknown-rest.ts @@ -0,0 +1,39 @@ +// A function with only `unknown` parameters, or none, flows into a +// `(...args: unknown[]) => R` slot: each call passes the callers' +// arguments in order, undefined for a parameter the caller left out, and +// drops the extra ones, as JS calls do. Sync and async functions, stored +// in variables and arrays and passed as arguments. +type Handler = (...args: unknown[]) => void; +type AsyncHandler = (...args: unknown[]) => Promise; + +const none: Handler = () => console.log("none"); +const one: Handler = (a: unknown) => console.log("one", a); +const two: Handler = (a: unknown, b: unknown) => console.log("two", a, b); + +none(); +none(1, 2, 3); +one(); +one("x"); +one("x", "y"); +two(1); +two(1, [2], { three: 3 }); + +const handlers: Handler[] = [none, one, two]; +for (const h of handlers) h("all", true); + +function dispatch(h: Handler, ...args: unknown[]): void { + h(...args); +} +dispatch(two, "spread", null); +dispatch(one); + +const first: AsyncHandler = async (value: unknown) => value; +const pair: AsyncHandler = async (a: unknown, b: unknown) => [a, b]; + +async function main(): Promise { + console.log(await first(10, 20)); + console.log(await first()); + console.log(await pair("p")); + console.log(await pair("p", "q", "r")); +} +main().then(() => console.log("done")); diff --git a/tests/diagnostics/maybe-promise-fences.ts b/tests/diagnostics/maybe-promise-fences.ts index efe541c9e..1dcccaf08 100644 --- a/tests/diagnostics/maybe-promise-fences.ts +++ b/tests/diagnostics/maybe-promise-fences.ts @@ -30,8 +30,3 @@ console.log(typeof widened); // `instanceof Promise` on a value that is never a promise. const record = { v: 1 }; console.log(record instanceof Promise); - -// A variadic `unknown[]` slot takes closures with the same variadic ABI. -type HandlerFn = (...args: unknown[]) => Promise; -const fixed: HandlerFn = async (first: unknown) => first; -fixed(1, 2).then((v) => console.log(v)); diff --git a/tests/diagnostics/mock-fences.ts b/tests/diagnostics/mock-fences.ts index 4425dfa5f..ef92f366f 100644 --- a/tests/diagnostics/mock-fences.ts +++ b/tests/diagnostics/mock-fences.ts @@ -1,8 +1,9 @@ -// node:test mocks lower for mock.fn(original[, implementation]), +// node:test mocks lower for mock.fn([original[, implementation]]), // mock.method(object, methodName[, implementation]) on records, their // t.mock twins, and the trackers' restoreAll and reset. The rest of the // mock surface is refused, each with a named diagnostic: mock.fn() without -// an original; mock.method on a class instance or a builtin object, with +// an original for a function type whose return type does not include +// undefined; mock.method on a class instance or a builtin object, with // an options object, or with an implementation of another type when the // result is used; and functions whose union return type has no undefined // arm (the calls log records undefined for calls that throw). @@ -11,8 +12,11 @@ import { mock, test } from "node:test"; const add = mock.fn((a: number, b: number) => a + b); // lowers add(1, 2); -const empty = mock.fn(); // no original function -empty(); +const empty = mock.fn(); // lowers +empty(1, "two"); + +const counted = mock.fn<() => number>(); // no original, returns number +counted(); const obj = { value: () => 1 }; mock.method(obj, "value"); // lowers diff --git a/tests/fixtures/node-test/cases/mock-fn-default.ts b/tests/fixtures/node-test/cases/mock-fn-default.ts new file mode 100644 index 000000000..4666dfa5e --- /dev/null +++ b/tests/fixtures/node-test/cases/mock-fn-default.ts @@ -0,0 +1,72 @@ +// mock.fn() and t.mock.fn() without an original: the mock returns +// undefined and records the arguments of each call, whatever its callers +// pass. mockImplementation and mockImplementationOnce replace its +// behavior, and restore() brings back the function that returns undefined. +// mock.fn() records F's parameters, for a function type whose return +// type is void or includes undefined. +import assert from "node:assert"; +import { mock, test } from "node:test"; + +test("records calls with any arguments", () => { + const fn = mock.fn(); + fn(1, "two"); + fn(); + fn(true, null, [3]); + assert.strictEqual(fn.mock.callCount(), 3); + const [first, second, third] = fn.mock.calls; + assert.deepStrictEqual(first!.arguments, [1, "two"]); + assert.strictEqual(second!.arguments.length, 0); + assert.deepStrictEqual(third!.arguments, [true, null, [3]]); + assert.ok(first!.result === undefined); + assert.ok(first!.error === undefined); + assert.ok(first!.this === undefined); +}); + +test("changes implementations", () => { + const seen: string[] = []; + const fn = mock.fn(); + fn.mock.mockImplementation((...args: unknown[]) => { + seen.push(`impl ${args.length}`); + return undefined; + }); + fn.mock.mockImplementationOnce(() => { + seen.push("once"); + return undefined; + }); + fn("a"); + fn("a", "b"); + fn.mock.restore(); + fn("c"); + assert.deepStrictEqual(seen, ["once", "impl 2"]); + assert.strictEqual(fn.mock.callCount(), 3); + fn.mock.resetCalls(); + assert.strictEqual(fn.mock.callCount(), 0); +}); + +test("records a throwing implementation", () => { + const fn = mock.fn(); + fn.mock.mockImplementation(() => { + throw new Error("boom"); + }); + assert.throws(() => fn(7), { message: "boom" }); + const call = fn.mock.calls[0]!; + assert.deepStrictEqual(call.arguments, [7]); + assert.ok(call.error instanceof Error); +}); + +test("t.mock.fn() and typed mock.fn()", (t) => { + const fn = t.mock.fn(); + fn("x"); + assert.strictEqual(fn.mock.callCount(), 1); + const typed = t.mock.fn<(a: number, b: string) => void>(); + typed(1, "x"); + typed(2, "y"); + assert.strictEqual(typed.mock.callCount(), 2); + assert.strictEqual(typed.mock.calls[1]!.arguments[0], 2); + assert.strictEqual(typed.mock.calls[1]!.arguments[1], "y"); + const maybe = t.mock.fn<() => number | undefined>(); + assert.ok(maybe() === undefined); + maybe.mock.mockImplementation(() => 4); + assert.ok(maybe() === 4); + assert.ok(maybe.mock.calls[1]!.result === 4); +}); diff --git a/tests/harness/__snapshots__/maybe-promise-fences.ts.txt b/tests/harness/__snapshots__/maybe-promise-fences.ts.txt index 58fb23517..159e88f76 100644 --- a/tests/harness/__snapshots__/maybe-promise-fences.ts.txt +++ b/tests/harness/__snapshots__/maybe-promise-fences.ts.txt @@ -55,11 +55,4 @@ maybe-promise-fences.ts:32:13 - error SC1090: 'instanceof Promise' on '{ v: numb 31 | const record = { v: 1 }; 32 | console.log(record instanceof Promise); | ^~~~~~~~~~~~~~~~~~~~~~~~~ - 33 | - -maybe-promise-fences.ts:36:26 - error SC1090: '(unknown) => Promise' values where '() => Promise' is expected is not supported yet - - 35 | type HandlerFn = (...args: unknown[]) => Promise; - 36 | const fixed: HandlerFn = async (first: unknown) => first; - | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - 37 | fixed(1, 2).then((v) => console.log(v)); \ No newline at end of file + 33 | \ No newline at end of file diff --git a/tests/harness/__snapshots__/mock-fences.ts.txt b/tests/harness/__snapshots__/mock-fences.ts.txt index 6b4defcbb..d4b8115e8 100644 --- a/tests/harness/__snapshots__/mock-fences.ts.txt +++ b/tests/harness/__snapshots__/mock-fences.ts.txt @@ -1,62 +1,62 @@ -mock-fences.ts:14:15 - error SC2020: 'mock.fn of this function type' is part of the standard library types but has no scriptc lowering yet +mock-fences.ts:18:17 - error SC2020: 'mock.fn() of a function returning 'number'' is part of the standard library types but has no scriptc lowering yet - 13 | - 14 | const empty = mock.fn(); // no original function - | ^~~~~~~~~ - 15 | empty(); + 17 | + 18 | const counted = mock.fn<() => number>(); // no original, returns number + | ^~~~~~~~~~~~~~~~~~~~~~~ + 19 | counted(); - hint: mock functions lower for functions with fixed parameters, or a single rest array parameter, and an implementation of the same type + hint: mock.fn() without an original returns undefined: pass an original, or use a function type whose return type includes undefined -mock-fences.ts:27:13 - error SC2020: 'mock.method on 'Counter'' is part of the standard library types but has no scriptc lowering yet +mock-fences.ts:31:13 - error SC2020: 'mock.method on 'Counter'' is part of the standard library types but has no scriptc lowering yet - 26 | } - 27 | mock.method(new Counter(), "count"); // class instance + 30 | } + 31 | mock.method(new Counter(), "count"); // class instance | ^~~~~~~~~~~~~ - 28 | mock.method(console, "log"); // builtin object + 32 | mock.method(console, "log"); // builtin object hint: mock.method lowers for objects whose type maps to a record (object literals and interface-typed objects) and a function-valued field named by a string literal -mock-fences.ts:28:13 - error SC2020: 'mock.method on this object' is part of the standard library types but has no scriptc lowering yet +mock-fences.ts:32:13 - error SC2020: 'mock.method on this object' is part of the standard library types but has no scriptc lowering yet - 27 | mock.method(new Counter(), "count"); // class instance - 28 | mock.method(console, "log"); // builtin object + 31 | mock.method(new Counter(), "count"); // class instance + 32 | mock.method(console, "log"); // builtin object | ^~~~~~~ - 29 | mock.method(obj, "value", { times: 1 }); // options + 33 | mock.method(obj, "value", { times: 1 }); // options hint: mock.method lowers for objects whose type maps to a record (object literals and interface-typed objects) and a function-valued field named by a string literal -mock-fences.ts:29:27 - error SC2020: 'mock.method with an options object' is part of the standard library types but has no scriptc lowering yet +mock-fences.ts:33:27 - error SC2020: 'mock.method with an options object' is part of the standard library types but has no scriptc lowering yet - 28 | mock.method(console, "log"); // builtin object - 29 | mock.method(obj, "value", { times: 1 }); // options + 32 | mock.method(console, "log"); // builtin object + 33 | mock.method(obj, "value", { times: 1 }); // options | ^~~~~~~~~~~~ - 30 | const other = mock.method(obj, "value", () => "one"); // implementation of another type + 34 | const other = mock.method(obj, "value", () => "one"); // implementation of another type hint: mock options (times, getter and setter) have no lowering -mock-fences.ts:30:41 - error SC2020: 'mock.method with an implementation of another type' is part of the standard library types but has no scriptc lowering yet +mock-fences.ts:34:41 - error SC2020: 'mock.method with an implementation of another type' is part of the standard library types but has no scriptc lowering yet - 29 | mock.method(obj, "value", { times: 1 }); // options - 30 | const other = mock.method(obj, "value", () => "one"); // implementation of another type + 33 | mock.method(obj, "value", { times: 1 }); // options + 34 | const other = mock.method(obj, "value", () => "one"); // implementation of another type | ^~~~~~~~~~~ - 31 | other(); + 35 | other(); hint: give the implementation the method's parameter and return types, or discard the result -mock-fences.ts:35:31 - error SC2020: 't.mock.method with an options object' is part of the standard library types but has no scriptc lowering yet +mock-fences.ts:39:31 - error SC2020: 't.mock.method with an options object' is part of the standard library types but has no scriptc lowering yet - 34 | t.mock.method(obj, "value"); // lowers - 35 | t.mock.method(obj, "value", { times: 2 }); // options + 38 | t.mock.method(obj, "value"); // lowers + 39 | t.mock.method(obj, "value", { times: 2 }); // options | ^~~~~~~~~~~~ - 36 | }); + 40 | }); hint: mock options (times, getter and setter) have no lowering -mock-fences.ts:38:14 - error SC2020: 'mock.fn of a function returning 'number | string'' is part of the standard library types but has no scriptc lowering yet +mock-fences.ts:42:14 - error SC2020: 'mock.fn of a function returning 'number | string'' is part of the standard library types but has no scriptc lowering yet - 37 | - 38 | const pick = mock.fn((flag: boolean): number | string => (flag ? 1 : "one")); // union without undefined + 41 | + 42 | const pick = mock.fn((flag: boolean): number | string => (flag ? 1 : "one")); // union without undefined | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - 39 | pick(true); + 43 | pick(true); hint: a union return type needs an undefined arm here (the calls log records undefined for calls that throw) \ No newline at end of file diff --git a/tests/harness/corpus-costs/5816-fixed-into-unknown-rest.ts.json b/tests/harness/corpus-costs/5816-fixed-into-unknown-rest.ts.json new file mode 100644 index 000000000..2b149eb6a --- /dev/null +++ b/tests/harness/corpus-costs/5816-fixed-into-unknown-rest.ts.json @@ -0,0 +1 @@ +{ "plain": 3028, "san": 5470 } From f7f239ad531fbc32b84b99187d421b8152bca71b Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 15:55:37 -0700 Subject: [PATCH 29/83] Lower memoized lazy loaders of bundled ES-module namespaces A loader `L = () => M ??= NS` over a lazily initialized module's namespace (optionally through __toESM) is respelled at every `L().name` as the module's initializer call followed by the export's live read; loaders whose result escapes as a value stay refused. The canonical export table keeps a getter it cannot chase as a live accessor, so a lazy re-export still initializes its module at the first read. An ES-module importer copies those accessors once when it links the CommonJS module, in the lexer's order, exactly like Node's loader; require() keeps reading them live. Co-Authored-By: Claude Opus 5.5 --- .../src/frontend/lowering/lower-exprs.ts | 102 ++++++++++- .../src/frontend/lowering/lower-modules.ts | 10 +- .../compiler/src/frontend/lowering/lowerer.ts | 4 + .../src/frontend/npm-static-bundled-esm.ts | 173 ++++++++++++++++-- .../src/frontend/npm-static-rewrite-host.ts | 2 + .../src/frontend/npm-static-rewrite-syntax.ts | 28 ++- packages/compiler/src/frontend/npm-static.ts | 10 + packages/compiler/src/frontend/program.ts | 9 +- packages/compiler/src/frontend/services.ts | 9 +- .../npm/cases/2564-lazy-barrel/main.ts | 19 ++ .../cases/2565-lazy-barrel-require/main.ts | 9 + .../npm/node_modules/gtlazy/dist/index.d.ts | 7 + .../npm/node_modules/gtlazy/dist/index.js | 116 ++++++++++++ .../npm/node_modules/gtlazy/package.json | 7 + .../npm/node_modules/gtlazyuser/index.d.ts | 2 + .../npm/node_modules/gtlazyuser/index.js | 10 + .../npm/node_modules/gtlazyuser/package.json | 10 + .../npm/node_modules/gtlazyws/dist/index.d.ts | 7 + .../npm/node_modules/gtlazyws/dist/index.js | 3 + .../npm/node_modules/gtlazyws/package.json | 7 + tests/harness/npm-cases.ts | 4 +- tests/harness/npm-static.test.ts | 13 +- 22 files changed, 537 insertions(+), 24 deletions(-) create mode 100644 tests/fixtures/npm/cases/2564-lazy-barrel/main.ts create mode 100644 tests/fixtures/npm/cases/2565-lazy-barrel-require/main.ts create mode 100644 tests/fixtures/npm/node_modules/gtlazy/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtlazy/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtlazy/package.json create mode 100644 tests/fixtures/npm/node_modules/gtlazyuser/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtlazyuser/index.js create mode 100644 tests/fixtures/npm/node_modules/gtlazyuser/package.json create mode 100644 tests/fixtures/npm/node_modules/gtlazyws/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtlazyws/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtlazyws/package.json diff --git a/packages/compiler/src/frontend/lowering/lower-exprs.ts b/packages/compiler/src/frontend/lowering/lower-exprs.ts index 09407f71f..cabbc31f5 100644 --- a/packages/compiler/src/frontend/lowering/lower-exprs.ts +++ b/packages/compiler/src/frontend/lowering/lower-exprs.ts @@ -114,6 +114,7 @@ import { cjsClassExprWholeExportOf, cjsExportAssignmentOf, cjsExportDiscardReason, + cjsExportTargetLiteral, isCjsExportTableLiteral, isCjsJsFile, isJsSourceFile, @@ -164,7 +165,7 @@ import { import { strCharsCall } from "./containers/array-construction.js"; import { lowerOptionalStringIndex, lowerUnionStringIndex } from "./string-index.js"; import { tryLowerIndexedComparison } from "./indexed-comparison.js"; -import { npmStaticPackageOfPath } from "../npm-static.js"; +import { npmStaticPackageOfPath, npmStaticLexerVisibleNames } from "../npm-static.js"; import { unsupportedModuleFeatureOf } from "../builtin-modules.js"; import { fenceEnumObjectValue, lowerEnumAccess } from "./lower-enums.js"; import { @@ -12541,9 +12542,108 @@ function cjsExportAccessorRead(lowerer: Lowerer, ident: ts.Identifier): IrExpr | if (!ts.isObjectLiteralExpression(getter.parent) || !isCjsExportTableLiteral(getter.parent)) { return null; } + // An ES-module import binding reads the value Node copied when the + // importer linked the CommonJS module (cjsAccessorSnapshotStores). + const raw = lowerer.checker.getSymbolAtLocation(ident); + const binding = raw ? lowerer.checker.declarationsOf(raw)[0] : undefined; + if ( + binding !== undefined && + (ts.isImportSpecifier(binding) || ts.isExportSpecifier(binding)) && + !isCjsJsFile(binding.getSourceFile(), lowerer.program) + ) { + const snapshot = cjsAccessorSnapshotOf(lowerer, getter); + if (snapshot !== null) + return { kind: "varRef", localId: snapshot.id, type: snapshot.type, loc: locOf(ident) }; + } return cjsAccessorCall(lowerer, getter, locOf(ident)); } +/** The names Node's ES-module loader copies off a CommonJS module that a + * bundler rewrite spells as accessors (npmStaticLexerVisibleNames), with + * their getters in that order; empty for every other module. */ +function lexerVisibleAccessors(dep: ts.SourceFile): ts.GetAccessorDeclaration[] { + const names = npmStaticLexerVisibleNames(dep.fileName); + if (names.length === 0) return []; + let table: ts.ObjectLiteralExpression | null = null; + for (const stmt of dep.statements.nodes) { + const cjs = cjsExportAssignmentOf(stmt); + if (cjs?.kind === "table" && cjsExportDiscardReason(stmt) === null) { + table = cjsExportTargetLiteral(cjs.expr.right, dep); + } + } + if (table === null) return []; + const getters = new Map(); + for (const p of table.properties.nodes) { + if (ts.isGetAccessorDeclaration(p) && (ts.isIdentifier(p.name) || ts.isStringLiteral(p.name))) + getters.set(p.name.text, p); + } + return names.flatMap((n) => { + const g = getters.get(n); + return g === undefined ? [] : [g]; + }); +} + +/** The module global holding the value an accessor export had when an + * ES-module importer first linked its CommonJS module. */ +function cjsAccessorSnapshotOf( + lowerer: Lowerer, + getter: ts.GetAccessorDeclaration, +): { id: string; type: IrType } | null { + const hit = lowerer.cjsAccessorSnapshots.get(getter); + if (hit !== undefined) return hit; + if (!lexerVisibleAccessors(getter.getSourceFile()).includes(getter)) return null; + const call = cjsAccessorCall(lowerer, getter, locOf(getter)); + if (call === null) return null; + const snapshot = { id: `%g.cjsSnapshot.${lowerer.cjsAccessorSnapshots.size}`, type: call.type }; + lowerer.globalsList.push({ id: snapshot.id, name: "%cjsSnapshot", type: call.type, mutable: true }); + lowerer.cjsAccessorSnapshots.set(getter, snapshot); + return snapshot; +} + +/** Node's ES-module loader reads every lexer-visible export of a CommonJS + * module once, when an importer first links it, and the import bindings + * keep those values. For the accessors a bundler rewrite introduced, the + * importer's link point runs each getter once, in the lexer's order, and + * stores the values its bindings read. Empty when the module has none. */ +export function cjsAccessorSnapshotStores( + lowerer: Lowerer, + dep: ts.SourceFile, + loc: SrcLoc, +): IrStmt[] { + const getters = lexerVisibleAccessors(dep); + if (getters.length === 0) return []; + let readyId = lowerer.cjsSnapshotReady.get(dep); + if (readyId === undefined) { + readyId = `%g.cjsSnapshotReady.${lowerer.cjsSnapshotReady.size}`; + lowerer.globalsList.push({ id: readyId, name: "%cjsSnapshotReady", type: BOOL, mutable: true }); + lowerer.cjsSnapshotReady.set(dep, readyId); + } + const stores: IrStmt[] = [ + { kind: "assign", localId: readyId, value: { kind: "boolLit", value: true, type: BOOL, loc }, loc }, + ]; + for (const getter of getters) { + const snapshot = cjsAccessorSnapshotOf(lowerer, getter); + const call = cjsAccessorCall(lowerer, getter, loc); + if (snapshot === null || call === null) return []; + stores.push({ kind: "assign", localId: snapshot.id, value: call, loc }); + } + return [ + { + kind: "if", + cond: { + kind: "unary", + op: "!", + operand: { kind: "varRef", localId: readyId, type: BOOL, loc }, + type: BOOL, + loc, + }, + then: stores, + else_: null, + loc, + }, + ]; +} + /** The interned lifted-getter call both accessor-read paths share. */ function cjsAccessorCall( lowerer: Lowerer, diff --git a/packages/compiler/src/frontend/lowering/lower-modules.ts b/packages/compiler/src/frontend/lowering/lower-modules.ts index 96e22f208..86fab82d4 100644 --- a/packages/compiler/src/frontend/lowering/lower-modules.ts +++ b/packages/compiler/src/frontend/lowering/lower-modules.ts @@ -143,7 +143,7 @@ import { forkTargetPaths } from "../fork-target.js"; import { workerTargetPaths } from "../worker-target.js"; import { isNativeProxyInitializer } from "./expressions/native-proxy.js"; import { handleEventReaderPrelude } from "./lower-event-target.js"; -import { jsHoldsDynObject } from "./lower-exprs.js"; +import { cjsAccessorSnapshotStores, jsHoldsDynObject } from "./lower-exprs.js"; /** One file's declarations, split for collection and init-body lowering. */ export interface FileParts { @@ -2823,6 +2823,14 @@ export function lowerFileInit( }); } else { header.push({ kind: "exprStmt", expr: call, loc }); + // An ES-module importer links a CommonJS dependency: Node + // copies its lexer-visible exports here. + if ( + (ts.isImportDeclaration(stmt) || ts.isExportDeclaration(stmt)) && + !isCjsJsFile(sf, lowerer.program) && + isCjsJsFile(dep, lowerer.program) + ) + header.push(...cjsAccessorSnapshotStores(lowerer, dep, loc)); } } } diff --git a/packages/compiler/src/frontend/lowering/lowerer.ts b/packages/compiler/src/frontend/lowering/lowerer.ts index d252af250..ce2390d95 100644 --- a/packages/compiler/src/frontend/lowering/lowerer.ts +++ b/packages/compiler/src/frontend/lowering/lowerer.ts @@ -2507,6 +2507,10 @@ export class Lowerer { * lifted lazily as module-level functions and interned per accessor * declaration: member reads call the getter (lower-exprs). */ readonly cjsAccessorFns = new Map(); + /** Accessor exports' link-time values for ES-module importers, and the + * per-module flag that the copy already ran (cjsAccessorSnapshotStores). */ + readonly cjsAccessorSnapshots = new Map(); + readonly cjsSnapshotReady = new Map(); /** Interned `%iter.drain.` helpers (classIteratorDrainCall): one per * receiver class — the eager drain of a class iterable's protocol into * a fresh element array, behind array/call spreads. */ diff --git a/packages/compiler/src/frontend/npm-static-bundled-esm.ts b/packages/compiler/src/frontend/npm-static-bundled-esm.ts index cf4f6280b..cf3664264 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-esm.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-esm.ts @@ -443,9 +443,154 @@ export function lowerBundledEsmInits( } } const lowered = new Set(); + /** Some binding between the use site and the top level rebinds a name + * the table's getters read. */ + const shadowed = (site: ts.Node, entries: readonly [string, ts.Expression][]): boolean => { + const read = new Set(); + for (const [, value] of entries) for (const x of references(value)) read.add(x.text); + for (let a = site.parent; a !== undefined && !ts.isSourceFile(a); a = a.parent) { + if ( + ts.isFunctionLike(a) || + ts.isBlock(a) || + ts.isClassDeclaration(a) || + ts.isClassExpression(a) + ) { + for (const n of declaredNames(a)) if (read.has(n)) return true; + } + } + return false; + }; + const getterText = (value: ts.Expression): string => + applyEdits(source, value.getStart(sf), value.getEnd(), edits); + + /* Memoized lazy loaders of those namespaces, `L = () => M ??= NS` (a + * bundle plugin's lazy re-exports): every `L().name` becomes the + * initializer call followed by the export's live read, so the first + * access still initializes the module, once, and later accesses read + * the binding. */ + const loaderSpans: { start: number; end: number }[] = []; + for (const [name, t] of top) { + const d = t.node; + if (!ts.isVariableDeclaration(d) || d.initializer === undefined) continue; + const fn = unparen(d.initializer); + if (!ts.isArrowFunction(fn) || fn.parameters.nodes.length !== 0 || ts.isBlock(fn.body)) + continue; + const memoize = unparen(fn.body); + if ( + !ts.isBinaryExpression(memoize) || + (memoize.operatorToken.kind !== ts.SyntaxKind.QuestionQuestionEqualsToken && + memoize.operatorToken.kind !== ts.SyntaxKind.BarBarEqualsToken) || + !ts.isIdentifier(memoize.left) + ) + continue; + let w = unparen(memoize.right); + let interop = false; + let nodeMode = false; + if ( + ts.isCallExpression(w) && + ts.isIdentifier(w.expression) && + helperBase(w.expression.text) === "__toESM" + ) { + const [arg, mode] = w.arguments.nodes; + if (!arg || w.arguments.nodes.length > 2) continue; + if (mode !== undefined) { + if (ts.isNumericLiteral(mode)) nodeMode = Number(mode.text) !== 0; + else if (mode.kind === ts.SyntaxKind.TrueKeyword) nodeMode = true; + else if (mode.kind !== ts.SyntaxKind.FalseKeyword) continue; + } + interop = true; + w = unparen(arg); + } + let initCall: ts.CallExpression | null = null; + if (ts.isBinaryExpression(w) && w.operatorToken.kind === ts.SyntaxKind.CommaToken) { + const left = unparen(w.left); + if ( + !ts.isCallExpression(left) || + !ts.isIdentifier(left.expression) || + !inits.has(left.expression.text) || + left.arguments.nodes.length !== 0 + ) + continue; + initCall = left; + w = unparen(w.right); + } + if ( + !ts.isCallExpression(w) || + !ts.isIdentifier(w.expression) || + helperBase(w.expression.text) !== "__toCommonJS" || + w.arguments.nodes.length !== 1 || + !ts.isIdentifier(w.arguments.nodes[0]!) + ) + continue; + const target = (w.arguments.nodes[0] as ts.Identifier).text; + const table = tables.get(target); + if (table === undefined) continue; + const loaderWhy = { + degrade: `its memoized lazy loader '${name}' returns the namespace of a bundled ES module as a value, a module object the static rewrite cannot represent (only member reads of its result compile)`, + }; + const memo = memoize.left.text; + const memoDecl = top.get(memo)?.node; + if ( + memoDecl === undefined || + !ts.isVariableDeclaration(memoDecl) || + memoDecl.initializer !== undefined || + writes.get(memo) !== 1 || + countDeclarations(sf, memo) !== 1 || + (refsByName.get(memo) ?? []).some((x) => x !== memoize.left) || + writes.has(name) || + countDeclarations(sf, name) !== 1 + ) + return loaderWhy; + const entries = new Map(table.entries); + for (const ref of refsByName.get(name) ?? []) { + const call = ref.parent; + if ( + call === undefined || + !ts.isCallExpression(call) || + call.expression !== ref || + call.arguments.nodes.length !== 0 + ) + return loaderWhy; + let access: ts.Node | undefined = call.parent; + if ( + access === undefined || + !ts.isPropertyAccessExpression(access) || + access.expression !== call || + !plainRead(access) + ) + return loaderWhy; + // Node mode binds `default` to the namespace object itself. + if (interop && nodeMode && access.name.text === "default") { + const outer: ts.Node | undefined = access.parent; + if ( + outer === undefined || + !ts.isPropertyAccessExpression(outer) || + outer.expression !== access || + !plainRead(outer) + ) + return loaderWhy; + access = outer; + } + const member = (access as ts.PropertyAccessExpression).name.text; + const value = entries.get(member); + if (value === undefined || shadowed(access, table.entries)) return loaderWhy; + const init = initCall === null ? "" : `${initCall.getText(sf)}, `; + edits.push({ + start: access.getStart(sf), + end: access.getEnd(), + text: `(${init}${getterText(value)})`, + }); + } + edits.push(...removeDeclarators(sf, new Set([d, memoDecl]))); + loaderSpans.push({ start: d.getStart(sf), end: d.getEnd() }); + lowered.add(target); + } + for (const r of allRefs) { const table = tables.get(r.text); if (table === undefined) continue; + if (loaderSpans.some((span) => r.getStart(sf) >= span.start && r.getEnd() <= span.end)) + continue; const p = r.parent; // The table's own declaration and __export call are handled below. if (p !== undefined && ts.isVariableDeclaration(p) && p.name === r) continue; @@ -528,29 +673,27 @@ export function lowerBundledEsmInits( } // The getters read top-level bindings: nothing between the use site // and the top level may rebind one of their names. - const read = new Set(); - for (const [, value] of table.entries) for (const x of references(value)) read.add(x.text); - for (let a = p.parent; a !== undefined && !ts.isSourceFile(a); a = a.parent) { - if ( - ts.isFunctionLike(a) || - ts.isBlock(a) || - ts.isClassDeclaration(a) || - ts.isClassExpression(a) - ) { - for (const n of declaredNames(a)) { - if (read.has(n)) return { degrade: `its shipped JS ${why}` }; - } - } - } + if (shadowed(p, table.entries)) return { degrade: `its shipped JS ${why}` }; const getters = table.entries .map( ([n, value]) => - `get ${/^[A-Za-z_$][\w$]*$/.test(n) ? n : JSON.stringify(n)}() { return ${applyEdits(source, value.getStart(sf), value.getEnd(), edits)}; }`, + `get ${/^[A-Za-z_$][\w$]*$/.test(n) ? n : JSON.stringify(n)}() { return ${getterText(value)}; }`, ) .join(", "); edits.push({ start: p.getStart(sf), end: p.getEnd(), text: `{ ${getters} }` }); lowered.add(r.text); } + // A table nothing reads (its module is never required) goes too: its + // names are no exports of the bundle. + for (const [target, table] of tables) { + if (lowered.has(target)) continue; + const live = (refsByName.get(target) ?? []).some((x) => { + const p = x.parent; + if (p !== undefined && ts.isVariableDeclaration(p) && p.name === x) return false; + return !(x.getStart(sf) >= table.stmt.getStart(sf) && x.getEnd() <= table.stmt.getEnd()); + }); + if (!live) lowered.add(target); + } for (const target of lowered) { const table = tables.get(target)!; const decl = top.get(target)?.node; diff --git a/packages/compiler/src/frontend/npm-static-rewrite-host.ts b/packages/compiler/src/frontend/npm-static-rewrite-host.ts index 9ff14c455..4e0c92e31 100644 --- a/packages/compiler/src/frontend/npm-static-rewrite-host.ts +++ b/packages/compiler/src/frontend/npm-static-rewrite-host.ts @@ -181,10 +181,12 @@ export function rewriteBundlerCjsWithParser( source: string, filePath: string, liveExportTable?: (filePath: string) => void, + lexerVisibleNames?: (filePath: string, names: readonly string[]) => void, ): string | { degrade: string } | null { if (!isBundlerCjsCandidate(source)) return null; return rewriteBundlerCjsSyntax(parser.parse(filePath, source, "js"), filePath, { liveExportTable, + lexerVisibleNames, requireOnly: isVirtualSource, requireTargetEsModuleStamped: (from, spec) => requireTargetEsModuleStamped(parser, from, spec), starTargetNames: (from, spec) => { diff --git a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts index 08dbc0db5..3b553c3f2 100644 --- a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts +++ b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts @@ -22,6 +22,9 @@ export interface CjsRewriteHost { * exists, so Node's lexer-visible name set bounds nothing and every * name the export plumbing defines is part of its surface. */ requireOnly?: ((filePath: string) => boolean) | undefined; + /** Told the names Node's lexer detects in the original file that the + * rewritten text spells only as accessors (which the lexer skips). */ + lexerVisibleNames?: ((filePath: string, names: readonly string[]) => void) | undefined; } /** True when esbuild's export object (`module.exports = __toCommonJS(T)` @@ -119,8 +122,12 @@ interface EntrySpec { name: string; /** "ident": VALUE is a plain identifier spelled directly in the table. * "hoist": VALUE is an expression text hoisted to a tail const first. + * "getter": VALUE is a getter body's returned expression the table + * keeps as a live accessor (read on every access, like the original + * getter, so a lazily initialized module still initializes at the + * first read). * "any": the checked-dynamic fallback (`__scriptc_any`). */ - kind: "ident" | "hoist" | "any"; + kind: "ident" | "hoist" | "getter" | "any"; value?: string; } @@ -760,7 +767,14 @@ export function rewriteBundlerCjsSyntax( ...exportTables.filter((t) => t.target !== esbuildMainTarget), ]; for (const t of tablesOrdered) { - for (const [name, value] of t.entries) claim(name, classify(name, value)); + for (const [name, value] of t.entries) { + const spec = classify(name, value); + // The table feeding module.exports keeps a getter it cannot chase + // as a live accessor rather than a snapshot. + if (spec.kind === "any" && value !== null && t.target === esbuildMainTarget) + claim(name, { name, kind: "getter", value: value.getText() }); + else claim(name, spec); + } } for (const d of definePropEntries) claim(d.name, classify(d.name, d.value)); @@ -1039,6 +1053,8 @@ export function rewriteBundlerCjsSyntax( const v = `__scriptc_e${hoistN++}`; hoists.push(`const ${v} = ${dropInteropDefault(spec.value!)};`); plainParts.push(`${keyOf(spec.name)}: ${v},`); + } else if (spec.kind === "getter") { + plainParts.push(`get ${keyOf(spec.name)}() { return ${dropInteropDefault(spec.value!)}; },`); } else { plainParts.push(`${keyOf(spec.name)}: __scriptc_any,`); } @@ -1064,5 +1080,13 @@ export function rewriteBundlerCjsSyntax( lines.push( `module.exports = {${spreadParts.join("")}${plainParts.join("")}${esModulePart}${memberParts.join("")}};`, ); + /* Node's lexer sees the original's names for these entries (esbuild's + * dead annotation), but no accessor of the canonical table: the host + * keeps those names lexer-visible, so ES-module importers link exactly + * as they do against the original. */ + const accessorNames = [...entries.values()] + .filter((spec) => spec.kind === "getter" && lexed.exports.has(spec.name)) + .map((spec) => spec.name); + if (accessorNames.length > 0) host.lexerVisibleNames?.(filePath, accessorNames); return text + lines.join("\n") + "\n"; } diff --git a/packages/compiler/src/frontend/npm-static.ts b/packages/compiler/src/frontend/npm-static.ts index 4c1bc9ae6..77eab1d3a 100644 --- a/packages/compiler/src/frontend/npm-static.ts +++ b/packages/compiler/src/frontend/npm-static.ts @@ -43,6 +43,14 @@ export function npmStaticLiveExportTable(path: string): boolean { return liveExportFiles.has(path.split("\\").join("/")); } +/** Names Node's lexer detects in a rewritten file's original text that + * its rewrite spells only as accessors (the lexer skips those). */ +const lexerNames = new Map(); + +export function npmStaticLexerVisibleNames(path: string): readonly string[] { + return lexerNames.get(path.split("\\").join("/")) ?? []; +} + export function setNpmStaticPackages>(packages: T): void { activePackages = new Set(packages); declarationOverloads = new Map(); @@ -51,6 +59,7 @@ export function setNpmStaticPackages>(packages: T): v fileDegrades.clear(); rewriteCache.clear(); liveExportFiles.clear(); + lexerNames.clear(); clearVirtualSources(); untypedPkgCache.clear(); realpathProbed.clear(); @@ -439,6 +448,7 @@ export function npmStaticFsShadow(services: FrontendServices): NpmStaticFsShadow source, path, (live) => liveExportFiles.add(live.split("\\").join("/")), + (file, names) => lexerNames.set(file.split("\\").join("/"), names), ); if (answer !== null && typeof answer === "object") { reportFileDegrade(path, target.pkg, answer.degrade); diff --git a/packages/compiler/src/frontend/program.ts b/packages/compiler/src/frontend/program.ts index 908a587ea..5c824c8ee 100644 --- a/packages/compiler/src/frontend/program.ts +++ b/packages/compiler/src/frontend/program.ts @@ -68,6 +68,7 @@ import { isNpmStaticPackage, npmStaticActive, npmStaticFsShadow, + npmStaticLexerVisibleNames, npmStaticLiveExportTable, npmStaticPackageOfPath, reportNpmStaticOffender, @@ -5440,12 +5441,18 @@ function cjsNamedImportLinkCheck( npmStaticDepSf7(program, from, spec, "require"); return dep !== null && isCjsJsFile7(dep, program) ? dep : null; }; + // A bundler rewrite spells some lexer-visible names as accessors. + const lexWithAccessorNames = (sf: ts.SourceFile): ReturnType => { + const lexed = cjsLexedExportsOfFile(sf); + for (const name of npmStaticLexerVisibleNames(sf.fileName)) lexed.exports.add(name); + return lexed; + }; const visible = (dep: ts.SourceFile, name: string): boolean => // `default` is the module.exports binding itself — always provided. // The program already owns native source ASTs; reuse them without // starting another syntax session or reparsing their source text. name === "default" || - cjsVisibleNames(dep, cjsLexedExportsOfFile, resolveCjsDep, lexMemo).has(name); + cjsVisibleNames(dep, lexWithAccessorNames, resolveCjsDep, lexMemo).has(name); /** The statement's resolved LOCAL CommonJS dependency, when it is an * import/re-export from one. */ diff --git a/packages/compiler/src/frontend/services.ts b/packages/compiler/src/frontend/services.ts index aeed52161..c5dcbf513 100644 --- a/packages/compiler/src/frontend/services.ts +++ b/packages/compiler/src/frontend/services.ts @@ -64,10 +64,17 @@ export class FrontendServices { source: string, path: string, liveExportTable?: (path: string) => void, + lexerVisibleNames?: (path: string, names: readonly string[]) => void, ): string | { degrade: string } | null { this.ensureOpen(); if (!isBundlerCjsCandidate(source)) return null; - return rewriteBundlerCjsWithParser(this.sourceParser(), source, path, liveExportTable); + return rewriteBundlerCjsWithParser( + this.sourceParser(), + source, + path, + liveExportTable, + lexerVisibleNames, + ); } /** A CommonJS bundle's __commonJS chunks as their own modules (null: no * chunks; a degrade reason: chunks the recognizer cannot move). */ diff --git a/tests/fixtures/npm/cases/2564-lazy-barrel/main.ts b/tests/fixtures/npm/cases/2564-lazy-barrel/main.ts new file mode 100644 index 000000000..31c88db9b --- /dev/null +++ b/tests/fixtures/npm/cases/2564-lazy-barrel/main.ts @@ -0,0 +1,19 @@ +// A barrel whose re-exports load their modules lazily: a build plugin +// (real esbuild output, plain and whitespace/syntax minified) turns each +// re-export into a getter over a memoized loader of a lazily initialized +// ES module. Node's ES-module import of a CommonJS module reads every +// lexer-visible export when it links, so each module initializes once, +// before main runs, and the imports are snapshots (greet reassigns +// GREETING afterwards). +import { greet, GREETING, Counter, describe } from "gtlazy"; +import { describe as wsDescribe, GREETING as wsGreeting } from "gtlazyws"; + +console.log("main start"); +console.log(GREETING); +console.log(greet("a")); +console.log(GREETING); +const c = new Counter(); +console.log(c.next(), c.next()); +console.log(describe()); +console.log(wsDescribe()); +console.log(wsGreeting); diff --git a/tests/fixtures/npm/cases/2565-lazy-barrel-require/main.ts b/tests/fixtures/npm/cases/2565-lazy-barrel-require/main.ts new file mode 100644 index 000000000..42bed4781 --- /dev/null +++ b/tests/fixtures/npm/cases/2565-lazy-barrel-require/main.ts @@ -0,0 +1,9 @@ +// The lazy barrel (gtlazy) required from CommonJS: require() hands out the +// export object itself, so each re-export's getter runs at its read. A +// module initializes at the first read of one of its exports, once, and +// later reads see its live bindings (greet reassigns GREETING). +import { first, again } from "gtlazyuser"; + +console.log("main start"); +console.log(first()); +console.log(again()); diff --git a/tests/fixtures/npm/node_modules/gtlazy/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtlazy/dist/index.d.ts new file mode 100644 index 000000000..d3b4bac73 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtlazy/dist/index.d.ts @@ -0,0 +1,7 @@ +export declare let GREETING: string; +export declare function greet(name: string): string; +export declare class Counter { + n: number; + next(): number; +} +export declare function describe(): string; diff --git a/tests/fixtures/npm/node_modules/gtlazy/dist/index.js b/tests/fixtures/npm/node_modules/gtlazy/dist/index.js new file mode 100644 index 000000000..9a94faa11 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtlazy/dist/index.js @@ -0,0 +1,116 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __esm = (fn, res) => function __init() { + return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; +}; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); + +// src/greet.ts +var greet_exports = {}; +__export(greet_exports, { + GREETING: () => GREETING, + greet: () => greet +}); +function greet(name) { + GREETING = "hi"; + return GREETING + " " + name; +} +var GREETING; +var init_greet = __esm({ + "src/greet.ts"() { + console.log("greet init"); + GREETING = "hello"; + } +}); + +// src/counter.ts +var counter_exports = {}; +__export(counter_exports, { + default: () => Counter +}); +var Counter; +var init_counter = __esm({ + "src/counter.ts"() { + console.log("counter init"); + Counter = class { + n = 0; + next() { + return ++this.n; + } + }; + } +}); + +// src/describe.ts +var describe_exports = {}; +__export(describe_exports, { + describe: () => describe +}); +function describe() { + return "described: " + greet("d"); +} +var init_describe = __esm({ + "src/describe.ts"() { + init_greet(); + console.log("describe init"); + } +}); + +// src/index.ts +var __create = Object.create; +var __defProp2 = Object.defineProperty; +var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor; +var __getOwnPropNames2 = Object.getOwnPropertyNames; +var __getProtoOf = Object.getPrototypeOf; +var __hasOwnProp2 = Object.prototype.hasOwnProperty; +var __export2 = (target, all) => { + for (var name in all) + __defProp2(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps2 = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames2(from)) + if (!__hasOwnProp2.call(to, key) && key !== except) + __defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable }); + } + return to; +}; +var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps2( + // If the importer is in node compatibility mode or this is not an ESM + // file that has been converted to a CommonJS file using a Babel- + // compatible transform (i.e. "__esModule" has not been set), then set + // "default" to the CommonJS "module.exports" for node compatibility. + isNodeMode || !mod || !mod.__esModule ? __defProp2(target, "default", { value: mod, enumerable: true }) : target, + mod +)); +var __toCommonJS2 = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod); +var stdin_exports = {}; +__export2(stdin_exports, { + Counter: () => load_import_counter().default, + GREETING: () => load_import_greet().GREETING, + describe: () => load_import_describe().describe, + greet: () => load_import_greet().greet +}); +module.exports = __toCommonJS2(stdin_exports); +var import_greet2; +var load_import_greet = () => import_greet2 ??= (init_greet(), __toCommonJS(greet_exports)); +var import_counter; +var load_import_counter = () => import_counter ??= __toESM((init_counter(), __toCommonJS(counter_exports))); +var import_describe; +var load_import_describe = () => import_describe ??= (init_describe(), __toCommonJS(describe_exports)); + +0 && (module.exports = {"Counter": null,"GREETING": null,"describe": null,"greet": null}); diff --git a/tests/fixtures/npm/node_modules/gtlazy/package.json b/tests/fixtures/npm/node_modules/gtlazy/package.json new file mode 100644 index 000000000..e63f696a6 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtlazy/package.json @@ -0,0 +1,7 @@ +{ + "name": "gtlazy", + "version": "1.0.0", + "description": "esbuild 0.25.10 output (--bundle --platform=node --format=cjs) of a barrel whose re-exports a build plugin turns into memoized lazy loaders: `load_x = () => x ??= (init_x(), __toCommonJS(x_exports))`", + "main": "./dist/index.js", + "types": "./dist/index.d.ts" +} diff --git a/tests/fixtures/npm/node_modules/gtlazyuser/index.d.ts b/tests/fixtures/npm/node_modules/gtlazyuser/index.d.ts new file mode 100644 index 000000000..948fec847 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtlazyuser/index.d.ts @@ -0,0 +1,2 @@ +export declare function first(): string; +export declare function again(): string; diff --git a/tests/fixtures/npm/node_modules/gtlazyuser/index.js b/tests/fixtures/npm/node_modules/gtlazyuser/index.js new file mode 100644 index 000000000..d5666d6e1 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtlazyuser/index.js @@ -0,0 +1,10 @@ +"use strict"; +const lazy = require("gtlazy"); + +console.log("user loaded"); +exports.first = function first() { + return lazy.GREETING + "/" + lazy.greet("u"); +}; +exports.again = function again() { + return lazy.GREETING + "/" + lazy.describe(); +}; diff --git a/tests/fixtures/npm/node_modules/gtlazyuser/package.json b/tests/fixtures/npm/node_modules/gtlazyuser/package.json new file mode 100644 index 000000000..841bdc7b2 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtlazyuser/package.json @@ -0,0 +1,10 @@ +{ + "name": "gtlazyuser", + "version": "1.0.0", + "description": "a CommonJS package that require()s the gtlazy barrel: its lazy re-exports initialize at the first read and read live bindings", + "main": "index.js", + "types": "index.d.ts", + "dependencies": { + "gtlazy": "1.0.0" + } +} diff --git a/tests/fixtures/npm/node_modules/gtlazyws/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtlazyws/dist/index.d.ts new file mode 100644 index 000000000..d3b4bac73 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtlazyws/dist/index.d.ts @@ -0,0 +1,7 @@ +export declare let GREETING: string; +export declare function greet(name: string): string; +export declare class Counter { + n: number; + next(): number; +} +export declare function describe(): string; diff --git a/tests/fixtures/npm/node_modules/gtlazyws/dist/index.js b/tests/fixtures/npm/node_modules/gtlazyws/dist/index.js new file mode 100644 index 000000000..a472466b0 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtlazyws/dist/index.js @@ -0,0 +1,3 @@ +var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __esm=(fn,res)=>function(){return fn&&(res=(0,fn[__getOwnPropNames(fn)[0]])(fn=0)),res};var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:!0})},__copyProps=(to,from,except,desc)=>{if(from&&typeof from=="object"||typeof from=="function")for(let key of __getOwnPropNames(from))!__hasOwnProp.call(to,key)&&key!==except&&__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable});return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:!0}),mod);var greet_exports={};__export(greet_exports,{GREETING:()=>GREETING,greet:()=>greet});function greet(name){return GREETING="hi",GREETING+" "+name}var GREETING,init_greet=__esm({"src/greet.ts"(){console.log("greet init");GREETING="hello"}});var counter_exports={};__export(counter_exports,{default:()=>Counter});var Counter,init_counter=__esm({"src/counter.ts"(){console.log("counter init");Counter=class{n=0;next(){return++this.n}}}});var describe_exports={};__export(describe_exports,{describe:()=>describe});function describe(){return"described: "+greet("d")}var init_describe=__esm({"src/describe.ts"(){init_greet();console.log("describe init")}});var __create=Object.create,__defProp2=Object.defineProperty,__getOwnPropDesc2=Object.getOwnPropertyDescriptor,__getOwnPropNames2=Object.getOwnPropertyNames,__getProtoOf=Object.getPrototypeOf,__hasOwnProp2=Object.prototype.hasOwnProperty,__export2=(target,all)=>{for(var name in all)__defProp2(target,name,{get:all[name],enumerable:!0})},__copyProps2=(to,from,except,desc)=>{if(from&&typeof from=="object"||typeof from=="function")for(let key of __getOwnPropNames2(from))!__hasOwnProp2.call(to,key)&&key!==except&&__defProp2(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc2(from,key))||desc.enumerable});return to},__toESM=(mod,isNodeMode,target)=>(target=mod!=null?__create(__getProtoOf(mod)):{},__copyProps2(isNodeMode||!mod||!mod.__esModule?__defProp2(target,"default",{value:mod,enumerable:!0}):target,mod)),__toCommonJS2=mod=>__copyProps2(__defProp2({},"__esModule",{value:!0}),mod),stdin_exports={};__export2(stdin_exports,{Counter:()=>load_import_counter().default,GREETING:()=>load_import_greet().GREETING,describe:()=>load_import_describe().describe,greet:()=>load_import_greet().greet});module.exports=__toCommonJS2(stdin_exports);var import_greet2,load_import_greet=()=>import_greet2??=(init_greet(),__toCommonJS(greet_exports)),import_counter,load_import_counter=()=>import_counter??=__toESM((init_counter(),__toCommonJS(counter_exports))),import_describe,load_import_describe=()=>import_describe??=(init_describe(),__toCommonJS(describe_exports)); + +0 && (module.exports = {"Counter": null,"GREETING": null,"describe": null,"greet": null}); diff --git a/tests/fixtures/npm/node_modules/gtlazyws/package.json b/tests/fixtures/npm/node_modules/gtlazyws/package.json new file mode 100644 index 000000000..bf5ec5b93 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtlazyws/package.json @@ -0,0 +1,7 @@ +{ + "name": "gtlazyws", + "version": "1.0.0", + "description": "the gtlazy source and plugin bundled by esbuild 0.25.10 with --minify-whitespace --minify-syntax", + "main": "./dist/index.js", + "types": "./dist/index.d.ts" +} diff --git a/tests/harness/npm-cases.ts b/tests/harness/npm-cases.ts index f8d346154..87a6de2a8 100644 --- a/tests/harness/npm-cases.ts +++ b/tests/harness/npm-cases.ts @@ -19,12 +19,12 @@ export function npmCases(fixturesRoot: string): NpmCase[] { return [ ...globSync(join(fixturesRoot, "npm/cases/*/main.ts")) .sort() - // 2465-2469 and 2556-2563 are the --npm-static bundler-emitted-CJS + // 2465-2469 and 2556-2565 are the --npm-static bundler-emitted-CJS // cases (npm-static.test.ts drives them with the opt-in): their // consumers exercise the static rewrite's surface (lexer-visible // names the shipped .d.ts never declares, the __toESM interop // family), so they stay out of the flagless island lane by design. - .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-3])-[^/]+\/main\.ts$/.test(entry)) + .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-5])-[^/]+\/main\.ts$/.test(entry)) .map((entry) => ({ name: entry.split("/").at(-2)!, entry, diff --git a/tests/harness/npm-static.test.ts b/tests/harness/npm-static.test.ts index 8560973cd..943b81695 100644 --- a/tests/harness/npm-static.test.ts +++ b/tests/harness/npm-static.test.ts @@ -76,7 +76,7 @@ async function buildStatic(entry: string, npmStatic: string[] | "auto"): Promise ...globSync( join(fixturesRoot, "npm/node_modules/cryptozoo/**/*.{js,mjs,cjs,json,d.ts}"), ).sort(), - // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2563) + // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2565) ...globSync(join(fixturesRoot, "npm/node_modules/gt*/**/*.{js,cjs,json}")).sort(), ]; for (const f of inputs) hash.update(f).update(readFileSync(f)); @@ -1478,6 +1478,17 @@ describe(`npm-static pilots${sanitize ? " (sanitized)" : ""}`, () => { ]); }, 180_000); + // 2564-2565: a barrel whose re-exports are memoized lazy loaders of + // lazily initialized ES modules (a bundle plugin's shape): ES-module + // importers copy every export when they link (Node's CommonJS import), + // require() reads each export's getter at the read. + test.each([ + ["2564-lazy-barrel", ["gtlazy", "gtlazyws"]], + ["2565-lazy-barrel-require", ["gtlazyuser", "gtlazy"]], + ])("the lazy barrel %s compiles statically and byte-matches Node", async (name, packages) => { + await expectNodeParity(join(fixturesRoot, `npm/cases/${name}/main.ts`), packages); + }, 180_000); + // 2562: a file with unverifiable interop that no compiled code can load // (the program imports a leaf data module and only a type from the // main entry) leaves its package static. From 503154e3874f3c82f1954377f9e5b6adc0744c3a Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 15:21:03 -0700 Subject: [PATCH 30/83] Compile the boot-path Node surface: process.send presence, process.exitCode reads, wider stream hooks, void race entries, RequestOptions and promisify of callbacks - `if (process.send)`, `Boolean(process.send)`, `!process.send` and `typeof process.send` answer whether the process has an IPC channel (a fork channel this binary opened, or NODE_CHANNEL_FD from a Node.js parent). The same holds for process.disconnect. - In a program without fork targets, `process.send?.(...)` answers undefined, and a direct call throws Node's TypeError when there is no channel. A Node.js parent's channel has no lowering, so a call fences when it runs. `process.send!(...)` lowers like the direct call. - process.exitCode reads answer the assigned code, or undefined. Only programs that read it record assignments, including process.exit(code) before the 'exit' listeners run. Other programs keep the untracked write. - A stream subclass hook may declare a supertype of Node's argument (`_write(chunk: Buffer | string, ...)`): the wrapper takes Node's Buffer and widens it. - Promise.race accepts a Promise entry when the result holds undefined. - http.RequestOptions and https.RequestOptions as declared types ride checked-dynamic storage, like SpawnOptions. - util.promisify over a typed callback-last function value (a bound method, a declaration, an arrow) builds a native promisified function. A truthy error rejects, the value fulfills, and a synchronous throw rejects. - The bundled Node declarations gain the callback-only generic promisify overloads. Before this, `promisify(obj.close.bind(obj))` matched crypto.randomFill's overload and reported "Expected 1-3 arguments". Co-Authored-By: Claude Opus 5.5 --- .../generated/node-v24-backlog.json | 27 +- .../generated/node-v24-internal.json | 34 +- internal/compatibility/static-support.json | 6 +- .../ambient/scriptc-node-fallback.d.ts | 7 + .../src/backend/llvm/expr-callbacks.ts | 17 +- .../compiler/src/backend/llvm/lib-shared.ts | 3 + .../src/frontend/lowering/builtin-calls.ts | 5 + .../lowering/builtins/callable-aliases.ts | 8 +- .../src/frontend/lowering/builtins/ipc.ts | 58 ++- .../src/frontend/lowering/builtins/process.ts | 213 ++++++++++- .../frontend/lowering/builtins/promises.ts | 3 + .../lowering/builtins/promisify-callback.ts | 357 ++++++++++++++++++ .../src/frontend/lowering/lower-exprs.ts | 20 +- .../src/frontend/lowering/lower-stmts.ts | 7 +- .../src/frontend/lowering/lower-stream.ts | 24 +- .../compiler/src/frontend/lowering/lowerer.ts | 2 + .../src/frontend/lowering/surfaces.ts | 6 +- packages/compiler/src/frontend/type-mapper.ts | 23 ++ packages/compiler/src/ir/builtin-effects.ts | 1 + .../compiler/src/ir/builtin-signatures.ts | 8 + packages/compiler/src/ir/runtime-features.ts | 1 + packages/compiler/surface-manifest.json | 4 +- .../corpus/6262-process-send-presence.ts.json | 6 + .../main.ts.json | 6 + .../corpus/6264-process-exitcode-read.ts.json | 6 + .../6265-process-exitcode-default.ts.json | 6 + .../6266-process-exit-keeps-exitcode.ts.json | 6 + .../6267-stream-hook-wider-chunk.ts.json | 6 + .../6268-promise-race-void-entry.ts.json | 6 + .../6269-http-request-options-type.ts.json | 6 + .../6270-promisify-callback-functions.ts.json | 6 + .../boot-path-node-surface-fences.ts.json | 6 + .../promisify-callback-functions.ts.json | 6 + packages/runtime/src/scr_child.c | 1 + packages/runtime/src/scr_child.h | 3 + packages/runtime/src/scr_lib.c | 13 + packages/runtime/src/scr_lib.h | 5 + tests/corpus/6262-process-send-presence.ts | 24 ++ .../6263-process-send-presence-fork/main.ts | 13 + .../6263-process-send-presence-fork/worker.ts | 6 + tests/corpus/6264-process-exitcode-read.ts | 22 ++ tests/corpus/6265-process-exitcode-default.ts | 10 + .../6266-process-exit-keeps-exitcode.ts | 5 + tests/corpus/6267-stream-hook-wider-chunk.ts | 46 +++ tests/corpus/6268-promise-race-void-entry.ts | 31 ++ .../corpus/6269-http-request-options-type.ts | 10 + .../6270-promisify-callback-functions.ts | 65 ++++ .../boot-path-node-surface-fences.ts | 23 ++ .../promisify-callback-functions.ts | 67 ++++ .../boot-path-node-surface-fences.ts.txt | 35 ++ tests/harness/project-config.test.ts | 18 + 51 files changed, 1267 insertions(+), 30 deletions(-) create mode 100644 packages/compiler/src/frontend/lowering/builtins/promisify-callback.ts create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6262-process-send-presence.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6263-process-send-presence-fork/main.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6264-process-exitcode-read.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6265-process-exitcode-default.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6266-process-exit-keeps-exitcode.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6267-stream-hook-wider-chunk.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6268-promise-race-void-entry.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6269-http-request-options-type.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6270-promisify-callback-functions.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/boot-path-node-surface-fences.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/node-types/promisify-callback-functions.ts.json create mode 100644 tests/corpus/6262-process-send-presence.ts create mode 100644 tests/corpus/6263-process-send-presence-fork/main.ts create mode 100644 tests/corpus/6263-process-send-presence-fork/worker.ts create mode 100644 tests/corpus/6264-process-exitcode-read.ts create mode 100644 tests/corpus/6265-process-exitcode-default.ts create mode 100644 tests/corpus/6266-process-exit-keeps-exitcode.ts create mode 100644 tests/corpus/6267-stream-hook-wider-chunk.ts create mode 100644 tests/corpus/6268-promise-race-void-entry.ts create mode 100644 tests/corpus/6269-http-request-options-type.ts create mode 100644 tests/corpus/6270-promisify-callback-functions.ts create mode 100644 tests/diagnostics/boot-path-node-surface-fences.ts create mode 100644 tests/fixtures/node-types/promisify-callback-functions.ts create mode 100644 tests/harness/__snapshots__/boot-path-node-surface-fences.ts.txt diff --git a/internal/compatibility/generated/node-v24-backlog.json b/internal/compatibility/generated/node-v24-backlog.json index 75e789c61..6b570257e 100644 --- a/internal/compatibility/generated/node-v24-backlog.json +++ b/internal/compatibility/generated/node-v24-backlog.json @@ -55949,7 +55949,9 @@ "confidence": "high", "source": "compiler-feature:process.'disconnect'", "tests": [ - "tests/corpus/2946-child-fork-ipc/main.ts" + "tests/corpus/2946-child-fork-ipc/main.ts", + "tests/corpus/6262-process-send-presence.ts", + "tests/corpus/6263-process-send-presence-fork/main.ts" ] } } @@ -55991,7 +55993,9 @@ "confidence": "high", "source": "compiler-feature:process.'message'", "tests": [ - "tests/corpus/2946-child-fork-ipc/main.ts" + "tests/corpus/2946-child-fork-ipc/main.ts", + "tests/corpus/6262-process-send-presence.ts", + "tests/corpus/6263-process-send-presence-fork/main.ts" ] } } @@ -56374,7 +56378,9 @@ "confidence": "high", "source": "compiler-feature:process.process.disconnect", "tests": [ - "tests/corpus/2946-child-fork-ipc/main.ts" + "tests/corpus/2946-child-fork-ipc/main.ts", + "tests/corpus/6262-process-send-presence.ts", + "tests/corpus/6263-process-send-presence-fork/main.ts" ] } } @@ -57022,7 +57028,9 @@ "confidence": "high", "source": "compiler-feature:process.process.send", "tests": [ - "tests/corpus/2946-child-fork-ipc/main.ts" + "tests/corpus/2946-child-fork-ipc/main.ts", + "tests/corpus/6262-process-send-presence.ts", + "tests/corpus/6263-process-send-presence-fork/main.ts" ] } } @@ -57476,7 +57484,9 @@ "confidence": "high", "source": "compiler-feature:process.connected", "tests": [ - "tests/corpus/2946-child-fork-ipc/main.ts" + "tests/corpus/2946-child-fork-ipc/main.ts", + "tests/corpus/6262-process-send-presence.ts", + "tests/corpus/6263-process-send-presence-fork/main.ts" ] } } @@ -57591,7 +57601,10 @@ "tests/corpus/2941-process-exit-code.ts", "tests/corpus/2942-process-exit-code-override.ts", "tests/corpus/2943-process-exit-code-invalid.ts", - "tests/corpus/2944-process-exit-code-wrap.ts" + "tests/corpus/2944-process-exit-code-wrap.ts", + "tests/corpus/6264-process-exitcode-read.ts", + "tests/corpus/6265-process-exitcode-default.ts", + "tests/corpus/6266-process-exit-keeps-exitcode.ts" ] } } @@ -76174,6 +76187,8 @@ "tests/fixtures/node-types/promisify-crypto.ts", "tests/corpus/5632-promisify-dns.ts", "tests/fixtures/node-types/promisify-dns.ts", + "tests/corpus/6270-promisify-callback-functions.ts", + "tests/fixtures/node-types/promisify-callback-functions.ts", "tests/harness/project-config.test.ts" ] } diff --git a/internal/compatibility/generated/node-v24-internal.json b/internal/compatibility/generated/node-v24-internal.json index c93030602..d95dba97a 100644 --- a/internal/compatibility/generated/node-v24-internal.json +++ b/internal/compatibility/generated/node-v24-internal.json @@ -79378,6 +79378,11 @@ "tests/corpus/5210-process-memory-usage.ts", "tests/corpus/5211-process-identity.ts", "tests/corpus/5809-process-exit-and-rejections.cjs", + "tests/corpus/6262-process-send-presence.ts", + "tests/corpus/6263-process-send-presence-fork/main.ts", + "tests/corpus/6264-process-exitcode-read.ts", + "tests/corpus/6265-process-exitcode-default.ts", + "tests/corpus/6266-process-exit-keeps-exitcode.ts", "tests/corpus/990-process-basics.ts", "tests/corpus/node24-env-files-errors.ts", "tests/corpus/node24-env-files.ts", @@ -79455,7 +79460,9 @@ "status": "partial", "evidence": "compiler-feature:process.'disconnect'", "tests": [ - "tests/corpus/2946-child-fork-ipc/main.ts" + "tests/corpus/2946-child-fork-ipc/main.ts", + "tests/corpus/6262-process-send-presence.ts", + "tests/corpus/6263-process-send-presence-fork/main.ts" ] }, "anchorSource": "exact" @@ -79494,7 +79501,9 @@ "status": "partial", "evidence": "compiler-feature:process.'message'", "tests": [ - "tests/corpus/2946-child-fork-ipc/main.ts" + "tests/corpus/2946-child-fork-ipc/main.ts", + "tests/corpus/6262-process-send-presence.ts", + "tests/corpus/6263-process-send-presence-fork/main.ts" ] }, "anchorSource": "exact" @@ -79907,7 +79916,9 @@ "status": "partial", "evidence": "compiler-feature:process.process.disconnect", "tests": [ - "tests/corpus/2946-child-fork-ipc/main.ts" + "tests/corpus/2946-child-fork-ipc/main.ts", + "tests/corpus/6262-process-send-presence.ts", + "tests/corpus/6263-process-send-presence-fork/main.ts" ] }, "anchorSource": "exact" @@ -80552,7 +80563,9 @@ "status": "partial", "evidence": "compiler-feature:process.process.send", "tests": [ - "tests/corpus/2946-child-fork-ipc/main.ts" + "tests/corpus/2946-child-fork-ipc/main.ts", + "tests/corpus/6262-process-send-presence.ts", + "tests/corpus/6263-process-send-presence-fork/main.ts" ] }, "anchorSource": "exact" @@ -80971,7 +80984,9 @@ "status": "partial", "evidence": "compiler-feature:process.connected", "tests": [ - "tests/corpus/2946-child-fork-ipc/main.ts" + "tests/corpus/2946-child-fork-ipc/main.ts", + "tests/corpus/6262-process-send-presence.ts", + "tests/corpus/6263-process-send-presence-fork/main.ts" ] }, "anchorSource": "chapter" @@ -81080,7 +81095,10 @@ "tests/corpus/2941-process-exit-code.ts", "tests/corpus/2942-process-exit-code-override.ts", "tests/corpus/2943-process-exit-code-invalid.ts", - "tests/corpus/2944-process-exit-code-wrap.ts" + "tests/corpus/2944-process-exit-code-wrap.ts", + "tests/corpus/6264-process-exitcode-read.ts", + "tests/corpus/6265-process-exitcode-default.ts", + "tests/corpus/6266-process-exit-keeps-exitcode.ts" ] }, "anchorSource": "chapter" @@ -101165,6 +101183,7 @@ "tests/corpus/5832-util-types-brands-dynamic.js", "tests/corpus/5835-util-format-integer-specifiers.ts", "tests/corpus/5884-inspect-layout-options.ts", + "tests/corpus/6270-promisify-callback-functions.ts", "tests/corpus/node24-deep-equality-properties.js", "tests/corpus/node24-deep-equality.cjs", "tests/corpus/node24-deep-equality.ts", @@ -101191,6 +101210,7 @@ "tests/corpus/text-codec-values/main.ts", "tests/fixtures/node-types/deep-equality.ts", "tests/fixtures/node-types/env-files.ts", + "tests/fixtures/node-types/promisify-callback-functions.ts", "tests/fixtures/node-types/promisify-crypto.ts", "tests/fixtures/node-types/promisify-dns.ts", "tests/fixtures/node-types/promisify-fs.ts", @@ -102849,6 +102869,8 @@ "tests/fixtures/node-types/promisify-crypto.ts", "tests/corpus/5632-promisify-dns.ts", "tests/fixtures/node-types/promisify-dns.ts", + "tests/corpus/6270-promisify-callback-functions.ts", + "tests/fixtures/node-types/promisify-callback-functions.ts", "tests/harness/project-config.test.ts" ] }, diff --git a/internal/compatibility/static-support.json b/internal/compatibility/static-support.json index 71605612e..106d38d00 100644 --- a/internal/compatibility/static-support.json +++ b/internal/compatibility/static-support.json @@ -148,7 +148,7 @@ "node-builtin.process.versions": ["tests/corpus/process-versions-storage.ts"], "node-builtin.process.rows": ["tests/corpus/2992-stream-rows.ts", "tests/harness/terminal-geometry.test.ts"], "node-builtin.process.columns": ["tests/corpus/1448-stream-columns.ts", "tests/harness/terminal-geometry.test.ts"], - "node-builtin.process.exitCode": ["tests/corpus/5809-process-exit-and-rejections.cjs", "tests/corpus/2941-process-exit-code.ts", "tests/corpus/2942-process-exit-code-override.ts", "tests/corpus/2943-process-exit-code-invalid.ts", "tests/corpus/2944-process-exit-code-wrap.ts"], + "node-builtin.process.exitCode": ["tests/corpus/5809-process-exit-and-rejections.cjs", "tests/corpus/2941-process-exit-code.ts", "tests/corpus/2942-process-exit-code-override.ts", "tests/corpus/2943-process-exit-code-invalid.ts", "tests/corpus/2944-process-exit-code-wrap.ts", "tests/corpus/6264-process-exitcode-read.ts", "tests/corpus/6265-process-exitcode-default.ts", "tests/corpus/6266-process-exit-keeps-exitcode.ts"], "node-builtin.punycode": ["tests/corpus/6122-punycode-conversions.ts", "tests/corpus/6124-punycode-require-warnings.cjs", "tests/corpus/6126-punycode-dependency/main.cjs"], "node-builtin.querystring": ["tests/corpus/957-builtins-namespace.ts", "tests/corpus/2917-builtin-callable-widths.ts"], "node-builtin.readline": ["tests/corpus/1475-readline-closed-stdin.ts"], @@ -333,7 +333,7 @@ { "chapter": "fs", "signatures": ["`fsPromises.lstat(path[, options])`"], "status": "partial", "evidence": ["tests/corpus/2941-fsp-lstat-identity.ts"] }, { "chapter": "fs", "signatures": ["`fs.globSync(pattern[, options])`", "`fs.glob(pattern[, options], callback)`", "`fsPromises.glob(pattern[, options])`"], "status": "partial", "evidence": ["tests/corpus/5448-fs-glob-patterns.ts", "tests/corpus/5449-fs-glob-options.ts", "tests/corpus/5450-fs-glob-validation.js", "tests/corpus/5451-fs-promises-glob.ts", "tests/corpus/5452-fs-glob-callback.ts", "tests/fixtures/node-types/fs-glob.ts"] }, { "chapter": "fs", "signatures": ["`fs.cpSync(src, dest[, options])`", "`fs.cp(src, dest[, options], callback)`", "`fsPromises.cp(src, dest[, options])`"], "status": "partial", "evidence": ["tests/corpus/5567-fs-cpsync.ts", "tests/corpus/5568-fs-cp-errors.js", "tests/corpus/5569-fs-cp-symlinks.ts", "tests/corpus/5570-fs-cp-async.ts"] }, - { "chapter": "process", "signatures": ["`process.send(message[, sendHandle[, options]][, callback])`", "`process.disconnect()`", "`connected` Type: {boolean}", "Event: `'message'`", "Event: `'disconnect'`"], "status": "partial", "evidence": ["tests/corpus/2946-child-fork-ipc/main.ts"] }, + { "chapter": "process", "signatures": ["`process.send(message[, sendHandle[, options]][, callback])`", "`process.disconnect()`", "`connected` Type: {boolean}", "Event: `'message'`", "Event: `'disconnect'`"], "status": "partial", "evidence": ["tests/corpus/2946-child-fork-ipc/main.ts", "tests/corpus/6262-process-send-presence.ts", "tests/corpus/6263-process-send-presence-fork/main.ts"] }, { "chapter": "fs", "symbols": ["fs.Stats", "stats.isBlockDevice", "stats.isCharacterDevice", "stats.isDirectory", "stats.isFIFO", "stats.isFile", "stats.isSocket", "stats.isSymbolicLink"], "status": "partial", "evidence": ["tests/corpus/5541-fs-stats-fields.cjs"] }, { "chapter": "fs", "parent": "Class: `fs.Stats`", "signatures": ["`mode` Type: {number|bigint}", "`nlink` Type: {number|bigint}", "`uid` Type: {number|bigint}", "`gid` Type: {number|bigint}", "`rdev` Type: {number|bigint}", "`size` Type: {number|bigint}", "`blksize` Type: {number|bigint}", "`blocks` Type: {number|bigint}", "`atimeMs` Type: {number|bigint}", "`mtimeMs` Type: {number|bigint}", "`birthtimeMs` Type: {number|bigint}", "`atimeNs` Type: {bigint}", "`mtimeNs` Type: {bigint}", "`ctimeNs` Type: {bigint}", "`birthtimeNs` Type: {bigint}", "`atime` Type: {Date}", "`mtime` Type: {Date}", "`ctime` Type: {Date}", "`birthtime` Type: {Date}"], "status": "partial", "evidence": ["tests/corpus/5541-fs-stats-fields.cjs"] }, { "chapter": "fs", "signatures": ["`dev` Type: {number|bigint}", "`ino` Type: {number|bigint}", "`ctimeMs` Type: {number|bigint}"], "status": "partial", "evidence": ["tests/corpus/2941-fsp-lstat-identity.ts"] }, @@ -370,7 +370,7 @@ { "chapter": "test", "parent": "Class: `TestContext`", "symbols": ["context.before", "context.after", "context.beforeEach", "context.afterEach", "context.plan", "context.fullName"], "status": "partial", "evidence": ["tests/fixtures/node-test/cases/ctx-hooks.ts", "tests/fixtures/node-test/cases/ctx-plan.ts", "tests/harness/node-test.test.ts"] }, { "chapter": "test", "symbols": ["getTestContext"], "status": "partial", "evidence": ["tests/fixtures/node-test/cases/get-test-context.ts", "tests/fixtures/node-test/cases/get-test-context-cancelled.ts", "tests/harness/node-test.test.ts"] }, { "chapter": "test", "symbols": ["describe.skip", "describe.todo", "describe.only", "suite.skip", "suite.todo", "suite.only", "it.skip", "it.todo", "it.only"], "status": "partial", "evidence": ["tests/fixtures/node-test/cases/suite-variants.ts", "tests/fixtures/node-test/cases/only-nested.ts", "tests/fixtures/node-test/cases/todo-suites.ts", "tests/harness/node-test.test.ts"] }, - { "chapter": "util", "signatures": ["`util.promisify(original)`"], "status": "partial", "evidence": ["tests/corpus/1473-promisify-execfile.ts", "tests/corpus/1573-promisify-execfile-env-spread.ts", "tests/corpus/2912-promisify-fs-readfile.ts", "tests/corpus/5625-promisify-exec.ts", "tests/corpus/5626-promisify-forms.ts", "tests/fixtures/node-types/promisify-fs.ts", "tests/corpus/5628-promisify-timers.ts", "tests/fixtures/node-types/promisify-timers.ts", "tests/corpus/5629-promisify-stream.ts", "tests/corpus/5630-promisify-zlib.ts", "tests/fixtures/node-types/promisify-zlib.ts", "tests/corpus/5631-promisify-crypto.ts", "tests/fixtures/node-types/promisify-crypto.ts", "tests/corpus/5632-promisify-dns.ts", "tests/fixtures/node-types/promisify-dns.ts", "tests/harness/project-config.test.ts"] }, + { "chapter": "util", "signatures": ["`util.promisify(original)`"], "status": "partial", "evidence": ["tests/corpus/1473-promisify-execfile.ts", "tests/corpus/1573-promisify-execfile-env-spread.ts", "tests/corpus/2912-promisify-fs-readfile.ts", "tests/corpus/5625-promisify-exec.ts", "tests/corpus/5626-promisify-forms.ts", "tests/fixtures/node-types/promisify-fs.ts", "tests/corpus/5628-promisify-timers.ts", "tests/fixtures/node-types/promisify-timers.ts", "tests/corpus/5629-promisify-stream.ts", "tests/corpus/5630-promisify-zlib.ts", "tests/fixtures/node-types/promisify-zlib.ts", "tests/corpus/5631-promisify-crypto.ts", "tests/fixtures/node-types/promisify-crypto.ts", "tests/corpus/5632-promisify-dns.ts", "tests/fixtures/node-types/promisify-dns.ts", "tests/corpus/6270-promisify-callback-functions.ts", "tests/fixtures/node-types/promisify-callback-functions.ts", "tests/harness/project-config.test.ts"] }, { "chapter": "typescript", "symbols": ["type"], "status": "supported", "evidence": ["tests/corpus/2702-type-import-link-static/main.ts", "tests/corpus/2703-type-import-link-static-reexport/main.ts", "tests/corpus/2704-type-import-link-static-success/main.ts", "tests/corpus/2707-type-import-link-static-default-reexport/main.ts", "tests/harness/errors.test.ts"] }, { "chapter": "zlib", "signatures": ["Class: `zlib.Deflate`", "Class: `zlib.Inflate`", "Class: `zlib.Gzip`", "Class: `zlib.Gunzip`", "Class: `zlib.DeflateRaw`", "Class: `zlib.InflateRaw`", "Class: `zlib.Unzip`", "Class: `zlib.ZlibBase`", "`zlib.close([callback])`", "`zlib.flush([kind, ]callback)`", "`zlib.params(level, strategy, callback)`", "`zlib.reset()`", "`bytesWritten` Type: {number}", "Class: `Options`"], "status": "partial", "evidence": ["tests/corpus/5370-zlib-stream-roundtrip.ts", "tests/corpus/5371-zlib-stream-chunking.cjs", "tests/corpus/5372-zlib-stream-errors.cjs", "tests/corpus/5373-zlib-stream-options.cjs", "tests/corpus/5374-zlib-stream-flush-params.ts", "tests/corpus/5375-zlib-stream-promises.ts", "tests/corpus/5377-zlib-stream-backpressure.cjs"] }, { "chapter": "zlib", "signatures": ["`zlib.constants`"], "status": "partial", "evidence": ["tests/corpus/5376-zlib-constants.cjs", "tests/corpus/5374-zlib-stream-flush-params.ts"] } diff --git a/packages/compiler/ambient/scriptc-node-fallback.d.ts b/packages/compiler/ambient/scriptc-node-fallback.d.ts index 3b522dd3e..3aca532ce 100644 --- a/packages/compiler/ambient/scriptc-node-fallback.d.ts +++ b/packages/compiler/ambient/scriptc-node-fallback.d.ts @@ -3267,6 +3267,13 @@ declare module "util" { info: import("node:crypto").BinaryLike, keylen: number, ) => Promise; + /* A callback-only function (`obj.close.bind(obj)`): before the targets + * whose leading parameters would otherwise capture it. Builtins that take + * arguments before their callback never match these. */ + export function promisify( + fn: (callback: (err: any, result: TResult) => void) => void, + ): () => Promise; + export function promisify(fn: (callback: (err?: any) => void) => void): () => Promise; export function promisify( fn: typeof import("node:crypto").randomFill, ): ( diff --git a/packages/compiler/src/backend/llvm/expr-callbacks.ts b/packages/compiler/src/backend/llvm/expr-callbacks.ts index d71df333a..db1dca1af 100644 --- a/packages/compiler/src/backend/llvm/expr-callbacks.ts +++ b/packages/compiler/src/backend/llvm/expr-callbacks.ts @@ -1,6 +1,13 @@ /* Focused LLVM expression emission extracted from emitter.ts. */ import { InternalCompilerError } from "../../errors.js"; -import { type IrType, isRefCounted, isUnitType, typeEquals, typeKey } from "../../ir/ir.js"; +import { + type IrType, + UNDEFINED_T, + isRefCounted, + isUnitType, + typeEquals, + typeKey, +} from "../../ir/ir.js"; import { mangleGenResThunk, mangleRecordNew, mangleRecordStruct } from "../mangle.js"; import { FN_ATTRS, llFieldType, releaseSym, retainSym, traceArg, vAdapters } from "./shapes.js"; import { LlvmUnsupportedError } from "./unsupported.js"; @@ -68,6 +75,14 @@ export function raceAdapterFor(host: LlvmEmitterContext, from: IrType, to: IrTyp `define internal void @${sym}(ptr %dst, ptr %src) ${FN_ATTRS} { ; race ${key}`, `entry:`, ]; + if (from.kind === "void") { + // A Promise entry fulfills with undefined: the result's + // undefined arm, whatever the entry's payload slot holds. + const tag = tagOf(UNDEFINED_T); + d.push(` ${fulfill(host.unitInstanceRef(to.unionId, tag))}`, ` ret void`, `}`, ``); + host.resolveThunkDefs.push(...d); + return sym; + } if (from.kind !== "union") { // One arm wrap, straight off the payload accessors. const tag = tagOf(from); diff --git a/packages/compiler/src/backend/llvm/lib-shared.ts b/packages/compiler/src/backend/llvm/lib-shared.ts index 73b6d72e7..4c7eea4af 100644 --- a/packages/compiler/src/backend/llvm/lib-shared.ts +++ b/packages/compiler/src/backend/llvm/lib-shared.ts @@ -294,9 +294,12 @@ export const LIB_FN_SYMS: Record = { "process.rusage": "scr_process_rusage", "process.activeResources": "scr_active_resources", "process.exiting": "scr_process_exiting", + "process.ipcOpen": "scr_process_ipc_open", "process.exit": "scr_process_exit", "process.setExitCode": "scr_process_set_exit_code", "process.currentExitCode": "scr_process_exit_code_or_zero", + "process.setExitCodeTracked": "scr_process_set_exit_code_tracked", + "process.hasExitCode": "scr_process_has_exit_code", "process.envSet": "scr_env_set", "process.envUnset": "scr_env_unset", "process.envPairs": "scr_env_pairs", diff --git a/packages/compiler/src/frontend/lowering/builtin-calls.ts b/packages/compiler/src/frontend/lowering/builtin-calls.ts index 7e1852600..6be798de8 100644 --- a/packages/compiler/src/frontend/lowering/builtin-calls.ts +++ b/packages/compiler/src/frontend/lowering/builtin-calls.ts @@ -12,6 +12,7 @@ import { lowerEventsEmitterHelper, lowerEventsOnce } from "./lower-event-emitter import type { Lowerer } from "./lowerer.js"; import { lowerTimersMemberCall } from "./lower-timers.js"; import { lowerStreamModuleCall } from "./lower-stream.js"; +import { lowerCallbackPromisify } from "./builtins/promisify-callback.js"; import { lowerNodeModuleCall } from "./builtins/module-resolution.js"; import { builtinModuleFnOf, builtinFenceHintOf } from "./surfaces.js"; @@ -87,6 +88,10 @@ export function lowerBuiltinCall( if (testServed) return testServed; const utilServed = lowerer.lowerUtilModuleCall(expr, bi, loc); if (utilServed) return utilServed; + if (bi.module === "util" && bi.member === "promisify") { + const promisified = lowerCallbackPromisify(lowerer, expr, loc); + if (promisified) return promisified; + } const streamServed = lowerStreamModuleCall(lowerer, expr, bi, loc); if (streamServed) return streamServed; const fsTs = lowerer.lowerFsToUnixTimestampCall(expr, bi, loc); diff --git a/packages/compiler/src/frontend/lowering/builtins/callable-aliases.ts b/packages/compiler/src/frontend/lowering/builtins/callable-aliases.ts index 1669978af..afb221d04 100644 --- a/packages/compiler/src/frontend/lowering/builtins/callable-aliases.ts +++ b/packages/compiler/src/frontend/lowering/builtins/callable-aliases.ts @@ -1,11 +1,12 @@ import * as ts from "../../ts7/adapter.js"; import { type Lowerer } from "../lowerer.js"; -import { isJsSourceFile } from "../../program.js"; +import { isJsSourceFile, locOf } from "../../program.js"; import { builtinModuleFnOf } from "../surfaces.js"; import { registerHttpClientFnBinding } from "../lower-server.js"; import { isPromisifiedZlibMember } from "./compression.js"; import { isPromisifiedCryptoMember } from "./crypto.js"; import { isPromisifiedDnsMember } from "../lower-dgram.js"; +import { callbackPromisifyShape } from "./promisify-callback.js"; /** Compile-time util.promisify projections. The immutable result binding * owns no runtime slot: calls rewrite to the target's promise lowering, @@ -164,11 +165,14 @@ export function registerPromisifiedBuiltinDecl( : target?.module === "dns" && isPromisifiedDnsMember(target.member) ? { kind: "promisified-dns" as const, member: target.member } : null; + // A typed callback-last function value: the call lowers to a native + // promisified function, and the binding holds it like any closure. + if (!projection && callbackPromisifyShape(lowerer, e, locOf(e)) !== null) return false; if (!projection) { lowerer.noLowering( "util.promisify of this target", argNode ?? e, - "child_process.exec, child_process.execFile, setTimeout, setImmediate, stream.finished, stream.pipeline, the zlib callback one-shots, the crypto callback functions (randomBytes, pbkdf2, scrypt, hkdf, randomFill, randomInt, generateKey), dns.lookup, dns.lookupService and the fs callback functions with an fs/promises counterpart are the supported targets", + "child_process.exec, child_process.execFile, setTimeout, setImmediate, stream.finished, stream.pipeline, the zlib callback one-shots, the crypto callback functions (randomBytes, pbkdf2, scrypt, hkdf, randomFill, randomInt, generateKey), dns.lookup, dns.lookupService, the fs callback functions with an fs/promises counterpart, and typed callback-last functions whose callback takes (error?: Error | null, value?) are the supported targets", ); } const symbol = lowerer.checker.getSymbolAtLocation(nameNode); diff --git a/packages/compiler/src/frontend/lowering/builtins/ipc.ts b/packages/compiler/src/frontend/lowering/builtins/ipc.ts index cf9d96117..90448854d 100644 --- a/packages/compiler/src/frontend/lowering/builtins/ipc.ts +++ b/packages/compiler/src/frontend/lowering/builtins/ipc.ts @@ -1,7 +1,17 @@ import * as ts from "../../ts7/adapter.js"; import { type Lowerer } from "../lowerer.js"; import { locOf } from "../../program.js"; -import { BOOL, type IrExpr, type IrType, STRING, type SrcLoc } from "../../../ir/ir.js"; +import { + BOOL, + type IrExpr, + type IrStmt, + type IrType, + STRING, + type SrcLoc, + UNDEFINED_T, +} from "../../../ir/ir.js"; +import { nodeThrowExpr } from "../../../ir/build.js"; +import { processHasIpc } from "./process.js"; /** Method calls on ChildProcess receivers: `child.on("exit"|"error", cb)` * registers a listener with the event loop's child registry. The event @@ -86,8 +96,54 @@ export function ipcSendCallback(lowerer: Lowerer, node: ts.Expression): IrExpr { return cb; } +/** process.send in a program without fork targets. This binary never opens + * a channel then, so only a Node.js parent's NODE_CHANNEL_FD can define + * process.send, and that channel has no lowering: a call fences when it runs. + * Without any channel the calls behave as Node's do on an undefined member: + * `process.send?.(...)` skips its arguments and answers undefined, and + * `process.send(...)` evaluates them and throws a TypeError. The message is + * never sent, so its type is not checked. */ +function lowerChannelLessSend(lowerer: Lowerer, call: ts.CallExpression): IrExpr { + const loc = locOf(call); + const fence = (type: IrType): IrExpr => + nodeThrowExpr( + 0, + "SC2020", + "process.send over an IPC channel opened by a Node.js parent (NODE_CHANNEL_FD) is not supported yet", + type, + loc, + ); + const hasIpc = processHasIpc(lowerer, loc); + if (call.questionDotToken) { + const type = lowerer.mapTypeOf(lowerer.typeOf(call)) ?? BOOL; + const absent: IrExpr = + type.kind === "bool" + ? { kind: "boolLit", value: false, type: BOOL, loc } + : lowerer.coerceToExpected( + { kind: "unitLit", unit: "undefined", type: UNDEFINED_T, loc }, + type, + ); + return { kind: "ternary", cond: hasIpc, then: fence(type), else_: absent, type, loc }; + } + const stmts: IrStmt[] = call.arguments.nodes.map((arg) => ({ + kind: "exprStmt", + expr: lowerer.lowerExpr(ts.isSpreadElement(arg) ? arg.expression : arg), + loc: locOf(arg), + })); + const result: IrExpr = { + kind: "ternary", + cond: hasIpc, + then: fence(BOOL), + else_: nodeThrowExpr(1, "", "process.send is not a function", BOOL, loc), + type: BOOL, + loc, + }; + return stmts.length === 0 ? result : { kind: "seqExpr", stmts, result, type: BOOL, loc }; +} + export function lowerProcessIpcSend(lowerer: Lowerer, call: ts.CallExpression): IrExpr { const loc = locOf(call); + if (lowerer.forkTargets.length === 0) return lowerChannelLessSend(lowerer, call); if ( call.arguments.nodes.length < 1 || call.arguments.nodes.length > 2 || diff --git a/packages/compiler/src/frontend/lowering/builtins/process.ts b/packages/compiler/src/frontend/lowering/builtins/process.ts index e4e5004c1..4e6898de3 100644 --- a/packages/compiler/src/frontend/lowering/builtins/process.ts +++ b/packages/compiler/src/frontend/lowering/builtins/process.ts @@ -17,6 +17,7 @@ import { type IrExpr, type IrLibFn, type IrStmt, + type IrType, STRING, type SrcLoc, UNDEFINED_T, @@ -29,6 +30,144 @@ import { lowerBuiltinLoaderValue } from "../lower-builtin-values.js"; import { lowerProcessIpcSend, ipcMessageListener, ipcDisconnectListener } from "./ipc.js"; import { lowerDiagnosticsSubscriber, lowerTracingArguments } from "./async-context.js"; +/** Whether the process has an IPC channel, Node's test for defining + * process.send and process.disconnect: a channel this fork child opened + * (programs with fork targets only), or a non-empty NODE_CHANNEL_FD from a + * Node.js parent, the variable Node itself tests at startup. Built from the + * existing environment read, so the presence test links no runtime code. */ +export function processHasIpc(lowerer: Lowerer, loc: SrcLoc): IrExpr { + const envT = lowerer.envValueType(); + const unionId = (envT as IrType & { kind: "union" }).unionId; + const stringTag = lowerer.armTag(unionId, STRING); + const value = lowerer.declareHiddenLocal("%channelFd", envT); + const fromNode: IrExpr = { + kind: "seqExpr", + stmts: [ + { + kind: "varDecl", + localId: value.id, + init: { + kind: "libCall", + fn: "process.envGet", + args: [strLit("NODE_CHANNEL_FD", loc)], + type: envT, + loc, + }, + loc, + }, + ], + result: { + kind: "logical", + op: "&&", + left: { + kind: "unionIsTag", + unionId, + tag: stringTag, + negated: false, + value: varRef(value.id, envT, loc), + type: BOOL, + loc, + }, + right: { + kind: "toBool", + operand: { + kind: "unionNarrow", + unionId, + tag: stringTag, + value: varRef(value.id, envT, loc), + type: STRING, + loc, + }, + type: BOOL, + loc, + }, + type: BOOL, + loc, + }, + type: BOOL, + loc, + }; + if (lowerer.forkTargets.length === 0) return fromNode; + return { + kind: "logical", + op: "||", + left: { kind: "libCall", fn: "process.ipcOpen", args: [], type: BOOL, loc }, + right: fromNode, + type: BOOL, + loc, + }; +} + +/** True when the value of `node` is consumed only by ToBoolean: a + * condition, the operand of `!`, `Boolean(node)`, or an operand of `&&` / + * `||` whose own value is consumed that way (or discarded, for the left + * operand). */ +export function isTruthinessOnlyRead(lowerer: Lowerer, node: ts.Expression): boolean { + let child: ts.Node = node; + let parent = node.parent; + while (parent !== undefined && ts.isParenthesizedExpression(parent)) { + child = parent; + parent = parent.parent; + } + if (parent === undefined) return false; + if (ts.isIfStatement(parent) || ts.isWhileStatement(parent) || ts.isDoStatement(parent)) + return parent.expression === child; + if (ts.isForStatement(parent)) return parent.condition === child; + if (ts.isConditionalExpression(parent)) return parent.condition === child; + if (ts.isPrefixUnaryExpression(parent)) return parent.operator === ts.SyntaxKind.ExclamationToken; + if (ts.isCallExpression(parent)) { + return ( + !parent.questionDotToken && + parent.arguments.nodes.length === 1 && + parent.arguments.nodes[0] === child && + lowerer.isStdlibGlobal(parent.expression, "Boolean") + ); + } + if (ts.isBinaryExpression(parent)) { + const op = parent.operatorToken.kind; + if (op !== ts.SyntaxKind.AmpersandAmpersandToken && op !== ts.SyntaxKind.BarBarToken) + return false; + if (isTruthinessOnlyRead(lowerer, parent)) return true; + let outer: ts.Node | undefined = parent.parent; + while (outer !== undefined && ts.isParenthesizedExpression(outer)) outer = outer.parent; + return parent.left === child && outer !== undefined && ts.isExpressionStatement(outer); + } + return false; +} + +/** Whether a program source reads process.exitCode. Such a program + * records its exit-code assignments (process.setExitCodeTracked), so a + * read can tell an unset code from 0; other programs keep the untracked + * write. Computed once per lowering pass. */ +export function programReadsExitCode(lowerer: Lowerer): boolean { + if (lowerer.readsExitCode !== null) return lowerer.readsExitCode; + let found = false; + const walk = (node: ts.Node): void => { + if (found) return; + if ( + ts.isPropertyAccessExpression(node) && + node.name.text === "exitCode" && + !( + ts.isBinaryExpression(node.parent) && + node.parent.left === node && + node.parent.operatorToken.kind === ts.SyntaxKind.EqualsToken + ) && + lowerer.stdlibGlobalMember(node, "process") === "exitCode" + ) { + found = true; + return; + } + ts.forEachChild(node, walk); + }; + for (const sf of lowerer.program.getImplementationSourceFiles()) { + if (sf.isDeclarationFile) continue; + walk(sf); + if (found) break; + } + lowerer.readsExitCode = found; + return found; +} + /** The pinned Node release the runtime reports as process.versions.node * (packages/runtime/src/scr_lib.c SCR_NODE_COMPAT_VERSION, kept equal to * internal/compatibility/node-v24.json by test/node-pin.test.ts). */ @@ -361,9 +500,45 @@ export function lowerProcessProperty( const member = lowerer.stdlibGlobalMember(expr, "process"); if (member === null) return null; const loc = locOf(expr); + // `if (process.send)`: Node defines process.send and process.disconnect + // only when the process has an IPC channel. A read whose value only + // feeds a truthiness test answers that presence; other value uses keep + // the member fence. + if ( + (member === "send" || member === "disconnect") && + !expr.questionDotToken && + isTruthinessOnlyRead(lowerer, expr) + ) { + return processHasIpc(lowerer, loc); + } if (member === "versions") { return { kind: "libCall", fn: "process.versions", args: [], type: DYN, loc }; } + // process.exitCode: undefined until something assigns it, then the + // assigned code. A read the checker narrowed to number follows an + // assignment, so the code is set. + if (member === "exitCode" && !expr.questionDotToken) { + const current: IrExpr = { + kind: "libCall", + fn: "process.currentExitCode", + args: [], + type: F64, + loc, + }; + if (lowerer.mapTypeOf(lowerer.typeOf(expr))?.kind === "f64") return current; + const type: IrType = { kind: "union", unionId: lowerer.unions.intern([F64, UNDEFINED_T]) }; + return { + kind: "ternary", + cond: { kind: "libCall", fn: "process.hasExitCode", args: [], type: BOOL, loc }, + then: lowerer.coerceToExpected(current, type), + else_: lowerer.coerceToExpected( + { kind: "unitLit", unit: "undefined", type: UNDEFINED_T, loc }, + type, + ), + type, + loc, + }; + } // process.noDeprecation / process.throwDeprecation: the values the // program assigned (undefined before that — no Node flags reach a // compiled binary). @@ -1483,10 +1658,36 @@ export function lowerProcessMethodCall( } if (member === "exit") { const arg = call.arguments.nodes[0]; - const code: IrExpr = + let code: IrExpr = arg !== undefined ? lowerer.lowerExprExpecting(arg, F64) : { kind: "libCall", fn: "process.currentExitCode", args: [], type: F64, loc }; + // process.exit(code) assigns process.exitCode before the 'exit' + // listeners run. Only a program that reads the property can observe + // that, so only such a program records the assignment. + if (arg !== undefined && programReadsExitCode(lowerer)) { + const binding = lowerer.declareHiddenLocal("%exitCode", F64); + code = { + kind: "seqExpr", + stmts: [ + { kind: "varDecl", localId: binding.id, init: code, loc }, + { + kind: "exprStmt", + expr: { + kind: "libCall", + fn: "process.setExitCodeTracked", + args: [varRef(binding.id, F64, loc)], + type: VOID, + loc, + }, + loc, + }, + ], + result: varRef(binding.id, F64, loc), + type: F64, + loc, + }; + } return { kind: "libCall", fn: "process.exit", args: [code], type: VOID, loc }; } return null; // process.argv(...) etc. are tsc errors before lowering @@ -1502,6 +1703,16 @@ export function lowerProcessOptionalMethodCall( lowerer: Lowerer, expr: ts.CallExpression, ): IrExpr | null { + // `process.send!(message)`: the asserted direct call of the optional + // member (the @types/node spelling under strict null checks). + if ( + !expr.questionDotToken && + ts.isNonNullExpression(expr.expression) && + ts.isPropertyAccessExpression(expr.expression.expression) && + !expr.expression.expression.questionDotToken && + lowerer.stdlibGlobalMember(expr.expression.expression, "process") === "send" + ) + return lowerProcessIpcSend(lowerer, expr); if (!expr.questionDotToken) return null; if (!ts.isPropertyAccessExpression(expr.expression)) return null; const member = lowerer.stdlibGlobalMember(expr.expression, "process"); diff --git a/packages/compiler/src/frontend/lowering/builtins/promises.ts b/packages/compiler/src/frontend/lowering/builtins/promises.ts index 2656da18b..48106d082 100644 --- a/packages/compiler/src/frontend/lowering/builtins/promises.ts +++ b/packages/compiler/src/frontend/lowering/builtins/promises.ts @@ -65,6 +65,9 @@ export function lowerPromiseStaticCall( const compatible = (inner: IrType): boolean => { if (typeEquals(inner, resultT.inner)) return true; if (!resultArms) return false; + // A Promise entry fulfills with undefined, which the result + // holds when the checker folded `void` into its undefined arm. + if (inner.kind === "void") return resultArms.some((a) => a.kind === "undefinedT"); if (inner.kind === "union") { const arms = lowerer.unions.get(inner.unionId)?.arms ?? []; return arms.every((a) => resultArms.some((b) => typeEquals(a, b))); diff --git a/packages/compiler/src/frontend/lowering/builtins/promisify-callback.ts b/packages/compiler/src/frontend/lowering/builtins/promisify-callback.ts new file mode 100644 index 000000000..cbb13d7f8 --- /dev/null +++ b/packages/compiler/src/frontend/lowering/builtins/promisify-callback.ts @@ -0,0 +1,357 @@ +import * as ts from "../../ts7/adapter.js"; +import { type Lowerer } from "../lowerer.js"; +import { varRef } from "../../../ir/build.js"; +import { + BOOL, + type IrExpr, + type IrLocal, + type IrParam, + type IrStmt, + type IrType, + type SrcLoc, + VOID, + funcOf, + isUnitType, + typeEquals, + typeKey, +} from "../../../ir/ir.js"; + +const ERROR_T: IrType = { kind: "object", className: "%Error" }; + +/** The parts of `util.promisify(fn)` for a typed callback-last function: + * `fn(...args, (err, value) => void)` becomes `(...args) => Promise`. */ +export interface CallbackShape { + fnT: IrType & { kind: "func" }; + resultT: IrType & { kind: "func" }; + args: readonly IrType[]; + callbackT: IrType & { kind: "func" }; + inner: IrType; + /** The error parameter's union and the tag of its Error arm, or null when + * the callback declares no error parameter. */ + error: { type: IrType; errorTag: number; unitTags: number[] } | null; +} + +function callbackShape( + lowerer: Lowerer, + fnT: IrType | null, + resultT: IrType | null, + loc: SrcLoc, +): CallbackShape | null { + if (fnT?.kind !== "func" || resultT?.kind !== "func") return null; + if (fnT.rest || fnT.restAbi || fnT.argumentsAll || resultT.rest || resultT.restAbi) return null; + const callbackT = fnT.params[fnT.params.length - 1]; + if (callbackT?.kind !== "func" || callbackT.ret.kind !== "void") return null; + if (callbackT.rest || callbackT.restAbi || callbackT.params.length > 2) return null; + const args = fnT.params.slice(0, -1); + if ( + resultT.ret.kind !== "promise" || + resultT.params.length !== args.length || + !resultT.params.every((type, i) => typeEquals(type, args[i]!)) + ) + return null; + const inner = resultT.ret.inner; + let error: CallbackShape["error"] = null; + const errorT = callbackT.params[0]; + if (errorT !== undefined) { + if (errorT.kind !== "union") return null; + const arms = lowerer.unions.get(errorT.unionId)?.arms ?? []; + const errorTags = arms.flatMap((arm, tag) => + arm.kind === "object" && + (arm.className === "%Error" || lowerer.isSubclassOf(arm.className, "%Error")) + ? [tag] + : [], + ); + const unitTags = arms.flatMap((arm, tag) => (isUnitType(arm) ? [tag] : [])); + if (errorTags.length !== 1 || errorTags.length + unitTags.length !== arms.length) return null; + error = { type: errorT, errorTag: errorTags[0]!, unitTags }; + } + if (inner.kind !== "void") { + const valueT = callbackT.params[1]; + if (valueT === undefined) return null; + const probe: IrExpr = { kind: "varRef", localId: "%probe", type: valueT, loc }; + if (!typeEquals(lowerer.coerceToExpected(probe, inner).type, inner)) return null; + } + return { fnT, resultT, args, callbackT, inner, error }; +} + +/** The callback shape of a `promisify(fn)` call, or null. */ +export function callbackPromisifyShape( + lowerer: Lowerer, + call: ts.CallExpression, + loc: SrcLoc, +): CallbackShape | null { + if (call.questionDotToken || call.arguments.nodes.length !== 1) return null; + const argNode = call.arguments.nodes[0]!; + if (ts.isSpreadElement(argNode)) return null; + return callbackShape( + lowerer, + lowerer.mapTypeOf(lowerer.typeOf(argNode)), + lowerer.mapTypeOf(lowerer.typeOf(call)), + loc, + ); +} + +function captureParam(localId: string, type: IrType): IrParam { + return { localId, name: localId, type }; +} + +function boxedLocal(localId: string, type: IrType): IrLocal { + return { id: localId, name: localId, type, mutable: false, boxed: true }; +} + +function plainLocal(localId: string, type: IrType): IrLocal { + return { id: localId, name: localId, type, mutable: false }; +} + +/** `util.promisify(fn)` over a typed callback-last function value (a bound + * method, a function declaration, a closure): a native function that calls + * `fn` with the arguments and a callback, and settles its promise from the + * callback like Node's wrapper: a truthy error rejects, otherwise the first + * value fulfills. A synchronous throw rejects (the executor's rule). Null + * when the argument or result does not have that typed shape. */ +export function lowerCallbackPromisify( + lowerer: Lowerer, + call: ts.CallExpression, + loc: SrcLoc, +): IrExpr | null { + const shape = callbackPromisifyShape(lowerer, call, loc); + if (shape === null) return null; + const argNode = call.arguments.nodes[0]!; + let fn = lowerer.lowerExpr(argNode); + if (!typeEquals(fn.type, shape.fnT)) fn = lowerer.coerceToExpected(fn, shape.fnT); + if (!typeEquals(fn.type, shape.fnT)) return null; + return { + kind: "call", + callee: promisifyMaker(lowerer, shape, loc), + args: [fn], + type: shape.resultT, + loc, + }; +} + +/** Interns the helper family for one (function, result) type pair and + * answers the maker: `(fn) => closure(...args) => Promise`. */ +function promisifyMaker(lowerer: Lowerer, shape: CallbackShape, loc: SrcLoc): string { + const key = `promisify:${typeKey(shape.fnT)}=>${typeKey(shape.resultT)}`; + const existing = lowerer.arrHofHelpers.get(key); + if (existing) return existing; + const base = `%promisify.${lowerer.arrHofHelpers.size}`; + lowerer.arrHofHelpers.set(key, base); + const { fnT, resultT, args, callbackT, inner, error } = shape; + const promiseT = resultT.ret; + const resolveT = funcOf(inner.kind === "void" ? [] : [inner], VOID); + const rejectT = funcOf([ERROR_T], VOID); + const executorT = funcOf([resolveT, rejectT], VOID); + const argIds = args.map((_, i) => `a${i}.0`); + + // The callback: (err, value) => err ? reject(err) : resolve(value). + const cbParams: IrParam[] = []; + callbackT.params.forEach((type, i) => + cbParams.push(captureParam(i === 0 ? "err.0" : "value.0", type)), + ); + const cbBody: IrStmt[] = []; + if (error !== null) { + const err = varRef("err.0", error.type, loc); + let truthy: IrExpr | null = null; + for (const tag of error.unitTags) { + const part: IrExpr = { + kind: "unionIsTag", + unionId: (error.type as IrType & { kind: "union" }).unionId, + tag, + negated: true, + value: err, + type: BOOL, + loc, + }; + truthy = + truthy === null + ? part + : { kind: "logical", op: "&&", left: truthy, right: part, type: BOOL, loc }; + } + const errorArm = lowerer.unions.get((error.type as IrType & { kind: "union" }).unionId)!.arms[ + error.errorTag + ]!; + let reason: IrExpr = { + kind: "unionNarrow", + unionId: (error.type as IrType & { kind: "union" }).unionId, + tag: error.errorTag, + value: err, + type: errorArm, + loc, + }; + if (!typeEquals(errorArm, ERROR_T)) + reason = { kind: "upcast", value: reason, type: ERROR_T, loc }; + const reject: IrStmt[] = [ + { + kind: "exprStmt", + expr: { + kind: "callValue", + callee: varRef("rej.0", rejectT, loc), + args: [reason], + type: VOID, + loc, + }, + loc, + }, + { kind: "return", value: null, loc }, + ]; + cbBody.push( + truthy === null ? reject[0]! : { kind: "if", cond: truthy, then: reject, else_: null, loc }, + ); + if (truthy === null) cbBody.push(reject[1]!); + } + const value = + inner.kind === "void" + ? [] + : [lowerer.coerceToExpected(varRef("value.0", callbackT.params[1]!, loc), inner)]; + cbBody.push({ + kind: "exprStmt", + expr: { + kind: "callValue", + callee: varRef("res.0", resolveT, loc), + args: value, + type: VOID, + loc, + }, + loc, + }); + lowerer.liftedFns.push({ + name: `${base}.cb`, + params: cbParams, + returnType: VOID, + captures: [captureParam("res.0", resolveT), captureParam("rej.0", rejectT)], + locals: [ + boxedLocal("res.0", resolveT), + boxedLocal("rej.0", rejectT), + ...cbParams.map((p) => plainLocal(p.localId, p.type)), + ], + body: cbBody, + loc, + }); + lowerer.liftedFns.push({ + name: `${base}.mkcb`, + params: [captureParam("res.0", resolveT), captureParam("rej.0", rejectT)], + returnType: callbackT, + locals: [boxedLocal("res.0", resolveT), boxedLocal("rej.0", rejectT)], + body: [ + { + kind: "return", + value: { + kind: "closure", + fnName: `${base}.cb`, + captures: ["res.0", "rej.0"], + type: callbackT, + loc, + }, + loc, + }, + ], + loc, + }); + + // The executor: (resolve, reject) => fn(...args, callback). + const captured: IrParam[] = [captureParam("f.0", fnT)]; + args.forEach((type, i) => captured.push(captureParam(argIds[i]!, type))); + lowerer.liftedFns.push({ + name: `${base}.exec`, + params: [captureParam("res.0", resolveT), captureParam("rej.0", rejectT)], + returnType: VOID, + captures: captured, + locals: [ + ...captured.map((p) => boxedLocal(p.localId, p.type)), + plainLocal("res.0", resolveT), + plainLocal("rej.0", rejectT), + ], + body: [ + { + kind: "exprStmt", + expr: { + kind: "callValue", + callee: varRef("f.0", fnT, loc), + args: [ + ...args.map((type, i) => varRef(argIds[i]!, type, loc)), + { + kind: "call", + callee: `${base}.mkcb`, + args: [varRef("res.0", resolveT, loc), varRef("rej.0", rejectT, loc)], + type: callbackT, + loc, + }, + ], + type: VOID, + loc, + }, + loc, + }, + ], + loc, + }); + lowerer.liftedFns.push({ + name: `${base}.mkexec`, + params: captured, + returnType: executorT, + locals: captured.map((p) => boxedLocal(p.localId, p.type)), + body: [ + { + kind: "return", + value: { + kind: "closure", + fnName: `${base}.exec`, + captures: captured.map((p) => p.localId), + type: executorT, + loc, + }, + loc, + }, + ], + loc, + }); + + // The promisified function: (...args) => new Promise(executor). + const implParams: IrParam[] = []; + args.forEach((type, i) => implParams.push(captureParam(argIds[i]!, type))); + lowerer.liftedFns.push({ + name: `${base}.impl`, + params: implParams, + returnType: promiseT, + captures: [captureParam("f.0", fnT)], + locals: [boxedLocal("f.0", fnT), ...implParams.map((p) => plainLocal(p.localId, p.type))], + body: [ + { + kind: "return", + value: { + kind: "newPromise", + executor: { + kind: "call", + callee: `${base}.mkexec`, + args: [ + varRef("f.0", fnT, loc), + ...implParams.map((p) => varRef(p.localId, p.type, loc)), + ], + type: executorT, + loc, + }, + type: promiseT, + loc, + }, + loc, + }, + ], + loc, + }); + lowerer.liftedFns.push({ + name: base, + params: [captureParam("f.0", fnT)], + returnType: resultT, + locals: [boxedLocal("f.0", fnT)], + body: [ + { + kind: "return", + value: { kind: "closure", fnName: `${base}.impl`, captures: ["f.0"], type: resultT, loc }, + loc, + }, + ], + loc, + }); + return base; +} diff --git a/packages/compiler/src/frontend/lowering/lower-exprs.ts b/packages/compiler/src/frontend/lowering/lower-exprs.ts index 18f17f390..99ee53547 100644 --- a/packages/compiler/src/frontend/lowering/lower-exprs.ts +++ b/packages/compiler/src/frontend/lowering/lower-exprs.ts @@ -6,7 +6,7 @@ import { lowerEventObjectProperty, lowerEventObjectPropertyWrite } from "./lower import { lowerDisposableStackInstanceOf } from "./builtins/disposable-stack.js"; import { lowerEventStaticProperty } from "./builtins/numbers.js"; import { lowerUnionEquality, tagEqualityMayMissAlias } from "./strict-equality.js"; -import { isOptionalProcessStreamProperty } from "./builtins/process.js"; +import { isOptionalProcessStreamProperty, processHasIpc } from "./builtins/process.js"; import { lowerWorkerMetadata } from "./builtins/workers.js"; import { esmImportBindingKind } from "./builtins/module-bindings.js"; import { lowerReadlineProperty } from "./builtins/readline.js"; @@ -644,6 +644,24 @@ function lowerExprInner(lowerer: Lowerer, expr: ts.Expression): IrExpr { if (webAssembly) return webAssembly; if (lowerer.isStdlibGlobal(expr.expression, "process")) return { kind: "strLit", value: "object", type: STRING, loc }; + { + let operand = expr.expression; + while (ts.isParenthesizedExpression(operand)) operand = operand.expression; + const ipcMember = + ts.isPropertyAccessExpression(operand) && !operand.questionDotToken + ? lowerer.stdlibGlobalMember(operand, "process") + : null; + if (ipcMember === "send" || ipcMember === "disconnect") { + return { + kind: "ternary", + cond: processHasIpc(lowerer, loc), + then: { kind: "strLit", value: "function", type: STRING, loc }, + else_: { kind: "strLit", value: "undefined", type: STRING, loc }, + type: STRING, + loc, + }; + } + } if (lowerer.isStdlibGlobal(expr.expression, "fetch")) return { kind: "strLit", value: "function", type: STRING, loc }; const performance = lowerPerfHooksTypeof(lowerer, expr.expression); diff --git a/packages/compiler/src/frontend/lowering/lower-stmts.ts b/packages/compiler/src/frontend/lowering/lower-stmts.ts index 5b6eb0d5b..8e8f9eee8 100644 --- a/packages/compiler/src/frontend/lowering/lower-stmts.ts +++ b/packages/compiler/src/frontend/lowering/lower-stmts.ts @@ -15,7 +15,7 @@ import { InternalCompilerError } from "../../errors.js"; * flow (if/while/for/for-of/do, switch, try/catch, abrupt completions), * explicit resource management, and blocked-binding poisoning. */ import * as ts from "../ts7/adapter.js"; -import { processDataMemberValue } from "./builtins/process.js"; +import { processDataMemberValue, programReadsExitCode } from "./builtins/process.js"; import { requireInitFlagStmt } from "./require-initialization.js"; import { isJsonRequireDecl } from "./json-require.js"; import { bindingSource } from "../binding-source.js"; @@ -7129,9 +7129,12 @@ export function lowerExprStatement(lowerer: Lowerer, expr: ts.Expression): IrStm ) { const loc = locOf(expr); const value = lowerer.lowerExprExpecting(expr.right, F64); + const fn = programReadsExitCode(lowerer) + ? "process.setExitCodeTracked" + : "process.setExitCode"; return { kind: "exprStmt", - expr: { kind: "libCall", fn: "process.setExitCode", args: [value], type: VOID, loc }, + expr: { kind: "libCall", fn, args: [value], type: VOID, loc }, loc, }; } diff --git a/packages/compiler/src/frontend/lowering/lower-stream.ts b/packages/compiler/src/frontend/lowering/lower-stream.ts index 7639e0974..12aa60f9d 100644 --- a/packages/compiler/src/frontend/lowering/lower-stream.ts +++ b/packages/compiler/src/frontend/lowering/lower-stream.ts @@ -999,6 +999,15 @@ export function lowerStreamNew(lowerer: Lowerer, expr: ts.NewExpression, info: C }; } +/** True when a value of Node's argument type `passed` widens implicitly + * into the declared parameter type `declared` (one of its union arms, or a + * sub-union of it). */ +function widensInto(lowerer: Lowerer, passed: IrType, declared: IrType, loc: SrcLoc): boolean { + if (declared.kind !== "union") return false; + const probe: IrExpr = { kind: "varRef", localId: "%probe", type: passed, loc }; + return typeEquals(lowerer.coerceToExpected(probe, declared).type, declared); +} + /* ── user subclasses (`extends Readable`, the underscore-override form) ── */ /** The wrapper closure binding one overridden underscore method as the @@ -1075,10 +1084,17 @@ function streamMethodWrapper( ); } } else if (!typeEquals(got.type, want)) { + // A declared supertype of what Node passes (`chunk: Buffer | string`) + // is sound: the wrapper takes Node's value and widens it into the + // declared parameter. + if (widensInto(lowerer, want, got.type, loc)) { + paramTypes.push(want); + continue; + } lowerer.noLowering( `a '${methodName}' method whose parameter ${i} is '${lowerer.fmt(got.type)}' where Node passes '${lowerer.fmt(want)}'`, blame, - "annotate the parameter with the Node signature's type (chunks are Buffers here)", + "annotate the parameter with the Node signature's type or a supertype of it (chunks are Buffers here)", ); } paramTypes.push(got.type); @@ -1094,7 +1110,11 @@ function streamMethodWrapper( for (let i = 0; i < paramTypes.length; i++) { const local = lowerer.declareHiddenLocal(`a${i}`, paramTypes[i]!); params.push({ localId: local.id, name: local.name, type: paramTypes[i]! }); - argRefs.push({ kind: "varRef", localId: local.id, type: paramTypes[i]!, loc }); + const arg: IrExpr = { kind: "varRef", localId: local.id, type: paramTypes[i]!, loc }; + const declaredType = declared[i]!.type; + argRefs.push( + typeEquals(arg.type, declaredType) ? arg : lowerer.coerceToExpected(arg, declaredType), + ); } const thisRef: IrExpr = { kind: "varRef", localId: thisLocal.id, type: thisType, loc }; let call: IrExpr; diff --git a/packages/compiler/src/frontend/lowering/lowerer.ts b/packages/compiler/src/frontend/lowering/lowerer.ts index c5752340c..4dccca764 100644 --- a/packages/compiler/src/frontend/lowering/lowerer.ts +++ b/packages/compiler/src/frontend/lowering/lowerer.ts @@ -1960,6 +1960,8 @@ export class Lowerer { readonly registeredClassSymbols = new Map(); readonly httpClientFnBindings = new Map(); streamPropMutatedSyms: Set | null = null; + /** Whether some source reads process.exitCode (processReadsExitCode). */ + readsExitCode: boolean | null = null; requestInitPropMutatedSyms: Set | null = null; emitterEventTable: Map | null = null; emitterComputedPatterns: ComputedEventPattern[] = []; diff --git a/packages/compiler/src/frontend/lowering/surfaces.ts b/packages/compiler/src/frontend/lowering/surfaces.ts index be202dfe3..7ed5bbf23 100644 --- a/packages/compiler/src/frontend/lowering/surfaces.ts +++ b/packages/compiler/src/frontend/lowering/surfaces.ts @@ -2625,8 +2625,8 @@ export const AMBIENT_SURFACE_FNS: readonly AmbientSurfaceRow[] = [ id: "node-builtin.process.exitCode", kind: "node-builtin", name: "process.exitCode", - fns: ["process.setExitCode"], - note: "numeric writes in statement position set the implicit exit status; process.exit() reads it", + fns: ["process.setExitCode", "process.setExitCodeTracked", "process.hasExitCode"], + note: "numeric writes in statement position set the implicit exit status; reads answer the assigned code or undefined; process.exit() reads it", }, // ── the tls CA store (lowerTlsCaCall / lowerTlsRootCertificates): the // host's trust anchors, read and replaced. Dedicated paths, and @@ -2938,7 +2938,7 @@ export const BUILTIN_MODULE_FENCE_HINTS: Record< }, util: { promisify: - "the lowered shapes are const bindings over child_process.execFile, child_process.exec, setTimeout, setImmediate, stream.finished, stream.pipeline, the zlib callback one-shots, the crypto callback functions, dns.lookup, dns.lookupService and the fs callback functions with an fs/promises counterpart; call the bound function directly", + "the lowered shapes are const bindings over child_process.execFile, child_process.exec, setTimeout, setImmediate, stream.finished, stream.pipeline, the zlib callback one-shots, the crypto callback functions, dns.lookup, dns.lookupService and the fs callback functions with an fs/promises counterpart, plus typed callback-last function values whose callback takes (error?: Error | null, value?); call the bound function directly", }, "stream/consumers": { blob: diff --git a/packages/compiler/src/frontend/type-mapper.ts b/packages/compiler/src/frontend/type-mapper.ts index 123a5d1a0..940b1eb96 100644 --- a/packages/compiler/src/frontend/type-mapper.ts +++ b/packages/compiler/src/frontend/type-mapper.ts @@ -125,6 +125,14 @@ export function jsOpenObjectType( : type; } +/** The modules whose RequestOptions interface maps to the checked dynamic. */ +const HTTP_REQUEST_OPTION_MODULES: ReadonlySet = new Set([ + "http", + "node:http", + "https", + "node:https", +]); + export const FETCH_AMBIENT_TYPES = [ "Response", "Request", @@ -2443,6 +2451,21 @@ function mapTypeInner(type: ts.Type, ctx: TypeMapperCtx): IrType | null { // spawn consumes a presence-sensitive option bag, including optional // stdio and env values. Keep it checked-dynamic for native normalization. if (isStdlibInterface("SpawnOptions")) return DYN; + // http.RequestOptions / https.RequestOptions as a declared type: an + // option record whose members include functions, agents and signals. + // Values ride checked-dynamic storage, like SpawnOptions. + if ( + psym?.name === "RequestOptions" && + checker.declarationsOf(psym).some((d) => { + if (!ts.isInterfaceDeclaration(d) || !ctx.isStdlibFile(d.getSourceFile())) return false; + for (let p: ts.Node | undefined = d.parent; p !== undefined; p = p.parent) { + if (ts.isModuleDeclaration(p) && ts.isStringLiteral(p.name)) + return HTTP_REQUEST_OPTION_MODULES.has(p.name.text); + } + return false; + }) + ) + return DYN; // exactly like npm-declared types. Static fetch has one deliberately // narrower native representation: Response is an opaque checked- diff --git a/packages/compiler/src/ir/builtin-effects.ts b/packages/compiler/src/ir/builtin-effects.ts index 20dc82555..f972245ff 100644 --- a/packages/compiler/src/ir/builtin-effects.ts +++ b/packages/compiler/src/ir/builtin-effects.ts @@ -346,6 +346,7 @@ export const MAY_THROW_LIB_FNS: ReadonlySet = new Set([ "process.kill", "process.killNum", "process.setExitCode", + "process.setExitCodeTracked", // Node's argument grammar throws ERR_INVALID_ARG_TYPE at the call. "process.emitWarning", // cpuUsage(prev)'s field validation: negative/non-finite prev fields diff --git a/packages/compiler/src/ir/builtin-signatures.ts b/packages/compiler/src/ir/builtin-signatures.ts index 028b12c21..74e868124 100644 --- a/packages/compiler/src/ir/builtin-signatures.ts +++ b/packages/compiler/src/ir/builtin-signatures.ts @@ -2390,6 +2390,9 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ /** Rest listeners use the checked function bridge to pack event arguments. */ "child.onDyn": { argTypes: [CHILD_T, STRING, DYN], result: VOID }, "process.connected": { argTypes: [], result: BOOL }, + /** Whether this fork child opened its channel; part of process.send's + * presence in programs with fork targets. Never throws. */ + "process.ipcOpen": { argTypes: [], result: BOOL }, "process.send": { argTypes: [STRING], result: BOOL }, "process.sendCb": { argTypes: [STRING, null], result: BOOL }, "process.disconnect": { argTypes: [], result: VOID }, @@ -3016,8 +3019,13 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ "process.exit": { argTypes: [F64], result: VOID }, /** Numeric process.exitCode write (integer validation, implicit exit status). */ "process.setExitCode": { argTypes: [F64], result: VOID }, + /** The process.exitCode write of a program that also reads the + * property: the same validation, and the code counts as assigned. */ + "process.setExitCodeTracked": { argTypes: [F64], result: VOID }, /** The code for process.exit() with no argument, or zero when unset. */ "process.currentExitCode": { argTypes: [], result: F64 }, + /** Whether process.exitCode reads a code rather than undefined. Never throws. */ + "process.hasExitCode": { argTypes: [], result: BOOL }, "process.cwd": { argTypes: [], result: STRING }, /** getpid(2) / getuid(2): zero args → f64. POSIX-only target, so both * always answer (the checker's `getuid?` optionality covers Windows — diff --git a/packages/compiler/src/ir/runtime-features.ts b/packages/compiler/src/ir/runtime-features.ts index 870ca14f0..8a9216f58 100644 --- a/packages/compiler/src/ir/runtime-features.ts +++ b/packages/compiler/src/ir/runtime-features.ts @@ -250,6 +250,7 @@ const CHILD_PROCESS_LIB_FNS: ReadonlySet = new Set([ "process.connected", "process.disconnect", "process.forkTarget", + "process.ipcOpen", "process.onDisconnect", "process.onMessage", "process.send", diff --git a/packages/compiler/surface-manifest.json b/packages/compiler/surface-manifest.json index ab1c3405e..6b77c7581 100644 --- a/packages/compiler/surface-manifest.json +++ b/packages/compiler/surface-manifest.json @@ -1988,7 +1988,7 @@ "kind": "node-builtin", "name": "process.exitCode", "status": "static", - "note": "numeric writes in statement position set the implicit exit status; process.exit() reads it" + "note": "numeric writes in statement position set the implicit exit status; reads answer the assigned code or undefined; process.exit() reads it" }, { "id": "node-builtin.process.getBuiltinModule", @@ -2532,7 +2532,7 @@ "name": "util.promisify", "status": "unsupported", "code": "SC2020", - "note": "the lowered shapes are const bindings over child_process.execFile, child_process.exec, setTimeout, setImmediate, stream.finished, stream.pipeline, the zlib callback one-shots, the crypto callback functions, dns.lookup, dns.lookupService and the fs callback functions with an fs/promises counterpart; call the bound function directly" + "note": "the lowered shapes are const bindings over child_process.execFile, child_process.exec, setTimeout, setImmediate, stream.finished, stream.pipeline, the zlib callback one-shots, the crypto callback functions, dns.lookup, dns.lookupService and the fs callback functions with an fs/promises counterpart, plus typed callback-last function values whose callback takes (error?: Error | null, value?); call the bound function directly" }, { "id": "node-builtin.util.stripVTControlCharacters", diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6262-process-send-presence.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6262-process-send-presence.ts.json new file mode 100644 index 000000000..cee8d4a17 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6262-process-send-presence.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6262-process-send-presence.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6263-process-send-presence-fork/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6263-process-send-presence-fork/main.ts.json new file mode 100644 index 000000000..64435c619 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6263-process-send-presence-fork/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6263-process-send-presence-fork/main.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6264-process-exitcode-read.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6264-process-exitcode-read.ts.json new file mode 100644 index 000000000..014ce19b2 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6264-process-exitcode-read.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6264-process-exitcode-read.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6265-process-exitcode-default.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6265-process-exitcode-default.ts.json new file mode 100644 index 000000000..6eb0692f9 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6265-process-exitcode-default.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6265-process-exitcode-default.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6266-process-exit-keeps-exitcode.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6266-process-exit-keeps-exitcode.ts.json new file mode 100644 index 000000000..6694c8045 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6266-process-exit-keeps-exitcode.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6266-process-exit-keeps-exitcode.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6267-stream-hook-wider-chunk.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6267-stream-hook-wider-chunk.ts.json new file mode 100644 index 000000000..13c0a961d --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6267-stream-hook-wider-chunk.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6267-stream-hook-wider-chunk.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6268-promise-race-void-entry.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6268-promise-race-void-entry.ts.json new file mode 100644 index 000000000..0e5a4fb75 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6268-promise-race-void-entry.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6268-promise-race-void-entry.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6269-http-request-options-type.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6269-http-request-options-type.ts.json new file mode 100644 index 000000000..c61e5dec5 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6269-http-request-options-type.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6269-http-request-options-type.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6270-promisify-callback-functions.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6270-promisify-callback-functions.ts.json new file mode 100644 index 000000000..2bcb2df90 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6270-promisify-callback-functions.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6270-promisify-callback-functions.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/boot-path-node-surface-fences.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/boot-path-node-surface-fences.ts.json new file mode 100644 index 000000000..de58c3423 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/boot-path-node-surface-fences.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/diagnostics/boot-path-node-surface-fences.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/node-types/promisify-callback-functions.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/node-types/promisify-callback-functions.ts.json new file mode 100644 index 000000000..a4a753c3d --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/node-types/promisify-callback-functions.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/node-types/promisify-callback-functions.ts" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_child.c b/packages/runtime/src/scr_child.c index 87ead5755..2537594a8 100644 --- a/packages/runtime/src/scr_child.c +++ b/packages/runtime/src/scr_child.c @@ -6406,6 +6406,7 @@ void scr_child_ipc_on_disconnect(ScrChild *c, ScrClosure *cb, bool once) { bool scr_process_ipc_connected(void) { return scr_process_ipc != NULL && scr_process_ipc->connected; } +bool scr_process_ipc_open(void) { return scr_process_ipc != NULL; } bool scr_process_ipc_send(ScrStr *json) { return scr_ipc_send(scr_process_ipc, json, NULL, NULL); } diff --git a/packages/runtime/src/scr_child.h b/packages/runtime/src/scr_child.h index 1187985c8..3159569aa 100644 --- a/packages/runtime/src/scr_child.h +++ b/packages/runtime/src/scr_child.h @@ -182,6 +182,9 @@ void scr_child_ipc_on_disconnect(ScrChild *c, ScrClosure *cb /*moves*/, bool once); double scr_process_fork_target(double target_count); bool scr_process_ipc_connected(void); +/* Whether this fork child opened its channel (process.send's presence in + * a program with fork targets; disconnecting keeps it). */ +bool scr_process_ipc_open(void); bool scr_process_ipc_send(ScrStr *json); bool scr_process_ipc_send_cb(ScrStr *json, ScrClosure *cb /*moves*/, ScrIpcSendFn fn); diff --git a/packages/runtime/src/scr_lib.c b/packages/runtime/src/scr_lib.c index 930d44818..3eef75750 100644 --- a/packages/runtime/src/scr_lib.c +++ b/packages/runtime/src/scr_lib.c @@ -2037,6 +2037,19 @@ double scr_process_exit_code_or_zero(void) { return (double)scr_exit_code_hint_get(); } +static SCR_TL bool scr_exit_code_assigned = false; + +void scr_process_set_exit_code_tracked(double code) { + int converted; + if (!scr_process_exit_code_of(code, &converted)) return; + scr_exit_code_note(converted); + scr_exit_code_assigned = true; +} + +bool scr_process_has_exit_code(void) { + return scr_exit_code_assigned || scr_exit_code_hint_get() != 0; +} + #if defined(__linux__) #include #include diff --git a/packages/runtime/src/scr_lib.h b/packages/runtime/src/scr_lib.h index 4c973e3e5..690f84875 100644 --- a/packages/runtime/src/scr_lib.h +++ b/packages/runtime/src/scr_lib.h @@ -149,6 +149,11 @@ void scr_process_exit(double code); extern const unsigned char scr_detached_exit; void scr_process_set_exit_code(double code); double scr_process_exit_code_or_zero(void); +/* The writer and the presence test of programs that read process.exitCode: + * the code reads undefined until an assignment (or a nonzero exit status + * the runtime chose, such as an uncaught exception's 1). */ +void scr_process_set_exit_code_tracked(double code); +bool scr_process_has_exit_code(void); /* process._exiting: true once the exit sequence began (process.exit or * the exit-listener runner set the flag). Never throws. */ extern SCR_TL bool scr_process_in_exit; diff --git a/tests/corpus/6262-process-send-presence.ts b/tests/corpus/6262-process-send-presence.ts new file mode 100644 index 000000000..59773ab44 --- /dev/null +++ b/tests/corpus/6262-process-send-presence.ts @@ -0,0 +1,24 @@ +// process.send exists only with an IPC channel: presence checks in every +// truthiness position, typeof, and guarded calls. +const viaIpc = Boolean(process.send); +console.log("Boolean", viaIpc); +console.log("not", !process.send, !!process.disconnect); +console.log("typeof", typeof process.send, typeof process.disconnect); +console.log("ternary", process.send ? "ipc" : "stdout"); +if (process.send) { + process.send({ kind: "log", line: "never" }); +} else { + console.log("if: no channel"); +} +let polls = 0; +while (process.send && polls < 3) polls++; +console.log("while", polls); +process.send && process.send({ unused: true }); +const sink = !process.send || !process.disconnect ? "console" : "ipc"; +console.log("or", sink); +console.log("optional", process.send?.({ value: 1 })); +try { + process.send!({ value: 2 }); +} catch (e) { + console.log("direct", (e as Error).name, (e as Error).message); +} diff --git a/tests/corpus/6263-process-send-presence-fork/main.ts b/tests/corpus/6263-process-send-presence-fork/main.ts new file mode 100644 index 000000000..11a634672 --- /dev/null +++ b/tests/corpus/6263-process-send-presence-fork/main.ts @@ -0,0 +1,13 @@ +// process.send and process.disconnect exist in a forked child (an IPC +// channel) and not in the parent; the child's presence checks pick the +// channel and its guarded send reaches the parent. +import { fork } from "node:child_process"; + +console.log("parent", Boolean(process.send), typeof process.send, typeof process.disconnect); +const child = fork(new URL("./worker.ts", import.meta.url), [], { + stdio: ["ignore", "inherit", "inherit", "ipc"], +}); +child.on("message", (message: { sink: string; send: string; disconnect: string }) => { + console.log("child says", message.sink, message.send, message.disconnect); +}); +child.on("exit", (code) => console.log("child exit", code)); diff --git a/tests/corpus/6263-process-send-presence-fork/worker.ts b/tests/corpus/6263-process-send-presence-fork/worker.ts new file mode 100644 index 000000000..81e8288a7 --- /dev/null +++ b/tests/corpus/6263-process-send-presence-fork/worker.ts @@ -0,0 +1,6 @@ +const sink = process.send ? "ipc" : "stdout"; +if (process.send) { + process.send({ sink, send: typeof process.send, disconnect: typeof process.disconnect }); +} +if (!process.send) console.log("unreachable"); +process.disconnect(); diff --git a/tests/corpus/6264-process-exitcode-read.ts b/tests/corpus/6264-process-exitcode-read.ts new file mode 100644 index 000000000..c320b4a0d --- /dev/null +++ b/tests/corpus/6264-process-exitcode-read.ts @@ -0,0 +1,22 @@ +// @exit: 7 +// process.exitCode: unset reads undefined, assignments (including 0) read back, +// and process.exit(n) assigns the code before 'exit' listeners run. +console.log("initial", process.exitCode); +process.on("exit", (code) => { + console.log("exit listener", code, process.exitCode); +}); +process.exitCode = 0; +console.log("after 0", process.exitCode); +process.exitCode = 4; +const seen = process.exitCode; +console.log("after 4", seen, typeof process.exitCode); +process.exitCode = process.exitCode ?? 9; +console.log("kept", process.exitCode); +try { + process.exitCode = 1.5; +} catch (e) { + console.log("rejected", (e as Error).message, process.exitCode); +} +setTimeout(() => { + process.exit(7); +}, 1); diff --git a/tests/corpus/6265-process-exitcode-default.ts b/tests/corpus/6265-process-exitcode-default.ts new file mode 100644 index 000000000..5dbc72294 --- /dev/null +++ b/tests/corpus/6265-process-exitcode-default.ts @@ -0,0 +1,10 @@ +// @exit: 2 +// The exit status follows process.exitCode when the program ends normally. +function settle(failed: boolean): void { + if (failed) process.exitCode = process.exitCode ?? 2; +} +console.log("start", process.exitCode === undefined); +settle(true); +settle(true); +console.log("code", process.exitCode); +process.on("exit", () => console.log("exiting with", process.exitCode)); diff --git a/tests/corpus/6266-process-exit-keeps-exitcode.ts b/tests/corpus/6266-process-exit-keeps-exitcode.ts new file mode 100644 index 000000000..e5a0b86d2 --- /dev/null +++ b/tests/corpus/6266-process-exit-keeps-exitcode.ts @@ -0,0 +1,5 @@ +// @exit: 5 +// process.exit() without an argument keeps process.exitCode. +process.exitCode = 5; +process.on("exit", (code) => console.log("exit", code, process.exitCode)); +process.exit(); diff --git a/tests/corpus/6267-stream-hook-wider-chunk.ts b/tests/corpus/6267-stream-hook-wider-chunk.ts new file mode 100644 index 000000000..be46b18be --- /dev/null +++ b/tests/corpus/6267-stream-hook-wider-chunk.ts @@ -0,0 +1,46 @@ +// Stream hooks may declare a supertype of what Node passes: chunks typed +// Buffer | string still arrive as Buffers. +import { Transform, Writable } from "node:stream"; + +class LineSink extends Writable { + private rest = ""; + readonly lines: string[] = []; + _write(chunk: Buffer | string, encoding: BufferEncoding, cb: (err?: Error | null) => void): void { + console.log("chunk", typeof chunk, Buffer.isBuffer(chunk), encoding); + const parts = (this.rest + chunk.toString()).split("\n"); + this.rest = parts.pop() ?? ""; + this.lines.push(...parts); + cb(); + } + _final(cb: (err?: Error | null) => void): void { + if (this.rest !== "") this.lines.push(this.rest); + cb(); + } +} + +class Upper extends Transform { + _transform(chunk: string | Buffer, _enc: string, cb: (err?: Error | null, data?: string) => void): void { + cb(null, chunk.toString().toUpperCase()); + } +} + +class Failing extends Writable { + _write(chunk: Uint8Array | string, _enc: BufferEncoding, cb: (err?: Error | null) => void): void { + cb(new Error(`refused ${chunk.toString().length} bytes`)); + } +} + +const sink = new LineSink(); +sink.write("a\nb"); +sink.write(Buffer.from("c\nd")); +sink.end("e"); +sink.on("finish", () => console.log("lines", JSON.stringify(sink.lines))); + +const up = new Upper(); +up.on("data", (d: Buffer) => console.log("upper", d.toString())); +up.write("hello "); +up.end("world"); + +const bad = new Failing(); +bad.on("error", (e) => console.log("error", e.message)); +bad.write("xyz"); diff --git a/tests/corpus/6268-promise-race-void-entry.ts b/tests/corpus/6268-promise-race-void-entry.ts new file mode 100644 index 000000000..b33f3b664 --- /dev/null +++ b/tests/corpus/6268-promise-race-void-entry.ts @@ -0,0 +1,31 @@ +// Promise.race over Promise and Promise: the void +// entry fulfills the race with undefined. +interface Msg { id: string } +function delay(ms: number, value: T): Promise { + return new Promise((resolve) => setTimeout(() => resolve(value), ms)); +} +function poll(ms: number, id: string | null): Promise { + return delay(ms, id === null ? undefined : { id }); +} +function exited(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} +async function main(): Promise { + const a = await Promise.race([poll(5, "m1"), exited(50)]); + console.log("message first:", a ? a.id : "none"); + const b = await Promise.race([poll(50, "m2"), exited(5)]); + console.log("exit first:", b === undefined, b ? b.id : "none"); + const c = await Promise.race([exited(5), poll(1, null)]); + console.log("empty poll first:", c); + const settled = Promise.resolve(); + const d = await Promise.race([settled, poll(0, "m3")]); + console.log("settled void entry wins:", d); + const n = await Promise.race([delay(20, 42), exited(1)]); + console.log("number or void:", n); + try { + await Promise.race([poll(20, "late"), Promise.reject(new Error("boom"))]); + } catch (e) { + console.log("rejected:", (e as Error).message); + } +} +void main(); diff --git a/tests/corpus/6269-http-request-options-type.ts b/tests/corpus/6269-http-request-options-type.ts new file mode 100644 index 000000000..17cb2d855 --- /dev/null +++ b/tests/corpus/6269-http-request-options-type.ts @@ -0,0 +1,10 @@ +// http.RequestOptions as a declared type: a checked-dynamic option record. +import type http from "node:http"; +const socket = process.env.NO_SUCH_SOCKET_VAR; +const base: http.RequestOptions = socket ? { socketPath: socket } : { host: "127.0.0.1", port: 8080 }; +const full: http.RequestOptions = { ...base, path: "/x", method: "POST", headers: { "x-a": "1" } }; +console.log(JSON.stringify(base), JSON.stringify(full)); +function describe(o: http.RequestOptions): string { + return `${String(o.host)}:${String(o.port)}${String(o.path ?? "/")}`; +} +console.log(describe(full), describe({ host: "h" })); diff --git a/tests/corpus/6270-promisify-callback-functions.ts b/tests/corpus/6270-promisify-callback-functions.ts new file mode 100644 index 000000000..9c2193882 --- /dev/null +++ b/tests/corpus/6270-promisify-callback-functions.ts @@ -0,0 +1,65 @@ +// util.promisify over typed callback-only functions (a bound method, a +// function declaration, an inline arrow): the error argument rejects, the +// value fulfills, a synchronous throw rejects, and settlement is async. +import { promisify } from "node:util"; + +class Connection { + private closed = false; + private readonly name: string; + constructor(name: string) { + this.name = name; + } + close(callback: (error?: Error) => void): void { + setTimeout(() => { + if (this.closed) { + callback(new Error(`${this.name} already closed`)); + return; + } + this.closed = true; + callback(); + }, 1); + } + stats(callback: (error: Error | null, open: number) => void): void { + setImmediate(() => (this.closed ? callback(new RangeError("closed"), 0) : callback(null, 3))); + } +} + +function boom(callback: (error: Error | null) => void): void { + if (callback !== undefined) throw new TypeError("synchronous failure"); +} + +async function main(): Promise { + const c = new Connection("db"); + const stats = promisify(c.stats.bind(c)); + console.log("open", await stats()); + const closeAsync = promisify(c.close.bind(c)); + await closeAsync(); + console.log("closed once"); + try { + await closeAsync(); + } catch (e) { + console.log("second close:", (e as Error).message); + } + try { + await stats(); + } catch (e) { + console.log("stats:", (e as Error).name, (e as Error).message); + } + try { + await promisify(boom)(); + } catch (e) { + console.log("sync throw:", (e as Error).name, (e as Error).message); + } + const order: string[] = []; + const tick = promisify((done: (error?: Error) => void) => { + order.push("called"); + done(); + }); + const p = tick().then(() => order.push("settled")); + order.push("returned"); + await p; + console.log(order.join(", ")); + await promisify(new Connection("inline").close.bind(new Connection("inline")))(); + console.log("inline done"); +} +void main(); diff --git a/tests/diagnostics/boot-path-node-surface-fences.ts b/tests/diagnostics/boot-path-node-surface-fences.ts new file mode 100644 index 000000000..146a32aa1 --- /dev/null +++ b/tests/diagnostics/boot-path-node-surface-fences.ts @@ -0,0 +1,23 @@ +// The boot-path Node surface keeps its boundaries: process.send answers +// presence checks but stays fenced as a stored value, a stream hook must +// declare Node's argument type or a supertype of it, and util.promisify +// over a function value needs an Error-typed callback error. +import { promisify } from "node:util"; +import { Writable } from "node:stream"; + +const send = process.send; +console.log(typeof send); + +class Counter extends Writable { + _write(chunk: number, _encoding: BufferEncoding, cb: (err?: Error | null) => void): void { + console.log(chunk); + cb(); + } +} +new Counter().end("x"); + +function legacy(cb: (code: number, value: string) => void): void { + cb(0, "v"); +} +const legacyAsync = promisify(legacy); +legacyAsync().then((v) => console.log(v)); diff --git a/tests/fixtures/node-types/promisify-callback-functions.ts b/tests/fixtures/node-types/promisify-callback-functions.ts new file mode 100644 index 000000000..78be5e3bd --- /dev/null +++ b/tests/fixtures/node-types/promisify-callback-functions.ts @@ -0,0 +1,67 @@ +// util.promisify over typed callback-last functions with leading arguments +// under @types/node: bound methods, a function declaration, rejection from +// the callback error, a synchronous throw, and asynchronous settlement. +import { promisify } from "node:util"; + +class Client { + private closed = false; + private readonly name: string; + constructor(name: string) { + this.name = name; + } + close(callback: (error?: Error) => void): void { + setTimeout(() => { + if (this.closed) { + callback(new Error(`${this.name} already closed`)); + return; + } + this.closed = true; + callback(); + }, 1); + } + fetch(key: string, callback: (error: Error | null, value?: string) => void): void { + setImmediate(() => { + if (key === "") callback(new RangeError("empty key")); + else callback(null, `${this.name}:${key}`); + }); + } +} + +function sum(a: number, b: number, done: (err: Error | null, total: number) => void): void { + if (a < 0) throw new TypeError("negative"); + process.nextTick(() => done(null, a + b)); +} + +async function main(): Promise { + const c = new Client("db"); + const closeAsync = promisify(c.close.bind(c)); + await closeAsync(); + console.log("closed once"); + try { + await closeAsync(); + } catch (e) { + console.log("second close:", (e as Error).message); + } + const fetchAsync = promisify(c.fetch.bind(c)); + console.log(await fetchAsync("k")); + try { + await fetchAsync(""); + } catch (e) { + console.log("fetch:", (e as Error).name, (e as Error).message); + } + const sumAsync = promisify(sum); + console.log("sum", await sumAsync(2, 3)); + try { + await sumAsync(-1, 3); + } catch (e) { + console.log("sync throw:", (e as Error).name, (e as Error).message); + } + await promisify(new Client("inline").close.bind(new Client("inline")))(); + console.log("inline done"); + const order: string[] = []; + const p = sumAsync(1, 1).then((v) => order.push(`then ${v}`)); + order.push("after call"); + await p; + console.log(order.join(", ")); +} +void main(); diff --git a/tests/harness/__snapshots__/boot-path-node-surface-fences.ts.txt b/tests/harness/__snapshots__/boot-path-node-surface-fences.ts.txt new file mode 100644 index 000000000..c287a1641 --- /dev/null +++ b/tests/harness/__snapshots__/boot-path-node-surface-fences.ts.txt @@ -0,0 +1,35 @@ +boot-path-node-surface-fences.ts:8:14 - error SC2020: 'process.send' is part of the standard library types but has no scriptc lowering yet + + 7 | + 8 | const send = process.send; + | ^~~~~~~~~~~~ + 9 | console.log(typeof send); + + hint: the type checker sees the full standard library, but only the supported surface compiles (https://scriptc.dev/limitations) + +boot-path-node-surface-fences.ts:11:1 - error SC2020: 'a '_write' method whose parameter 0 is 'number' where Node passes 'Uint8Array'' is part of the standard library types but has no scriptc lowering yet + + 10 | + 11 | class Counter extends Writable { + | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 12 | _write(chunk: number, _encoding: BufferEncoding, cb: (err?: Error | null) => void): void { + + hint: annotate the parameter with the Node signature's type or a supertype of it (chunks are Buffers here) + +boot-path-node-surface-fences.ts:22:31 - error SC2020: 'util.promisify of this target' is part of the standard library types but has no scriptc lowering yet + + 21 | } + 22 | const legacyAsync = promisify(legacy); + | ^~~~~~ + 23 | legacyAsync().then((v) => console.log(v)); + + hint: child_process.exec, child_process.execFile, setTimeout, setImmediate, stream.finished, stream.pipeline, the zlib callback one-shots, the crypto callback functions (randomBytes, pbkdf2, scrypt, hkdf, randomFill, randomInt, generateKey), dns.lookup, dns.lookupService, the fs callback functions with an fs/promises counterpart, and typed callback-last functions whose callback takes (error?: Error | null, value?) are the supported targets + +boot-path-node-surface-fences.ts:23:1 - error SC2004: uses of 'legacyAsync' inherit the blocker on its declaration + + 22 | const legacyAsync = promisify(legacy); + 23 | legacyAsync().then((v) => console.log(v)); + | ^~~~~~~~~~~ + 24 | + + hint: the declaration of 'legacyAsync' did not compile — fix the diagnostic reported there and these sites clear with it \ No newline at end of file diff --git a/tests/harness/project-config.test.ts b/tests/harness/project-config.test.ts index bf57a830a..8069ccadd 100644 --- a/tests/harness/project-config.test.ts +++ b/tests/harness/project-config.test.ts @@ -404,6 +404,24 @@ test("node-types: promisified fs callback functions lower as their fs/promises c expect(comparableStderr(native.stderr)).toBe(node.stderr); }); +test("node-types: util.promisify over typed callback-last functions settles like Node", async () => { + const outDir = outDirFor("promisify-callback-functions"); + const entry = join(nodeTypesDir, "promisify-callback-functions.ts"); + const result = await compile(entry, { + outPath: join(outDir, "promisify-callback-functions"), + outDir, + sanitize, + }); + expect(result.ok, !result.ok ? JSON.stringify(result.diagnostics, null, 2) : "").toBe(true); + if (!result.ok) return; + const [native, node] = await Promise.all([ + execFileAsync(result.binaryPath), + execFileAsync(process.execPath, ["--disable-warning=MODULE_TYPELESS_PACKAGE_JSON", entry]), + ]); + expect(native.stdout).toBe(node.stdout); + expect(comparableStderr(native.stderr)).toBe(node.stderr); +}); + test("node-types: promisified setTimeout and setImmediate lower as timers/promises", async () => { const outDir = outDirFor("promisify-timers"); const entry = join(nodeTypesDir, "promisify-timers.ts"); From d9d3dc251778d011a8849e93fb7dfacbbc0ce419 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 15:21:04 -0700 Subject: [PATCH 31/83] Keep __dirname and __filename in TypeScript files that load as ES modules A .ts or .tsx file with import syntax is an ES module under Node's strip-types rule, which has no __dirname or __filename. TypeScript projects are commonly shipped as CommonJS bundles where both exist, and @types/node declares them for every file. scriptc now compiles them in such files to the module's source path, the same compile-time constants as in CommonJS and the same strings as import.meta.dirname and import.meta.filename. JavaScript ES modules keep the fence. The limitations page documents the difference. Co-Authored-By: Claude Opus 5.5 --- docs/content/docs/limitations.mdx | 4 ++ .../limitations/module-path-globals.mdx | 1 + .../src/frontend/lowering/lower-exprs.ts | 10 +++-- .../tests/corpus/6271-esm-js-dirname.mjs.json | 6 +++ tests/corpus/6271-esm-js-dirname.mjs | 9 +++++ tests/fixtures/esm-path-globals/lib/helper.ts | 2 + tests/fixtures/esm-path-globals/main.ts | 12 ++++++ tests/harness/esm-path-globals.test.ts | 38 +++++++++++++++++++ 8 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 docs/content/limitations/module-path-globals.mdx create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6271-esm-js-dirname.mjs.json create mode 100644 tests/corpus/6271-esm-js-dirname.mjs create mode 100644 tests/fixtures/esm-path-globals/lib/helper.ts create mode 100644 tests/fixtures/esm-path-globals/main.ts create mode 100644 tests/harness/esm-path-globals.test.ts diff --git a/docs/content/docs/limitations.mdx b/docs/content/docs/limitations.mdx index d261b6385..c64c5cebd 100644 --- a/docs/content/docs/limitations.mdx +++ b/docs/content/docs/limitations.mdx @@ -228,6 +228,10 @@ title: "Limitations" ../limitations/process-arguments-and-errors.mdx +### Module path globals + +../limitations/module-path-globals.mdx + ## Runtime code evaluation diff --git a/docs/content/limitations/module-path-globals.mdx b/docs/content/limitations/module-path-globals.mdx new file mode 100644 index 000000000..4c888fd32 --- /dev/null +++ b/docs/content/limitations/module-path-globals.mdx @@ -0,0 +1 @@ +`__dirname` and `__filename` are compile-time constants: the absolute path of the module's source directory and file at build time, the same strings as `import.meta.dirname` and `import.meta.filename`. A `.ts` or `.tsx` file keeps them even when it uses `import` syntax, which Node.js would load as an ES module without these globals, because TypeScript projects are commonly shipped as CommonJS bundles where they exist. In such a file `typeof __dirname` is `"string"`, where Node.js running the source directly reports `"undefined"`. JavaScript ES modules (`.mjs`, or `.js` in a `"type": "module"` package) do not have them, as in Node.js. diff --git a/packages/compiler/src/frontend/lowering/lower-exprs.ts b/packages/compiler/src/frontend/lowering/lower-exprs.ts index 99ee53547..e20a702c8 100644 --- a/packages/compiler/src/frontend/lowering/lower-exprs.ts +++ b/packages/compiler/src/frontend/lowering/lower-exprs.ts @@ -1214,14 +1214,18 @@ function lowerExprInner(lowerer: Lowerer, expr: ts.Expression): IrExpr { // MODULE compile-time constants, the containing file's location // (Node's exact values when the same tree runs in place; WASI uses // the equivalent guest-visible path; locals shadowing the names - // resolved above). In a file with ESM syntax Node never defines them - // (ReferenceError) — fence rather than invent a value there. + // resolved above), the same strings as import.meta.dirname/filename. + // In a JavaScript ES module Node never defines them (ReferenceError), + // so they fence there. A .ts/.tsx file keeps them even when Node's + // strip-types rule would load it as ESM: TypeScript projects are + // commonly built as CommonJS bundles (tsc, esbuild, tsx) where these + // globals exist, and @types/node declares them for every file. if ( (expr.text === "__dirname" || expr.text === "__filename") && lowerer.isStdlibGlobal(expr, expr.text) ) { const sf = expr.getSourceFile(); - if (isNodeEsmFile(sf, lowerer.program)) { + if (isNodeEsmFile(sf, lowerer.program) && !/\.tsx?$/.test(sf.fileName)) { lowerer.unsupported( "SC1090", expr, diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6271-esm-js-dirname.mjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6271-esm-js-dirname.mjs.json new file mode 100644 index 000000000..c1ea88546 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6271-esm-js-dirname.mjs.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6271-esm-js-dirname.mjs" + ], + "diags": [] +} diff --git a/tests/corpus/6271-esm-js-dirname.mjs b/tests/corpus/6271-esm-js-dirname.mjs new file mode 100644 index 000000000..e6280e6ec --- /dev/null +++ b/tests/corpus/6271-esm-js-dirname.mjs @@ -0,0 +1,9 @@ +// A JavaScript ES module has no __dirname or __filename (Node throws a +// ReferenceError where they are read); import.meta carries the location. +// TypeScript files keep the CommonJS globals as the source path. +try { + console.log(__dirname); +} catch (error) { + console.log("no __dirname:", error instanceof Error); +} +console.log(import.meta.filename.endsWith("6271-esm-js-dirname.mjs")); diff --git a/tests/fixtures/esm-path-globals/lib/helper.ts b/tests/fixtures/esm-path-globals/lib/helper.ts new file mode 100644 index 000000000..61960c097 --- /dev/null +++ b/tests/fixtures/esm-path-globals/lib/helper.ts @@ -0,0 +1,2 @@ +export const helperDir: string = __dirname; +export const helperFile: string = __filename; diff --git a/tests/fixtures/esm-path-globals/main.ts b/tests/fixtures/esm-path-globals/main.ts new file mode 100644 index 000000000..d3d3c3b8a --- /dev/null +++ b/tests/fixtures/esm-path-globals/main.ts @@ -0,0 +1,12 @@ +// __dirname and __filename in TypeScript files with import syntax: each +// module reads its own source location, the same strings as +// import.meta.dirname and import.meta.filename. +import { basename, join } from "node:path"; +import { helperDir, helperFile } from "./lib/helper.ts"; + +console.log(typeof __dirname, typeof __filename); +console.log(basename(__dirname), basename(__filename)); +console.log(__dirname === import.meta.dirname, __filename === import.meta.filename); +console.log(join(__dirname, "lib") === helperDir, basename(helperFile)); +const home = typeof __dirname === "string" && __dirname.endsWith("/lib") ? join(__dirname, "..") : "/tmp"; +console.log(home); diff --git a/tests/harness/esm-path-globals.test.ts b/tests/harness/esm-path-globals.test.ts new file mode 100644 index 000000000..546c924a9 --- /dev/null +++ b/tests/harness/esm-path-globals.test.ts @@ -0,0 +1,38 @@ +/* __dirname / __filename in TypeScript files that Node's strip-types rule + * loads as ES modules: scriptc compiles them to the module's source path, + * the CommonJS-bundle reading. Node itself has no such globals in an ES + * module, so the oracle runs a twin of each source with the globals spelled + * import.meta.dirname / import.meta.filename, at the same paths. */ +import { execFile } from "node:child_process"; +import { cpSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { promisify } from "node:util"; +import { expect, test } from "vitest"; +import { compile } from "@scriptc/compiler"; + +const execFileAsync = promisify(execFile); +const fixture = join(import.meta.dirname, "../fixtures/esm-path-globals"); + +test("__dirname and __filename in TypeScript ES modules read the module's source path", async () => { + const dir = mkdtempSync(join(tmpdir(), "scriptc-esm-path-globals-")); + cpSync(fixture, join(dir, "src"), { recursive: true }); + const entry = join(dir, "src", "main.ts"); + const result = await compile(entry, { + outPath: join(dir, "main"), + outDir: dir, + sanitize: process.env["SCRIPTC_SAN"] === "1", + }); + expect(result.ok, !result.ok ? JSON.stringify(result.diagnostics, null, 2) : "").toBe(true); + if (!result.ok) return; + const native = await execFileAsync(result.binaryPath); + for (const file of [entry, join(dir, "src", "lib", "helper.ts")]) { + const text = readFileSync(file, "utf8") + .replaceAll("__dirname", "import.meta.dirname") + .replaceAll("__filename", "import.meta.filename"); + writeFileSync(file, text); + } + const node = await execFileAsync(process.execPath, [entry]); + expect(native.stdout).toBe(node.stdout); + expect(native.stdout).toContain("string string\nsrc main.ts\ntrue true\ntrue helper.ts\n"); +}); From ea86299fd76f20841f2c5513c70e7a1af1cb764e Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 16:46:44 -0700 Subject: [PATCH 32/83] Extend the @types/node stream classes A class that extends Writable, Readable, Duplex or Transform from node:stream compiled only against the bundled Node declarations. With @types/node in the project, which nests the classes in the module's `internal` namespace, the base was refused as "not declared in the program". The recognition now accepts either declaration of the stream module's classes. Co-Authored-By: Claude Opus 5.5 --- .../src/frontend/lowering/lower-classes.ts | 5 +- .../node-types/stream-subclass-hooks.ts.json | 6 +++ .../node-types/stream-subclass-hooks.ts | 46 +++++++++++++++++++ tests/harness/project-config.test.ts | 18 ++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/node-types/stream-subclass-hooks.ts.json create mode 100644 tests/fixtures/node-types/stream-subclass-hooks.ts diff --git a/packages/compiler/src/frontend/lowering/lower-classes.ts b/packages/compiler/src/frontend/lowering/lower-classes.ts index 770c74840..216b21da2 100644 --- a/packages/compiler/src/frontend/lowering/lower-classes.ts +++ b/packages/compiler/src/frontend/lowering/lower-classes.ts @@ -83,7 +83,6 @@ import { isCjsJsFile, isJsSourceFile, isModuleExportsAccess, - isNodeTypesPath, locOf, } from "../program.js"; import { PoisonError, dynFallbackType, newFnCtx, own } from "./lowerer.js"; @@ -1012,9 +1011,11 @@ export function builtinStreamInfoOf( if (rec.lib === symbol.name) irName = name; } if (!irName) return null; + // Both the bundled declarations and @types/node declare the classes + // inside `declare module "stream"` (@types/node nests them in the module's + // `internal` namespace). const declared = lowerer.checker.declarationsOf(symbol).some((d) => { if (!ts.isClassDeclaration(d)) return false; - if (isNodeTypesPath(d.getSourceFile().fileName)) return false; let node: ts.Node | undefined = d.parent; while (node) { if (ts.isModuleDeclaration(node) && ts.isStringLiteral(node.name)) { diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/node-types/stream-subclass-hooks.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/node-types/stream-subclass-hooks.ts.json new file mode 100644 index 000000000..295d7deda --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/node-types/stream-subclass-hooks.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/node-types/stream-subclass-hooks.ts" + ], + "diags": [] +} diff --git a/tests/fixtures/node-types/stream-subclass-hooks.ts b/tests/fixtures/node-types/stream-subclass-hooks.ts new file mode 100644 index 000000000..0e7cfbad5 --- /dev/null +++ b/tests/fixtures/node-types/stream-subclass-hooks.ts @@ -0,0 +1,46 @@ +// Stream subclasses under @types/node: a Writable whose hooks declare a +// supertype of Node's chunk type, a Transform, and an erroring Writable. +import { Transform, Writable } from "node:stream"; + +class LineSink extends Writable { + private rest = ""; + readonly lines: string[] = []; + _write(chunk: Buffer | string, encoding: BufferEncoding, cb: (err?: Error | null) => void): void { + console.log("chunk", typeof chunk, Buffer.isBuffer(chunk), encoding); + const parts = (this.rest + chunk.toString()).split("\n"); + this.rest = parts.pop() ?? ""; + this.lines.push(...parts); + cb(); + } + _final(cb: (err?: Error | null) => void): void { + if (this.rest !== "") this.lines.push(this.rest); + cb(); + } +} + +class Upper extends Transform { + _transform(chunk: string | Buffer, _enc: string, cb: (err?: Error | null, data?: string) => void): void { + cb(null, chunk.toString().toUpperCase()); + } +} + +class Failing extends Writable { + _write(chunk: Uint8Array | string, _enc: BufferEncoding, cb: (err?: Error | null) => void): void { + cb(new Error(`refused ${chunk.toString().length} bytes`)); + } +} + +const sink = new LineSink(); +sink.write("a\nb"); +sink.write(Buffer.from("c\nd")); +sink.end("e"); +sink.on("finish", () => console.log("lines", JSON.stringify(sink.lines))); + +const up = new Upper(); +up.on("data", (d: Buffer) => console.log("upper", d.toString())); +up.write("hello "); +up.end("world"); + +const bad = new Failing(); +bad.on("error", (e) => console.log("error", e.message)); +bad.write("xyz"); diff --git a/tests/harness/project-config.test.ts b/tests/harness/project-config.test.ts index 8069ccadd..2196e602f 100644 --- a/tests/harness/project-config.test.ts +++ b/tests/harness/project-config.test.ts @@ -422,6 +422,24 @@ test("node-types: util.promisify over typed callback-last functions settles like expect(comparableStderr(native.stderr)).toBe(node.stderr); }); +test("node-types: stream subclasses extend the @types/node stream classes", async () => { + const outDir = outDirFor("stream-subclass-hooks"); + const entry = join(nodeTypesDir, "stream-subclass-hooks.ts"); + const result = await compile(entry, { + outPath: join(outDir, "stream-subclass-hooks"), + outDir, + sanitize, + }); + expect(result.ok, !result.ok ? JSON.stringify(result.diagnostics, null, 2) : "").toBe(true); + if (!result.ok) return; + const [native, node] = await Promise.all([ + execFileAsync(result.binaryPath), + execFileAsync(process.execPath, ["--disable-warning=MODULE_TYPELESS_PACKAGE_JSON", entry]), + ]); + expect(native.stdout).toBe(node.stdout); + expect(comparableStderr(native.stderr)).toBe(node.stderr); +}); + test("node-types: promisified setTimeout and setImmediate lower as timers/promises", async () => { const outDir = outDirFor("promisify-timers"); const entry = join(nodeTypesDir, "promisify-timers.ts"); From bc32d97339b1b0f82618e4d38feb4b78babef6f0 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 16:52:01 -0700 Subject: [PATCH 33/83] Compile build-utils' bundled code: star re-exports, aliases, three lowering fixes - A star re-export of a lazily initialized module into the bundle's exports (__reExport(main, (init_x(), __toCommonJS(x_exports)))) initializes the module at the statement and adds its names to the export table after the explicit ones, without default. - The rewrite keeps the original's lexer answer whenever its export table holds accessors (Node's lexer stops at the first one). - An npm alias directory of a selected package ("x-updated": "npm:x@6.3.0") resolves to that package's JavaScript in both resolvers. - "use strict" code may read arguments in functions with parameters. - A comma expression ending in null/undefined coerces its result. - A void conditional return lowers as control flow. - A detached method of a local class drops its empty capture list. Co-Authored-By: Claude Opus 5.5 --- .../src/frontend/lowering/call-signatures.ts | 27 +++++++++++++- .../frontend/lowering/class-method-values.ts | 5 ++- .../compiler/src/frontend/lowering/lowerer.ts | 20 +++++++++++ .../src/frontend/npm-static-bundled-esm.ts | 35 +++++++++++++++++++ .../src/frontend/npm-static-rewrite-syntax.ts | 15 ++++---- packages/compiler/src/frontend/npm-static.ts | 24 +++++++++++++ packages/compiler/src/frontend/resolve.ts | 24 ++++++++++++- .../6260-local-class-detached-method.ts | 16 +++++++++ tests/corpus/6262-comma-null-union-return.cjs | 15 ++++++++ .../6263-strict-arguments-parameters.cjs | 15 ++++++++ .../cases/2566-npm-alias-two-releases/main.ts | 7 ++++ .../node_modules/gtroute | 1 + .../node_modules/gtpattern/lib/index.d.ts | 2 ++ .../node_modules/gtpattern/lib/index.js | 5 +++ .../node_modules/gtpattern/package.json | 6 ++++ .../node_modules/gtpattern/lib/index.d.ts | 2 ++ .../node_modules/gtpattern/lib/index.js | 5 +++ .../node_modules/gtpattern/package.json | 6 ++++ .../gtroute@1.0.0/node_modules/gtpattern | 1 + .../gtroute@1.0.0/node_modules/gtpattern-next | 1 + .../node_modules/gtroute/index.d.ts | 1 + .../node_modules/gtroute/index.js | 7 ++++ .../node_modules/gtroute/package.json | 11 ++++++ tests/harness/npm-cases.ts | 4 +-- tests/harness/npm-static.test.ts | 23 +++++++++++- 25 files changed, 264 insertions(+), 14 deletions(-) create mode 100644 tests/corpus/6260-local-class-detached-method.ts create mode 100644 tests/corpus/6262-comma-null-union-return.cjs create mode 100644 tests/corpus/6263-strict-arguments-parameters.cjs create mode 100644 tests/fixtures/npm/cases/2566-npm-alias-two-releases/main.ts create mode 120000 tests/fixtures/npm/cases/2566-npm-alias-two-releases/node_modules/gtroute create mode 100644 tests/fixtures/npm/store/gtpattern@1.0.0/node_modules/gtpattern/lib/index.d.ts create mode 100644 tests/fixtures/npm/store/gtpattern@1.0.0/node_modules/gtpattern/lib/index.js create mode 100644 tests/fixtures/npm/store/gtpattern@1.0.0/node_modules/gtpattern/package.json create mode 100644 tests/fixtures/npm/store/gtpattern@2.0.0/node_modules/gtpattern/lib/index.d.ts create mode 100644 tests/fixtures/npm/store/gtpattern@2.0.0/node_modules/gtpattern/lib/index.js create mode 100644 tests/fixtures/npm/store/gtpattern@2.0.0/node_modules/gtpattern/package.json create mode 120000 tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtpattern create mode 120000 tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtpattern-next create mode 100644 tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtroute/index.d.ts create mode 100644 tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtroute/index.js create mode 100644 tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtroute/package.json diff --git a/packages/compiler/src/frontend/lowering/call-signatures.ts b/packages/compiler/src/frontend/lowering/call-signatures.ts index 9d9c9668c..9de7cd026 100644 --- a/packages/compiler/src/frontend/lowering/call-signatures.ts +++ b/packages/compiler/src/frontend/lowering/call-signatures.ts @@ -707,6 +707,27 @@ export function collectSignatureInner(lowerer: Lowerer, decl: ts.FunctionDeclara }); } +/** True when a "use strict" directive prologue covers the node: its + * file's or an enclosing function's (strict code's arguments object never + * aliases the named parameters). */ +function inStrictCode(node: ts.Node): boolean { + const strict = (stmts: readonly ts.Statement[]): boolean => { + for (const st of stmts) { + if (!ts.isExpressionStatement(st) || !ts.isStringLiteral(st.expression)) return false; + if (st.expression.text === "use strict") return true; + } + return false; + }; + for (let n: ts.Node | undefined = node; n !== undefined; n = n.parent) { + if (ts.isSourceFile(n)) return strict(n.statements.nodes); + if (ts.isFunctionLike(n)) { + const body = (n as { body?: ts.Node }).body; + if (body !== undefined && ts.isBlock(body) && strict(body.statements.nodes)) return true; + } + } + return false; +} + /** Arrows inherit arguments. Ordinary JS functions receive a hidden * array unless a declared rest parameter already owns that contract. * Class methods are strict; other parameterized forms require ES modules @@ -732,7 +753,11 @@ function appendArgumentsParameter( const classMethod = ts.isMethodDeclaration(node) && (ts.isClassDeclaration(node.parent) || ts.isClassExpression(node.parent)); - if (!classMethod && !isNodeEsmFile(node.getSourceFile(), lowerer.program)) { + if ( + !classMethod && + !isNodeEsmFile(node.getSourceFile(), lowerer.program) && + !inStrictCode(node) + ) { lowerer.unsupported( "SC1090", node, diff --git a/packages/compiler/src/frontend/lowering/class-method-values.ts b/packages/compiler/src/frontend/lowering/class-method-values.ts index 10572377e..ff732efff 100644 --- a/packages/compiler/src/frontend/lowering/class-method-values.ts +++ b/packages/compiler/src/frontend/lowering/class-method-values.ts @@ -539,8 +539,11 @@ export function finalizeClassMethodValues(lowerer: Lowerer, functions: IrFunctio if (usesThis) continue; // A method that never uses this remains callable when detached. const name = `%method.receiverless:${target.name}`; + // A local class's method keeps an (empty) class-capture list; the + // detached copy has no receiver to read captures through. + const { classCaptures: _none, ...detached } = target; functions.push({ - ...target, + ...detached, name, params: target.params.slice(1), locals: target.locals.filter((local) => local.id !== thisParam.localId), diff --git a/packages/compiler/src/frontend/lowering/lowerer.ts b/packages/compiler/src/frontend/lowering/lowerer.ts index ce2390d95..1f905cae4 100644 --- a/packages/compiler/src/frontend/lowering/lowerer.ts +++ b/packages/compiler/src/frontend/lowering/lowerer.ts @@ -8478,6 +8478,17 @@ export class Lowerer { * else (including a DIFFERENT union) is left for requireExactShape, which * rejects union mismatches with SC2003. */ coerceToExpected(expr: IrExpr, expected: IrType): IrExpr { + // A comma expression ending in a unit literal (`return log(), null`): + // its effects run first, unchanged, and the unit value takes the + // destination's representation (a bare unit literal has none). + if ( + expr.kind === "seqExpr" && + expr.result.kind === "unitLit" && + !typeEquals(expr.type, expected) + ) { + const result = this.coerceToExpected(expr.result, expected); + return { ...expr, result, type: result.type }; + } // A destination that holds undefined takes an unassigned deferred-init // field's slot as is (Node passes the undefined along). if (expected.kind === "union" && this.armTag(expected.unionId, UNDEFINED_T) >= 0) { @@ -9905,6 +9916,15 @@ export class Lowerer { e = this.returnedPromiseAwait(e); } if (e.kind === "unitLit") return { kind: "return", value: null, loc }; + // `return c ? f() : g()` over void calls: a void conditional has no + // value representation, so its branch selection becomes control flow. + if (e.kind === "ternary" && e.type.kind === "void") { + const branch = (x: IrExpr): IrStmt => + x.kind === "ternary" && x.type.kind === "void" + ? { kind: "if", cond: x.cond, then: [branch(x.then)], else_: [branch(x.else_)], loc } + : { kind: "exprStmt", expr: x, loc }; + return { kind: "block", body: [branch(e), { kind: "return", value: null, loc }], loc }; + } if (e.type.kind === "void") return { kind: "return", value: e, loc }; return { kind: "block", diff --git a/packages/compiler/src/frontend/npm-static-bundled-esm.ts b/packages/compiler/src/frontend/npm-static-bundled-esm.ts index cf3664264..10177ccb9 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-esm.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-esm.ts @@ -633,6 +633,41 @@ export function lowerBundledEsmInits( names.has(access.name.text) && plainRead(access); const holder = ns.parent; + // `__reExport(main, ns, module.exports)`: a star re-export of the + // module into the bundle's own exports (`export * from` in its entry). + // The initializer runs at the statement; the names become live getter + // entries of the export table, after its explicit ones (first wins) + // and without `default`, exactly the names __reExport copies. + if ( + holder !== undefined && + ts.isCallExpression(holder) && + ts.isIdentifier(holder.expression) && + helperBase(holder.expression.text) === "__reExport" && + holder.arguments.nodes[1] === ns && + holder.arguments.nodes.length <= 3 && + ts.isExpressionStatement(holder.parent) && + mainTarget !== null && + ts.isIdentifier(holder.arguments.nodes[0]!) && + (holder.arguments.nodes[0] as ts.Identifier).text === mainTarget + ) { + const inner = unparen(ns); + const init = + ts.isBinaryExpression(inner) && inner.operatorToken.kind === ts.SyntaxKind.CommaToken + ? `${unparen(inner.left).getText(sf)}; ` + : ""; + if (shadowed(holder, table.entries)) return { degrade: `its shipped JS ${why}` }; + const entries = table.entries + .filter(([n]) => n !== "default") + .map(([n, value]) => `${JSON.stringify(n)}: () => ${getterText(value)}`) + .join(", "); + edits.push({ + start: holder.parent.getStart(sf), + end: holder.parent.getEnd(), + text: `${init}__export(${mainTarget}, { ${entries} });`, + }); + lowered.add(r.text); + continue; + } let readable = memberRead(holder, ns); if ( !readable && diff --git a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts index 3b553c3f2..5963cf7b5 100644 --- a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts +++ b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts @@ -1080,13 +1080,12 @@ export function rewriteBundlerCjsSyntax( lines.push( `module.exports = {${spreadParts.join("")}${plainParts.join("")}${esModulePart}${memberParts.join("")}};`, ); - /* Node's lexer sees the original's names for these entries (esbuild's - * dead annotation), but no accessor of the canonical table: the host - * keeps those names lexer-visible, so ES-module importers link exactly - * as they do against the original. */ - const accessorNames = [...entries.values()] - .filter((spec) => spec.kind === "getter" && lexed.exports.has(spec.name)) - .map((spec) => spec.name); - if (accessorNames.length > 0) host.lexerVisibleNames?.(filePath, accessorNames); + /* Node's lexer skips accessors (and stops reading an object literal at + * the first one), so a canonical table with accessor entries hides + * names the original showed it (esbuild's dead annotation): the host + * keeps the original's lexer answer, so ES-module importers link + * exactly as they do against the original. */ + if ([...entries.values()].some((spec) => spec.kind === "getter")) + host.lexerVisibleNames?.(filePath, [...lexed.exports]); return text + lines.join("\n") + "\n"; } diff --git a/packages/compiler/src/frontend/npm-static.ts b/packages/compiler/src/frontend/npm-static.ts index 77eab1d3a..e00122238 100644 --- a/packages/compiler/src/frontend/npm-static.ts +++ b/packages/compiler/src/frontend/npm-static.ts @@ -63,6 +63,7 @@ export function setNpmStaticPackages>(packages: T): v clearVirtualSources(); untypedPkgCache.clear(); realpathProbed.clear(); + aliasNames.clear(); } export function setNpmStaticDeclarationOverloads( @@ -177,9 +178,32 @@ function shadowTargetOf(path: string): { pkg: string; viaTypes: boolean } | null if (mangledTypesName(pkg) === mangled) return { pkg, viaTypes: true }; } } + // An npm alias (`"x-updated": "npm:x@6.3.0"`) installs the package under + // another directory name: its own manifest names the package. + const aliased = aliasedPackageName(`${norm.slice(0, idx)}/node_modules/${dirName}`); + if (aliased !== null && aliased !== dirName && activePackages.has(aliased)) + return { pkg: aliased, viaTypes: false }; return null; } +/** The "name" a node_modules directory's own package.json declares, or + * null (cached per load). */ +const aliasNames = new Map(); +function aliasedPackageName(pkgDir: string): string | null { + const hit = aliasNames.get(pkgDir); + if (hit !== undefined) return hit; + let name: string | null = null; + try { + const text = trackedReadFile(`${pkgDir}/package.json`); + const declared = text === null ? undefined : (JSON.parse(text) as { name?: unknown }).name; + if (typeof declared === "string") name = declared; + } catch { + name = null; + } + aliasNames.set(pkgDir, name); + return name; +} + /** The opted-in package.json transform, applied identically in BOTH * resolution worlds (the tsgo host's fs shadow and resolve.ts): * 1. every types claim goes — the top-level "types"/"typings" fields diff --git a/packages/compiler/src/frontend/resolve.ts b/packages/compiler/src/frontend/resolve.ts index 463400475..f061ca065 100644 --- a/packages/compiler/src/frontend/resolve.ts +++ b/packages/compiler/src/frontend/resolve.ts @@ -18,6 +18,7 @@ import { dirname, isAbsolute, join, resolve } from "node:path"; import { isNpmStaticPackage, + npmStaticActive, npmStaticPackageOfPath, npmStaticTransformPkgJson, } from "./npm-static.js"; @@ -868,7 +869,11 @@ export function resolveBareModule( // never consulted — mirroring the shadowed world the tsgo host serves. const runtimeImport = mode === "runtime-js" || mode === "runtime-source"; const npmStatic = - mode === "js-only" || (!runtimeImport && mode !== "types-only" && isNpmStaticPackage(pkgName)); + mode === "js-only" || + (!runtimeImport && + mode !== "types-only" && + (isNpmStaticPackage(pkgName) || + (npmStaticActive() && isNpmStaticPackage(aliasedPackageOf(fromFile, pkgName))))); const conditions = runtimeImport ? RUNTIME_IMPORT_CONDITIONS : npmStatic @@ -970,6 +975,23 @@ export function resolveBareModule( return npmStatic ? passOnce("js") : (passOnce("types") ?? passOnce("js")); } +/** The package an npm alias directory installs (`"x-updated": + * "npm:x@6.3.0"` puts package x under node_modules/x-updated): the name + * the nearest node_modules//package.json declares when it + * differs from the directory name, else null. */ +function aliasedPackageOf(fromFile: string, dirName: string): string | null { + for (let dir = dirname(resolve(fromFile)); ; ) { + const pkgDir = join(dir, "node_modules", dirName); + if (isDirectory(pkgDir)) { + const name = pkgJsonOf(pkgDir)?.name; + return typeof name === "string" && name !== dirName ? name : null; + } + const parent = dirname(dir); + if (parent === dir) return null; + dir = parent; + } +} + export function resolveWorkspaceSourceModule( fromFile: string, specifier: string, diff --git a/tests/corpus/6260-local-class-detached-method.ts b/tests/corpus/6260-local-class-detached-method.ts new file mode 100644 index 000000000..528539c48 --- /dev/null +++ b/tests/corpus/6260-local-class-detached-method.ts @@ -0,0 +1,16 @@ +// A method that never reads `this`, taken off an instance of a class +// declared inside a function: the detached method runs without a receiver. +function helper(): string { + return "helper"; +} +function make() { + return class Local { + tag = "t"; + plain(): string { + return helper(); + } + }; +} +const C = make(); +const detached = new C().plain; +console.log(detached()); diff --git a/tests/corpus/6262-comma-null-union-return.cjs b/tests/corpus/6262-comma-null-union-return.cjs new file mode 100644 index 000000000..52cef027e --- /dev/null +++ b/tests/corpus/6262-comma-null-union-return.cjs @@ -0,0 +1,15 @@ +// `return log(), null` from a function that otherwise returns a list: the +// comma expression's effects run, then null takes the result's +// representation. +"use strict"; +async function promote(list) { + if (list.length === 0) return console.log("no static mounts"), null; + return list.map((x) => x * 2); +} +function first(list) { + if (list.length === 0) return console.log("empty"), undefined; + return list[0]; +} +promote([]).then((v) => console.log("promoted", v)); +promote([1, 2]).then((v) => console.log("promoted", v)); +console.log(first([]), first([7])); diff --git a/tests/corpus/6263-strict-arguments-parameters.cjs b/tests/corpus/6263-strict-arguments-parameters.cjs new file mode 100644 index 000000000..463cb316a --- /dev/null +++ b/tests/corpus/6263-strict-arguments-parameters.cjs @@ -0,0 +1,15 @@ +// A CommonJS file under "use strict": a function with named parameters +// reads `arguments`, which in strict code never aliases those parameters. +"use strict"; +function wrap(fn, label) { + label = label + "!"; + return [label, arguments.length, arguments[1], typeof arguments[0]].join(" "); +} +function sum(a, b) { + a = 100; + let total = 0; + for (let i = 0; i < arguments.length; i++) total += arguments[i]; + return total; +} +console.log(wrap(() => 1, "tag")); +console.log(sum(1, 2, 3)); diff --git a/tests/fixtures/npm/cases/2566-npm-alias-two-releases/main.ts b/tests/fixtures/npm/cases/2566-npm-alias-two-releases/main.ts new file mode 100644 index 000000000..6ccf96cdb --- /dev/null +++ b/tests/fixtures/npm/cases/2566-npm-alias-two-releases/main.ts @@ -0,0 +1,7 @@ +// gtroute requires two releases of one package: gtpattern 1.0.0 under its +// own name and 2.0.0 through an npm alias directory (gtpattern-next), +// whose manifest still names gtpattern. Both compile from their own +// JavaScript, as Node loads them. +import { compile } from "gtroute"; + +console.log(compile("/users/:id")); diff --git a/tests/fixtures/npm/cases/2566-npm-alias-two-releases/node_modules/gtroute b/tests/fixtures/npm/cases/2566-npm-alias-two-releases/node_modules/gtroute new file mode 120000 index 000000000..e0ddd1565 --- /dev/null +++ b/tests/fixtures/npm/cases/2566-npm-alias-two-releases/node_modules/gtroute @@ -0,0 +1 @@ +../../../store/gtroute@1.0.0/node_modules/gtroute \ No newline at end of file diff --git a/tests/fixtures/npm/store/gtpattern@1.0.0/node_modules/gtpattern/lib/index.d.ts b/tests/fixtures/npm/store/gtpattern@1.0.0/node_modules/gtpattern/lib/index.d.ts new file mode 100644 index 000000000..1ba7851fa --- /dev/null +++ b/tests/fixtures/npm/store/gtpattern@1.0.0/node_modules/gtpattern/lib/index.d.ts @@ -0,0 +1,2 @@ +export declare const version: string; +export declare function compile(path: string): string; diff --git a/tests/fixtures/npm/store/gtpattern@1.0.0/node_modules/gtpattern/lib/index.js b/tests/fixtures/npm/store/gtpattern@1.0.0/node_modules/gtpattern/lib/index.js new file mode 100644 index 000000000..c76f7222f --- /dev/null +++ b/tests/fixtures/npm/store/gtpattern@1.0.0/node_modules/gtpattern/lib/index.js @@ -0,0 +1,5 @@ +"use strict"; +exports.version = "v1"; +exports.compile = function compile(path) { + return path.split("/").filter((part) => part.length > 0).join("."); +}; diff --git a/tests/fixtures/npm/store/gtpattern@1.0.0/node_modules/gtpattern/package.json b/tests/fixtures/npm/store/gtpattern@1.0.0/node_modules/gtpattern/package.json new file mode 100644 index 000000000..fd67b847e --- /dev/null +++ b/tests/fixtures/npm/store/gtpattern@1.0.0/node_modules/gtpattern/package.json @@ -0,0 +1,6 @@ +{ + "name": "gtpattern", + "version": "1.0.0", + "main": "lib/index.js", + "typings": "lib/index.d.ts" +} diff --git a/tests/fixtures/npm/store/gtpattern@2.0.0/node_modules/gtpattern/lib/index.d.ts b/tests/fixtures/npm/store/gtpattern@2.0.0/node_modules/gtpattern/lib/index.d.ts new file mode 100644 index 000000000..1ba7851fa --- /dev/null +++ b/tests/fixtures/npm/store/gtpattern@2.0.0/node_modules/gtpattern/lib/index.d.ts @@ -0,0 +1,2 @@ +export declare const version: string; +export declare function compile(path: string): string; diff --git a/tests/fixtures/npm/store/gtpattern@2.0.0/node_modules/gtpattern/lib/index.js b/tests/fixtures/npm/store/gtpattern@2.0.0/node_modules/gtpattern/lib/index.js new file mode 100644 index 000000000..a192ea3ab --- /dev/null +++ b/tests/fixtures/npm/store/gtpattern@2.0.0/node_modules/gtpattern/lib/index.js @@ -0,0 +1,5 @@ +"use strict"; +exports.version = "v2"; +exports.compile = function compile(path) { + return path.split("/").filter((part) => part.length > 0).map((part) => (part.startsWith(":") ? "<" + part.slice(1) + ">" : part)).join("/"); +}; diff --git a/tests/fixtures/npm/store/gtpattern@2.0.0/node_modules/gtpattern/package.json b/tests/fixtures/npm/store/gtpattern@2.0.0/node_modules/gtpattern/package.json new file mode 100644 index 000000000..6eb6c6d78 --- /dev/null +++ b/tests/fixtures/npm/store/gtpattern@2.0.0/node_modules/gtpattern/package.json @@ -0,0 +1,6 @@ +{ + "name": "gtpattern", + "version": "2.0.0", + "main": "lib/index.js", + "typings": "lib/index.d.ts" +} diff --git a/tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtpattern b/tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtpattern new file mode 120000 index 000000000..25c5c39d5 --- /dev/null +++ b/tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtpattern @@ -0,0 +1 @@ +../../gtpattern@1.0.0/node_modules/gtpattern \ No newline at end of file diff --git a/tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtpattern-next b/tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtpattern-next new file mode 120000 index 000000000..916b8c35f --- /dev/null +++ b/tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtpattern-next @@ -0,0 +1 @@ +../../gtpattern@2.0.0/node_modules/gtpattern \ No newline at end of file diff --git a/tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtroute/index.d.ts b/tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtroute/index.d.ts new file mode 100644 index 000000000..2d203bf4e --- /dev/null +++ b/tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtroute/index.d.ts @@ -0,0 +1 @@ +export declare function compile(path: string): string; diff --git a/tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtroute/index.js b/tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtroute/index.js new file mode 100644 index 000000000..da52d5e12 --- /dev/null +++ b/tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtroute/index.js @@ -0,0 +1,7 @@ +"use strict"; +const legacy = require("gtpattern"); +const next = require("gtpattern-next"); + +exports.compile = function compile(path) { + return legacy.version + ":" + legacy.compile(path) + " " + next.version + ":" + next.compile(path); +}; diff --git a/tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtroute/package.json b/tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtroute/package.json new file mode 100644 index 000000000..10bee06e7 --- /dev/null +++ b/tests/fixtures/npm/store/gtroute@1.0.0/node_modules/gtroute/package.json @@ -0,0 +1,11 @@ +{ + "name": "gtroute", + "version": "1.0.0", + "description": "depends on two releases of gtpattern, the newer one through an npm alias (\"gtpattern-next\": \"npm:gtpattern@2.0.0\"), in a pnpm store layout", + "main": "index.js", + "types": "index.d.ts", + "dependencies": { + "gtpattern": "1.0.0", + "gtpattern-next": "npm:gtpattern@2.0.0" + } +} diff --git a/tests/harness/npm-cases.ts b/tests/harness/npm-cases.ts index 87a6de2a8..69ce3917f 100644 --- a/tests/harness/npm-cases.ts +++ b/tests/harness/npm-cases.ts @@ -19,12 +19,12 @@ export function npmCases(fixturesRoot: string): NpmCase[] { return [ ...globSync(join(fixturesRoot, "npm/cases/*/main.ts")) .sort() - // 2465-2469 and 2556-2565 are the --npm-static bundler-emitted-CJS + // 2465-2469 and 2556-2566 are the --npm-static bundler-emitted-CJS // cases (npm-static.test.ts drives them with the opt-in): their // consumers exercise the static rewrite's surface (lexer-visible // names the shipped .d.ts never declares, the __toESM interop // family), so they stay out of the flagless island lane by design. - .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-5])-[^/]+\/main\.ts$/.test(entry)) + .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-6])-[^/]+\/main\.ts$/.test(entry)) .map((entry) => ({ name: entry.split("/").at(-2)!, entry, diff --git a/tests/harness/npm-static.test.ts b/tests/harness/npm-static.test.ts index 943b81695..1741c7470 100644 --- a/tests/harness/npm-static.test.ts +++ b/tests/harness/npm-static.test.ts @@ -76,8 +76,9 @@ async function buildStatic(entry: string, npmStatic: string[] | "auto"): Promise ...globSync( join(fixturesRoot, "npm/node_modules/cryptozoo/**/*.{js,mjs,cjs,json,d.ts}"), ).sort(), - // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2565) + // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2566) ...globSync(join(fixturesRoot, "npm/node_modules/gt*/**/*.{js,cjs,json}")).sort(), + ...globSync(join(fixturesRoot, "npm/store/gt*/**/*.{js,cjs,json}")).sort(), ]; for (const f of inputs) hash.update(f).update(readFileSync(f)); const key = hash @@ -1489,6 +1490,26 @@ describe(`npm-static pilots${sanitize ? " (sanitized)" : ""}`, () => { await expectNodeParity(join(fixturesRoot, `npm/cases/${name}/main.ts`), packages); }, 180_000); + // 2566: two releases of one package in a pnpm store, the newer through + // an npm alias directory; the default selection compiles both copies. + test("an npm alias of a selected package compiles from its JavaScript", async () => { + const entry = join(fixturesRoot, "npm/cases/2566-npm-alias-two-releases/main.ts"); + const { coverage } = analyze(entry); + expect(coverage.npmStatic).toEqual( + expect.arrayContaining([ + { package: "gtroute", status: "static" }, + { package: "gtpattern", status: "static" }, + ]), + ); + const binary = await buildStatic(entry, "auto"); + const [nodeRes, nativeRes] = await Promise.all([ + runBinary("node", [entry]), + runBinary(binary, []), + ]); + expect(nativeRes.stdout.toString("utf8")).toBe(nodeRes.stdout.toString("utf8")); + expect(nativeRes.exitCode).toBe(nodeRes.exitCode); + }, 180_000); + // 2562: a file with unverifiable interop that no compiled code can load // (the program imports a leaf data module and only a type from the // main entry) leaves its package static. From 02dba55958f66d22f02e621a1d572e7fe09db367 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 16:55:55 -0700 Subject: [PATCH 34/83] Record order baselines for the new cases and corpus programs; format Co-Authored-By: Claude Opus 5.5 --- .../compiler/src/frontend/lowering/lower-exprs.ts | 14 ++++++++++++-- packages/compiler/src/frontend/resolve.ts | 2 +- .../6260-local-class-detached-method.ts.json | 6 ++++++ .../corpus/6262-comma-null-union-return.cjs.json | 6 ++++++ .../6263-strict-arguments-parameters.cjs.json | 6 ++++++ .../npm/cases/2564-lazy-barrel/main.ts.json | 6 ++++++ .../cases/2565-lazy-barrel-require/main.ts.json | 6 ++++++ .../cases/2566-npm-alias-two-releases/main.ts.json | 6 ++++++ tests/harness/npm-static.test.ts | 10 +++++++--- 9 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6260-local-class-detached-method.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6262-comma-null-union-return.cjs.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6263-strict-arguments-parameters.cjs.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2564-lazy-barrel/main.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2565-lazy-barrel-require/main.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2566-npm-alias-two-releases/main.ts.json diff --git a/packages/compiler/src/frontend/lowering/lower-exprs.ts b/packages/compiler/src/frontend/lowering/lower-exprs.ts index cabbc31f5..afcecb2c5 100644 --- a/packages/compiler/src/frontend/lowering/lower-exprs.ts +++ b/packages/compiler/src/frontend/lowering/lower-exprs.ts @@ -12595,7 +12595,12 @@ function cjsAccessorSnapshotOf( const call = cjsAccessorCall(lowerer, getter, locOf(getter)); if (call === null) return null; const snapshot = { id: `%g.cjsSnapshot.${lowerer.cjsAccessorSnapshots.size}`, type: call.type }; - lowerer.globalsList.push({ id: snapshot.id, name: "%cjsSnapshot", type: call.type, mutable: true }); + lowerer.globalsList.push({ + id: snapshot.id, + name: "%cjsSnapshot", + type: call.type, + mutable: true, + }); lowerer.cjsAccessorSnapshots.set(getter, snapshot); return snapshot; } @@ -12619,7 +12624,12 @@ export function cjsAccessorSnapshotStores( lowerer.cjsSnapshotReady.set(dep, readyId); } const stores: IrStmt[] = [ - { kind: "assign", localId: readyId, value: { kind: "boolLit", value: true, type: BOOL, loc }, loc }, + { + kind: "assign", + localId: readyId, + value: { kind: "boolLit", value: true, type: BOOL, loc }, + loc, + }, ]; for (const getter of getters) { const snapshot = cjsAccessorSnapshotOf(lowerer, getter); diff --git a/packages/compiler/src/frontend/resolve.ts b/packages/compiler/src/frontend/resolve.ts index f061ca065..36470a643 100644 --- a/packages/compiler/src/frontend/resolve.ts +++ b/packages/compiler/src/frontend/resolve.ts @@ -980,7 +980,7 @@ export function resolveBareModule( * the nearest node_modules//package.json declares when it * differs from the directory name, else null. */ function aliasedPackageOf(fromFile: string, dirName: string): string | null { - for (let dir = dirname(resolve(fromFile)); ; ) { + for (let dir = dirname(resolve(fromFile)); ;) { const pkgDir = join(dir, "node_modules", dirName); if (isDirectory(pkgDir)) { const name = pkgJsonOf(pkgDir)?.name; diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6260-local-class-detached-method.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6260-local-class-detached-method.ts.json new file mode 100644 index 000000000..d73c701d7 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6260-local-class-detached-method.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6260-local-class-detached-method.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6262-comma-null-union-return.cjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6262-comma-null-union-return.cjs.json new file mode 100644 index 000000000..e167cc977 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6262-comma-null-union-return.cjs.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6262-comma-null-union-return.cjs" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6263-strict-arguments-parameters.cjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6263-strict-arguments-parameters.cjs.json new file mode 100644 index 000000000..b7ff6a960 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6263-strict-arguments-parameters.cjs.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6263-strict-arguments-parameters.cjs" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2564-lazy-barrel/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2564-lazy-barrel/main.ts.json new file mode 100644 index 000000000..b14560cea --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2564-lazy-barrel/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2564-lazy-barrel/main.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2565-lazy-barrel-require/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2565-lazy-barrel-require/main.ts.json new file mode 100644 index 000000000..77cf9e52c --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2565-lazy-barrel-require/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2565-lazy-barrel-require/main.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2566-npm-alias-two-releases/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2566-npm-alias-two-releases/main.ts.json new file mode 100644 index 000000000..a412bc875 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2566-npm-alias-two-releases/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2566-npm-alias-two-releases/main.ts" + ], + "diags": [] +} diff --git a/tests/harness/npm-static.test.ts b/tests/harness/npm-static.test.ts index 1741c7470..3543aeb0a 100644 --- a/tests/harness/npm-static.test.ts +++ b/tests/harness/npm-static.test.ts @@ -1486,9 +1486,13 @@ describe(`npm-static pilots${sanitize ? " (sanitized)" : ""}`, () => { test.each([ ["2564-lazy-barrel", ["gtlazy", "gtlazyws"]], ["2565-lazy-barrel-require", ["gtlazyuser", "gtlazy"]], - ])("the lazy barrel %s compiles statically and byte-matches Node", async (name, packages) => { - await expectNodeParity(join(fixturesRoot, `npm/cases/${name}/main.ts`), packages); - }, 180_000); + ])( + "the lazy barrel %s compiles statically and byte-matches Node", + async (name, packages) => { + await expectNodeParity(join(fixturesRoot, `npm/cases/${name}/main.ts`), packages); + }, + 180_000, + ); // 2566: two releases of one package in a pnpm store, the newer through // an npm alias directory; the default selection compiles both copies. From c18a4a790a46ca2d9c30295dfd57cf7a8c37fa9b Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Mon, 19 Oct 2026 19:07:43 -0500 Subject: [PATCH 35/83] Resolve chunk references by scope, keep shared functions out of chunk copies, and treat indirect calls as calls in cycles Three fixes for the bundled-chunk splitter and the CommonJS cycle check: - A reference counts as local only when a scope between it and the chunk factory (or the copied helper) declares its name. A nested function, block, loop or catch binding with the name of a top-level helper no longer hides the chunk's read of that helper. Before, the helper was not copied and the chunk failed at runtime on the unresolved reference. - A top-level function is copied into a chunk only when the bundle calls it and uses it in no other way. Each chunk gets its own copy, so a function that is exported, compared, constructed, or given properties would lose its shared identity. Such a chunk now keeps the package refused. - During a cycle's initialization, a read of a partner's exported function is safe only as a plain value read. A call through esbuild's `(0, binding.fn)()` spelling, `new`, a tagged template, a member of the function, or passing it to a call can run the function, so these uses keep SC1016 (Node throws when the function reads a binding in its temporal dead zone). --- .../npm-static-bundled-chunks.test.ts | 60 ++++++ .../src/frontend/npm-static-bundled-chunks.ts | 172 +++++++++++++++++- packages/compiler/src/frontend/program.ts | 21 ++- .../2564-chunk-helper-shadowing/main.ts.json | 6 + .../2565-chunk-shared-function/main.ts.json | 6 + .../2566-cjs-cycle-indirect-call/main.ts.json | 6 + .../cases/2564-chunk-helper-shadowing/main.ts | 7 + .../cases/2565-chunk-shared-function/main.ts | 7 + .../2566-cjs-cycle-indirect-call/main.ts | 7 + .../gtchunkshadow/dist/index.d.ts | 2 + .../node_modules/gtchunkshadow/dist/index.js | 52 ++++++ .../node_modules/gtchunkshadow/package.json | 7 + .../gtchunkshared/dist/index.d.ts | 2 + .../node_modules/gtchunkshared/dist/index.js | 56 ++++++ .../node_modules/gtchunkshared/package.json | 7 + .../node_modules/gtcyclecall/dist/index.d.ts | 3 + .../node_modules/gtcyclecall/dist/index.js | 37 ++++ .../node_modules/gtcyclecall/dist/reader.js | 33 ++++ .../npm/node_modules/gtcyclecall/package.json | 7 + tests/harness/npm-cases.ts | 4 +- tests/harness/npm-static.test.ts | 42 ++++- 21 files changed, 536 insertions(+), 8 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts.json create mode 100644 tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts create mode 100644 tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts create mode 100644 tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts create mode 100644 tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtchunkshadow/package.json create mode 100644 tests/fixtures/npm/node_modules/gtchunkshared/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtchunkshared/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtchunkshared/package.json create mode 100644 tests/fixtures/npm/node_modules/gtcyclecall/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtcyclecall/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtcyclecall/dist/reader.js create mode 100644 tests/fixtures/npm/node_modules/gtcyclecall/package.json diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts index 82926d2b8..fe54681d7 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts @@ -111,6 +111,66 @@ console.log(require_named().name); expect(chunk).toContain("var __defProp = Object.defineProperty;"); }); +test("a helper travels with a chunk whose nested scopes declare its name", () => { + const answer = expectSameBehavior(`${keyedHelper}function label(n) { + return "label " + n; +} +function wrap(n) { + function inner() { + var label = "inner"; + return label; + } + return label(n) + " " + inner(); +} +var require_e = __commonJS({ + "e.js"(exports2, module2) { + function nested() { + var label = "nested"; + return label; + } + var seen = []; + for (let label of ["loop"]) seen.push(label); + try { + throw "caught"; + } catch (label) { + seen.push(label); + } + { + const label = "block"; + seen.push(label); + } + module2.exports = { outer: label(1), indirect: (0, label)(3), wrapped: wrap(2), nested: nested(), seen }; + } +}); +var e = require_e(); +console.log(e.outer, e.indirect, e.wrapped, e.nested, e.seen.join(",")); +`); + const chunk = answer.chunks[0]!.source; + expect(chunk).toContain("function label(n)"); + expect(chunk).toContain("function wrap(n)"); +}); + +test.each([ + ["exports it", `function tally() {}`, `module2.exports = { tally };`], + ["reads its properties", `function tally() {}`, `exports2.calls = tally.calls;`], + ["constructs it", `function tally() {}`, `exports2.made = new tally();`], + ["stores an arrow helper", `var tally = () => 1;`, `exports2.fn = tally;`], +])("a chunk that %s keeps the package refused", (_, declaration, statement) => { + const result = split(`${keyedHelper}${declaration} +var require_f = __commonJS({ + "f.js"(exports2, module2) { + ${statement} + } +}); +require_f(); +`); + expect(result).toEqual({ + degrade: expect.stringContaining( + "uses the bundle's top-level function 'tally' as a value", + ) as string, + }); +}); + test.each([ ["reads its factory's receiver", `console.log(this);`], ["reads its factory's arguments object", `console.log(arguments.length);`], diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts index cdb9dff64..abc86bcbd 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts @@ -105,6 +105,119 @@ function declaredNames(root: ts.Node): Set { return names; } +/** Adds the identifiers a binding name (an identifier or a destructuring + * pattern) declares. */ +function addBindingNames(name: ts.BindingName, out: Set): void { + if (ts.isIdentifier(name)) { + out.add(name.text); + return; + } + for (const element of name.elements.nodes) { + if (!ts.isOmittedExpression(element) && element.name !== undefined) + addBindingNames(element.name, out); + } +} + +function isFunctionScope(n: ts.Node): n is ts.SignatureDeclaration { + return ( + ts.isFunctionDeclaration(n) || + ts.isFunctionExpression(n) || + ts.isArrowFunction(n) || + ts.isMethodDeclaration(n) || + ts.isGetAccessorDeclaration(n) || + ts.isSetAccessorDeclaration(n) || + ts.isConstructorDeclaration(n) + ); +} + +/** The `var` names a function body declares, without entering nested + * functions. Function declarations in nested blocks count as block-scoped + * here; treating one as free only adds an unused helper copy. */ +function varScopedNames(body: ts.Node): Set { + const out = new Set(); + ts.walkPreorder(body, (n) => { + if (n !== body && (isFunctionScope(n) || ts.isClassDeclaration(n) || ts.isClassExpression(n))) + return "skip"; + if (ts.isVariableDeclarationList(n) && (n.flags & ts.NodeFlags.BlockScoped) === 0) { + for (const d of n.declarations.nodes) addBindingNames(d.name, out); + } + return undefined; + }); + return out; +} + +/** The names one scope node binds for the references inside it. */ +function scopeNames(scope: ts.Node): Set { + const out = new Set(); + if (isFunctionScope(scope)) { + for (const param of scope.parameters.nodes) addBindingNames(param.name, out); + if (ts.isFunctionExpression(scope) && scope.name !== undefined) out.add(scope.name.text); + if (scope.body !== undefined) for (const name of varScopedNames(scope.body)) out.add(name); + } else if (ts.isBlock(scope) || ts.isSourceFile(scope) || ts.isCaseBlock(scope)) { + const statements = ts.isCaseBlock(scope) + ? scope.clauses.nodes.flatMap((clause) => clause.statements.nodes) + : scope.statements.nodes; + for (const stmt of statements) { + if ((ts.isFunctionDeclaration(stmt) || ts.isClassDeclaration(stmt)) && stmt.name) + out.add(stmt.name.text); + else if ( + ts.isVariableStatement(stmt) && + (stmt.declarationList.flags & ts.NodeFlags.BlockScoped) !== 0 + ) { + for (const d of stmt.declarationList.declarations.nodes) addBindingNames(d.name, out); + } + } + } else if (ts.isCatchClause(scope) && scope.variableDeclaration !== undefined) { + addBindingNames(scope.variableDeclaration.name, out); + } else if ( + (ts.isForStatement(scope) || ts.isForInStatement(scope) || ts.isForOfStatement(scope)) && + scope.initializer !== undefined && + ts.isVariableDeclarationList(scope.initializer) && + (scope.initializer.flags & ts.NodeFlags.BlockScoped) !== 0 + ) { + for (const d of scope.initializer.declarations.nodes) addBindingNames(d.name, out); + } else if ( + (ts.isClassDeclaration(scope) || ts.isClassExpression(scope)) && + scope.name !== undefined + ) { + out.add(scope.name.text); + } + return out; +} + +/** Resolves references against one bundle's scopes, keeping each scope's + * names for the bundle's split. */ +class BundleScopes { + private readonly names = new Map>(); + + private of(scope: ts.Node): Set { + let names = this.names.get(scope); + if (names === undefined) { + names = scopeNames(scope); + this.names.set(scope, names); + } + return names; + } + + /** True when a scope between the reference and `root` (inclusive) + * binds its name. */ + boundWithin(id: ts.Identifier, root: ts.Node): boolean { + for (let n = id.parent; n !== undefined; n = n.parent) { + if (this.of(n).has(id.text)) return true; + if (n === root) return false; + } + return false; + } + + /** True when the reference resolves to a top-level binding of `sf`. */ + topLevel(id: ts.Identifier, sf: ts.SourceFile): boolean { + for (let n = id.parent; n !== undefined && n !== sf; n = n.parent) { + if (this.of(n).has(id.text)) return false; + } + return true; + } +} + /** Every binding reference inside a subtree. */ function references(root: ts.Node): ts.Identifier[] { const out: ts.Identifier[] = []; @@ -230,6 +343,33 @@ function copyableInitializer(init: ts.Expression | undefined): boolean { return depth > 0 && ts.isIdentifier(walk) && walk.text === "Object"; } +/** A top-level declaration whose value is a function. */ +function isFunctionValued(node: ts.VariableDeclaration | ts.FunctionDeclaration): boolean { + if (ts.isFunctionDeclaration(node)) return true; + const init = node.initializer && unparen(node.initializer); + return init !== undefined && (ts.isArrowFunction(init) || ts.isFunctionExpression(init)); +} + +/** True when a reference is the function a call invokes: `f(...)`, or the + * `(0, f)(...)` form that drops the receiver. */ +function isCallee(id: ts.Identifier): boolean { + let callee: ts.Node = id; + const p = id.parent; + if ( + p !== undefined && + ts.isBinaryExpression(p) && + p.operatorToken.kind === ts.SyntaxKind.CommaToken && + p.right === id && + p.parent !== undefined && + ts.isParenthesizedExpression(p.parent) + ) + callee = p.parent; + while (callee.parent !== undefined && ts.isParenthesizedExpression(callee.parent)) + callee = callee.parent; + const call = callee.parent; + return call !== undefined && ts.isCallExpression(call) && call.expression === callee; +} + /** One recognized chunk declaration. */ interface Chunk { name: string; @@ -302,6 +442,12 @@ export function splitBundledChunks( if (declaredAnywhere.has("Object") || written.has("Object")) return { degrade: CHUNK_SHAPE_DEGRADE }; const stable = (name: string): boolean => !duplicate.has(name) && !written.has(name); + // Names whose every reference in the bundle is a callee: `f(...)` or the + // `(0, f)(...)` form. + const scopes = new BundleScopes(); + const valueUsed = new Set(); + for (const r of allRefs) if (!isCallee(r) && scopes.topLevel(r, sf)) valueUsed.add(r.text); + const calledOnly = (name: string): boolean => !valueUsed.has(name); const ownNames = (name: string): boolean => { const t = top.get(name); if (t === undefined || !stable(name) || !ts.isVariableDeclaration(t.node)) return false; @@ -422,7 +568,17 @@ export function splitBundledChunks( const out: BundledChunkSplit["chunks"] = []; const bundleEdits: Edit[] = [...callEdits]; for (const chunk of chunks.values()) { - const result = chunkSource(sf, chunk, chunks, top, stable, callEdits, bundleStrict); + const result = chunkSource( + sf, + chunk, + chunks, + top, + stable, + calledOnly, + scopes, + callEdits, + bundleStrict, + ); if (typeof result !== "string") return result; out.push({ path: chunk.path, source: result }); bundleEdits.push({ start: chunk.stmt.getStart(sf), end: chunk.stmt.getEnd(), text: "" }); @@ -479,6 +635,8 @@ function chunkSource( chunks: ReadonlyMap, top: ReadonlyMap, stable: (name: string) => boolean, + calledOnly: (name: string) => boolean, + scopes: BundleScopes, callEdits: readonly Edit[], bundleStrict: boolean, ): string | { degrade: string } { @@ -563,7 +721,8 @@ function chunkSource( } continue; } - if (local.has(name)) continue; + // The factory function itself binds the parameters and `var`s. + if (scopes.boundWithin(r, body.parent ?? body)) continue; if (name === "exports" || name === "module") return why(`reads the bundle's own '${name}' binding`); if (name === "__filename") return why("reads __filename, which names the bundle file"); @@ -587,9 +746,14 @@ function chunkSource( (ts.isVariableDeclaration(node) && copyableInitializer(node.initializer))); if (!pure || chunks.has(name)) return why(`reads the bundle's top-level binding '${name}', which it cannot carry along`); - const own = declaredNames(node); + // Each chunk gets its own copy, so a function's identity and its + // properties must stay unobservable: the bundle only ever calls it. + if (isFunctionValued(node) && !calledOnly(name)) + return why( + `uses the bundle's top-level function '${name}' as a value, which its chunks cannot share`, + ); for (const r of references(node)) { - if (own.has(r.text) || !top.has(r.text) || copied.has(r.text)) continue; + if (scopes.boundWithin(r, node) || !top.has(r.text) || copied.has(r.text)) continue; copied.add(r.text); copyQueue.push(r.text); } diff --git a/packages/compiler/src/frontend/program.ts b/packages/compiler/src/frontend/program.ts index 908a587ea..12d9b703a 100644 --- a/packages/compiler/src/frontend/program.ts +++ b/packages/compiler/src/frontend/program.ts @@ -3207,11 +3207,30 @@ function cjsCycleRefusal7( function functionExportRead7(id: ts.Identifier, dep: ts.SourceFile): boolean { const access = id.parent; if (!ts.isPropertyAccessExpression(access) || access.expression !== id) return false; + // The value the read yields: through parentheses and the right operand + // of a comma, so `(0, binding.name)(...)` is the call it spells. let use: ts.Node = access; - while (ts.isParenthesizedExpression(use.parent)) use = use.parent; + for (;;) { + const p = use.parent; + if (ts.isParenthesizedExpression(p)) use = p; + else if ( + ts.isBinaryExpression(p) && + p.operatorToken.kind === ts.SyntaxKind.CommaToken && + p.right === use + ) + use = p; + else break; + } const parent = use.parent; + // A use that can run the function (a call, `new`, a tag, a member of it + // such as `.call`, or an argument another call receives) is not a read. if ( (ts.isCallExpression(parent) && parent.expression === use) || + (ts.isCallExpression(parent) && parent.arguments.nodes.includes(use as ts.Expression)) || + ts.isNewExpression(parent) || + (ts.isTaggedTemplateExpression(parent) && parent.tag === use) || + (ts.isPropertyAccessExpression(parent) && parent.expression === use) || + (ts.isElementAccessExpression(parent) && parent.expression === use) || ts.isDeleteExpression(parent) || ((ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent)) && (parent.operator === ts.SyntaxKind.PlusPlusToken || diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts.json new file mode 100644 index 000000000..90e304cbe --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts.json new file mode 100644 index 000000000..6443d170b --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts.json new file mode 100644 index 000000000..f51a29951 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts" + ], + "diags": [] +} diff --git a/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts b/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts new file mode 100644 index 000000000..d52b3731d --- /dev/null +++ b/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts @@ -0,0 +1,7 @@ +// A bundled CommonJS chunk that calls the bundle's top-level `label` while +// a nested function of the chunk declares its own `label`: the chunk +// carries the top-level function along, and each reference keeps its own +// binding. +import { inner, outer } from "gtchunkshadow"; + +console.log(outer(), inner()); diff --git a/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts b/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts new file mode 100644 index 000000000..21ca81b40 --- /dev/null +++ b/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts @@ -0,0 +1,7 @@ +// Two bundled CommonJS chunks export the bundle's top-level `tally` and +// count calls on a property of it. Node prints "1 2 true": both chunks +// share one function. A per-chunk copy would split its identity and its +// properties, so the package stays refused. +import { counts, same } from "gtchunkshared"; + +console.log(counts(), same()); diff --git a/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts b/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts new file mode 100644 index 000000000..3cca145e0 --- /dev/null +++ b/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts @@ -0,0 +1,7 @@ +// A require cycle whose partner calls an export of index.js with esbuild's +// `(0, import_index.peek)()` spelling while index is still initializing: +// peek reads a const in its temporal dead zone, so Node throws a +// ReferenceError, and scriptc refuses the cycle. +import { show } from "gtcyclecall"; + +console.log(show()); diff --git a/tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.d.ts new file mode 100644 index 000000000..238cf1744 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.d.ts @@ -0,0 +1,2 @@ +export declare function outer(): string; +export declare function inner(): string; diff --git a/tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.js b/tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.js new file mode 100644 index 000000000..0f89c7ec5 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.js @@ -0,0 +1,52 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __commonJS = (cb, mod) => function __require() { + return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; +}; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +function label(n) { + return "label " + n; +} + +// chunk/vendor/shadow.js +var require_shadow = __commonJS({ + "chunk/vendor/shadow.js"(exports2, module2) { + function nested() { + var label = "nested"; + return label; + } + module2.exports = { outer: label(1), inner: nested() }; + } +}); + +// chunk/src/index.ts +var index_exports = {}; +__export(index_exports, { + inner: () => inner, + outer: () => outer +}); +module.exports = __toCommonJS(index_exports); +function outer() { + return require_shadow().outer; +} +function inner() { + return require_shadow().inner; +} +0 && (module.exports = { + inner, + outer +}); diff --git a/tests/fixtures/npm/node_modules/gtchunkshadow/package.json b/tests/fixtures/npm/node_modules/gtchunkshadow/package.json new file mode 100644 index 000000000..6830fbd2c --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunkshadow/package.json @@ -0,0 +1,7 @@ +{ + "name": "gtchunkshadow", + "version": "1.0.0", + "description": "esbuild's CommonJS bundle shape: a chunk calls a top-level function whose name a nested scope of the chunk also declares", + "main": "./dist/index.js", + "types": "./dist/index.d.ts" +} diff --git a/tests/fixtures/npm/node_modules/gtchunkshared/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtchunkshared/dist/index.d.ts new file mode 100644 index 000000000..3a47a0495 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunkshared/dist/index.d.ts @@ -0,0 +1,2 @@ +export declare function same(): boolean; +export declare function counts(): string; diff --git a/tests/fixtures/npm/node_modules/gtchunkshared/dist/index.js b/tests/fixtures/npm/node_modules/gtchunkshared/dist/index.js new file mode 100644 index 000000000..0d3aacec8 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunkshared/dist/index.js @@ -0,0 +1,56 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __commonJS = (cb, mod) => function __require() { + return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; +}; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +function tally() { + tally.calls = (tally.calls || 0) + 1; + return tally.calls; +} + +// chunk/vendor/a.js +var require_a = __commonJS({ + "chunk/vendor/a.js"(exports2, module2) { + module2.exports = { tally, first: tally() }; + } +}); + +// chunk/vendor/b.js +var require_b = __commonJS({ + "chunk/vendor/b.js"(exports2, module2) { + module2.exports = { tally, second: tally() }; + } +}); + +// chunk/src/index.ts +var index_exports = {}; +__export(index_exports, { + counts: () => counts, + same: () => same +}); +module.exports = __toCommonJS(index_exports); +function same() { + return require_a().tally === require_b().tally; +} +function counts() { + return require_a().first + " " + require_b().second; +} +0 && (module.exports = { + counts, + same +}); diff --git a/tests/fixtures/npm/node_modules/gtchunkshared/package.json b/tests/fixtures/npm/node_modules/gtchunkshared/package.json new file mode 100644 index 000000000..bfc90c5d2 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunkshared/package.json @@ -0,0 +1,7 @@ +{ + "name": "gtchunkshared", + "version": "1.0.0", + "description": "esbuild's CommonJS bundle shape: two chunks export one top-level function and count calls on a property of it", + "main": "./dist/index.js", + "types": "./dist/index.d.ts" +} diff --git a/tests/fixtures/npm/node_modules/gtcyclecall/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtcyclecall/dist/index.d.ts new file mode 100644 index 000000000..5f4373e2f --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclecall/dist/index.d.ts @@ -0,0 +1,3 @@ +export declare const LIMIT = 3; +export declare function show(): string; +export declare function peek(): number; diff --git a/tests/fixtures/npm/node_modules/gtcyclecall/dist/index.js b/tests/fixtures/npm/node_modules/gtcyclecall/dist/index.js new file mode 100644 index 000000000..77aca3c9b --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclecall/dist/index.js @@ -0,0 +1,37 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var index_exports = {}; +__export(index_exports, { + LIMIT: () => LIMIT, + peek: () => peek, + show: () => show +}); +module.exports = __toCommonJS(index_exports); +var import_reader = require("./reader"); +const LIMIT = 3; +function peek() { + return LIMIT; +} +function show() { + return (0, import_reader.describe)(); +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + LIMIT, + show +}); diff --git a/tests/fixtures/npm/node_modules/gtcyclecall/dist/reader.js b/tests/fixtures/npm/node_modules/gtcyclecall/dist/reader.js new file mode 100644 index 000000000..c98d1ae46 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclecall/dist/reader.js @@ -0,0 +1,33 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var reader_exports = {}; +__export(reader_exports, { + captured: () => captured, + describe: () => describe +}); +module.exports = __toCommonJS(reader_exports); +var import_index = require("./index"); +const captured = (0, import_index.peek)(); +function describe() { + return "limit " + captured; +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + captured, + describe +}); diff --git a/tests/fixtures/npm/node_modules/gtcyclecall/package.json b/tests/fixtures/npm/node_modules/gtcyclecall/package.json new file mode 100644 index 000000000..cc1069a6f --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclecall/package.json @@ -0,0 +1,7 @@ +{ + "name": "gtcyclecall", + "version": "1.0.0", + "description": "esbuild 0.25.10 per-file CommonJS output shape of a cycle whose partner calls an exported function through (0, binding.fn)() while that function's const binding is uninitialized", + "main": "./dist/index.js", + "types": "./dist/index.d.ts" +} diff --git a/tests/harness/npm-cases.ts b/tests/harness/npm-cases.ts index 4b3b9fdee..451441009 100644 --- a/tests/harness/npm-cases.ts +++ b/tests/harness/npm-cases.ts @@ -19,12 +19,12 @@ export function npmCases(fixturesRoot: string): NpmCase[] { return [ ...globSync(join(fixturesRoot, "npm/cases/*/main.ts")) .sort() - // 2465-2469 and 2556-2562 are the --npm-static bundler-emitted-CJS + // 2465-2469 and 2556-2566 are the --npm-static bundler-emitted-CJS // cases (npm-static.test.ts drives them with the opt-in): their // consumers exercise the static rewrite's surface (lexer-visible // names the shipped .d.ts never declares, the __toESM interop // family), so they stay out of the flagless island lane by design. - .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-2])-[^/]+\/main\.ts$/.test(entry)) + .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-24-6])-[^/]+\/main\.ts$/.test(entry)) .map((entry) => ({ name: entry.split("/").at(-2)!, entry, diff --git a/tests/harness/npm-static.test.ts b/tests/harness/npm-static.test.ts index a8df98c52..4930487e2 100644 --- a/tests/harness/npm-static.test.ts +++ b/tests/harness/npm-static.test.ts @@ -76,7 +76,7 @@ async function buildStatic(entry: string, npmStatic: string[] | "auto"): Promise ...globSync( join(fixturesRoot, "npm/node_modules/cryptozoo/**/*.{js,mjs,cjs,json,d.ts}"), ).sort(), - // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2562) + // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2566) ...globSync(join(fixturesRoot, "npm/node_modules/gt*/**/*.{js,cjs,json}")).sort(), ]; for (const f of inputs) hash.update(f).update(readFileSync(f)); @@ -1466,6 +1466,46 @@ describe(`npm-static pilots${sanitize ? " (sanitized)" : ""}`, () => { ]); }, 180_000); + // 2566: the same early read through esbuild's `(0, binding.fn)()` call + // spelling also keeps SC1016. + test("a CommonJS cycle that calls an export during initialization stays refused", () => { + const entry = join(fixturesRoot, "npm/cases/2566-cjs-cycle-indirect-call/main.ts"); + const { coverage } = analyze(entry, { npmStatic: ["gtcyclecall"] }); + expect(coverage.npmStatic).toEqual([ + { + package: "gtcyclecall", + status: "refused", + detail: expect.stringMatching( + /circular imports .*can use its binding 'import_index' of a module of the cycle/, + ) as string, + }, + ]); + }, 120_000); + + // 2564: a chunk reads a top-level function whose name a nested scope of + // the chunk also declares; the function still travels with the chunk. + test("a chunk carries a top-level function its nested scopes shadow", async () => { + await expectNodeParity(join(fixturesRoot, "npm/cases/2564-chunk-helper-shadowing/main.ts"), [ + "gtchunkshadow", + ]); + }, 180_000); + + // 2565: Node shares one function between the chunks that export it; a + // per-chunk copy would not, so the package stays refused. + test("chunks that export a shared top-level function stay refused", () => { + const entry = join(fixturesRoot, "npm/cases/2565-chunk-shared-function/main.ts"); + const { coverage } = analyze(entry, { npmStatic: ["gtchunkshared"] }); + expect(coverage.npmStatic).toEqual([ + { + package: "gtchunkshared", + status: "refused", + detail: expect.stringMatching( + /uses the bundle's top-level function 'tally' as a value, which its chunks cannot share/, + ) as string, + }, + ]); + }, 120_000); + test("published type-only declarations coexist with a static runtime bundle", async () => { const entry = join(fixturesRoot, "npm/cases/2469-bundle-offender/main.ts"); const { coverage } = analyze(entry, { npmStatic: ["gtghost"] }); From 4f7539103c553f46a0cdfdf8bdafc7e5f611dd6d Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Mon, 19 Oct 2026 19:10:11 -0500 Subject: [PATCH 36/83] Keep a lowered ES module namespace unobservable through calls that pass it as `this` A call through a bundled ES module's namespace (`ns.fn()`, `(ns.fn)()`, a tagged template) passes the namespace as `this`. Node's namespace is the __toCommonJS object (`__esModule` first and non-enumerable, non-configurable getters); the lowered object of getters differs. The namespace lowers only when every such call targets a top-level function, never reassigned, that reads no `this` (an arrow inside it included); otherwise the package stays refused. --- .../frontend/npm-static-bundled-esm.test.ts | 36 +++++++ .../src/frontend/npm-static-bundled-esm.ts | 58 +++++++++++- .../2567-esm-namespace-this/main.ts.json | 6 ++ .../npm/cases/2567-esm-namespace-this/main.ts | 7 ++ .../node_modules/gtesmthis/dist/index.d.ts | 2 + .../npm/node_modules/gtesmthis/dist/index.js | 94 +++++++++++++++++++ .../npm/node_modules/gtesmthis/package.json | 10 ++ tests/harness/npm-cases.ts | 4 +- tests/harness/npm-static.test.ts | 17 +++- 9 files changed, 227 insertions(+), 7 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2567-esm-namespace-this/main.ts.json create mode 100644 tests/fixtures/npm/cases/2567-esm-namespace-this/main.ts create mode 100644 tests/fixtures/npm/node_modules/gtesmthis/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtesmthis/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtesmthis/package.json diff --git a/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts b/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts index 0b9ecd042..46ebfad4f 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts @@ -108,6 +108,42 @@ console.log(load(), (init_lazy(), __toCommonJS(lazy_exports)).value()); expect(lowered).toContain('(require("os")).EOL'); }); +test("calls of exported functions that never read their own `this` still lower", () => { + const lowered = expectSameBehavior(`${helpers}var t_exports = {}; +__export(t_exports, { plain: () => plain, inner: () => inner }); +function plain(x) { return "plain " + x; } +function inner() { + function own() { return typeof this; } + return "inner " + own.call(1); +} +var init_t = __esm({ "t.ts"() {} }); +const t = (init_t(), __toCommonJS(t_exports)); +console.log(t.plain(1), (t.plain)(2), t.inner()); +`); + expect(lowered).not.toContain("__toCommonJS(t_exports)"); +}); + +test.each([ + ["a direct call", `NS.who()`], + ["a parenthesized callee", `(NS.who)()`], + ["a tagged template", "NS.who`x`"], + ["a call through a local", `(() => { const m = NS; return m.who(); })()`], + ["an arrow inside the function", `NS.arrow()()`], +])("%s that passes the namespace as `this` keeps the package refused", (_, use) => { + const result = lower(`${helpers}var w_exports = {}; +__export(w_exports, { who: () => who, arrow: () => arrow }); +function who() { return this.__esModule; } +function arrow() { return () => this; } +var init_w = __esm({ "w.ts"() {} }); +console.log(${use.replaceAll("NS", "(init_w(), __toCommonJS(w_exports))")}); +`); + expect(result).toEqual({ + degrade: expect.stringContaining( + "calls only of exported functions that do not read 'this'", + ) as string, + }); +}); + test("minified initializers sharing a var statement lower the same way", () => { expectSameBehavior( helpers + diff --git a/packages/compiler/src/frontend/npm-static-bundled-esm.ts b/packages/compiler/src/frontend/npm-static-bundled-esm.ts index cf4f6280b..37d2d7b1d 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-esm.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-esm.ts @@ -185,6 +185,44 @@ function plainRead(access: ts.Expression): boolean { return true; } +/** True when a member read is the callee of a call or a tag, which receives + * the object it was read from as `this`. */ +function calledWithReceiver(access: ts.Expression): boolean { + let callee: ts.Node = access; + while (callee.parent !== undefined && ts.isParenthesizedExpression(callee.parent)) + callee = callee.parent; + const p = callee.parent; + return ( + p !== undefined && + ((ts.isCallExpression(p) && p.expression === callee) || + (ts.isTaggedTemplateExpression(p) && p.tag === callee)) + ); +} + +/** True when a function reads its own `this`: in its body, or in an arrow + * function inside it. */ +function readsThis(fn: ts.FunctionDeclaration): boolean { + let found = false; + if (fn.body === undefined) return false; + ts.walkPreorder(fn.body, (n) => { + if (found) return "stop"; + if ( + ts.isFunctionDeclaration(n) || + ts.isFunctionExpression(n) || + ts.isMethodDeclaration(n) || + ts.isGetAccessorDeclaration(n) || + ts.isSetAccessorDeclaration(n) || + ts.isConstructorDeclaration(n) || + ts.isClassDeclaration(n) || + ts.isClassExpression(n) + ) + return "skip"; + if (n.kind === ts.SyntaxKind.ThisKeyword) found = true; + return undefined; + }); + return found; +} + /** The `__export(T, { name: () => value, … })` getter table of T, or null. */ function exportTableOf( call: ts.CallExpression, @@ -457,7 +495,7 @@ export function lowerBundledEsmInits( p.parent === table.stmt ) continue; - const why = `reads the namespace of a bundled ES module ('${r.text}') in a way the static rewrite cannot follow (only member reads of its exports compile)`; + const why = `reads the namespace of a bundled ES module ('${r.text}') in a way the static rewrite cannot follow (only member reads of its exports compile, and calls only of exported functions that do not read 'this')`; if ( p === undefined || !ts.isCallExpression(p) || @@ -480,13 +518,25 @@ export function lowerBundledEsmInits( ns = up; else break; } - const names = new Set(table.entries.map(([n]) => n)); + const values = new Map(table.entries); + // A call through the namespace (`ns.fn()`) passes the object the read + // came from as `this`; the lowered object differs from Node's module + // object (`__esModule`, property attributes), so it must stay + // unobservable: the callee is a top-level function that never reads + // `this`. + const receiverHidden = (name: string): boolean => { + const value = values.get(name); + if (value === undefined || !ts.isIdentifier(value) || !stable(value.text)) return false; + const decl = top.get(value.text)?.node; + return decl !== undefined && ts.isFunctionDeclaration(decl) && !readsThis(decl); + }; const memberRead = (access: ts.Node | undefined, of: ts.Node): boolean => access !== undefined && ts.isPropertyAccessExpression(access) && access.expression === of && - names.has(access.name.text) && - plainRead(access); + values.has(access.name.text) && + plainRead(access) && + (!calledWithReceiver(access) || receiverHidden(access.name.text)); const holder = ns.parent; let readable = memberRead(holder, ns); if ( diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2567-esm-namespace-this/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2567-esm-namespace-this/main.ts.json new file mode 100644 index 000000000..0739a6f0c --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2567-esm-namespace-this/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2567-esm-namespace-this/main.ts" + ], + "diags": [] +} diff --git a/tests/fixtures/npm/cases/2567-esm-namespace-this/main.ts b/tests/fixtures/npm/cases/2567-esm-namespace-this/main.ts new file mode 100644 index 000000000..d6f084bc0 --- /dev/null +++ b/tests/fixtures/npm/cases/2567-esm-namespace-this/main.ts @@ -0,0 +1,7 @@ +// A lazily initialized ES module export that reads `this`, called through +// the module's namespace (`lazy.who()`): Node passes the namespace object, +// whose `__esModule` is true. A lowered namespace object would differ, so +// the package stays refused. +import { load } from "gtesmthis"; + +console.log(load()); diff --git a/tests/fixtures/npm/node_modules/gtesmthis/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtesmthis/dist/index.d.ts new file mode 100644 index 000000000..af4369397 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtesmthis/dist/index.d.ts @@ -0,0 +1,2 @@ +export declare function describe(): string; +export declare function load(): string; diff --git a/tests/fixtures/npm/node_modules/gtesmthis/dist/index.js b/tests/fixtures/npm/node_modules/gtesmthis/dist/index.js new file mode 100644 index 000000000..46b81aa7d --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtesmthis/dist/index.js @@ -0,0 +1,94 @@ +var __create = Object.create; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __getProtoOf = Object.getPrototypeOf; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __esm = (fn, res) => function __init() { + return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; +}; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( + // If the importer is in node compatibility mode or this is not an ESM + // file that has been converted to a CommonJS file using a Babel- + // compatible transform (i.e. "__esModule" has not been set), then set + // "default" to the CommonJS "module.exports" for node compatibility. + isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, + mod +)); +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); + +// src/helper.ts +function helper(x) { + COUNT++; + return "[" + x + "]"; +} +var COUNT; +var init_helper = __esm({ + "src/helper.ts"() { + console.log("helper init"); + COUNT = 0; + } +}); + +// src/lazy.ts +var lazy_exports = {}; +__export(lazy_exports, { + VALUE: () => VALUE, + value: () => value, + who: () => who +}); +function who() { + return [typeof this, this.__esModule, Object.getOwnPropertyNames(this).join(","), Object.keys(this).join(",")].join(" "); +} +function value() { + return VALUE + ":" + import_gtcore.default.tag + ":" + COUNT; +} +var path, import_gtcore, VALUE; +var init_lazy = __esm({ + "src/lazy.ts"() { + path = __toESM(require("path")); + import_gtcore = __toESM(require("gtcore")); + init_helper(); + console.log("lazy init"); + VALUE = helper(path.basename("/a/b.txt")); + } +}); + +// src/index.ts +var index_exports = {}; +__export(index_exports, { + describe: () => describe, + load: () => load +}); +module.exports = __toCommonJS(index_exports); + +// src/greet.ts +function greet(name) { + return "hello " + name; +} + +// src/index.ts +function describe() { + return greet("main"); +} +function load() { + const lazy = (init_lazy(), __toCommonJS(lazy_exports)); + return lazy.value() + " " + lazy.VALUE + " | " + lazy.who() + " | " + (init_lazy(), __toCommonJS(lazy_exports)).who(); +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + describe, + load +}); diff --git a/tests/fixtures/npm/node_modules/gtesmthis/package.json b/tests/fixtures/npm/node_modules/gtesmthis/package.json new file mode 100644 index 000000000..c6344fa8c --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtesmthis/package.json @@ -0,0 +1,10 @@ +{ + "name": "gtesmthis", + "version": "1.0.0", + "description": "gtesm's esbuild 0.25.10 output shape with a lazily initialized ES module export that reads `this`, called through the module's namespace", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "dependencies": { + "gtcore": "1.0.0" + } +} diff --git a/tests/harness/npm-cases.ts b/tests/harness/npm-cases.ts index f8d346154..35a9df4a6 100644 --- a/tests/harness/npm-cases.ts +++ b/tests/harness/npm-cases.ts @@ -19,12 +19,12 @@ export function npmCases(fixturesRoot: string): NpmCase[] { return [ ...globSync(join(fixturesRoot, "npm/cases/*/main.ts")) .sort() - // 2465-2469 and 2556-2563 are the --npm-static bundler-emitted-CJS + // 2465-2469, 2556-2563 and 2567 are the --npm-static bundler-emitted-CJS // cases (npm-static.test.ts drives them with the opt-in): their // consumers exercise the static rewrite's surface (lexer-visible // names the shipped .d.ts never declares, the __toESM interop // family), so they stay out of the flagless island lane by design. - .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-3])-[^/]+\/main\.ts$/.test(entry)) + .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-37])-[^/]+\/main\.ts$/.test(entry)) .map((entry) => ({ name: entry.split("/").at(-2)!, entry, diff --git a/tests/harness/npm-static.test.ts b/tests/harness/npm-static.test.ts index 8560973cd..443c0c72d 100644 --- a/tests/harness/npm-static.test.ts +++ b/tests/harness/npm-static.test.ts @@ -76,7 +76,7 @@ async function buildStatic(entry: string, npmStatic: string[] | "auto"): Promise ...globSync( join(fixturesRoot, "npm/node_modules/cryptozoo/**/*.{js,mjs,cjs,json,d.ts}"), ).sort(), - // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2563) + // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2563, 2567) ...globSync(join(fixturesRoot, "npm/node_modules/gt*/**/*.{js,cjs,json}")).sort(), ]; for (const f of inputs) hash.update(f).update(readFileSync(f)); @@ -1478,6 +1478,21 @@ describe(`npm-static pilots${sanitize ? " (sanitized)" : ""}`, () => { ]); }, 180_000); + // 2567: an export that reads `this`, called through its namespace, sees + // Node's module object; the lowered namespace would differ, so the + // package stays refused. + test("a namespace call to an export that reads `this` stays refused", () => { + const entry = join(fixturesRoot, "npm/cases/2567-esm-namespace-this/main.ts"); + const { coverage } = analyze(entry, { npmStatic: ["gtesmthis", "gtcore"] }); + expect(coverage.npmStatic).toContainEqual({ + package: "gtesmthis", + status: "refused", + detail: expect.stringMatching( + /calls only of exported functions that do not read 'this'/, + ) as string, + }); + }, 120_000); + // 2562: a file with unverifiable interop that no compiled code can load // (the program imports a leaf data module and only a type from the // main entry) leaves its package static. From 1c3a405277793908948e99fe1c70720ac18ab1ec Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 17:31:49 -0700 Subject: [PATCH 37/83] Expect the lazy-loader refusal for a namespace kept by a loader Co-Authored-By: Claude Opus 5.5 --- packages/compiler/src/frontend/npm-static-bundled-esm.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts b/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts index 0b9ecd042..0a144036c 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts @@ -127,7 +127,7 @@ test.each([ `var init_a = __esm({ "a.ts"() { console.log(this); } });\ninit_a();`, ], [ - "stores the namespace of a bundled ES module ('a_exports') in a variable", + "memoized lazy loader 'load' returns the namespace of a bundled ES module as a value", `var a_exports = {};\n__export(a_exports, { v: () => v });\nvar v, init_a = __esm({ "a.ts"() { v = 1; } });\nvar ns, load = () => ns ??= __toESM((init_a(), __toCommonJS(a_exports)));\nload();`, ], [ From 5670bc182e53e2c0544c1038b12b0e5db01d5d9d Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 18:21:52 -0700 Subject: [PATCH 38/83] Respell members of a function export object; name conditional exports A CommonJS module whose export object is one of its own top-level functions (semver 5/6's `exports = module.exports = SemVer`) reaches its members through that function after the statement: member reads and writes through module.exports (and, after the rebinding, every `exports` reference) become members of the function, the expando form both resolvers model, and the rebinding itself drops. Constructor functions exported whole with members attached afterwards no longer lose those members at runtime. Replacing module.exports or rebinding exports inside a branch (an environment-dependent export object, readable-stream 2.x) now names the construct instead of the generic assignment fence, and a required builtin module object is truthy in the LLVM backend. Co-Authored-By: Claude Opus 5.5 --- packages/compiler/src/backend/llvm/emitter.ts | 3 + .../src/frontend/lowering/lower-stmts.ts | 20 ++ .../frontend/npm-static-cjs-exports-alias.ts | 179 ++++++++++++++++++ packages/compiler/src/frontend/npm-static.ts | 6 +- packages/compiler/src/frontend/services.ts | 8 + .../6264-required-module-truthiness.cjs | 10 + .../cases/2567-cjs-function-exports/main.ts | 12 ++ .../2568-cjs-conditional-exports/main.ts | 7 + .../npm/node_modules/gtcondexports/index.d.ts | 2 + .../npm/node_modules/gtcondexports/index.js | 14 ++ .../node_modules/gtcondexports/package.json | 7 + .../npm/node_modules/gtfnexports/index.d.ts | 13 ++ .../npm/node_modules/gtfnexports/index.js | 25 +++ .../npm/node_modules/gtfnexports/package.json | 7 + .../npm/node_modules/gtfnmembers/index.d.ts | 11 ++ .../npm/node_modules/gtfnmembers/index.js | 13 ++ .../npm/node_modules/gtfnmembers/package.json | 7 + tests/harness/npm-cases.ts | 4 +- tests/harness/npm-static.test.ts | 28 ++- 19 files changed, 372 insertions(+), 4 deletions(-) create mode 100644 packages/compiler/src/frontend/npm-static-cjs-exports-alias.ts create mode 100644 tests/corpus/6264-required-module-truthiness.cjs create mode 100644 tests/fixtures/npm/cases/2567-cjs-function-exports/main.ts create mode 100644 tests/fixtures/npm/cases/2568-cjs-conditional-exports/main.ts create mode 100644 tests/fixtures/npm/node_modules/gtcondexports/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtcondexports/index.js create mode 100644 tests/fixtures/npm/node_modules/gtcondexports/package.json create mode 100644 tests/fixtures/npm/node_modules/gtfnexports/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtfnexports/index.js create mode 100644 tests/fixtures/npm/node_modules/gtfnexports/package.json create mode 100644 tests/fixtures/npm/node_modules/gtfnmembers/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtfnmembers/index.js create mode 100644 tests/fixtures/npm/node_modules/gtfnmembers/package.json diff --git a/packages/compiler/src/backend/llvm/emitter.ts b/packages/compiler/src/backend/llvm/emitter.ts index 9e2d3524e..f20c63b1d 100644 --- a/packages/compiler/src/backend/llvm/emitter.ts +++ b/packages/compiler/src/backend/llvm/emitter.ts @@ -5882,6 +5882,9 @@ export class LlEmitter { // for stdin. They remain JavaScript objects in boolean contexts. case "procStream": return "true"; + // A module namespace (a required builtin module object) is an object. + case "moduleNs": + return "true"; case "array": case "record": case "object": diff --git a/packages/compiler/src/frontend/lowering/lower-stmts.ts b/packages/compiler/src/frontend/lowering/lower-stmts.ts index 36abb9130..4a6fb15a3 100644 --- a/packages/compiler/src/frontend/lowering/lower-stmts.ts +++ b/packages/compiler/src/frontend/lowering/lower-stmts.ts @@ -30,6 +30,7 @@ import { import { cjsExportMemberWriteGlobalOf, isCjsExportsIdentifier, + isCjsModuleExports, isFreshExportsReset, } from "./cjs-exports-object.js"; import { isDeadTscHelperDecl, tscInteropBindingOfDecl } from "./cjs-tsc-interop.js"; @@ -6627,6 +6628,25 @@ export function lowerExprStatement(lowerer: Lowerer, expr: ts.Expression): IrStm "require() outside the module's top level (move it to the top of the file)", ); } + // Replacing the export object (or rebinding `exports`) anywhere but a + // top-level statement makes the module's exports depend on which path + // ran (`cond ? module.exports = a : module.exports = b`, an + // environment-dependent branch); the static export table has one shape. + if ( + !topLevelJs && + isJsSourceFile(expr.getSourceFile()) && + ts.isBinaryExpression(expr) && + expr.operatorToken.kind === ts.SyntaxKind.EqualsToken && + (isCjsModuleExports(lowerer, expr.left) || isCjsExportsIdentifier(lowerer, expr.left)) + ) { + lowerer.unsupported( + "SC1090", + expr.left, + isCjsModuleExports(lowerer, expr.left) + ? "replacing module.exports outside a top-level statement (a conditional or environment-dependent export object)" + : "rebinding 'exports' outside a top-level statement (a conditional or environment-dependent export object)", + ); + } if (topLevelJs && ts.isExpressionStatement(stmtNode)) { if (isFreshExportsReset(lowerer, stmtNode)) return { kind: "block", body: [], loc: locOf(expr) }; diff --git a/packages/compiler/src/frontend/npm-static-cjs-exports-alias.ts b/packages/compiler/src/frontend/npm-static-cjs-exports-alias.ts new file mode 100644 index 000000000..c88c9949e --- /dev/null +++ b/packages/compiler/src/frontend/npm-static-cjs-exports-alias.ts @@ -0,0 +1,179 @@ +/* CommonJS modules whose export object is one of their own functions: + * + * exports = module.exports = SemVer; // or module.exports = SemVer; + * exports.parse = parse; // or module.exports.parse = … + * function SemVer(version) { … } + * + * After that statement `module.exports` (and, after the rebinding form, + * `exports`) IS the function, so every member read or write through them + * is a member of the function itself. The pass respells those accesses + * through the function's name, the expando form both resolvers model + * (members on a function declaration), and drops the `exports =` + * rebinding the static export protocol has no storage for. + * + * Exact only while nothing can observe the module before the statement + * runs and nothing replaces the export object again: the statement is a + * top-level statement preceded only by inert declarations, it is the + * file's only write of `module.exports` and `exports`, the function is a + * top-level declaration nothing reassigns, and `exports`/`module` are the + * CommonJS globals (never declared in the file). Other shapes answer + * null and keep their existing treatment. */ +import * as ts from "./ts7/syntax.js"; +import { + applyEdits, + declaredNames, + isWritten, + references, + unparen, + type Edit, +} from "./npm-static-bundled-chunks.js"; + +function isModuleExports(e: ts.Expression): e is ts.PropertyAccessExpression { + const x = unparen(e); + return ( + ts.isPropertyAccessExpression(x) && + !x.questionDotToken && + ts.isIdentifier(x.expression) && + x.expression.text === "module" && + x.name.text === "exports" + ); +} + +/** True for a statement whose evaluation runs no code that could observe + * the export object: declarations and member writes (`F.prototype.m = + * function …`) without calls, and directives. */ +function inert(stmt: ts.Statement): boolean { + if (ts.isFunctionDeclaration(stmt) || ts.isEmptyStatement(stmt)) return true; + if (ts.isExpressionStatement(stmt) && ts.isStringLiteral(stmt.expression)) return true; + if (ts.isExpressionStatement(stmt)) { + const e = unparen(stmt.expression); + if ( + !ts.isBinaryExpression(e) || + e.operatorToken.kind !== ts.SyntaxKind.EqualsToken || + !(ts.isPropertyAccessExpression(e.left) || ts.isElementAccessExpression(e.left)) + ) + return false; + let root: ts.Expression = e.left; + while (ts.isPropertyAccessExpression(root) || ts.isElementAccessExpression(root)) + root = root.expression; + if (!ts.isIdentifier(root) || root.text === "module" || root.text === "exports") return false; + } else if (!ts.isVariableStatement(stmt)) return false; + let calls = false; + ts.walkPreorder(stmt, (n) => { + if (calls) return "stop"; + if (ts.isArrowFunction(n) || ts.isFunctionExpression(n)) return "skip"; + if (ts.isCallExpression(n) || ts.isNewExpression(n) || ts.isTaggedTemplateExpression(n)) + calls = true; + return undefined; + }); + return !calls; +} + +export function normalizeCjsFunctionExports(sf: ts.SourceFile): string | null { + const source = sf.text; + if (!source.includes("module.exports")) return null; + if (!sf.fileName.endsWith(".cjs") && ts.isExternalModule(sf)) return null; + const declared = declaredNames(sf); + if (declared.has("exports") || declared.has("module")) return null; + + // The whole-export statement: `[exports =] module.exports = W;`. + let whole: { stmt: ts.ExpressionStatement; fn: string; rebinds: boolean } | null = null; + const stmts = sf.statements.nodes; + for (let i = 0; i < stmts.length; i++) { + const stmt = stmts[i]!; + if (!ts.isExpressionStatement(stmt)) continue; + const top = unparen(stmt.expression); + if (!ts.isBinaryExpression(top) || top.operatorToken.kind !== ts.SyntaxKind.EqualsToken) + continue; + let e: ts.BinaryExpression = top; + let rebinds = false; + if (ts.isIdentifier(top.left) && top.left.text === "exports") { + const inner = unparen(top.right); + if (!ts.isBinaryExpression(inner) || inner.operatorToken.kind !== ts.SyntaxKind.EqualsToken) + continue; + rebinds = true; + e = inner; + } + if (!isModuleExports(e.left)) continue; + const value = unparen(e.right); + if (!ts.isIdentifier(value)) return null; + if (whole !== null || !stmts.slice(0, i).every(inert)) return null; + whole = { stmt, fn: value.text, rebinds }; + } + if (whole === null) return null; + const fnName = whole.fn; + const fnDecls = stmts.filter( + (st) => ts.isFunctionDeclaration(st) && st.name?.text === fnName && st.body !== undefined, + ); + if (fnDecls.length !== 1) return null; + + const edits: Edit[] = []; + let unsafe = false; + const refs = references(sf); + for (const r of refs) { + if (r.text === fnName && isWritten(r)) unsafe = true; + if (r.text === "exports") { + if (isWritten(r)) { + // Only the whole statement's own rebinding. + const assign = r.parent; + if ( + !whole.rebinds || + assign === undefined || + !ts.isBinaryExpression(assign) || + assign.parent !== whole.stmt || + assign.left !== r + ) + unsafe = true; + continue; + } + // Without the rebinding, `exports` keeps the replaced object. + if (!whole.rebinds || r.getStart(sf) < whole.stmt.getEnd()) { + unsafe = true; + continue; + } + edits.push({ start: r.getStart(sf), end: r.getEnd(), text: fnName }); + continue; + } + if (r.text === "module") { + const access = r.parent; + if (access === undefined || !isModuleExports(access as ts.Expression)) continue; + const outer = access.parent; + if (outer !== undefined && ts.isBinaryExpression(outer) && outer.left === access) { + if ( + outer.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && + outer.operatorToken.kind <= ts.SyntaxKind.LastAssignment && + !( + (outer.parent === whole.stmt && !whole.rebinds) || + (whole.rebinds && outer.parent?.parent === whole.stmt) + ) + ) + unsafe = true; + continue; + } + // A member access through module.exports: a member of the function. + if ( + outer !== undefined && + (ts.isPropertyAccessExpression(outer) || ts.isElementAccessExpression(outer)) && + outer.expression === access + ) { + if (access.getStart(sf) < whole.stmt.getEnd() && !insideFunction(access)) unsafe = true; + edits.push({ start: access.getStart(sf), end: access.getEnd(), text: fnName }); + } + } + } + if (unsafe) return null; + if (whole.rebinds) { + // `exports = module.exports = W` → `module.exports = W`. + const e = unparen(whole.stmt.expression) as ts.BinaryExpression; + edits.push({ start: e.getStart(sf), end: unparen(e.right).getStart(sf), text: "" }); + } + if (edits.length === 0) return null; + return applyEdits(source, 0, source.length, edits); +} + +function insideFunction(node: ts.Node): boolean { + for (let p = node.parent; p !== undefined && !ts.isSourceFile(p); p = p.parent) { + if (ts.isFunctionLike(p)) return true; + } + return false; +} diff --git a/packages/compiler/src/frontend/npm-static.ts b/packages/compiler/src/frontend/npm-static.ts index e00122238..182c5b62b 100644 --- a/packages/compiler/src/frontend/npm-static.ts +++ b/packages/compiler/src/frontend/npm-static.ts @@ -448,7 +448,11 @@ export function npmStaticFsShadow(services: FrontendServices): NpmStaticFsShadow } else if (esm !== null) { original = esm; } - const respelled = split !== null || esm !== null; + // A module exporting one of its functions reaches its members + // through that function. + const fnExports = original === null ? null : services.cjsFunctionExports(original, path); + if (fnExports !== null) original = fnExports; + const respelled = split !== null || esm !== null || fnExports !== null; if (original !== null) { const namepaths = services.jsDocNamepaths(path, original); const source = namepaths ?? original; diff --git a/packages/compiler/src/frontend/services.ts b/packages/compiler/src/frontend/services.ts index c5dcbf513..2e3d686ab 100644 --- a/packages/compiler/src/frontend/services.ts +++ b/packages/compiler/src/frontend/services.ts @@ -2,6 +2,7 @@ import { Ts7SourceParser, type Ts7SourceKind } from "./ts7/source-parser.js"; import { Ts7Host, type Ts7ApiFactory, type Ts7HostOptions } from "./ts7/program-host.js"; import { lowerEsmInitsWithParser, rewriteBundlerCjsWithParser } from "./npm-static-rewrite-host.js"; import { isBundlerCjsCandidate } from "./npm-static-rewrite-syntax.js"; +import { normalizeCjsFunctionExports } from "./npm-static-cjs-exports-alias.js"; import { mayBundleChunks, splitBundledChunks, @@ -91,6 +92,13 @@ export class FrontendServices { if (!source.includes("__esm")) return null; return lowerEsmInitsWithParser(this.sourceParser(), source, path); } + /** A CommonJS module whose export object is one of its functions, with + * its member accesses respelled through that function (null: none). */ + cjsFunctionExports(source: string, path: string): string | null { + this.ensureOpen(); + if (!source.includes("module.exports")) return null; + return normalizeCjsFunctionExports(this.parse(path, source, "js")); + } nullableClassFields(path: string, source: string): syntax.NpmStaticOverloadRewrite | null { return syntax.applyNpmStaticNullableClassFields(this.parse(path, source, "js"), source); } diff --git a/tests/corpus/6264-required-module-truthiness.cjs b/tests/corpus/6264-required-module-truthiness.cjs new file mode 100644 index 000000000..626d1cd4d --- /dev/null +++ b/tests/corpus/6264-required-module-truthiness.cjs @@ -0,0 +1,10 @@ +// A required builtin module object in a boolean context is truthy, as any +// object: readable-stream's `process.env.READABLE_STREAM === "disable" && +// Stream` and `Stream || exports` guards. +"use strict"; +var Stream = require("stream"); +var os = require("os"); +console.log(Stream ? "stream" : "none", !!os, !os); +console.log(process.env.SCRIPTC_UNSET_FLAG === "disable" && Stream ? "disabled" : "default"); +var fallback = Stream || null; +console.log(fallback === Stream); diff --git a/tests/fixtures/npm/cases/2567-cjs-function-exports/main.ts b/tests/fixtures/npm/cases/2567-cjs-function-exports/main.ts new file mode 100644 index 000000000..29e65cbfc --- /dev/null +++ b/tests/fixtures/npm/cases/2567-cjs-function-exports/main.ts @@ -0,0 +1,12 @@ +// CommonJS modules whose export object is one of their own functions: +// semver 5/6's `exports = module.exports = Version` with members attached +// through both `exports` and `module.exports` (and read back through +// `exports` inside the module), and a constructor function exported whole +// with function and value members attached afterwards. +import Version from "gtfnexports"; +import Point from "gtfnmembers"; + +const v = Version.parse("2.4.1"); +console.log(v === null ? "null" : v.major(), String(new Version("3.0.0"))); +console.log(Version.compare("5.0.0", "3.2.0"), Version.gt("1.0.0", "2.0.0"), Version.MAX_LENGTH); +console.log(Point.origin().sum(), new Point(2, 3).sum(), Point.dimensions); diff --git a/tests/fixtures/npm/cases/2568-cjs-conditional-exports/main.ts b/tests/fixtures/npm/cases/2568-cjs-conditional-exports/main.ts new file mode 100644 index 000000000..2c117e914 --- /dev/null +++ b/tests/fixtures/npm/cases/2568-cjs-conditional-exports/main.ts @@ -0,0 +1,7 @@ +// An environment-dependent export object (readable-stream 2.x's +// READABLE_STREAM=disable switch): module.exports is replaced inside an +// if/else, so the module's exports depend on which branch ran. scriptc +// names the construct; Node picks the branch at load. +import rs from "gtcondexports"; + +console.log(rs.Readable.name); diff --git a/tests/fixtures/npm/node_modules/gtcondexports/index.d.ts b/tests/fixtures/npm/node_modules/gtcondexports/index.d.ts new file mode 100644 index 000000000..ba94e150b --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcondexports/index.d.ts @@ -0,0 +1,2 @@ +declare const exported: { Readable: Function; Stream: unknown; name: string }; +export = exported; diff --git a/tests/fixtures/npm/node_modules/gtcondexports/index.js b/tests/fixtures/npm/node_modules/gtcondexports/index.js new file mode 100644 index 000000000..b4017d055 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcondexports/index.js @@ -0,0 +1,14 @@ +var Stream = require("stream"); +function Local(name) { + this.name = name; +} +if (process.env.GT_STREAMS === "native" && Stream) { + module.exports = Stream; + exports = module.exports = Stream.Readable; + exports.Readable = Stream.Readable; + exports.Stream = Stream; +} else { + exports = module.exports = Local; + exports.Stream = Stream || exports; + exports.Readable = exports; +} diff --git a/tests/fixtures/npm/node_modules/gtcondexports/package.json b/tests/fixtures/npm/node_modules/gtcondexports/package.json new file mode 100644 index 000000000..eab4647ed --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcondexports/package.json @@ -0,0 +1,7 @@ +{ + "name": "gtcondexports", + "version": "1.0.0", + "description": "readable-stream 2.x's environment-dependent export object: module.exports is replaced (and exports rebound) inside an if/else on an environment variable", + "main": "index.js", + "types": "index.d.ts" +} diff --git a/tests/fixtures/npm/node_modules/gtfnexports/index.d.ts b/tests/fixtures/npm/node_modules/gtfnexports/index.d.ts new file mode 100644 index 000000000..c2ee082d3 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtfnexports/index.d.ts @@ -0,0 +1,13 @@ +declare class Version { + constructor(raw: string); + raw: string; + major(): number; + toString(): string; +} +declare namespace Version { + const MAX_LENGTH: number; + function parse(raw: string): Version | null; + function compare(a: string, b: string): number; + function gt(a: string, b: string): boolean; +} +export = Version; diff --git a/tests/fixtures/npm/node_modules/gtfnexports/index.js b/tests/fixtures/npm/node_modules/gtfnexports/index.js new file mode 100644 index 000000000..d64b58f0c --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtfnexports/index.js @@ -0,0 +1,25 @@ +exports = module.exports = Version; + +var label = process.env.GT_LABEL ? "[" + process.env.GT_LABEL + "]" : ""; + +function Version(raw) { + this.raw = raw; + this.parts = raw.split("."); +} +Version.prototype.major = function () { + return Number(this.parts[0]); +}; +Version.prototype.toString = function () { + return label + this.raw; +}; + +exports.MAX_LENGTH = 256; +exports.parse = function parse(raw) { + return raw.length > exports.MAX_LENGTH ? null : new Version(raw); +}; +exports.compare = function compare(a, b) { + return exports.parse(a).major() - exports.parse(b).major(); +}; +module.exports.gt = function gt(a, b) { + return exports.compare(a, b) > 0; +}; diff --git a/tests/fixtures/npm/node_modules/gtfnexports/package.json b/tests/fixtures/npm/node_modules/gtfnexports/package.json new file mode 100644 index 000000000..4524cd1bc --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtfnexports/package.json @@ -0,0 +1,7 @@ +{ + "name": "gtfnexports", + "version": "1.0.0", + "description": "semver 5/6's export idiom: exports = module.exports = Version, then members attached through exports and module.exports", + "main": "index.js", + "types": "index.d.ts" +} diff --git a/tests/fixtures/npm/node_modules/gtfnmembers/index.d.ts b/tests/fixtures/npm/node_modules/gtfnmembers/index.d.ts new file mode 100644 index 000000000..ea1888e0b --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtfnmembers/index.d.ts @@ -0,0 +1,11 @@ +declare class Point { + constructor(x: number, y: number); + x: number; + y: number; + sum(): number; +} +declare namespace Point { + function origin(): Point; + const dimensions: number; +} +export = Point; diff --git a/tests/fixtures/npm/node_modules/gtfnmembers/index.js b/tests/fixtures/npm/node_modules/gtfnmembers/index.js new file mode 100644 index 000000000..6eaf003f7 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtfnmembers/index.js @@ -0,0 +1,13 @@ +"use strict"; +function Point(x, y) { + this.x = x; + this.y = y; +} +Point.prototype.sum = function () { + return this.x + this.y; +}; +module.exports = Point; +module.exports.origin = function origin() { + return new Point(0, 0); +}; +module.exports.dimensions = 2; diff --git a/tests/fixtures/npm/node_modules/gtfnmembers/package.json b/tests/fixtures/npm/node_modules/gtfnmembers/package.json new file mode 100644 index 000000000..5179c6caf --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtfnmembers/package.json @@ -0,0 +1,7 @@ +{ + "name": "gtfnmembers", + "version": "1.0.0", + "description": "a constructor function as module.exports with function and value members attached through module.exports afterwards", + "main": "index.js", + "types": "index.d.ts" +} diff --git a/tests/harness/npm-cases.ts b/tests/harness/npm-cases.ts index 69ce3917f..e0f1384bb 100644 --- a/tests/harness/npm-cases.ts +++ b/tests/harness/npm-cases.ts @@ -19,12 +19,12 @@ export function npmCases(fixturesRoot: string): NpmCase[] { return [ ...globSync(join(fixturesRoot, "npm/cases/*/main.ts")) .sort() - // 2465-2469 and 2556-2566 are the --npm-static bundler-emitted-CJS + // 2465-2469 and 2556-2568 are the --npm-static bundler-emitted-CJS // cases (npm-static.test.ts drives them with the opt-in): their // consumers exercise the static rewrite's surface (lexer-visible // names the shipped .d.ts never declares, the __toESM interop // family), so they stay out of the flagless island lane by design. - .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-6])-[^/]+\/main\.ts$/.test(entry)) + .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-8])-[^/]+\/main\.ts$/.test(entry)) .map((entry) => ({ name: entry.split("/").at(-2)!, entry, diff --git a/tests/harness/npm-static.test.ts b/tests/harness/npm-static.test.ts index 3543aeb0a..02ddadb0d 100644 --- a/tests/harness/npm-static.test.ts +++ b/tests/harness/npm-static.test.ts @@ -76,7 +76,7 @@ async function buildStatic(entry: string, npmStatic: string[] | "auto"): Promise ...globSync( join(fixturesRoot, "npm/node_modules/cryptozoo/**/*.{js,mjs,cjs,json,d.ts}"), ).sort(), - // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2566) + // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2568) ...globSync(join(fixturesRoot, "npm/node_modules/gt*/**/*.{js,cjs,json}")).sort(), ...globSync(join(fixturesRoot, "npm/store/gt*/**/*.{js,cjs,json}")).sort(), ]; @@ -1514,6 +1514,32 @@ describe(`npm-static pilots${sanitize ? " (sanitized)" : ""}`, () => { expect(nativeRes.exitCode).toBe(nodeRes.exitCode); }, 180_000); + // 2567: a module whose export object is one of its functions (semver + // 5/6's `exports = module.exports = Version`), under both values of the + // environment variable the module reads. + test("function export objects 2567-cjs-function-exports compile statically and byte-match Node", async () => { + const entry = join(fixturesRoot, "npm/cases/2567-cjs-function-exports/main.ts"); + await expectNodeParity(entry, ["gtfnexports", "gtfnmembers"]); + const binary = await buildStatic(entry, ["gtfnexports", "gtfnmembers"]); + const env = { ...process.env, GT_LABEL: "x" }; + const [nodeRes, nativeRes] = await Promise.all([ + runBinary("node", [entry], env), + runBinary(binary, [], env), + ]); + expect(nativeRes.stdout.toString("utf8")).toBe(nodeRes.stdout.toString("utf8")); + expect(nativeRes.stdout.toString("utf8")).toContain("[x]"); + }, 180_000); + + // 2568: an export object replaced inside an environment-dependent branch + // (readable-stream 2.x) is named precisely. + test("an environment-dependent export object is refused by name", () => { + const entry = join(fixturesRoot, "npm/cases/2568-cjs-conditional-exports/main.ts"); + const { coverage } = analyze(entry, { npmStatic: ["gtcondexports"] }); + expect(JSON.stringify(coverage)).toContain( + "replacing module.exports outside a top-level statement (a conditional or environment-dependent export object)", + ); + }, 120_000); + // 2562: a file with unverifiable interop that no compiled code can load // (the program imports a leaf data module and only a type from the // main entry) leaves its package static. From 3723e05417ec5229afc23cb861da81251e26586c Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 18:28:45 -0700 Subject: [PATCH 39/83] Resolve bundled chunks that re-export a builtin to the builtin A chunk whose whole body is `module.exports = require("")` (readable-stream's internal/streams/stream.js) is the builtin module: its calls become that require, and no chunk module is emitted. Co-Authored-By: Claude Opus 5.5 --- .../npm-static-bundled-chunks.test.ts | 18 ++++++++ .../src/frontend/npm-static-bundled-chunks.ts | 44 ++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts index 82926d2b8..7ccad33f0 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts @@ -182,3 +182,21 @@ export const g = import_g.g; degrade: expect.stringContaining("bundled CommonJS modules") as string, }); }); + +test("a chunk that only re-exports a builtin module becomes that module", () => { + const answer = expectSameBehavior(`${keyedHelper}var require_stream = __commonJS({ + "lib/stream.js"(exports2, module2) { + module2.exports = require("stream"); + } +}); +var require_user = __commonJS({ + "lib/user.js"(exports2) { + var Stream = require_stream(); + exports2.same = Stream === require("stream"); + } +}); +console.log(typeof require_stream().PassThrough, require_user().same); +`); + expect(answer.chunks.map((c) => basename(c.path))).toEqual(["index.js.require_user.cjs"]); + expect(answer.text).toContain('typeof require("stream").PassThrough'); +}); diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts index 28fe4a1b9..03b639cbe 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts @@ -23,6 +23,7 @@ * beside it). Anything else answers a degrade reason. */ import * as ts from "./ts7/syntax.js"; import { helperTokens } from "./helper-tokens.js"; +import { canonicalBuiltinModule } from "./builtin-modules.js"; export interface BundledChunkSplit { /** The bundle with chunk declarations blanked and calls respelled. */ @@ -407,10 +408,11 @@ export function splitBundledChunks( return { degrade: `its bundled CommonJS module factory '${r.text}' is used other than as a direct call`, }; + const forwarded = forwarderSpec(chunk); callEdits.push({ start: call.getStart(sf), end: call.getEnd(), - text: `require(${JSON.stringify(chunk.spec)})`, + text: `require(${JSON.stringify(forwarded ?? chunk.spec)})`, }); } for (const name of chunks.keys()) { @@ -422,6 +424,11 @@ export function splitBundledChunks( const out: BundledChunkSplit["chunks"] = []; const bundleEdits: Edit[] = [...callEdits]; for (const chunk of chunks.values()) { + if (forwarderSpec(chunk) !== null) { + // A chunk that only re-exports a builtin module is that module. + bundleEdits.push({ start: chunk.stmt.getStart(sf), end: chunk.stmt.getEnd(), text: "" }); + continue; + } const result = chunkSource(sf, chunk, chunks, top, stable, callEdits, bundleStrict); if (typeof result !== "string") return result; out.push({ path: chunk.path, source: result }); @@ -472,6 +479,41 @@ export function applyEdits(source: string, from: number, to: number, edits: Edit return text + source.slice(at, to); } +/** The builtin module a chunk forwards, or null: a body that is exactly + * `module.exports = require("")` (readable-stream's + * internal/streams/stream.js). Requiring the chunk is then requiring the + * builtin itself (same object, and loading a builtin runs no code). */ +function forwarderSpec(chunk: Chunk): string | null { + const moduleParam = chunk.params[1]; + if (moduleParam === undefined) return null; + const stmts = chunk.body.statements.nodes.filter( + (st) => !(ts.isExpressionStatement(st) && ts.isStringLiteral(st.expression)), + ); + const only = stmts[0]; + if (stmts.length !== 1 || only === undefined || !ts.isExpressionStatement(only)) return null; + const e = unparen(only.expression); + if ( + !ts.isBinaryExpression(e) || + e.operatorToken.kind !== ts.SyntaxKind.EqualsToken || + !ts.isPropertyAccessExpression(e.left) || + !ts.isIdentifier(e.left.expression) || + e.left.expression.text !== moduleParam || + e.left.name.text !== "exports" + ) + return null; + const call = unparen(e.right); + if ( + !ts.isCallExpression(call) || + !ts.isIdentifier(call.expression) || + call.expression.text !== "require" || + call.arguments.nodes.length !== 1 || + !ts.isStringLiteral(call.arguments.nodes[0]!) + ) + return null; + const spec = (call.arguments.nodes[0] as ts.StringLiteral).text; + return canonicalBuiltinModule(spec) !== null ? spec : null; +} + /** How code in a factory body (a chunk or a lazy ES-module initializer) * observes the factory's own frame, or null: its receiver, its arguments * object, `super`, a meta property, or (when `returns`) a top-level From 2b7c892af3645f871e55aaecd590d42395ca2185 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 18:29:54 -0700 Subject: [PATCH 40/83] Record order baselines for the new cases and corpus program Co-Authored-By: Claude Opus 5.5 --- .../tests/corpus/6264-required-module-truthiness.cjs.json | 6 ++++++ .../npm/cases/2567-cjs-function-exports/main.ts.json | 6 ++++++ .../npm/cases/2568-cjs-conditional-exports/main.ts.json | 6 ++++++ 3 files changed, 18 insertions(+) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6264-required-module-truthiness.cjs.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2567-cjs-function-exports/main.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2568-cjs-conditional-exports/main.ts.json diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6264-required-module-truthiness.cjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6264-required-module-truthiness.cjs.json new file mode 100644 index 000000000..2fa691953 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6264-required-module-truthiness.cjs.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6264-required-module-truthiness.cjs" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2567-cjs-function-exports/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2567-cjs-function-exports/main.ts.json new file mode 100644 index 000000000..63a9b7efc --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2567-cjs-function-exports/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2567-cjs-function-exports/main.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2568-cjs-conditional-exports/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2568-cjs-conditional-exports/main.ts.json new file mode 100644 index 000000000..9cad58f88 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2568-cjs-conditional-exports/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2568-cjs-conditional-exports/main.ts" + ], + "diags": [] +} From fa012f5da3ecc2fd5e818164678bdea271cb0e0f Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 19:51:26 -0700 Subject: [PATCH 41/83] Name the corpus programs instead of numbering them The numbers collided with other open changes. Co-Authored-By: Claude Opus 5.5 --- .../tests/corpus/6260-local-class-detached-method.ts.json | 6 ------ .../tests/corpus/6262-comma-null-union-return.cjs.json | 6 ------ .../tests/corpus/6263-strict-arguments-parameters.cjs.json | 6 ------ .../tests/corpus/comma-null-union-return.cjs.json | 6 ++++++ .../tests/corpus/local-class-detached-method.ts.json | 6 ++++++ .../tests/corpus/strict-arguments-parameters.cjs.json | 6 ++++++ ...ma-null-union-return.cjs => comma-null-union-return.cjs} | 0 ...ss-detached-method.ts => local-class-detached-method.ts} | 0 ...ments-parameters.cjs => strict-arguments-parameters.cjs} | 0 9 files changed, 18 insertions(+), 18 deletions(-) delete mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6260-local-class-detached-method.ts.json delete mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6262-comma-null-union-return.cjs.json delete mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6263-strict-arguments-parameters.cjs.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/comma-null-union-return.cjs.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/local-class-detached-method.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/strict-arguments-parameters.cjs.json rename tests/corpus/{6262-comma-null-union-return.cjs => comma-null-union-return.cjs} (100%) rename tests/corpus/{6260-local-class-detached-method.ts => local-class-detached-method.ts} (100%) rename tests/corpus/{6263-strict-arguments-parameters.cjs => strict-arguments-parameters.cjs} (100%) diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6260-local-class-detached-method.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6260-local-class-detached-method.ts.json deleted file mode 100644 index d73c701d7..000000000 --- a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6260-local-class-detached-method.ts.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "order": [ - "/tests/corpus/6260-local-class-detached-method.ts" - ], - "diags": [] -} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6262-comma-null-union-return.cjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6262-comma-null-union-return.cjs.json deleted file mode 100644 index e167cc977..000000000 --- a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6262-comma-null-union-return.cjs.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "order": [ - "/tests/corpus/6262-comma-null-union-return.cjs" - ], - "diags": [] -} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6263-strict-arguments-parameters.cjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6263-strict-arguments-parameters.cjs.json deleted file mode 100644 index b7ff6a960..000000000 --- a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6263-strict-arguments-parameters.cjs.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "order": [ - "/tests/corpus/6263-strict-arguments-parameters.cjs" - ], - "diags": [] -} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/comma-null-union-return.cjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/comma-null-union-return.cjs.json new file mode 100644 index 000000000..1fad3c559 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/comma-null-union-return.cjs.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/comma-null-union-return.cjs" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/local-class-detached-method.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/local-class-detached-method.ts.json new file mode 100644 index 000000000..6d311fce9 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/local-class-detached-method.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/local-class-detached-method.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/strict-arguments-parameters.cjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/strict-arguments-parameters.cjs.json new file mode 100644 index 000000000..8c9cc9279 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/strict-arguments-parameters.cjs.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/strict-arguments-parameters.cjs" + ], + "diags": [] +} diff --git a/tests/corpus/6262-comma-null-union-return.cjs b/tests/corpus/comma-null-union-return.cjs similarity index 100% rename from tests/corpus/6262-comma-null-union-return.cjs rename to tests/corpus/comma-null-union-return.cjs diff --git a/tests/corpus/6260-local-class-detached-method.ts b/tests/corpus/local-class-detached-method.ts similarity index 100% rename from tests/corpus/6260-local-class-detached-method.ts rename to tests/corpus/local-class-detached-method.ts diff --git a/tests/corpus/6263-strict-arguments-parameters.cjs b/tests/corpus/strict-arguments-parameters.cjs similarity index 100% rename from tests/corpus/6263-strict-arguments-parameters.cjs rename to tests/corpus/strict-arguments-parameters.cjs From c1a9341739c469c1f32ed7eb07b6aa20555c4364 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 19:51:39 -0700 Subject: [PATCH 42/83] Name the module truthiness corpus program instead of numbering it Its number collided with another open change. Co-Authored-By: Claude Opus 5.5 --- .../tests/corpus/6264-required-module-truthiness.cjs.json | 6 ------ .../tests/corpus/required-module-truthiness.cjs.json | 6 ++++++ ...module-truthiness.cjs => required-module-truthiness.cjs} | 0 3 files changed, 6 insertions(+), 6 deletions(-) delete mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6264-required-module-truthiness.cjs.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/required-module-truthiness.cjs.json rename tests/corpus/{6264-required-module-truthiness.cjs => required-module-truthiness.cjs} (100%) diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6264-required-module-truthiness.cjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6264-required-module-truthiness.cjs.json deleted file mode 100644 index 2fa691953..000000000 --- a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6264-required-module-truthiness.cjs.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "order": [ - "/tests/corpus/6264-required-module-truthiness.cjs" - ], - "diags": [] -} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/required-module-truthiness.cjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/required-module-truthiness.cjs.json new file mode 100644 index 000000000..101bb2d57 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/required-module-truthiness.cjs.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/required-module-truthiness.cjs" + ], + "diags": [] +} diff --git a/tests/corpus/6264-required-module-truthiness.cjs b/tests/corpus/required-module-truthiness.cjs similarity index 100% rename from tests/corpus/6264-required-module-truthiness.cjs rename to tests/corpus/required-module-truthiness.cjs From 297ec38f07a9ed09ccbc09a4664e02e6802756cb Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 19:42:15 -0700 Subject: [PATCH 43/83] Promisify functions whose callback is optional `util.promisify(client.close.bind(client))` over `close(callback?: (error?: Error) => void)` was refused because the callback parameter is `callback | undefined`. The promisified function now takes the union's function arm and passes the callback into the union. Co-Authored-By: Claude Opus 5.5 --- .../lowering/builtins/promisify-callback.ts | 39 ++++++++++++++----- .../6428-promisify-callback-functions.ts | 13 +++++-- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/packages/compiler/src/frontend/lowering/builtins/promisify-callback.ts b/packages/compiler/src/frontend/lowering/builtins/promisify-callback.ts index cbb13d7f8..693079d3f 100644 --- a/packages/compiler/src/frontend/lowering/builtins/promisify-callback.ts +++ b/packages/compiler/src/frontend/lowering/builtins/promisify-callback.ts @@ -25,6 +25,9 @@ export interface CallbackShape { resultT: IrType & { kind: "func" }; args: readonly IrType[]; callbackT: IrType & { kind: "func" }; + /** The parameter's declared type: callbackT, or `callbackT | undefined` + * for an optional callback. */ + callbackParamT: IrType; inner: IrType; /** The error parameter's union and the tag of its Error arm, or null when * the callback declares no error parameter. */ @@ -39,7 +42,20 @@ function callbackShape( ): CallbackShape | null { if (fnT?.kind !== "func" || resultT?.kind !== "func") return null; if (fnT.rest || fnT.restAbi || fnT.argumentsAll || resultT.rest || resultT.restAbi) return null; - const callbackT = fnT.params[fnT.params.length - 1]; + const callbackParamT = fnT.params[fnT.params.length - 1]; + // An optional callback (`close(callback?: (err?: Error) => void)`) takes + // its function arm; the call passes the callback into the union. + const callbackT = + callbackParamT?.kind === "union" + ? (() => { + const arms = lowerer.unions.get(callbackParamT.unionId)?.arms ?? []; + const fns = arms.filter((arm) => arm.kind === "func"); + return fns.length === 1 && + arms.every((arm) => arm.kind === "func" || arm.kind === "undefinedT") + ? fns[0] + : undefined; + })() + : callbackParamT; if (callbackT?.kind !== "func" || callbackT.ret.kind !== "void") return null; if (callbackT.rest || callbackT.restAbi || callbackT.params.length > 2) return null; const args = fnT.params.slice(0, -1); @@ -71,7 +87,7 @@ function callbackShape( const probe: IrExpr = { kind: "varRef", localId: "%probe", type: valueT, loc }; if (!typeEquals(lowerer.coerceToExpected(probe, inner).type, inner)) return null; } - return { fnT, resultT, args, callbackT, inner, error }; + return { fnT, resultT, args, callbackT, callbackParamT: callbackParamT!, inner, error }; } /** The callback shape of a `promisify(fn)` call, or null. */ @@ -137,7 +153,7 @@ function promisifyMaker(lowerer: Lowerer, shape: CallbackShape, loc: SrcLoc): st if (existing) return existing; const base = `%promisify.${lowerer.arrHofHelpers.size}`; lowerer.arrHofHelpers.set(key, base); - const { fnT, resultT, args, callbackT, inner, error } = shape; + const { fnT, resultT, args, callbackT, callbackParamT, inner, error } = shape; const promiseT = resultT.ret; const resolveT = funcOf(inner.kind === "void" ? [] : [inner], VOID); const rejectT = funcOf([ERROR_T], VOID); @@ -270,13 +286,16 @@ function promisifyMaker(lowerer: Lowerer, shape: CallbackShape, loc: SrcLoc): st callee: varRef("f.0", fnT, loc), args: [ ...args.map((type, i) => varRef(argIds[i]!, type, loc)), - { - kind: "call", - callee: `${base}.mkcb`, - args: [varRef("res.0", resolveT, loc), varRef("rej.0", rejectT, loc)], - type: callbackT, - loc, - }, + lowerer.coerceToExpected( + { + kind: "call", + callee: `${base}.mkcb`, + args: [varRef("res.0", resolveT, loc), varRef("rej.0", rejectT, loc)], + type: callbackT, + loc, + }, + callbackParamT, + ), ], type: VOID, loc, diff --git a/tests/corpus/6428-promisify-callback-functions.ts b/tests/corpus/6428-promisify-callback-functions.ts index 9c2193882..45cf17052 100644 --- a/tests/corpus/6428-promisify-callback-functions.ts +++ b/tests/corpus/6428-promisify-callback-functions.ts @@ -1,6 +1,7 @@ -// util.promisify over typed callback-only functions (a bound method, a -// function declaration, an inline arrow): the error argument rejects, the -// value fulfills, a synchronous throw rejects, and settlement is async. +// util.promisify over typed callback-only functions (bound methods, one with +// an optional callback, a function declaration, an inline arrow): the error +// argument rejects, the value fulfills, a synchronous throw rejects, and +// settlement is async. import { promisify } from "node:util"; class Connection { @@ -19,6 +20,9 @@ class Connection { callback(); }, 1); } + flush(callback?: (error?: Error) => void): void { + setImmediate(() => callback?.(this.closed ? new Error("flushed after close") : undefined)); + } stats(callback: (error: Error | null, open: number) => void): void { setImmediate(() => (this.closed ? callback(new RangeError("closed"), 0) : callback(null, 3))); } @@ -32,6 +36,8 @@ async function main(): Promise { const c = new Connection("db"); const stats = promisify(c.stats.bind(c)); console.log("open", await stats()); + await promisify(c.flush.bind(c))(); + console.log("flushed"); const closeAsync = promisify(c.close.bind(c)); await closeAsync(); console.log("closed once"); @@ -45,6 +51,7 @@ async function main(): Promise { } catch (e) { console.log("stats:", (e as Error).name, (e as Error).message); } + await promisify(c.flush.bind(c))().catch((e) => console.log("flush:", (e as Error).message)); try { await promisify(boom)(); } catch (e) { From d07188d250b987fc65c5ee38f86ab0cc6cb7cf56 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Mon, 19 Oct 2026 22:00:59 -0500 Subject: [PATCH 44/83] Merge base --- .../npm-static-bundled-chunks.test.ts | 92 ++++++++ .../src/frontend/npm-static-bundled-chunks.ts | 200 +++++++++++++++++- packages/compiler/src/frontend/program.ts | 110 ++++++++-- .../2564-chunk-helper-shadowing/main.ts.json | 6 + .../2565-chunk-shared-function/main.ts.json | 6 + .../2566-cjs-cycle-indirect-call/main.ts.json | 6 + .../main.ts.json | 6 + .../cases/2564-chunk-helper-shadowing/main.ts | 7 + .../cases/2565-chunk-shared-function/main.ts | 7 + .../2566-cjs-cycle-indirect-call/main.ts | 7 + .../2571-cjs-cycle-stored-function/main.ts | 8 + .../gtchunkshadow/dist/index.d.ts | 2 + .../node_modules/gtchunkshadow/dist/index.js | 52 +++++ .../node_modules/gtchunkshadow/package.json | 7 + .../gtchunkshared/dist/index.d.ts | 2 + .../node_modules/gtchunkshared/dist/index.js | 56 +++++ .../node_modules/gtchunkshared/package.json | 7 + .../node_modules/gtcyclealias/dist/index.d.ts | 3 + .../node_modules/gtcyclealias/dist/index.js | 37 ++++ .../node_modules/gtcyclealias/dist/reader.js | 34 +++ .../node_modules/gtcyclealias/package.json | 7 + .../node_modules/gtcyclebox/dist/index.d.ts | 3 + .../npm/node_modules/gtcyclebox/dist/index.js | 37 ++++ .../node_modules/gtcyclebox/dist/reader.js | 34 +++ .../npm/node_modules/gtcyclebox/package.json | 7 + .../node_modules/gtcyclecall/dist/index.d.ts | 3 + .../node_modules/gtcyclecall/dist/index.js | 37 ++++ .../node_modules/gtcyclecall/dist/reader.js | 33 +++ .../npm/node_modules/gtcyclecall/package.json | 7 + tests/harness/npm-cases.ts | 4 +- tests/harness/npm-static.test.ts | 57 ++++- 31 files changed, 860 insertions(+), 24 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts.json create mode 100644 tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts create mode 100644 tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts create mode 100644 tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts create mode 100644 tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts create mode 100644 tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtchunkshadow/package.json create mode 100644 tests/fixtures/npm/node_modules/gtchunkshared/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtchunkshared/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtchunkshared/package.json create mode 100644 tests/fixtures/npm/node_modules/gtcyclealias/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtcyclealias/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtcyclealias/dist/reader.js create mode 100644 tests/fixtures/npm/node_modules/gtcyclealias/package.json create mode 100644 tests/fixtures/npm/node_modules/gtcyclebox/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtcyclebox/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtcyclebox/dist/reader.js create mode 100644 tests/fixtures/npm/node_modules/gtcyclebox/package.json create mode 100644 tests/fixtures/npm/node_modules/gtcyclecall/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtcyclecall/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtcyclecall/dist/reader.js create mode 100644 tests/fixtures/npm/node_modules/gtcyclecall/package.json diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts index 82926d2b8..f9463ea1e 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts @@ -111,6 +111,98 @@ console.log(require_named().name); expect(chunk).toContain("var __defProp = Object.defineProperty;"); }); +test("a helper travels with a chunk whose nested scopes declare its name", () => { + const answer = expectSameBehavior(`${keyedHelper}function label(n) { + return "label " + n; +} +function wrap(n) { + function inner() { + var label = "inner"; + return label; + } + return label(n) + " " + inner(); +} +var require_e = __commonJS({ + "e.js"(exports2, module2) { + function nested() { + var label = "nested"; + return label; + } + var seen = []; + for (let label of ["loop"]) seen.push(label); + try { + throw "caught"; + } catch (label) { + seen.push(label); + } + { + const label = "block"; + seen.push(label); + } + module2.exports = { outer: label(1), indirect: (0, label)(3), wrapped: wrap(2), nested: nested(), seen }; + } +}); +var e = require_e(); +console.log(e.outer, e.indirect, e.wrapped, e.nested, e.seen.join(",")); +`); + const chunk = answer.chunks[0]!.source; + expect(chunk).toContain("function label(n)"); + expect(chunk).toContain("function wrap(n)"); +}); + +test("a named function expression that only calls itself travels with a chunk", () => { + const answer = expectSameBehavior(`${keyedHelper}var fact = function self(n) { + return n <= 1 ? 1 : n * self(n - 1); +}; +var kind = function self(self) { + return typeof self; +}; +var outer = function self() { + var self = 2; + return self; +}; +var require_g = __commonJS({ + "g.js"(exports2, module2) { + module2.exports = [fact(5), kind(1), outer()]; + } +}); +console.log(require_g().join(",")); +`); + const chunk = answer.chunks[0]!.source; + expect(chunk).toContain("var fact = function self(n)"); +}); + +test.each([ + ["exports it", `function tally() {}`, `module2.exports = { tally };`], + ["reads its properties", `function tally() {}`, `exports2.calls = tally.calls;`], + ["constructs it", `function tally() {}`, `exports2.made = new tally();`], + ["stores an arrow helper", `var tally = () => 1;`, `exports2.fn = tally;`], + [ + "calls a function that reads its own name as a value", + `var tally = function self() { self.n = (self.n || 0) + 1; return self.n; };`, + `exports2.count = tally();`, + ], + [ + "calls a function whose parameter default reads its own name", + `var tally = function self(box = self) { box.n = (box.n || 0) + 1; return box.n; };`, + `exports2.count = tally();`, + ], +])("a chunk that %s keeps the package refused", (_, declaration, statement) => { + const result = split(`${keyedHelper}${declaration} +var require_f = __commonJS({ + "f.js"(exports2, module2) { + ${statement} + } +}); +require_f(); +`); + expect(result).toEqual({ + degrade: expect.stringContaining( + "uses the bundle's top-level function 'tally' as a value", + ) as string, + }); +}); + test.each([ ["reads its factory's receiver", `console.log(this);`], ["reads its factory's arguments object", `console.log(arguments.length);`], diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts index 28fe4a1b9..2a3ad0b53 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts @@ -105,6 +105,128 @@ export function declaredNames(root: ts.Node): Set { return names; } +/** Adds the identifiers a binding name (an identifier or a destructuring + * pattern) declares. */ +function addBindingNames(name: ts.BindingName, out: Set): void { + if (ts.isIdentifier(name)) { + out.add(name.text); + return; + } + for (const element of name.elements.nodes) { + if (!ts.isOmittedExpression(element) && element.name !== undefined) + addBindingNames(element.name, out); + } +} + +function isFunctionScope(n: ts.Node): n is ts.SignatureDeclaration { + return ( + ts.isFunctionDeclaration(n) || + ts.isFunctionExpression(n) || + ts.isArrowFunction(n) || + ts.isMethodDeclaration(n) || + ts.isGetAccessorDeclaration(n) || + ts.isSetAccessorDeclaration(n) || + ts.isConstructorDeclaration(n) + ); +} + +/** The `var` names a function body declares, without entering nested + * functions. Function declarations in nested blocks count as block-scoped + * here; treating one as free only adds an unused helper copy. */ +function varScopedNames(body: ts.Node): Set { + const out = new Set(); + ts.walkPreorder(body, (n) => { + if (n !== body && (isFunctionScope(n) || ts.isClassDeclaration(n) || ts.isClassExpression(n))) + return "skip"; + if (ts.isVariableDeclarationList(n) && (n.flags & ts.NodeFlags.BlockScoped) === 0) { + for (const d of n.declarations.nodes) addBindingNames(d.name, out); + } + return undefined; + }); + return out; +} + +/** The names one scope node binds for the references inside it. */ +function scopeNames(scope: ts.Node): Set { + const out = new Set(); + if (isFunctionScope(scope)) { + for (const param of scope.parameters.nodes) addBindingNames(param.name, out); + if (ts.isFunctionExpression(scope) && scope.name !== undefined) out.add(scope.name.text); + if (scope.body !== undefined) for (const name of varScopedNames(scope.body)) out.add(name); + } else if (ts.isBlock(scope) || ts.isSourceFile(scope) || ts.isCaseBlock(scope)) { + const statements = ts.isCaseBlock(scope) + ? scope.clauses.nodes.flatMap((clause) => clause.statements.nodes) + : scope.statements.nodes; + for (const stmt of statements) { + if ((ts.isFunctionDeclaration(stmt) || ts.isClassDeclaration(stmt)) && stmt.name) + out.add(stmt.name.text); + else if ( + ts.isVariableStatement(stmt) && + (stmt.declarationList.flags & ts.NodeFlags.BlockScoped) !== 0 + ) { + for (const d of stmt.declarationList.declarations.nodes) addBindingNames(d.name, out); + } + } + } else if (ts.isCatchClause(scope) && scope.variableDeclaration !== undefined) { + addBindingNames(scope.variableDeclaration.name, out); + } else if ( + (ts.isForStatement(scope) || ts.isForInStatement(scope) || ts.isForOfStatement(scope)) && + scope.initializer !== undefined && + ts.isVariableDeclarationList(scope.initializer) && + (scope.initializer.flags & ts.NodeFlags.BlockScoped) !== 0 + ) { + for (const d of scope.initializer.declarations.nodes) addBindingNames(d.name, out); + } else if ( + (ts.isClassDeclaration(scope) || ts.isClassExpression(scope)) && + scope.name !== undefined + ) { + out.add(scope.name.text); + } + return out; +} + +/** Resolves references against one bundle's scopes, keeping each scope's + * names for the bundle's split. */ +class BundleScopes { + private readonly names = new Map>(); + + private of(scope: ts.Node): Set { + let names = this.names.get(scope); + if (names === undefined) { + names = scopeNames(scope); + this.names.set(scope, names); + } + return names; + } + + /** True when a scope between the reference and `root` (inclusive) + * binds its name. */ + boundWithin(id: ts.Identifier, root: ts.Node): boolean { + for (let n = id.parent; n !== undefined; n = n.parent) { + if (this.of(n).has(id.text)) return true; + if (n === root) return false; + } + return false; + } + + /** True when a scope inside `root` (exclusive) binds the reference's + * name. */ + boundInside(id: ts.Identifier, root: ts.Node): boolean { + for (let n = id.parent; n !== undefined && n !== root; n = n.parent) { + if (this.of(n).has(id.text)) return true; + } + return false; + } + + /** True when the reference resolves to a top-level binding of `sf`. */ + topLevel(id: ts.Identifier, sf: ts.SourceFile): boolean { + for (let n = id.parent; n !== undefined && n !== sf; n = n.parent) { + if (this.of(n).has(id.text)) return false; + } + return true; + } +} + /** Every binding reference inside a subtree. */ export function references(root: ts.Node): ts.Identifier[] { const out: ts.Identifier[] = []; @@ -230,6 +352,52 @@ function copyableInitializer(init: ts.Expression | undefined): boolean { return depth > 0 && ts.isIdentifier(walk) && walk.text === "Object"; } +/** A top-level declaration whose value is a function. */ +function isFunctionValued(node: ts.VariableDeclaration | ts.FunctionDeclaration): boolean { + if (ts.isFunctionDeclaration(node)) return true; + const init = node.initializer && unparen(node.initializer); + return init !== undefined && (ts.isArrowFunction(init) || ts.isFunctionExpression(init)); +} + +/** True when a named function expression initializer (`var f = function + * self() {}`) uses its own name only as a callee: `self` is the copied + * function, so it is as observable as the top-level binding. */ +function selfCalledOnly( + node: ts.VariableDeclaration | ts.FunctionDeclaration, + scopes: BundleScopes, +): boolean { + if (!ts.isVariableDeclaration(node) || node.initializer === undefined) return true; + const fn = unparen(node.initializer); + if (!ts.isFunctionExpression(fn) || fn.name === undefined || fn.body === undefined) return true; + const self = fn.name.text; + // A parameter or `var` of the same name hides the function's own name. + const own = varScopedNames(fn.body); + for (const param of fn.parameters.nodes) addBindingNames(param.name, own); + if (own.has(self)) return true; + // Its parameter initializers see the name too. + return references(fn).every((r) => r.text !== self || scopes.boundInside(r, fn) || isCallee(r)); +} + +/** True when a reference is the function a call invokes: `f(...)`, or the + * `(0, f)(...)` form that drops the receiver. */ +function isCallee(id: ts.Identifier): boolean { + let callee: ts.Node = id; + const p = id.parent; + if ( + p !== undefined && + ts.isBinaryExpression(p) && + p.operatorToken.kind === ts.SyntaxKind.CommaToken && + p.right === id && + p.parent !== undefined && + ts.isParenthesizedExpression(p.parent) + ) + callee = p.parent; + while (callee.parent !== undefined && ts.isParenthesizedExpression(callee.parent)) + callee = callee.parent; + const call = callee.parent; + return call !== undefined && ts.isCallExpression(call) && call.expression === callee; +} + /** One recognized chunk declaration. */ interface Chunk { name: string; @@ -302,6 +470,12 @@ export function splitBundledChunks( if (declaredAnywhere.has("Object") || written.has("Object")) return { degrade: CHUNK_SHAPE_DEGRADE }; const stable = (name: string): boolean => !duplicate.has(name) && !written.has(name); + // Names whose every reference in the bundle is a callee: `f(...)` or the + // `(0, f)(...)` form. + const scopes = new BundleScopes(); + const valueUsed = new Set(); + for (const r of allRefs) if (!isCallee(r) && scopes.topLevel(r, sf)) valueUsed.add(r.text); + const calledOnly = (name: string): boolean => !valueUsed.has(name); const ownNames = (name: string): boolean => { const t = top.get(name); if (t === undefined || !stable(name) || !ts.isVariableDeclaration(t.node)) return false; @@ -422,7 +596,17 @@ export function splitBundledChunks( const out: BundledChunkSplit["chunks"] = []; const bundleEdits: Edit[] = [...callEdits]; for (const chunk of chunks.values()) { - const result = chunkSource(sf, chunk, chunks, top, stable, callEdits, bundleStrict); + const result = chunkSource( + sf, + chunk, + chunks, + top, + stable, + calledOnly, + scopes, + callEdits, + bundleStrict, + ); if (typeof result !== "string") return result; out.push({ path: chunk.path, source: result }); bundleEdits.push({ start: chunk.stmt.getStart(sf), end: chunk.stmt.getEnd(), text: "" }); @@ -524,6 +708,8 @@ function chunkSource( chunks: ReadonlyMap, top: ReadonlyMap, stable: (name: string) => boolean, + calledOnly: (name: string) => boolean, + scopes: BundleScopes, callEdits: readonly Edit[], bundleStrict: boolean, ): string | { degrade: string } { @@ -570,7 +756,8 @@ function chunkSource( } continue; } - if (local.has(name)) continue; + // The factory function itself binds the parameters and `var`s. + if (scopes.boundWithin(r, body.parent ?? body)) continue; if (name === "exports" || name === "module") return why(`reads the bundle's own '${name}' binding`); if (name === "__filename") return why("reads __filename, which names the bundle file"); @@ -594,9 +781,14 @@ function chunkSource( (ts.isVariableDeclaration(node) && copyableInitializer(node.initializer))); if (!pure || chunks.has(name)) return why(`reads the bundle's top-level binding '${name}', which it cannot carry along`); - const own = declaredNames(node); + // Each chunk gets its own copy, so a function's identity and its + // properties must stay unobservable: the bundle only ever calls it. + if (isFunctionValued(node) && (!calledOnly(name) || !selfCalledOnly(node, scopes))) + return why( + `uses the bundle's top-level function '${name}' as a value, which its chunks cannot share`, + ); for (const r of references(node)) { - if (own.has(r.text) || !top.has(r.text) || copied.has(r.text)) continue; + if (scopes.boundWithin(r, node) || !top.has(r.text) || copied.has(r.text)) continue; copied.add(r.text); copyQueue.push(r.text); } diff --git a/packages/compiler/src/frontend/program.ts b/packages/compiler/src/frontend/program.ts index 908a587ea..79978abee 100644 --- a/packages/compiler/src/frontend/program.ts +++ b/packages/compiler/src/frontend/program.ts @@ -3199,29 +3199,105 @@ function cjsCycleRefusal7( return null; } -/** True for a plain read `binding.name` (not a call, not a write) of the - * cycle member `dep` whose export `name` is a top-level function - * declaration nothing reassigns. Every member settles its exports before - * entering the cycle, so Node reads that same function whenever the read - * runs, and the compiled read resolves to the declaration itself. */ +/** True when `use` initializes a top-level const whose every other + * reference is a value of an export statement (`module.exports = { … }` + * or `exports.n = …`). */ +function publishedAlias7(use: ts.Node, decl: ts.Node | undefined): boolean { + if ( + decl === undefined || + !ts.isVariableDeclaration(decl) || + decl.initializer !== use || + !ts.isIdentifier(decl.name) + ) + return false; + const list = decl.parent; + const stmt = list?.parent; + const sf = stmt?.parent; + if ( + list === undefined || + (list.flags & ts.NodeFlags.Const) === 0 || + stmt === undefined || + !ts.isVariableStatement(stmt) || + sf === undefined || + !ts.isSourceFile(sf) + ) + return false; + const alias = decl.name; + const published = new Set(); + for (const st of sf.statements.nodes) { + const cjs = cjsExportAssignmentOf(st); + if (cjs === null) continue; + if (cjs.kind === "member") published.add(cjs.value); + else if (cjs.obj !== null) { + for (const p of cjs.obj.properties.nodes) { + if (ts.isPropertyAssignment(p) && !ts.isComputedPropertyName(p.name)) + published.add(p.initializer); + else if (ts.isShorthandPropertyAssignment(p)) published.add(p.name); + } + } + } + let kept = false; + ts.walkPreorder(sf, (n) => { + if (kept) return "stop"; + if (!ts.isIdentifier(n) || n === alias || n.text !== alias.text) return undefined; + const p = n.parent; + if ( + p !== undefined && + ((ts.isPropertyAccessExpression(p) && p.name === n) || + (ts.isPropertyAssignment(p) && p.name === n)) + ) + return undefined; + if (!published.has(n)) kept = true; + return undefined; + }); + return !kept; +} + +/** True for a plain read `binding.name` (not a call, not a write, not a + * value that is stored or passed on) of the cycle member `dep` whose + * export `name` is a top-level function declaration nothing reassigns. + * Every member settles its exports before entering the cycle, so Node + * reads that same function whenever the read runs, and the compiled read + * resolves to the declaration itself. */ function functionExportRead7(id: ts.Identifier, dep: ts.SourceFile): boolean { const access = id.parent; if (!ts.isPropertyAccessExpression(access) || access.expression !== id) return false; + // The value the read yields: through parentheses and the right operand + // of a comma, so `(0, binding.name)(...)` is the call it spells. let use: ts.Node = access; - while (ts.isParenthesizedExpression(use.parent)) use = use.parent; + for (;;) { + const p = use.parent; + if (ts.isParenthesizedExpression(p)) use = p; + else if ( + ts.isBinaryExpression(p) && + p.operatorToken.kind === ts.SyntaxKind.CommaToken && + p.right === use + ) + use = p; + else break; + } const parent = use.parent; - if ( - (ts.isCallExpression(parent) && parent.expression === use) || - ts.isDeleteExpression(parent) || - ((ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent)) && - (parent.operator === ts.SyntaxKind.PlusPlusToken || - parent.operator === ts.SyntaxKind.MinusMinusToken)) || + // Only a use that neither runs the function nor keeps it is a read: a + // stored alias (`const f = binding.name`, `{ f: binding.name }`) or an + // argument can be called while the cycle initializes. A top-level const + // that only publishes it (`module.exports = { name: f }`, the static + // rewrite's re-export spelling) runs nothing: a module that reads the + // export back checks that read itself. + const observes = + publishedAlias7(use, parent) || + ts.isExpressionStatement(parent) || + ts.isTypeOfExpression(parent) || + ts.isVoidExpression(parent) || + (ts.isPrefixUnaryExpression(parent) && parent.operator === ts.SyntaxKind.ExclamationToken) || (ts.isBinaryExpression(parent) && - parent.left === use && - parent.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && - parent.operatorToken.kind <= ts.SyntaxKind.LastAssignment) - ) - return false; + (parent.operatorToken.kind === ts.SyntaxKind.EqualsEqualsEqualsToken || + parent.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsEqualsToken || + parent.operatorToken.kind === ts.SyntaxKind.EqualsEqualsToken || + parent.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsToken)) || + ((ts.isIfStatement(parent) || ts.isWhileStatement(parent) || ts.isDoStatement(parent)) && + parent.expression === use) || + (ts.isConditionalExpression(parent) && parent.condition === use); + if (!observes) return false; const name = access.name.text; // The exported value's identifier: a table entry or a member export. const values: ts.Expression[] = []; diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts.json new file mode 100644 index 000000000..90e304cbe --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts.json new file mode 100644 index 000000000..6443d170b --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts.json new file mode 100644 index 000000000..f51a29951 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts.json new file mode 100644 index 000000000..c3e0442f9 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts" + ], + "diags": [] +} diff --git a/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts b/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts new file mode 100644 index 000000000..d52b3731d --- /dev/null +++ b/tests/fixtures/npm/cases/2564-chunk-helper-shadowing/main.ts @@ -0,0 +1,7 @@ +// A bundled CommonJS chunk that calls the bundle's top-level `label` while +// a nested function of the chunk declares its own `label`: the chunk +// carries the top-level function along, and each reference keeps its own +// binding. +import { inner, outer } from "gtchunkshadow"; + +console.log(outer(), inner()); diff --git a/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts b/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts new file mode 100644 index 000000000..21ca81b40 --- /dev/null +++ b/tests/fixtures/npm/cases/2565-chunk-shared-function/main.ts @@ -0,0 +1,7 @@ +// Two bundled CommonJS chunks export the bundle's top-level `tally` and +// count calls on a property of it. Node prints "1 2 true": both chunks +// share one function. A per-chunk copy would split its identity and its +// properties, so the package stays refused. +import { counts, same } from "gtchunkshared"; + +console.log(counts(), same()); diff --git a/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts b/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts new file mode 100644 index 000000000..3cca145e0 --- /dev/null +++ b/tests/fixtures/npm/cases/2566-cjs-cycle-indirect-call/main.ts @@ -0,0 +1,7 @@ +// A require cycle whose partner calls an export of index.js with esbuild's +// `(0, import_index.peek)()` spelling while index is still initializing: +// peek reads a const in its temporal dead zone, so Node throws a +// ReferenceError, and scriptc refuses the cycle. +import { show } from "gtcyclecall"; + +console.log(show()); diff --git a/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts b/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts new file mode 100644 index 000000000..ae2be341a --- /dev/null +++ b/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts @@ -0,0 +1,8 @@ +// Require cycles whose partner stores an export of index.js (in a const, +// or in an object literal) and calls it while index is still +// initializing: the export reads a const in its temporal dead zone, so +// Node throws a ReferenceError, and scriptc refuses both cycles. +import { show } from "gtcyclealias"; +import { show as showBox } from "gtcyclebox"; + +console.log(show(), showBox()); diff --git a/tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.d.ts new file mode 100644 index 000000000..238cf1744 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.d.ts @@ -0,0 +1,2 @@ +export declare function outer(): string; +export declare function inner(): string; diff --git a/tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.js b/tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.js new file mode 100644 index 000000000..0f89c7ec5 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunkshadow/dist/index.js @@ -0,0 +1,52 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __commonJS = (cb, mod) => function __require() { + return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; +}; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +function label(n) { + return "label " + n; +} + +// chunk/vendor/shadow.js +var require_shadow = __commonJS({ + "chunk/vendor/shadow.js"(exports2, module2) { + function nested() { + var label = "nested"; + return label; + } + module2.exports = { outer: label(1), inner: nested() }; + } +}); + +// chunk/src/index.ts +var index_exports = {}; +__export(index_exports, { + inner: () => inner, + outer: () => outer +}); +module.exports = __toCommonJS(index_exports); +function outer() { + return require_shadow().outer; +} +function inner() { + return require_shadow().inner; +} +0 && (module.exports = { + inner, + outer +}); diff --git a/tests/fixtures/npm/node_modules/gtchunkshadow/package.json b/tests/fixtures/npm/node_modules/gtchunkshadow/package.json new file mode 100644 index 000000000..6830fbd2c --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunkshadow/package.json @@ -0,0 +1,7 @@ +{ + "name": "gtchunkshadow", + "version": "1.0.0", + "description": "esbuild's CommonJS bundle shape: a chunk calls a top-level function whose name a nested scope of the chunk also declares", + "main": "./dist/index.js", + "types": "./dist/index.d.ts" +} diff --git a/tests/fixtures/npm/node_modules/gtchunkshared/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtchunkshared/dist/index.d.ts new file mode 100644 index 000000000..3a47a0495 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunkshared/dist/index.d.ts @@ -0,0 +1,2 @@ +export declare function same(): boolean; +export declare function counts(): string; diff --git a/tests/fixtures/npm/node_modules/gtchunkshared/dist/index.js b/tests/fixtures/npm/node_modules/gtchunkshared/dist/index.js new file mode 100644 index 000000000..0d3aacec8 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunkshared/dist/index.js @@ -0,0 +1,56 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __commonJS = (cb, mod) => function __require() { + return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; +}; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +function tally() { + tally.calls = (tally.calls || 0) + 1; + return tally.calls; +} + +// chunk/vendor/a.js +var require_a = __commonJS({ + "chunk/vendor/a.js"(exports2, module2) { + module2.exports = { tally, first: tally() }; + } +}); + +// chunk/vendor/b.js +var require_b = __commonJS({ + "chunk/vendor/b.js"(exports2, module2) { + module2.exports = { tally, second: tally() }; + } +}); + +// chunk/src/index.ts +var index_exports = {}; +__export(index_exports, { + counts: () => counts, + same: () => same +}); +module.exports = __toCommonJS(index_exports); +function same() { + return require_a().tally === require_b().tally; +} +function counts() { + return require_a().first + " " + require_b().second; +} +0 && (module.exports = { + counts, + same +}); diff --git a/tests/fixtures/npm/node_modules/gtchunkshared/package.json b/tests/fixtures/npm/node_modules/gtchunkshared/package.json new file mode 100644 index 000000000..bfc90c5d2 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtchunkshared/package.json @@ -0,0 +1,7 @@ +{ + "name": "gtchunkshared", + "version": "1.0.0", + "description": "esbuild's CommonJS bundle shape: two chunks export one top-level function and count calls on a property of it", + "main": "./dist/index.js", + "types": "./dist/index.d.ts" +} diff --git a/tests/fixtures/npm/node_modules/gtcyclealias/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtcyclealias/dist/index.d.ts new file mode 100644 index 000000000..5f4373e2f --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclealias/dist/index.d.ts @@ -0,0 +1,3 @@ +export declare const LIMIT = 3; +export declare function show(): string; +export declare function peek(): number; diff --git a/tests/fixtures/npm/node_modules/gtcyclealias/dist/index.js b/tests/fixtures/npm/node_modules/gtcyclealias/dist/index.js new file mode 100644 index 000000000..77aca3c9b --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclealias/dist/index.js @@ -0,0 +1,37 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var index_exports = {}; +__export(index_exports, { + LIMIT: () => LIMIT, + peek: () => peek, + show: () => show +}); +module.exports = __toCommonJS(index_exports); +var import_reader = require("./reader"); +const LIMIT = 3; +function peek() { + return LIMIT; +} +function show() { + return (0, import_reader.describe)(); +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + LIMIT, + show +}); diff --git a/tests/fixtures/npm/node_modules/gtcyclealias/dist/reader.js b/tests/fixtures/npm/node_modules/gtcyclealias/dist/reader.js new file mode 100644 index 000000000..943e61a0a --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclealias/dist/reader.js @@ -0,0 +1,34 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var reader_exports = {}; +__export(reader_exports, { + captured: () => captured, + describe: () => describe +}); +module.exports = __toCommonJS(reader_exports); +var import_index = require("./index"); +const read = import_index.peek; +const captured = read(); +function describe() { + return "limit " + captured; +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + captured, + describe +}); diff --git a/tests/fixtures/npm/node_modules/gtcyclealias/package.json b/tests/fixtures/npm/node_modules/gtcyclealias/package.json new file mode 100644 index 000000000..9ee7441e0 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclealias/package.json @@ -0,0 +1,7 @@ +{ + "name": "gtcyclealias", + "version": "1.0.0", + "description": "esbuild 0.25.10 per-file CommonJS output shape of a cycle whose partner stores an exported function in a const and calls it while that function's const binding is uninitialized", + "main": "./dist/index.js", + "types": "./dist/index.d.ts" +} diff --git a/tests/fixtures/npm/node_modules/gtcyclebox/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtcyclebox/dist/index.d.ts new file mode 100644 index 000000000..5f4373e2f --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclebox/dist/index.d.ts @@ -0,0 +1,3 @@ +export declare const LIMIT = 3; +export declare function show(): string; +export declare function peek(): number; diff --git a/tests/fixtures/npm/node_modules/gtcyclebox/dist/index.js b/tests/fixtures/npm/node_modules/gtcyclebox/dist/index.js new file mode 100644 index 000000000..77aca3c9b --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclebox/dist/index.js @@ -0,0 +1,37 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var index_exports = {}; +__export(index_exports, { + LIMIT: () => LIMIT, + peek: () => peek, + show: () => show +}); +module.exports = __toCommonJS(index_exports); +var import_reader = require("./reader"); +const LIMIT = 3; +function peek() { + return LIMIT; +} +function show() { + return (0, import_reader.describe)(); +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + LIMIT, + show +}); diff --git a/tests/fixtures/npm/node_modules/gtcyclebox/dist/reader.js b/tests/fixtures/npm/node_modules/gtcyclebox/dist/reader.js new file mode 100644 index 000000000..b8f8a927e --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclebox/dist/reader.js @@ -0,0 +1,34 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var reader_exports = {}; +__export(reader_exports, { + captured: () => captured, + describe: () => describe +}); +module.exports = __toCommonJS(reader_exports); +var import_index = require("./index"); +const box = { read: import_index.peek }; +const captured = box.read(); +function describe() { + return "limit " + captured; +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + captured, + describe +}); diff --git a/tests/fixtures/npm/node_modules/gtcyclebox/package.json b/tests/fixtures/npm/node_modules/gtcyclebox/package.json new file mode 100644 index 000000000..67f943195 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclebox/package.json @@ -0,0 +1,7 @@ +{ + "name": "gtcyclebox", + "version": "1.0.0", + "description": "esbuild 0.25.10 per-file CommonJS output shape of a cycle whose partner stores an exported function in an object literal and calls it while that function's const binding is uninitialized", + "main": "./dist/index.js", + "types": "./dist/index.d.ts" +} diff --git a/tests/fixtures/npm/node_modules/gtcyclecall/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtcyclecall/dist/index.d.ts new file mode 100644 index 000000000..5f4373e2f --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclecall/dist/index.d.ts @@ -0,0 +1,3 @@ +export declare const LIMIT = 3; +export declare function show(): string; +export declare function peek(): number; diff --git a/tests/fixtures/npm/node_modules/gtcyclecall/dist/index.js b/tests/fixtures/npm/node_modules/gtcyclecall/dist/index.js new file mode 100644 index 000000000..77aca3c9b --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclecall/dist/index.js @@ -0,0 +1,37 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var index_exports = {}; +__export(index_exports, { + LIMIT: () => LIMIT, + peek: () => peek, + show: () => show +}); +module.exports = __toCommonJS(index_exports); +var import_reader = require("./reader"); +const LIMIT = 3; +function peek() { + return LIMIT; +} +function show() { + return (0, import_reader.describe)(); +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + LIMIT, + show +}); diff --git a/tests/fixtures/npm/node_modules/gtcyclecall/dist/reader.js b/tests/fixtures/npm/node_modules/gtcyclecall/dist/reader.js new file mode 100644 index 000000000..c98d1ae46 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclecall/dist/reader.js @@ -0,0 +1,33 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var reader_exports = {}; +__export(reader_exports, { + captured: () => captured, + describe: () => describe +}); +module.exports = __toCommonJS(reader_exports); +var import_index = require("./index"); +const captured = (0, import_index.peek)(); +function describe() { + return "limit " + captured; +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + captured, + describe +}); diff --git a/tests/fixtures/npm/node_modules/gtcyclecall/package.json b/tests/fixtures/npm/node_modules/gtcyclecall/package.json new file mode 100644 index 000000000..cc1069a6f --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclecall/package.json @@ -0,0 +1,7 @@ +{ + "name": "gtcyclecall", + "version": "1.0.0", + "description": "esbuild 0.25.10 per-file CommonJS output shape of a cycle whose partner calls an exported function through (0, binding.fn)() while that function's const binding is uninitialized", + "main": "./dist/index.js", + "types": "./dist/index.d.ts" +} diff --git a/tests/harness/npm-cases.ts b/tests/harness/npm-cases.ts index 35a9df4a6..9ca3c0df0 100644 --- a/tests/harness/npm-cases.ts +++ b/tests/harness/npm-cases.ts @@ -19,12 +19,12 @@ export function npmCases(fixturesRoot: string): NpmCase[] { return [ ...globSync(join(fixturesRoot, "npm/cases/*/main.ts")) .sort() - // 2465-2469, 2556-2563 and 2567 are the --npm-static bundler-emitted-CJS + // 2465-2469, 2556-2567 and 2571 are the --npm-static bundler-emitted-CJS // cases (npm-static.test.ts drives them with the opt-in): their // consumers exercise the static rewrite's surface (lexer-visible // names the shipped .d.ts never declares, the __toESM interop // family), so they stay out of the flagless island lane by design. - .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-37])-[^/]+\/main\.ts$/.test(entry)) + .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-7]|2571)-[^/]+\/main\.ts$/.test(entry)) .map((entry) => ({ name: entry.split("/").at(-2)!, entry, diff --git a/tests/harness/npm-static.test.ts b/tests/harness/npm-static.test.ts index 443c0c72d..32639e0d1 100644 --- a/tests/harness/npm-static.test.ts +++ b/tests/harness/npm-static.test.ts @@ -76,7 +76,7 @@ async function buildStatic(entry: string, npmStatic: string[] | "auto"): Promise ...globSync( join(fixturesRoot, "npm/node_modules/cryptozoo/**/*.{js,mjs,cjs,json,d.ts}"), ).sort(), - // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2563, 2567) + // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2567, 2571) ...globSync(join(fixturesRoot, "npm/node_modules/gt*/**/*.{js,cjs,json}")).sort(), ]; for (const f of inputs) hash.update(f).update(readFileSync(f)); @@ -1502,6 +1502,61 @@ describe(`npm-static pilots${sanitize ? " (sanitized)" : ""}`, () => { ]); }, 180_000); + // 2566: the same early read through esbuild's `(0, binding.fn)()` call + // spelling also keeps SC1016. + test("a CommonJS cycle that calls an export during initialization stays refused", () => { + const entry = join(fixturesRoot, "npm/cases/2566-cjs-cycle-indirect-call/main.ts"); + const { coverage } = analyze(entry, { npmStatic: ["gtcyclecall"] }); + expect(coverage.npmStatic).toEqual([ + { + package: "gtcyclecall", + status: "refused", + detail: expect.stringMatching( + /circular imports .*can use its binding 'import_index' of a module of the cycle/, + ) as string, + }, + ]); + }, 120_000); + + // 2571: the partner stores the export (in a const, or in an object + // literal) and calls it while the cycle initializes; both keep SC1016. + test("a CommonJS cycle that stores an export and calls it stays refused", () => { + const entry = join(fixturesRoot, "npm/cases/2571-cjs-cycle-stored-function/main.ts"); + const { coverage } = analyze(entry, { npmStatic: ["gtcyclealias", "gtcyclebox"] }); + for (const pkg of ["gtcyclealias", "gtcyclebox"]) + expect(coverage.npmStatic).toContainEqual({ + package: pkg, + status: "refused", + detail: expect.stringMatching( + /circular imports .*can use its binding 'import_index' of a module of the cycle/, + ) as string, + }); + }, 120_000); + + // 2564: a chunk reads a top-level function whose name a nested scope of + // the chunk also declares; the function still travels with the chunk. + test("a chunk carries a top-level function its nested scopes shadow", async () => { + await expectNodeParity(join(fixturesRoot, "npm/cases/2564-chunk-helper-shadowing/main.ts"), [ + "gtchunkshadow", + ]); + }, 180_000); + + // 2565: Node shares one function between the chunks that export it; a + // per-chunk copy would not, so the package stays refused. + test("chunks that export a shared top-level function stay refused", () => { + const entry = join(fixturesRoot, "npm/cases/2565-chunk-shared-function/main.ts"); + const { coverage } = analyze(entry, { npmStatic: ["gtchunkshared"] }); + expect(coverage.npmStatic).toEqual([ + { + package: "gtchunkshared", + status: "refused", + detail: expect.stringMatching( + /uses the bundle's top-level function 'tally' as a value, which its chunks cannot share/, + ) as string, + }, + ]); + }, 120_000); + test("published type-only declarations coexist with a static runtime bundle", async () => { const entry = join(fixturesRoot, "npm/cases/2469-bundle-offender/main.ts"); const { coverage } = analyze(entry, { npmStatic: ["gtghost"] }); From 1af7a862ce12e1cd16e5b48e7b2a0d2b4cc6a72b Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Mon, 19 Oct 2026 22:05:25 -0500 Subject: [PATCH 45/83] Keep stored cycle functions and self-referencing helper copies refused Two fixes for the CommonJS cycle check and the bundled-chunk splitter: - During a cycle's initialization, a read of a partner's exported function is safe only where the value neither runs nor stays: an expression statement, typeof, void, !, an equality operand, or the test of if, while, do and ?:. A const alias (`const f = binding.fn`), an object or array literal, a return, or an argument can be called before the cycle settles, so these keep SC1016 (Node throws when the function reads a binding in its temporal dead zone). A top-level const that only publishes the function in an export statement, as the static rewrite spells an export-table re-export, stays a read. - A copied helper whose initializer is a named function expression reaches itself through its own name. Each chunk gets its own copy, so that name must also be used only as a callee; otherwise the chunk keeps the package refused with the existing reason. --- .../npm-static-bundled-chunks.test.ts | 32 ++++++ .../src/frontend/npm-static-bundled-chunks.ts | 30 ++++- packages/compiler/src/frontend/program.ts | 103 ++++++++++++++---- .../main.ts.json | 6 + .../2571-cjs-cycle-stored-function/main.ts | 8 ++ .../node_modules/gtcyclealias/dist/index.d.ts | 3 + .../node_modules/gtcyclealias/dist/index.js | 37 +++++++ .../node_modules/gtcyclealias/dist/reader.js | 34 ++++++ .../node_modules/gtcyclealias/package.json | 7 ++ .../node_modules/gtcyclebox/dist/index.d.ts | 3 + .../npm/node_modules/gtcyclebox/dist/index.js | 37 +++++++ .../node_modules/gtcyclebox/dist/reader.js | 34 ++++++ .../npm/node_modules/gtcyclebox/package.json | 7 ++ tests/harness/npm-cases.ts | 4 +- tests/harness/npm-static.test.ts | 17 ++- 15 files changed, 335 insertions(+), 27 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts.json create mode 100644 tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts create mode 100644 tests/fixtures/npm/node_modules/gtcyclealias/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtcyclealias/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtcyclealias/dist/reader.js create mode 100644 tests/fixtures/npm/node_modules/gtcyclealias/package.json create mode 100644 tests/fixtures/npm/node_modules/gtcyclebox/dist/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/gtcyclebox/dist/index.js create mode 100644 tests/fixtures/npm/node_modules/gtcyclebox/dist/reader.js create mode 100644 tests/fixtures/npm/node_modules/gtcyclebox/package.json diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts index fe54681d7..f9463ea1e 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts @@ -150,11 +150,43 @@ console.log(e.outer, e.indirect, e.wrapped, e.nested, e.seen.join(",")); expect(chunk).toContain("function wrap(n)"); }); +test("a named function expression that only calls itself travels with a chunk", () => { + const answer = expectSameBehavior(`${keyedHelper}var fact = function self(n) { + return n <= 1 ? 1 : n * self(n - 1); +}; +var kind = function self(self) { + return typeof self; +}; +var outer = function self() { + var self = 2; + return self; +}; +var require_g = __commonJS({ + "g.js"(exports2, module2) { + module2.exports = [fact(5), kind(1), outer()]; + } +}); +console.log(require_g().join(",")); +`); + const chunk = answer.chunks[0]!.source; + expect(chunk).toContain("var fact = function self(n)"); +}); + test.each([ ["exports it", `function tally() {}`, `module2.exports = { tally };`], ["reads its properties", `function tally() {}`, `exports2.calls = tally.calls;`], ["constructs it", `function tally() {}`, `exports2.made = new tally();`], ["stores an arrow helper", `var tally = () => 1;`, `exports2.fn = tally;`], + [ + "calls a function that reads its own name as a value", + `var tally = function self() { self.n = (self.n || 0) + 1; return self.n; };`, + `exports2.count = tally();`, + ], + [ + "calls a function whose parameter default reads its own name", + `var tally = function self(box = self) { box.n = (box.n || 0) + 1; return box.n; };`, + `exports2.count = tally();`, + ], ])("a chunk that %s keeps the package refused", (_, declaration, statement) => { const result = split(`${keyedHelper}${declaration} var require_f = __commonJS({ diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts index abc86bcbd..276079494 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts @@ -209,6 +209,15 @@ class BundleScopes { return false; } + /** True when a scope inside `root` (exclusive) binds the reference's + * name. */ + boundInside(id: ts.Identifier, root: ts.Node): boolean { + for (let n = id.parent; n !== undefined && n !== root; n = n.parent) { + if (this.of(n).has(id.text)) return true; + } + return false; + } + /** True when the reference resolves to a top-level binding of `sf`. */ topLevel(id: ts.Identifier, sf: ts.SourceFile): boolean { for (let n = id.parent; n !== undefined && n !== sf; n = n.parent) { @@ -350,6 +359,25 @@ function isFunctionValued(node: ts.VariableDeclaration | ts.FunctionDeclaration) return init !== undefined && (ts.isArrowFunction(init) || ts.isFunctionExpression(init)); } +/** True when a named function expression initializer (`var f = function + * self() {}`) uses its own name only as a callee: `self` is the copied + * function, so it is as observable as the top-level binding. */ +function selfCalledOnly( + node: ts.VariableDeclaration | ts.FunctionDeclaration, + scopes: BundleScopes, +): boolean { + if (!ts.isVariableDeclaration(node) || node.initializer === undefined) return true; + const fn = unparen(node.initializer); + if (!ts.isFunctionExpression(fn) || fn.name === undefined || fn.body === undefined) return true; + const self = fn.name.text; + // A parameter or `var` of the same name hides the function's own name. + const own = varScopedNames(fn.body); + for (const param of fn.parameters.nodes) addBindingNames(param.name, own); + if (own.has(self)) return true; + // Its parameter initializers see the name too. + return references(fn).every((r) => r.text !== self || scopes.boundInside(r, fn) || isCallee(r)); +} + /** True when a reference is the function a call invokes: `f(...)`, or the * `(0, f)(...)` form that drops the receiver. */ function isCallee(id: ts.Identifier): boolean { @@ -748,7 +776,7 @@ function chunkSource( return why(`reads the bundle's top-level binding '${name}', which it cannot carry along`); // Each chunk gets its own copy, so a function's identity and its // properties must stay unobservable: the bundle only ever calls it. - if (isFunctionValued(node) && !calledOnly(name)) + if (isFunctionValued(node) && (!calledOnly(name) || !selfCalledOnly(node, scopes))) return why( `uses the bundle's top-level function '${name}' as a value, which its chunks cannot share`, ); diff --git a/packages/compiler/src/frontend/program.ts b/packages/compiler/src/frontend/program.ts index 12d9b703a..79978abee 100644 --- a/packages/compiler/src/frontend/program.ts +++ b/packages/compiler/src/frontend/program.ts @@ -3199,11 +3199,66 @@ function cjsCycleRefusal7( return null; } -/** True for a plain read `binding.name` (not a call, not a write) of the - * cycle member `dep` whose export `name` is a top-level function - * declaration nothing reassigns. Every member settles its exports before - * entering the cycle, so Node reads that same function whenever the read - * runs, and the compiled read resolves to the declaration itself. */ +/** True when `use` initializes a top-level const whose every other + * reference is a value of an export statement (`module.exports = { … }` + * or `exports.n = …`). */ +function publishedAlias7(use: ts.Node, decl: ts.Node | undefined): boolean { + if ( + decl === undefined || + !ts.isVariableDeclaration(decl) || + decl.initializer !== use || + !ts.isIdentifier(decl.name) + ) + return false; + const list = decl.parent; + const stmt = list?.parent; + const sf = stmt?.parent; + if ( + list === undefined || + (list.flags & ts.NodeFlags.Const) === 0 || + stmt === undefined || + !ts.isVariableStatement(stmt) || + sf === undefined || + !ts.isSourceFile(sf) + ) + return false; + const alias = decl.name; + const published = new Set(); + for (const st of sf.statements.nodes) { + const cjs = cjsExportAssignmentOf(st); + if (cjs === null) continue; + if (cjs.kind === "member") published.add(cjs.value); + else if (cjs.obj !== null) { + for (const p of cjs.obj.properties.nodes) { + if (ts.isPropertyAssignment(p) && !ts.isComputedPropertyName(p.name)) + published.add(p.initializer); + else if (ts.isShorthandPropertyAssignment(p)) published.add(p.name); + } + } + } + let kept = false; + ts.walkPreorder(sf, (n) => { + if (kept) return "stop"; + if (!ts.isIdentifier(n) || n === alias || n.text !== alias.text) return undefined; + const p = n.parent; + if ( + p !== undefined && + ((ts.isPropertyAccessExpression(p) && p.name === n) || + (ts.isPropertyAssignment(p) && p.name === n)) + ) + return undefined; + if (!published.has(n)) kept = true; + return undefined; + }); + return !kept; +} + +/** True for a plain read `binding.name` (not a call, not a write, not a + * value that is stored or passed on) of the cycle member `dep` whose + * export `name` is a top-level function declaration nothing reassigns. + * Every member settles its exports before entering the cycle, so Node + * reads that same function whenever the read runs, and the compiled read + * resolves to the declaration itself. */ function functionExportRead7(id: ts.Identifier, dep: ts.SourceFile): boolean { const access = id.parent; if (!ts.isPropertyAccessExpression(access) || access.expression !== id) return false; @@ -3222,25 +3277,27 @@ function functionExportRead7(id: ts.Identifier, dep: ts.SourceFile): boolean { else break; } const parent = use.parent; - // A use that can run the function (a call, `new`, a tag, a member of it - // such as `.call`, or an argument another call receives) is not a read. - if ( - (ts.isCallExpression(parent) && parent.expression === use) || - (ts.isCallExpression(parent) && parent.arguments.nodes.includes(use as ts.Expression)) || - ts.isNewExpression(parent) || - (ts.isTaggedTemplateExpression(parent) && parent.tag === use) || - (ts.isPropertyAccessExpression(parent) && parent.expression === use) || - (ts.isElementAccessExpression(parent) && parent.expression === use) || - ts.isDeleteExpression(parent) || - ((ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent)) && - (parent.operator === ts.SyntaxKind.PlusPlusToken || - parent.operator === ts.SyntaxKind.MinusMinusToken)) || + // Only a use that neither runs the function nor keeps it is a read: a + // stored alias (`const f = binding.name`, `{ f: binding.name }`) or an + // argument can be called while the cycle initializes. A top-level const + // that only publishes it (`module.exports = { name: f }`, the static + // rewrite's re-export spelling) runs nothing: a module that reads the + // export back checks that read itself. + const observes = + publishedAlias7(use, parent) || + ts.isExpressionStatement(parent) || + ts.isTypeOfExpression(parent) || + ts.isVoidExpression(parent) || + (ts.isPrefixUnaryExpression(parent) && parent.operator === ts.SyntaxKind.ExclamationToken) || (ts.isBinaryExpression(parent) && - parent.left === use && - parent.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && - parent.operatorToken.kind <= ts.SyntaxKind.LastAssignment) - ) - return false; + (parent.operatorToken.kind === ts.SyntaxKind.EqualsEqualsEqualsToken || + parent.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsEqualsToken || + parent.operatorToken.kind === ts.SyntaxKind.EqualsEqualsToken || + parent.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsToken)) || + ((ts.isIfStatement(parent) || ts.isWhileStatement(parent) || ts.isDoStatement(parent)) && + parent.expression === use) || + (ts.isConditionalExpression(parent) && parent.condition === use); + if (!observes) return false; const name = access.name.text; // The exported value's identifier: a table entry or a member export. const values: ts.Expression[] = []; diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts.json new file mode 100644 index 000000000..c3e0442f9 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts" + ], + "diags": [] +} diff --git a/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts b/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts new file mode 100644 index 000000000..ae2be341a --- /dev/null +++ b/tests/fixtures/npm/cases/2571-cjs-cycle-stored-function/main.ts @@ -0,0 +1,8 @@ +// Require cycles whose partner stores an export of index.js (in a const, +// or in an object literal) and calls it while index is still +// initializing: the export reads a const in its temporal dead zone, so +// Node throws a ReferenceError, and scriptc refuses both cycles. +import { show } from "gtcyclealias"; +import { show as showBox } from "gtcyclebox"; + +console.log(show(), showBox()); diff --git a/tests/fixtures/npm/node_modules/gtcyclealias/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtcyclealias/dist/index.d.ts new file mode 100644 index 000000000..5f4373e2f --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclealias/dist/index.d.ts @@ -0,0 +1,3 @@ +export declare const LIMIT = 3; +export declare function show(): string; +export declare function peek(): number; diff --git a/tests/fixtures/npm/node_modules/gtcyclealias/dist/index.js b/tests/fixtures/npm/node_modules/gtcyclealias/dist/index.js new file mode 100644 index 000000000..77aca3c9b --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclealias/dist/index.js @@ -0,0 +1,37 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var index_exports = {}; +__export(index_exports, { + LIMIT: () => LIMIT, + peek: () => peek, + show: () => show +}); +module.exports = __toCommonJS(index_exports); +var import_reader = require("./reader"); +const LIMIT = 3; +function peek() { + return LIMIT; +} +function show() { + return (0, import_reader.describe)(); +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + LIMIT, + show +}); diff --git a/tests/fixtures/npm/node_modules/gtcyclealias/dist/reader.js b/tests/fixtures/npm/node_modules/gtcyclealias/dist/reader.js new file mode 100644 index 000000000..943e61a0a --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclealias/dist/reader.js @@ -0,0 +1,34 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var reader_exports = {}; +__export(reader_exports, { + captured: () => captured, + describe: () => describe +}); +module.exports = __toCommonJS(reader_exports); +var import_index = require("./index"); +const read = import_index.peek; +const captured = read(); +function describe() { + return "limit " + captured; +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + captured, + describe +}); diff --git a/tests/fixtures/npm/node_modules/gtcyclealias/package.json b/tests/fixtures/npm/node_modules/gtcyclealias/package.json new file mode 100644 index 000000000..9ee7441e0 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclealias/package.json @@ -0,0 +1,7 @@ +{ + "name": "gtcyclealias", + "version": "1.0.0", + "description": "esbuild 0.25.10 per-file CommonJS output shape of a cycle whose partner stores an exported function in a const and calls it while that function's const binding is uninitialized", + "main": "./dist/index.js", + "types": "./dist/index.d.ts" +} diff --git a/tests/fixtures/npm/node_modules/gtcyclebox/dist/index.d.ts b/tests/fixtures/npm/node_modules/gtcyclebox/dist/index.d.ts new file mode 100644 index 000000000..5f4373e2f --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclebox/dist/index.d.ts @@ -0,0 +1,3 @@ +export declare const LIMIT = 3; +export declare function show(): string; +export declare function peek(): number; diff --git a/tests/fixtures/npm/node_modules/gtcyclebox/dist/index.js b/tests/fixtures/npm/node_modules/gtcyclebox/dist/index.js new file mode 100644 index 000000000..77aca3c9b --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclebox/dist/index.js @@ -0,0 +1,37 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var index_exports = {}; +__export(index_exports, { + LIMIT: () => LIMIT, + peek: () => peek, + show: () => show +}); +module.exports = __toCommonJS(index_exports); +var import_reader = require("./reader"); +const LIMIT = 3; +function peek() { + return LIMIT; +} +function show() { + return (0, import_reader.describe)(); +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + LIMIT, + show +}); diff --git a/tests/fixtures/npm/node_modules/gtcyclebox/dist/reader.js b/tests/fixtures/npm/node_modules/gtcyclebox/dist/reader.js new file mode 100644 index 000000000..b8f8a927e --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclebox/dist/reader.js @@ -0,0 +1,34 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var reader_exports = {}; +__export(reader_exports, { + captured: () => captured, + describe: () => describe +}); +module.exports = __toCommonJS(reader_exports); +var import_index = require("./index"); +const box = { read: import_index.peek }; +const captured = box.read(); +function describe() { + return "limit " + captured; +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + captured, + describe +}); diff --git a/tests/fixtures/npm/node_modules/gtcyclebox/package.json b/tests/fixtures/npm/node_modules/gtcyclebox/package.json new file mode 100644 index 000000000..67f943195 --- /dev/null +++ b/tests/fixtures/npm/node_modules/gtcyclebox/package.json @@ -0,0 +1,7 @@ +{ + "name": "gtcyclebox", + "version": "1.0.0", + "description": "esbuild 0.25.10 per-file CommonJS output shape of a cycle whose partner stores an exported function in an object literal and calls it while that function's const binding is uninitialized", + "main": "./dist/index.js", + "types": "./dist/index.d.ts" +} diff --git a/tests/harness/npm-cases.ts b/tests/harness/npm-cases.ts index 451441009..0830d4162 100644 --- a/tests/harness/npm-cases.ts +++ b/tests/harness/npm-cases.ts @@ -19,12 +19,12 @@ export function npmCases(fixturesRoot: string): NpmCase[] { return [ ...globSync(join(fixturesRoot, "npm/cases/*/main.ts")) .sort() - // 2465-2469 and 2556-2566 are the --npm-static bundler-emitted-CJS + // 2465-2469, 2556-2566 and 2571 are the --npm-static bundler-emitted-CJS // cases (npm-static.test.ts drives them with the opt-in): their // consumers exercise the static rewrite's surface (lexer-visible // names the shipped .d.ts never declares, the __toESM interop // family), so they stay out of the flagless island lane by design. - .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-24-6])-[^/]+\/main\.ts$/.test(entry)) + .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-24-6]|2571)-[^/]+\/main\.ts$/.test(entry)) .map((entry) => ({ name: entry.split("/").at(-2)!, entry, diff --git a/tests/harness/npm-static.test.ts b/tests/harness/npm-static.test.ts index 4930487e2..8325e2998 100644 --- a/tests/harness/npm-static.test.ts +++ b/tests/harness/npm-static.test.ts @@ -76,7 +76,7 @@ async function buildStatic(entry: string, npmStatic: string[] | "auto"): Promise ...globSync( join(fixturesRoot, "npm/node_modules/cryptozoo/**/*.{js,mjs,cjs,json,d.ts}"), ).sort(), - // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2566) + // the bundler-emitted-CJS mini packages (cases 2465-2469, 2556-2566, 2571) ...globSync(join(fixturesRoot, "npm/node_modules/gt*/**/*.{js,cjs,json}")).sort(), ]; for (const f of inputs) hash.update(f).update(readFileSync(f)); @@ -1482,6 +1482,21 @@ describe(`npm-static pilots${sanitize ? " (sanitized)" : ""}`, () => { ]); }, 120_000); + // 2571: the partner stores the export (in a const, or in an object + // literal) and calls it while the cycle initializes; both keep SC1016. + test("a CommonJS cycle that stores an export and calls it stays refused", () => { + const entry = join(fixturesRoot, "npm/cases/2571-cjs-cycle-stored-function/main.ts"); + const { coverage } = analyze(entry, { npmStatic: ["gtcyclealias", "gtcyclebox"] }); + for (const pkg of ["gtcyclealias", "gtcyclebox"]) + expect(coverage.npmStatic).toContainEqual({ + package: pkg, + status: "refused", + detail: expect.stringMatching( + /circular imports .*can use its binding 'import_index' of a module of the cycle/, + ) as string, + }); + }, 120_000); + // 2564: a chunk reads a top-level function whose name a nested scope of // the chunk also declares; the function still travels with the chunk. test("a chunk carries a top-level function its nested scopes shadow", async () => { From 80355af8ea3ae20b0dba90ba230cb5fafbb25638 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Mon, 19 Oct 2026 22:26:32 -0500 Subject: [PATCH 46/83] Give errors held as any their own stack accessor In Node every Error has an own, non-enumerable `stack` accessor, shared by all errors, that formats the stack on first access. Errors that reached checked-dynamic code read `stack` as undefined: the error encoding carried a stack only when a typed read had already formatted one. Reads on instances of Error subclasses found no class arm for it, and assigning `stack` on a subclass instance, typed or through `any`, did not change what a typed read answered. The encoding now lists `stack` first among its own properties as a non-enumerable accessor. Its getter formats the native error's stack on first access, so the heading is the name and message at that point, and its setter stores a string in the native error, which typed reads share. Any other value replaces the accessor with a data property. Objects that inherit from an error read the error's stack, and a getter called on any other receiver answers undefined. A `stack` read on a checked-dynamic value counts as a stack observation, so such programs capture frames as they do for typed reads. Dynamic reads and writes of `stack` on Error subclass instances use the native stack; non-string values are kept with the instance's own properties. --- .../compiler/src/backend/llvm/lib-shared.ts | 1 + .../src/backend/stack-observations.test.ts | 18 +++ .../src/backend/stack-observations.ts | 5 +- .../lowering/class-dynamic-dispatch.ts | 105 +++++++++++++++ .../compiler/src/ir/builtin-signatures.ts | 1 + packages/compiler/src/ir/validate.ts | 1 + .../5916-dyn-error-stack-property.ts.json | 6 + .../6400-dyn-error-stack-property-js.js.json | 6 + packages/runtime/src/scr_error.c | 6 + packages/runtime/src/scr_error.h | 3 + packages/runtime/src/scr_json.c | 105 ++++++++++++++- packages/runtime/src/scr_message.c | 6 + tests/corpus/5916-dyn-error-stack-property.ts | 126 ++++++++++++++++++ .../6400-dyn-error-stack-property-js.js | 126 ++++++++++++++++++ .../5916-dyn-error-stack-property.ts.json | 1 + .../6400-dyn-error-stack-property-js.js.json | 1 + 16 files changed, 511 insertions(+), 6 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5916-dyn-error-stack-property.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6400-dyn-error-stack-property-js.js.json create mode 100644 tests/corpus/5916-dyn-error-stack-property.ts create mode 100644 tests/corpus/6400-dyn-error-stack-property-js.js create mode 100644 tests/harness/corpus-costs/5916-dyn-error-stack-property.ts.json create mode 100644 tests/harness/corpus-costs/6400-dyn-error-stack-property-js.js.json diff --git a/packages/compiler/src/backend/llvm/lib-shared.ts b/packages/compiler/src/backend/llvm/lib-shared.ts index 2d91386b3..77f352571 100644 --- a/packages/compiler/src/backend/llvm/lib-shared.ts +++ b/packages/compiler/src/backend/llvm/lib-shared.ts @@ -827,6 +827,7 @@ export const LIB_FN_SYMS: Record = { "dyn.sameValue": "scr_dyn_same_value", "error.toString": "scr_error_to_string", "error.stack": "scr_error_stack", + "error.setStack": "scr_error_set_stack", "error.stackLimitGet": "scr_stack_limit_get", "error.stackLimitSet": "scr_stack_limit_set", "class.name": "scr_classobj_name", diff --git a/packages/compiler/src/backend/stack-observations.test.ts b/packages/compiler/src/backend/stack-observations.test.ts index a95f0140f..a09f7f58d 100644 --- a/packages/compiler/src/backend/stack-observations.test.ts +++ b/packages/compiler/src/backend/stack-observations.test.ts @@ -1,5 +1,6 @@ import { expect, test } from "vitest"; import { + DYN, F64, STRING, VOID, @@ -49,6 +50,23 @@ test("a reachable stack read is observed", () => { expect(observesStacks(mod([fn("%main", [call("parse")]), fn("parse", [stackRead])]))).toBe(true); }); +test("a stack read on a checked-dynamic value is observed", () => { + const value: IrExpr = { loc, kind: "varRef", localId: "d", type: DYN }; + const read = (key: string): IrStmt => ({ + loc, + kind: "exprStmt", + expr: { + loc, + kind: "dynKeyGet", + value, + key: { loc, kind: "strLit", value: key, type: STRING }, + type: DYN, + }, + }); + expect(observesStacks(mod([fn("%main", [read("stack")])]))).toBe(true); + expect(observesStacks(mod([fn("%main", [read("message")])]))).toBe(false); +}); + test("programs without stack reads are not", () => { expect(observesStacks(mod([fn("%main", [call("helper")]), fn("helper", [ret])]))).toBe(false); }); diff --git a/packages/compiler/src/backend/stack-observations.ts b/packages/compiler/src/backend/stack-observations.ts index 7debf0751..4894e927a 100644 --- a/packages/compiler/src/backend/stack-observations.ts +++ b/packages/compiler/src/backend/stack-observations.ts @@ -13,8 +13,11 @@ import { writesPublished } from "./publication.js"; /** Lib calls that read a captured stack trace. */ const STACK_READS: ReadonlySet = new Set(["error.stack"]); +/** A typed `error.stack` read, or a `stack` read on a checked-dynamic + * value, which reaches an error's own `stack` accessor. */ function readsStack(expr: IrExpr): boolean { - return expr.kind === "libCall" && STACK_READS.has(expr.fn); + if (expr.kind === "libCall") return STACK_READS.has(expr.fn); + return expr.kind === "dynKeyGet" && expr.key.kind === "strLit" && expr.key.value === "stack"; } function containsStackRead(fn: IrFunction): boolean { diff --git a/packages/compiler/src/frontend/lowering/class-dynamic-dispatch.ts b/packages/compiler/src/frontend/lowering/class-dynamic-dispatch.ts index 6b515c1ff..400005ea9 100644 --- a/packages/compiler/src/frontend/lowering/class-dynamic-dispatch.ts +++ b/packages/compiler/src/frontend/lowering/class-dynamic-dispatch.ts @@ -2694,6 +2694,7 @@ export class ClassDynamicDispatch { !findGenericMethodOn(lowerer, info, dispatch.name) && dispatch.name !== "constructor" && !info.fields.has(dispatch.name) && + !(dispatch.name === "stack" && lowerer.isSubclassOf(info.def.name, "%Error")) && !findMethodOn(lowerer, info, `get:${dispatch.name}`) && !findMethodOn(lowerer, info, `set:${dispatch.name}`)) ) @@ -4498,6 +4499,110 @@ export class ClassDynamicDispatch { }, ]; } + // An Error's own `stack`: a string is the native error's stack, which + // typed reads share; any other value is kept with the instance's own + // properties, which a read checks first. A class whose chain declares + // a `stack` accessor answers through it (the accessor code below). + if ( + name === "stack" && + !info.fields.has(name) && + lowerer.isSubclassOf(info.def.name, "%Error") && + !findMethodOn(lowerer, info, "get:stack") && + !findMethodOn(lowerer, info, "set:stack") + ) { + const key: IrExpr = { kind: "strLit", value: name, type: STRING, loc }; + const error = lowerer.upcastTo(receiver, "%Error"); + if (write) { + const helper = classPropertiesHelper(lowerer, loc); + this.generated.add(helper); + const store: IrStmt = { + kind: "exprStmt", + expr: { + kind: "libCall", + fn: "dyn.keySet", + args: [ + { + kind: "call", + callee: helper.name, + args: [lowerer.coerceToExpected(receiver, DYN)], + type: DYN, + loc, + }, + key, + varRef("p.1", DYN, loc), + ], + type: VOID, + loc, + }, + loc, + }; + return [ + { + kind: "if", + cond: { + kind: "dynTest", + test: "string", + value: varRef("p.1", DYN, loc), + type: BOOL, + loc, + }, + then: [ + { + kind: "exprStmt", + expr: { + kind: "libCall", + fn: "error.setStack", + args: [error, lowerer.coerceToExpected(varRef("p.1", DYN, loc), STRING)], + type: VOID, + loc, + }, + loc, + }, + { + kind: "if", + cond: this.readBagHas( + lowerer, + "dyn.hasKey", + this.readPropertyBag(lowerer, receiver, loc), + key, + loc, + ), + then: [store], + else_: null, + loc, + }, + ], + else_: [store], + loc, + }, + { kind: "return", value: null, loc }, + ]; + } + const bag = this.readPropertyBag(lowerer, receiver, loc); + return [ + { + kind: "return", + value: { + kind: "ternary", + cond: this.readBagHas(lowerer, "dyn.hasKey", bag, key, loc), + then: { + kind: "libCall", + fn: "dyn.bagGet", + args: [bag, key, lowerer.coerceToExpected(receiver, DYN)], + type: DYN, + loc, + }, + else_: lowerer.coerceToExpected( + { kind: "libCall", fn: "error.stack", args: [error], type: STRING, loc }, + DYN, + ), + type: DYN, + loc, + }, + loc, + }, + ]; + } if ((name === "message" || name === "name") && lowerer.isSubclassOf(info.def.name, "%Error")) return write ? [ diff --git a/packages/compiler/src/ir/builtin-signatures.ts b/packages/compiler/src/ir/builtin-signatures.ts index 697a185a5..f12df2052 100644 --- a/packages/compiler/src/ir/builtin-signatures.ts +++ b/packages/compiler/src/ir/builtin-signatures.ts @@ -3231,6 +3231,7 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ /** SameValue over checked values, including NaN, signed zero and reference identity. */ "dyn.sameValue": { argTypes: [DYN, DYN], result: BOOL }, "error.stack": { argTypes: [null], result: STRING }, + "error.setStack": { argTypes: [null, STRING], result: VOID }, "error.stackLimitGet": { argTypes: [], result: F64 }, "error.stackLimitSet": { argTypes: [F64], result: VOID }, // Receiver (any error-hierarchy object) and the program-dependent diff --git a/packages/compiler/src/ir/validate.ts b/packages/compiler/src/ir/validate.ts index 4a961dfca..50ba236b8 100644 --- a/packages/compiler/src/ir/validate.ts +++ b/packages/compiler/src/ir/validate.ts @@ -4109,6 +4109,7 @@ function validateFunction( if (checkLibEmitterCall(e, sig.result)) return; if ( e.fn === "error.stack" || + e.fn === "error.setStack" || e.fn === "error.cause" || e.fn === "error.hasCause" || e.fn === "error.setCause" || diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5916-dyn-error-stack-property.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5916-dyn-error-stack-property.ts.json new file mode 100644 index 000000000..fa5b5460f --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5916-dyn-error-stack-property.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/5916-dyn-error-stack-property.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6400-dyn-error-stack-property-js.js.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6400-dyn-error-stack-property-js.js.json new file mode 100644 index 000000000..7bea48883 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6400-dyn-error-stack-property-js.js.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6400-dyn-error-stack-property-js.js" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_error.c b/packages/runtime/src/scr_error.c index 6f616c35a..4f514681b 100644 --- a/packages/runtime/src/scr_error.c +++ b/packages/runtime/src/scr_error.c @@ -354,6 +354,12 @@ ScrStr *scr_error_stack(ScrError *e) { return scr_str_retain(e->stack); } +void scr_error_set_stack(ScrError *e, ScrStr *stack) { + ScrStr *replacement = scr_str_retain(stack); + scr_str_release(e->stack); + e->stack = replacement; +} + bool scr_error_is(const void *obj) { const ScrVt *vt = ((const ScrError *)obj)->vt; return scr_error_vts[SCR_ERR_ERROR].pre <= vt->pre && diff --git a/packages/runtime/src/scr_error.h b/packages/runtime/src/scr_error.h index ce60dac23..453ebf3e1 100644 --- a/packages/runtime/src/scr_error.h +++ b/packages/runtime/src/scr_error.h @@ -252,6 +252,9 @@ void scr_error_install_message_reader(ScrStr *(*reader)(ScrError *)); void scr_error_install_name_reader(ScrStr *(*reader)(ScrError *)); ScrStr *scr_error_default_name(const ScrError *e); ScrStr *scr_error_stack(ScrError *e); +/* Replaces the error's stack string, as an assignment to `stack` does. + * Borrowed operands. */ +void scr_error_set_stack(ScrError *e, ScrStr *stack); ScrError *scr_error_retain(ScrError *e); void scr_error_release(ScrError *e); /* dispatches through e->vt */ diff --git a/packages/runtime/src/scr_json.c b/packages/runtime/src/scr_json.c index 83b56bea4..aa331f617 100644 --- a/packages/runtime/src/scr_json.c +++ b/packages/runtime/src/scr_json.c @@ -6719,10 +6719,109 @@ static void scr_errdyn_teardown(void) { free(cache); } +/* An error's own `stack`: a non-enumerable accessor shared by every error, + * as V8 defines it. The getter formats the stack on first access, so its + * heading is the name and message at that point, and later reads answer + * the same string. Both functions find the error through the receiver or + * its prototype chain; on any other receiver the getter answers undefined + * and the setter changes nothing but the receiver's own accessor. */ +static SCR_TL ScrDyn *scr_error_stack_accessors[2]; +static SCR_TL bool scr_error_stack_accessors_registered; + +static void scr_error_stack_accessors_cleanup(void) { + for (size_t i = 0; i < 2; i++) { + scr_dyn_release(scr_error_stack_accessors[i]); + scr_error_stack_accessors[i] = NULL; + } + scr_error_stack_accessors_registered = false; +} + +/* The native error behind an accessor receiver. +1 or NULL. */ +static ScrError *scr_error_stack_holder(const ScrDyn *receiver) { + for (const ScrDyn *current = receiver; current; current = scr_dyn_proto_next(current)) { + ScrError *error = scr_errdyn_err_of(current); + if (error) return error; + if (current->kind != SCR_DYN_OBJ) return NULL; + } + return NULL; +} + +static ScrDyn *scr_error_stack_get_call(ScrClosure *closure, ScrDyn *const *args, size_t argc) { + (void)closure; (void)args; (void)argc; + ScrDyn *self = scr_dyn_this_get(); + ScrError *error = scr_error_stack_holder(self); + scr_dyn_release(self); + if (!error) return scr_dyn_retain(scr_dyn_undefined()); + ScrStr *stack = scr_error_stack(error); + scr_error_release(error); + ScrDyn *out = scr_dyn_new_str(stack); + scr_str_release(stack); + return out; +} + +static ScrDyn *scr_error_stack_set_call(ScrClosure *closure, ScrDyn *const *args, size_t argc); + +/* Whether an own property is an error's `stack` accessor. */ +static bool scr_dyn_entry_is_error_stack(const ScrDynEntry *entry) { + return entry->accessor && entry->setter && entry->setter->kind == SCR_DYN_FUNC && + entry->setter->v.fn.thunk == scr_error_stack_set_call; +} + +static ScrDyn *scr_error_stack_set_call(ScrClosure *closure, ScrDyn *const *args, size_t argc) { + (void)closure; + ScrDyn *self = scr_dyn_this_get(); + ScrError *error = scr_error_stack_holder(self); + ScrDyn *value = argc > 0 ? args[0] : scr_dyn_undefined(); + if (error && value->kind == SCR_DYN_STR) { + scr_error_set_stack(error, value->v.str); + } else if (self->kind == SCR_DYN_OBJ) { + /* The native slot holds strings only, and a typed reference's + * snapshot names no error: the receiver's own accessor becomes a data + * property holding the value, which a snapshot's commit stores. */ + ScrStr *key = scr_str_new("stack", 5); + ScrDynEntry *entry = scr_dyn_entry(self, key); + scr_str_release(key); + if (entry && scr_dyn_entry_is_error_stack(entry)) { + ScrDyn *old_value = entry->value, *old_getter = entry->getter, *old_setter = entry->setter; + entry->value = scr_dyn_retain(value); + entry->getter = entry->setter = NULL; + entry->accessor = false; + entry->writable = true; + scr_dyn_release(old_value); + scr_dyn_release(old_getter); + scr_dyn_release(old_setter); + } + } + scr_error_release(error); + scr_dyn_release(self); + return scr_dyn_retain(scr_dyn_undefined()); +} + +static void scr_error_dyn_add_stack(ScrDyn *d) { + if (!scr_error_stack_accessors_registered) { + scr_error_stack_accessors_registered = true; + scr_atexit(scr_error_stack_accessors_cleanup); + } + if (!scr_error_stack_accessors[0]) { + scr_error_stack_accessors[0] = + scr_dyn_new_func(scr_closure_new(NULL, 0), scr_error_stack_get_call, 0, "native:error.stack", NULL); + scr_error_stack_accessors[1] = + scr_dyn_new_func(scr_closure_new(NULL, 0), scr_error_stack_set_call, 1, "native:error.stack", NULL); + } + scr_dyn_obj_set(d, "stack", 5, scr_dyn_retain(scr_dyn_undefined())); + ScrDynEntry *entry = &d->v.obj.entries[d->v.obj.len - 1]; + entry->accessor = true; + entry->getter = scr_dyn_retain(scr_error_stack_accessors[0]); + entry->setter = scr_dyn_retain(scr_error_stack_accessors[1]); + entry->enumerable = false; +} + ScrDyn *scr_error_dyn_fields(const ScrError *e) { ScrDyn *d = scr_dyn_new_obj(); scr_dyn_obj_set(d, "%error", 6, scr_dyn_new_bool(true)); /* the checked-dynamic tree's error marker */ d->v.obj.entries[d->v.obj.len - 1].enumerable = false; + /* Node lists `stack` first among an error's own properties. */ + scr_error_dyn_add_stack(d); if (e->name_present) { scr_dyn_obj_set(d, "name", 4, scr_dyn_new_str(e->name)); d->v.obj.entries[d->v.obj.len - 1].enumerable = e->name_enumerable; @@ -6744,11 +6843,6 @@ ScrDyn *scr_error_dyn_fields(const ScrError *e) { scr_dyn_obj_set(d, "message", 7, scr_dyn_new_str(e->message)); d->v.obj.entries[d->v.obj.len - 1].enumerable = e->message_enumerable; } - if (e->stack) { - /* Node's own `stack` is non-enumerable. */ - scr_dyn_obj_set(d, "stack", 5, scr_dyn_new_str(e->stack)); - d->v.obj.entries[d->v.obj.len - 1].enumerable = false; - } if (e->system_message) { /* SystemError: code, the read-only `info` context ({ message, path, * syscall, errno, code }), then errno, syscall, path and dest. */ @@ -10860,6 +10954,7 @@ static ScrDyn *scr_sc_clone(const ScrDyn *v, const ScrScParent *up, ScrDynRegion ScrDyn *out = scr_dyn_new_obj(); for (size_t i = 0; i < v->v.obj.len; i++) { const ScrDynEntry *e = &v->v.obj.entries[i]; + if (scr_dyn_entry_is_error_stack(e)) continue; ScrDyn *c = scr_sc_clone(e->value, &self, region); if (c == NULL) { scr_dyn_release(out); diff --git a/packages/runtime/src/scr_message.c b/packages/runtime/src/scr_message.c index 7c969a2e3..e77e3b9c2 100644 --- a/packages/runtime/src/scr_message.c +++ b/packages/runtime/src/scr_message.c @@ -801,6 +801,12 @@ ScrDyn *scr_message_decode(const ScrMessage *message) { } if (!values[i]->v.arr.properties) values[i]->v.arr.properties = scr_dyn_new_obj(); scr_dyn_obj_set(values[i]->v.arr.properties, property->key, property->length, value); + } else if (node->kind == SCR_MESSAGE_ERROR && property->length == 5 && !memcmp(property->key, "stack", 5)) { + /* The new error's own `stack` accessor stores the value. */ + ScrStr *key = scr_str_new(property->key, property->length); + scr_dyn_key_set(values[i], key, value); + scr_str_release(key); + scr_dyn_release(value); } else scr_dyn_obj_set(values[i], property->key, property->length, value); } if (node->kind == SCR_MESSAGE_ERROR) { diff --git a/tests/corpus/5916-dyn-error-stack-property.ts b/tests/corpus/5916-dyn-error-stack-property.ts new file mode 100644 index 000000000..6198e839c --- /dev/null +++ b/tests/corpus/5916-dyn-error-stack-property.ts @@ -0,0 +1,126 @@ +// Errors held as `any` or `unknown` have an own `stack` like Node's: a +// non-enumerable accessor that every error shares. The first read formats +// "Name: message" from the error's name and message at that point, and +// later reads answer the same string. Assigning a value replaces it, +// `delete` removes it, Object.keys, spread, Object.assign and +// JSON.stringify leave it out, `in` and Object.hasOwn find it, objects +// that inherit from an error read the error's stack. Typed and dynamic +// reads answer the same string, and a stack read through `any` captures +// frames. Only the first line is printed: frame lines differ between +// runtimes. + +class ParseError extends Error { + constructor(message: string) { + super(message); + this.name = "ParseError"; + } +} + +class PlainSubclass extends Error {} + +function dyn(value: unknown): any { + return value; +} + +function firstLine(value: unknown): string { + return typeof value === "string" ? JSON.stringify(value.split("\n")[0]) : `(${typeof value} ${String(value)})`; +} + +function describe(label: string, error: any): void { + console.log( + label, + "|", + typeof error.stack, + firstLine(error.stack), + "|", + Object.keys(error).join(","), + JSON.stringify(error), + "|", + "stack" in error, + Object.hasOwn(error, "stack"), + error.propertyIsEnumerable("stack"), + ); +} + +try { + throw new RangeError("thrown"); +} catch (error) { + describe("caught", error); +} + +const unknownError: unknown = new TypeError("bad thing"); +describe("unknown", unknownError); +describe("plain", dyn(new Error("plain"))); +describe("no message", dyn(new SyntaxError())); +describe("subclass", dyn(new ParseError("line 3"))); +describe("plain subclass", dyn(new PlainSubclass("inherited name"))); + +const coded = dyn(new Error("with code")); +coded.code = "E_CODE"; +describe("own property", coded); + +const descriptor = Object.getOwnPropertyDescriptor(dyn(new Error("a")), "stack")!; +const other = Object.getOwnPropertyDescriptor(dyn(new TypeError("b")), "stack")!; +console.log("descriptor", Object.keys(descriptor).join(","), descriptor.enumerable, descriptor.configurable); +console.log("shared accessors", typeof descriptor.get, descriptor.get === other.get, descriptor.set === other.set); +console.log("foreign receiver", descriptor.get!.call({}), firstLine(descriptor.get!.call(dyn(new TypeError("b"))))); +const subclassDescriptor = Object.getOwnPropertyDescriptor(dyn(new PlainSubclass("s")), "stack")!; +console.log("subclass descriptor", Object.keys(subclassDescriptor).join(","), subclassDescriptor.get === descriptor.get); + +const renamed = dyn(new Error("before")); +renamed.message = "after"; +renamed.name = "Renamed"; +console.log("first read", firstLine(renamed.stack)); +renamed.message = "later"; +console.log("later read", firstLine(renamed.stack)); +const subclassRenamed = dyn(new PlainSubclass("before")); +subclassRenamed.message = "after"; +console.log("subclass first read", firstLine(subclassRenamed.stack)); + +const replaced = dyn(new Error("replaced")); +replaced.stack = "custom stack\n at nowhere"; +describe("assigned", replaced); +console.log("assigned descriptor", Object.keys(Object.getOwnPropertyDescriptor(replaced, "stack")!).join(",")); +const subclassReplaced = dyn(new ParseError("replaced")); +subclassReplaced.stack = "subclass stack"; +console.log("subclass assigned", subclassReplaced.stack); +const typedReplaced = new ParseError("typed write"); +typedReplaced.stack = "typed subclass stack"; +console.log("typed subclass assigned", typedReplaced.stack, dyn(typedReplaced).stack); +const subclassNumbered = dyn(new PlainSubclass("numbered")); +subclassNumbered.stack = 7; +console.log("subclass number", subclassNumbered.stack, typeof subclassNumbered.stack); +subclassNumbered.stack = "string again"; +console.log("subclass string again", subclassNumbered.stack); +const numbered = dyn(new Error("numbered")); +numbered.stack = 5; +console.log("number", numbered.stack, typeof numbered.stack); +const removed = dyn(new Error("removed")); +delete removed.stack; +console.log("deleted", removed.stack, "stack" in removed, Object.hasOwn(removed, "stack")); + +const spreadSource = dyn(new Error("spread")); +console.log("spread", Object.keys({ ...spreadSource }).length, Object.keys(Object.assign({}, spreadSource)).length); +console.log("inherited", firstLine(Object.create(dyn(new RangeError("prototype"))).stack)); + +const typed = new Error("typed"); +const view = dyn(typed); +console.log("typed and dynamic", view.stack === typed.stack, firstLine(typed.stack)); +const typedSubclass = new ParseError("typed subclass"); +console.log("typed subclass", firstLine(dyn(typedSubclass).stack), dyn(typedSubclass).stack === typedSubclass.stack); +const assignedThroughView = new Error("typed"); +dyn(assignedThroughView).stack = "through the view"; +console.log("assigned through view", assignedThroughView.stack); + +function inner(): any { + return dyn(new Error("deep")); +} +function outer(): any { + return inner(); +} +const deep: string = outer().stack; +console.log("frames", deep.split("\n").length > 1, deep.includes("at inner"), deep.includes("at outer")); +function makeSubclass(): any { + return dyn(new PlainSubclass("deep")); +} +console.log("subclass frames", (makeSubclass().stack as string).includes("at makeSubclass")); diff --git a/tests/corpus/6400-dyn-error-stack-property-js.js b/tests/corpus/6400-dyn-error-stack-property-js.js new file mode 100644 index 000000000..e46db41eb --- /dev/null +++ b/tests/corpus/6400-dyn-error-stack-property-js.js @@ -0,0 +1,126 @@ +// In JavaScript sources, errors have an own `stack` like Node's: a +// non-enumerable accessor that every error shares. The first read formats +// "Name: message" from the error's name and message at that point, and +// later reads answer the same string. Assigning a value replaces it, +// `delete` removes it, Object.keys, spread, Object.assign and +// JSON.stringify leave it out, `in` and Object.hasOwn find it, objects +// that inherit from an error read the error's stack. Typed and dynamic +// reads answer the same string, and a stack read through `any` captures +// frames. Only the first line is printed: frame lines differ between +// runtimes. + +class ParseError extends Error { + constructor(message) { + super(message); + this.name = "ParseError"; + } +} + +class PlainSubclass extends Error {} + +function dyn(value) { + return value; +} + +function firstLine(value) { + return typeof value === "string" ? JSON.stringify(value.split("\n")[0]) : `(${typeof value} ${String(value)})`; +} + +function describe(label, error) { + console.log( + label, + "|", + typeof error.stack, + firstLine(error.stack), + "|", + Object.keys(error).join(","), + JSON.stringify(error), + "|", + "stack" in error, + Object.hasOwn(error, "stack"), + error.propertyIsEnumerable("stack"), + ); +} + +try { + throw new RangeError("thrown"); +} catch (error) { + describe("caught", error); +} + +const unknownError = new TypeError("bad thing"); +describe("unknown", unknownError); +describe("plain", dyn(new Error("plain"))); +describe("no message", dyn(new SyntaxError())); +describe("subclass", dyn(new ParseError("line 3"))); +describe("plain subclass", dyn(new PlainSubclass("inherited name"))); + +const coded = dyn(new Error("with code")); +coded.code = "E_CODE"; +describe("own property", coded); + +const descriptor = dyn(Object.getOwnPropertyDescriptor(dyn(new Error("a")), "stack")); +const other = dyn(Object.getOwnPropertyDescriptor(dyn(new TypeError("b")), "stack")); +console.log("descriptor", Object.keys(descriptor).join(","), descriptor.enumerable, descriptor.configurable); +console.log("shared accessors", typeof descriptor.get, descriptor.get === other.get, descriptor.set === other.set); +console.log("foreign receiver", descriptor.get.call({}), firstLine(descriptor.get.call(dyn(new TypeError("b"))))); +const subclassDescriptor = dyn(Object.getOwnPropertyDescriptor(dyn(new PlainSubclass("s")), "stack")); +console.log("subclass descriptor", Object.keys(subclassDescriptor).join(","), subclassDescriptor.get === descriptor.get); + +const renamed = dyn(new Error("before")); +renamed.message = "after"; +renamed.name = "Renamed"; +console.log("first read", firstLine(renamed.stack)); +renamed.message = "later"; +console.log("later read", firstLine(renamed.stack)); +const subclassRenamed = dyn(new PlainSubclass("before")); +subclassRenamed.message = "after"; +console.log("subclass first read", firstLine(subclassRenamed.stack)); + +const replaced = dyn(new Error("replaced")); +replaced.stack = "custom stack\n at nowhere"; +describe("assigned", replaced); +console.log("assigned descriptor", Object.keys(Object.getOwnPropertyDescriptor(replaced, "stack")).join(",")); +const subclassReplaced = dyn(new ParseError("replaced")); +subclassReplaced.stack = "subclass stack"; +console.log("subclass assigned", subclassReplaced.stack); +const typedReplaced = new ParseError("typed write"); +typedReplaced.stack = "typed subclass stack"; +console.log("typed subclass assigned", typedReplaced.stack, dyn(typedReplaced).stack); +const subclassNumbered = dyn(new PlainSubclass("numbered")); +subclassNumbered.stack = 7; +console.log("subclass number", subclassNumbered.stack, typeof subclassNumbered.stack); +subclassNumbered.stack = "string again"; +console.log("subclass string again", subclassNumbered.stack); +const numbered = dyn(new Error("numbered")); +numbered.stack = 5; +console.log("number", numbered.stack, typeof numbered.stack); +const removed = dyn(new Error("removed")); +delete removed.stack; +console.log("deleted", removed.stack, "stack" in removed, Object.hasOwn(removed, "stack")); + +const spreadSource = dyn(new Error("spread")); +console.log("spread", Object.keys({ ...spreadSource }).length, Object.keys(Object.assign({}, spreadSource)).length); +console.log("inherited", firstLine(Object.create(dyn(new RangeError("prototype"))).stack)); + +const typed = new Error("typed"); +const view = dyn(typed); +console.log("typed and dynamic", view.stack === typed.stack, firstLine(typed.stack)); +const typedSubclass = new ParseError("typed subclass"); +console.log("typed subclass", firstLine(dyn(typedSubclass).stack), dyn(typedSubclass).stack === typedSubclass.stack); +const assignedThroughView = new Error("typed"); +dyn(assignedThroughView).stack = "through the view"; +console.log("assigned through view", assignedThroughView.stack); + +function inner() { + return dyn(new Error("deep")); +} +function outer() { + return inner(); +} +const deep = outer().stack; +console.log("frames", deep.split("\n").length > 1, deep.includes("at inner"), deep.includes("at outer")); +function makeSubclass() { + return dyn(new PlainSubclass("deep")); +} +console.log("subclass frames", makeSubclass().stack.includes("at makeSubclass")); diff --git a/tests/harness/corpus-costs/5916-dyn-error-stack-property.ts.json b/tests/harness/corpus-costs/5916-dyn-error-stack-property.ts.json new file mode 100644 index 000000000..3ed0acb36 --- /dev/null +++ b/tests/harness/corpus-costs/5916-dyn-error-stack-property.ts.json @@ -0,0 +1 @@ +{ "plain": 3232, "san": 4298 } diff --git a/tests/harness/corpus-costs/6400-dyn-error-stack-property-js.js.json b/tests/harness/corpus-costs/6400-dyn-error-stack-property-js.js.json new file mode 100644 index 000000000..c51a3475f --- /dev/null +++ b/tests/harness/corpus-costs/6400-dyn-error-stack-property-js.js.json @@ -0,0 +1 @@ +{ "plain": 3172, "san": 4044 } From 72eab2aa0e7e65e75cdd8db5590ce5cba31f5409 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 21:06:01 -0700 Subject: [PATCH 47/83] Document process.exitCode reads on the limitations page Co-Authored-By: Claude Opus 5.5 --- docs/content/limitations/builtin-apis/exit-status.mdx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/content/limitations/builtin-apis/exit-status.mdx b/docs/content/limitations/builtin-apis/exit-status.mdx index d9bb779db..3cdf10496 100644 --- a/docs/content/limitations/builtin-apis/exit-status.mdx +++ b/docs/content/limitations/builtin-apis/exit-status.mdx @@ -1,3 +1,4 @@ - **Exit status:** Numeric `process.exitCode = value` writes in statement position set the implicit exit status, which `process.exit()` uses when called without an argument. `process.exit(code)` and `process.exitCode` throw a `RangeError` (`ERR_OUT_OF_RANGE`) for a code that is not a safe integer, and convert other codes to int32, as Node does. - Reads, resets, and assignments used as values are unsupported. + Reading `process.exitCode` answers the assigned code, or `undefined` before any assignment; `process.exit(code)` assigns it before the `'exit'` listeners run. + Resets (`process.exitCode = undefined`) and assignments used as values are unsupported. From 72e22ed0edfb0c29d3cc68fef5d53ea338bbae28 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Mon, 19 Oct 2026 23:22:23 -0500 Subject: [PATCH 48/83] Run SHA-384 and SHA-512 blocks with AVX2, AVX-512VL or the arm64 SHA-512 instructions SHA-384, SHA-512, SHA-512/224 and SHA-512/256 (createHash, createHmac, crypto.hash, pbkdf2, hkdf) process their blocks with the fastest kernel the CPU supports: - x86-64 with AVX2 and BMI2: the message schedule is computed two words at a time in vector registers, interleaved with the scalar rounds, which rotate with RORX. - x86-64 with AVX-512VL: the same, with vector rotates and three-input XOR in the schedule. - arm64 with the SHA-512 extension: SHA512H, SHA512H2, SHA512SU0 and SHA512SU1. - Otherwise the portable blocks, now unrolled over a 16-word schedule ring. The CPU and OS checks run once and are cached. The update and final paths pass all their whole blocks to the kernel in one call. test_sha_kernels.c checks the FIPS 180-4 SHA-384 and SHA-512 vectors through every kernel the CPU supports and compares each kernel with the portable blocks over every length from 0 to 1100 bytes and 1 MiB, whole and in 1-, 7-, 64- and 100-byte updates. Corpus 5779 compares SHA-384, SHA-512 and HMAC-SHA512 digests over many lengths and update sizes with Node; tests/harness/corpus-costs/ records its plain and sanitized costs. --- .../tests/corpus/5779-sha512-lengths.ts.json | 6 + packages/runtime/src/scr_lib.c | 344 ++++++++++++++++-- packages/runtime/test/sha-kernels.test.ts | 4 +- packages/runtime/test/test_sha_kernels.c | 100 ++++- tests/corpus/5779-sha512-lengths.ts | 35 ++ .../corpus-costs/5779-sha512-lengths.ts.json | 1 + 6 files changed, 456 insertions(+), 34 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5779-sha512-lengths.ts.json create mode 100644 tests/corpus/5779-sha512-lengths.ts create mode 100644 tests/harness/corpus-costs/5779-sha512-lengths.ts.json diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5779-sha512-lengths.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5779-sha512-lengths.ts.json new file mode 100644 index 000000000..60582ec60 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/5779-sha512-lengths.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/5779-sha512-lengths.ts" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_lib.c b/packages/runtime/src/scr_lib.c index 930d44818..99c5a8f63 100644 --- a/packages/runtime/src/scr_lib.c +++ b/packages/runtime/src/scr_lib.c @@ -7405,29 +7405,319 @@ static uint64_t scr_sha512_rotr(uint64_t x, unsigned n) { return (x >> n) | (x << (64 - n)); } -SCR_DIGEST_ALIGNED static void scr_sha512_block(uint64_t h[8], const unsigned char *data) { - uint64_t w[80]; - for (size_t i = 0; i < 16; i++) { - w[i] = 0; - for (size_t b = 0; b < 8; b++) w[i] = (w[i] << 8) | data[i * 8 + b]; +#define SCR_SHA512_BSIG0(x) \ + (scr_sha512_rotr(x, 28) ^ scr_sha512_rotr(x, 34) ^ scr_sha512_rotr(x, 39)) +#define SCR_SHA512_BSIG1(x) \ + (scr_sha512_rotr(x, 14) ^ scr_sha512_rotr(x, 18) ^ scr_sha512_rotr(x, 41)) +#define SCR_SHA512_SSIG0(x) (scr_sha512_rotr(x, 1) ^ scr_sha512_rotr(x, 8) ^ ((x) >> 7)) +#define SCR_SHA512_SSIG1(x) (scr_sha512_rotr(x, 19) ^ scr_sha512_rotr(x, 61) ^ ((x) >> 6)) + +/* One round with the eight working variables passed in rotated order, so + * an unrolled sequence of rounds needs no register moves. */ +#define SCR_SHA512_ROUND(a, b, c, d, e, f, g, h, k, w) \ + do { \ + uint64_t t1 = (h) + SCR_SHA512_BSIG1(e) + ((g) ^ ((e) & ((f) ^ (g)))) + (k) + (w); \ + (d) += t1; \ + (h) = t1 + SCR_SHA512_BSIG0(a) + (((a) & (b)) | ((c) & ((a) | (b)))); \ + } while (0) + +static uint64_t scr_sha512_load(const unsigned char *p) { + uint64_t v = 0; + for (size_t b = 0; b < 8; b++) v = (v << 8) | p[b]; + return v; +} + +/* The message schedule is kept as a 16-word ring and extended 16 words + * at a time between groups of 16 rounds. */ +SCR_DIGEST_ALIGNED static void scr_sha512_blocks_portable(uint64_t state[8], const unsigned char *p, + size_t blocks) { + uint64_t a = state[0], b = state[1], c = state[2], d = state[3]; + uint64_t e = state[4], f = state[5], g = state[6], h = state[7]; + for (; blocks != 0; blocks--, p += 128) { + uint64_t w[16]; + for (size_t i = 0; i < 16; i++) w[i] = scr_sha512_load(p + i * 8); + for (size_t i = 0; i < 80; i += 16) { + if (i != 0) { + for (size_t j = 0; j < 16; j++) { + w[j] += SCR_SHA512_SSIG1(w[(j + 14) & 15]) + w[(j + 9) & 15] + + SCR_SHA512_SSIG0(w[(j + 1) & 15]); + } + } + const uint64_t *k = scr_sha512_k + i; + SCR_SHA512_ROUND(a, b, c, d, e, f, g, h, k[0], w[0]); + SCR_SHA512_ROUND(h, a, b, c, d, e, f, g, k[1], w[1]); + SCR_SHA512_ROUND(g, h, a, b, c, d, e, f, k[2], w[2]); + SCR_SHA512_ROUND(f, g, h, a, b, c, d, e, k[3], w[3]); + SCR_SHA512_ROUND(e, f, g, h, a, b, c, d, k[4], w[4]); + SCR_SHA512_ROUND(d, e, f, g, h, a, b, c, k[5], w[5]); + SCR_SHA512_ROUND(c, d, e, f, g, h, a, b, k[6], w[6]); + SCR_SHA512_ROUND(b, c, d, e, f, g, h, a, k[7], w[7]); + SCR_SHA512_ROUND(a, b, c, d, e, f, g, h, k[8], w[8]); + SCR_SHA512_ROUND(h, a, b, c, d, e, f, g, k[9], w[9]); + SCR_SHA512_ROUND(g, h, a, b, c, d, e, f, k[10], w[10]); + SCR_SHA512_ROUND(f, g, h, a, b, c, d, e, k[11], w[11]); + SCR_SHA512_ROUND(e, f, g, h, a, b, c, d, k[12], w[12]); + SCR_SHA512_ROUND(d, e, f, g, h, a, b, c, k[13], w[13]); + SCR_SHA512_ROUND(c, d, e, f, g, h, a, b, k[14], w[14]); + SCR_SHA512_ROUND(b, c, d, e, f, g, h, a, k[15], w[15]); + } + a = state[0] += a; b = state[1] += b; c = state[2] += c; d = state[3] += d; + e = state[4] += e; f = state[5] += f; g = state[6] += g; h = state[7] += h; } - for (size_t i = 16; i < 80; i++) { - uint64_t s0 = scr_sha512_rotr(w[i - 15], 1) ^ scr_sha512_rotr(w[i - 15], 8) ^ (w[i - 15] >> 7); - uint64_t s1 = scr_sha512_rotr(w[i - 2], 19) ^ scr_sha512_rotr(w[i - 2], 61) ^ (w[i - 2] >> 6); - w[i] = w[i - 16] + s0 + w[i - 7] + s1; +} + +#ifdef SCR_SHA_NI +/* ── SHA-512 kernels (x86-64) ───────────────────────────────────────── + * With AVX2 and BMI2, the message schedule is computed two words at a + * time in vector registers, interleaved with the scalar rounds, and the + * rounds rotate with RORX. With AVX-512VL the schedule uses vector + * rotates and three-input XOR. The CPU and OS state checks run once and + * are cached. */ +static int scr_sha512_x86_state; /* 0 unknown, 1 portable, 2 AVX2, 3 AVX-512VL */ + +static int scr_sha512_x86(void) { + if (scr_sha512_x86_state == 0) { + unsigned a, b, c, d; + int kind = 1; + if (__get_cpuid(1, &a, &b, &c, &d) && (c & bit_OSXSAVE) && (c & bit_AVX)) { + unsigned lo, hi; + __asm__ volatile("xgetbv" : "=a"(lo), "=d"(hi) : "c"(0)); + (void)hi; + /* XMM and YMM state enabled. */ + if ((lo & 6) == 6 && __get_cpuid_count(7, 0, &a, &b, &c, &d) && (b & bit_AVX2) && + (b & bit_BMI2)) { + kind = 2; + /* Opmask, ZMM0-15 upper halves and ZMM16-31 state enabled. */ + if ((b & bit_AVX512F) && (b & bit_AVX512VL) && (lo & 0xe0) == 0xe0) kind = 3; + } + } + scr_sha512_x86_state = kind; } - uint64_t a = h[0], b = h[1], c = h[2], d = h[3]; - uint64_t e = h[4], f = h[5], g = h[6], v = h[7]; - for (size_t i = 0; i < 80; i++) { - uint64_t s1 = scr_sha512_rotr(e, 14) ^ scr_sha512_rotr(e, 18) ^ scr_sha512_rotr(e, 41); - uint64_t t1 = v + s1 + ((e & f) ^ (~e & g)) + scr_sha512_k[i] + w[i]; - uint64_t s0 = scr_sha512_rotr(a, 28) ^ scr_sha512_rotr(a, 34) ^ scr_sha512_rotr(a, 39); - uint64_t t2 = s0 + ((a & b) ^ (a & c) ^ (b & c)); - v = g; g = f; f = e; e = d + t1; - d = c; c = b; b = a; a = t1 + t2; + return scr_sha512_x86_state; +} + +__attribute__((target("avx2,bmi2"))) static inline __m128i scr_sha512_ssig0_avx2(__m128i x) { + return _mm_xor_si128(_mm_xor_si128(_mm_or_si128(_mm_srli_epi64(x, 1), _mm_slli_epi64(x, 63)), + _mm_or_si128(_mm_srli_epi64(x, 8), _mm_slli_epi64(x, 56))), + _mm_srli_epi64(x, 7)); +} + +__attribute__((target("avx2,bmi2"))) static inline __m128i scr_sha512_ssig1_avx2(__m128i x) { + return _mm_xor_si128(_mm_xor_si128(_mm_or_si128(_mm_srli_epi64(x, 19), _mm_slli_epi64(x, 45)), + _mm_or_si128(_mm_srli_epi64(x, 61), _mm_slli_epi64(x, 3))), + _mm_srli_epi64(x, 6)); +} + +__attribute__((target("avx512f,avx512vl,bmi2"))) static inline __m128i +scr_sha512_ssig0_avx512(__m128i x) { + return _mm_ternarylogic_epi64(_mm_ror_epi64(x, 1), _mm_ror_epi64(x, 8), _mm_srli_epi64(x, 7), + 0x96); +} + +__attribute__((target("avx512f,avx512vl,bmi2"))) static inline __m128i +scr_sha512_ssig1_avx512(__m128i x) { + return _mm_ternarylogic_epi64(_mm_ror_epi64(x, 19), _mm_ror_epi64(x, 61), _mm_srli_epi64(x, 6), + 0x96); +} + +/* A round for the vector kernels. wk is the schedule word plus its + * constant. Maj(a, b, c) is ((a ^ b) & (b ^ c)) ^ b, where b ^ c is the + * previous round's a ^ b. The empty asm statements keep the compiler from + * reassociating the additions: adding Sigma1(e) last keeps the sum of h, + * wk and Ch off the dependency chain through e. */ +#define SCR_SHA512_PIN(v) __asm__("" : "+r"(v)) +#define SCR_SHA512_ROUND_WK(a, b, c, d, e, f, g, h, wk, ab, bc) \ + do { \ + uint64_t t1 = (h) + (wk); \ + SCR_SHA512_PIN(t1); \ + t1 += (g) ^ ((e) & ((f) ^ (g))); \ + SCR_SHA512_PIN(t1); \ + t1 += SCR_SHA512_BSIG1(e); \ + SCR_SHA512_PIN(t1); \ + (d) += t1; \ + (ab) = (a) ^ (b); \ + uint64_t m = t1 + (((ab) & (bc)) ^ (b)); \ + SCR_SHA512_PIN(m); \ + (h) = m + SCR_SHA512_BSIG0(a); \ + } while (0) + +/* Words 2j and 2j+1 of the schedule, in the ring slot of words 2j-16 and + * 2j-15, and their sum with the round constants into wk. */ +#define SCR_SHA512_SCHEDULE(j, ssig0, ssig1) \ + do { \ + if ((j) < 40) { \ + __m128i s15 = _mm_alignr_epi8(w[((j) + 1) & 7], w[(j) & 7], 8); \ + __m128i s7 = _mm_alignr_epi8(w[((j) + 5) & 7], w[((j) + 4) & 7], 8); \ + __m128i next = _mm_add_epi64(_mm_add_epi64(w[(j) & 7], ssig0(s15)), \ + _mm_add_epi64(s7, ssig1(w[((j) + 7) & 7]))); \ + w[(j) & 7] = next; \ + _mm_store_si128((__m128i *)&wk[(j) * 2], \ + _mm_add_epi64(next, _mm_loadu_si128((const __m128i *)&scr_sha512_k[(j) * 2]))); \ + } \ + } while (0) + +/* Rounds 2i to 2i+7 compute the schedule words 2i+16 to 2i+23. */ +#define SCR_SHA512_VECTOR_BODY(ssig0, ssig1) \ + const __m128i bswap = _mm_set_epi64x(0x08090a0b0c0d0e0fULL, 0x0001020304050607ULL); \ + uint64_t a = state[0], b = state[1], c = state[2], d = state[3]; \ + uint64_t e = state[4], f = state[5], g = state[6], h = state[7]; \ + for (; blocks != 0; blocks--, p += 128) { \ + __m128i w[8]; \ + uint64_t wk[80] __attribute__((aligned(16))); \ + _Pragma("GCC unroll 8") for (int i = 0; i < 8; i++) { \ + w[i] = _mm_shuffle_epi8(_mm_loadu_si128((const __m128i *)(p + i * 16)), bswap); \ + _mm_store_si128((__m128i *)&wk[i * 2], \ + _mm_add_epi64(w[i], _mm_loadu_si128((const __m128i *)&scr_sha512_k[i * 2]))); \ + } \ + uint64_t x = b ^ c, y; \ + _Pragma("GCC unroll 10") for (int i = 0; i < 40; i += 4) { \ + SCR_SHA512_SCHEDULE(i + 8, ssig0, ssig1); \ + SCR_SHA512_ROUND_WK(a, b, c, d, e, f, g, h, wk[2 * i + 0], y, x); \ + SCR_SHA512_ROUND_WK(h, a, b, c, d, e, f, g, wk[2 * i + 1], x, y); \ + SCR_SHA512_SCHEDULE(i + 9, ssig0, ssig1); \ + SCR_SHA512_ROUND_WK(g, h, a, b, c, d, e, f, wk[2 * i + 2], y, x); \ + SCR_SHA512_ROUND_WK(f, g, h, a, b, c, d, e, wk[2 * i + 3], x, y); \ + SCR_SHA512_SCHEDULE(i + 10, ssig0, ssig1); \ + SCR_SHA512_ROUND_WK(e, f, g, h, a, b, c, d, wk[2 * i + 4], y, x); \ + SCR_SHA512_ROUND_WK(d, e, f, g, h, a, b, c, wk[2 * i + 5], x, y); \ + SCR_SHA512_SCHEDULE(i + 11, ssig0, ssig1); \ + SCR_SHA512_ROUND_WK(c, d, e, f, g, h, a, b, wk[2 * i + 6], y, x); \ + SCR_SHA512_ROUND_WK(b, c, d, e, f, g, h, a, wk[2 * i + 7], x, y); \ + } \ + a = state[0] += a; b = state[1] += b; c = state[2] += c; d = state[3] += d; \ + e = state[4] += e; f = state[5] += f; g = state[6] += g; h = state[7] += h; \ } - h[0] += a; h[1] += b; h[2] += c; h[3] += d; - h[4] += e; h[5] += f; h[6] += g; h[7] += v; + +__attribute__((target("avx2,bmi2"), aligned(64))) static void +scr_sha512_blocks_avx2(uint64_t state[8], const unsigned char *p, size_t blocks) { + SCR_SHA512_VECTOR_BODY(scr_sha512_ssig0_avx2, scr_sha512_ssig1_avx2) +} + +__attribute__((target("avx512f,avx512vl,bmi2"), aligned(64))) static void +scr_sha512_blocks_avx512(uint64_t state[8], const unsigned char *p, size_t blocks) { + SCR_SHA512_VECTOR_BODY(scr_sha512_ssig0_avx512, scr_sha512_ssig1_avx512) +} +#endif + + +#if defined(__aarch64__) && (defined(__clang__) || defined(__GNUC__)) && !defined(_WIN32) +/* ── SHA-512 kernel (arm64) ─────────────────────────────────────────── + * On CPUs with the SHA-512 extension (FEAT_SHA512), the blocks run with + * SHA512H, SHA512H2, SHA512SU0 and SHA512SU1; other CPUs use the portable + * blocks. The CPU check runs once and is cached. */ +#include +#if defined(__APPLE__) +#include +#else +#include +#endif +#define SCR_SHA512_ARM 1 +#if defined(__clang__) +#define SCR_SHA512_ARM_TARGET __attribute__((target("sha3"))) +#else +#define SCR_SHA512_ARM_TARGET __attribute__((target("+sha3"))) +#endif + +static int scr_sha512_arm_state; /* 0 unknown, 1 available, 2 absent */ + +static bool scr_sha512_arm(void) { + if (scr_sha512_arm_state == 0) { + bool ok; +#if defined(__APPLE__) + int value = 0; + size_t size = sizeof value; + ok = sysctlbyname("hw.optional.armv8_2_sha512", &value, &size, NULL, 0) == 0 && value != 0; +#else + ok = (getauxval(AT_HWCAP) & (1ul << 21)) != 0; /* HWCAP_SHA512 */ +#endif + scr_sha512_arm_state = ok ? 1 : 2; + } + return scr_sha512_arm_state == 1; +} + +/* Two rounds per step. With the state pairs in the roles (A, B, C, D) = + * (ab, cd, ef, gh), a step computes I = SHA512H(swap(K + W) + D, ext(C, D), + * ext(B, C)), D = SHA512H2(I, B, A) and B += I; the roles then rotate to + * (D, A, B, C). */ +#define SCR_SHA512_ARM_STEP(A, B, C, D, msg, k) \ + do { \ + uint64x2_t kw = vaddq_u64((msg), vld1q_u64(k)); \ + uint64x2_t sum = vaddq_u64(vextq_u64(kw, kw, 1), (D)); \ + uint64x2_t i = vsha512hq_u64(sum, vextq_u64((C), (D), 1), vextq_u64((B), (C), 1)); \ + (D) = vsha512h2q_u64(i, (B), (A)); \ + (B) = vaddq_u64((B), i); \ + } while (0) + +/* Message words 2j and 2j+1 from the eight pairs before them. */ +#define SCR_SHA512_ARM_SCHEDULE(m0, m1, m4, m5, m7) \ + (m0) = vsha512su1q_u64(vsha512su0q_u64((m0), (m1)), (m7), vextq_u64((m4), (m5), 1)) + +SCR_SHA512_ARM_TARGET static void scr_sha512_blocks_arm(uint64_t state[8], const unsigned char *p, + size_t blocks) { + uint64x2_t ab = vld1q_u64(&state[0]), cd = vld1q_u64(&state[2]); + uint64x2_t ef = vld1q_u64(&state[4]), gh = vld1q_u64(&state[6]); + for (; blocks != 0; blocks--, p += 128) { + uint64x2_t ab0 = ab, cd0 = cd, ef0 = ef, gh0 = gh; + uint64x2_t m0 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(p + 0))); + uint64x2_t m1 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(p + 16))); + uint64x2_t m2 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(p + 32))); + uint64x2_t m3 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(p + 48))); + uint64x2_t m4 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(p + 64))); + uint64x2_t m5 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(p + 80))); + uint64x2_t m6 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(p + 96))); + uint64x2_t m7 = vreinterpretq_u64_u8(vrev64q_u8(vld1q_u8(p + 112))); + const uint64_t *k = scr_sha512_k; + for (int i = 0; i < 5; i++, k += 16) { + SCR_SHA512_ARM_STEP(ab, cd, ef, gh, m0, k + 0); + SCR_SHA512_ARM_STEP(gh, ab, cd, ef, m1, k + 2); + SCR_SHA512_ARM_STEP(ef, gh, ab, cd, m2, k + 4); + SCR_SHA512_ARM_STEP(cd, ef, gh, ab, m3, k + 6); + SCR_SHA512_ARM_STEP(ab, cd, ef, gh, m4, k + 8); + SCR_SHA512_ARM_STEP(gh, ab, cd, ef, m5, k + 10); + SCR_SHA512_ARM_STEP(ef, gh, ab, cd, m6, k + 12); + SCR_SHA512_ARM_STEP(cd, ef, gh, ab, m7, k + 14); + if (i == 4) break; + SCR_SHA512_ARM_SCHEDULE(m0, m1, m4, m5, m7); + SCR_SHA512_ARM_SCHEDULE(m1, m2, m5, m6, m0); + SCR_SHA512_ARM_SCHEDULE(m2, m3, m6, m7, m1); + SCR_SHA512_ARM_SCHEDULE(m3, m4, m7, m0, m2); + SCR_SHA512_ARM_SCHEDULE(m4, m5, m0, m1, m3); + SCR_SHA512_ARM_SCHEDULE(m5, m6, m1, m2, m4); + SCR_SHA512_ARM_SCHEDULE(m6, m7, m2, m3, m5); + SCR_SHA512_ARM_SCHEDULE(m7, m0, m3, m4, m6); + } + ab = vaddq_u64(ab, ab0); + cd = vaddq_u64(cd, cd0); + ef = vaddq_u64(ef, ef0); + gh = vaddq_u64(gh, gh0); + } + vst1q_u64(&state[0], ab); + vst1q_u64(&state[2], cd); + vst1q_u64(&state[4], ef); + vst1q_u64(&state[6], gh); +} +#endif + +static void scr_sha512_blocks(uint64_t h[8], const unsigned char *p, size_t blocks) { +#ifdef SCR_SHA_NI + switch (scr_sha512_x86()) { + case 3: + scr_sha512_blocks_avx512(h, p, blocks); + return; + case 2: + scr_sha512_blocks_avx2(h, p, blocks); + return; + default: + break; + } +#endif +#ifdef SCR_SHA512_ARM + if (scr_sha512_arm()) { + scr_sha512_blocks_arm(h, p, blocks); + return; + } +#endif + scr_sha512_blocks_portable(h, p, blocks); } static void scr_sha512_init(ScrSha512Ctx *ctx, bool sha384) { @@ -7456,14 +7746,15 @@ static void scr_sha512_update(ScrSha512Ctx *ctx, const unsigned char *data, size data += take; len -= take; if (ctx->tail_len == 128) { - scr_sha512_block(ctx->h, ctx->tail); + scr_sha512_blocks(ctx->h, ctx->tail, 1); ctx->tail_len = 0; } } - while (len >= 128) { - scr_sha512_block(ctx->h, data); - data += 128; - len -= 128; + if (len >= 128) { + size_t blocks = len / 128; + scr_sha512_blocks(ctx->h, data, blocks); + data += blocks * 128; + len -= blocks * 128; } if (len != 0) { memcpy(ctx->tail, data, len); @@ -7482,8 +7773,7 @@ static size_t scr_sha512_final(const ScrSha512Ctx *source, unsigned char out[64] tail[pad - 1 - b] = (unsigned char)(low >> (8 * b)); tail[pad - 9 - b] = (unsigned char)(high >> (8 * b)); } - scr_sha512_block(ctx.h, tail); - if (pad == 256) scr_sha512_block(ctx.h, tail + 128); + scr_sha512_blocks(ctx.h, tail, pad / 128); size_t len = sha384 ? 48 : 64; for (size_t i = 0; i < len; i++) out[i] = (unsigned char)(ctx.h[i / 8] >> (56 - 8 * (i % 8))); return len; diff --git a/packages/runtime/test/sha-kernels.test.ts b/packages/runtime/test/sha-kernels.test.ts index 724841fb1..6f7759348 100644 --- a/packages/runtime/test/sha-kernels.test.ts +++ b/packages/runtime/test/sha-kernels.test.ts @@ -42,7 +42,7 @@ const runtimeSources = [ ]; // Compiled once with ASan, UBSan and the RC audit. The test runs the SHA-NI and portable -// SHA-1/SHA-256 blocks over the same inputs and checks known vectors. +// SHA-1/SHA-256 blocks and the SHA-512 kernels over the same inputs and checks known vectors. beforeAll(async () => { await mkdir(join(testDir, "build"), { recursive: true }); await execFileAsync("clang", [ @@ -64,7 +64,7 @@ beforeAll(async () => { ]); }, 300_000); -test("SHA-1 and SHA-256: the SHA-NI blocks match the portable blocks", async () => { +test("SHA-1, SHA-256, SHA-384 and SHA-512: the CPU kernels match the portable blocks", async () => { const { stderr } = await execFileAsync(bin, [], { env: { ...process.env, UBSAN_OPTIONS: "halt_on_error=1" }, }); diff --git a/packages/runtime/test/test_sha_kernels.c b/packages/runtime/test/test_sha_kernels.c index 7f854216e..f9f9b9df7 100644 --- a/packages/runtime/test/test_sha_kernels.c +++ b/packages/runtime/test/test_sha_kernels.c @@ -1,9 +1,11 @@ -/* SHA-1 and SHA-256 block kernels (scr_lib.c), built by +/* SHA-1, SHA-256, SHA-384 and SHA-512 block kernels (scr_lib.c), built by * sha-kernels.test.ts. The unit is included directly so the test can run - * the SHA-NI blocks and the portable blocks over the same inputs: known - * vectors, every length from 0 to 1100 bytes, and 1 MiB, each fed whole - * and in 1-, 7-, 64- and 100-byte updates. On CPUs without the SHA - * extensions only the portable blocks run. */ + * each kernel the CPU supports and the portable blocks over the same + * inputs: known vectors, every length from 0 to 1100 bytes, and 1 MiB, + * each fed whole and in 1-, 7-, 64- and 100-byte updates. The SHA-1 and + * SHA-256 kernels use the SHA extensions; the SHA-512 kernels use AVX2 or + * AVX-512VL on x86-64 and the SHA-512 extension on arm64. On CPUs without + * them only the portable blocks run. */ #include "../src/scr_lib.c" #include @@ -45,6 +47,71 @@ static void sha1_of(const unsigned char *data, size_t len, size_t chunk, unsigne scr_sha1_final(&ctx, out); } +static void sha512_of(const unsigned char *data, size_t len, size_t chunk, bool sha384, + unsigned char out[64]) { + ScrSha512Ctx ctx; + scr_sha512_init(&ctx, sha384); + for (size_t at = 0; at < len;) { + size_t take = chunk == 0 || chunk > len - at ? len - at : chunk; + scr_sha512_update(&ctx, data + at, take); + at += take; + } + scr_sha512_final(&ctx, out, sha384); +} + +/* FIPS 180-4 example vectors for SHA-384 and SHA-512. */ +static void vectors512(const char *path) { + unsigned char d[64]; + char text[129]; + static const char two_block[] = + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"; + sha512_of((const unsigned char *)"abc", 3, 0, false, d); + hex(d, 64, text); + check(strcmp(text, "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f") == 0, path, 3, 0); + sha512_of((const unsigned char *)"", 0, 0, false, d); + hex(d, 64, text); + check(strcmp(text, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e") == 0, path, 0, 0); + sha512_of((const unsigned char *)two_block, sizeof two_block - 1, 0, false, d); + hex(d, 64, text); + check(strcmp(text, "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909") == 0, path, sizeof two_block - 1, 0); + sha512_of((const unsigned char *)"abc", 3, 0, true, d); + hex(d, 48, text); + check(strcmp(text, "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7") == 0, path, 3, 0); + sha512_of((const unsigned char *)two_block, sizeof two_block - 1, 0, true, d); + hex(d, 48, text); + check(strcmp(text, "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039") == 0, path, sizeof two_block - 1, 0); + static unsigned char million[1000000]; + memset(million, 'a', sizeof million); + sha512_of(million, sizeof million, 0, false, d); + hex(d, 64, text); + check(strcmp(text, "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b") == 0, path, sizeof million, 0); + sha512_of(million, sizeof million, 0, true, d); + hex(d, 48, text); + check(strcmp(text, "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985") == 0, path, sizeof million, 0); +} + +/* Selects the SHA-512 kernel: 0 the portable blocks, otherwise the CPU's + * kernel. Returns false when the CPU has none. */ +static bool select512(int kernel) { +#ifdef SCR_SHA_NI + scr_sha512_x86_state = 0; + int best = scr_sha512_x86(); + if (kernel > best - 1) return false; + scr_sha512_x86_state = kernel == 0 ? 1 : kernel + 1; + return true; +#elif defined(SCR_SHA512_ARM) + scr_sha512_arm_state = 0; + bool available = scr_sha512_arm(); + if (kernel > (available ? 1 : 0)) return false; + scr_sha512_arm_state = kernel == 0 ? 2 : 1; + return true; +#else + return kernel == 0; +#endif +} + +static const char *const kernel512_names[] = {"portable", "avx2-or-arm", "avx512"}; + static void vectors(const char *path) { unsigned char d[32]; char text[65]; @@ -108,8 +175,31 @@ int main(void) { vectors("ni"); } #endif + /* SHA-512 and SHA-384: every kernel against the portable blocks, and + * the known vectors through each. */ + int kernels512 = 0; + for (int k = 0; k < 3 && select512(k); k++) { + kernels512 = k + 1; + vectors512(kernel512_names[k]); + } + for (int k = 1; k < kernels512; k++) { + for (size_t len = 0; len <= 1100 + 1; len++) { + size_t n = len == 1101 ? BIG : len; + for (size_t c = 0; c < sizeof chunks / sizeof chunks[0]; c++) { + for (int sha384 = 0; sha384 < 2; sha384++) { + unsigned char a[64], b[64]; + select512(k); + sha512_of(data, n, chunks[c], sha384, a); + select512(0); + sha512_of(data, n, chunks[c], sha384, b); + check(memcmp(a, b, 64) == 0, kernel512_names[k], n, chunks[c]); + } + } + } + } free(data); fprintf(stderr, "sha-ni %s\n", ni ? "available" : "absent"); + fprintf(stderr, "sha-512 kernels %d\n", kernels512); fprintf(stderr, "%ld/%ld cases passed\n", total - failed, total); return failed != 0; } diff --git a/tests/corpus/5779-sha512-lengths.ts b/tests/corpus/5779-sha512-lengths.ts new file mode 100644 index 000000000..277408409 --- /dev/null +++ b/tests/corpus/5779-sha512-lengths.ts @@ -0,0 +1,35 @@ +// SHA-384 and SHA-512 over every length from 0 to 520 bytes, fed whole and +// in 1-, 7-, 100- and 128-byte updates, then over 1 MiB, and HMAC-SHA512 +// over a few lengths. Each length's digests are folded into one running +// SHA-512 so the output stays short. The CPU's SHA-512 kernel (vector or +// SHA-512 instructions) and the portable blocks give the same digests. +import { createHash, createHmac } from "node:crypto"; + +const data = Buffer.alloc(1 << 20); +let x = 0x9e3779b9; +for (let i = 0; i < data.length; i++) { + x ^= x << 13; + x ^= x >>> 17; + x ^= x << 5; + data[i] = x & 0xff; +} + +function digest(algorithm: string, input: Buffer, chunk: number): Buffer { + const hash = createHash(algorithm); + if (chunk === 0) hash.update(input); + else for (let at = 0; at < input.length; at += chunk) hash.update(input.subarray(at, at + chunk)); + return hash.digest(); +} + +for (const algorithm of ["sha384", "sha512"]) { + for (const chunk of [0, 1, 7, 100, 128]) { + const fold = createHash("sha512"); + for (let length = 0; length <= 520; length++) fold.update(digest(algorithm, data.subarray(0, length), chunk)); + console.log(algorithm, "chunk", chunk, fold.digest("hex").slice(0, 32)); + } + console.log(algorithm, "1 MiB", digest(algorithm, data, 0).toString("hex").slice(0, 32), digest(algorithm, data, 4096).toString("hex").slice(0, 32)); +} +console.log("abc", createHash("sha512").update("abc").digest("hex")); +for (const length of [0, 1, 111, 112, 127, 128, 129, 255, 256, 1000]) { + console.log("hmac", length, createHmac("sha512", "key").update(data.subarray(0, length)).digest("hex").slice(0, 32)); +} diff --git a/tests/harness/corpus-costs/5779-sha512-lengths.ts.json b/tests/harness/corpus-costs/5779-sha512-lengths.ts.json new file mode 100644 index 000000000..9e04a316c --- /dev/null +++ b/tests/harness/corpus-costs/5779-sha512-lengths.ts.json @@ -0,0 +1 @@ +{"plain":2610,"san":3540} From d02c85a6119ee2b64b61362a2884157a27f937e2 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 21:23:53 -0700 Subject: [PATCH 49/83] Use classes that extend EventEmitter or a stream class as values Naming a class that extends EventEmitter, Readable, Writable, Duplex or Transform as a value was refused ("classes extending builtin bases as values"), and so was a class expression of that kind. The refusal also showed up as a deferred runtime fence at the class declaration, because the checked-dynamic tier reflects every class as a value. A user subclass always constructs through its own or synthesized constructor, whose super call runs the runtime initializer, so the class object's construct thunk serves it like any program class. EventTarget subclasses keep the refusal. Co-Authored-By: Claude Opus 5.5 --- .../src/frontend/lowering/lower-classes.ts | 21 ++----- .../6450-builtin-base-class-values.ts.json | 6 ++ ...51-builtin-base-class-expressions.cjs.json | 6 ++ .../corpus/6450-builtin-base-class-values.ts | 63 +++++++++++++++++++ .../6451-builtin-base-class-expressions.cjs | 33 ++++++++++ 5 files changed, 114 insertions(+), 15 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6450-builtin-base-class-values.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6451-builtin-base-class-expressions.cjs.json create mode 100644 tests/corpus/6450-builtin-base-class-values.ts create mode 100644 tests/corpus/6451-builtin-base-class-expressions.cjs diff --git a/packages/compiler/src/frontend/lowering/lower-classes.ts b/packages/compiler/src/frontend/lowering/lower-classes.ts index 8a5170a7a..81cf09e1f 100644 --- a/packages/compiler/src/frontend/lowering/lower-classes.ts +++ b/packages/compiler/src/frontend/lowering/lower-classes.ts @@ -2212,11 +2212,7 @@ export function collectClassShapeInner( } if (lowerer.collectingExprClasses.has(decl)) { for (let parent = base; parent; parent = parent.base) { - if ( - parent.builtinEmitter || - parent.builtinStream !== undefined || - parent.builtinEventTarget !== undefined - ) { + if (parent.builtinEventTarget !== undefined) { lowerer.unsupported( "SC1090", decl, @@ -5132,16 +5128,11 @@ export function classValueRef(lowerer: Lowerer, info: ClassInfo, blame: ts.Node) loc: locOf(blame), }; } - if ( - lowerer.inheritsBuiltinEmitterCtor(info) || - inheritsBuiltinStreamCtor(lowerer, info) || - eventTargetRootOf(info) !== null || - (() => { - for (let c = info.base; c; c = c.base) - if (c.builtinEmitter || c.builtinStream !== undefined) return true; - return false; - })() - ) { + // Classes extending EventEmitter or a stream class construct through + // their own (or synthesized) `%C.constructor`, whose super call runs the + // runtime initializer, so the class object's construct thunk serves them + // like any program class. EventTarget subclasses keep the refusal. + if (eventTargetRootOf(info) !== null) { lowerer.unsupported( "SC1090", blame, diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6450-builtin-base-class-values.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6450-builtin-base-class-values.ts.json new file mode 100644 index 000000000..bdd8fcbc8 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6450-builtin-base-class-values.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6450-builtin-base-class-values.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6451-builtin-base-class-expressions.cjs.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6451-builtin-base-class-expressions.cjs.json new file mode 100644 index 000000000..34cde5811 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6451-builtin-base-class-expressions.cjs.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6451-builtin-base-class-expressions.cjs" + ], + "diags": [] +} diff --git a/tests/corpus/6450-builtin-base-class-values.ts b/tests/corpus/6450-builtin-base-class-values.ts new file mode 100644 index 000000000..0dafa5d82 --- /dev/null +++ b/tests/corpus/6450-builtin-base-class-values.ts @@ -0,0 +1,63 @@ +// Classes extending Node builtin bases (Writable, Transform, EventEmitter) +// used as values: through a variable, a generic factory, an object table and +// an array, with instanceof against the class and its base. +import { EventEmitter } from "node:events"; +import { Readable, Transform, Writable } from "node:stream"; + +class Sink extends Writable { + readonly lines: string[] = []; + private readonly tag: string; + constructor(tag: string) { + super(); + this.tag = tag; + } + _write(chunk: Buffer, _enc: BufferEncoding, cb: (err?: Error | null) => void): void { + this.lines.push(this.tag + chunk.toString()); + cb(); + } +} + +class Upper extends Transform { + _transform(chunk: Buffer, _enc: BufferEncoding, cb: (err?: Error | null, data?: string) => void): void { + cb(null, chunk.toString().toUpperCase()); + } +} + +class Ticker extends EventEmitter { + count = 0; + tick(): void { + this.count++; + this.emit("tick", this.count); + } +} + +class Plain extends EventEmitter {} + +function build(ctor: new () => T): T { + return new ctor(); +} + +const SinkClass = Sink; +const s = new SinkClass("> "); +s.write("a"); +s.end("b", () => console.log("sink", JSON.stringify(s.lines), s instanceof Sink, s instanceof Writable)); + +const t = build(Ticker); +t.on("tick", (n: number) => console.log("tick", n)); +t.tick(); +t.tick(); +console.log("ticker", t instanceof Ticker, t instanceof EventEmitter, t.count); + +const p = build(Plain); +p.on("x", () => console.log("plain x")); +p.emit("x"); + +const registry = { upper: Upper }; +const u = new registry.upper(); +u.on("data", (d: Buffer) => console.log("upper", d.toString())); +u.end("hello"); + +const kinds = [Ticker, Ticker]; +console.log("names", kinds.map((k) => k.name).join(","), typeof Sink, Sink.name); +const r = Readable.from(["x"]); +console.log("readable", r instanceof Readable); diff --git a/tests/corpus/6451-builtin-base-class-expressions.cjs b/tests/corpus/6451-builtin-base-class-expressions.cjs new file mode 100644 index 000000000..64230a679 --- /dev/null +++ b/tests/corpus/6451-builtin-base-class-expressions.cjs @@ -0,0 +1,33 @@ +// Class expressions extending Node builtin bases (a bundler's output shape). +"use strict"; +const { Readable, Transform } = require("stream"); +const { EventEmitter } = require("events"); + +const Wrap = class extends Readable { + constructor(parts) { + super(); + this.parts = parts; + } + _read() { + const next = this.parts.shift(); + this.push(next === undefined ? null : next); + } +}; +const Bus = class extends EventEmitter {}; +const Lower = class extends Transform { + _transform(chunk, _enc, cb) { + cb(null, chunk.toString().toLowerCase()); + } +}; + +const r = new Wrap(["a", "b"]); +const chunks = []; +r.on("data", (c) => chunks.push(c.toString())); +r.on("end", () => console.log("wrap", chunks.join("+"), r instanceof Readable)); +const bus = new Bus(); +bus.on("ping", (n) => console.log("ping", n)); +bus.emit("ping", 1); +const l = new Lower(); +l.on("data", (d) => console.log("lower", d.toString())); +l.end("HeLLo"); +console.log(Wrap.name, Bus.name, typeof Lower); From 73bd0b53c396c485785f6ad3db0b7caeba76f16e Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 21:23:54 -0700 Subject: [PATCH 50/83] Pass rest-parameter functions into slots with fixed parameters A typed rest-parameter function passed where a function with fixed parameters is expected (`log(...m: string[])` into `(m: string) => void`) was refused. The function adapter now packs the slot's remaining arguments into the rest array, and the value check admits the function wherever that adapter fits. A nested function declaration with a typed rest parameter becomes a value with its own typed-rest signature, converted at each use. Co-Authored-By: Claude Opus 5.5 --- .../language-features/function-values.mdx | 2 +- .../src/frontend/lowering/call-signatures.ts | 26 +++++++ .../lowering/coercions/function-adapters.ts | 72 +++++++++++++++++++ .../6445-rest-function-fixed-slot.ts.json | 6 ++ ...6-rest-function-nested-declaration.ts.json | 6 ++ tests/corpus/6445-rest-function-fixed-slot.ts | 28 ++++++++ .../6446-rest-function-nested-declaration.ts | 26 +++++++ 7 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6445-rest-function-fixed-slot.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6446-rest-function-nested-declaration.ts.json create mode 100644 tests/corpus/6445-rest-function-fixed-slot.ts create mode 100644 tests/corpus/6446-rest-function-nested-declaration.ts diff --git a/docs/content/limitations/language-features/function-values.mdx b/docs/content/limitations/language-features/function-values.mdx index 88422c406..75229ff95 100644 --- a/docs/content/limitations/language-features/function-values.mdx +++ b/docs/content/limitations/language-features/function-values.mdx @@ -1,4 +1,4 @@ - **Function values:** Immutable aliases of overloaded program functions retain per-call overload resolution and identity. A function stored in a slot with a different signature remains the same function for equality, array searches, `length`, and own properties. - Typed rest-parameter functions can be stored and called indirectly. + Typed rest-parameter functions can be stored and called indirectly, including through a slot with fixed parameters, where the slot's arguments arrive in the rest array. Builtin function values have API-specific restrictions; see [Builtin function values](#builtin-function-values). diff --git a/packages/compiler/src/frontend/lowering/call-signatures.ts b/packages/compiler/src/frontend/lowering/call-signatures.ts index 6db1735db..96fbd106b 100644 --- a/packages/compiler/src/frontend/lowering/call-signatures.ts +++ b/packages/compiler/src/frontend/lowering/call-signatures.ts @@ -27,6 +27,7 @@ import { isStreamUndefCallExpr } from "./lower-server.js"; import { nsPathPrefix } from "./lower-namespaces.js"; import { declSymbolOf } from "./lower-modules.js"; import { npmStaticPackageOfPath } from "../npm-static.js"; +import { restPackingAdapterFits } from "./coercions/function-adapters.js"; import { implicitMonoFile, implicitAnyParamSymbolsOf } from "./generic-functions.js"; /** How a parameter participates in CALL-SITE COMPLETION (the frontend @@ -522,6 +523,21 @@ export function requireExactArityValue( // optional/defaulted params spell their `T | undefined` slots (mapType's // completed-signature contract), so omitted trailing args complete with // the undefined arm like any direct call. + // A nested function declaration whose only non-required parameter is a + // typed rest: its value carries the typed-rest ABI its own type spells, + // and every use converts it at its slot (same signature, or the rest + // packing adapter for fixed parameters). + if ( + contextual === null && + ts.isFunctionDeclaration(blame) && + blame.name !== undefined && + funcType.kind === "func" && + funcType.restAbi === "typed" && + shapes.every((s) => s.mode === "required" || s.mode === "rest") + ) { + const own = lowerer.mapTypeOf(lowerer.typeOf(blame.name)); + if (own && typeEquals(own, funcType)) return; + } const target = contextual ? lowerer.checker.getContextualType(contextual) : undefined; const mapped = target ? lowerer.mapTypeOf(target) @@ -582,6 +598,16 @@ export function requireExactArityValue( ) { return; } + // A typed-rest function in a slot with fixed parameters (`log(...m: + // string[])` into `(m: string) => void`): the slot coercion's packing + // adapter collects the slot's arguments into the rest array. + if ( + mappedFn?.kind === "func" && + funcType.kind === "func" && + restPackingAdapterFits(lowerer, funcType, mappedFn) + ) { + return; + } // An uncontextualized npm-static implementation function can still // have a declaration-backed completed ABI: the package's .d.ts // projection supplies the body-facing parameter/return types while diff --git a/packages/compiler/src/frontend/lowering/coercions/function-adapters.ts b/packages/compiler/src/frontend/lowering/coercions/function-adapters.ts index b99c24a15..36908e6f9 100644 --- a/packages/compiler/src/frontend/lowering/coercions/function-adapters.ts +++ b/packages/compiler/src/frontend/lowering/coercions/function-adapters.ts @@ -130,12 +130,84 @@ export function cleanFuncAdaptable( /** Adapt a function value to a destination signature, forwarding and * converting its arguments and result. Unsupported parameter/result pairs * produce checked traps when called; rest signatures remain refused. */ +/** Whether a typed-rest function (`(...m: T[]) => R`) can serve a slot + * with fixed parameters (`(m: T) => void`): the slot's parameters convert + * to the function's fixed parameters, then to the rest element type. */ +export function restPackingAdapterFits( + lowerer: Lowerer, + fromT: IrType & { kind: "func" }, + toT: IrType & { kind: "func" }, +): boolean { + if (fromT.rest !== true || fromT.restAbi !== "typed" || toT.rest === true) return false; + const fixed = fromT.params.length - 1; + const restT = fromT.params[fixed]; + if (restT?.kind !== "array" || toT.params.length < fixed) return false; + for (let i = 0; i < toT.params.length; i++) { + if (!lowerer.coercibleValue(toT.params[i]!, i < fixed ? fromT.params[i]! : restT.elem)) + return false; + } + return toT.ret.kind === "void" || lowerer.coercibleValue(fromT.ret, toT.ret); +} + +/** The adapter for restPackingAdapterFits: forwards the fixed arguments and + * packs the remaining ones into the rest array, as a JavaScript call of the + * function with those arguments does. */ +function restPackingAdapter( + lowerer: Lowerer, + fromT: IrType & { kind: "func" }, + toT: IrType & { kind: "func" }, + loc: SrcLoc, +): string { + const key = `fnrestadapt:${typeKey(fromT)}:${typeKey(toT)}`; + const existing = lowerer.coercions.retags.get(key); + if (existing) return existing; + const name = `%fn.adapt.${lowerer.coercions.retags.size}`; + lowerer.coercions.retags.set(key, name); + lowerer.freshClosureAdapters.add(name); + const fixed = fromT.params.length - 1; + const restT = fromT.params[fixed] as IrType & { kind: "array" }; + const params: IrParam[] = toT.params.map((t, i) => ({ + localId: `a.${i}`, + name: `a${i}`, + type: t, + })); + const arg = (i: number, want: IrType): IrExpr => + lowerer.coerceToExpected( + { kind: "varRef", localId: `a.${i}`, type: toT.params[i]!, loc }, + want, + ); + const args: IrExpr[] = []; + for (let i = 0; i < fixed; i++) args.push(arg(i, fromT.params[i]!)); + const packed: IrExpr[] = []; + for (let i = fixed; i < toT.params.length; i++) packed.push(arg(i, restT.elem)); + args.push({ kind: "arrayLit", elems: packed, type: restT, loc }); + const call: IrExpr = { + kind: "callValue", + callee: { kind: "varRef", localId: "f.0", type: fromT, loc }, + receiver: { kind: "libCall", fn: "dyn.this", args: [], type: DYN, loc }, + args, + type: fromT.ret, + loc, + }; + const body: IrStmt[] = + toT.ret.kind === "void" + ? [ + { kind: "exprStmt", expr: call, loc }, + { kind: "return", value: null, loc }, + ] + : [{ kind: "return", value: lowerer.coerceToExpected(call, toT.ret), loc }]; + lowerer.liftedFns.push(...buildFunctionAdapter(name, fromT, toT, params, [], body, loc)); + return name; +} + export function funcCoerceAdapter( lowerer: Lowerer, fromT: IrType & { kind: "func" }, toT: IrType & { kind: "func" }, loc: SrcLoc, ): string | null { + if (restPackingAdapterFits(lowerer, fromT, toT)) + return restPackingAdapter(lowerer, fromT, toT, loc); if (fromT.rest === true || toT.rest === true) return null; if (fromT.params.length > toT.params.length) return null; // Piece dispositions beyond coercibleValue, all CHECKER-APPROVED diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6445-rest-function-fixed-slot.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6445-rest-function-fixed-slot.ts.json new file mode 100644 index 000000000..b39c2fa38 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6445-rest-function-fixed-slot.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6445-rest-function-fixed-slot.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6446-rest-function-nested-declaration.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6446-rest-function-nested-declaration.ts.json new file mode 100644 index 000000000..41c034395 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6446-rest-function-nested-declaration.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6446-rest-function-nested-declaration.ts" + ], + "diags": [] +} diff --git a/tests/corpus/6445-rest-function-fixed-slot.ts b/tests/corpus/6445-rest-function-fixed-slot.ts new file mode 100644 index 000000000..6c7dbeada --- /dev/null +++ b/tests/corpus/6445-rest-function-fixed-slot.ts @@ -0,0 +1,28 @@ +// A rest-parameter function in a slot with fixed parameters: the slot's +// arguments arrive in the rest array, as in a JavaScript call. +function log(...messages: string[]): void { + console.log(`log(${messages.length}):`, messages.join(" | ")); +} +function tagged(tag: string, ...values: number[]): number { + console.log(tag, values.join(",")); + return values.length; +} + +class Line { + private readonly sink: (m: string) => void; + constructor(sink: (m: string) => void) { + this.sink = sink; + } + emit(m: string): void { + this.sink(m); + } +} + +new Line(log).emit("one"); +const pair: (a: string, b: string) => void = log; +pair("x", "y"); +const none: () => void = log; +none(); +const count: (tag: string, a: number, b: number) => number = tagged; +console.log("count", count("t", 1, 2)); +["a", "b"].forEach((m) => new Line(log).emit(m)); diff --git a/tests/corpus/6446-rest-function-nested-declaration.ts b/tests/corpus/6446-rest-function-nested-declaration.ts new file mode 100644 index 000000000..1b2d7aa17 --- /dev/null +++ b/tests/corpus/6446-rest-function-nested-declaration.ts @@ -0,0 +1,26 @@ +// Nested rest-parameter function declarations passed to a fixed slot. +class Line { + private readonly sink: (m: string) => void; + constructor(sink: (m: string) => void) { + this.sink = sink; + } + emit(m: string): void { + this.sink(m); + } +} +interface Logger { + line: () => Line; + log: (...messages: string[]) => void; +} +function makeLogger(prefix: string): Logger { + return { + line: () => new Line(log), + log, + }; + function log(...messages: string[]) { + console.log(prefix, messages.join(" ")); + } +} +const logger = makeLogger(">"); +logger.log("a", "b"); +logger.line().emit("c"); From d321ea578033b442c7f005261d0dbe444c743cbf Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Mon, 19 Oct 2026 23:52:46 -0500 Subject: [PATCH 51/83] Leave the error marker out of own property names Object.getOwnPropertyNames, Reflect.ownKeys and Object.getOwnPropertyDescriptors on errors held as `any` or `unknown` listed "%error", the key that marks the checked-dynamic error encoding. Node lists only the error's properties. The marker is now left out of own property names; the runtime's own checks still read it. --- .../6401-dyn-error-own-property-names.ts.json | 6 +++ ...02-dyn-error-own-property-names-js.js.json | 6 +++ packages/runtime/src/scr_json.c | 15 ++++++- .../6401-dyn-error-own-property-names.ts | 44 +++++++++++++++++++ .../6402-dyn-error-own-property-names-js.js | 44 +++++++++++++++++++ .../6401-dyn-error-own-property-names.ts.json | 1 + ...02-dyn-error-own-property-names-js.js.json | 1 + 7 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6401-dyn-error-own-property-names.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6402-dyn-error-own-property-names-js.js.json create mode 100644 tests/corpus/6401-dyn-error-own-property-names.ts create mode 100644 tests/corpus/6402-dyn-error-own-property-names-js.js create mode 100644 tests/harness/corpus-costs/6401-dyn-error-own-property-names.ts.json create mode 100644 tests/harness/corpus-costs/6402-dyn-error-own-property-names-js.js.json diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6401-dyn-error-own-property-names.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6401-dyn-error-own-property-names.ts.json new file mode 100644 index 000000000..cf96c4f96 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6401-dyn-error-own-property-names.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6401-dyn-error-own-property-names.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6402-dyn-error-own-property-names-js.js.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6402-dyn-error-own-property-names-js.js.json new file mode 100644 index 000000000..49cf88884 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6402-dyn-error-own-property-names-js.js.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6402-dyn-error-own-property-names-js.js" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_json.c b/packages/runtime/src/scr_json.c index aa331f617..e41c2c37d 100644 --- a/packages/runtime/src/scr_json.c +++ b/packages/runtime/src/scr_json.c @@ -11478,7 +11478,20 @@ ScrDyn *scr_dyn_get_own_property_names(const ScrDyn *value) { scr_dyn_release(table); return out; } - if (value->kind == SCR_DYN_OBJ) return scr_dyn_obj_own_keys(value); + if (value->kind == SCR_DYN_OBJ) { + ScrDyn *keys = scr_dyn_obj_own_keys(value); + if (!scr_dyn_obj_get(value, "%error", 6)) return keys; + /* The checked-dynamic tree's error marker is not a property. */ + ScrDyn *names = scr_dyn_new_arr(); + scr_dyn_arr_reserve(names, keys->v.arr.len); + for (size_t i = 0; i < keys->v.arr.len; i++) { + const ScrStr *key = keys->v.arr.items[i]->v.str; + if (key->len == 6 && memcmp(key->data, "%error", 6) == 0) continue; + scr_dyn_arr_push(names, scr_dyn_retain(keys->v.arr.items[i])); + } + scr_dyn_release(keys); + return names; + } if (value->kind == SCR_DYN_ARR || value->kind == SCR_DYN_STR || value->kind == SCR_DYN_BYTES) { ScrDyn *keys = scr_dyn_new_arr(); size_t length = value->kind == SCR_DYN_ARR ? value->v.arr.len : diff --git a/tests/corpus/6401-dyn-error-own-property-names.ts b/tests/corpus/6401-dyn-error-own-property-names.ts new file mode 100644 index 000000000..1020cf014 --- /dev/null +++ b/tests/corpus/6401-dyn-error-own-property-names.ts @@ -0,0 +1,44 @@ +// The own property names of errors held as `any` or `unknown` match +// Node's: Object.getOwnPropertyNames, Reflect.ownKeys and +// Object.getOwnPropertyDescriptors list `stack`, `message` and the +// properties the error has, in order, and nothing else. + +class PlainSubclass extends Error {} + +function dyn(value: unknown): any { + return value; +} + +function names(label: string, error: any): void { + console.log( + label, + "|", + Object.getOwnPropertyNames(error).join(","), + "|", + Reflect.ownKeys(error).map(String).join(","), + "|", + Object.keys(Object.getOwnPropertyDescriptors(error)).join(","), + ); +} + +names("error", dyn(new Error("plain"))); +names("type error", dyn(new TypeError("bad thing"))); +names("no message", dyn(new RangeError())); +names("cause", dyn(new Error("outer", { cause: new Error("inner") }))); +names("subclass", dyn(new PlainSubclass("sub"))); + +const unknownError: unknown = new SyntaxError("unexpected"); +names("unknown", unknownError); + +const coded = dyn(new Error("with code")); +coded.code = "E_CODE"; +coded.detail = 1; +names("own properties", coded); + +try { + throw new URIError("thrown"); +} catch (error) { + names("caught", error); +} + +console.log("marker", Object.getOwnPropertyNames(dyn(new Error("x"))).some((name) => name.startsWith("%"))); diff --git a/tests/corpus/6402-dyn-error-own-property-names-js.js b/tests/corpus/6402-dyn-error-own-property-names-js.js new file mode 100644 index 000000000..6f6ca7fb7 --- /dev/null +++ b/tests/corpus/6402-dyn-error-own-property-names-js.js @@ -0,0 +1,44 @@ +// In JavaScript sources, the own property names of errors match +// Node's: Object.getOwnPropertyNames, Reflect.ownKeys and +// Object.getOwnPropertyDescriptors list `stack`, `message` and the +// properties the error has, in order, and nothing else. + +class PlainSubclass extends Error {} + +function dyn(value) { + return value; +} + +function names(label, error) { + console.log( + label, + "|", + Object.getOwnPropertyNames(error).join(","), + "|", + Reflect.ownKeys(error).map(String).join(","), + "|", + Object.keys(Object.getOwnPropertyDescriptors(error)).join(","), + ); +} + +names("error", dyn(new Error("plain"))); +names("type error", dyn(new TypeError("bad thing"))); +names("no message", dyn(new RangeError())); +names("cause", dyn(new Error("outer", { cause: new Error("inner") }))); +names("subclass", dyn(new PlainSubclass("sub"))); + +const unknownError = new SyntaxError("unexpected"); +names("unknown", unknownError); + +const coded = dyn(new Error("with code")); +coded.code = "E_CODE"; +coded.detail = 1; +names("own properties", coded); + +try { + throw new URIError("thrown"); +} catch (error) { + names("caught", error); +} + +console.log("marker", Object.getOwnPropertyNames(dyn(new Error("x"))).some((name) => name.startsWith("%"))); diff --git a/tests/harness/corpus-costs/6401-dyn-error-own-property-names.ts.json b/tests/harness/corpus-costs/6401-dyn-error-own-property-names.ts.json new file mode 100644 index 000000000..f85c4901a --- /dev/null +++ b/tests/harness/corpus-costs/6401-dyn-error-own-property-names.ts.json @@ -0,0 +1 @@ +{ "plain": 3175, "san": 4362 } diff --git a/tests/harness/corpus-costs/6402-dyn-error-own-property-names-js.js.json b/tests/harness/corpus-costs/6402-dyn-error-own-property-names-js.js.json new file mode 100644 index 000000000..55012b442 --- /dev/null +++ b/tests/harness/corpus-costs/6402-dyn-error-own-property-names-js.js.json @@ -0,0 +1 @@ +{ "plain": 3056, "san": 4620 } From 619c857fcd6f73c1339d667c5d0d240d836ac813 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 00:09:04 -0500 Subject: [PATCH 52/83] List an error's own name where it was created Node lists an object's own properties in creation order. An Error's own `name` is created by an assignment after construction defines `message` and `cause`, or during construction by a `name` field or a constructor's `this.name = ...`. Errors listed an own `name` before `message`, and instances of Error subclasses listed it before every field, so Object.keys, Object.getOwnPropertyNames and JSON.stringify answered a different order than Node's: `{"name":"E","extra":1}` for `{"extra":1, "name":"E"}`. An error's own-property view now lists `name` after the properties its construction defines. For an Error subclass, the class records which field `name` follows: a `name` field takes its place among the fields, parameter properties first, and `this.name = ...` in a constructor follows the fields created before it, including the ones that constructor assigns in JavaScript sources. When no class of the chain sets `name` during construction, an own `name` follows every other own property. --- .../src/backend/llvm/expr-stream-bridges.ts | 12 +++ .../src/frontend/lowering/error-name-order.ts | 86 +++++++++++++++ .../src/frontend/lowering/lower-classes.ts | 7 ++ .../src/ir/function-references.test.ts | 1 + packages/compiler/src/ir/ir.ts | 5 + .../corpus/6403-error-own-name-order.ts.json | 6 ++ .../6404-error-own-name-order-js.js.json | 6 ++ packages/runtime/src/scr_dyn.h | 1 + packages/runtime/src/scr_json.c | 32 +++++- tests/corpus/6403-error-own-name-order.ts | 102 ++++++++++++++++++ tests/corpus/6404-error-own-name-order-js.js | 84 +++++++++++++++ .../6403-error-own-name-order.ts.json | 1 + .../6404-error-own-name-order-js.js.json | 1 + 13 files changed, 340 insertions(+), 4 deletions(-) create mode 100644 packages/compiler/src/frontend/lowering/error-name-order.ts create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6403-error-own-name-order.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6404-error-own-name-order-js.js.json create mode 100644 tests/corpus/6403-error-own-name-order.ts create mode 100644 tests/corpus/6404-error-own-name-order-js.js create mode 100644 tests/harness/corpus-costs/6403-error-own-name-order.ts.json create mode 100644 tests/harness/corpus-costs/6404-error-own-name-order-js.js.json diff --git a/packages/compiler/src/backend/llvm/expr-stream-bridges.ts b/packages/compiler/src/backend/llvm/expr-stream-bridges.ts index 8a53b5fa8..c6e6bf85d 100644 --- a/packages/compiler/src/backend/llvm/expr-stream-bridges.ts +++ b/packages/compiler/src/backend/llvm/expr-stream-bridges.ts @@ -920,6 +920,18 @@ export function streamTypedRefMaterializeAdapter( B.line(`${copied} = call ptr @scr_dyn_copy_property_descriptors(ptr ${out}, ptr ${bag})`); B.line(`call void @scr_dyn_release_v(ptr ${copied})`); } + // An Error subclass instance's own `name` sits where its class's + // construction creates it: after a field, or after every other + // property when no constructor sets it. + const nameAfter = meta.def.errorNameAfter; + if (meta.root.def.name === "%Error" && !meta.def.runtime && nameAfter !== null) { + host.declare(`declare void @scr_error_dyn_place_name(ptr, ptr, ${host.sizeType})`); + B.line( + nameAfter === undefined + ? `call void @scr_error_dyn_place_name(ptr ${out}, ptr null, ${host.sizeType} 0)` + : `call void @scr_error_dyn_place_name(ptr ${out}, ptr ${host.cstr(nameAfter)}, ${host.sizeType} ${Buffer.byteLength(nameAfter, "utf8")})`, + ); + } B.terminate(`ret ptr ${out}`); } else if (t.kind === "record") { const shape = host.recordsById.get(t.shapeId); diff --git a/packages/compiler/src/frontend/lowering/error-name-order.ts b/packages/compiler/src/frontend/lowering/error-name-order.ts new file mode 100644 index 000000000..8523b47cb --- /dev/null +++ b/packages/compiler/src/frontend/lowering/error-name-order.ts @@ -0,0 +1,86 @@ +import * as ts from "../ts7/adapter.js"; +import { isClassOwnEnumerableFieldName } from "../../ir/ir.js"; +import type { ClassInfo } from "./lower-classes.js"; + +type ErrorClassLink = Pick & { + base: ErrorClassLink | null; +}; + +/** Whether `node` is an assignment to `this.`, and the name. */ +function thisAssignmentName(node: ts.Node): string | null { + if (!ts.isBinaryExpression(node) || node.operatorToken.kind !== ts.SyntaxKind.EqualsToken) + return null; + const target = node.left; + if ( + !ts.isPropertyAccessExpression(target) || + target.expression.kind !== ts.SyntaxKind.ThisKeyword + ) + return null; + return ts.isIdentifier(target.name) ? target.name.text : null; +} + +/** Where an Error subclass instance's own `name` sits among its fields when + * a class of its chain gives it one during construction. A property's + * position is fixed when it is created: a `name` field takes its place + * among the fields (parameter properties first, then the declared ones), + * and `this.name = ...` in a constructor follows the fields created so far, + * including the ones that constructor created before it. The answer is the + * field `name` follows, null when it precedes every field, and undefined + * when no class of the chain sets it during construction. `fields` are the + * instance's own enumerable fields. */ +export function errorNameAfterField( + info: ErrorClassLink, + fields: ReadonlySet, +): string | null | undefined { + const chain: ErrorClassLink[] = []; + for (let current: ErrorClassLink | null | undefined = info; current; current = current.base) + if (current.decl) chain.unshift(current); + const created = new Set(); + let last: string | null = null; + const create = (name: string): void => { + if (!fields.has(name) || created.has(name)) return; + created.add(name); + last = name; + }; + for (const link of chain) { + for (const property of link.paramProps ?? []) create(property.name); + for (const member of link.decl!.members.nodes) { + if (!ts.isPropertyDeclaration(member) || !ts.isIdentifier(member.name)) continue; + if ( + member.modifiers?.nodes.some( + (m) => m.kind === ts.SyntaxKind.StaticKeyword || m.kind === ts.SyntaxKind.DeclareKeyword, + ) + ) + continue; + if (member.name.text === "name") return last; + create(member.name.text); + } + const body = link.ctor?.body; + if (!body) continue; + let found = false; + ts.walkPreorder(body, (node) => { + if (ts.isTypeNode(node)) return "skip"; + if (ts.isFunctionLike(node) && !ts.isArrowFunction(node)) return "skip"; + const name = thisAssignmentName(node); + if (name === "name") { + found = true; + return "stop"; + } + if (name !== null) create(name); + return undefined; + }); + if (found) return last; + } + return undefined; +} + +/** The fields an Error subclass's own-property view lists. */ +export function errorViewFields(fields: readonly { name: string }[]): Set { + return new Set( + fields + .map((field) => field.name) + .filter( + (name) => isClassOwnEnumerableFieldName(name) && name !== "name" && name !== "message", + ), + ); +} diff --git a/packages/compiler/src/frontend/lowering/lower-classes.ts b/packages/compiler/src/frontend/lowering/lower-classes.ts index 8a5170a7a..e4ca6d4b6 100644 --- a/packages/compiler/src/frontend/lowering/lower-classes.ts +++ b/packages/compiler/src/frontend/lowering/lower-classes.ts @@ -70,6 +70,7 @@ import { } from "./instantiation-path.js"; import { appendImplicitUndefinedReturn, hasExplicitJsDocReturn } from "./function-returns.js"; import { bodyReadsArguments } from "../arguments-usage.js"; +import { errorNameAfterField, errorViewFields } from "./error-name-order.js"; import { checkedJsDocumentedParameter, documentedParameter, @@ -4209,6 +4210,12 @@ export function collectClassShapeInner( ...(deferredInitFields.size > 0 ? { deferredInitFields } : {}), ...(escape.escapes ? { constructionEscapes: true as const } : {}), }; + let root: ClassInfo | null = info; + while (root?.base) root = root.base; + if (root !== info && root?.def.name === "%Error") { + const after = errorNameAfterField(info, errorViewFields(info.def.fields)); + if (after !== undefined) info.def.errorNameAfter = after; + } // GENERIC members get their declaring-class backlink now that the // info exists (instance lowering reads it for `this` typing and the // generic-class binding merge). diff --git a/packages/compiler/src/ir/function-references.test.ts b/packages/compiler/src/ir/function-references.test.ts index c2d461e09..8c82b42ba 100644 --- a/packages/compiler/src/ir/function-references.test.ts +++ b/packages/compiler/src/ir/function-references.test.ts @@ -154,6 +154,7 @@ const MODULE_FIELDS: Record>, Role> = { "classes[].jsName": "other", "classes[].genericOf": "other", "classes[].baseValueGlobal": "other", + "classes[].errorNameAfter": "other", "classes[].prototypeDataHelper": "reference", "classes[].instancePrototypeHelper": "reference", "classes[].localCaptures[].name": "other", diff --git a/packages/compiler/src/ir/ir.ts b/packages/compiler/src/ir/ir.ts index b84c0594e..890b33426 100644 --- a/packages/compiler/src/ir/ir.ts +++ b/packages/compiler/src/ir/ir.ts @@ -1406,6 +1406,11 @@ export interface IrClassDef { * bag. Its descriptors preserve presence and insertion order independently * of native layout order; field values remain in their native slots. */ tracksOwnFields?: boolean; + /** Error subclasses: the field an instance's own `name` follows among its + * own properties, or null when it precedes every field. Absent when no + * class of the chain sets `name` during construction, and then an own + * `name` follows every other own property. */ + errorNameAfter?: string | null; /** Stable module symbol identities for public instance layout fields. */ symbolFields?: { field: string; globalId: string }[]; /** RUNTIME-PROVIDED class (the builtin Error hierarchy): the struct, RC diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6403-error-own-name-order.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6403-error-own-name-order.ts.json new file mode 100644 index 000000000..da39be0a0 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6403-error-own-name-order.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6403-error-own-name-order.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6404-error-own-name-order-js.js.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6404-error-own-name-order-js.js.json new file mode 100644 index 000000000..b19997ac3 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6404-error-own-name-order-js.js.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6404-error-own-name-order-js.js" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_dyn.h b/packages/runtime/src/scr_dyn.h index a3d506a10..78efdd129 100644 --- a/packages/runtime/src/scr_dyn.h +++ b/packages/runtime/src/scr_dyn.h @@ -1066,6 +1066,7 @@ extern SCR_TL size_t scr_uncloneable_count; ScrDyn *scr_dyn_from_error(const ScrError *e); void scr_error_register_dyn(const ScrVt *vt, ScrDyn *(*box)(void *)); ScrDyn *scr_error_dyn_fields(const ScrError *e); +void scr_error_dyn_place_name(ScrDyn *view, const char *after, size_t after_len); void scr_error_commit_dyn(ScrError *e, const ScrDyn *view); /* The reverse extraction, riding the same identity cache: a dyn error * that came from a runtime ScrError answers THAT instance (+1; diff --git a/packages/runtime/src/scr_json.c b/packages/runtime/src/scr_json.c index e41c2c37d..ce8ba53ed 100644 --- a/packages/runtime/src/scr_json.c +++ b/packages/runtime/src/scr_json.c @@ -6822,10 +6822,6 @@ ScrDyn *scr_error_dyn_fields(const ScrError *e) { d->v.obj.entries[d->v.obj.len - 1].enumerable = false; /* Node lists `stack` first among an error's own properties. */ scr_error_dyn_add_stack(d); - if (e->name_present) { - scr_dyn_obj_set(d, "name", 4, scr_dyn_new_str(e->name)); - d->v.obj.entries[d->v.obj.len - 1].enumerable = e->name_enumerable; - } if (!e->name_present || !e->message_present) { ScrDyn *prototype = scr_dyn_new_obj(); if (!e->name_present) { @@ -6901,9 +6897,37 @@ ScrDyn *scr_error_dyn_fields(const ScrError *e) { scr_dyn_obj_set(d, "errors", 6, scr_aggregate_errors((ScrError *)e)); d->v.obj.entries[d->v.obj.len - 1].enumerable = false; } + /* An own `name` is assigned after construction defines the others. */ + if (e->name_present) { + scr_dyn_obj_set(d, "name", 4, scr_dyn_new_str(e->name)); + d->v.obj.entries[d->v.obj.len - 1].enumerable = e->name_enumerable; + } return d; } +/* Moves an Error subclass view's own `name` to follow the property + * `after`, or every other property when `after` is NULL: the position + * the class's construction creates it at. Nothing changes when the view + * has no own `name` or no `after` property. */ +void scr_error_dyn_place_name(ScrDyn *view, const char *after, size_t after_len) { + if (view->kind != SCR_DYN_OBJ) return; + ScrDynEntry *entries = view->v.obj.entries; + size_t n = view->v.obj.len, from = n, to = n; + for (size_t i = 0; i < n; i++) { + if (entries[i].key_len == 4 && memcmp(entries[i].key, "name", 4) == 0) from = i; + else if (after && entries[i].key_len == after_len && memcmp(entries[i].key, after, after_len) == 0) to = i; + } + if (from == n || (after && to == n)) return; + size_t at = after ? (to > from ? to : to + 1) : n - 1; + if (at == from) return; + ScrDynEntry name = entries[from]; + memmove(&entries[from], &entries[from + 1], (n - from - 1) * sizeof *entries); + memmove(&entries[at + 1], &entries[at], (n - 1 - at) * sizeof *entries); + entries[at] = name; + free(view->v.obj.index); + view->v.obj.index = NULL; +} + ScrDyn *scr_dyn_from_error(const ScrError *e) { size_t hit = scr_errdyn_find(e, true); if (hit != SIZE_MAX) return scr_dyn_retain(scr_errdyn_cache[hit].dyn); diff --git a/tests/corpus/6403-error-own-name-order.ts b/tests/corpus/6403-error-own-name-order.ts new file mode 100644 index 000000000..25e59bb7c --- /dev/null +++ b/tests/corpus/6403-error-own-name-order.ts @@ -0,0 +1,102 @@ +// An error's own `name` is listed where it was created, as in Node: +// after `message` and `cause`, after the fields created before it, and +// before the fields created after it. A `name` field takes its place among +// the fields, `this.name = ...` in a constructor follows that class's +// fields, and a name assigned after construction follows every other own +// property. Object.keys, JSON.stringify and Object.getOwnPropertyNames +// list the same order, for typed values and values held as `any`. + +class AssignedName extends Error { + extra = 1; + constructor(message: string) { + super(message); + this.name = "AssignedName"; + } +} + +class NameField extends Error { + override name = "NameField"; + extra = 1; +} + +class LaterNameField extends Error { + extra = 1; + override name = "LaterNameField"; + last = 2; +} + +class CodeThenName extends Error { + code: string; + constructor(message: string) { + super(message); + this.code = "E_CODE"; + this.name = "CodeThenName"; + } +} + +class NamedBase extends Error { + constructor(message: string) { + super(message); + this.name = "NamedBase"; + } +} + +class ChildOfNamed extends NamedBase { + child = true; +} + +class FieldBase extends Error { + base = "base"; +} + +class NamedChild extends FieldBase { + child = "child"; + constructor(message: string) { + super(message); + this.name = "NamedChild"; + } +} + +class Unnamed extends Error { + extra = 1; +} + +function dyn(value: unknown): any { + return value; +} + +function show(label: string, error: Error): void { + console.log( + label, + "|", + Object.keys(dyn(error)).join(","), + "|", + JSON.stringify(dyn(error)), + "|", + Object.getOwnPropertyNames(dyn(error)).join(","), + ); +} + +show("assigned", new AssignedName("a")); +show("field", new NameField("b")); +show("later field", new LaterNameField("c")); +show("code then name", new CodeThenName("d")); +show("child of named", new ChildOfNamed("e")); +show("named child", new NamedChild("f")); + +const late = new Unnamed("g"); +late.name = "Late"; +show("assigned later", late); + +const builtin = new TypeError("h", { cause: 1 }); +builtin.name = "Renamed"; +show("builtin", builtin); + +const viaAny = dyn(new RangeError("i")); +viaAny.code = "E_RANGE"; +viaAny.name = "Custom"; +viaAny.after = true; +console.log("through any", Object.keys(viaAny).join(","), JSON.stringify(viaAny)); + +const typed = new AssignedName("j"); +console.log("typed keys", Object.keys(typed).join(","), JSON.stringify({ error: typed })); diff --git a/tests/corpus/6404-error-own-name-order-js.js b/tests/corpus/6404-error-own-name-order-js.js new file mode 100644 index 000000000..0b5ad4e82 --- /dev/null +++ b/tests/corpus/6404-error-own-name-order-js.js @@ -0,0 +1,84 @@ +// In JavaScript sources, an error's own `name` is listed where it was +// created, as in Node: among the properties a constructor assigns, in +// assignment order, after the fields declared before it, and after every +// other own property when it is assigned after construction. + +class AssignedBetween extends Error { + constructor(message) { + super(message); + this.code = "E_CODE"; + this.name = "AssignedBetween"; + this.detail = { at: 1 }; + } +} + +class AssignedFirst extends Error { + constructor(message) { + super(message); + this.name = "AssignedFirst"; + this.status = 500; + } +} + +class DeclaredField extends Error { + kind = "declared"; + constructor(message) { + super(message); + this.name = "DeclaredField"; + this.after = true; + } +} + +class NameField extends Error { + name = "NameField"; + extra = 1; +} + +class Base extends Error { + constructor(message) { + super(message); + this.base = 1; + } +} + +class Derived extends Base { + constructor(message) { + super(message); + this.name = "Derived"; + this.derived = 2; + } +} + +class Unnamed extends Error { + constructor(message) { + super(message); + this.extra = 1; + } +} + +function show(label, error) { + console.log( + label, + "|", + Object.keys(error).join(","), + "|", + JSON.stringify(error), + "|", + Object.getOwnPropertyNames(error).join(","), + ); +} + +show("between", new AssignedBetween("a")); +show("first", new AssignedFirst("b")); +show("declared field", new DeclaredField("c")); +show("name field", new NameField("d")); +show("derived", new Derived("e")); + +const late = new Unnamed("f"); +late.name = "Late"; +show("assigned later", late); + +const builtin = new Error("g", { cause: "reason" }); +builtin.name = "Renamed"; +builtin.code = "E_AFTER"; +show("builtin", builtin); diff --git a/tests/harness/corpus-costs/6403-error-own-name-order.ts.json b/tests/harness/corpus-costs/6403-error-own-name-order.ts.json new file mode 100644 index 000000000..882c13186 --- /dev/null +++ b/tests/harness/corpus-costs/6403-error-own-name-order.ts.json @@ -0,0 +1 @@ +{ "plain": 3289, "san": 4302 } diff --git a/tests/harness/corpus-costs/6404-error-own-name-order-js.js.json b/tests/harness/corpus-costs/6404-error-own-name-order-js.js.json new file mode 100644 index 000000000..05ac342cb --- /dev/null +++ b/tests/harness/corpus-costs/6404-error-own-name-order-js.js.json @@ -0,0 +1 @@ +{ "plain": 3030, "san": 3963 } From 4b82886458e784373908d24ff361141467a4f4a2 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 22:23:28 -0700 Subject: [PATCH 53/83] Keep builtin constructor members refused instead of reading undefined Reading Object, Date, RegExp, Map, Set, Promise, WeakMap and WeakSet as interned function values made every use of those globals a dynamic value, including the receivers of member reads. The interned function models only name, length and constructor, so `Map.prototype`, `Date.prototype.getYear`, `Object.isFrozen` as a value, `Map["prototype"]` and `"prototype" in Map` stopped reaching their SC2020/SC1090 fences and read undefined through the dynamic paths instead (769 Test262 variants went from a refusal to a wrong result). A builtin global read for its members now keeps its member lowerings and fences; only name, length and constructor read through the value. A member read through an alias typed as the constructor's static side (`const D = Date; D.prototype`) reports SC2020 before the generic field read. Passing the value to Object or Reflect statics that reflect on or redefine its members (`Object.hasOwn(Object, ...)`, `Object.defineProperty(Promise, ...)`, `hasOwnProperty.call(Map, ...)`) is refused; identity and the empty enumerable key lists still compile. The interned functions answer Node's lengths (Date 7, RegExp 2, Promise 1). Co-Authored-By: Claude Opus 5.5 --- .../src/frontend/lowering/lower-exprs.ts | 114 +++++++++++++++++- .../builtin-constructor-members.ts.json | 6 + packages/runtime/src/scr_dyn_invoke.c | 5 +- .../builtin-constructor-members.ts | 14 +++ .../builtin-constructor-members.ts.txt | 71 +++++++++++ tests/harness/errors.test.ts | 64 ++++++++++ 6 files changed, 271 insertions(+), 3 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/builtin-constructor-members.ts.json create mode 100644 tests/diagnostics/builtin-constructor-members.ts create mode 100644 tests/harness/__snapshots__/builtin-constructor-members.ts.txt diff --git a/packages/compiler/src/frontend/lowering/lower-exprs.ts b/packages/compiler/src/frontend/lowering/lower-exprs.ts index e391dac20..653f4b9ce 100644 --- a/packages/compiler/src/frontend/lowering/lower-exprs.ts +++ b/packages/compiler/src/frontend/lowering/lower-exprs.ts @@ -1554,7 +1554,9 @@ function lowerExprInner(lowerer: Lowerer, expr: ts.Expression): IrExpr { const canonical = stdlibGlobalNameOf(lowerer, expr) ?? expr.text; // Builtin constructors have real function values in both source // languages (TypeScript reads them as dynamic values). - const builtinValue = builtinGlobalValue( + const builtinValue = builtinGlobalValueAt( + lowerer, + expr, canonical, loc, isJsSourceFile(expr.getSourceFile()), @@ -2440,6 +2442,12 @@ function lowerExprInner(lowerer: Lowerer, expr: ts.Expression): IrExpr { lowerStreamObjectProperty(lowerer, expr) ?? lowerEventObjectProperty(lowerer, expr) ?? lowerer.lowerUnionProperty(expr) ?? + // A builtin constructor reached through another binding (`const D = + // Date; D.prototype`) holds its interned function value, which + // models only name, length and constructor: a static member or the + // prototype that no lowering above claimed keeps the SC2020 fence + // rather than reading undefined through the field and dynamic reads. + fenceBuiltinConstructorStaticRead(lowerer, expr) ?? lowerer.lowerFieldRead(expr); // A union-typed field read narrows like an identifier when the // checker has narrowed this use to one arm. @@ -2487,7 +2495,7 @@ function lowerExprInner(lowerer: Lowerer, expr: ts.Expression): IrExpr { loc, }; return ( - builtinGlobalValue(canonical, loc, true) ?? { + builtinGlobalValueAt(lowerer, expr, canonical, loc, true) ?? { kind: "strLit", value: `[builtin ${canonical}]`, type: STRING, @@ -17090,6 +17098,108 @@ export function builtinGlobalValue(canonical: string, loc: SrcLoc, js = false): return null; } +/** The members the interned builtin function values model (the runtime + * answers each one as Node does); any other member read through them would + * answer undefined, so it keeps its static lowering or its fence. */ +const BUILTIN_VALUE_MEMBERS: ReadonlySet = new Set(["name", "length", "constructor"]); + +/** The Object statics whose answers for an interned builtin function value + * match Node's (identity, and the empty enumerable own-key lists). */ +const BUILTIN_VALUE_REFLECTION: ReadonlySet = new Set(["is", "keys", "values", "entries"]); + +/** The value `builtinGlobalValue` gives the stdlib global `expr` read at its + * use site: null when the use reads the global's members (they keep their + * lowerings and fences), and SC2020 when it is the target of reflection or + * redefinition (`Object.hasOwn(Object, "hasOwn")`, + * `Object.defineProperty(Promise, ...)`, `hasOwnProperty.call(Map, ...)`), + * which would observe or change members the interned value does not + * model. */ +function builtinGlobalValueAt( + lowerer: Lowerer, + expr: ts.Expression, + canonical: string, + loc: SrcLoc, + js: boolean, +): IrExpr | null { + if (readsBuiltinGlobalMembers(expr)) return null; + const value = builtinGlobalValue(canonical, loc, js); + if (value === null) return null; + let node: ts.Node = expr; + while (node.parent !== undefined && ts.isParenthesizedExpression(node.parent)) node = node.parent; + const call = node.parent; + if ( + call !== undefined && + ts.isCallExpression(call) && + call.expression !== node && + ts.isPropertyAccessExpression(call.expression) + ) { + const callee = call.expression; + const owner = stdlibGlobalNameOf(lowerer, callee.expression); + const reflects = + owner === "Reflect" || + (owner === "Object" && !BUILTIN_VALUE_REFLECTION.has(callee.name.text)) || + ((callee.name.text === "call" || callee.name.text === "apply") && + call.arguments.nodes[0] === node && + lowerer.isStdlibSymbol( + lowerer.checker.getSymbolAtLocation( + ts.isPropertyAccessExpression(callee.expression) + ? callee.expression.name + : callee.expression, + ), + )); + if (reflects) { + const name = + owner !== null ? `${owner}.${callee.name.text}` : callee.getText().replace(/\s+/g, ""); + lowerer.noLowering( + `${name}(${canonical}, ...)`, + expr, + "builtin constructor and function values model only name, length and constructor, so reflecting on or redefining their members has no lowering", + ); + } + } + return value; +} + +/** Whether the stdlib global `expr` is read for its members (`Map.prototype`, + * `Date["UTC"]`, `"prototype" in Map`) rather than as a value. Those reads + * keep the global's member lowerings and fences: its interned function value + * models only BUILTIN_VALUE_MEMBERS. */ +function readsBuiltinGlobalMembers(expr: ts.Expression): boolean { + let node: ts.Node = expr; + while (node.parent !== undefined && ts.isParenthesizedExpression(node.parent)) node = node.parent; + const parent = node.parent; + if (parent === undefined) return false; + if (ts.isPropertyAccessExpression(parent) && parent.expression === node) + return !BUILTIN_VALUE_MEMBERS.has(parent.name.text); + if (ts.isElementAccessExpression(parent) && parent.expression === node) return true; + return ( + ts.isBinaryExpression(parent) && + parent.operatorToken.kind === ts.SyntaxKind.InKeyword && + parent.right === node + ); +} + +/** Reports SC2020 when `access` reads a stdlib static member or the + * prototype of a builtin constructor with an interned function value through + * a receiver the checker types as the constructor's static side + * (`MapConstructor`); null otherwise. Calls that lower statically never + * reach the member read. */ +function fenceBuiltinConstructorStaticRead( + lowerer: Lowerer, + access: ts.PropertyAccessExpression, +): null { + if (BUILTIN_VALUE_MEMBERS.has(access.name.text)) return null; + const symbol = lowerer.typeOf(access.expression).getSymbol(); + if ( + symbol !== undefined && + symbol.name.endsWith("Constructor") && + GLOBAL_CONSTRUCTOR_VALUES.has(symbol.name.slice(0, -"Constructor".length)) && + lowerer.isStdlibSymbol(symbol) + ) + lowerer.stdlibMemberFence(access); + return null; +} + /** Object.prototype members a plain record inherits, read as values. */ const OBJECT_PROTOTYPE_VALUE_READS: ReadonlySet = new Set([ "constructor", diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/builtin-constructor-members.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/builtin-constructor-members.ts.json new file mode 100644 index 000000000..ae3a963f3 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/builtin-constructor-members.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/diagnostics/builtin-constructor-members.ts" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_dyn_invoke.c b/packages/runtime/src/scr_dyn_invoke.c index 5b6491b83..d03b7381e 100644 --- a/packages/runtime/src/scr_dyn_invoke.c +++ b/packages/runtime/src/scr_dyn_invoke.c @@ -1846,8 +1846,11 @@ static ScrDyn *dyn_global_ctor(const char *name) { scr_atexit(dyn_global_ctors_cleanup); } if (!dyn_global_ctors[i]) { + /* Each constructor's own length: Object 1, Date 7, RegExp 2, Promise 1, + * the collections 0. */ + uint32_t length = i == 0 || i == 5 ? 1 : i == 1 ? 7 : i == 2 ? 2 : 0; dyn_global_ctors[i] = scr_dyn_new_func(scr_closure_new((void *)dyn_global_ctor_names[i], 0), - dyn_global_ctor_call, i == 0 ? 1 : 0, dyn_s6, + dyn_global_ctor_call, length, dyn_s6, dyn_global_ctor_names[i]); } return scr_dyn_retain(dyn_global_ctors[i]); diff --git a/tests/diagnostics/builtin-constructor-members.ts b/tests/diagnostics/builtin-constructor-members.ts new file mode 100644 index 000000000..294fe397b --- /dev/null +++ b/tests/diagnostics/builtin-constructor-members.ts @@ -0,0 +1,14 @@ +// Builtin constructors have interned function values, but those values model +// only name, length and constructor. Every other static member or prototype +// read as a value reports SC2020 at the read, whether it is spelled directly, +// through globalThis or through an alias; it never reads undefined. +const mapPrototype = Map.prototype; +const datePrototype = Date.prototype; +const regExpPrototype = RegExp.prototype; +const promisePrototype = Promise.prototype; +const weakSetPrototype = WeakSet.prototype; +const frozen = Object.isFrozen; +const setPrototype = globalThis.Set.prototype; +const D = Date; +const aliasPrototype = D.prototype; +// End with a nonblank context line so diagnostic snapshots carry no trailing whitespace. diff --git a/tests/harness/__snapshots__/builtin-constructor-members.ts.txt b/tests/harness/__snapshots__/builtin-constructor-members.ts.txt new file mode 100644 index 000000000..c0e45edea --- /dev/null +++ b/tests/harness/__snapshots__/builtin-constructor-members.ts.txt @@ -0,0 +1,71 @@ +builtin-constructor-members.ts:5:22 - error SC2020: 'Map.prototype' is part of the standard library types but has no scriptc lowering yet + + 4 | // through globalThis or through an alias; it never reads undefined. + 5 | const mapPrototype = Map.prototype; + | ^~~~~~~~~~~~~ + 6 | const datePrototype = Date.prototype; + + hint: prototype objects are not values here (method lookup is static) — call the method on an instance instead + +builtin-constructor-members.ts:6:23 - error SC2020: 'Date.prototype' is part of the standard library types but has no scriptc lowering yet + + 5 | const mapPrototype = Map.prototype; + 6 | const datePrototype = Date.prototype; + | ^~~~~~~~~~~~~~ + 7 | const regExpPrototype = RegExp.prototype; + + hint: prototype objects are not values here (method lookup is static) — call the method on an instance instead + +builtin-constructor-members.ts:7:25 - error SC2020: 'RegExp.prototype' is part of the standard library types but has no scriptc lowering yet + + 6 | const datePrototype = Date.prototype; + 7 | const regExpPrototype = RegExp.prototype; + | ^~~~~~~~~~~~~~~~ + 8 | const promisePrototype = Promise.prototype; + + hint: prototype objects are not values here (method lookup is static) — call the method on an instance instead + +builtin-constructor-members.ts:8:26 - error SC2020: 'Promise.prototype' is part of the standard library types but has no scriptc lowering yet + + 7 | const regExpPrototype = RegExp.prototype; + 8 | const promisePrototype = Promise.prototype; + | ^~~~~~~~~~~~~~~~~ + 9 | const weakSetPrototype = WeakSet.prototype; + + hint: prototype objects are not values here (method lookup is static) — call the method on an instance instead + +builtin-constructor-members.ts:9:26 - error SC2020: 'WeakSet.prototype' is part of the standard library types but has no scriptc lowering yet + + 8 | const promisePrototype = Promise.prototype; + 9 | const weakSetPrototype = WeakSet.prototype; + | ^~~~~~~~~~~~~~~~~ + 10 | const frozen = Object.isFrozen; + + hint: prototype objects are not values here (method lookup is static) — call the method on an instance instead + +builtin-constructor-members.ts:10:16 - error SC2020: 'Object.isFrozen' is part of the standard library types but has no scriptc lowering yet + + 9 | const weakSetPrototype = WeakSet.prototype; + 10 | const frozen = Object.isFrozen; + | ^~~~~~~~~~~~~~~ + 11 | const setPrototype = globalThis.Set.prototype; + + hint: the type checker sees the full standard library, but only the supported surface compiles (https://scriptc.dev/limitations) + +builtin-constructor-members.ts:11:22 - error SC2020: 'SetConstructor.prototype' is part of the standard library types but has no scriptc lowering yet + + 10 | const frozen = Object.isFrozen; + 11 | const setPrototype = globalThis.Set.prototype; + | ^~~~~~~~~~~~~~~~~~~~~~~~ + 12 | const D = Date; + + hint: prototype objects are not values here (method lookup is static) — call the method on an instance instead + +builtin-constructor-members.ts:13:24 - error SC2020: 'DateConstructor.prototype' is part of the standard library types but has no scriptc lowering yet + + 12 | const D = Date; + 13 | const aliasPrototype = D.prototype; + | ^~~~~~~~~~~ + 14 | // End with a nonblank context line so diagnostic snapshots carry no trailing whitespace. + + hint: prototype objects are not values here (method lookup is static) — call the method on an instance instead \ No newline at end of file diff --git a/tests/harness/errors.test.ts b/tests/harness/errors.test.ts index 92ac8f5ab..869ed1a80 100644 --- a/tests/harness/errors.test.ts +++ b/tests/harness/errors.test.ts @@ -1130,3 +1130,67 @@ console.log(count(require("./lib.cjs"))); ); }); }); + +describe("builtin constructor values (JS lane)", () => { + test("members without a lowering stay refused instead of reading undefined", async () => { + // Builtin constructors read as interned function values that model only + // name, length and constructor. Every other static member or prototype + // read (direct, bracketed, `in`, through an alias or globalThis), and + // reflection on or redefinition of their members, must stay a located + // SC2020/SC1090 refusal, never an undefined or otherwise wrong value. + // Test262 lost 769 refusals to undefined reads before this was pinned. + const reads = [ + ["Map.prototype", "Map.prototype"], + ["Date.prototype", "Date.prototype"], + ["RegExp.prototype", "RegExp.prototype"], + ["Set.prototype", "Set.prototype"], + ["Promise.prototype", "Promise.prototype"], + ["WeakMap.prototype", "WeakMap.prototype"], + ["WeakSet.prototype", "WeakSet.prototype"], + ["Date.prototype.getYear", "Date.prototype.getYear"], + ["Map.prototype.constructor", "Map.prototype.constructor"], + ["Object.isFrozen", "Object.isFrozen"], + [ + "getOwnPropertyDescriptor(Map.prototype)", + 'Object.getOwnPropertyDescriptor(Map.prototype, "size")', + ], + ["isExtensible(Date.prototype)", "Object.isExtensible(Date.prototype)"], + ["Map['prototype']", 'Map["prototype"]'], + ["'prototype' in Map", '"prototype" in Map'], + ["globalThis.Map.prototype", "globalThis.Map.prototype"], + ["Map alias prototype", "MapAlias.prototype"], + ["Date alias prototype", "DateAlias.prototype"], + ["Object alias isFrozen", "ObjectAlias.isFrozen"], + ["Object.hasOwn(Object)", 'Object.hasOwn(Object, "hasOwn")'], + ["getOwnPropertyNames(Date)", "Object.getOwnPropertyNames(Date)"], + ["defineProperty(Promise)", 'Object.defineProperty(Promise, "resolve", { value: 1 })'], + ["hasOwnProperty.call(Map)", 'Object.prototype.hasOwnProperty.call(Map, "groupBy")'], + ] as const; + const source = `"use strict"; +const MapAlias = Map; +const DateAlias = Date; +const { Object: ObjectAlias } = globalThis; +function report(label, e) { + const m = /\\[(SC\\d{4}) at [^\\]]*:(\\d+)\\]$/.exec(e.message); + console.log(label, m ? "refused " + m[1] + " line " + m[2] : "threw " + e.message); +} +${reads.map(([label, read]) => `try { const value = ${read}; console.log(${JSON.stringify(label)}, "read", typeof value); } catch (e) { report(${JSON.stringify(label)}, e); }`).join("\n")} +console.log(Date.length, RegExp.length, Promise.length, Object.length, Map.length, Set.length); +console.log(Date.name, Map.name, Map.constructor === Function, Object.constructor === Function); +`; + const r = await compileAndRun("builtin-constructor-members", source, "cjs"); + expect(r.stderr).toBe(""); + expect(r.exitCode).toBe(0); + const lines = r.stdout.trimEnd().split("\n"); + expect(lines.slice(0, reads.length)).toEqual( + reads.map(([label], i) => + expect.stringMatching( + new RegExp( + `^${label.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")} refused SC(2020|1090) line ${i + 9}$`, + ), + ), + ), + ); + expect(lines.slice(reads.length)).toEqual(["7 2 1 1 0 0", "Date Map true true"]); + }); +}); From ba23a111105c1d3dbd99caaab7d79fd0c7935ee4 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 22:44:14 -0700 Subject: [PATCH 54/83] Format the npm case filter Co-Authored-By: Claude Opus 5.5 --- tests/harness/npm-cases.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/harness/npm-cases.ts b/tests/harness/npm-cases.ts index f77807d83..01718abf9 100644 --- a/tests/harness/npm-cases.ts +++ b/tests/harness/npm-cases.ts @@ -24,7 +24,9 @@ export function npmCases(fixturesRoot: string): NpmCase[] { // consumers exercise the static rewrite's surface (lexer-visible // names the shipped .d.ts never declares, the __toESM interop // family), so they stay out of the flagless island lane by design. - .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-7]|2571|258[0-4])-[^/]+\/main\.ts$/.test(entry)) + .filter( + (entry) => !/\/(246[5-9]|255[6-9]|256[0-7]|2571|258[0-4])-[^/]+\/main\.ts$/.test(entry), + ) .map((entry) => ({ name: entry.split("/").at(-2)!, entry, From 4b4f80581e3267a5c759a7c477d730d79311e52d Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 22:44:14 -0700 Subject: [PATCH 55/83] Format the npm case filter Co-Authored-By: Claude Opus 5.5 --- tests/harness/npm-cases.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/harness/npm-cases.ts b/tests/harness/npm-cases.ts index 1d15ddc6b..ef46a59de 100644 --- a/tests/harness/npm-cases.ts +++ b/tests/harness/npm-cases.ts @@ -24,7 +24,9 @@ export function npmCases(fixturesRoot: string): NpmCase[] { // consumers exercise the static rewrite's surface (lexer-visible // names the shipped .d.ts never declares, the __toESM interop // family), so they stay out of the flagless island lane by design. - .filter((entry) => !/\/(246[5-9]|255[6-9]|256[0-7]|2571|258[0-2])-[^/]+\/main\.ts$/.test(entry)) + .filter( + (entry) => !/\/(246[5-9]|255[6-9]|256[0-7]|2571|258[0-2])-[^/]+\/main\.ts$/.test(entry), + ) .map((entry) => ({ name: entry.split("/").at(-2)!, entry, From 8c5d65212aade390992b034f11cb24da2495af60 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 23:11:39 -0700 Subject: [PATCH 56/83] Throw Node's ReferenceError for __dirname in a JavaScript ES module The read used to defer a refusal, which kept the corpus program from compiling statically. It now throws Node's catchable ReferenceError ("__dirname is not defined"), and typeof answers "undefined" as in Node. Co-Authored-By: Claude Opus 5.5 --- .../lowering/expressions/identifiers.ts | 18 ++++++++++-------- .../frontend/lowering/expressions/typeof.ts | 17 ++++++++++++++++- tests/corpus/6429-esm-js-dirname.mjs | 6 ++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/packages/compiler/src/frontend/lowering/expressions/identifiers.ts b/packages/compiler/src/frontend/lowering/expressions/identifiers.ts index 5463502eb..1d4755467 100644 --- a/packages/compiler/src/frontend/lowering/expressions/identifiers.ts +++ b/packages/compiler/src/frontend/lowering/expressions/identifiers.ts @@ -352,9 +352,9 @@ export function lowerIdentifierExpr(lowerer: Lowerer, expr: ts.Identifier, loc: // (Node's exact values when the same tree runs in place; WASI uses // the equivalent guest-visible path; locals shadowing the names // resolved above), the same strings as import.meta.dirname/filename. - // In a JavaScript ES module Node never defines them (ReferenceError), - // so they fence there. A .ts/.tsx file keeps them even when Node's - // strip-types rule would load it as ESM: TypeScript projects are + // In a JavaScript ES module Node never defines them, so the read throws + // Node's catchable ReferenceError. A .ts/.tsx file keeps them even when + // Node's strip-types rule would load it as ESM: TypeScript projects are // commonly built as CommonJS bundles (tsc, esbuild, tsx) where these // globals exist, and @types/node declares them for every file. if ( @@ -363,11 +363,13 @@ export function lowerIdentifierExpr(lowerer: Lowerer, expr: ts.Identifier, loc: ) { const sf = expr.getSourceFile(); if (isNodeEsmFile(sf, lowerer.program) && !/\.tsx?$/.test(sf.fileName)) { - lowerer.unsupported( - "SC1090", - expr, - `'${expr.text}' in an ES module (Node defines the CJS module globals only in CommonJS modules)`, - ); + return { + kind: "libCall", + fn: "global.undefRead", + args: [{ kind: "strLit", value: expr.text, type: STRING, loc }], + type: STRING, + loc, + }; } // The WASI runner mounts the host cwd at guest `/` and the host temp // directory at `/tmp`. Bake the matching GUEST spelling so resource diff --git a/packages/compiler/src/frontend/lowering/expressions/typeof.ts b/packages/compiler/src/frontend/lowering/expressions/typeof.ts index bed929362..c56ef4097 100644 --- a/packages/compiler/src/frontend/lowering/expressions/typeof.ts +++ b/packages/compiler/src/frontend/lowering/expressions/typeof.ts @@ -28,7 +28,7 @@ import { typeofAnswer, } from "../lower-exprs.js"; import { tryLowerExpression } from "./try-lower-expression.js"; -import { isJsSourceFile } from "../../program.js"; +import { isJsSourceFile, isNodeEsmFile } from "../../program.js"; import { isSafeToDiscard, isSafeToRepeat } from "./evaluation-safety.js"; import { varRef } from "../../../ir/build.js"; import { isExecExceptionType, refuseExecExceptionCode } from "../builtins/errors.js"; @@ -59,6 +59,21 @@ export function lowerTypeofExpr(lowerer: Lowerer, expr: ts.TypeOfExpression, loc } if (lowerer.isStdlibGlobal(expr.expression, "fetch")) return { kind: "strLit", value: "function", type: STRING, loc }; + // `__dirname` / `__filename` in a JavaScript ES module, where Node never + // defines them: typeof answers "undefined" instead of throwing. + { + let operand = expr.expression; + while (ts.isParenthesizedExpression(operand)) operand = operand.expression; + if ( + ts.isIdentifier(operand) && + (operand.text === "__dirname" || operand.text === "__filename") && + lowerer.isStdlibGlobal(operand, operand.text) + ) { + const sf = operand.getSourceFile(); + if (isNodeEsmFile(sf, lowerer.program) && !/\.tsx?$/.test(sf.fileName)) + return { kind: "strLit", value: "undefined", type: STRING, loc }; + } + } if (isNavigatorGlobal(lowerer, expr.expression)) return { kind: "strLit", value: "object", type: STRING, loc }; const performance = lowerPerfHooksTypeof(lowerer, expr.expression); diff --git a/tests/corpus/6429-esm-js-dirname.mjs b/tests/corpus/6429-esm-js-dirname.mjs index 450544dd7..2ecc4088a 100644 --- a/tests/corpus/6429-esm-js-dirname.mjs +++ b/tests/corpus/6429-esm-js-dirname.mjs @@ -7,3 +7,9 @@ try { console.log("no __dirname:", error instanceof Error); } console.log(import.meta.filename.endsWith("6429-esm-js-dirname.mjs")); +console.log(typeof __dirname, typeof __filename); +try { + console.log(__filename.length); +} catch (error) { + console.log(error.name, error.message); +} From 5e65642fcf2802a4377be8c6efaab6aaed3c9b41 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 22:17:17 -0700 Subject: [PATCH 57/83] Compile mixed-tuple Promise.all, Boolean callbacks, enum key lookups and nullable regex receivers - `Promise.all([p, q, r])` over different promise types (the tuple overload) lowers in TypeScript through one interned async helper that awaits the runtime combinator over the same promises (widened to Promise, a type-only view), so settlement order and rejection handling are Node's, then reads each fulfilled entry into the tuple. Entries keep their identity; plain scalar entries pass through, and a checked-dynamic entry joins through the promise it holds. - The ambient `Boolean` as an `every`/`some`/`find`/`findIndex`/`map` callback in TypeScript is the interned one-parameter truthiness closure `filter(Boolean)` already used, and `filter(Boolean)` accepts checked-dynamic elements (runtime truthiness). - `E[key]` with a runtime string key answers the named member's constant, or undefined for a name no member carries; a numeric member's reverse key and inherited Object.prototype names throw a named runtime refusal. - Regex-argument `replace`/`replaceAll`/`split`/`search`/`matchAll` on a `string | undefined` receiver take the string lowering and throw Node's "Cannot read properties of undefined (reading 'replace')". Co-Authored-By: Claude Opus 5.5 --- .../frontend/lowering/builtins/promises.ts | 173 +++++++++++++++++- .../lowering/containers/array-callbacks.ts | 32 +--- .../lowering/containers/callback-arguments.ts | 60 ++++++ .../lowering/containers/string-and-regexp.ts | 81 +++++++- .../src/frontend/lowering/lower-enums.ts | 115 +++++++++++- .../corpus/6480-boolean-callbacks.ts.json | 6 + .../corpus/6481-enum-runtime-keys.ts.json | 6 + .../corpus/6482-promise-all-tuples.ts.json | 6 + .../6483-nullable-regex-receivers.ts.json | 6 + tests/corpus/6480-boolean-callbacks.ts | 34 ++++ tests/corpus/6481-enum-runtime-keys.ts | 29 +++ tests/corpus/6482-promise-all-tuples.ts | 76 ++++++++ tests/corpus/6483-nullable-regex-receivers.ts | 23 +++ tests/diagnostics/promise-all-fences.ts | 17 +- .../__snapshots__/promise-all-fences.ts.txt | 27 +-- 15 files changed, 628 insertions(+), 63 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6480-boolean-callbacks.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6481-enum-runtime-keys.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6482-promise-all-tuples.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6483-nullable-regex-receivers.ts.json create mode 100644 tests/corpus/6480-boolean-callbacks.ts create mode 100644 tests/corpus/6481-enum-runtime-keys.ts create mode 100644 tests/corpus/6482-promise-all-tuples.ts create mode 100644 tests/corpus/6483-nullable-regex-receivers.ts diff --git a/packages/compiler/src/frontend/lowering/builtins/promises.ts b/packages/compiler/src/frontend/lowering/builtins/promises.ts index 2656da18b..5d4a2e3f2 100644 --- a/packages/compiler/src/frontend/lowering/builtins/promises.ts +++ b/packages/compiler/src/frontend/lowering/builtins/promises.ts @@ -1,15 +1,18 @@ import { checkedPromiseAll } from "../checked-promise-all.js"; import { InternalCompilerError } from "../../../errors.js"; import * as ts from "../../ts7/adapter.js"; -import { type Lowerer } from "../lowerer.js"; +import { newFnCtx, type Lowerer } from "../lowerer.js"; +import { varRef } from "../../../ir/build.js"; import { maybePromiseResolve, maybePromiseUnionOf } from "../maybe-promise.js"; import { isJsSourceFile, locOf } from "../../program.js"; import { + BOOL, DYN, type IrExpr, type IrFunction, type IrStmt, type IrType, + UNDEFINED_T, VOID, arrayOf, isUnitType, @@ -17,6 +20,170 @@ import { typeKey, } from "../../../ir/ir.js"; +/** `Promise.all([p, q, r])` over DIFFERENT promise types — the checker's + * tuple overload, `Promise<[A, B, C]>`. One interned async helper per + * entry/result signature takes the entries as arguments, awaits the + * runtime combinator over the same promises (each widened to + * Promise, a type-only view), so settlement is Node's exactly: the + * first rejection in settlement order wins and every later one counts as + * handled; then it reads each (now fulfilled) entry and returns the + * tuple. Entries keep their identity — no value converts through the + * checked-dynamic tree. Non-promise entries (numbers, strings, booleans, + * unit values — never thenable) pass straight into their slot; a + * checked-dynamic entry (a JavaScript function's result) joins through + * the promise it holds, or a promise fulfilled with its plain value. The tuple + * fulfills one microtask turn after the combinator does. Null when the + * result type or an entry has no typed home here. */ +function lowerTuplePromiseAll( + lowerer: Lowerer, + call: ts.CallExpression, + elems: IrExpr[], +): IrExpr | null { + const resultT = lowerer.mapTypeOf(lowerer.typeOf(call)); + if (resultT?.kind !== "promise" || resultT.inner.kind !== "record") return null; + const shape = lowerer.shapes.get(resultT.inner.shapeId); + if (!shape?.tuple || shape.fields.length !== elems.length) return null; + const plainValue = (t: IrType): boolean => + t.kind === "f64" || t.kind === "string" || t.kind === "bool" || isUnitType(t); + // A checked-dynamic entry (a JavaScript function's result) may hold a + // promise or a plain value at run time; it joins through its promise view. + const settles = (t: IrType): boolean => t.kind === "promise" || t.kind === "dyn"; + if (!elems.some((e) => settles(e.type))) return null; + if (!elems.every((e) => settles(e.type) || plainValue(e.type))) return null; + const loc = locOf(call); + const key = `${elems.map((e) => typeKey(e.type)).join(",")}->${typeKey(resultT.inner)}`; + let name = lowerer.arrHofHelpers.get(`promise.allTuple:${key}`); + if (name === undefined) { + name = `%promise.allTuple.${lowerer.arrHofHelpers.size}`; + lowerer.arrHofHelpers.set(`promise.allTuple:${key}`, name); + const ctx = newFnCtx(false, null, null, resultT.inner); + ctx.isAsync = true; + lowerer.fnStack.push(ctx); + try { + const params = elems.map((e, i) => ({ + localId: `entry.${i}`, + name: `entry${i}`, + type: e.type, + })); + const ref = (i: number): IrExpr => varRef(`entry.${i}`, elems[i]!.type, loc); + const dynPromise: IrType = { kind: "promise", inner: DYN }; + const body: IrStmt[] = []; + // Each dyn entry's promise view: the promise it holds, or a promise + // fulfilled with the plain value (Promise.resolve's rule). + const views = new Map(); + elems.forEach((e, i) => { + if (e.type.kind !== "dyn") return; + const view = lowerer.declareHiddenLocal("%allEntry", dynPromise); + body.push({ + kind: "varDecl", + localId: view.id, + init: { + kind: "ternary", + cond: { kind: "dynTest", test: "promise", value: ref(i), type: BOOL, loc }, + then: { kind: "dynCheck", value: ref(i), type: dynPromise, loc }, + else_: { + kind: "intrinsic", + name: "promise.resolve", + args: [ref(i)], + type: dynPromise, + loc, + }, + type: dynPromise, + loc, + }, + loc, + }); + views.set(i, varRef(view.id, dynPromise, loc)); + }); + const widened: IrExpr[] = []; + elems.forEach((e, i) => { + const view = views.get(i); + if (view !== undefined) { + widened.push({ + kind: "promiseVoidWiden", + value: view, + type: { kind: "promise", inner: VOID }, + loc, + }); + return; + } + if (e.type.kind !== "promise") return; + widened.push( + e.type.inner.kind === "void" + ? ref(i) + : { + kind: "promiseVoidWiden", + value: ref(i), + type: { kind: "promise", inner: VOID }, + loc, + }, + ); + }); + const settled: IrExpr = { + kind: "awaitExpr", + value: { + kind: "intrinsic", + name: "promise.all", + args: [ + { + kind: "arrayLit", + elems: widened, + type: arrayOf({ kind: "promise", inner: VOID }), + loc, + }, + ], + type: { kind: "promise", inner: VOID }, + loc, + }, + type: VOID, + loc, + }; + const fields = shape.fields.map((field, i) => { + const entry = elems[i]!; + let value: IrExpr; + const view = views.get(i); + if (view !== undefined) { + value = { kind: "awaitExpr", value: view, type: DYN, loc }; + } else if (entry.type.kind === "promise") { + value = + entry.type.inner.kind === "void" + ? { kind: "unitLit", unit: "undefined", type: UNDEFINED_T, loc } + : { kind: "awaitExpr", value: ref(i), type: entry.type.inner, loc }; + } else { + value = ref(i); + } + return { name: field.name, value: lowerer.coerceToExpected(value, field.type) }; + }); + body.push( + { kind: "exprStmt", expr: settled, loc }, + { + kind: "return", + value: { kind: "recordLit", fields, type: resultT.inner, loc }, + loc, + }, + ); + lowerer.liftedFns.push({ + name, + params, + returnType: resultT.inner, + locals: [ + ...params.map((p) => ({ id: p.localId, name: p.name, type: p.type, mutable: false })), + ...lowerer.ctx.locals, + ], + body, + async: true, + loc, + }); + } catch (e) { + lowerer.arrHofHelpers.delete(`promise.allTuple:${key}`); + throw e; + } finally { + lowerer.fnStack.pop(); + } + } + return { kind: "call", callee: name, args: elems, type: resultT, loc }; +} + /** `Promise.race([...])` on THE Promise global: the entries lower * individually (the array never materializes — promise-element arrays * have no representation) into a promise.race intrinsic; the result @@ -140,6 +307,10 @@ export function lowerPromiseStaticCall( return { kind: "intrinsic", name: "promise.all", args: [entriesArr], type: resultT, loc }; } + if (!isJsSourceFile(call.getSourceFile())) { + const tuple = lowerTuplePromiseAll(lowerer, call, elems); + if (tuple) return tuple; + } if ( isJsSourceFile(call.getSourceFile()) && elems.every((entry) => lowerer.dynConvertible(entry.type)) diff --git a/packages/compiler/src/frontend/lowering/containers/array-callbacks.ts b/packages/compiler/src/frontend/lowering/containers/array-callbacks.ts index 2e93e2fd5..735361f54 100644 --- a/packages/compiler/src/frontend/lowering/containers/array-callbacks.ts +++ b/packages/compiler/src/frontend/lowering/containers/array-callbacks.ts @@ -44,6 +44,7 @@ import { lowerArrayCallback, requireProducedArrayElement, callbackArrayElement, + truthyFilterCallback, } from "./callback-arguments.js"; import { reverseArrayLoop, @@ -85,7 +86,9 @@ export function lowerArrayHofCall( const valueT = callbackValueType(lowerer, elem, present); if (booleanFilter) { if (valueT.kind === "union") lowerer.requireTruthyUnion(valueT.unionId, argNode); - if (valueT.kind === "dyn" || valueT.kind === "void" || isUnitType(valueT)) { + // Checked-dynamic elements (`anyText.split("\n").map(...).filter(Boolean)`) + // keep their runtime kind, so their truthiness is the runtime's. + if (valueT.kind === "void" || isUnitType(valueT)) { lowerer.badType(argNode, lowerer.typeOf(argNode)); } } @@ -231,33 +234,6 @@ function filterResultElem( return outElem; } -/** A native closure for the ambient Boolean callback. Reifying the - * JavaScript constructor itself is unnecessary; Array#filter only observes - * its one-argument ToBoolean result. */ -function truthyFilterCallback( - lowerer: Lowerer, - valueT: IrType, - loc: SrcLoc, -): IrExpr & { type: IrType & { kind: "func" } } { - const fnT: IrType & { kind: "func" } = { kind: "func", params: [valueT], ret: BOOL }; - const key = `filterBoolean:${typeKey(valueT)}`; - let name = lowerer.arrHofHelpers.get(key); - if (!name) { - name = `%arr.filterBoolean.${lowerer.arrHofHelpers.size}`; - lowerer.arrHofHelpers.set(key, name); - const value = varRef("v.0", valueT, loc); - lowerer.liftedFns.push({ - name, - params: [{ localId: "v.0", name: "v", type: valueT }], - returnType: BOOL, - locals: [{ id: "v.0", name: "v", type: valueT, mutable: false }], - body: [{ kind: "return", value: { kind: "toBool", operand: value, type: BOOL, loc }, loc }], - loc, - }); - } - return { kind: "closure", fnName: name, captures: [], type: fnT, loc }; -} - function filterPredicateOk(lowerer: Lowerer, ret: IrType): boolean { if (ret.kind === "bool") return true; // VOID is a TYPE erasure, not a runtime value: TS lets a value-returning diff --git a/packages/compiler/src/frontend/lowering/containers/callback-arguments.ts b/packages/compiler/src/frontend/lowering/containers/callback-arguments.ts index e87a5e1bc..dcd9934bd 100644 --- a/packages/compiler/src/frontend/lowering/containers/callback-arguments.ts +++ b/packages/compiler/src/frontend/lowering/containers/callback-arguments.ts @@ -1,12 +1,18 @@ import * as ts from "../../ts7/adapter.js"; import type { Lowerer } from "../lowerer.js"; +import { isJsSourceFile, locOf } from "../../program.js"; +import { varRef } from "../../../ir/build.js"; +import { typeKey } from "../../type-mapper.js"; import { + BOOL, F64, type IrExpr, type IrType, + type SrcLoc, UNDEFINED_T, funcOf, isSupportedArrayElem, + isUnitType, typeEquals, } from "../../../ir/ir.js"; @@ -44,6 +50,24 @@ export function lowerArrayCallback( bindUntyped = false, expectedReturn?: IrType, ): { fnArg: IrExpr & { type: IrType & { kind: "func" } }; arity: number } { + // The ambient `Boolean` as the callback (`flags.every(Boolean)`, + // `xs.some(Boolean)`, `xs.map(Boolean)`): Boolean(value) answers the + // ToBoolean of its FIRST argument and ignores the rest, so the callback + // is the interned one-parameter truthiness closure over the lead slot. + // The constructor's own generic signature has no compiled value form. + // JavaScript files keep their primitive-constructor closure. + if ( + ts.isIdentifier(argNode) && + argNode.text === "Boolean" && + !isJsSourceFile(argNode.getSourceFile()) && + lowerer.isStdlibSymbol(lowerer.resolveValueSymbol(argNode) ?? undefined) + ) { + const valueT = lead[0]; + if (valueT === undefined || valueT.kind === "void" || isUnitType(valueT)) + lowerer.badType(argNode, lowerer.typeOf(argNode)); + if (valueT.kind === "union") lowerer.requireTruthyUnion(valueT.unionId, argNode); + return { fnArg: truthyFilterCallback(lowerer, valueT, locOf(argNode)), arity: 1 }; + } const full = [...lead, F64, arrT]; // A DYN-receiver HOF's callback (`parsed.flatMap((value) => ...)`): // the contextual signature types the unannotated param `any` (the @@ -248,3 +272,39 @@ function runtimeOptionalPairVariant(lowerer: Lowerer, expected: IrType, mapped: : t; return lowerer.isRuntimeOptionalRecordVariant(strip(expected), strip(mapped)); } + +/** A native closure for the ambient Boolean callback. Reifying the + * JavaScript constructor itself is unnecessary; Array#filter only observes + * its one-argument ToBoolean result. */ +export function truthyFilterCallback( + lowerer: Lowerer, + valueT: IrType, + loc: SrcLoc, +): IrExpr & { type: IrType & { kind: "func" } } { + const fnT: IrType & { kind: "func" } = { kind: "func", params: [valueT], ret: BOOL }; + const key = `filterBoolean:${typeKey(valueT)}`; + let name = lowerer.arrHofHelpers.get(key); + if (!name) { + name = `%arr.filterBoolean.${lowerer.arrHofHelpers.size}`; + lowerer.arrHofHelpers.set(key, name); + const value = varRef("v.0", valueT, loc); + lowerer.liftedFns.push({ + name, + params: [{ localId: "v.0", name: "v", type: valueT }], + returnType: BOOL, + locals: [{ id: "v.0", name: "v", type: valueT, mutable: false }], + body: [ + { + kind: "return", + value: + valueT.kind === "dyn" + ? { kind: "dynTest", test: "truthy", value, type: BOOL, loc } + : { kind: "toBool", operand: value, type: BOOL, loc }, + loc, + }, + ], + loc, + }); + } + return { kind: "closure", fnName: name, captures: [], type: fnT, loc }; +} diff --git a/packages/compiler/src/frontend/lowering/containers/string-and-regexp.ts b/packages/compiler/src/frontend/lowering/containers/string-and-regexp.ts index 39154a820..50693e5e4 100644 --- a/packages/compiler/src/frontend/lowering/containers/string-and-regexp.ts +++ b/packages/compiler/src/frontend/lowering/containers/string-and-regexp.ts @@ -302,6 +302,53 @@ function lowerMethodReceiver( return lowerer.coerceInto(node, lowered, expected); } +/** A `string | undefined | null` receiver of a string method: evaluated + * once, each unit arm throws the TypeError Node's member read throws + * ("Cannot read properties of undefined (reading 'replace')") before the + * arguments evaluate, and the string arm is the receiver. */ +function nullableStringReceiver(lowerer: Lowerer, node: ts.Expression, member: string): IrExpr { + const lowered = lowerer.lowerExpr(node); + if (lowered.type.kind !== "union") return lowerer.coerceInto(node, lowered, STRING); + const loc = locOf(node); + const unionId = lowered.type.unionId; + const local = lowerer.declareHiddenLocal("%nullableReceiver", lowered.type); + const ref: IrExpr = { kind: "varRef", localId: local.id, type: lowered.type, loc }; + const arms = lowerer.unions.get(unionId)?.arms ?? []; + let result: IrExpr = { + kind: "unionNarrow", + unionId, + tag: arms.findIndex((arm) => arm.kind === "string"), + value: ref, + type: STRING, + loc, + }; + for (const [tag, arm] of arms.entries()) { + if (!isUnitType(arm)) continue; + const unit = arm.kind === "nullT" ? "null" : "undefined"; + result = { + kind: "ternary", + cond: { kind: "unionIsTag", unionId, tag, value: ref, negated: false, type: BOOL, loc }, + then: nodeThrowExpr( + 1, + "", + `Cannot read properties of ${unit} (reading '${member}')`, + STRING, + loc, + ), + else_: result, + type: STRING, + loc, + }; + } + return { + kind: "seqExpr", + stmts: [{ kind: "varDecl", localId: local.id, init: lowered, loc }], + result, + type: STRING, + loc, + }; +} + /** Relative string indexing and code-point decoding over the UTF-16 intrinsics. */ export function lowerStringIndexCall( lowerer: Lowerer, @@ -733,6 +780,15 @@ export function lowerStringPaddingCall( return { kind: "call", callee: helper, args: values, type: STRING, loc }; } +/** String methods whose regex-argument forms lower in lowerRegexMethodCall. */ +const REGEX_ARGUMENT_STRING_METHODS = new Set([ + "replace", + "replaceAll", + "split", + "search", + "matchAll", +]); + export function lowerRegexMethodCall( lowerer: Lowerer, call: ts.CallExpression, @@ -747,11 +803,30 @@ export function lowerRegexMethodCall( // by construction — the symbol gate doesn't apply to `any` receivers. if (dynReceiver === undefined && !lowerer.isStdlibMember(access)) return null; const name = access.name.text; - const receiverKind = dynReceiver - ? "string" - : lowerer.mapTypeOf(lowerer.typeOf(access.expression))?.kind; + const mappedReceiver = dynReceiver ? null : lowerer.mapTypeOf(lowerer.typeOf(access.expression)); + // A nullable string receiver of a REGEX-argument call + // (`part.replace(re, "")` where an unchecked index read typed `part` + // `string | undefined`) takes the string path, as + // lowerStringMethodCall's gate does for the string-argument forms: + // lowerMethodReceiver throws Node's "Cannot read properties of + // undefined" on the unit arms. (`match` keeps its own gate below.) + const regexArgument = call.arguments.nodes[0]; + const nullableString = + REGEX_ARGUMENT_STRING_METHODS.has(name) && + regexArgument !== undefined && + !ts.isSpreadElement(regexArgument) && + lowerer.mapTypeOf(lowerer.typeOf(regexArgument))?.kind === "regex" && + mappedReceiver?.kind === "union" && + (lowerer.unions.get(mappedReceiver.unionId)?.arms.some((arm) => arm.kind === "string") ?? + false) && + (lowerer.unions + .get(mappedReceiver.unionId) + ?.arms.every((arm) => arm.kind === "string" || isUnitType(arm)) ?? + false); + const receiverKind = dynReceiver || nullableString ? "string" : mappedReceiver?.kind; const lowerReceiver = (): IrExpr => { if (dynReceiver) return dynReceiver(); + if (nullableString) return nullableStringReceiver(lowerer, access.expression, name); if (receiverKind === "string") return lowerMethodReceiver(lowerer, access.expression, STRING, access.name.text); if (receiverKind === "regex") diff --git a/packages/compiler/src/frontend/lowering/lower-enums.ts b/packages/compiler/src/frontend/lowering/lower-enums.ts index 159d72f32..cc2797fb9 100644 --- a/packages/compiler/src/frontend/lowering/lower-enums.ts +++ b/packages/compiler/src/frontend/lowering/lower-enums.ts @@ -29,7 +29,8 @@ import * as ts from "../ts7/adapter.js"; import type { Lowerer } from "./lowerer.js"; import { locOf } from "../program.js"; -import { F64, type IrExpr, STRING } from "../../ir/ir.js"; +import { BOOL, F64, type IrExpr, STRING, UNDEFINED_T } from "../../ir/ir.js"; +import { nodeThrowExpr } from "../../ir/build.js"; /** The member's source-name text (identifiers and string-literal names — * the only name forms tsc allows on enum members). */ @@ -182,6 +183,15 @@ function lowerEnumReverseRead( // existence tracking; nothing needs it — fence through the generic path. if (decls.some((d) => isAmbientEnumDecl(d))) return null; const idx = foldConstIndex(lowerer, key); + const keyType = lowerer.typeOf(key); + if ( + idx === null && + ((keyType.flags & ts.TypeFlags.StringLike) !== 0 || + (keyType.isUnionType() && + ts.constituentTypes(keyType).every((t) => (t.flags & ts.TypeFlags.StringLike) !== 0))) + ) { + return lowerEnumKeyRead(lowerer, expr, decls, key); + } if (idx === null) { lowerer.unsupported( "SC1090", @@ -206,6 +216,109 @@ function lowerEnumReverseRead( return { kind: "strLit", value: name, type: STRING, loc }; } +/** Object.prototype members every enum object inherits: a runtime key + * naming one reads a function, which the member value types cannot hold. */ +const OBJECT_PROTOTYPE_KEYS = new Set([ + "__defineGetter__", + "__defineSetter__", + "__lookupGetter__", + "__lookupSetter__", + "__proto__", + "constructor", + "hasOwnProperty", + "isPrototypeOf", + "propertyIsEnumerable", + "toLocaleString", + "toString", + "valueOf", +]); + +/** `E[key]` with a RUNTIME string key (`Color[name as keyof typeof + * Color]` — the name-to-value lookup): the key evaluates once and is + * compared against every member NAME, each hit answering that member's + * constant, exactly the forward entries tsc's emit stores. A key naming no + * member is Node's undefined, so the read carries an undefined arm. Keys + * Node would answer from elsewhere on the object throw a named runtime + * refusal instead of a wrong value: a numeric member's reverse entry (its + * value spelled as a string — Node answers the member NAME) and the + * inherited Object.prototype members. */ +function lowerEnumKeyRead( + lowerer: Lowerer, + expr: ts.ElementAccessExpression, + decls: readonly ts.EnumDeclaration[], + key: ts.Expression, +): IrExpr { + const loc = locOf(expr); + const enumName = decls[0]!.name.text; + const valueType = lowerer.mapTypeOf(lowerer.typeOf(expr)); + if (valueType === null) lowerer.badType(expr, lowerer.typeOf(expr)); + const resultType = lowerer.withUndefinedArm(valueType); + const keyValue = lowerer.coerceToExpected(lowerer.lowerExpr(key), STRING); + const keyLocal = lowerer.declareHiddenLocal("%enumKey", STRING); + const keyRef: IrExpr = { kind: "varRef", localId: keyLocal.id, type: STRING, loc }; + const matches = (text: string): IrExpr => ({ + kind: "strEq", + negated: false, + left: keyRef, + right: { kind: "strLit", value: text, type: STRING, loc }, + type: BOOL, + loc, + }); + const refusal = (what: string): IrExpr => + nodeThrowExpr( + 0, + "SC1090", + `${what} through a runtime key ('${enumName}[key]') is not supported yet`, + resultType, + loc, + ); + const arms: { cond: IrExpr; value: IrExpr }[] = []; + const names = new Set(); + const reverse = new Set(); + for (const d of decls) { + for (const member of d.members.nodes) { + const name = memberNameText(member); + const constant = lowerEnumMemberRead(lowerer, expr, member); + if (constant === null) + lowerer.unsupported("SC1090", expr, `'${enumName}[key]' over this enum`); + if (!names.has(name)) { + names.add(name); + arms.push({ cond: matches(name), value: lowerer.coerceToExpected(constant, resultType) }); + } + const value = lowerer.checker.getConstantValue(member); + if (typeof value === "number") reverse.add(String(value)); + } + } + for (const text of reverse) { + if (names.has(text)) continue; + arms.push({ cond: matches(text), value: refusal("enum reverse lookups") }); + } + for (const text of OBJECT_PROTOTYPE_KEYS) { + if (names.has(text) || reverse.has(text)) continue; + arms.push({ cond: matches(text), value: refusal("inherited enum object members") }); + } + let result: IrExpr = { kind: "unitLit", unit: "undefined", type: UNDEFINED_T, loc }; + result = lowerer.coerceToExpected(result, resultType); + for (let i = arms.length - 1; i >= 0; i--) { + const arm = arms[i]!; + result = { + kind: "ternary", + cond: arm.cond, + then: arm.value, + else_: result, + type: resultType, + loc, + }; + } + return { + kind: "seqExpr", + stmts: [{ kind: "varDecl", localId: keyLocal.id, init: keyValue, loc }], + result, + type: resultType, + loc, + }; +} + /** A compile-time-constant NUMERIC index: a numeric literal, its negation, * or an enum member read whose constant is a number. */ function foldConstIndex(lowerer: Lowerer, e: ts.Expression): number | null { diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6480-boolean-callbacks.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6480-boolean-callbacks.ts.json new file mode 100644 index 000000000..3279178b8 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6480-boolean-callbacks.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6480-boolean-callbacks.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6481-enum-runtime-keys.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6481-enum-runtime-keys.ts.json new file mode 100644 index 000000000..7fc1885aa --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6481-enum-runtime-keys.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6481-enum-runtime-keys.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6482-promise-all-tuples.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6482-promise-all-tuples.ts.json new file mode 100644 index 000000000..5ceea99a5 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6482-promise-all-tuples.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6482-promise-all-tuples.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6483-nullable-regex-receivers.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6483-nullable-regex-receivers.ts.json new file mode 100644 index 000000000..6a6263bbc --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6483-nullable-regex-receivers.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6483-nullable-regex-receivers.ts" + ], + "diags": [] +} diff --git a/tests/corpus/6480-boolean-callbacks.ts b/tests/corpus/6480-boolean-callbacks.ts new file mode 100644 index 000000000..b9ac4b4ab --- /dev/null +++ b/tests/corpus/6480-boolean-callbacks.ts @@ -0,0 +1,34 @@ +// The ambient Boolean as an array callback beyond filter: every, some, +// find, findIndex and map call it with the element first, so each answers +// ToBoolean of the element (later arguments are ignored). filter(Boolean) +// also covers elements the checker types `any`. +const flags = [true, true, false]; +console.log(flags.every(Boolean), flags.some(Boolean), flags.slice(0, 2).every(Boolean)); +const counts = [0, 3, 0, 7]; +console.log(counts.find(Boolean), counts.findIndex(Boolean), counts.map(Boolean).join(",")); +const names = ["", "a", "", "b"]; +console.log(names.some(Boolean), names.every(Boolean), names.findIndex(Boolean)); +const maybe: (string | undefined)[] = [undefined, "x", undefined]; +console.log(maybe.some(Boolean), maybe.find(Boolean), maybe.map(Boolean).join(",")); + +// Elements the checker types `any` keep their runtime kind; filter(Boolean) +// answers their runtime truthiness. +function untyped(value: unknown): any { + return value; +} +const configFiles = [untyped("app.json"), untyped(""), untyped(undefined), untyped("site.json")].filter( + Boolean, +) as string[]; +console.log(configFiles.length, configFiles.join(",")); + +async function check(n: number): Promise { + return n % 2 === 1; +} + +async function main(): Promise { + const all = (await Promise.all([1, 3, 5].map(async (n) => check(n)))).every(Boolean); + const any = (await Promise.all([2, 4].map(async (n) => check(n)))).some(Boolean); + console.log(all, any); +} + +main(); diff --git a/tests/corpus/6481-enum-runtime-keys.ts b/tests/corpus/6481-enum-runtime-keys.ts new file mode 100644 index 000000000..183d509b2 --- /dev/null +++ b/tests/corpus/6481-enum-runtime-keys.ts @@ -0,0 +1,29 @@ +// @transform-types +// Enum member lookup through a runtime string key (`E[name as keyof +// typeof E]`): each member name answers its constant — string, numeric and +// renamed members alike — and a name no member carries reads undefined. +enum Color { + red = "red", + "dark-blue" = "dark-blue", + green = "light-green", +} + +enum Level { + low, + mid = 5, + high, +} + +function color(name: string | undefined): Color | undefined { + return name ? Color[name as keyof typeof Color] : undefined; +} + +for (const name of ["red", "dark-blue", "green", "light-green", "", undefined]) { + console.log(JSON.stringify(name), JSON.stringify(color(name))); +} + +const levelName = process.argv.length > 99 ? "unused" : "high"; +const level: Level | undefined = Level[levelName as keyof typeof Level]; +console.log(level, level === Level.high); +const missing: Level | undefined = Level[("med" + "ium") as keyof typeof Level]; +console.log(missing === undefined); diff --git a/tests/corpus/6482-promise-all-tuples.ts b/tests/corpus/6482-promise-all-tuples.ts new file mode 100644 index 000000000..ff1615604 --- /dev/null +++ b/tests/corpus/6482-promise-all-tuples.ts @@ -0,0 +1,76 @@ +// Promise.all over an array literal of DIFFERENT promise types (the +// checker's tuple overload): destructuring with holes and nested +// patterns, void entries, plain values, `any` entries that may or may not +// hold a promise, entry identity, and rejection — +// the first rejection in settlement order wins, later ones count as +// handled. +interface Version { + major: number; + range: string; +} + +class Scan { + tool: string; + schema: number | undefined; + constructor(tool: string, schema?: number) { + this.tool = tool; + this.schema = schema; + } +} + +const shared: Version = { major: 22, range: "22.x" }; +const scanned = new Scan("make", 9); + +async function version(): Promise { + return shared; +} +async function scan(): Promise { + return scanned; +} +async function patch(): Promise { + console.log("patched"); +} +async function creds(ok: boolean): Promise<{ key: string } | undefined> { + return ok ? { key: "k" } : undefined; +} +function delay(ms: number, value: T, fail?: string): Promise { + return new Promise((resolve, reject) => + setTimeout(() => (fail ? reject(new Error(fail)) : resolve(value)), ms), + ); +} + +// JavaScript-style results the checker types `any`: one holds a promise, +// one a plain value. +function untypedCount(): any { + return 41; +} +function untypedRecord(): any { + return Promise.resolve({ label: "from a promise" }); +} + +async function main(): Promise { + const [runtime, { tool: kind, schema: level }] = await Promise.all([version(), scan()]); + console.log(runtime.major, kind, level, runtime === shared); + + const first = scan(); + const second = patch(); + const third = creds(true); + const [found, , c] = await Promise.all([first, second, third]); + console.log(found === scanned, c?.key); + + const both = await Promise.all([version(), 5, "x", creds(false)]); + console.log(both[0].range, both[1] + 1, both[2], both[3]); + + const [count, { label }, plain] = await Promise.all([untypedCount(), untypedRecord(), "p"]); + console.log(count + 1, label, plain); + + try { + await Promise.all([delay(20, 1, "slow failure"), delay(5, "s", "fast failure"), delay(1, true)]); + } catch (e) { + console.log("rejected:", (e as Error).message); + } + await delay(40, 0); + console.log("done"); +} + +main(); diff --git a/tests/corpus/6483-nullable-regex-receivers.ts b/tests/corpus/6483-nullable-regex-receivers.ts new file mode 100644 index 000000000..d6b190a30 --- /dev/null +++ b/tests/corpus/6483-nullable-regex-receivers.ts @@ -0,0 +1,23 @@ +// Regex-argument string methods on a receiver the checker types +// `string | undefined` (an unchecked index read, ts-ignored): a string +// takes the regex lowering, undefined throws Node's TypeError. +const lines = "100 /srv/node1/a.txt\n2048 /srv/node22/b/c.bin"; +const pairs = lines.split("\n").map((line) => { + const [count, file] = line.trim().split(/\s+/) as [string | undefined, string | undefined]; + // @ts-ignore possibly undefined + return `${count.replace(/0+$/, "")}:${file.replace(/^\/srv\/node\d+\//, "")}`; +}); +console.log(pairs); + +function parts(text: string): (string | undefined)[] { + return text.split(",").map((s) => (s === "" ? undefined : s)); +} + +for (const value of parts("a-b-c,")) { + try { + // @ts-ignore possibly undefined + console.log(value.replaceAll(/-/g, "+"), value.split(/-/).length, value.search(/c/)); + } catch (e) { + console.log(e instanceof TypeError, (e as Error).message); + } +} diff --git a/tests/diagnostics/promise-all-fences.ts b/tests/diagnostics/promise-all-fences.ts index 89c29c308..c7ddc74bd 100644 --- a/tests/diagnostics/promise-all-fences.ts +++ b/tests/diagnostics/promise-all-fences.ts @@ -1,14 +1,8 @@ -// Promise.all, Promise.allSettled and Promise.any lower over ONE promise -// type (any Promise[] expression); heterogeneous array literals land -// on the checker's tuple overload and fence with the shared-type hint, -// and non-array arguments fence on their shape. -async function hetero(): Promise { - const pair = await Promise.all([ - new Promise((resolve) => resolve("s")), - new Promise((resolve) => resolve(1)), - ]); - console.log(pair.length); -} +// Promise.allSettled and Promise.any lower over ONE promise type (any +// Promise[] expression); their heterogeneous array literals land on +// the checker's tuple overload and fence with the shared-type hint, and +// non-array arguments fence on their shape. (Promise.all's tuple overload +// lowers: corpus 6482.) async function settled(): Promise { const results = await Promise.allSettled([ new Promise((resolve) => resolve("s")), @@ -22,6 +16,5 @@ function* pending(): Generator> { async function anyOfGenerator(): Promise { console.log(await Promise.any(pending())); } -hetero(); settled(); anyOfGenerator(); diff --git a/tests/harness/__snapshots__/promise-all-fences.ts.txt b/tests/harness/__snapshots__/promise-all-fences.ts.txt index 8d17f3f57..18018a8b5 100644 --- a/tests/harness/__snapshots__/promise-all-fences.ts.txt +++ b/tests/harness/__snapshots__/promise-all-fences.ts.txt @@ -1,26 +1,17 @@ -promise-all-fences.ts:6:34 - error SC2020: 'Promise.all over this argument shape' is part of the standard library types but has no scriptc lowering yet +promise-all-fences.ts:7:44 - error SC2020: 'Promise.allSettled over this argument shape' is part of the standard library types but has no scriptc lowering yet - 5 | async function hetero(): Promise { - 6 | const pair = await Promise.all([ - | ^ - 7 | new Promise((resolve) => resolve("s")), - - hint: the entries must share ONE promise type — Promise.all([p, q]) lowers when p and q are the same Promise - -promise-all-fences.ts:13:44 - error SC2020: 'Promise.allSettled over this argument shape' is part of the standard library types but has no scriptc lowering yet - - 12 | async function settled(): Promise { - 13 | const results = await Promise.allSettled([ - | ^ - 14 | new Promise((resolve) => resolve("s")), + 6 | async function settled(): Promise { + 7 | const results = await Promise.allSettled([ + | ^ + 8 | new Promise((resolve) => resolve("s")), hint: the entries must share ONE promise type — Promise.allSettled([p, q]) lowers when p and q are the same Promise -promise-all-fences.ts:23:33 - error SC2020: 'Promise.any over this argument shape' is part of the standard library types but has no scriptc lowering yet +promise-all-fences.ts:17:33 - error SC2020: 'Promise.any over this argument shape' is part of the standard library types but has no scriptc lowering yet - 22 | async function anyOfGenerator(): Promise { - 23 | console.log(await Promise.any(pending())); + 16 | async function anyOfGenerator(): Promise { + 17 | console.log(await Promise.any(pending())); | ^~~~~~~~~ - 24 | } + 18 | } hint: an array of promises (Promise[]) is the supported form \ No newline at end of file From 6197a45ab0796615bca979264168ce376be4a547 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 01:48:22 -0500 Subject: [PATCH 58/83] Refuse arguments through class values of ctor-less stream subclasses A class without a constructor that extends a stream class or EventEmitter inherits the runtime constructor, which takes no arguments here. Direct construction with arguments already reported that, but construction through the class's value (`new registry.upper({ ... })`) completed the arguments against the empty inherited signature and dropped them. Both paths now share refuseInheritedBuiltinCtorArgs, so the class-value path reports at the first argument with the same message. --- .../src/frontend/lowering/lower-classes.ts | 48 ++++++++++++------- ...builtin-base-class-value-arguments.ts.json | 6 +++ .../builtin-base-class-value-arguments.ts | 14 ++++++ .../builtin-base-class-value-arguments.ts.txt | 17 +++++++ 4 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/builtin-base-class-value-arguments.ts.json create mode 100644 tests/diagnostics/builtin-base-class-value-arguments.ts create mode 100644 tests/harness/__snapshots__/builtin-base-class-value-arguments.ts.txt diff --git a/packages/compiler/src/frontend/lowering/lower-classes.ts b/packages/compiler/src/frontend/lowering/lower-classes.ts index 81cf09e1f..7f86de06c 100644 --- a/packages/compiler/src/frontend/lowering/lower-classes.ts +++ b/packages/compiler/src/frontend/lowering/lower-classes.ts @@ -8605,6 +8605,32 @@ function inheritsBuiltinStreamCtor(lowerer: Lowerer, info: ClassInfo): boolean { return false; } +/** `new C(args)` where C's constructor is the inherited runtime stream or + * EventEmitter constructor (a ctor-less chain into the base): that + * constructor takes zero arguments here (the options bag fences, like the + * super() form), so arguments report at the first one. Applies to direct + * construction and to construction through a class value of C. Stream + * subclasses come first: their chain roots at the emitter too, but the + * message should name the stream story. */ +function refuseInheritedBuiltinCtorArgs( + lowerer: Lowerer, + info: ClassInfo, + expr: ts.NewExpression, +): void { + const first = expr.arguments?.nodes[0]; + if (first === undefined) return; + if (inheritsBuiltinStreamCtor(lowerer, info)) { + lowerer.noLowering( + `new ${info.def.name.replace(/^%/, "")} with arguments through an inherited stream constructor`, + first, + "declare a constructor that passes an inline options object to super(...)", + ); + } + if (lowerer.inheritsBuiltinEmitterCtor(info)) { + lowerer.unsupported("SC1090", first, "EventEmitter constructor options ('captureRejections')"); + } +} + /** Preserve argument evaluation order and option presence through builtin * and inherited Error constructors. Checked-dynamic conversion retains * Error identity and distinguishes a missing cause from undefined. */ @@ -8729,24 +8755,7 @@ function lowerProgramClassNew( "construction through a function-local class's internal name", ); lowerer.noteEdge(`%${info.def.name}.constructor`); - // A ctor-less chain into an EventEmitter base inherits `new C()` — - // zero arguments (the options bag fences, like the super() form). - // Stream subclasses come first: their chain roots at the emitter - // too, but the message should name the stream story. - if (inheritsBuiltinStreamCtor(lowerer, info) && (expr.arguments?.nodes ?? []).length > 0) { - lowerer.noLowering( - `new ${info.def.name.replace(/^%/, "")} with arguments through an inherited stream constructor`, - expr.arguments!.nodes[0]!, - "declare a constructor that passes an inline options object to super(...)", - ); - } - if (lowerer.inheritsBuiltinEmitterCtor(info) && (expr.arguments?.nodes ?? []).length > 0) { - lowerer.unsupported( - "SC1090", - expr.arguments!.nodes[0]!, - "EventEmitter constructor options ('captureRejections')", - ); - } + refuseInheritedBuiltinCtorArgs(lowerer, info, expr); // A ctor-less chain into a builtin error base inherits both arguments; // each synthesized constructor forwards the same pair to its base. const args = lowerer.inheritsBuiltinErrorCtor(info) @@ -9994,6 +10003,9 @@ export function lowerNew(lowerer: Lowerer, expr: ts.NewExpression): IrExpr { `EventEmitter and stream class values constructed with options (construct with 'new ${info.def.name.slice(1)}({ ... })' directly)`, ); } + // A program class whose constructor is the inherited runtime one + // takes no arguments through its class value either. + refuseInheritedBuiltinCtorArgs(lowerer, info, expr); defineClassValueConstruct(lowerer, info); const args = lowerer.completeArgs( expr.arguments?.nodes ?? [], diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/builtin-base-class-value-arguments.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/builtin-base-class-value-arguments.ts.json new file mode 100644 index 000000000..622882c24 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/builtin-base-class-value-arguments.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/diagnostics/builtin-base-class-value-arguments.ts" + ], + "diags": [] +} diff --git a/tests/diagnostics/builtin-base-class-value-arguments.ts b/tests/diagnostics/builtin-base-class-value-arguments.ts new file mode 100644 index 000000000..db5be873b --- /dev/null +++ b/tests/diagnostics/builtin-base-class-value-arguments.ts @@ -0,0 +1,14 @@ +// A class without a constructor that extends a stream class inherits the +// runtime constructor, which takes no arguments here. Passing arguments +// through the class's value reports at the first argument, as direct +// construction does. +import { Transform } from "node:stream"; + +class Upper extends Transform {} +const registry = { upper: Upper }; +const upper = new registry.upper({ objectMode: true }); +upper.destroy(); + +const Kind = Upper; +const other = new Kind({ highWaterMark: 4 }); +other.destroy(); diff --git a/tests/harness/__snapshots__/builtin-base-class-value-arguments.ts.txt b/tests/harness/__snapshots__/builtin-base-class-value-arguments.ts.txt new file mode 100644 index 000000000..5e3af4f45 --- /dev/null +++ b/tests/harness/__snapshots__/builtin-base-class-value-arguments.ts.txt @@ -0,0 +1,17 @@ +builtin-base-class-value-arguments.ts:9:34 - error SC2020: 'new Upper with arguments through an inherited stream constructor' is part of the standard library types but has no scriptc lowering yet + + 8 | const registry = { upper: Upper }; + 9 | const upper = new registry.upper({ objectMode: true }); + | ^~~~~~~~~~~~~~~~~~~~ + 10 | upper.destroy(); + + hint: declare a constructor that passes an inline options object to super(...) + +builtin-base-class-value-arguments.ts:13:24 - error SC2020: 'new Upper with arguments through an inherited stream constructor' is part of the standard library types but has no scriptc lowering yet + + 12 | const Kind = Upper; + 13 | const other = new Kind({ highWaterMark: 4 }); + | ^~~~~~~~~~~~~~~~~~~~ + 14 | other.destroy(); + + hint: declare a constructor that passes an inline options object to super(...) \ No newline at end of file From 44f4e2e74c39f1ad1ad3fed7021a7547ed283183 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 22:35:23 -0700 Subject: [PATCH 59/83] Refuse inline stream completion callbacks that name the error Node passes write and end callbacks an error argument (null on success), but the native callbacks receive none, so an inline callback that names the parameter read undefined. It now refuses where it is written. The stream-shims npm case compiles now that stream subclasses are values; its remaining scenarios are deferred refusals, so it is recorded as one. Co-Authored-By: Claude Opus 5.5 --- .../src/frontend/lowering/lower-stream.ts | 26 ++++++++++++++++--- .../stream-completion-callback-error.ts.json | 6 +++++ .../6451-builtin-base-class-expressions.cjs | 13 ++++++++++ .../stream-completion-callback-error.ts | 11 ++++++++ .../stream-completion-callback-error.ts.txt | 17 ++++++++++++ tests/harness/inheritance.test.ts | 20 ++++++-------- tests/harness/npm.test.ts | 2 +- 7 files changed, 78 insertions(+), 17 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/stream-completion-callback-error.ts.json create mode 100644 tests/diagnostics/stream-completion-callback-error.ts create mode 100644 tests/harness/__snapshots__/stream-completion-callback-error.ts.txt diff --git a/packages/compiler/src/frontend/lowering/lower-stream.ts b/packages/compiler/src/frontend/lowering/lower-stream.ts index 5769e1e0a..64d2e60e8 100644 --- a/packages/compiler/src/frontend/lowering/lower-stream.ts +++ b/packages/compiler/src/frontend/lowering/lower-stream.ts @@ -3654,8 +3654,10 @@ export function lowerStreamMethodCall( : lowerer.lowerExprExpecting(args[0]!, STRING); const fn = kind === "bytes" ? "writable.write" : "writable.writeStr"; if (cbArg !== undefined) { - const cb = lowerer.lowerExprExpecting(cbArg, funcOf([], VOID)); - if (cb.type.kind !== "func" || cb.type.params.length > 0) { + const cb = callbackDeclaresParameters(cbArg) + ? null + : lowerer.lowerExprExpecting(cbArg, funcOf([], VOID)); + if (cb === null || cb.type.kind !== "func" || cb.type.params.length > 0) { lowerer.noLowering( "write completion callbacks taking the error argument", cbArg, @@ -3712,8 +3714,10 @@ export function lowerStreamMethodCall( ); } if (cbArg !== undefined) { - const cb = lowerer.lowerExprExpecting(cbArg, funcOf([], VOID)); - if (cb.type.kind !== "func" || cb.type.params.length > 0) { + const cb = callbackDeclaresParameters(cbArg) + ? null + : lowerer.lowerExprExpecting(cbArg, funcOf([], VOID)); + if (cb === null || cb.type.kind !== "func" || cb.type.params.length > 0) { lowerer.noLowering( "end callbacks with parameters", cbArg, @@ -3978,3 +3982,17 @@ export function lowerStreamProperty( } return null; } + +/** Whether an inline completion callback declares a parameter. Node calls + * write and end callbacks with an error argument (null on success), but the + * native callback receives none, so the parameter would read undefined + * where Node passes null or the error. A function literal with parameters + * would otherwise adapt to the zero-parameter slot silently. */ +function callbackDeclaresParameters(node: ts.Expression): boolean { + let callback = node; + while (ts.isParenthesizedExpression(callback)) callback = callback.expression; + return ( + (ts.isArrowFunction(callback) || ts.isFunctionExpression(callback)) && + callback.parameters.nodes.length > 0 + ); +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/stream-completion-callback-error.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/stream-completion-callback-error.ts.json new file mode 100644 index 000000000..af31bb8cf --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/stream-completion-callback-error.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/diagnostics/stream-completion-callback-error.ts" + ], + "diags": [] +} diff --git a/tests/corpus/6451-builtin-base-class-expressions.cjs b/tests/corpus/6451-builtin-base-class-expressions.cjs index 64230a679..6d9241b97 100644 --- a/tests/corpus/6451-builtin-base-class-expressions.cjs +++ b/tests/corpus/6451-builtin-base-class-expressions.cjs @@ -31,3 +31,16 @@ const l = new Lower(); l.on("data", (d) => console.log("lower", d.toString())); l.end("HeLLo"); console.log(Wrap.name, Bus.name, typeof Lower); +{ + // A class declared in a block is collected like a class expression. + class Once extends Readable { + _read() { + this.push(null); + } + } + function onReadable() { + console.log("block", stream.read() === null); + } + const stream = new Once(); + stream.once("readable", onReadable); +} diff --git a/tests/diagnostics/stream-completion-callback-error.ts b/tests/diagnostics/stream-completion-callback-error.ts new file mode 100644 index 000000000..eb73b485d --- /dev/null +++ b/tests/diagnostics/stream-completion-callback-error.ts @@ -0,0 +1,11 @@ +import { Writable } from "node:stream"; + +// Node passes write and end callbacks an error argument (null on success); +// the native callbacks receive none, so a callback that names it refuses. +const sink = new Writable({ + write(_chunk, _encoding, done) { + done(); + }, +}); +sink.write("a", (err) => console.log("written", err)); +sink.end((err?: Error | null) => console.log("ended", err)); diff --git a/tests/harness/__snapshots__/stream-completion-callback-error.ts.txt b/tests/harness/__snapshots__/stream-completion-callback-error.ts.txt new file mode 100644 index 000000000..f3b876cc7 --- /dev/null +++ b/tests/harness/__snapshots__/stream-completion-callback-error.ts.txt @@ -0,0 +1,17 @@ +stream-completion-callback-error.ts:10:17 - error SC2020: 'write completion callbacks taking the error argument' is part of the standard library types but has no scriptc lowering yet + + 9 | }); + 10 | sink.write("a", (err) => console.log("written", err)); + | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 11 | sink.end((err?: Error | null) => console.log("ended", err)); + + hint: () => void callbacks are supported (listen for 'error' to observe failures) + +stream-completion-callback-error.ts:11:10 - error SC2020: 'end callbacks with parameters' is part of the standard library types but has no scriptc lowering yet + + 10 | sink.write("a", (err) => console.log("written", err)); + 11 | sink.end((err?: Error | null) => console.log("ended", err)); + | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 12 | + + hint: () => void callbacks are supported \ No newline at end of file diff --git a/tests/harness/inheritance.test.ts b/tests/harness/inheritance.test.ts index c0ce3d7d4..27a176764 100644 --- a/tests/harness/inheritance.test.ts +++ b/tests/harness/inheritance.test.ts @@ -204,14 +204,11 @@ r.on("data", (b: Buffer) => console.log(b.toString())); expect(llvm).toContain("call void @scr_stream_init_readable(ptr "); }); - test("a runtime-fenced nested stream class emits the refusal without class helpers", async () => { - // The phase-1 reachable bug: a class declared inside a block (a - // runtime fence in JS) whose instances are captured emitted capture- - // box RC adapters for the uncollected class — a C compile error. - // run()'s unregistered-class sweep now rewrites such type slots to - // the inert f64 placeholder before emission (no instance can exist; - // every use traps), so the capture box is a PLAIN-kind box and the - // class never reaches the emitter at all. + test("a block-scoped stream class lowers natively with its class helpers", async () => { + // A class declared inside a block is collected like a class expression. + // Its captured instances once needed the unregistered-class sweep (the + // class was fenced); a builtin stream base no longer fences, so the + // class reaches the emitter with its object type and stream init. const llvm = await emittedLlvm( "stream-nested-fence", `const { Readable } = require("stream"); @@ -226,11 +223,10 @@ r.on("data", (b: Buffer) => console.log(b.toString())); `, "js", ); - expect(llvm).not.toContain("sc_retain_R_v"); expect(llvm).not.toContain("uncollected class"); - expect(llvm).not.toContain("%sc_o_R"); - expect(llvm).toMatch(/%sc_l_stream_0 = alloca ptr/); - expect(llvm).toContain("call void @scr_throw_error_msg_code"); + expect(llvm).toContain("%sc_o_R"); + expect(llvm).toContain("call void @scr_stream_init_readable(ptr "); + expect(llvm).not.toContain("call void @scr_throw_error_msg_code"); }); test("a getter-only override of a pair fills the set slot with a thrower", async () => { diff --git a/tests/harness/npm.test.ts b/tests/harness/npm.test.ts index 54207f246..ac3210dee 100644 --- a/tests/harness/npm.test.ts +++ b/tests/harness/npm.test.ts @@ -104,7 +104,7 @@ const KNOWN: Record = { "package-imports": { build: "SC2013" }, "promise-bridge": { build: "SC1090" }, "self-name": { build: "SC2013" }, - "stream-shims": { build: "SC1090" }, + "stream-shims": { run: "SC1090" }, "util-shims": { run: "SC2020" }, "web-streams": { build: "SC2013" }, "workspace-copied": { build: "SC2013" }, From 9791aa587e11a52a1b49417bc4440bb3284b2080 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 01:50:55 -0500 Subject: [PATCH 60/83] Return parked workers' memory while the event loop waits, after a delay that grows The trim helper returned parked workers' memory once no worker had run for 100 ms. A compiler in watch mode starts its workers again for each rebuild and faulted the returned pages back in, and a rebuild that runs on the main thread alone had the discards run alongside it. - The delay starts at 1 s (SCR_SA_DISCARD_DELAY_MS) and doubles each time workers start within ten delays of a discard, up to SCR_SA_DISCARD_DELAY_MAX_MS (10 s by default). - Once the event loop of the thread that is not a worker runs, the helper discards one arena at a time, and only while that loop has been in one wait with nothing ready for a step (scr_sa_loop_waiting, called by the event loop around its sleep). A loop that does not wait gets the discards SCR_SA_DISCARD_DELAY_MAX_MS after the delay. - Programs without a running event loop discard after the delay, as before. --- packages/runtime/src/scr_alloc.c | 102 +++++++++++++++++++++++--- packages/runtime/src/scr_alloc.test.c | 87 +++++++++++++++++++++- packages/runtime/src/scr_async.c | 6 ++ packages/runtime/src/scr_rc.h | 4 + 4 files changed, 187 insertions(+), 12 deletions(-) diff --git a/packages/runtime/src/scr_alloc.c b/packages/runtime/src/scr_alloc.c index 246d27f0a..935736612 100644 --- a/packages/runtime/src/scr_alloc.c +++ b/packages/runtime/src/scr_alloc.c @@ -935,12 +935,19 @@ static void scr_sa_trim(void) { * interrupts every running thread of the process to flush its TLB, and * glibc's trim locks the arenas other threads allocate from, so discards * wait for the last worker of a group, which hands them to one helper; - * - the helper waits SCR_SA_DISCARD_DELAY_MS (100 by default) and stops if + * - the helper waits a delay (scr_sa_discard_delay_ms: 1 s to start, + * doubled each time workers start again soon after a discard) and stops if * a worker started meanwhile: a program that starts its next workers or * exits within that time does not return pages it would fault back in or * never touch again, and the last worker of the next group hands the * discards to a new helper; a thread that finds every arena claimed * discards one waiting arena and claims it; + * - once the event loop of the thread that is not a worker runs, the + * helper then discards one arena at a time, and only while that loop + * waits with nothing ready (scr_sa_loop_waiting): a program that does + * its next work on that thread alone (a compiler rebuilding one file in + * watch mode) would otherwise have it slowed by the discards. A loop that + * does not wait gets them SCR_SA_DISCARD_DELAY_MAX_MS after the delay; * - the helper then returns the pages * it carved (MADV_DONTNEED; the start of the first page, which it may * share with older blocks, is cleared), frees the recorded system blocks, @@ -1214,12 +1221,20 @@ void scr_sa_thread_shares(void) { /* Worker threads that started and have not parked. */ static _Atomic(unsigned) scr_sa_workers_live; +#ifdef SCR_SA_TRIM +static void scr_sa_group_starting(void); +#endif + /* Library archives run on their embedder's threads and keep their memory. */ static void scr_sa_worker_starting(void) { #ifndef SCR_LIB scr_sa_worker = true; +#ifdef SCR_SA_TRIM + if (atomic_fetch_add_explicit(&scr_sa_workers_live, 1, memory_order_relaxed) == 0) scr_sa_group_starting(); +#else atomic_fetch_add_explicit(&scr_sa_workers_live, 1, memory_order_relaxed); #endif +#endif } #define SCR_SA_WORKER_STARTING() scr_sa_worker_starting() #endif @@ -1309,28 +1324,91 @@ static bool scr_sa_drain_some(bool one) { static bool scr_sa_drain(void) { return scr_sa_drain_some(false); } -/* SCR_SA_DISCARD_DELAY_MS: how long the trim helper waits after the last - * worker parks before it returns their memory (default 100). */ -static long scr_sa_discard_delay_ms(void) { - const char *delay = getenv("SCR_SA_DISCARD_DELAY_MS"); - if (!delay) return 100; - long ms = strtol(delay, NULL, 10); +/* How long the trim helper waits after the last worker parks before it + * returns their memory: SCR_SA_DISCARD_DELAY_MS (1000 by default) to start. + * Each time workers start again within ten delays of a discard, the delay + * doubles, up to SCR_SA_DISCARD_DELAY_MAX_MS (10000 by default): a program + * that keeps starting workers (a compiler rebuilding in watch mode, a + * server) would fault the returned pages straight back in. Memory that + * stays idle for the grown delay still goes back. */ +static _Atomic(long) scr_sa_delay_grown_ms; /* 0 until a discard proved too early */ +static _Atomic(long) scr_sa_discarded_ms; /* when the helper last discarded; 0: not since */ + +static long scr_sa_env_ms(const char *name, long fallback) { + const char *value = getenv(name); + if (!value) return fallback; + long ms = strtol(value, NULL, 10); return ms < 0 ? 0 : ms; } +static long scr_sa_discard_delay_ms(void) { + long start = scr_sa_env_ms("SCR_SA_DISCARD_DELAY_MS", 1000); + long max = scr_sa_env_ms("SCR_SA_DISCARD_DELAY_MAX_MS", 10000); + long grown = atomic_load_explicit(&scr_sa_delay_grown_ms, memory_order_relaxed); + long delay = grown > start ? grown : start; + return max >= start && delay > max ? max : delay; +} + +static long scr_sa_now_ms(void) { + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + return (long)now.tv_sec * 1000 + now.tv_nsec / 1000000 + 1; +} + +/* The first worker of a group starts: a discard shortly before means the + * delay was too short for this program. */ +static void scr_sa_group_starting(void) { + long at = atomic_exchange_explicit(&scr_sa_discarded_ms, 0, memory_order_relaxed); + if (!at) return; + long delay = scr_sa_discard_delay_ms(); + if (scr_sa_now_ms() - at >= 10 * (delay > 10 ? delay : 10)) return; + long max = scr_sa_env_ms("SCR_SA_DISCARD_DELAY_MAX_MS", 10000); + long grown = delay > 0 ? 2 * delay : 1; + if (grown > max) grown = max; + if (grown > atomic_load_explicit(&scr_sa_delay_grown_ms, memory_order_relaxed)) + atomic_store_explicit(&scr_sa_delay_grown_ms, grown, memory_order_relaxed); +} + +/* The event loop of the thread that is not a worker: an odd count while it + * waits with nothing ready, an even one while it runs, 0 until it first + * waits. Only that thread writes it. */ +static _Atomic(unsigned long) scr_sa_loop_turns; + +void scr_sa_loop_waiting(bool waiting) { + if (scr_sa_worker) return; + unsigned long turns = atomic_load_explicit(&scr_sa_loop_turns, memory_order_relaxed); + if ((bool)(turns & 1) != waiting) atomic_store_explicit(&scr_sa_loop_turns, turns + 1, memory_order_relaxed); +} + /* Waits out the delay first, and stops if a worker starts meanwhile: the * discards then wait for the last worker of that group. A program that exits * within the delay does not spend its last milliseconds returning pages, and * one that starts its next workers within it does not return pages while - * they start. */ + * they start. Once the event loop runs, each discard then waits until the + * loop has been in one wait for a step, or until the maximum delay has + * passed. */ static void *scr_sa_drain_main(void *arg) { (void)arg; - for (long left = scr_sa_discard_delay_ms(); left > 0; left -= 5) { - struct timespec pause = {0, (left < 5 ? left : 5) * 1000000L}; + long delay = scr_sa_discard_delay_ms(), step = delay > 200 ? 20 : 5; + for (long left = delay; left > 0; left -= step) { + struct timespec pause = {0, (left < step ? left : step) * 1000000L}; nanosleep(&pause, NULL); if (atomic_load_explicit(&scr_sa_workers_live, memory_order_acquire) != 0) return NULL; } - if (atomic_load_explicit(&scr_sa_workers_live, memory_order_acquire) == 0) scr_sa_drain(); + long max = scr_sa_env_ms("SCR_SA_DISCARD_DELAY_MAX_MS", 10000); + unsigned long seen = 0; + for (long waited = 0; atomic_load_explicit(&scr_sa_workers_live, memory_order_acquire) == 0;) { + unsigned long turns = atomic_load_explicit(&scr_sa_loop_turns, memory_order_relaxed); + if (turns == 0 || ((turns & 1) && turns == seen) || waited >= max) { + if (!scr_sa_drain_some(true)) break; + atomic_store_explicit(&scr_sa_discarded_ms, scr_sa_now_ms(), memory_order_relaxed); + continue; + } + seen = turns; + struct timespec pause = {0, step * 1000000L}; + nanosleep(&pause, NULL); + waited += step; + } return NULL; } @@ -1361,6 +1439,7 @@ void scr_sa_wait_trims(void) { #else void scr_sa_wait_trims(void) {} void scr_sa_thread_shares(void) {} +void scr_sa_loop_waiting(bool waiting) { (void)waiting; } #endif static void scr_sa_park_arena(void); @@ -1564,6 +1643,7 @@ void *scr_mem_realloc(void *p, size_t n) { return realloc(p, n); } void scr_sa_thread_park(void) {} void scr_sa_wait_trims(void) {} void scr_sa_thread_shares(void) {} +void scr_sa_loop_waiting(bool waiting) { (void)waiting; } void scr_sa_free_foreign(void *p) { free(p); } #endif #endif diff --git a/packages/runtime/src/scr_alloc.test.c b/packages/runtime/src/scr_alloc.test.c index aa6b0b60b..6c83afdf9 100644 --- a/packages/runtime/src/scr_alloc.test.c +++ b/packages/runtime/src/scr_alloc.test.c @@ -700,11 +700,91 @@ static void check_discard_delay(void) { assert(pthread_join(holder, NULL) == 0); assert(pthread_join(plain, NULL) == 0); scr_sa_wait_trims(); - unsetenv("SCR_SA_DISCARD_DELAY_MS"); + setenv("SCR_SA_DISCARD_DELAY_MS", "100", 1); assert(resident_pages(first, discard_hi) < total / 2); discard_in_old_arena = true; } +/* A discard that workers follow within ten delays was too early: the next + * delay is twice as long, and memory idle for that long still goes back. */ +static void *plain_discard_worker(void *arg) { + (void)arg; + scr_sa_thread_starting(); + allocate_discardable(); + scr_sa_thread_park(); + return NULL; +} + +static void check_discard_delay_grows(void) { + size_t page = (size_t)sysconf(_SC_PAGESIZE); + setenv("SCR_SA_DISCARD_DELAY_MS", "100", 1); + setenv("SCR_SA_DISCARD_DELAY_MAX_MS", "10000", 1); + discard_blocks = malloc(DISCARD_BLOCKS * sizeof *discard_blocks); + assert(discard_blocks); + /* A plain thread holds the arena whose slice the earlier checks filled. */ + atomic_store(&discard_step, 0); + pthread_t plain; + assert(pthread_create(&plain, NULL, holding_thread, NULL) == 0); + wait_step(1); + run(plain_discard_worker); /* returned after the delay */ + pthread_t t; + assert(pthread_create(&t, NULL, plain_discard_worker, NULL) == 0); + assert(pthread_join(t, NULL) == 0); + uintptr_t first = (discard_lo + page - 1) & ~(uintptr_t)(page - 1); + size_t total = (discard_hi - first) / page; + struct timespec pause = {0, 140000000L}; + nanosleep(&pause, NULL); + assert(resident_pages(first, discard_hi) > total / 2); /* the delay is 200 ms or more now */ + scr_sa_wait_trims(); + assert(resident_pages(first, discard_hi) < total / 2); + setenv("SCR_SA_DISCARD_DELAY_MS", "100", 1); + setenv("SCR_SA_DISCARD_DELAY_MAX_MS", "0", 1); + atomic_store(&discard_step, 3); + assert(pthread_join(plain, NULL) == 0); + free(discard_blocks); +} + +/* Once the event loop runs, discards wait for it to wait with nothing + * ready; a loop that does not wait gets them SCR_SA_DISCARD_DELAY_MAX_MS + * after the delay. The test's thread plays the loop's. */ +static void check_discard_waits_for_loop(void) { + size_t page = (size_t)sysconf(_SC_PAGESIZE); + setenv("SCR_SA_DISCARD_DELAY_MS", "100", 1); + setenv("SCR_SA_DISCARD_DELAY_MAX_MS", "10000", 1); + discard_blocks = malloc(DISCARD_BLOCKS * sizeof *discard_blocks); + assert(discard_blocks); + atomic_store(&discard_step, 0); + pthread_t plain; + assert(pthread_create(&plain, NULL, holding_thread, NULL) == 0); + wait_step(1); + scr_sa_loop_waiting(true); + scr_sa_loop_waiting(false); /* the loop runs */ + pthread_t t; + assert(pthread_create(&t, NULL, plain_discard_worker, NULL) == 0); + assert(pthread_join(t, NULL) == 0); + uintptr_t first = (discard_lo + page - 1) & ~(uintptr_t)(page - 1); + size_t total = (discard_hi - first) / page; + struct timespec pause = {0, 900000000L}; + nanosleep(&pause, NULL); + assert(resident_pages(first, discard_hi) > total / 2); /* past the delay */ + scr_sa_loop_waiting(true); + scr_sa_wait_trims(); + assert(resident_pages(first, discard_hi) < total / 2); + /* A loop that keeps running. */ + scr_sa_loop_waiting(false); + setenv("SCR_SA_DISCARD_DELAY_MAX_MS", "300", 1); + assert(pthread_create(&t, NULL, plain_discard_worker, NULL) == 0); + assert(pthread_join(t, NULL) == 0); + first = (discard_lo + page - 1) & ~(uintptr_t)(page - 1); + total = (discard_hi - first) / page; + scr_sa_wait_trims(); + assert(resident_pages(first, discard_hi) < total / 2); + setenv("SCR_SA_DISCARD_DELAY_MAX_MS", "0", 1); + atomic_store(&discard_step, 3); + assert(pthread_join(plain, NULL) == 0); + free(discard_blocks); +} + static void check_discard(void) { discard_blocks = malloc(DISCARD_BLOCKS * sizeof *discard_blocks); assert(discard_blocks); @@ -819,10 +899,15 @@ int main(void) { check_heap_huge_pages(); #endif #if defined(SCR_WORKERS) && SCR_SMALL_ALLOC + /* A short discard delay, which stays at its start until check_discard_delay_grows. */ + setenv("SCR_SA_DISCARD_DELAY_MS", "100", 1); + setenv("SCR_SA_DISCARD_DELAY_MAX_MS", "0", 1); check_threads(); #ifdef __linux__ check_trim(); check_discard(); + check_discard_delay_grows(); + check_discard_waits_for_loop(); #endif printf("allocator checks passed: size-class workers\n"); #else diff --git a/packages/runtime/src/scr_async.c b/packages/runtime/src/scr_async.c index 155f91be4..8185422af 100644 --- a/packages/runtime/src/scr_async.c +++ b/packages/runtime/src/scr_async.c @@ -4410,6 +4410,9 @@ static bool scr_loop_run_pass(ScrPromise *top_level, bool first_checkpoint) { #endif #if !defined(_WIN32) && !defined(__wasi__) if (due > now) scr_fiber_stacks_clear(); +#endif +#ifdef SCR_WORKERS + if (due > now) scr_sa_loop_waiting(true); #endif bool evw = scr_events_watching_fn != NULL && scr_events_watching_fn(); #ifdef SCR_WORKERS @@ -4564,6 +4567,9 @@ static bool scr_loop_run_pass(ScrPromise *top_level, bool first_checkpoint) { now = due; } } +#ifdef SCR_WORKERS + scr_sa_loop_waiting(false); +#endif while (scr_ntimers > 0 && scr_timers[0].deadline_ms <= now) { ScrTimer t = scr_timer_pop(); /* Timer callbacks are plain sync closures, run on the main stack. diff --git a/packages/runtime/src/scr_rc.h b/packages/runtime/src/scr_rc.h index eb28fb2e7..8611741da 100644 --- a/packages/runtime/src/scr_rc.h +++ b/packages/runtime/src/scr_rc.h @@ -352,6 +352,10 @@ void scr_sa_thread_starting(void); * pointer, or a process-wide cache keeps an immortal object. Its memory * then stays when it exits (scr_alloc.c). */ void scr_sa_thread_shares(void); +/* The event loop of the calling thread is about to wait with nothing ready + * (true), or is done waiting (false); worker threads are ignored. The + * memory of parked workers goes back while the loop waits (scr_alloc.c). */ +void scr_sa_loop_waiting(bool waiting); #define SCR_SA_FREE_FOREIGN(p) scr_sa_free_foreign(p) #else #define SCR_SA_FREE_FOREIGN(p) free(p) From 5a38f414dd228f131f207dee4c63cff8f5b187d9 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 01:54:30 -0500 Subject: [PATCH 61/83] Resolve parameter-list references without the function body's vars A function's parameter initializers run in their own scope, which sees the parameters and a function expression's own name but not the body's `var`s. Chunk splitting resolved them against the body's names too, so a helper that a default value reads was not copied into the chunk when the body declared a `var` of the same name, and the chunk read an undefined binding. A computed method name now resolves outside the method. A named function expression whose body declares a `var` of its own name still exposes the shared function to its parameter initializers, so those reads must be calls for the function to travel with a chunk. --- .../npm-static-bundled-chunks.test.ts | 24 ++++++ .../src/frontend/npm-static-bundled-chunks.ts | 80 ++++++++++++++++--- 2 files changed, 92 insertions(+), 12 deletions(-) diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts index f9463ea1e..6bc830d74 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.test.ts @@ -172,6 +172,25 @@ console.log(require_g().join(",")); expect(chunk).toContain("var fact = function self(n)"); }); +test("a helper a parameter default reads travels with a chunk whose body declares its name", () => { + const answer = expectSameBehavior(`${keyedHelper}var ten = function () { + return 10; +}; +var add = function (n, base = ten()) { + var ten = 1; + return n + base + ten; +}; +var require_g = __commonJS({ + "g.js"(exports2, module2) { + module2.exports = add(1); + } +}); +console.log(require_g()); +`); + const chunk = answer.chunks[0]!.source; + expect(chunk).toContain("var ten = function ()"); +}); + test.each([ ["exports it", `function tally() {}`, `module2.exports = { tally };`], ["reads its properties", `function tally() {}`, `exports2.calls = tally.calls;`], @@ -187,6 +206,11 @@ test.each([ `var tally = function self(box = self) { box.n = (box.n || 0) + 1; return box.n; };`, `exports2.count = tally();`, ], + [ + "calls a function whose parameter default reads its own name past a body var of that name", + `var tally = function self(box = self) { var self = 0; box.n = (box.n || 0) + 1; return box.n + self; };`, + `exports2.count = tally();`, + ], ])("a chunk that %s keeps the package refused", (_, declaration, statement) => { const result = split(`${keyedHelper}${declaration} var require_f = __commonJS({ diff --git a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts index 276079494..33ff865ec 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-chunks.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-chunks.ts @@ -146,6 +146,26 @@ function varScopedNames(body: ts.Node): Set { return out; } +/** The names a function binds for references in its parameter list + * (default values, computed keys of destructuring patterns): its + * parameters and a function expression's own name. Parameter initializers + * run in their own scope, so the body's `var`s are not visible there. */ +function parameterScopeNames(scope: ts.SignatureDeclaration): Set { + const out = new Set(); + for (const param of scope.parameters.nodes) addBindingNames(param.name, out); + if (ts.isFunctionExpression(scope) && scope.name !== undefined) out.add(scope.name.text); + return out; +} + +const NO_NAMES: ReadonlySet = new Set(); + +/** True when `node` lies inside `ancestor` (or is it). */ +function within(node: ts.Node, ancestor: ts.Node): boolean { + for (let n: ts.Node | undefined = node; n !== undefined; n = n.parent) + if (n === ancestor) return true; + return false; +} + /** The names one scope node binds for the references inside it. */ function scopeNames(scope: ts.Node): Set { const out = new Set(); @@ -189,6 +209,7 @@ function scopeNames(scope: ts.Node): Set { * names for the bundle's split. */ class BundleScopes { private readonly names = new Map>(); + private readonly parameterNames = new Map>(); private of(scope: ts.Node): Set { let names = this.names.get(scope); @@ -199,11 +220,26 @@ class BundleScopes { return names; } + /** The names `scope` binds for a reference reached through its child + * `from`: a function's parameter list sees its parameters and own name + * but not its body's `var`s, and a computed method name sees none of + * them. */ + private visible(scope: ts.Node, from: ts.Node): ReadonlySet { + if (!isFunctionScope(scope) || from === scope.body) return this.of(scope); + if (from === scope.name) return NO_NAMES; + let names = this.parameterNames.get(scope); + if (names === undefined) { + names = parameterScopeNames(scope); + this.parameterNames.set(scope, names); + } + return names; + } + /** True when a scope between the reference and `root` (inclusive) * binds its name. */ boundWithin(id: ts.Identifier, root: ts.Node): boolean { - for (let n = id.parent; n !== undefined; n = n.parent) { - if (this.of(n).has(id.text)) return true; + for (let from: ts.Node = id, n = id.parent; n !== undefined; from = n, n = n.parent) { + if (this.visible(n, from).has(id.text)) return true; if (n === root) return false; } return false; @@ -212,16 +248,24 @@ class BundleScopes { /** True when a scope inside `root` (exclusive) binds the reference's * name. */ boundInside(id: ts.Identifier, root: ts.Node): boolean { - for (let n = id.parent; n !== undefined && n !== root; n = n.parent) { - if (this.of(n).has(id.text)) return true; + for ( + let from: ts.Node = id, n = id.parent; + n !== undefined && n !== root; + from = n, n = n.parent + ) { + if (this.visible(n, from).has(id.text)) return true; } return false; } /** True when the reference resolves to a top-level binding of `sf`. */ topLevel(id: ts.Identifier, sf: ts.SourceFile): boolean { - for (let n = id.parent; n !== undefined && n !== sf; n = n.parent) { - if (this.of(n).has(id.text)) return false; + for ( + let from: ts.Node = id, n = id.parent; + n !== undefined && n !== sf; + from = n, n = n.parent + ) { + if (this.visible(n, from).has(id.text)) return false; } return true; } @@ -370,12 +414,24 @@ function selfCalledOnly( const fn = unparen(node.initializer); if (!ts.isFunctionExpression(fn) || fn.name === undefined || fn.body === undefined) return true; const self = fn.name.text; - // A parameter or `var` of the same name hides the function's own name. - const own = varScopedNames(fn.body); - for (const param of fn.parameters.nodes) addBindingNames(param.name, own); - if (own.has(self)) return true; - // Its parameter initializers see the name too. - return references(fn).every((r) => r.text !== self || scopes.boundInside(r, fn) || isCallee(r)); + // A parameter of the same name hides the function's own name everywhere + // in it; a `var` of the same name hides it only in the body, since the + // parameter initializers do not see the body's `var`s. + if (fn.parameters.nodes.some((p) => bindsName(p.name, self))) return true; + const bodyVar = varScopedNames(fn.body).has(self); + const body = fn.body; + return references(fn).every( + (r) => + r.text !== self || scopes.boundInside(r, fn) || (bodyVar && within(r, body)) || isCallee(r), + ); +} + +/** True when a binding name (an identifier or a destructuring pattern) + * declares `name`. */ +function bindsName(binding: ts.BindingName, name: string): boolean { + const names = new Set(); + addBindingNames(binding, names); + return names.has(name); } /** True when a reference is the function a call invokes: `f(...)`, or the From 0c3a7ae73e5c3ff1ffd1bc9420eccec53881a484 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 02:00:21 -0500 Subject: [PATCH 62/83] Keep error stacks through computed keys, clones and throwing getters A read through a computed key now counts as a stack read when the program contains the string "stack", so `e[key]` captures frames like `e.stack` does. A key the program assembles from other strings or from data it reads keeps the static answer. structuredClone of an error held as `any` copies a stack that was already formatted as a non-enumerable data property, as it did before the stack became an accessor. When a name or message getter throws while the stack is formatted, the stack accessor propagates the exception instead of reading the missing string. --- .../src/backend/stack-observations.test.ts | 17 +++++++++ .../src/backend/stack-observations.ts | 36 ++++++++++++++----- packages/runtime/src/scr_json.c | 14 +++++++- tests/corpus/5916-dyn-error-stack-property.ts | 21 +++++++++++ .../6400-dyn-error-stack-property-js.js | 22 ++++++++++++ 5 files changed, 101 insertions(+), 9 deletions(-) diff --git a/packages/compiler/src/backend/stack-observations.test.ts b/packages/compiler/src/backend/stack-observations.test.ts index a09f7f58d..0e1178ce9 100644 --- a/packages/compiler/src/backend/stack-observations.test.ts +++ b/packages/compiler/src/backend/stack-observations.test.ts @@ -67,6 +67,23 @@ test("a stack read on a checked-dynamic value is observed", () => { expect(observesStacks(mod([fn("%main", [read("message")])]))).toBe(false); }); +test("a computed key reads the stack when the program spells it", () => { + const value: IrExpr = { loc, kind: "varRef", localId: "d", type: DYN }; + const key: IrExpr = { loc, kind: "varRef", localId: "k", type: STRING }; + const computed: IrStmt = { + loc, + kind: "exprStmt", + expr: { loc, kind: "dynKeyGet", value, key, type: DYN }, + }; + const spelled: IrStmt = { + loc, + kind: "exprStmt", + expr: { loc, kind: "strLit", value: "stack", type: STRING }, + }; + expect(observesStacks(mod([fn("%main", [computed, spelled])]))).toBe(true); + expect(observesStacks(mod([fn("%main", [computed])]))).toBe(false); +}); + test("programs without stack reads are not", () => { expect(observesStacks(mod([fn("%main", [call("helper")]), fn("helper", [ret])]))).toBe(false); }); diff --git a/packages/compiler/src/backend/stack-observations.ts b/packages/compiler/src/backend/stack-observations.ts index 4894e927a..cc6d109e4 100644 --- a/packages/compiler/src/backend/stack-observations.ts +++ b/packages/compiler/src/backend/stack-observations.ts @@ -13,15 +13,34 @@ import { writesPublished } from "./publication.js"; /** Lib calls that read a captured stack trace. */ const STACK_READS: ReadonlySet = new Set(["error.stack"]); -/** A typed `error.stack` read, or a `stack` read on a checked-dynamic - * value, which reaches an error's own `stack` accessor. */ -function readsStack(expr: IrExpr): boolean { +/** A typed `error.stack` read, or a read on a checked-dynamic value that + * can reach an error's own `stack` accessor: the constant key `stack`, or + * a computed key when `computedKeys` (the program spells "stack", so a + * computed key can name it). A key the program assembles from other + * strings or from data it reads keeps the static answer. */ +function readsStack(expr: IrExpr, computedKeys: boolean): boolean { if (expr.kind === "libCall") return STACK_READS.has(expr.fn); - return expr.kind === "dynKeyGet" && expr.key.kind === "strLit" && expr.key.value === "stack"; + if (expr.kind !== "dynKeyGet") return false; + if (expr.key.kind === "strLit") return expr.key.value === "stack"; + return computedKeys; } -function containsStackRead(fn: IrFunction): boolean { - return !everyStmtList(fn.body, { stmt: () => true, expr: (expr) => !readsStack(expr) }); +/** Whether the module contains the string literal "stack". */ +function spellsStack(mod: IrModule): boolean { + return mod.functions.some( + (fn) => + !everyStmtList(fn.body, { + stmt: () => true, + expr: (expr) => !(expr.kind === "strLit" && expr.value === "stack"), + }), + ); +} + +function containsStackRead(fn: IrFunction, computedKeys: boolean): boolean { + return !everyStmtList(fn.body, { + stmt: () => true, + expr: (expr) => !readsStack(expr, computedKeys), + }); } /** Visit every expression of `body` that execution can reach: statements @@ -46,7 +65,8 @@ function visitLiveExprs(body: IrStmt[], visit: (expr: IrExpr) => void): void { * and those reads must not cost every call of the program a frame. * Programs without any stack read answer false without the walk. */ export function observesStacks(mod: IrModule): boolean { - if (!mod.functions.some(containsStackRead)) return false; + const computedKeys = spellsStack(mod); + if (!mod.functions.some((fn) => containsStackRead(fn, computedKeys))) return false; const byName = new Map(mod.functions.map((fn) => [fn.name, fn])); const classNames = new Set((mod.classes ?? []).map((cls) => cls.name)); const pending: string[] = [...moduleFunctionReferences(mod)]; @@ -70,7 +90,7 @@ export function observesStacks(mod: IrModule): boolean { const fn = byName.get(name); if (fn === undefined) continue; visitLiveExprs(fn.body, (expr) => { - if (readsStack(expr)) observed = true; + if (readsStack(expr, computedKeys)) observed = true; if (expr.kind === "call" && !reached.has(expr.callee)) pending.push(expr.callee); const referenced = exprFunctionReference(expr); if (referenced !== undefined && !reached.has(referenced)) pending.push(referenced); diff --git a/packages/runtime/src/scr_json.c b/packages/runtime/src/scr_json.c index e41c2c37d..04fbe038b 100644 --- a/packages/runtime/src/scr_json.c +++ b/packages/runtime/src/scr_json.c @@ -6754,6 +6754,8 @@ static ScrDyn *scr_error_stack_get_call(ScrClosure *closure, ScrDyn *const *args if (!error) return scr_dyn_retain(scr_dyn_undefined()); ScrStr *stack = scr_error_stack(error); scr_error_release(error); + /* A name or message getter that throws leaves its exception pending. */ + if (!stack) return NULL; ScrDyn *out = scr_dyn_new_str(stack); scr_str_release(stack); return out; @@ -10954,7 +10956,17 @@ static ScrDyn *scr_sc_clone(const ScrDyn *v, const ScrScParent *up, ScrDynRegion ScrDyn *out = scr_dyn_new_obj(); for (size_t i = 0; i < v->v.obj.len; i++) { const ScrDynEntry *e = &v->v.obj.entries[i]; - if (scr_dyn_entry_is_error_stack(e)) continue; + if (scr_dyn_entry_is_error_stack(e)) { + /* A stack that was already formatted copies as a non-enumerable + * data property; an unformatted one has nothing to copy. */ + ScrError *error = scr_errdyn_err_of(v); + if (error && error->stack) { + scr_dyn_obj_set(out, "stack", 5, scr_dyn_new_str(error->stack)); + out->v.obj.entries[out->v.obj.len - 1].enumerable = false; + } + scr_error_release(error); + continue; + } ScrDyn *c = scr_sc_clone(e->value, &self, region); if (c == NULL) { scr_dyn_release(out); diff --git a/tests/corpus/5916-dyn-error-stack-property.ts b/tests/corpus/5916-dyn-error-stack-property.ts index 6198e839c..2e1b42ef3 100644 --- a/tests/corpus/5916-dyn-error-stack-property.ts +++ b/tests/corpus/5916-dyn-error-stack-property.ts @@ -124,3 +124,24 @@ function makeSubclass(): any { return dyn(new PlainSubclass("deep")); } console.log("subclass frames", (makeSubclass().stack as string).includes("at makeSubclass")); + +const formatted = dyn(new Error("formatted")); +console.log("formatted before clone", firstLine(formatted.stack)); +console.log("clone keeps a formatted stack", firstLine(dyn(structuredClone(formatted)).stack)); + +class GrumpyError extends Error { + override get name(): string { + throw new Error("no name today"); + } +} +try { + console.log("throwing name getter", firstLine(dyn(new GrumpyError("grumpy")).stack)); +} catch (error) { + console.log("throwing name getter threw", (error as Error).message); +} + +function readKey(target: any, key: string): unknown { + return target[key]; +} +const computed = readKey(inner(), ["sta", "ck"].join("")) as string; +console.log("computed key", firstLine(computed), computed.includes("at inner")); diff --git a/tests/corpus/6400-dyn-error-stack-property-js.js b/tests/corpus/6400-dyn-error-stack-property-js.js index e46db41eb..f0f666103 100644 --- a/tests/corpus/6400-dyn-error-stack-property-js.js +++ b/tests/corpus/6400-dyn-error-stack-property-js.js @@ -124,3 +124,25 @@ function makeSubclass() { return dyn(new PlainSubclass("deep")); } console.log("subclass frames", makeSubclass().stack.includes("at makeSubclass")); + +const formatted = dyn(new Error("formatted")); +console.log("formatted before clone", firstLine(formatted.stack)); +console.log("clone keeps a formatted stack", firstLine(dyn(structuredClone(formatted)).stack)); + +class GrumpyError extends Error { + /** @returns {string} */ + get name() { + throw new Error("no name today"); + } +} +try { + console.log("throwing name getter", firstLine(dyn(new GrumpyError("grumpy")).stack)); +} catch (error) { + console.log("throwing name getter threw", error.message); +} + +function readKey(target, key) { + return target[key]; +} +const computed = readKey(inner(), ["sta", "ck"].join("")); +console.log("computed key", firstLine(computed), computed.includes("at inner")); From bdf1bb973482da3b00b129674aa12ca1a8c336cf Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 02:06:21 -0500 Subject: [PATCH 63/83] Pass rest-parameter functions into nullable and optional fixed-parameter slots Structural conversion lifts a function into a union's function arm only when cleanFuncAdaptable accepts the pair, and it refused every rest signature. A typed-rest function assigned to `((m: string) => void) | null`, an optional parameter of that type, or a `| undefined` binding therefore failed with SC2003. cleanFuncAdaptable now accepts the pairs the rest packing adapter serves, so these slots use the same adapter as a plain fixed-parameter slot. 6445 covers the nullable, optional and undefined slots, and 6445 and 6446 get their corpus costs. --- .../lowering/coercions/function-adapters.ts | 13 ++++++++---- tests/corpus/6445-rest-function-fixed-slot.ts | 21 +++++++++++++++++++ .../6445-rest-function-fixed-slot.ts.json | 1 + ...6-rest-function-nested-declaration.ts.json | 1 + 4 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 tests/harness/corpus-costs/6445-rest-function-fixed-slot.ts.json create mode 100644 tests/harness/corpus-costs/6446-rest-function-nested-declaration.ts.json diff --git a/packages/compiler/src/frontend/lowering/coercions/function-adapters.ts b/packages/compiler/src/frontend/lowering/coercions/function-adapters.ts index 36908e6f9..76f602cf7 100644 --- a/packages/compiler/src/frontend/lowering/coercions/function-adapters.ts +++ b/packages/compiler/src/frontend/lowering/coercions/function-adapters.ts @@ -111,12 +111,15 @@ export function coercibleValue(lowerer: Lowerer, src: IrType, dst: IrType): bool } /** Whether all parameters and the result adapt without a trap-only path. - * Rest signatures are excluded; extra destination parameters are ignored. */ + * Rest signatures are excluded, except a typed-rest source in a slot with + * fixed parameters (the rest packing adapter); extra destination + * parameters are ignored. */ export function cleanFuncAdaptable( lowerer: Lowerer, src: IrType & { kind: "func" }, dst: IrType & { kind: "func" }, ): boolean { + if (restPackingAdapterFits(lowerer, src, dst)) return true; if (src.rest === true || dst.rest === true) return false; if (src.params.length > dst.params.length) return false; for (let i = 0; i < src.params.length; i++) { @@ -127,9 +130,6 @@ export function cleanFuncAdaptable( return src.ret.kind === "void" && dst.ret.kind === "dyn"; } -/** Adapt a function value to a destination signature, forwarding and - * converting its arguments and result. Unsupported parameter/result pairs - * produce checked traps when called; rest signatures remain refused. */ /** Whether a typed-rest function (`(...m: T[]) => R`) can serve a slot * with fixed parameters (`(m: T) => void`): the slot's parameters convert * to the function's fixed parameters, then to the rest element type. */ @@ -200,6 +200,11 @@ function restPackingAdapter( return name; } +/** Adapt a function value to a destination signature, forwarding and + * converting its arguments and result. Unsupported parameter/result pairs + * produce checked traps when called. Rest signatures are refused, except a + * typed-rest source in a slot with fixed parameters, which the rest + * packing adapter serves. */ export function funcCoerceAdapter( lowerer: Lowerer, fromT: IrType & { kind: "func" }, diff --git a/tests/corpus/6445-rest-function-fixed-slot.ts b/tests/corpus/6445-rest-function-fixed-slot.ts index 6c7dbeada..e78e83836 100644 --- a/tests/corpus/6445-rest-function-fixed-slot.ts +++ b/tests/corpus/6445-rest-function-fixed-slot.ts @@ -26,3 +26,24 @@ none(); const count: (tag: string, a: number, b: number) => number = tagged; console.log("count", count("t", 1, 2)); ["a", "b"].forEach((m) => new Line(log).emit(m)); + +// Nullable and optional slots: the value takes the slot's function arm. +class OptionalLine { + private readonly sink: ((m: string) => void) | null; + constructor(sink: ((m: string) => void) | null) { + this.sink = sink; + } + emit(m: string): void { + if (this.sink) this.sink(m); + else console.log("no sink:", m); + } +} +new OptionalLine(log).emit("nullable"); +new OptionalLine(null).emit("dropped"); +function relay(m: string, sink?: (m: string) => void): void { + sink?.(m); +} +relay("optional", log); +relay("absent"); +const maybe: ((a: string, b: string) => void) | undefined = log; +maybe?.("p", "q"); diff --git a/tests/harness/corpus-costs/6445-rest-function-fixed-slot.ts.json b/tests/harness/corpus-costs/6445-rest-function-fixed-slot.ts.json new file mode 100644 index 000000000..858c662a8 --- /dev/null +++ b/tests/harness/corpus-costs/6445-rest-function-fixed-slot.ts.json @@ -0,0 +1 @@ +{ "plain": 2526, "san": 3450 } diff --git a/tests/harness/corpus-costs/6446-rest-function-nested-declaration.ts.json b/tests/harness/corpus-costs/6446-rest-function-nested-declaration.ts.json new file mode 100644 index 000000000..39f698b22 --- /dev/null +++ b/tests/harness/corpus-costs/6446-rest-function-nested-declaration.ts.json @@ -0,0 +1 @@ +{ "plain": 2222, "san": 3035 } From 9febfe8ca3939e79923a8e3a35d47cbcb89e46ce Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 02:07:18 -0500 Subject: [PATCH 64/83] Place a quoted name field among an error's fields The position of an Error subclass's own `name` among its fields was found from identifier keys only, so a quoted `"name"` field, other quoted fields, and `this["name"] = ...` in a constructor were not seen, and `name` was listed last. Field keys and constructor assignments now read any statically spelled key: an identifier, a quoted string, a number in its canonical spelling, or a computed string literal. --- .../src/frontend/lowering/error-name-order.ts | 32 +++++++++++++------ tests/corpus/6403-error-own-name-order.ts | 6 ++++ tests/corpus/6404-error-own-name-order-js.js | 10 ++++++ 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/packages/compiler/src/frontend/lowering/error-name-order.ts b/packages/compiler/src/frontend/lowering/error-name-order.ts index 8523b47cb..7453d123e 100644 --- a/packages/compiler/src/frontend/lowering/error-name-order.ts +++ b/packages/compiler/src/frontend/lowering/error-name-order.ts @@ -6,17 +6,27 @@ type ErrorClassLink = Pick & { base: ErrorClassLink | null; }; -/** Whether `node` is an assignment to `this.`, and the name. */ +/** The property key a member or element-access name spells statically: + * an identifier, a quoted string, a number in its canonical spelling, or a + * computed string literal. Null for other keys. */ +function staticKeyText(name: ts.Node): string | null { + if (ts.isIdentifier(name) || ts.isStringLiteral(name)) return name.text; + if (ts.isNumericLiteral(name)) return String(Number(name.text)); + if (ts.isComputedPropertyName(name)) return staticKeyText(name.expression); + return null; +} + +/** Whether `node` is an assignment to `this.` or `this[""]`, + * and the name. */ function thisAssignmentName(node: ts.Node): string | null { if (!ts.isBinaryExpression(node) || node.operatorToken.kind !== ts.SyntaxKind.EqualsToken) return null; const target = node.left; - if ( - !ts.isPropertyAccessExpression(target) || - target.expression.kind !== ts.SyntaxKind.ThisKeyword - ) - return null; - return ts.isIdentifier(target.name) ? target.name.text : null; + if (ts.isPropertyAccessExpression(target) && target.expression.kind === ts.SyntaxKind.ThisKeyword) + return ts.isIdentifier(target.name) ? target.name.text : null; + if (ts.isElementAccessExpression(target) && target.expression.kind === ts.SyntaxKind.ThisKeyword) + return staticKeyText(target.argumentExpression); + return null; } /** Where an Error subclass instance's own `name` sits among its fields when @@ -45,15 +55,17 @@ export function errorNameAfterField( for (const link of chain) { for (const property of link.paramProps ?? []) create(property.name); for (const member of link.decl!.members.nodes) { - if (!ts.isPropertyDeclaration(member) || !ts.isIdentifier(member.name)) continue; + if (!ts.isPropertyDeclaration(member)) continue; + const key = staticKeyText(member.name); + if (key === null) continue; if ( member.modifiers?.nodes.some( (m) => m.kind === ts.SyntaxKind.StaticKeyword || m.kind === ts.SyntaxKind.DeclareKeyword, ) ) continue; - if (member.name.text === "name") return last; - create(member.name.text); + if (key === "name") return last; + create(key); } const body = link.ctor?.body; if (!body) continue; diff --git a/tests/corpus/6403-error-own-name-order.ts b/tests/corpus/6403-error-own-name-order.ts index 25e59bb7c..d1a5200c4 100644 --- a/tests/corpus/6403-error-own-name-order.ts +++ b/tests/corpus/6403-error-own-name-order.ts @@ -57,6 +57,11 @@ class NamedChild extends FieldBase { } } +class QuotedName extends Error { + override "name" = "QuotedName"; + "quoted-extra" = 1; +} + class Unnamed extends Error { extra = 1; } @@ -83,6 +88,7 @@ show("later field", new LaterNameField("c")); show("code then name", new CodeThenName("d")); show("child of named", new ChildOfNamed("e")); show("named child", new NamedChild("f")); +show("quoted name field", new QuotedName("q")); const late = new Unnamed("g"); late.name = "Late"; diff --git a/tests/corpus/6404-error-own-name-order-js.js b/tests/corpus/6404-error-own-name-order-js.js index 0b5ad4e82..b5f9b4d32 100644 --- a/tests/corpus/6404-error-own-name-order-js.js +++ b/tests/corpus/6404-error-own-name-order-js.js @@ -49,6 +49,15 @@ class Derived extends Base { } } +class ElementName extends Error { + constructor(message) { + super(message); + this.first = 1; + this["name"] = "ElementName"; + this.second = 2; + } +} + class Unnamed extends Error { constructor(message) { super(message); @@ -73,6 +82,7 @@ show("first", new AssignedFirst("b")); show("declared field", new DeclaredField("c")); show("name field", new NameField("d")); show("derived", new Derived("e")); +show("element name", new ElementName("g")); const late = new Unnamed("f"); late.name = "Late"; From b6ef49d39d95b8ba08354a01c71f749b92416910 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 02:09:48 -0500 Subject: [PATCH 65/83] Promisify callback-last functions that return a value The promisified executor typed its call of the function as void, so a function with a non-void return type failed to compile with SC9001. The call now has the function's return type, and its value is discarded, as in Node. --- .../lowering/builtins/promisify-callback.ts | 3 ++- tests/corpus/6428-promisify-callback-functions.ts | 14 +++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/compiler/src/frontend/lowering/builtins/promisify-callback.ts b/packages/compiler/src/frontend/lowering/builtins/promisify-callback.ts index 693079d3f..cdb8a1fbb 100644 --- a/packages/compiler/src/frontend/lowering/builtins/promisify-callback.ts +++ b/packages/compiler/src/frontend/lowering/builtins/promisify-callback.ts @@ -297,7 +297,8 @@ function promisifyMaker(lowerer: Lowerer, shape: CallbackShape, loc: SrcLoc): st callbackParamT, ), ], - type: VOID, + // The function's own return type; the value is discarded. + type: fnT.ret, loc, }, loc, diff --git a/tests/corpus/6428-promisify-callback-functions.ts b/tests/corpus/6428-promisify-callback-functions.ts index 45cf17052..b899dd292 100644 --- a/tests/corpus/6428-promisify-callback-functions.ts +++ b/tests/corpus/6428-promisify-callback-functions.ts @@ -1,7 +1,8 @@ // util.promisify over typed callback-only functions (bound methods, one with -// an optional callback, a function declaration, an inline arrow): the error -// argument rejects, the value fulfills, a synchronous throw rejects, and -// settlement is async. +// an optional callback, a function declaration, an inline arrow, a function +// that returns a value): the error argument rejects, the value fulfills, the +// return value is ignored, a synchronous throw rejects, and settlement is +// async. import { promisify } from "node:util"; class Connection { @@ -32,6 +33,12 @@ function boom(callback: (error: Error | null) => void): void { if (callback !== undefined) throw new TypeError("synchronous failure"); } +let pending = 0; +function size(callback: (error: Error | null, size: number) => void): number { + setImmediate(() => callback(null, 4)); + return ++pending; +} + async function main(): Promise { const c = new Connection("db"); const stats = promisify(c.stats.bind(c)); @@ -68,5 +75,6 @@ async function main(): Promise { console.log(order.join(", ")); await promisify(new Connection("inline").close.bind(new Connection("inline")))(); console.log("inline done"); + console.log("size", await promisify(size)(), pending); } void main(); From 2553818b0c037ee59d48972694e5d7a69385e898 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 23:44:30 -0700 Subject: [PATCH 66/83] Compile the round-2 index-signature and union-of-records shapes - An object literal that spreads an index-signature record (the metric-tags idiom '{ ...tags, bucket, status }') maps to the pure index-signature record joining the spread slots and member types: the checker drops the signature, but the object holds every spread key. Declared members read and write as keys of it. - A conditional spread into an index-signature record adds no key when it does not apply. - '{ ...unknownValue }' into an index-signature record copies the value's own keys through the checked extraction; null, undefined, numbers and booleans spread nothing. - 'return Promise.reject(e)' in an async function is typed by the function's own result. - A union of record layouts flowing into one record slot converts each arm; fields the checker types 'undefined' alone lift into undefined-armed slots. - '(x as { m: T }).m' on a union of records reads the arms that declare m and answers undefined for the others. Co-Authored-By: Claude Opus 5.5 --- .../lowering/coercions/structural-plans.ts | 14 +- .../frontend/lowering/coercions/structural.ts | 13 ++ .../lowering/expressions/object-literals.ts | 71 ++++++- .../lowering/expressions/property-access.ts | 125 +++++++++++++ .../src/frontend/lowering/lower-exprs.ts | 22 ++- .../src/frontend/lowering/lower-stmts.ts | 26 +++ .../compiler/src/frontend/lowering/lowerer.ts | 83 +++++++++ .../src/frontend/lowering/width-lift.ts | 1 + packages/compiler/src/frontend/type-mapper.ts | 174 ++++++++++++++++++ .../6470-promise-reject-async-return.ts.json | 6 + ...-union-literal-results-into-record.ts.json | 6 + .../corpus/6472-asserted-member-read.ts.json | 6 + ...3-unknown-spread-into-index-record.ts.json | 6 + .../6474-index-record-spread-literals.ts.json | 6 + .../6470-promise-reject-async-return.ts | 17 ++ .../6471-union-literal-results-into-record.ts | 37 ++++ tests/corpus/6472-asserted-member-read.ts | 16 ++ .../6473-unknown-spread-into-index-record.ts | 21 +++ .../6474-index-record-spread-literals.ts | 59 ++++++ 19 files changed, 706 insertions(+), 3 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6470-promise-reject-async-return.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6471-union-literal-results-into-record.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6472-asserted-member-read.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6473-unknown-spread-into-index-record.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6474-index-record-spread-literals.ts.json create mode 100644 tests/corpus/6470-promise-reject-async-return.ts create mode 100644 tests/corpus/6471-union-literal-results-into-record.ts create mode 100644 tests/corpus/6472-asserted-member-read.ts create mode 100644 tests/corpus/6473-unknown-spread-into-index-record.ts create mode 100644 tests/corpus/6474-index-record-spread-literals.ts diff --git a/packages/compiler/src/frontend/lowering/coercions/structural-plans.ts b/packages/compiler/src/frontend/lowering/coercions/structural-plans.ts index 2bc991c14..53b0748b4 100644 --- a/packages/compiler/src/frontend/lowering/coercions/structural-plans.ts +++ b/packages/compiler/src/frontend/lowering/coercions/structural-plans.ts @@ -23,7 +23,19 @@ export function widthLiftPlan(lowerer: Lowerer, src: IrType, dst: IrType): Width } if (dst.kind === "union") { if (src.kind === "union") { - return lowerer.unionRetagMappable(src.unionId, dst.unionId) ? { how: "retag" } : null; + if (lowerer.unionRetagMappable(src.unionId, dst.unionId)) return { how: "retag" }; + // A field the checker types `undefined` alone (an object literal's + // omitted member, normalized to `?: undefined` across a union of + // literal types) carries the unit-only union. Into a slot with an + // undefined arm it lifts as that arm; its null arm cannot occur. + const srcArms = lowerer.unions.get(src.unionId)?.arms ?? []; + if ( + srcArms.length > 0 && + srcArms.every(isUnitType) && + lowerer.armTag(dst.unionId, UNDEFINED_T) >= 0 + ) + return { how: "unitRetag" }; + return null; } // A unit-typed source can't wrap here (unionWrap requires the // LITERAL unit — and no lowered shape carries a bare unit field). diff --git a/packages/compiler/src/frontend/lowering/coercions/structural.ts b/packages/compiler/src/frontend/lowering/coercions/structural.ts index c85b9868d..496a29607 100644 --- a/packages/compiler/src/frontend/lowering/coercions/structural.ts +++ b/packages/compiler/src/frontend/lowering/coercions/structural.ts @@ -125,6 +125,19 @@ export function applyWidthLift( throw new InternalCompilerError("lowerer bug: planned retag lift failed to intern"); return { kind: "call", callee: retag, args: [value], type: dst, loc }; } + case "unitRetag": { + if (dst.kind !== "union" || value.type.kind !== "union") + throw new InternalCompilerError("lowerer bug: unit retag lift shape"); + const fromId = value.type.unionId; + const trappable = new Set(); + (lowerer.unions.get(fromId)?.arms ?? []).forEach((arm, tag) => { + if (lowerer.armTag(dst.unionId, arm) < 0) trappable.add(tag); + }); + const retag = lowerer.unionRetagHelper(fromId, dst.unionId, loc, trappable); + if (!retag) + throw new InternalCompilerError("lowerer bug: planned unit retag lift failed to intern"); + return { kind: "call", callee: retag, args: [value], type: dst, loc }; + } case "liftWrap": { if (dst.kind !== "union") throw new InternalCompilerError("lowerer bug: liftWrap lift against a non-union"); diff --git a/packages/compiler/src/frontend/lowering/expressions/object-literals.ts b/packages/compiler/src/frontend/lowering/expressions/object-literals.ts index 6238479e0..5a9d0ea10 100644 --- a/packages/compiler/src/frontend/lowering/expressions/object-literals.ts +++ b/packages/compiler/src/frontend/lowering/expressions/object-literals.ts @@ -640,6 +640,74 @@ function literalUnionArmOf( /** Object spread treats nullish sources as empty. Preserve evaluation once, * then turn `Record | undefined` into either that record or a fresh * empty record of the same represented shape. */ +/** `{ ...unknownValue }` into a pure index-signature record (headers + * forwarded from an untyped parameter): Node copies the source's own + * enumerable keys in order, and spreads nothing for null, undefined, + * numbers and booleans. An object source converts through the checked + * extraction into the target's own shape, which keeps its keys and order + * and checks each value against the slot type (a value outside it throws + * the catchable TypeError of every unknown-to-typed conversion). A string + * source takes the same extraction, which rejects it, rather than + * silently dropping the characters Node would spread. Null for other + * sources and targets. */ +function dynIndexSpreadSource( + lowerer: Lowerer, + source: IrExpr, + target: IrType, + loc: SrcLoc, +): IrExpr | null { + if (source.type.kind !== "dyn" || target.kind !== "record") return null; + const shape = lowerer.shapes.get(target.shapeId); + if (!shape?.indexValue || shape.fields.length > 0 || shape.tuple) return null; + if ( + !canDynCheckTo( + target, + (id) => lowerer.shapes.get(id), + (id) => lowerer.unions.get(id), + ) + ) + return null; + const local = lowerer.declareHiddenLocal("%dynSpread", DYN); + const ref = varRef(local.id, DYN, loc); + const test = (kind: "object" | "string" | "nullish"): IrExpr => ({ + kind: "dynTest", + test: kind, + value: ref, + type: BOOL, + loc, + }); + const extract: IrExpr = { kind: "dynCheck", value: ref, type: target, loc }; + const empty: IrExpr = { kind: "recordLit", fields: [], type: target, loc }; + return { + kind: "seqExpr", + stmts: [{ kind: "varDecl", localId: local.id, init: source, loc }], + result: { + kind: "ternary", + cond: test("nullish"), + then: empty, + else_: { + kind: "ternary", + cond: { + kind: "logical", + op: "||", + left: test("object"), + right: test("string"), + type: BOOL, + loc, + }, + then: extract, + else_: empty, + type: target, + loc, + }, + type: target, + loc, + }, + type: target, + loc, + }; +} + function lowerOptionalIndexSpreadSource( lowerer: Lowerer, source: IrExpr, @@ -1372,7 +1440,7 @@ export function lowerObjectLiteral( vNode, ); contributors.push({ - kind: "field", + kind: "condField", name: csProp.name.text, value: { kind: "ternary", @@ -1387,6 +1455,7 @@ export function lowerObjectLiteral( } let src = lowerer.lowerExpr(prop.expression); src = lowerOptionalIndexSpreadSource(lowerer, src, locOf(prop)) ?? src; + src = dynIndexSpreadSource(lowerer, src, type, locOf(prop)) ?? src; if (src.type.kind !== "record") { lowerer.unsupported( "SC1090", diff --git a/packages/compiler/src/frontend/lowering/expressions/property-access.ts b/packages/compiler/src/frontend/lowering/expressions/property-access.ts index 7f0b2ddbb..b7414f986 100644 --- a/packages/compiler/src/frontend/lowering/expressions/property-access.ts +++ b/packages/compiler/src/frontend/lowering/expressions/property-access.ts @@ -1239,6 +1239,8 @@ export function lowerPropertyAccessExpr( const mapped = lowerer.mapTypeOf(lowerer.typeOf(expr)); return mapped && mapped.kind !== "void" ? lowerer.coerceInto(expr, read, mapped) : read; } + const asserted = assertedMemberRead(lowerer, expr, recvLowered, loc); + if (asserted) return asserted; lowerer.unsupported( "SC1090", expr, @@ -1246,6 +1248,101 @@ export function lowerPropertyAccessExpr( ); } +/** `(source as { repoName: string }).repoName` where the assertion names a + * member some arms of the stored union of records do not declare: Node + * reads the property off whichever object it is, so arms that declare it + * answer their slot, and the others answer undefined (native records are + * closed, so they cannot hold it). The result keeps that undefined arm + * whatever the assertion claims; a consumer that needs the asserted type + * checks it like any other narrowing. The arms that declare the member + * must agree on its type. Null when the receiver is not such an + * assertion over records, or no arm declares the member. */ +function assertedMemberRead( + lowerer: Lowerer, + expr: ts.PropertyAccessExpression, + recv: IrExpr, + loc: SrcLoc, +): IrExpr | null { + let receiverNode: ts.Expression = expr.expression; + while (ts.isParenthesizedExpression(receiverNode)) receiverNode = receiverNode.expression; + if (!ts.isAsExpression(receiverNode) && !ts.isTypeAssertion(receiverNode)) return null; + if (recv.type.kind !== "union") return null; + const unionId = recv.type.unionId; + const arms = lowerer.unions.get(unionId)?.arms ?? []; + const name = expr.name.text; + let resultType: IrType | null = null; + let records = 0; + const declaring: { tag: number; shapeId: string; type: IrType }[] = []; + const units: number[] = []; + for (let tag = 0; tag < arms.length; tag++) { + const arm = arms[tag]!; + if (isUnitType(arm)) { + units.push(tag); + continue; + } + if (arm.kind !== "record") return null; + records++; + const shape = lowerer.shapes.get(arm.shapeId); + if (!shape || shape.tuple || shape.indexValue) return null; + const field = shape.fields.find((f) => f.name === name); + if (!field) continue; + // The declaring arms agree up to an undefined arm (`string` beside + // `string | undefined`). + const armResult = lowerer.withUndefinedArmOf(field.type); + if (!armResult || (resultType && !typeEquals(resultType, armResult))) return null; + resultType = armResult; + declaring.push({ tag, shapeId: arm.shapeId, type: field.type }); + } + if (!resultType || declaring.length === records) return null; + const stable = lowerer.declareHiddenLocal("%assertedRecv", recv.type); + const ref = (): IrExpr => ({ kind: "varRef", localId: stable.id, type: recv.type, loc }); + let result: IrExpr | null = lowerer.wrappedUndefined(resultType, loc); + if (!result) return null; + // A unit arm the checker narrowed away reads like Node would read it. + for (const tag of units) { + const unit = arms[tag]!.kind === "nullT" ? "null" : "undefined"; + result = { + kind: "ternary", + cond: { kind: "unionIsTag", unionId, tag, negated: false, value: ref(), type: BOOL, loc }, + then: nodeThrowExpr( + 1, + "", + `Cannot read properties of ${unit} (reading '${name}')`, + resultType, + loc, + ), + else_: result, + type: resultType, + loc, + }; + } + for (const { tag, shapeId, type: fieldType } of declaring) { + const read: IrExpr = { + kind: "recordGet", + obj: { kind: "unionNarrow", unionId, tag, value: ref(), type: arms[tag]!, loc }, + shapeId, + field: name, + type: fieldType, + loc, + }; + result = { + kind: "ternary", + cond: { kind: "unionIsTag", unionId, tag, negated: false, value: ref(), type: BOOL, loc }, + then: lowerer.coerceToExpected(read, resultType), + else_: result, + type: resultType, + loc, + }; + } + return { + kind: "seqExpr", + stmts: [{ kind: "varDecl", localId: stable.id, init: recv, loc }], + result, + type: resultType, + loc, + }; +} + function importMetaProperty(lowerer: Lowerer, expr: ts.PropertyAccessExpression): IrExpr | null { return isImportMeta(expr.expression) ? importMetaField(lowerer, expr, expr.name.text) : null; } @@ -2568,6 +2665,27 @@ export function lowerFieldRead(lowerer: Lowerer, expr: ts.PropertyAccessExpressi loc: locOf(expr), }; } + // A member the checker declares on the receiver, stored in a pure + // index-signature record (an object literal that spread one, see + // dictSpreadLiteralShape): the key is always present, so the keyed + // read answers the member through the slot type. + if ( + actual?.type.kind === "record" && + shape.fields.length === 0 && + declaredMemberOf(lowerer, expr.expression, expr.name.text) + ) { + return lowerer.maybeNarrow( + { + kind: "recordKeyGet", + obj: actual, + shapeId: actual.type.shapeId, + key: { kind: "strLit", value: expr.name.text, type: STRING, loc: locOf(expr.name) }, + type: shape.indexValue, + loc: locOf(expr), + }, + expr, + ); + } lowerer.unsupported( "SC1090", expr, @@ -3549,3 +3667,10 @@ function jsFunctionExpandoMember(lowerer: Lowerer, access: ts.PropertyAccessExpr }) ); } + +/** Whether the checker declares `name` as a property of `receiver`'s type + * (as opposed to reading it through an index signature). */ +export function declaredMemberOf(lowerer: Lowerer, receiver: ts.Expression, name: string): boolean { + const symbol = lowerer.checker.getPropertyOfType(lowerer.typeOf(receiver), name); + return !!symbol && symbol.declarations.length > 0; +} diff --git a/packages/compiler/src/frontend/lowering/lower-exprs.ts b/packages/compiler/src/frontend/lowering/lower-exprs.ts index ac51cfbe6..fb8db2657 100644 --- a/packages/compiler/src/frontend/lowering/lower-exprs.ts +++ b/packages/compiler/src/frontend/lowering/lower-exprs.ts @@ -2573,6 +2573,24 @@ export function symbolFieldTarget( }; } +/** `return Promise.reject(reason)` in an async function: the checker's + * context is the awaited return type (or its union with PromiseLike), not + * a promise, but the async function adopts the returned promise, so the + * promise of its own result type is the honest type. Null elsewhere. */ +function asyncReturnPromise(lowerer: Lowerer, call: ts.CallExpression): IrType | null { + if (lowerer.ctx.isAsync !== true || lowerer.ctx.generator) return null; + let node: ts.Node = call; + while (ts.isParenthesizedExpression(node.parent)) node = node.parent; + const parent = node.parent; + const returned = + (ts.isReturnStatement(parent) && parent.expression === node) || + (ts.isArrowFunction(parent) && parent.body === node); + if (!returned) return null; + const ret = lowerer.ctx.returnType; + if (ret.kind === "void" || ret.kind === "promise") return null; + return { kind: "promise", inner: ret }; +} + /** `Promise.reject(reason)` on THE Promise global: a pre-rejected * promise. The reason is pinned to the Error hierarchy (the executor * reject parameter's contract — rejection payloads share the @@ -2604,7 +2622,9 @@ export function lowerPromiseRejectCall( // A maybe-promise context (`return Promise.reject(e)` from a function // returning `T | Promise`) names its one promise arm. const maybePromise = contextual ? maybePromiseUnionOf(lowerer, contextual) : null; - const type = maybePromise ? maybePromise.promise : contextual; + const type = maybePromise + ? maybePromise.promise + : (asyncReturnPromise(lowerer, call) ?? contextual); const reasonNode = call.arguments.nodes[0]!; // A CHECKED-DYNAMIC reason (`Promise.reject(value)` where value rode // an untyped binding — the tracingChannel suite's shape): the checked-dynamic tree diff --git a/packages/compiler/src/frontend/lowering/lower-stmts.ts b/packages/compiler/src/frontend/lowering/lower-stmts.ts index 41c048ae9..9866fba8a 100644 --- a/packages/compiler/src/frontend/lowering/lower-stmts.ts +++ b/packages/compiler/src/frontend/lowering/lower-stmts.ts @@ -1,3 +1,4 @@ +import { declaredMemberOf } from "./expressions/property-access.js"; import { matchAllRowsReadByElements, storedMatchAllRowsReadByElements, @@ -7469,6 +7470,31 @@ export function lowerExprStatement(lowerer: Lowerer, expr: ts.Expression): IrStm const recvIr = lowerer.mapTypeOf(lowerer.typeOf(expr.left.expression)); if (recvIr?.kind === "record") { const shape = lowerer.shapes.get(recvIr.shapeId); + if ( + shape?.indexValue && + shape.fields.length === 0 && + declaredMemberOf(lowerer, expr.left.expression, expr.left.name.text) + ) { + // A declared member of a literal stored as a pure index-signature + // record (dictSpreadLiteralShape): a keyed write of that key. + const loc = locOf(expr); + const obj = lowerer.lowerExpr(expr.left.expression); + if (obj.type.kind === "record") { + const value = lowerer.intoIndexValueSlot( + lowerer.lowerExprExpecting(expr.right, shape.indexValue), + shape.indexValue, + expr.right, + ); + return { + kind: "recordKeySet", + obj, + shapeId: obj.type.shapeId, + key: { kind: "strLit", value: expr.left.name.text, type: STRING, loc }, + value, + loc, + }; + } + } if ( shape?.indexValue && !shape.fields.some( diff --git a/packages/compiler/src/frontend/lowering/lowerer.ts b/packages/compiler/src/frontend/lowering/lowerer.ts index 27011df24..6428b7c45 100644 --- a/packages/compiler/src/frontend/lowering/lowerer.ts +++ b/packages/compiler/src/frontend/lowering/lowerer.ts @@ -8927,6 +8927,10 @@ export class Lowerer { return this.applyWidthLift(lift, narrowed, expected, expr.loc); } } + if (expr.type.kind === "union" && expected.kind === "record") { + const lifted = unionArmsWidthLift(this, expr, expected); + if (lifted) return lifted; + } if (!typeEquals(expr.type, expected)) { const w = this.widthCoerce(expr, expected); if (w) return w; @@ -12984,3 +12988,82 @@ export class Lowerer { return lowerStdlibMethodCall(this, call, access); } } + +/** A union of record layouts flowing into one record slot: a function + * returning differently-shaped object literals (`{ ok: false }` and + * `{ ok: true, size }`, whose union the checker normalizes to arms with + * `size?: undefined`) into its declared result type. Each record arm + * width-lifts into the slot on its own tag; unit arms take the stranded + * TypeError (the checker excluded them). Null when some record arm does + * not lift, or when there is a non-record, non-unit arm. */ +function unionArmsWidthLift( + lowerer: Lowerer, + expr: IrExpr, + expected: IrType & { kind: "record" }, +): IrExpr | null { + if (expr.type.kind !== "union") return null; + const unionId = expr.type.unionId; + const arms = lowerer.unions.get(unionId)?.arms ?? []; + const lifts: (WidthLift | null)[] = []; + let records = 0; + for (const arm of arms) { + if (isUnitType(arm)) { + lifts.push(null); + continue; + } + if (arm.kind !== "record") return null; + const lift = lowerer.widthLiftPlan(arm, expected); + if (!lift) return null; + lifts.push(lift); + records++; + } + if (records < 2) return null; + const loc = expr.loc; + const local = lowerer.declareHiddenLocal("%unionLift", expr.type); + const ref = (): IrExpr => ({ kind: "varRef", localId: local.id, type: expr.type, loc }); + let result: IrExpr | null = null; + for (let tag = arms.length - 1; tag >= 0; tag--) { + const lift = lifts[tag]; + const arm = arms[tag]!; + const branch: IrExpr = lift + ? lowerer.applyWidthLift( + lift, + { kind: "unionNarrow", unionId, tag, value: ref(), type: arm, loc }, + expected, + loc, + ) + : nodeThrowExpr( + 1, + "", + `${arm.kind === "nullT" ? "null" : "undefined"} is not representable in a '${lowerer.fmt(expected)}' slot (a value narrowed or asserted past the type still held it)`, + expected, + loc, + ); + result = + result === null + ? branch + : { + kind: "ternary", + cond: { + kind: "unionIsTag", + unionId, + tag, + negated: false, + value: ref(), + type: BOOL, + loc, + }, + then: branch, + else_: result, + type: expected, + loc, + }; + } + return { + kind: "seqExpr", + stmts: [{ kind: "varDecl", localId: local.id, init: expr, loc }], + result: result!, + type: expected, + loc, + }; +} diff --git a/packages/compiler/src/frontend/lowering/width-lift.ts b/packages/compiler/src/frontend/lowering/width-lift.ts index d90f9ed35..e3722bc32 100644 --- a/packages/compiler/src/frontend/lowering/width-lift.ts +++ b/packages/compiler/src/frontend/lowering/width-lift.ts @@ -9,6 +9,7 @@ export type WidthLift = | { how: "discriminantWrap" } | { how: "discriminantRoute" } | { how: "retag" } + | { how: "unitRetag" } | { how: "liftWrap"; tag: number; arm: IrType } | { how: "width" } | { how: "unionWidth" } diff --git a/packages/compiler/src/frontend/type-mapper.ts b/packages/compiler/src/frontend/type-mapper.ts index 437aba2b8..de4e13de0 100644 --- a/packages/compiler/src/frontend/type-mapper.ts +++ b/packages/compiler/src/frontend/type-mapper.ts @@ -5860,6 +5860,8 @@ function mapRecordType(widened: ts.Type, ctx: TypeMapperCtx): IrType | null { (inner.indexValue === undefined || inner.indexValue.kind === "dyn") ) return DYN; + const merged = dictSpreadLiteralShape(widened, inner, ctx); + if (merged !== null) return { kind: "record", shapeId: merged }; const refined = refinedConstituentShape(widened, inner, ctx); if (refined !== null) return { kind: "record", shapeId: refined }; return { @@ -5871,6 +5873,178 @@ function mapRecordType(widened: ts.Type, ctx: TypeMapperCtx): IrType | null { } } +/** The type of an object literal that spreads an index-signature record + * (`{ ...tags, bucket: b, status }`, the metric-tags idiom). The checker + * drops the signature and types the literal by its explicit members only, + * but at run time the object holds every key the spread copied, in one + * insertion order. Such a literal maps to the pure index-signature record + * whose slot joins the spread slots and the member types, so the merge, + * every enumeration and keyed writes see the same keys Node does. A + * member read still answers its member type: members written after the + * spreads always win, and a member written before a spread must have that + * spread's slot type, so an override cannot change its type. The join + * covers scalar and unit types only; other literals keep their shape and + * the spread fence. */ +function dictSpreadLiteralShape( + widened: ts.Type, + parts: RecordShapeParts, + ctx: TypeMapperCtx, +): string | null { + if (parts.indexValue) return null; + const symbol = widened.getSymbol(); + if (!symbol || (symbol.flags & ts.SymbolFlags.ObjectLiteral) === 0) return null; + const literal = symbol.declarations.find((d): d is ts.ObjectLiteralExpression => + ts.isObjectLiteralExpression(d), + ); + if (!literal) return null; + const { checker, shapes, unions } = ctx; + const props = literal.properties.nodes; + if (!props.some((p) => ts.isSpreadAssignment(p))) return null; + // Decide on checker types alone before mapping anything: object literals + // with ordinary spreads are everywhere, and mapping their sources here + // would intern types out of their usual order. + // A literal written into a typed index-signature slot already lowers into + // it (the merge into the contextual record); its own type keeps its + // mapping. + const contextualRaw = checker.getContextualType(literal); + const contextual = contextualRaw ? checker.getNonNullableType(contextualRaw) : undefined; + // An unknown-valued slot (`Record`) is the checked-dynamic + // value, which has no typed merge of its own: the literal still needs its + // own record. + if ( + contextual && + checker + .getIndexInfosOfType(contextual) + .some((info) => (info.valueType.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) === 0) + ) + return null; + const kinds = props.map((p) => + ts.isSpreadAssignment(p) ? spreadSourceKindTs(checker, p.expression) : "member", + ); + if (kinds.some((k) => k === null) || !kinds.includes("dict")) return null; + const dictSlotOf = (e: ts.Expression): IrType | null => { + const t = checker.getNonNullableType(checker.getTypeAtLocation(e)); + if (checker.getPropertiesOfType(t).length === 0) { + const infos = checker.getIndexInfosOfType(t); + if (infos.length !== 1 || !(infos[0]!.keyType.flags & ts.TypeFlags.String)) return null; + return mapType(infos[0]!.valueType, ctx); + } + // Another such literal's type (`{ ...tags, repo }` over a binding + // initialized by `{ ...base, plan }`) is stored as the same kind of + // pure index-signature record. + if (t === widened) return null; + const mapped = mapType(t, ctx); + const shape = mapped?.kind === "record" ? shapes.get(mapped.shapeId) : undefined; + return shape?.indexValue && shape.fields.length === 0 && !shape.tuple ? shape.indexValue : null; + }; + const arms = new Map(); + const addArms = (t: IrType): boolean => { + const list = t.kind === "union" ? (unions.get(t.unionId)?.arms ?? []) : [t]; + for (const arm of list) { + if (!OVERLOAD_JOIN_SCALARS.has(arm.kind)) return false; + arms.set(typeKey(arm), arm); + } + return true; + }; + const before: string[] = []; + for (const [i, p] of props.entries()) { + // A fixed-shape or conditional spread contributes its own members, + // which the checker already types exactly (they are in `parts`). + if (kinds[i] === "record") { + // Its members may be overwritten by a later dict spread, like members + // written before one. + before.push(...spreadMemberNamesTs(checker, (p as ts.SpreadAssignment).expression)); + continue; + } + if (ts.isSpreadAssignment(p)) { + const slot = dictSlotOf(p.expression); + if (!slot || !addArms(slot)) return null; + // A member written before this spread may be overwritten by it. + for (const name of before) { + const field = parts.fields.find((f) => f.name === name); + if (!field || !typeEquals(field.type, slot)) return null; + } + continue; + } + if (!(ts.isPropertyAssignment(p) || ts.isShorthandPropertyAssignment(p))) return null; + const name = p.name; + if (!(ts.isIdentifier(name) || ts.isStringLiteral(name))) return null; + before.push(name.text); + } + if (parts.fields.some((f) => f.name.startsWith("%"))) return null; + for (const field of parts.fields) if (!addArms(field.type)) return null; + if (![...arms.values()].some((arm) => !isUnitType(arm))) return null; + const sorted = [...arms.values()].sort((a, b) => (typeKey(a) < typeKey(b) ? -1 : 1)); + const slot: IrType = + sorted.length === 1 ? sorted[0]! : { kind: "union", unionId: unions.intern(sorted) }; + return shapes.intern([], false, slot, []); +} + +/** How a spread source contributes to dictSpreadLiteralShape, judged on + * checker types only: "dict" for a pure string index signature or another + * literal that spreads one, "record" for a fixed object shape or a + * conditional spread (`...(c && { k: v })`), null otherwise. */ +function spreadSourceKindTs(checker: ts.TypeChecker, e: ts.Expression): "dict" | "record" | null { + if (dictSpreadSourceTs(checker, e, 0)) return "dict"; + let inner = e; + while (ts.isParenthesizedExpression(inner)) inner = inner.expression; + if ( + (ts.isBinaryExpression(inner) && + inner.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken) || + ts.isConditionalExpression(inner) + ) + return "record"; + const t = checker.getNonNullableType(checker.getTypeAtLocation(e)); + if ((t.flags & ts.TypeFlags.Object) === 0 || t.isUnionType()) return null; + if (checker.getIndexInfosOfType(t).length > 0) return null; + if (checker.getCallSignatures(t).length > 0 || checker.getConstructSignatures(t).length > 0) + return null; + const symbol = t.getSymbol(); + if (symbol && (symbol.flags & ts.SymbolFlags.Class) !== 0) return null; + return "record"; +} + +/** The member names a fixed-shape or conditional spread contributes. */ +function spreadMemberNamesTs(checker: ts.TypeChecker, e: ts.Expression): string[] { + let inner = e; + while (ts.isParenthesizedExpression(inner)) inner = inner.expression; + const literals: ts.Expression[] = []; + if (ts.isBinaryExpression(inner)) literals.push(inner.right); + else if (ts.isConditionalExpression(inner)) literals.push(inner.whenTrue, inner.whenFalse); + else literals.push(inner); + const names: string[] = []; + for (const l of literals) { + const t = checker.getNonNullableType(checker.getTypeAtLocation(l)); + for (const property of checker.getPropertiesOfType(t)) names.push(property.name); + } + return names; +} + +/** A spread source the checker types as a pure string index signature, + * or as another object literal that spreads one (dictSpreadLiteralShape), + * judged on checker types only. */ +function dictSpreadSourceTs(checker: ts.TypeChecker, e: ts.Expression, depth: number): boolean { + const t = checker.getNonNullableType(checker.getTypeAtLocation(e)); + if (checker.getPropertiesOfType(t).length === 0) { + const infos = checker.getIndexInfosOfType(t); + return infos.length === 1 && (infos[0]!.keyType.flags & ts.TypeFlags.String) !== 0; + } + if (depth > 4) return false; + const symbol = t.getSymbol(); + if (!symbol || (symbol.flags & ts.SymbolFlags.ObjectLiteral) === 0) return false; + const literal = symbol.declarations.find((d): d is ts.ObjectLiteralExpression => + ts.isObjectLiteralExpression(d), + ); + if (!literal) return false; + const spreads = literal.properties.nodes.filter((p) => ts.isSpreadAssignment(p)); + return ( + spreads.length > 0 && + spreads.every((p) => + dictSpreadSourceTs(checker, (p as ts.SpreadAssignment).expression, depth + 1), + ) + ); +} + /** A refinement intersection (`Expr & { kind: "mul" }`, which the checker * reduces to `Mul & { kind: "mul" }`) keeps the shape of the constituent it * refines when the refinement changes no field's representation. Recursive diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6470-promise-reject-async-return.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6470-promise-reject-async-return.ts.json new file mode 100644 index 000000000..dda9bd1e8 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6470-promise-reject-async-return.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6470-promise-reject-async-return.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6471-union-literal-results-into-record.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6471-union-literal-results-into-record.ts.json new file mode 100644 index 000000000..1a02071a1 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6471-union-literal-results-into-record.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6471-union-literal-results-into-record.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6472-asserted-member-read.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6472-asserted-member-read.ts.json new file mode 100644 index 000000000..dfb88018b --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6472-asserted-member-read.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6472-asserted-member-read.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6473-unknown-spread-into-index-record.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6473-unknown-spread-into-index-record.ts.json new file mode 100644 index 000000000..abf47b7db --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6473-unknown-spread-into-index-record.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6473-unknown-spread-into-index-record.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6474-index-record-spread-literals.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6474-index-record-spread-literals.ts.json new file mode 100644 index 000000000..8991f227d --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6474-index-record-spread-literals.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6474-index-record-spread-literals.ts" + ], + "diags": [] +} diff --git a/tests/corpus/6470-promise-reject-async-return.ts b/tests/corpus/6470-promise-reject-async-return.ts new file mode 100644 index 000000000..4d9a62eab --- /dev/null +++ b/tests/corpus/6470-promise-reject-async-return.ts @@ -0,0 +1,17 @@ +// `return Promise.reject(err)` in an async function returns a promise of the +// function's own result type, which the async function adopts. +type Ctx = { id: string; endpoint: string; meta?: { [key: string]: string } }; +async function context(endpoint: string | undefined): Promise { + if (!endpoint || endpoint.length === 0) { + return Promise.reject(new Error("invalid endpoint")); + } + return Promise.resolve({ id: "c1", endpoint }); +} +function sync(flag: boolean): Promise { + if (flag) return Promise.reject(new TypeError("nope")); + return Promise.resolve(3); +} +context("http://x").then((c) => console.log(c.id, c.endpoint)); +context(undefined).catch((e) => console.log("rejected", String(e))); +sync(false).then((n) => console.log(n)); +sync(true).catch((e) => console.log(e instanceof TypeError, String(e))); diff --git a/tests/corpus/6471-union-literal-results-into-record.ts b/tests/corpus/6471-union-literal-results-into-record.ts new file mode 100644 index 000000000..0f6c158d1 --- /dev/null +++ b/tests/corpus/6471-union-literal-results-into-record.ts @@ -0,0 +1,37 @@ +// A function returning differently shaped object literals (the checker +// normalizes omitted members to `?: undefined`) flows into a declared record +// result: each literal shape converts on its own. +class Tracer { + trace(_name: string, fn: () => T): T { + return fn(); + } +} +const tracer = new Tracer(); +type CacheResult = { downloaded: boolean; size?: number; cacheType?: string; dir?: string }; +async function download(n: number, dir: string | undefined): Promise { + const pending = tracer.trace("download", async () => { + if (n === 0) { + return { downloaded: false, dir }; + } + if (n < 0) { + return { downloaded: false }; + } + return { downloaded: true, size: n, cacheType: "full", dir }; + }); + return pending.finally(() => { + console.log("done", n); + }); +} +type Details = { obtain: string; url?: string; kind?: string }; +async function details(flag: number): Promise
{ + return tracer.trace("details", async () => { + if (flag === 0) return { obtain: "skipped" }; + return { obtain: "found", url: "u", kind: "k" }; + }); +} +async function main() { + console.log(await download(0, "/d"), await download(5, undefined), await download(-1, "z")); + const d0 = await details(0); + console.log(d0, "url" in d0, JSON.stringify(await details(1))); +} +main(); diff --git a/tests/corpus/6472-asserted-member-read.ts b/tests/corpus/6472-asserted-member-read.ts new file mode 100644 index 000000000..498a9bb57 --- /dev/null +++ b/tests/corpus/6472-asserted-member-read.ts @@ -0,0 +1,16 @@ +// `(source as { repoName: string }).repoName` on a union of records: arms +// that declare the member answer it, the others answer undefined, as Node +// reads a missing property. +type GithubSource = { type: string; repoId: number; repoName: string; ref: string }; +type GitlabSource = { type: string; projectId: number; ref: string }; +type BitbucketSource = { type: string; repoName: string; owner: string; ref: string }; +type Source = GithubSource | GitlabSource | BitbucketSource; +function label(source: Source): string { + return (source as { repoName: string }).repoName || "???"; +} +const sources: Source[] = [ + { type: "github", repoId: 1, repoName: "r1", ref: "main" }, + { type: "gitlab", projectId: 2, ref: "dev" }, + { type: "bitbucket", repoName: "", owner: "o", ref: "x" }, +]; +console.log(sources.map(label)); diff --git a/tests/corpus/6473-unknown-spread-into-index-record.ts b/tests/corpus/6473-unknown-spread-into-index-record.ts new file mode 100644 index 000000000..c5cd4f6ad --- /dev/null +++ b/tests/corpus/6473-unknown-spread-into-index-record.ts @@ -0,0 +1,21 @@ +// Spreading an unknown value into an index-signature record copies its own +// keys in order and spreads nothing for null and undefined. +function build(extra: unknown, attempt: number): Record { + return { + "content-type": "application/json", + ...(extra as Record), + "x-attempt": String(attempt), + }; +} +function headersOf(params: { headers?: T }, attempt: number): Record { + return { + ...params.headers, + "x-attempt": String(attempt), + }; +} +const parsed: unknown = JSON.parse('{"authorization":"t","content-type":"text/plain"}'); +console.log(build(parsed, 1)); +console.log(headersOf({ headers: parsed }, 3), headersOf({}, 4)); +const nothing: unknown = JSON.parse("null"); +const five: unknown = JSON.parse("5"); +console.log(headersOf({ headers: nothing }, 5), headersOf({ headers: five }, 6)); diff --git a/tests/corpus/6474-index-record-spread-literals.ts b/tests/corpus/6474-index-record-spread-literals.ts new file mode 100644 index 000000000..59e1603a9 --- /dev/null +++ b/tests/corpus/6474-index-record-spread-literals.ts @@ -0,0 +1,59 @@ +// An object literal that spreads an index-signature record holds every key +// the spread copied, in insertion order, though the checker types it by its +// explicit members only. A conditional spread that does not apply adds no +// key. +function bucket(n: number | undefined): string { + return n === undefined ? "unknown" : n < 100 ? "small" : "large"; +} +function emit(name: string, tags: Record): void { + console.log(name, JSON.stringify(tags), Object.keys(tags).length); +} +function measure(base: Record, count: number | undefined, ok: boolean) { + const metricTags = { + ...base, + fileCountBucket: bucket(count), + status: ok ? "succeeded" : "failed", + }; + emit("files", metricTags); + console.log(metricTags.status.toUpperCase(), metricTags.fileCountBucket); + return metricTags; +} +const base = { plan: "pro", region: "iad1", status: "pending" }; +const m = measure(base, 42, true); +console.log(Object.entries(m)); +function walker(enable: boolean, metricTags: Record = {}) { + const tags = { + plan: "unknown", + ...metricTags, + largeCliDplPerformance: String(enable), + walkerFallback: "false", + }; + tags.walkerFallback = "true"; + console.log(tags, tags.plan); +} +walker(true, { plan: "hobby", extra: "x" }); +walker(false); +function counts(tags: Record, n: number) { + const t = { ...tags, attempts: n, retried: n > 1 }; + console.log(t, t.attempts + 1, t.retried); +} +counts({ a: "1" }, 2); +function depthTags(deep: boolean): { depth: number | undefined; kind: "shallow" | "deep"; useDeep: boolean } { + return { depth: deep ? undefined : 1, kind: deep ? "deep" : "shallow", useDeep: deep }; +} +function attempt(base: Record, n: number, deep: boolean, bytes: number | undefined) { + const tags = { + ...base, + git_attempt: n, + repo: "r", + ...(bytes !== undefined && { packBytes: bytes }), + ...depthTags(deep), + }; + console.log(JSON.stringify(tags), Object.keys(tags).join(","), tags.git_attempt, tags.useDeep); +} +attempt({ plan: "pro" }, 1, false, 10); +attempt({ plan: "pro", repo: "x" }, 2, true, undefined); +function addTags(tags: Record): void { + console.log(Object.entries(tags).map(([k, v]) => `${k}=${String(v)}`).join(" ")); +} +addTags({ ...{ plan: "pro", team: "t" } as Record, count: 3, ok: true }); From b74224f31c4f165a951ecaa12e2c01ca7a654bfd Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 19 Oct 2026 23:56:38 -0700 Subject: [PATCH 67/83] Throw checked-dynamic primitives as primitives so catch typeof tests see them A value thrown from `unknown` storage (`throw values[i]`) that holds a number, string or boolean now throws as that primitive, like a statically typed `throw 5`. Before, it threw as a checked-dynamic reference, so `typeof e === "number"` (and "string"/"boolean") on the catch binding answered false and the narrowed reads were unreachable. A primitive has no identity to keep, and reading the binding as `unknown` converts it back. Co-Authored-By: Claude Opus 5.5 --- .../6480-catch-typeof-untyped-throws.ts.json | 6 +++ packages/runtime/src/scr_json.c | 22 ++++++++++ .../6480-catch-typeof-untyped-throws.ts | 40 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6480-catch-typeof-untyped-throws.ts.json create mode 100644 tests/corpus/6480-catch-typeof-untyped-throws.ts diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6480-catch-typeof-untyped-throws.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6480-catch-typeof-untyped-throws.ts.json new file mode 100644 index 000000000..38b99f9fc --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6480-catch-typeof-untyped-throws.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6480-catch-typeof-untyped-throws.ts" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_json.c b/packages/runtime/src/scr_json.c index 27168dad1..33793c26d 100644 --- a/packages/runtime/src/scr_json.c +++ b/packages/runtime/src/scr_json.c @@ -6359,6 +6359,28 @@ void scr_dyn_throw(ScrDyn *d) { scr_dyn_release(d); return; } + /* A checked-dynamic primitive throws as the primitive itself, as a + * statically typed `throw 5` does: catch bindings test and extract the + * same snapshot kinds (`typeof e === "number"`), and a primitive has no + * identity to keep. */ + if (d->kind == SCR_DYN_NUM) { + double value = d->v.num; + scr_dyn_release(d); + scr_throw_f64(value); + return; + } + if (d->kind == SCR_DYN_BOOL) { + bool value = d->v.b; + scr_dyn_release(d); + scr_throw_bool(value); + return; + } + if (d->kind == SCR_DYN_STR) { + ScrStr *value = scr_str_retain(d->v.str); + scr_dyn_release(d); + scr_throw_str(value); + return; + } scr_throw_ref_classified(d, scr_dyn_retain_v, scr_dyn_release_v, scr_dyn_trace_v, scr_dyn_is_object(d)); } diff --git a/tests/corpus/6480-catch-typeof-untyped-throws.ts b/tests/corpus/6480-catch-typeof-untyped-throws.ts new file mode 100644 index 000000000..98122f851 --- /dev/null +++ b/tests/corpus/6480-catch-typeof-untyped-throws.ts @@ -0,0 +1,40 @@ +// Values thrown from `unknown` storage read back through typeof tests on +// the catch binding, like the same values thrown with static types. +const thrown: unknown[] = [5, 0, -1.5, "text", "", true, false, null, undefined, { kind: "box" }]; +function describe(value: unknown): string { + try { + throw value; + } catch (e) { + const same = e === value; + if (typeof e === "number") return `${same} number ${e + 1}`; + if (typeof e === "string") return `${same} string ${e.length}`; + if (typeof e === "boolean") return `${same} boolean ${!e}`; + if (typeof e === "object") return `${same} object ${e === null ? "null" : "value"}`; + return `${same} ${typeof e}`; + } +} +for (const value of thrown) console.log(describe(value)); +function rethrow(value: unknown): void { + try { + throw value; + } catch (e) { + if (typeof e !== "string") throw e; + console.log("kept string", e); + } +} +for (const value of ["s", 7, false]) { + try { + rethrow(value); + } catch (e) { + console.log("rethrown", typeof e, String(e)); + } +} +let count = 0; +for (const value of thrown) { + try { + throw value; + } catch (e) { + if (typeof e === "number" && e > 0) count += e; + } +} +console.log(count); From ea611bec028d394e80994e8cb4b9b4629681c999 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 02:52:38 -0500 Subject: [PATCH 68/83] Convert object literals with accessors to unknown again #1490 also took records with accessor slots out of the dyn-convertible domain, which refused passing such a literal where `unknown` is expected. Native APIs read these values through their accessor slots: fileURLToPath, fileURLToPathBuffer and pathToFileURL call the `windows` getter of an options object, so tests/corpus/node24-file-url-options.ts stopped compiling. Records with accessor slots convert to checked-dynamic values again. JSON.stringify of their typed values stays refused, since the typed serializer cannot invoke the getter. --- packages/compiler/src/ir/ir.ts | 3 --- .../object-literal-accessor-values.ts | 14 ++-------- .../object-literal-accessor-values.ts.txt | 26 +++---------------- 3 files changed, 6 insertions(+), 37 deletions(-) diff --git a/packages/compiler/src/ir/ir.ts b/packages/compiler/src/ir/ir.ts index d6ff0fe72..de6bf558c 100644 --- a/packages/compiler/src/ir/ir.ts +++ b/packages/compiler/src/ir/ir.ts @@ -4267,9 +4267,6 @@ function canBoxDynCompositeAt( const shape = getRecord(t.shapeId); if (!shape) return false; if (recordTextCodecClass(shape) !== null) return false; - // Accessor slots hold the getter and setter closures, not property - // values (see accessorSlotProp). - if (shapeHasAccessorSlots(shape)) return false; // Recursive shapes answer coinductively, like isJsonSafeType. if (visiting.has(t.shapeId)) return true; visiting.add(t.shapeId); diff --git a/tests/diagnostics/object-literal-accessor-values.ts b/tests/diagnostics/object-literal-accessor-values.ts index 107ab1f69..e4c456a76 100644 --- a/tests/diagnostics/object-literal-accessor-values.ts +++ b/tests/diagnostics/object-literal-accessor-values.ts @@ -1,11 +1,6 @@ // Object literals with get/set accessors keep their closures in reserved -// slots, so their typed values have no JSON or checked-dynamic form: Node's -// JSON.stringify and dynamic reads invoke the getter, which neither a typed -// serializer nor a converted copy could do. - -function hold(value: unknown): unknown { - return value; -} +// slots, so their typed values have no JSON form: Node's JSON.stringify +// invokes the getter, which the typed serializer cannot do. const counter = { count: 1, @@ -17,11 +12,6 @@ const counter = { // JSON.stringify of the typed value would omit the getter's property. console.log(JSON.stringify(counter)); -// Converting the typed value to `unknown` or `any` would lose the getter. -hold(counter); -const viaAny: any = counter; -console.log(viaAny); - // A literal written into an `any` slot directly keeps its accessor. const direct: any = { get value(): number { diff --git a/tests/harness/__snapshots__/object-literal-accessor-values.ts.txt b/tests/harness/__snapshots__/object-literal-accessor-values.ts.txt index 89bc092aa..372849388 100644 --- a/tests/harness/__snapshots__/object-literal-accessor-values.ts.txt +++ b/tests/harness/__snapshots__/object-literal-accessor-values.ts.txt @@ -1,24 +1,6 @@ -object-literal-accessor-values.ts:18:28 - error SC1090: JSON.stringify of '{ get doubled(): number; count: number }' values (only number, string, boolean, records, arrays, unions of those, and 'unknown' stringify) are not supported yet +object-literal-accessor-values.ts:13:28 - error SC1090: JSON.stringify of '{ get doubled(): number; count: number }' values (only number, string, boolean, records, arrays, unions of those, and 'unknown' stringify) are not supported yet - 17 | // JSON.stringify of the typed value would omit the getter's property. - 18 | console.log(JSON.stringify(counter)); + 12 | // JSON.stringify of the typed value would omit the getter's property. + 13 | console.log(JSON.stringify(counter)); | ^~~~~~~ - 19 | - -object-literal-accessor-values.ts:21:6 - error SC1101: converting typed values to 'unknown' is not supported yet - - 20 | // Converting the typed value to `unknown` or `any` would lose the getter. - 21 | hold(counter); - | ^~~~~~~ - 22 | const viaAny: any = counter; - - hint: numbers, strings, booleans, JSON-safe records/arrays/unions (a deep copy), functions, program class instances, and supported native handles convert into 'unknown' slots; functions, exact-class round trips, and handles preserve identity, while this value's type has no dynamic representation yet - -object-literal-accessor-values.ts:22:21 - error SC1101: converting typed values to 'unknown' is not supported yet - - 21 | hold(counter); - 22 | const viaAny: any = counter; - | ^~~~~~~ - 23 | console.log(viaAny); - - hint: numbers, strings, booleans, JSON-safe records/arrays/unions (a deep copy), functions, program class instances, and supported native handles convert into 'unknown' slots; functions, exact-class round trips, and handles preserve identity, while this value's type has no dynamic representation yet \ No newline at end of file + 14 | \ No newline at end of file From c70d9d7e28d224fc00008696b835cdba213e3477 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Tue, 20 Oct 2026 00:53:19 -0700 Subject: [PATCH 69/83] Refuse returns a generic callback's inferred result would drop members of A callback passed to a generic call without a return annotation can return values the checker unifies into a narrower result type ('return items' beside 'return items.map((i) => ({ ...i, extra }))' infers Item[]). Converting the extended objects into that type copied them and dropped the added members, which Node keeps and a later wider view reads back. Such a return is now refused by name, pointing at the annotation that keeps the members. Co-Authored-By: Claude Opus 5.5 --- .../compiler/src/frontend/lowering/lowerer.ts | 72 +++++++++++++++++++ ...-generic-callback-extended-results.ts.json | 6 ++ .../inferred-callback-drops.ts.json | 6 ++ .../6480-generic-callback-extended-results.ts | 41 +++++++++++ tests/diagnostics/inferred-callback-drops.ts | 13 ++++ .../inferred-callback-drops.ts.txt | 6 ++ 6 files changed, 144 insertions(+) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6480-generic-callback-extended-results.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/inferred-callback-drops.ts.json create mode 100644 tests/corpus/6480-generic-callback-extended-results.ts create mode 100644 tests/diagnostics/inferred-callback-drops.ts create mode 100644 tests/harness/__snapshots__/inferred-callback-drops.ts.txt diff --git a/packages/compiler/src/frontend/lowering/lowerer.ts b/packages/compiler/src/frontend/lowering/lowerer.ts index 27011df24..3edf33d22 100644 --- a/packages/compiler/src/frontend/lowering/lowerer.ts +++ b/packages/compiler/src/frontend/lowering/lowerer.ts @@ -9264,6 +9264,7 @@ export class Lowerer { * lowering goes through here (via lowerExprExpecting) or calls this * directly when the expression was already lowered. */ coerceInto(node: ts.Node, expr: IrExpr, expected: IrType): IrExpr { + refuseInferredCallbackDrops(this, node, expr.type, expected); // A union destination can accept the binding's tagged storage directly. // The checker's single-arm flow type can be stale after a callback writes // the binding. Extracting that arm and wrapping it again would discard the @@ -12984,3 +12985,74 @@ export class Lowerer { return lowerStdlibMethodCall(this, call, access); } } + +/** A callback passed to a generic call without a return annotation, whose + * returns the checker unified into a narrower result type: `trace(name, + * async () => (cond ? items : items.map((i) => ({ ...i, sum }))))` infers + * `T` from the narrower `Item[]`, though at run time the extended objects + * keep `sum`. Converting such a return into the inferred type copies the + * records and drops the members the narrower type lacks, which Node keeps + * and any later wider view (`Promise`) reads back. Refuse + * that conversion by name rather than lose the members silently; an + * annotation on the callback's return type or on the call's type argument + * names a type that keeps them. */ +function refuseInferredCallbackDrops( + lowerer: Lowerer, + node: ts.Node, + actual: IrType, + expected: IrType, +): void { + if (typeEquals(actual, expected)) return; + const structured = (t: IrType): boolean => + t.kind === "record" || t.kind === "array" || t.kind === "promise"; + if (!structured(actual) || !structured(expected)) return; + let site: ts.Node = node; + while (ts.isParenthesizedExpression(site.parent) || ts.isAwaitExpression(site.parent)) + site = site.parent; + let fn: ts.Node | undefined; + if (ts.isReturnStatement(site.parent) && site.parent.expression === site) { + fn = site.parent.parent; + while (fn && !ts.isFunctionLike(fn)) fn = fn.parent; + } else if (ts.isArrowFunction(site.parent) && site.parent.body === site) { + fn = site.parent; + } + if (!fn || !(ts.isArrowFunction(fn) || ts.isFunctionExpression(fn)) || fn.type) return; + let arg: ts.Node = fn; + while (ts.isParenthesizedExpression(arg.parent)) arg = arg.parent; + const call = arg.parent; + if (!(ts.isCallExpression(call) || ts.isNewExpression(call))) return; + if (!call.arguments?.nodes.includes(arg as ts.Expression)) return; + const dropped = droppedMembers(lowerer, actual, expected, new Set()); + if (dropped.length === 0) return; + lowerer.unsupported( + "SC1090", + node, + `returning '${lowerer.fmt(actual)}' from a callback whose result the checker infers as the narrower '${lowerer.fmt(expected)}' (the conversion would drop ${dropped.map((d) => `'${d}'`).join(", ")}, which the object keeps at run time — annotate the callback's return type or the call's type argument with a type that declares them)`, + ); +} + +/** Member names a structural conversion from `from` into `to` drops: + * declared fields of a source record the target record lacks, through + * array elements and promise payloads. */ +function droppedMembers(lowerer: Lowerer, from: IrType, to: IrType, seen: Set): string[] { + if (typeEquals(from, to)) return []; + if (from.kind === "array" && to.kind === "array") + return droppedMembers(lowerer, from.elem, to.elem, seen); + if (from.kind === "promise" && to.kind === "promise") + return droppedMembers(lowerer, from.inner, to.inner, seen); + if (from.kind !== "record" || to.kind !== "record") return []; + const key = `${from.shapeId}>${to.shapeId}`; + if (seen.has(key)) return []; + seen.add(key); + const source = lowerer.shapes.get(from.shapeId); + const target = lowerer.shapes.get(to.shapeId); + if (!source || !target || source.tuple || target.tuple || target.indexValue) return []; + const out: string[] = []; + for (const f of source.fields) { + if (f.name.startsWith("%")) continue; + const t = target.fields.find((x) => x.name === f.name); + if (!t) out.push(f.name); + else out.push(...droppedMembers(lowerer, f.type, t.type, seen).map((n) => `${f.name}.${n}`)); + } + return out; +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6480-generic-callback-extended-results.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6480-generic-callback-extended-results.ts.json new file mode 100644 index 000000000..d3b5c41b7 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6480-generic-callback-extended-results.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6480-generic-callback-extended-results.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/inferred-callback-drops.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/inferred-callback-drops.ts.json new file mode 100644 index 000000000..a6b592336 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/inferred-callback-drops.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/diagnostics/inferred-callback-drops.ts" + ], + "diags": [] +} diff --git a/tests/corpus/6480-generic-callback-extended-results.ts b/tests/corpus/6480-generic-callback-extended-results.ts new file mode 100644 index 000000000..0d75a1f53 --- /dev/null +++ b/tests/corpus/6480-generic-callback-extended-results.ts @@ -0,0 +1,41 @@ +// A callback whose returns extend the elements it was given keeps the +// added members once its result type names them (an annotation on the +// callback or on the type argument), as Node keeps them on the objects. +type Plain = { name: string; n: number }; +type Item = Plain & { extra?: boolean }; +const plain: Plain[] = [ + { name: "a", n: 1 }, + { name: "b", n: 2 }, +]; + +function run(fn: () => T): T { + return fn(); +} +async function runAsync(fn: () => Promise): Promise { + return fn(); +} + +const annotated = run((): Item[] => { + if (plain.length === 0) return plain; + return plain.map((x) => ({ ...x, extra: x.n > 1 })); +}); +console.log(JSON.stringify(annotated)); + +const viaTypeArgument = run(() => { + if (plain.length > 5) return plain; + return plain.map((x) => ({ ...x, extra: true })); +}); +console.log(JSON.stringify(viaTypeArgument), viaTypeArgument[0]!.extra); + +runAsync(async (): Promise => { + if (plain.length === 0) return plain; + return plain.map((x) => ({ ...x, extra: false })); +}).then((items) => console.log(items)); + +// Callbacks whose results the checker types by their own returns keep +// every member already. +const mapped = plain.map((x) => (x.n > 1 ? x : { ...x, extra: true })); +const asItems: Item[] = mapped; +console.log(JSON.stringify(asItems)); +const summed = plain.reduce((acc, x) => ({ ...acc, total: acc.total + x.n }), { total: 0, label: "sum" }); +console.log(JSON.stringify(summed)); diff --git a/tests/diagnostics/inferred-callback-drops.ts b/tests/diagnostics/inferred-callback-drops.ts new file mode 100644 index 000000000..0d4808b6a --- /dev/null +++ b/tests/diagnostics/inferred-callback-drops.ts @@ -0,0 +1,13 @@ +type Plain = { name: string; n: number }; +type Item = Plain & { extra?: boolean }; +const plain: Plain[] = [{ name: "a", n: 1 }]; +function run(fn: () => T): T { + return fn(); +} +// The checker unifies both returns into Plain[]; the extended objects keep +// `extra` in Node, so converting them into Plain would drop it silently. +const items: Item[] = run(() => { + if (plain.length === 0) return plain; + return plain.map((x) => ({ ...x, extra: true })); +}); +console.log(items.length); diff --git a/tests/harness/__snapshots__/inferred-callback-drops.ts.txt b/tests/harness/__snapshots__/inferred-callback-drops.ts.txt new file mode 100644 index 000000000..daa62f484 --- /dev/null +++ b/tests/harness/__snapshots__/inferred-callback-drops.ts.txt @@ -0,0 +1,6 @@ +inferred-callback-drops.ts:11:10 - error SC1090: returning '{ extra: boolean; n: number; name: string }[]' from a callback whose result the checker infers as the narrower '{ n: number; name: string }[]' (the conversion would drop 'extra', which the object keeps at run time — annotate the callback's return type or the call's type argument with a type that declares them) is not supported yet + + 10 | if (plain.length === 0) return plain; + 11 | return plain.map((x) => ({ ...x, extra: true })); + | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 12 | }); \ No newline at end of file From 66d2010388cb572fc5617c322555e2e8120f7eb5 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 03:08:09 -0500 Subject: [PATCH 70/83] Remove lazy loaders together and keep their receiver rule Lazy loaders that share a var statement each removed their declarators on their own, and declarators of one statement removed separately leave overlapping edits, of which applyEdits keeps the first. A later loader in the statement (one using __toESM, say) stayed behind and the interop pass refused the package. Every removed declarator of the pass (the interop requires, the loaders and the unused export tables) now goes into one set, removed once. A call through a loader (`load().fn()`) respelled the callee as the export's live read, which drops the namespace receiver. Loader calls now take the rule namespace calls already use: the callee must be a top-level function that never reads `this`; other calls keep the package refused. --- .../frontend/npm-static-bundled-esm.test.ts | 32 +++++++++++++++ .../src/frontend/npm-static-bundled-esm.ts | 39 +++++++++++-------- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts b/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts index 1e2ba24a3..98decbb41 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-esm.test.ts @@ -152,6 +152,38 @@ test("minified initializers sharing a var statement lower the same way", () => { ); }); +test("memoized lazy loaders sharing a var statement lower together", () => { + const lowered = expectSameBehavior(`${helpers}var a_exports = {}; +__export(a_exports, { v: () => v }); +var v, init_a = __esm({ "a.ts"() { console.log("a init"); v = 1; } }); +var b_exports = {}; +__export(b_exports, { default: () => w }); +var w, init_b = __esm({ "b.ts"() { console.log("b init"); w = 2; } }); +var nsA, loadA = () => nsA ??= (init_a(), __toCommonJS(a_exports)), nsB, loadB = () => nsB ??= __toESM((init_b(), __toCommonJS(b_exports))); +console.log(loadA().v, loadB().default, loadA().v); +`); + expect(lowered).not.toContain("loadA"); + expect(lowered).not.toContain("loadB"); +}); + +test("calls through a memoized lazy loader keep the receiver rule", () => { + const source = (use: string): string => `${helpers}var w_exports = {}; +__export(w_exports, { who: () => who, plain: () => plain }); +function who() { return this.__esModule; } +function plain(x) { return "plain " + x; } +var init_w = __esm({ "w.ts"() {} }); +var ns, load = () => ns ??= (init_w(), __toCommonJS(w_exports)); +console.log(${use}); +`; + expect(lower(source("load().who()"))).toEqual({ + degrade: expect.stringContaining( + "calls only of exported functions that do not read 'this'", + ) as string, + }); + const lowered = expectSameBehavior(source("load().plain(1), (load().plain)(2)")); + expect(lowered).not.toContain("load()"); +}); + test.each([ ["is asynchronous", `var init_a = __esm({ async "a.ts"() { await 0; } });\ninit_a();`], [ diff --git a/packages/compiler/src/frontend/npm-static-bundled-esm.ts b/packages/compiler/src/frontend/npm-static-bundled-esm.ts index 6e9c4f315..9c77128e1 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-esm.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-esm.ts @@ -449,7 +449,6 @@ export function lowerBundledEsmInits( } } } - edits.push(...removeDeclarators(sf, deadDeclarators)); /* Namespaces of lazily initialized modules read through members. */ let mainTarget: string | null = null; @@ -500,6 +499,17 @@ export function lowerBundledEsmInits( }; const getterText = (value: ts.Expression): string => applyEdits(source, value.getStart(sf), value.getEnd(), edits); + /** A call through the namespace (`ns.fn()`, `L().fn()`) passes the object + * the read came from as `this`; the lowered object differs from Node's + * module object (`__esModule`, property attributes), so it must stay + * unobservable: the callee is a top-level function that never reads + * `this`. */ + const receiverHidden = (values: ReadonlyMap, name: string): boolean => { + const value = values.get(name); + if (value === undefined || !ts.isIdentifier(value) || !stable(value.text)) return false; + const decl = top.get(value.text)?.node; + return decl !== undefined && ts.isFunctionDeclaration(decl) && !readsThis(decl); + }; /* Memoized lazy loaders of those namespaces, `L = () => M ??= NS` (a * bundle plugin's lazy re-exports): every `L().name` becomes the @@ -564,7 +574,7 @@ export function lowerBundledEsmInits( const table = tables.get(target); if (table === undefined) continue; const loaderWhy = { - degrade: `its memoized lazy loader '${name}' returns the namespace of a bundled ES module as a value, a module object the static rewrite cannot represent (only member reads of its result compile)`, + degrade: `its memoized lazy loader '${name}' returns the namespace of a bundled ES module as a value, a module object the static rewrite cannot represent (only member reads of its result compile, and calls only of exported functions that do not read 'this')`, }; const memo = memoize.left.text; const memoDecl = top.get(memo)?.node; @@ -612,6 +622,10 @@ export function lowerBundledEsmInits( const member = (access as ts.PropertyAccessExpression).name.text; const value = entries.get(member); if (value === undefined || shadowed(access, table.entries)) return loaderWhy; + // The rewrite reads the export without the namespace object, so a + // call through it (`L().fn()`) loses its receiver. + if (calledWithReceiver(access as ts.Expression) && !receiverHidden(entries, member)) + return loaderWhy; const init = initCall === null ? "" : `${initCall.getText(sf)}, `; edits.push({ start: access.getStart(sf), @@ -619,7 +633,7 @@ export function lowerBundledEsmInits( text: `(${init}${getterText(value)})`, }); } - edits.push(...removeDeclarators(sf, new Set([d, memoDecl]))); + deadDeclarators.add(d).add(memoDecl); loaderSpans.push({ start: d.getStart(sf), end: d.getEnd() }); lowered.add(target); } @@ -664,24 +678,13 @@ export function lowerBundledEsmInits( else break; } const values = new Map(table.entries); - // A call through the namespace (`ns.fn()`) passes the object the read - // came from as `this`; the lowered object differs from Node's module - // object (`__esModule`, property attributes), so it must stay - // unobservable: the callee is a top-level function that never reads - // `this`. - const receiverHidden = (name: string): boolean => { - const value = values.get(name); - if (value === undefined || !ts.isIdentifier(value) || !stable(value.text)) return false; - const decl = top.get(value.text)?.node; - return decl !== undefined && ts.isFunctionDeclaration(decl) && !readsThis(decl); - }; const memberRead = (access: ts.Node | undefined, of: ts.Node): boolean => access !== undefined && ts.isPropertyAccessExpression(access) && access.expression === of && values.has(access.name.text) && plainRead(access) && - (!calledWithReceiver(access) || receiverHidden(access.name.text)); + (!calledWithReceiver(access) || receiverHidden(values, access.name.text)); const holder = ns.parent; // `__reExport(main, ns, module.exports)`: a star re-export of the // module into the bundle's own exports (`export * from` in its entry). @@ -792,8 +795,12 @@ export function lowerBundledEsmInits( ) return { degrade: ESM_SHAPE_DEGRADE }; edits.push({ start: table.stmt.getStart(sf), end: table.stmt.getEnd(), text: "" }); - edits.push(...removeDeclarators(sf, new Set([decl]))); + deadDeclarators.add(decl); } + // Every removed declarator in one pass: declarators of one statement + // removed separately would leave overlapping edits, and applyEdits keeps + // only the first of them. + edits.push(...removeDeclarators(sf, deadDeclarators)); /* The run-once closures. */ for (const init of inits.values()) { From 603c6720f9f7dd8e155bbf3e3bbfd62d848da059 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 03:13:40 -0500 Subject: [PATCH 71/83] Keep function export rewrites from capturing names or keys The pass respells `exports.x` and `module.exports.x` through the exported function's name. A parameter, local, nested function or catch binding of that name between a respelled read and the top level would capture it, so the pass now applies only when the file binds the name once, at its top-level declaration. `{ exports }` after the rebinding respelled to `{ SemVer }`, which renames the property; the shorthand now becomes `{ exports: SemVer }`. --- .../npm-static-cjs-exports-alias.test.ts | 81 +++++++++++++++++++ .../frontend/npm-static-cjs-exports-alias.ts | 20 ++++- 2 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 packages/compiler/src/frontend/npm-static-cjs-exports-alias.test.ts diff --git a/packages/compiler/src/frontend/npm-static-cjs-exports-alias.test.ts b/packages/compiler/src/frontend/npm-static-cjs-exports-alias.test.ts new file mode 100644 index 000000000..c3704ea29 --- /dev/null +++ b/packages/compiler/src/frontend/npm-static-cjs-exports-alias.test.ts @@ -0,0 +1,81 @@ +import { spawnSync } from "node:child_process"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterAll, expect, test } from "vitest"; +import { nodeFrontendServices } from "./services-node.js"; + +const dirs: string[] = []; +afterAll(() => { + for (const dir of dirs) rmSync(dir, { recursive: true, force: true }); +}); + +function rewrite(source: string): string | null { + return nodeFrontendServices().cjsFunctionExports(source, "/pkg/index.js"); +} + +function run(file: string) { + const result = spawnSync(process.execPath, [file], { encoding: "utf8" }); + return { status: result.status, stdout: result.stdout, stderr: result.stderr }; +} + +/** The rewritten module prints what the original prints. */ +function expectSameBehavior(source: string): string { + const result = rewrite(source); + expect(typeof result).toBe("string"); + const dir = mkdtempSync(join(tmpdir(), "scriptc-cjs-function-exports-")); + dirs.push(dir); + const original = join(dir, "original.cjs"); + const rewritten = join(dir, "rewritten.cjs"); + writeFileSync(original, source); + writeFileSync(rewritten, result as string); + const oracle = run(original); + expect(oracle.status).toBe(0); + expect(run(rewritten)).toEqual(oracle); + return result as string; +} + +const report = ` +console.log(typeof module.exports, module.exports === Version, Object.keys(Version).join(",")); +`; + +test("members of a function export object read and write the function", () => { + const rewritten = expectSameBehavior(`exports = module.exports = Version; +exports.parse = parse; +module.exports.major = (v) => parse(v)[0]; +function Version(v) { return { v }; } +function parse(v) { return v.split(".").map(Number); } +console.log(exports.major("2.1.0"), module.exports.parse("1.2").length); +${report}`); + expect(rewritten).toContain("Version.parse = parse"); + expect(rewritten).not.toContain("exports = module.exports"); +}); + +test.each([ + ["a parameter", "function inner(Version) { return exports.parse; }"], + ["a local", "function inner() { const Version = {}; return exports.parse; }"], + ["a nested function", "function inner() { function Version() {} return exports.parse; }"], + [ + "a catch binding", + "function inner() { try { throw 0; } catch (Version) { return exports.parse; } }", + ], +])("a nested binding of the function's name (%s) keeps the module as it is", (_, inner) => { + expect( + rewrite(`exports = module.exports = Version; +exports.parse = parse; +function Version() {} +function parse() {} +${inner} +`), + ).toBeNull(); +}); + +test("an `exports` shorthand property keeps its key", () => { + const rewritten = expectSameBehavior(`exports = module.exports = Version; +exports.tag = "v"; +function Version() {} +const holder = { exports }; +console.log(Object.keys(holder).join(","), holder.exports === Version, holder.exports.tag); +${report}`); + expect(rewritten).toContain("{ exports: Version }"); +}); diff --git a/packages/compiler/src/frontend/npm-static-cjs-exports-alias.ts b/packages/compiler/src/frontend/npm-static-cjs-exports-alias.ts index c88c9949e..dfe45b862 100644 --- a/packages/compiler/src/frontend/npm-static-cjs-exports-alias.ts +++ b/packages/compiler/src/frontend/npm-static-cjs-exports-alias.ts @@ -15,12 +15,14 @@ * runs and nothing replaces the export object again: the statement is a * top-level statement preceded only by inert declarations, it is the * file's only write of `module.exports` and `exports`, the function is a - * top-level declaration nothing reassigns, and `exports`/`module` are the - * CommonJS globals (never declared in the file). Other shapes answer + * top-level declaration nothing reassigns and the file's only binding of + * its name, and `exports`/`module` are the CommonJS globals (never + * declared in the file). Other shapes answer * null and keep their existing treatment. */ import * as ts from "./ts7/syntax.js"; import { applyEdits, + countDeclarations, declaredNames, isWritten, references, @@ -105,7 +107,10 @@ export function normalizeCjsFunctionExports(sf: ts.SourceFile): string | null { const fnDecls = stmts.filter( (st) => ts.isFunctionDeclaration(st) && st.name?.text === fnName && st.body !== undefined, ); - if (fnDecls.length !== 1) return null; + // The respelled reads name the function from wherever they sit, so no + // other binding of its name (a parameter, a local, a nested function) + // may exist anywhere in the file. + if (fnDecls.length !== 1 || countDeclarations(sf, fnName) !== 1) return null; const edits: Edit[] = []; let unsafe = false; @@ -131,7 +136,14 @@ export function normalizeCjsFunctionExports(sf: ts.SourceFile): string | null { unsafe = true; continue; } - edits.push({ start: r.getStart(sf), end: r.getEnd(), text: fnName }); + // `{ exports }` keeps its key: the shorthand spells out the value. + const shorthand = + r.parent !== undefined && ts.isShorthandPropertyAssignment(r.parent) && r.parent.name === r; + edits.push({ + start: r.getStart(sf), + end: r.getEnd(), + text: shorthand ? `exports: ${fnName}` : fnName, + }); continue; } if (r.text === "module") { From e1e45f12b9a86803c51c83e52a685080295aa61c Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 03:55:43 -0500 Subject: [PATCH 72/83] Give converted object literals their accessors An object literal with get/set accessors converted to `any` or `unknown` listed its reserved closure slots (`%get:x`) as data keys, so a dynamic read of the accessor answered undefined without calling the getter, and Object.keys and JSON.stringify listed the slot instead of the property. The checked-dynamic copy now defines each accessor as one enumerable, configurable property whose getter and setter are the slot closures, in declaration order: the record shape records its accessor key order (`accessorOrder`). Live views of such records build that copy, and checking a copy back into the record reads the closures from the accessor's getter and setter. Reads, writes, Object.keys, Object.entries, JSON.stringify and property descriptors answer as in Node. --- packages/compiler/src/backend/llvm/dyn.ts | 63 +++++++++++++++++-- .../src/backend/llvm/expr-stream-bridges.ts | 2 + packages/compiler/src/frontend/type-mapper.ts | 43 ++++++++----- .../src/ir/function-references.test.ts | 1 + packages/compiler/src/ir/ir.ts | 4 ++ .../object-literal-accessors-dyn.ts.json | 6 ++ packages/runtime/src/scr_dyn.h | 5 ++ packages/runtime/src/scr_json.c | 25 ++++++++ tests/corpus/object-literal-accessors-dyn.ts | 31 +++++++++ .../object-literal-accessors-dyn.ts.json | 1 + 10 files changed, 162 insertions(+), 19 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/object-literal-accessors-dyn.ts.json create mode 100644 tests/corpus/object-literal-accessors-dyn.ts create mode 100644 tests/harness/corpus-costs/object-literal-accessors-dyn.ts.json diff --git a/packages/compiler/src/backend/llvm/dyn.ts b/packages/compiler/src/backend/llvm/dyn.ts index 86b4f5be8..7e414daee 100644 --- a/packages/compiler/src/backend/llvm/dyn.ts +++ b/packages/compiler/src/backend/llvm/dyn.ts @@ -34,6 +34,7 @@ import { InternalCompilerError } from "../../errors.js"; * ScrDynPath { parent, key, index } — the %ScrDynPath type. */ import type { IrRecordShape, IrType, IrUnionDef } from "../../ir/ir.js"; import { + accessorSlotProp, DYN_HANDLE_KINDS, isDirentShape, isDynTypedRefType, @@ -613,6 +614,18 @@ export class LlDyn { * tracking does not count the read's reference. Returns the value and * the matching release. */ private memberRead(B: BlockBuilder, key: string, t: IrType): { value: string; release: string } { + // An accessor slot reads the getter or setter of the accessor property + // the checked-dynamic copy holds for it (see accessorSlotProp). + const slot = accessorSlotProp(key); + if (slot !== null) { + this.host.declare(`declare ptr @scr_dyn_obj_accessor_fn(ptr, ptr, ${this.S}, i1 zeroext)`); + this.host.declare(`declare void @scr_dyn_release(ptr)`); + const fn = B.tmp(); + B.line( + `${fn} = call ptr @scr_dyn_obj_accessor_fn(ptr %d, ptr ${this.host.cstr(slot.prop)}, ${this.S} ${Buffer.byteLength(slot.prop, "utf8")}, i1 ${slot.kind === "set" ? "true" : "false"})`, + ); + return { value: fn, release: "scr_dyn_release" }; + } const held = this.isRecursiveShape(t) || this.holdsRecursiveShape(t); const release = held ? "scr_dyn_release_held" : "scr_dyn_release"; this.host.declare(`declare void @${release}(ptr)`); @@ -2687,13 +2700,53 @@ export class LlDyn { // Keys insert in DECLARED order (JS insertion order); internal // '%'-fields follow so a record→dyn→record round trip keeps them. const byName = new Map(shape.fields.map((f) => [f.name, f])); - const order = shape.declaredOrder ?? shape.fields.map((f) => f.name); + // An accessor property is one key whose getter and setter are the + // slot closures (see accessorSlotProp), in accessorOrder's place. + const order = shape.accessorOrder ?? shape.declaredOrder ?? shape.fields.map((f) => f.name); const inOrder = new Set(order); - const dynFields = [ - ...order.map((n) => byName.get(n)).filter((f) => f !== undefined), - ...shape.fields.filter((f) => !inOrder.has(f.name)), + const accessors = new Set( + shape.accessorOrder === undefined + ? [] + : shape.fields.flatMap((f) => { + const slot = accessorSlotProp(f.name); + return slot === null ? [] : [slot.prop]; + }), + ); + type DynField = { name: string; type: IrType } | { accessor: string }; + const dynFields: DynField[] = [ + ...order.flatMap((n) => { + if (accessors.has(n)) return [{ accessor: n } as DynField]; + const f = byName.get(n); + return f === undefined ? [] : [f as DynField]; + }), + ...shape.fields.filter( + (f) => + !inOrder.has(f.name) && !(accessors.size > 0 && accessorSlotProp(f.name) !== null), + ), ]; - for (const f of dynFields) { + for (const entry of dynFields) { + if ("accessor" in entry) { + host.declare( + `declare void @scr_dyn_obj_set_accessor(ptr, ptr, ${host.sizeType}, ptr, ptr)`, + ); + const boxed = (kind: "get" | "set"): string => { + const slot = byName.get(`%${kind}:${entry.accessor}`); + if (slot === undefined) return "null"; + const fv = loadFieldOf(slot.name, slot.type); + const conv = B.tmp(); + B.line( + `${conv} = call ptr @${this.toDynHelper(slot.type)}(${this.valTy(slot.type)} ${fv})`, + ); + return conv; + }; + const getter = boxed("get"); + const setter = boxed("set"); + B.line( + `call void @scr_dyn_obj_set_accessor(ptr ${d}, ptr ${host.cstr(entry.accessor)}, ${host.sizeType} ${Buffer.byteLength(entry.accessor, "utf8")}, ptr ${getter}, ptr ${setter}) ; ${llvmCommentText(entry.accessor)}`, + ); + continue; + } + const f = entry; const klen = Buffer.byteLength(f.name, "utf8"); const fv = loadFieldOf(f.name, f.type); // An ABSENT optional field contributes no key. diff --git a/packages/compiler/src/backend/llvm/expr-stream-bridges.ts b/packages/compiler/src/backend/llvm/expr-stream-bridges.ts index 134c74b92..1841053df 100644 --- a/packages/compiler/src/backend/llvm/expr-stream-bridges.ts +++ b/packages/compiler/src/backend/llvm/expr-stream-bridges.ts @@ -15,6 +15,7 @@ import { isDirentShape, isDynTypedRefType, isRefCounted, + shapeHasAccessorSlots, typeKey, } from "../../ir/ir.js"; import { mangleFunction, mangleGlobal, mangleRecordStruct } from "../mangle.js"; @@ -965,6 +966,7 @@ export function streamTypedRefMaterializeAdapter( * Declared-field records and tuples cover the live stream values. */ const ordinary = shape.indexValue || + shapeHasAccessorSlots(shape) || shape.fields.some((field) => field.name === "handleEvent" && field.type.kind === "func"); if (ordinary) { const out = B.tmp(); diff --git a/packages/compiler/src/frontend/type-mapper.ts b/packages/compiler/src/frontend/type-mapper.ts index 94506f034..b095ecbb8 100644 --- a/packages/compiler/src/frontend/type-mapper.ts +++ b/packages/compiler/src/frontend/type-mapper.ts @@ -5895,6 +5895,7 @@ interface RecordShapeParts { fields: { name: string; type: IrType }[]; indexValue?: IrType; declaredOrder?: string[]; + accessorOrder?: string[]; } function mapRecordType(widened: ts.Type, ctx: TypeMapperCtx): IrType | null { @@ -5931,15 +5932,14 @@ function mapRecordType(widened: ts.Type, ctx: TypeMapperCtx): IrType | null { // (generic type parameters, mixin instantiations) cannot intern by // checker-type identity — fence recursive generic-open shapes. if (contextResolutions !== sensitivityAtEntry) return null; - return { - kind: "record", - shapeId: shapes.finalizeRecursive( - widened, - inner.fields, - inner.indexValue, - inner.declaredOrder, - ), - }; + const recursive = shapes.finalizeRecursive( + widened, + inner.fields, + inner.indexValue, + inner.declaredOrder, + ); + adoptAccessorOrder(shapes.get(recursive), inner.accessorOrder); + return { kind: "record", shapeId: recursive }; } // A record with no typed payload contract is a view of an existing // object, including across parameters and returns. Building a native @@ -5954,15 +5954,20 @@ function mapRecordType(widened: ts.Type, ctx: TypeMapperCtx): IrType | null { return DYN; const refined = refinedConstituentShape(widened, inner, ctx); if (refined !== null) return { kind: "record", shapeId: refined }; - return { - kind: "record", - shapeId: shapes.intern(inner.fields, false, inner.indexValue, inner.declaredOrder), - }; + const shapeId = shapes.intern(inner.fields, false, inner.indexValue, inner.declaredOrder); + adoptAccessorOrder(shapes.get(shapeId), inner.accessorOrder); + return { kind: "record", shapeId }; } finally { shapes.inProgress.delete(widened); } } +/** The first-seen accessor key order of a shape with accessor slots. */ +function adoptAccessorOrder(shape: IrRecordShape | undefined, order: string[] | undefined): void { + if (shape !== undefined && order !== undefined && shape.accessorOrder === undefined) + shape.accessorOrder = order; +} + /** A refinement intersection (`Expr & { kind: "mul" }`, which the checker * reduces to `Mul & { kind: "mul" }`) keeps the shape of the constituent it * refines when the refinement changes no field's representation. Recursive @@ -6322,8 +6327,18 @@ function mapRecordTypeInner( const declaredOrder = esOwnKeyOrder( fields.filter((f) => accessorSlotProp(f.name) === null).map((f) => f.name), ); + // With accessors: every own key in that order, an accessor property + // named once for its get/set pair (the checked-dynamic copy lists it). + const accessorOrder = fields.some((f) => accessorSlotProp(f.name) !== null) + ? esOwnKeyOrder([...new Set(fields.map((f) => accessorSlotProp(f.name)?.prop ?? f.name))]) + : undefined; fields.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)); - return { fields, ...(indexValue ? { indexValue } : {}), declaredOrder }; + return { + fields, + ...(indexValue ? { indexValue } : {}), + declaredOrder, + ...(accessorOrder ? { accessorOrder } : {}), + }; } } diff --git a/packages/compiler/src/ir/function-references.test.ts b/packages/compiler/src/ir/function-references.test.ts index c2d461e09..2c5a45dd5 100644 --- a/packages/compiler/src/ir/function-references.test.ts +++ b/packages/compiler/src/ir/function-references.test.ts @@ -170,6 +170,7 @@ const MODULE_FIELDS: Record>, Role> = { "records[].id": "other", "records[].fields[].name": "other", "records[].declaredOrder[]": "other", + "records[].accessorOrder[]": "other", "unions[].id": "other", "unions[].discriminant.field": "other", "unions[].discriminant.cases[].values[]": "other", diff --git a/packages/compiler/src/ir/ir.ts b/packages/compiler/src/ir/ir.ts index de6bf558c..34e70f3f0 100644 --- a/packages/compiler/src/ir/ir.ts +++ b/packages/compiler/src/ir/ir.ts @@ -1621,6 +1621,10 @@ export interface IrRecordShape { * fields carry that declaredOrder OMITS are internal '%'-fields (Dirent's * %dtype) — hidden from every key-order surface, JSON included. */ declaredOrder?: string[]; + /** Shapes with accessor slots: every own key in declaration order, with + * an accessor property named once (see accessorSlotProp). The + * checked-dynamic copy lists its keys in this order. */ + accessorOrder?: string[]; } /** fs.Dirent rows: the hidden %dtype entry kind (libuv's UV_DIRENT diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/object-literal-accessors-dyn.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/object-literal-accessors-dyn.ts.json new file mode 100644 index 000000000..ffe0c75d3 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/object-literal-accessors-dyn.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/object-literal-accessors-dyn.ts" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_dyn.h b/packages/runtime/src/scr_dyn.h index 6fdbbb408..ff6763cda 100644 --- a/packages/runtime/src/scr_dyn.h +++ b/packages/runtime/src/scr_dyn.h @@ -1076,6 +1076,11 @@ extern SCR_TL size_t scr_uncloneable_count; ScrDyn *scr_dyn_from_error(const ScrError *e); void scr_error_register_dyn(const ScrVt *vt, ScrDyn *(*box)(void *)); ScrDyn *scr_error_dyn_fields(const ScrError *e); +/* An accessor property of a fresh object: enumerable and configurable, + * with the given getter and setter (either may be NULL). Moves both. */ +void scr_dyn_obj_set_accessor(ScrDyn *obj, const char *key, size_t key_len, ScrDyn *getter, ScrDyn *setter); +/* The getter or setter of an own accessor property, +1 (undefined when none). */ +ScrDyn *scr_dyn_obj_accessor_fn(const ScrDyn *obj, const char *key, size_t key_len, bool setter); void scr_error_commit_dyn(ScrError *e, const ScrDyn *view); /* The reverse extraction, riding the same identity cache: a dyn error * that came from a runtime ScrError answers THAT instance (+1; diff --git a/packages/runtime/src/scr_json.c b/packages/runtime/src/scr_json.c index c42e66734..131514d4b 100644 --- a/packages/runtime/src/scr_json.c +++ b/packages/runtime/src/scr_json.c @@ -4356,6 +4356,31 @@ void scr_dyn_obj_set(ScrDyn *obj, const char *key, size_t key_len, ScrDyn *value scr_dyn_obj_put(obj, key, key_len, value); } +/* The getter (or, with `setter`, the setter) of the own accessor property + * `key`: +1, or the undefined value when the property is not an accessor + * or has none. */ +ScrDyn *scr_dyn_obj_accessor_fn(const ScrDyn *obj, const char *key, size_t key_len, bool setter) { + const ScrDynEntry *entry = obj->kind == SCR_DYN_OBJ ? scr_dyn_find_entry(obj, key, key_len) : NULL; + ScrDyn *fn = entry && entry->accessor ? (setter ? entry->setter : entry->getter) : NULL; + return scr_dyn_retain(fn ? fn : scr_dyn_undefined()); +} + +/* An object literal's get/set accessor property on a fresh copy: + * enumerable and configurable, with the given getter and setter functions + * (either NULL when the literal declares none). Moves both. */ +void scr_dyn_obj_set_accessor(ScrDyn *obj, const char *key, size_t key_len, ScrDyn *getter, ScrDyn *setter) { + scr_dyn_obj_put(obj, key, key_len, scr_dyn_retain(scr_dyn_undefined())); + ScrDynEntry *entry = scr_dyn_find_entry(obj, key, key_len); + if (!entry) { + scr_dyn_release(getter); + scr_dyn_release(setter); + return; + } + entry->accessor = true; + entry->getter = getter; + entry->setter = setter; +} + /* A class instance's view takes its native field values whatever the * attributes copied into it (a frozen instance's view is built the same way * as any other). Consumes value. */ diff --git a/tests/corpus/object-literal-accessors-dyn.ts b/tests/corpus/object-literal-accessors-dyn.ts new file mode 100644 index 000000000..3b7dc95e4 --- /dev/null +++ b/tests/corpus/object-literal-accessors-dyn.ts @@ -0,0 +1,31 @@ +// Object literals with get/set accessors held as `any` or `unknown` keep +// their accessors, as in Node: a read calls the getter, a write calls the +// setter, Object.keys, Object.entries and JSON.stringify list the accessor +// in declaration order and call the getter, and the property descriptor +// has a get and set function. Writes to data properties reach the +// original object. + +function dyn(value: unknown): any { + return value; +} + +let count = 0; +const lit = { + a: 1, + get g(): number { count++; return 42; }, + set g(v: number) { console.log("set", v); }, + b: 2, + get only(): string { return "read-only"; }, +}; +const d = dyn(lit); +console.log("read", d.g, d.only, count); +console.log("keys", Object.keys(d).join(","), JSON.stringify(d), count); +console.log("entries", Object.entries(d).map((e) => e.join("=")).join(";")); +console.log("in", "g" in d, Object.hasOwn(d, "g")); +d.g = 7; +d.a = 5; +console.log("write", lit.a, d.a); +const desc = Object.getOwnPropertyDescriptor(d, "g")!; +console.log("descriptor", typeof desc.get, typeof desc.set, desc.enumerable, desc.configurable); +const u: unknown = lit; +console.log("unknown", (u as any).g, JSON.stringify(u)); diff --git a/tests/harness/corpus-costs/object-literal-accessors-dyn.ts.json b/tests/harness/corpus-costs/object-literal-accessors-dyn.ts.json new file mode 100644 index 000000000..9643a761b --- /dev/null +++ b/tests/harness/corpus-costs/object-literal-accessors-dyn.ts.json @@ -0,0 +1 @@ +{ "plain": 2903, "san": 3777 } From 1ccda45f7dcef77db7727a6a929ecf71ceea03d4 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 03:59:25 -0500 Subject: [PATCH 73/83] Decode an AST list's position only when it is read Every read of an AST list field built an AstNodeList whose pos and end were decoded from the wire first, though most readers only use `nodes`. The list now keeps the wire file and the list's index, and pos and end decode when they are read. The allocation per read stays the same size, and a decoded file still holds no list objects. --- packages/compiler/src/frontend/ts7/ast-node.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/compiler/src/frontend/ts7/ast-node.ts b/packages/compiler/src/frontend/ts7/ast-node.ts index 92ab5e4b8..3760fff8d 100644 --- a/packages/compiler/src/frontend/ts7/ast-node.ts +++ b/packages/compiler/src/frontend/ts7/ast-node.ts @@ -19,13 +19,22 @@ import { SyntaxKind, NodeFlags } from "./enums.js"; /** A list of child nodes with the list's own position, the shape of tsc-ts's * NodeList. The file keeps the nodes array, not this object: each read of a * list field returns a new AstNodeList over the same array, so `nodes` keeps - * its identity and a decoded file holds no list objects. */ + * its identity and a decoded file holds no list objects. The position is + * decoded from the wire when it is read; most readers only use `nodes`. */ export class AstNodeList { constructor( - readonly pos: number, - readonly end: number, + private readonly wire: AstWireFile, + private readonly index: number, readonly nodes: readonly T[], ) {} + + get pos(): number { + return this.wire.pos(this.index) >>> 0; + } + + get end(): number { + return this.wire.end(this.index) >>> 0; + } } /** One immutable AST response owns one object per node and one array per @@ -83,7 +92,7 @@ export class AstFile { this.lists[index] = nodes; this.materialized?.(); } - return new AstNodeList(this.wire.pos(index) >>> 0, this.wire.end(index) >>> 0, nodes); + return new AstNodeList(this.wire, index, nodes); } namedChild(node: AstNode, name: string): number { From d968975f346840a3bdb84ca5c4914977de6f03ff Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Tue, 20 Oct 2026 01:14:02 -0700 Subject: [PATCH 74/83] Build dict-spreading literals with other members as checked-dynamic objects An object literal that spreads an index-signature record but has members outside the scalar join (an array of labels), an unknown-valued spread (Record), or a member a later spread may overwrite with another type now maps to the checked-dynamic value: it is built like a JavaScript object literal, so spreads copy their own keys in order and later keyed writes land on the same object. 'c && { k: v }' spreads contribute k exactly when c is truthy even where the checker's union of the two does not map (an AbortSignal member), and a conditional spread onto an 'unknown' field leaves the checked-dynamic undefined in its empty arm. Co-Authored-By: Claude Opus 5.5 --- .../lowering/expressions/object-literals.ts | 34 +++++++- packages/compiler/src/frontend/type-mapper.ts | 77 ++++++++++++++++--- .../6475-dynamic-spread-literals.ts.json | 6 ++ tests/corpus/6475-dynamic-spread-literals.ts | 63 +++++++++++++++ 4 files changed, 167 insertions(+), 13 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6475-dynamic-spread-literals.ts.json create mode 100644 tests/corpus/6475-dynamic-spread-literals.ts diff --git a/packages/compiler/src/frontend/lowering/expressions/object-literals.ts b/packages/compiler/src/frontend/lowering/expressions/object-literals.ts index 5a9d0ea10..77c4c8fc5 100644 --- a/packages/compiler/src/frontend/lowering/expressions/object-literals.ts +++ b/packages/compiler/src/frontend/lowering/expressions/object-literals.ts @@ -1700,7 +1700,11 @@ export function lowerObjectLiteral( } shapeMismatch(prop); } - const absent = lowerer.absentFieldValue(fieldType, locOf(prop)); + // An 'unknown' slot holds the checked-dynamic undefined for the + // empty arm, the absent-property read the width copy uses too. + const absent = + lowerer.absentFieldValue(fieldType, locOf(prop)) ?? + (fieldType.kind === "dyn" ? dynUndefinedExpr(locOf(prop)) : null); if (!absent) { lowerer.unsupported( "SC1090", @@ -2628,6 +2632,14 @@ export function andSpreadOf( while (ts.isParenthesizedExpression(right)) right = right.expression; if (!ts.isObjectLiteralExpression(right)) return null; const spreadType = lowerer.mapTypeOf(lowerer.typeOf(e)); + // Every falsy value is a primitive, which spreads nothing, so `c && { + // ... }` contributes the literal's members exactly when c is truthy, + // whatever c's type. The checker's union of the two decides the + // representation where it maps; where it does not (a member typed by a + // checked-dynamic value such as an AbortSignal), the syntax decides. + if (spreadType === null || spreadType.kind === "dyn") { + return syntacticAndSpread(right, e.left); + } if (spreadType?.kind !== "union") return null; const arms = lowerer.unions.get(spreadType.unionId)?.arms ?? []; if ( @@ -2659,6 +2671,26 @@ export function andSpreadOf( return { cond: e.left, props, whenTrue: true }; } +function syntacticAndSpread( + right: ts.ObjectLiteralExpression, + cond: ts.Expression, +): { cond: ts.Expression; props: CondSpreadProp[]; whenTrue: boolean } | "unsupported" { + if (right.properties.nodes.length === 0) return "unsupported"; + const props: CondSpreadProp[] = []; + for (const p of right.properties.nodes) { + if ( + (ts.isPropertyAssignment(p) || ts.isShorthandPropertyAssignment(p)) && + p.name && + (ts.isIdentifier(p.name) || ts.isStringLiteral(p.name)) + ) { + props.push(p as CondSpreadProp); + } else { + return "unsupported"; + } + } + return { cond, props, whenTrue: true }; +} + /** A conditional spread's carrier property: `name: value` or shorthand, * with an identifier/string-literal name. */ export type CondSpreadProp = (ts.PropertyAssignment | ts.ShorthandPropertyAssignment) & { diff --git a/packages/compiler/src/frontend/type-mapper.ts b/packages/compiler/src/frontend/type-mapper.ts index de4e13de0..f2440a521 100644 --- a/packages/compiler/src/frontend/type-mapper.ts +++ b/packages/compiler/src/frontend/type-mapper.ts @@ -5861,6 +5861,7 @@ function mapRecordType(widened: ts.Type, ctx: TypeMapperCtx): IrType | null { ) return DYN; const merged = dictSpreadLiteralShape(widened, inner, ctx); + if (merged === "dyn") return DYN; if (merged !== null) return { kind: "record", shapeId: merged }; const refined = refinedConstituentShape(widened, inner, ctx); if (refined !== null) return { kind: "record", shapeId: refined }; @@ -5889,7 +5890,7 @@ function dictSpreadLiteralShape( widened: ts.Type, parts: RecordShapeParts, ctx: TypeMapperCtx, -): string | null { +): string | "dyn" | null { if (parts.indexValue) return null; const symbol = widened.getSymbol(); if (!symbol || (symbol.flags & ts.SymbolFlags.ObjectLiteral) === 0) return null; @@ -5908,16 +5909,29 @@ function dictSpreadLiteralShape( // mapping. const contextualRaw = checker.getContextualType(literal); const contextual = contextualRaw ? checker.getNonNullableType(contextualRaw) : undefined; - // An unknown-valued slot (`Record`) is the checked-dynamic - // value, which has no typed merge of its own: the literal still needs its - // own record. + // A literal written into a declared slot lowers into that slot (a typed + // index-signature record merges into it, a declared record converts), + // and its own type keeps its mapping. Only a literal with no declared + // destination (an unannotated binding, a type parameter, `any` or + // `unknown`, an unknown-valued record) is built by its own type. + // A generic argument's context is the instantiated parameter: the + // literal's own type when the type argument was inferred from it. + const ownContext = + contextual !== undefined && + (contextual === widened || contextual.getSymbol()?.declarations.includes(literal) === true); if ( contextual && - checker - .getIndexInfosOfType(contextual) - .some((info) => (info.valueType.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) === 0) - ) - return null; + !ownContext && + !(contextual.flags & (ts.TypeFlags.TypeParameter | ts.TypeFlags.Any | ts.TypeFlags.Unknown)) + ) { + const infos = checker.getIndexInfosOfType(contextual); + const unknownValued = + infos.length > 0 && + infos.every( + (info) => (info.valueType.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) !== 0, + ); + if (!unknownValued) return null; + } const kinds = props.map((p) => ts.isSpreadAssignment(p) ? spreadSourceKindTs(checker, p.expression) : "member", ); @@ -5934,6 +5948,7 @@ function dictSpreadLiteralShape( // pure index-signature record. if (t === widened) return null; const mapped = mapType(t, ctx); + if (mapped?.kind === "dyn") return DYN; const shape = mapped?.kind === "record" ? shapes.get(mapped.shapeId) : undefined; return shape?.indexValue && shape.fields.length === 0 && !shape.tuple ? shape.indexValue : null; }; @@ -5947,6 +5962,11 @@ function dictSpreadLiteralShape( return true; }; const before: string[] = []; + // An unknown-valued spread (`Record`, or another such + // literal) makes the object the checked-dynamic value: it is built like + // a JavaScript object literal (copy each spread's own keys in order), and + // member reads check their declared types. + let unknownValued = false; for (const [i, p] of props.entries()) { // A fixed-shape or conditional spread contributes its own members, // which the checker already types exactly (they are in `parts`). @@ -5958,11 +5978,32 @@ function dictSpreadLiteralShape( } if (ts.isSpreadAssignment(p)) { const slot = dictSlotOf(p.expression); - if (!slot || !addArms(slot)) return null; - // A member written before this spread may be overwritten by it. + if (slot?.kind === "dyn") { + unknownValued = true; + continue; + } + if (!slot) return null; + if (!addArms(slot)) { + // A slot outside the scalar join still has the checked-dynamic + // representation when it converts to it. + if ( + !canConvertToDyn( + slot, + (id) => shapes.get(id), + (id) => unions.get(id), + ) + ) + return null; + unknownValued = true; + } + // A member written before this spread may be overwritten by it: the + // typed join keeps member types only where the slot is that type; + // otherwise the checked-dynamic object (whose member reads check + // their types) holds whatever the spread wrote. for (const name of before) { const field = parts.fields.find((f) => f.name === name); - if (!field || !typeEquals(field.type, slot)) return null; + if (!field) return null; + if (!typeEquals(field.type, slot)) unknownValued = true; } continue; } @@ -5972,6 +6013,18 @@ function dictSpreadLiteralShape( before.push(name.text); } if (parts.fields.some((f) => f.name.startsWith("%"))) return null; + // Members outside the scalar join (an array of labels beside the tags) + // make the object the checked-dynamic value too. + if (!unknownValued && !parts.fields.every((f) => addArms(f.type))) unknownValued = true; + if (unknownValued) { + const getRecord = (id: string) => shapes.get(id); + const getUnion = (id: string) => unions.get(id); + return parts.fields.every( + (f) => f.type.kind === "dyn" || canConvertToDyn(f.type, getRecord, getUnion), + ) + ? "dyn" + : null; + } for (const field of parts.fields) if (!addArms(field.type)) return null; if (![...arms.values()].some((arm) => !isUnitType(arm))) return null; const sorted = [...arms.values()].sort((a, b) => (typeKey(a) < typeKey(b) ? -1 : 1)); diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6475-dynamic-spread-literals.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6475-dynamic-spread-literals.ts.json new file mode 100644 index 000000000..29207c929 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6475-dynamic-spread-literals.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6475-dynamic-spread-literals.ts" + ], + "diags": [] +} diff --git a/tests/corpus/6475-dynamic-spread-literals.ts b/tests/corpus/6475-dynamic-spread-literals.ts new file mode 100644 index 000000000..eaf76598e --- /dev/null +++ b/tests/corpus/6475-dynamic-spread-literals.ts @@ -0,0 +1,63 @@ +// Object literals spreading index-signature records whose members are not +// all scalars, or whose spread values are unknown, are checked-dynamic +// objects built like JavaScript's: each spread copies its own keys in +// order, conditional spreads add nothing when they do not apply, and later +// keyed writes land on the same object. +function flatten(obj: unknown, prefix: string): Record { + const out: Record = {}; + if (typeof obj === "object" && obj !== null) { + for (const [k, v] of Object.entries(obj)) out[`${prefix}.${k}`] = v; + } + return out; +} +function summary(status: string, ms: number, bytes: number | undefined, err: string | undefined) { + const milestones = { connect: 3, done: 9 }; + const resultTags = { + "git.fetch.status": status, + "git.fetch.durationMs": ms, + ...flatten(milestones, "git.fetch"), + ...(bytes !== undefined && { "git.fetch.packBytes": bytes }), + ...(err !== undefined && { "git.fetch.error": err }), + }; + console.log(JSON.stringify({ event: "git.fetch.summary", version: "v1", ...resultTags })); +} +summary("ok", 12, 100, undefined); +summary("failed", 3, undefined, "boom"); +type SendOptions = { method: string; headersTimeout?: number; signal?: AbortSignal }; +function send(url: string, options: SendOptions): string { + return `${url} ${options.method} ${options.headersTimeout ?? "-"} ${options.signal ? options.signal.aborted : "none"}`; +} +function callWith(timeout: number | undefined, attemptTimeout: number | undefined): string { + return send("u", { + headersTimeout: timeout, + method: "GET", + ...(attemptTimeout && { signal: AbortSignal.timeout(attemptTimeout) }), + }); +} +console.log(callWith(5, 1000)); +console.log(callWith(undefined, undefined)); +console.log(callWith(5, 0)); +function report(tags: string[], from: string, logs: [string, string][]) { + const ddTags = { + "errClass.from": from, + "errClass.tagLabels": Array.from(new Set(tags)).sort(), + "errClass.tags": "", + ...tags.reduce((obj: Record, tag: string) => { + obj[`errClass.tags.${tag}`] = true; + return obj; + }, {}), + "errClass.logLinesSent": logs.length, + "errClass.lastRule": "none", + }; + if (tags.length > 1) { + ddTags["errClass.lastRule"] = "rule-2"; + } + for (const tag of ["late"]) { + // @ts-expect-error -- keys outside the literal's declared ones + ddTags[`errClass.tags.${tag}`] = true; + } + ddTags["errClass.tags"] = tags.join(","); + console.log(JSON.stringify(ddTags), ddTags["errClass.logLinesSent"] + 1); +} +report(["b", "a"], "build", [["stdout", "x"]]); +report([], "init", []); From 1092f69ea9d4abad2894d95f630b1a55aa89fda4 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Tue, 20 Oct 2026 05:22:55 -0700 Subject: [PATCH 75/83] Split a chained Set call the self-hosted compiler cannot lower `deadDeclarators.add(d).add(memoDecl)` stopped the self-hosting build with "chained Set method calls ... has no scriptc lowering yet". Co-Authored-By: Claude Opus 5.5 --- packages/compiler/src/frontend/npm-static-bundled-esm.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/compiler/src/frontend/npm-static-bundled-esm.ts b/packages/compiler/src/frontend/npm-static-bundled-esm.ts index 9c77128e1..d9fa88fc4 100644 --- a/packages/compiler/src/frontend/npm-static-bundled-esm.ts +++ b/packages/compiler/src/frontend/npm-static-bundled-esm.ts @@ -633,7 +633,8 @@ export function lowerBundledEsmInits( text: `(${init}${getterText(value)})`, }); } - deadDeclarators.add(d).add(memoDecl); + deadDeclarators.add(d); + deadDeclarators.add(memoDecl); loaderSpans.push({ start: d.getStart(sf), end: d.getEnd() }); lowered.add(target); } From 465abda816f9ce827a156e3c7b77abfd7c4253a7 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Tue, 20 Oct 2026 04:30:43 -0700 Subject: [PATCH 76/83] Compile the remaining safe-fetch shapes: Promise race entries, typed Array(n).fill, narrowed header entries, lookup option unions and CommonJS export aliases - Promise.race admits a Promise entry (the deadline idiom) under any result type: it can only reject, so its fulfillment adapter traps. A Promise executor's one-parameter resolve validates against the void inner. - Array(n).fill(v) and new Array(n).fill(v), which the checker types as any[], take v's type. - Object.entries/values over an index-signature record cast to a narrower value type check each value into the element type instead of refusing. - dns.LookupAllOptions and the lookup-function option union that holds it (LookupOneOptions | LookupAllOptions | number | undefined) ride checked-dynamic storage, and dns.lookup passes a cast of a checked-dynamic options value through unchanged. - npm packages: a CommonJS module that names its export object through a variable (`var ip = exports; ip.isPrivate = ...`, the ip package) is respelled through `exports`, and tsc output that adds a default-export object (`exports.default = { metrics: metrics_api_1.metrics }`, the @opentelemetry/api index) keeps its named exports linked. Co-Authored-By: Claude Opus 5.5 --- .../src/backend/llvm/expr-callbacks.ts | 17 ++ .../frontend/lowering/builtins/promises.ts | 13 +- .../lowering/containers/array-construction.ts | 25 +++ .../lowering/containers/indexed-objects.ts | 76 +++++-- .../src/frontend/lowering/lower-dgram.ts | 13 +- .../frontend/npm-static-cjs-exports-var.ts | 198 ++++++++++++++++++ .../src/frontend/npm-static-rewrite-syntax.ts | 22 +- packages/compiler/src/frontend/npm-static.ts | 6 +- packages/compiler/src/frontend/services.ts | 9 + packages/compiler/src/frontend/type-mapper.ts | 29 +++ packages/compiler/src/ir/validate.ts | 5 +- .../6450-promise-race-never-entry.ts.json | 6 + .../6451-array-length-fill-spread.ts.json | 6 + .../lookup-and-header-entries.ts.json | 6 + .../npm/cases/cjs-exports-var/main.ts.json | 6 + .../npm/cases/tsc-default-object/main.ts.json | 6 + tests/corpus/6450-promise-race-never-entry.ts | 22 ++ tests/corpus/6451-array-length-fill-spread.ts | 15 ++ .../node-types/lookup-and-header-entries.ts | 47 +++++ .../npm/cases/cjs-exports-var/main.ts | 12 ++ .../npm/cases/tsc-default-object/main.ts | 7 + .../npm/node_modules/exportsvar/index.d.ts | 6 + .../npm/node_modules/exportsvar/lib/ip.js | 35 ++++ .../npm/node_modules/exportsvar/package.json | 6 + .../node_modules/tscdefault/build/index.d.ts | 8 + .../node_modules/tscdefault/build/index.js | 11 + .../tscdefault/build/registry-api.d.ts | 2 + .../tscdefault/build/registry-api.js | 5 + .../tscdefault/build/registry.d.ts | 5 + .../node_modules/tscdefault/build/registry.js | 20 ++ .../npm/node_modules/tscdefault/package.json | 6 + tests/harness/project-config.test.ts | 19 ++ 32 files changed, 651 insertions(+), 18 deletions(-) create mode 100644 packages/compiler/src/frontend/npm-static-cjs-exports-var.ts create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6450-promise-race-never-entry.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6451-array-length-fill-spread.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/node-types/lookup-and-header-entries.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/cjs-exports-var/main.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/tsc-default-object/main.ts.json create mode 100644 tests/corpus/6450-promise-race-never-entry.ts create mode 100644 tests/corpus/6451-array-length-fill-spread.ts create mode 100644 tests/fixtures/node-types/lookup-and-header-entries.ts create mode 100644 tests/fixtures/npm/cases/cjs-exports-var/main.ts create mode 100644 tests/fixtures/npm/cases/tsc-default-object/main.ts create mode 100644 tests/fixtures/npm/node_modules/exportsvar/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/exportsvar/lib/ip.js create mode 100644 tests/fixtures/npm/node_modules/exportsvar/package.json create mode 100644 tests/fixtures/npm/node_modules/tscdefault/build/index.d.ts create mode 100644 tests/fixtures/npm/node_modules/tscdefault/build/index.js create mode 100644 tests/fixtures/npm/node_modules/tscdefault/build/registry-api.d.ts create mode 100644 tests/fixtures/npm/node_modules/tscdefault/build/registry-api.js create mode 100644 tests/fixtures/npm/node_modules/tscdefault/build/registry.d.ts create mode 100644 tests/fixtures/npm/node_modules/tscdefault/build/registry.js create mode 100644 tests/fixtures/npm/node_modules/tscdefault/package.json diff --git a/packages/compiler/src/backend/llvm/expr-callbacks.ts b/packages/compiler/src/backend/llvm/expr-callbacks.ts index db1dca1af..b7954c174 100644 --- a/packages/compiler/src/backend/llvm/expr-callbacks.ts +++ b/packages/compiler/src/backend/llvm/expr-callbacks.ts @@ -55,6 +55,23 @@ export function raceAdapterFor(host: LlvmEmitterContext, from: IrType, to: IrTyp if (sym) return sym; sym = `sc_race_${host.resolveThunks.size}`; host.resolveThunks.set(key, sym); + const toArms = to.kind === "union" ? (host.unionsById.get(to.unionId)?.arms ?? []) : []; + if (from.kind === "void" && !toArms.some((a) => a.kind === "undefinedT")) { + // A Promise entry (the frontend admits void entries without an + // undefined result arm only for those): it can only reject, so its + // fulfillment adapter never runs. + host.declare(`declare void @scr_trap(ptr)`); + const msg = host.cstr("scriptc: a Promise race entry fulfilled\n"); + host.resolveThunkDefs.push( + `define internal void @${sym}(ptr %dst, ptr %src) ${FN_ATTRS} { ; race ${key}`, + `entry:`, + ` call void @scr_trap(ptr ${msg})`, + ` unreachable`, + `}`, + ``, + ); + return sym; + } if (to.kind !== "union") throw new InternalCompilerError("llvm emitter bug: race adapter to a non-union"); const toDef = host.unionsById.get(to.unionId); diff --git a/packages/compiler/src/frontend/lowering/builtins/promises.ts b/packages/compiler/src/frontend/lowering/builtins/promises.ts index 48106d082..03e9c7497 100644 --- a/packages/compiler/src/frontend/lowering/builtins/promises.ts +++ b/packages/compiler/src/frontend/lowering/builtins/promises.ts @@ -74,6 +74,17 @@ export function lowerPromiseStaticCall( } return resultArms.some((a) => typeEquals(a, inner)); }; + // A Promise entry (the deadline idiom: `new Promise((_, + // reject) => setTimeout(reject, ms))`) can only reject, so it races + // under any result type; its inner type maps to void. + const neverEntry = (el: ts.Expression): boolean => { + const t = lowerer.typeOf(el); + const arg = + t.getSymbol()?.name === "Promise" + ? lowerer.checker.getTypeArguments(t as ts.TypeReference)[0] + : undefined; + return arg !== undefined && (arg.flags & ts.TypeFlags.Never) !== 0; + }; const entries = argNode.elements.nodes.map((el) => { const entry = lowerer.lowerExpr(el); if (entry.type.kind !== "promise") { @@ -83,7 +94,7 @@ export function lowerPromiseStaticCall( "JS would resolve plain values — wrap them: new Promise((r) => r(v))", ); } - if (!compatible(entry.type.inner)) { + if (!compatible(entry.type.inner) && !(entry.type.inner.kind === "void" && neverEntry(el))) { lowerer.unsupported( "SC1090", el, diff --git a/packages/compiler/src/frontend/lowering/containers/array-construction.ts b/packages/compiler/src/frontend/lowering/containers/array-construction.ts index 6072b58b7..08ac43920 100644 --- a/packages/compiler/src/frontend/lowering/containers/array-construction.ts +++ b/packages/compiler/src/frontend/lowering/containers/array-construction.ts @@ -265,6 +265,22 @@ function appendWidenedArrayValues( } /** The Array constructor's element and count forms, shared by calls and new. */ +/** The value of `.fill(v)` when `expr` is that call's receiver. */ +function fillArgumentOfReceiver(expr: ts.Expression): ts.Expression | null { + let receiver: ts.Node = expr; + let parent = expr.parent; + while (ts.isParenthesizedExpression(parent)) { + receiver = parent; + parent = parent.parent; + } + if (!ts.isPropertyAccessExpression(parent) || parent.expression !== receiver) return null; + if (parent.name.text !== "fill" || parent.questionDotToken) return null; + const call = parent.parent; + if (!ts.isCallExpression(call) || call.expression !== parent) return null; + const value = call.arguments.nodes[0]; + return value !== undefined && !ts.isSpreadElement(value) ? value : null; +} + export function lowerArrayConstructor( lowerer: Lowerer, expr: ts.CallExpression | ts.NewExpression, @@ -295,6 +311,15 @@ export function lowerArrayConstructor( const contextual = lowerer.checker.getContextualType(expr); if (contextual) result = lowerer.mapTypeOf(contextual); } + // `Array(n).fill(v)`: the checker types the length-constructed array as + // any[], but fill gives every element v's type. + if (result?.kind !== "array") { + const fillValue = fillArgumentOfReceiver(expr); + const elem = fillValue + ? lowerer.mapTypeOf(lowerer.checker.getBaseTypeOfLiteralType(lowerer.typeOf(fillValue))) + : null; + if (elem !== null && isSupportedArrayElem(elem)) result = arrayOf(elem); + } if (isJsSourceFile(expr.getSourceFile()) && (result === null || result.kind === "dyn")) result = arrayOf(DYN); if (result?.kind !== "array" || !isSupportedArrayElem(result.elem)) { diff --git a/packages/compiler/src/frontend/lowering/containers/indexed-objects.ts b/packages/compiler/src/frontend/lowering/containers/indexed-objects.ts index 450e64142..326419504 100644 --- a/packages/compiler/src/frontend/lowering/containers/indexed-objects.ts +++ b/packages/compiler/src/frontend/lowering/containers/indexed-objects.ts @@ -148,12 +148,29 @@ export function objectIterOverIndexShape( } // The overflow value must surface as the element type (identity, an // arm of a union element, or a dyn element over a dyn signature). + // A cast that narrows the signature's union (`Object.entries(headers as + // Record)` over a signature that + // also holds numbers): each overflow value checks into the element type, + // so a value of a dropped arm throws instead of mistyping. + let ivNarrows = false; if (valueT) { const ivSurfaces = typeEquals(iv, valueT) || (valueT.kind === "union" && lowerer.armTag(valueT.unionId, iv) >= 0) || (valueT.kind === "dyn" && iv.kind === "dyn"); - if (!ivSurfaces) { + if ( + !ivSurfaces && + iv.kind === "union" && + lowerer.dynConvertible(iv) && + lowerer.dynConvertible(valueT) + ) { + const ivUnion = iv; + const narrowArms = + valueT.kind === "union" ? (lowerer.unions.get(valueT.unionId)?.arms ?? []) : [valueT]; + ivNarrows = + narrowArms.length > 0 && narrowArms.every((a) => lowerer.armTag(ivUnion.unionId, a) >= 0); + } + if (!ivSurfaces && !ivNarrows) { lowerer.unsupported( "SC1090", node, @@ -267,6 +284,21 @@ export function objectIterOverIndexShape( } } } + // A cast that narrows the field's type (the element type keeps + // some of its arms): the value checks into the element type, so a + // dropped arm throws instead of mistyping. + if (lowerer.dynConvertible(f.type) && lowerer.dynConvertible(valueT)) { + const armsOf = (t: IrType): IrType[] => + t.kind === "union" ? (lowerer.unions.get(t.unionId)?.arms ?? []) : [t]; + const target = armsOf(valueT); + if (armsOf(f.type).some((a) => target.some((b) => typeEquals(a, b)))) + return { + kind: "dynCheck", + value: { kind: "dynFrom", value: raw, type: DYN, loc }, + type: valueT, + loc, + }; + } return null; }; const pushedOf = (element: IrExpr): IrExpr => @@ -337,17 +369,37 @@ export function objectIterOverIndexShape( const ksT = arrayOf(STRING); const ksRef = varRef("ks.0", ksT, loc); const kRef = varRef("k.0", STRING, loc); - const readValue: IrExpr | null = valueT - ? { - kind: "recordKeyGet", - obj: rRef, - shapeId: argIr.shapeId, - key: kRef, - overflowOnly: true, - type: valueT, - loc, - } - : null; + const readValue: IrExpr | null = !valueT + ? null + : ivNarrows + ? { + kind: "dynCheck", + value: { + kind: "dynFrom", + value: { + kind: "recordKeyGet", + obj: rRef, + shapeId: argIr.shapeId, + key: kRef, + overflowOnly: true, + type: iv, + loc, + }, + type: DYN, + loc, + }, + type: valueT, + loc, + } + : { + kind: "recordKeyGet", + obj: rRef, + shapeId: argIr.shapeId, + key: kRef, + overflowOnly: true, + type: valueT, + loc, + }; const loopPushed: IrExpr = member === "keys" ? kRef diff --git a/packages/compiler/src/frontend/lowering/lower-dgram.ts b/packages/compiler/src/frontend/lowering/lower-dgram.ts index 54f9d158b..d035b21c2 100644 --- a/packages/compiler/src/frontend/lowering/lower-dgram.ts +++ b/packages/compiler/src/frontend/lowering/lower-dgram.ts @@ -290,8 +290,17 @@ function lowerDnsModuleCall( loc: SrcLoc, ): IrExpr { const args = expr.arguments.nodes; - const dyn = (node: ts.Expression): IrExpr => - lowerer.coerceToExpected(lowerer.lowerExpr(node), DYN); + const dyn = (node: ts.Expression): IrExpr => { + // `opts as dns.LookupOneOptions` over a checked-dynamic value (the + // overload-satisfying cast of a pass-through lookup): the cast changes + // no value, so the options reach the runtime as they are. + let inner = node; + while (ts.isAsExpression(inner) || ts.isParenthesizedExpression(inner)) + inner = inner.expression; + if (inner !== node && lowerer.mapTypeOf(lowerer.typeOf(inner))?.kind === "dyn") + return lowerer.lowerExpr(inner); + return lowerer.coerceToExpected(lowerer.lowerExpr(node), DYN); + }; // A callback whose second parameter is typed (string, LookupAddress[]) // cannot take the undefined Node passes after an error. const typedResult = (node: ts.Expression): IrExpr => { diff --git a/packages/compiler/src/frontend/npm-static-cjs-exports-var.ts b/packages/compiler/src/frontend/npm-static-cjs-exports-var.ts new file mode 100644 index 000000000..38972990c --- /dev/null +++ b/packages/compiler/src/frontend/npm-static-cjs-exports-var.ts @@ -0,0 +1,198 @@ +/* CommonJS modules that name their export object through a variable: + * + * var ip = exports; + * ip.isPrivate = function (addr) { … }; + * ip.isPublic = function (addr) { return !ip.isPrivate(addr); }; + * + * `ip` IS the export object, so every member read or write through it is + * an export. The pass respells those accesses as `exports.`, the + * form the static export protocol models, and drops the alias declaration + * (the export object has no storage of its own to bind). + * + * Exact only while the variable is nothing but that alias: one top-level + * declaration initialized with the CommonJS `exports` global, never + * reassigned, never declared again anywhere in the file (so no nested + * binding shadows it), and used only as the object of member accesses. + * `exports` itself must be the global (never declared in the file) and is + * never reassigned. Other shapes answer null and keep their existing + * treatment. */ +import * as ts from "./ts7/syntax.js"; + +function unparen(e: ts.Expression): ts.Expression { + let x = e; + while (ts.isParenthesizedExpression(x)) x = x.expression; + return x; +} + +function isDeclarationName(id: ts.Identifier): boolean { + const p = id.parent; + return ( + ((ts.isVariableDeclaration(p) || + ts.isParameter(p) || + ts.isBindingElement(p) || + ts.isFunctionDeclaration(p) || + ts.isFunctionExpression(p) || + ts.isClassDeclaration(p) || + ts.isClassExpression(p)) && + p.name === id) || + (ts.isPropertyAccessExpression(p) && p.name === id) || + ((ts.isPropertyAssignment(p) || ts.isMethodDeclaration(p) || ts.isPropertyDeclaration(p)) && + p.name === id) + ); +} + +function isFunctionScope(n: ts.Node): boolean { + return ( + ts.isFunctionDeclaration(n) || + ts.isFunctionExpression(n) || + ts.isArrowFunction(n) || + ts.isMethodDeclaration(n) || + ts.isGetAccessorDeclaration(n) || + ts.isSetAccessorDeclaration(n) || + ts.isConstructorDeclaration(n) + ); +} + +function bindsName(name: ts.BindingName | undefined, text: string): boolean { + if (name === undefined) return false; + if (ts.isIdentifier(name)) return name.text === text; + for (const element of name.elements.nodes) { + if (!ts.isOmittedExpression(element) && bindsName(element.name, text)) return true; + } + return false; +} + +/** True when `scope` (a function, or a block or catch clause) declares + * `text` itself, so references inside it name that binding instead of the + * module-level alias: a parameter, a var/let/const or a function or class + * declaration in a function (nested functions excluded), a let/const or + * class in a block, a catch binding. */ +function shadows(scope: ts.Node, text: string): boolean { + if (isFunctionScope(scope)) { + const fn = scope as ts.SignatureDeclaration; + if (fn.parameters.nodes.some((p) => bindsName(p.name, text))) return true; + if ( + (ts.isFunctionExpression(scope) || ts.isClassExpression(scope)) && + scope.name?.text === text + ) + return true; + } else if (ts.isCatchClause(scope)) { + if (bindsName(scope.variableDeclaration?.name, text)) return true; + } else if (!ts.isBlock(scope)) { + return false; + } + const functionScope = isFunctionScope(scope); + let found = false; + ts.walkPreorder(scope, (n) => { + if (found) return "stop"; + if (n === scope) return undefined; + if (isFunctionScope(n)) { + if (ts.isFunctionDeclaration(n) && n.name?.text === text) found = true; + return "skip"; + } + if (!functionScope && ts.isBlock(n)) return "skip"; /* its own scope */ + if (ts.isVariableDeclaration(n) && bindsName(n.name, text)) { + const list = n.parent; + const blockScoped = + ts.isVariableDeclarationList(list) && (list.flags & ts.NodeFlags.BlockScoped) !== 0; + if (functionScope || blockScoped) found = true; + } + if (ts.isClassDeclaration(n) && n.name?.text === text) found = true; + return undefined; + }); + return found; +} + +export function normalizeCjsExportsVar(sf: ts.SourceFile): string | null { + const source = sf.text; + if (!/\b(?:var|let|const)\s+[A-Za-z_$][\w$]*\s*=\s*exports\s*[;,\n]/.test(source)) return null; + if (!sf.fileName.endsWith(".cjs") && ts.isExternalModule(sf)) return null; + let alias: { name: string; stmt: ts.VariableStatement } | null = null; + for (const stmt of sf.statements.nodes) { + if (!ts.isVariableStatement(stmt)) continue; + const decls = stmt.declarationList.declarations.nodes; + if (decls.length !== 1) continue; + const decl = decls[0]!; + const init = decl.initializer !== undefined ? unparen(decl.initializer) : undefined; + if ( + ts.isIdentifier(decl.name) && + init !== undefined && + ts.isIdentifier(init) && + init.text === "exports" + ) { + if (alias !== null) return null; /* two aliases: keep the plain treatment */ + alias = { name: decl.name.text, stmt }; + } + } + if (alias === null) return null; + const name = alias.name; + const aliasDecl = alias.stmt.declarationList.declarations.nodes[0]!; + const edits: { start: number; end: number; text: string }[] = []; + let ok = true; + ts.walkPreorder(sf, (n) => { + if (!ok) return "stop"; + if (n.kind >= ts.SyntaxKind.FirstJSDocNode && n.kind <= ts.SyntaxKind.LastJSDocNode) + return "skip"; + if ( + ts.isBinaryExpression(n) && + n.operatorToken.kind === ts.SyntaxKind.EqualsToken && + ts.isPropertyAccessExpression(unparen(n.left)) && + (unparen(n.left) as ts.PropertyAccessExpression).name.text === "exports" && + ts.isIdentifier((unparen(n.left) as ts.PropertyAccessExpression).expression) && + ((unparen(n.left) as ts.PropertyAccessExpression).expression as ts.Identifier).text === + "module" + ) { + ok = false; /* module.exports replaced: the alias no longer names the exports */ + return "stop"; + } + // A scope that declares the alias's name again: its references name + // that binding, not the export object. + if ( + n !== sf && + (isFunctionScope(n) || ts.isBlock(n) || ts.isCatchClause(n)) && + shadows(n, name) + ) + return "skip"; + if (!ts.isIdentifier(n)) return undefined; + if (n.text === "exports" && n !== aliasDecl.initializer) { + // `exports` declared in the file, or the export object replaced + if (isDeclarationName(n) && !ts.isPropertyAccessExpression(n.parent)) ok = false; + const p = n.parent; + if ( + ts.isBinaryExpression(p) && + p.left === n && + p.operatorToken.kind === ts.SyntaxKind.EqualsToken + ) + ok = false; + return undefined; + } + if (n.text !== name) return undefined; + if (n === aliasDecl.name) return undefined; + const p = n.parent; + if (ts.isPropertyAccessExpression(p) && p.name === n) return undefined; + if ((ts.isPropertyAssignment(p) || ts.isMethodDeclaration(p)) && p.name === n) return undefined; + if (isDeclarationName(n)) { + ok = false; /* declared again (a shadowing binding): not a plain alias */ + return undefined; + } + if ( + (ts.isPropertyAccessExpression(p) || ts.isElementAccessExpression(p)) && + p.expression === n + ) { + edits.push({ start: n.getStart(sf), end: n.getEnd(), text: "exports" }); + return undefined; + } + ok = false; /* used as a value, assigned, or passed along */ + return undefined; + }); + if (!ok || edits.length === 0) return null; + edits.push({ start: alias.stmt.getStart(sf), end: alias.stmt.getEnd(), text: "" }); + edits.sort((a, b) => a.start - b.start); + let out = ""; + let at = 0; + for (const e of edits) { + out += source.slice(at, e.start) + e.text; + at = e.end; + } + return out + source.slice(at); +} diff --git a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts index 1730e1153..2fef30224 100644 --- a/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts +++ b/packages/compiler/src/frontend/npm-static-rewrite-syntax.ts @@ -158,6 +158,26 @@ function chaseValue(e: ts.Expression): { kind: "ident" | "hoist"; text: string } return null; } +/** An object literal of side-effect-free member values (`exports.default + * = { context: context_api_1.context, metrics: metrics_api_1.metrics }`, + * tsc's default-export object): the tail const can build it once, like a + * chased value. Null for any other shape (spreads, methods, accessors, + * computed keys, or a value chaseValue rejects). */ +function chaseObjectLiteral(e: ts.Expression): { kind: "hoist"; text: string } | null { + let cur: ts.Expression = e; + while (ts.isParenthesizedExpression(cur)) cur = cur.expression; + if (!ts.isObjectLiteralExpression(cur)) return null; + for (const prop of cur.properties.nodes) { + if ( + !ts.isPropertyAssignment(prop) || + !(ts.isIdentifier(prop.name) || ts.isStringLiteral(prop.name)) || + (chaseValue(prop.initializer) === null && chaseObjectLiteral(prop.initializer) === null) + ) + return null; + } + return { kind: "hoist", text: cur.getText() }; +} + /** The getter body's returned expression for the two descriptor getter * spellings (`get() { return X }` / `get: function () { return X }`) and * the arrow forms the esbuild table uses (`() => X`, `() => { return X }`). */ @@ -792,7 +812,7 @@ export function rewriteBundlerCjsSyntax( // anything else keeps the file untouched (the offender path owns // it: neutralizing would drop an evaluation, keeping it meets the // lowering's discard fence at load). - const chased = chaseValue(cur); + const chased = chaseValue(cur) ?? chaseObjectLiteral(cur); if (chased === null) { return toEsm.pads.length > 0 ? { degrade: TO_ESM_MIXED_DEGRADE } : null; } diff --git a/packages/compiler/src/frontend/npm-static.ts b/packages/compiler/src/frontend/npm-static.ts index 7dffd05f7..7d6e08307 100644 --- a/packages/compiler/src/frontend/npm-static.ts +++ b/packages/compiler/src/frontend/npm-static.ts @@ -407,6 +407,10 @@ export function npmStaticFsShadow(services: FrontendServices): NpmStaticFsShadow for (const chunk of split.chunks) setVirtualSource(chunk.path, chunk.source); original = split.text; } + // A module that names its export object through a variable reaches + // its members through `exports`. + const exportsVar = original === null ? null : services.cjsExportsVar(original, path); + if (exportsVar !== null) original = exportsVar; if (original !== null) { const namepaths = services.jsDocNamepaths(path, original); const source = namepaths ?? original; @@ -441,7 +445,7 @@ export function npmStaticFsShadow(services: FrontendServices): NpmStaticFsShadow findWidened?.text ?? classFields?.text ?? namepaths ?? - (split !== null ? original : null); + (split !== null || exportsVar !== null ? original : null); } } } catch { diff --git a/packages/compiler/src/frontend/services.ts b/packages/compiler/src/frontend/services.ts index c6bb24ae5..c5100d52d 100644 --- a/packages/compiler/src/frontend/services.ts +++ b/packages/compiler/src/frontend/services.ts @@ -8,6 +8,7 @@ import { type BundledChunkSplit, } from "./npm-static-bundled-chunks.js"; import * as syntax from "./npm-static-declaration-syntax.js"; +import { normalizeCjsExportsVar } from "./npm-static-cjs-exports-var.js"; import type { SourceFile } from "./ts7/ast-types.js"; /** Evaluate an isolated, self-contained TypeScript callback under a finite @@ -76,6 +77,14 @@ export class FrontendServices { if (!mayBundleChunks(source)) return null; return splitBundledChunks(this.parse(path, source, "js"), path); } + /** A CommonJS module that names its export object through a variable + * (`var ip = exports`), its member accesses respelled through `exports` + * (null: none). */ + cjsExportsVar(source: string, path: string): string | null { + this.ensureOpen(); + if (!source.includes("exports")) return null; + return normalizeCjsExportsVar(this.parse(path, source, "js")); + } nullableClassFields(path: string, source: string): syntax.NpmStaticOverloadRewrite | null { return syntax.applyNpmStaticNullableClassFields(this.parse(path, source, "js"), source); } diff --git a/packages/compiler/src/frontend/type-mapper.ts b/packages/compiler/src/frontend/type-mapper.ts index 431d9fe3e..abc35e350 100644 --- a/packages/compiler/src/frontend/type-mapper.ts +++ b/packages/compiler/src/frontend/type-mapper.ts @@ -1355,6 +1355,26 @@ function isWorkerHandleSymbol( /** The `{ message: any }` object type (or its union with undefined) that * worker_threads declares as receiveMessageOnPort's result. */ +/** A union of dns lookup option bags with numbers and nullish values that + * includes LookupAllOptions (whose `all: true` arm has no record form). */ +function isLookupOptionsUnion(type: ts.Type, ctx: Pick): boolean { + if (!type.isUnionType()) return false; + let all = false; + for (const member of ts.constituentTypes(type)) { + if (member.flags & (ts.TypeFlags.NumberLike | ts.TypeFlags.Null | ts.TypeFlags.Undefined)) + continue; + const symbol = member.getSymbol(); + if ( + symbol === undefined || + !/^Lookup(One|All)?Options$/.test(symbol.name) || + !symbol.declarations.some((d) => ctx.isStdlibFile(d.getSourceFile())) + ) + return false; + if (symbol.name === "LookupAllOptions") all = true; + } + return all; +} + function isReceiveMessageResult( type: ts.Type, ctx: Pick, @@ -1545,6 +1565,10 @@ function mapTypeInner(type: ts.Type, ctx: TypeMapperCtx): IrType | null { // worker_threads.receiveMessageOnPort's `{ message: any } | undefined`: // the runtime's { message } object, a checked-dynamic value. if (isReceiveMessageResult(type, ctx)) return DYN; + // The lookup-function option union (`LookupOneOptions | LookupAllOptions + // | number | undefined`): with a LookupAllOptions arm it is an option bag + // dns.lookup reads at run time, so it rides checked-dynamic storage. + if (isLookupOptionsUnion(type, ctx)) return DYN; // @types/node spells child_process.ExecFileException as an intersection // type alias over two Error-derived metadata interfaces. The value is // still one ordinary Error; recognize the owned alias before generic @@ -2511,6 +2535,11 @@ function mapTypeInner(type: ts.Type, ctx: TypeMapperCtx): IrType | null { // spawn consumes a presence-sensitive option bag, including optional // stdio and env values. Keep it checked-dynamic for native normalization. if (isStdlibInterface("SpawnOptions")) return DYN; + // dns.LookupAllOptions (`all: true`, the overload whose callback takes + // an address list): a lookup option bag dns.lookup reads at run time. + // It rides checked-dynamic storage, so the lookup function type that + // takes `LookupOneOptions | LookupAllOptions | number` compiles. + if (isStdlibInterface("LookupAllOptions")) return DYN; // http.RequestOptions / https.RequestOptions as a declared type: an // option record whose members include functions, agents and signals. // Values ride checked-dynamic storage, like SpawnOptions. diff --git a/packages/compiler/src/ir/validate.ts b/packages/compiler/src/ir/validate.ts index 50ba236b8..716774b03 100644 --- a/packages/compiler/src/ir/validate.ts +++ b/packages/compiler/src/ir/validate.ts @@ -6200,7 +6200,10 @@ function validateFunction( if (expectsArg && (rp.params.length !== 1 || !typeEquals(rp.params[0]!, inner))) { err(`newPromise resolve param must be (${inner.kind}) => void`, e.loc); } - if (!expectsArg && rp.params.length !== 0) { + // A Promise executor types resolve as `(value: never) => + // void` (one parameter over a void inner): the void resolve never + // reads an argument, and a never value cannot be produced. + if (!expectsArg && rp.params.length > 1) { err("newPromise resolve takes no argument", e.loc); } } diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6450-promise-race-never-entry.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6450-promise-race-never-entry.ts.json new file mode 100644 index 000000000..bb709e7f3 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6450-promise-race-never-entry.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6450-promise-race-never-entry.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6451-array-length-fill-spread.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6451-array-length-fill-spread.ts.json new file mode 100644 index 000000000..0302374fc --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/6451-array-length-fill-spread.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/6451-array-length-fill-spread.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/node-types/lookup-and-header-entries.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/node-types/lookup-and-header-entries.ts.json new file mode 100644 index 000000000..13a440abb --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/node-types/lookup-and-header-entries.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/node-types/lookup-and-header-entries.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/cjs-exports-var/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/cjs-exports-var/main.ts.json new file mode 100644 index 000000000..9ec4bbdeb --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/cjs-exports-var/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/cjs-exports-var/main.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/tsc-default-object/main.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/tsc-default-object/main.ts.json new file mode 100644 index 000000000..7ce71c66f --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/fixtures/npm/cases/tsc-default-object/main.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/fixtures/npm/cases/tsc-default-object/main.ts" + ], + "diags": [] +} diff --git a/tests/corpus/6450-promise-race-never-entry.ts b/tests/corpus/6450-promise-race-never-entry.ts new file mode 100644 index 000000000..412c5f817 --- /dev/null +++ b/tests/corpus/6450-promise-race-never-entry.ts @@ -0,0 +1,22 @@ +// Promise.race with a Promise deadline entry under a typed result: +// the never entry can only reject, so it races with any result type. A +// read that settles first wins; a deadline that fires first rejects. +class Deadline extends Error {} +async function readWithDeadline(read: () => Promise<{ done: boolean; value?: Uint8Array }>, ms: number): Promise<{ done: boolean; value?: Uint8Array }> { + let timer: ReturnType | undefined; + try { + return await Promise.race([ + read(), + new Promise((_, reject) => { + timer = setTimeout(() => reject(new Deadline()), ms); + }), + ]); + } catch (error) { + if (error instanceof Deadline) return { done: true }; + throw error; + } finally { + if (timer !== undefined) clearTimeout(timer); + } +} +readWithDeadline(async () => ({ done: false, value: new Uint8Array([1, 2]) }), 1000).then((r) => console.log(r.done, r.value?.length)); +readWithDeadline(() => new Promise((res) => setTimeout(() => res({ done: false }), 200)), 20).then((r) => console.log("deadline", r.done)); diff --git a/tests/corpus/6451-array-length-fill-spread.ts b/tests/corpus/6451-array-length-fill-spread.ts new file mode 100644 index 000000000..03b80c300 --- /dev/null +++ b/tests/corpus/6451-array-length-fill-spread.ts @@ -0,0 +1,15 @@ +// Array(n).fill(v): the checker types the length-constructed array as +// any[], but fill gives every element v's type, so the array (and an +// array literal spreading it) holds strings. +function expand(head: string, tail: string): number[] | null { + const headParts = head ? head.split(":") : []; + const tailParts = tail ? tail.split(":") : []; + const missing = 8 - headParts.length - tailParts.length; + if (missing < 0) return null; + const all = [...headParts, ...Array(missing).fill("0"), ...tailParts]; + if (all.length !== 8) return null; + return all.map((part) => Number.parseInt(part || "0", 16)); +} +console.log(JSON.stringify(expand("2001:db8", "1")), JSON.stringify(expand("fe80", ""))); +const pad: string[] = Array(3).fill("-"); +console.log(pad.join(""), [..."ab", ...new Array(2).fill("c")].join("")); diff --git a/tests/fixtures/node-types/lookup-and-header-entries.ts b/tests/fixtures/node-types/lookup-and-header-entries.ts new file mode 100644 index 000000000..a87f612d7 --- /dev/null +++ b/tests/fixtures/node-types/lookup-and-header-entries.ts @@ -0,0 +1,47 @@ +// A dns lookup function whose options parameter is Node's lookup-option +// union (`LookupOneOptions | LookupAllOptions | number | undefined`) passes +// the options to dns.lookup as they are, and Object.entries over a header +// record cast to a narrower value type reads every entry. +import * as dns from "node:dns"; +import type { OutgoingHttpHeaders } from "node:http"; + +type LookupCallback = ( + err: NodeJS.ErrnoException | null, + address: string | dns.LookupAddress[], + family?: number, +) => void; + +function createLookup(): ( + hostname: string, + opts: dns.LookupOneOptions | dns.LookupAllOptions | number | undefined, + callback: LookupCallback, +) => void { + return function lookup(hostname, opts, callback) { + dns.lookup(hostname, opts as dns.LookupOneOptions, (err, address, family) => { + if (err) return callback(err, "", 0); + callback(null, address, family); + }); + }; +} + +type HeadersLike = Headers | [string, string][] | OutgoingHttpHeaders; + +function flatten(input: HeadersLike | undefined): string[] { + const out: string[] = []; + if (!input || input instanceof Headers || Array.isArray(input)) return out; + for (const [name, value] of Object.entries(input as Record)) { + if (value === undefined) continue; + if (Array.isArray(value)) for (const v of value) out.push(`${name}=${v}`); + else out.push(`${name}=${value}`); + } + return out; +} + +const lookup = createLookup(); +lookup("localhost", { family: 4 }, (err, address, family) => { + console.log("one", err, address, family); + lookup("127.0.0.1", 4, (err3, address3, family3) => { + console.log("family", err3, address3, family3); + console.log(flatten({ accept: "text/plain", "x-list": ["a", "b"], "x-skip": undefined }).join(",")); + }); +}); diff --git a/tests/fixtures/npm/cases/cjs-exports-var/main.ts b/tests/fixtures/npm/cases/cjs-exports-var/main.ts new file mode 100644 index 000000000..60a13b850 --- /dev/null +++ b/tests/fixtures/npm/cases/cjs-exports-var/main.ts @@ -0,0 +1,12 @@ +// A CommonJS module that names its export object through a variable +// (`var ip = exports; ip.isPrivate = function …`), the ip package's +// shape, imported by default as Node does: its members are the exports, +// including the ones it calls on itself through the alias. +import ip from "exportsvar"; + +for (const addr of ["10.0.0.1", "192.168.1.20", "127.0.0.1", "::1", "8.8.8.8"]) { + console.log(addr, ip.isPrivate(addr), ip.isLoopback(addr), ip.isPublic(addr)); +} +console.log(ip.toLong("192.168.1.1")); +// Members whose parameter shadows the alias name. +console.log(ip.isV4Format("1.2.3.4"), ip.isV4Format("::1"), ip.normalize(" FE80::1 ")); diff --git a/tests/fixtures/npm/cases/tsc-default-object/main.ts b/tests/fixtures/npm/cases/tsc-default-object/main.ts new file mode 100644 index 000000000..bcc650c06 --- /dev/null +++ b/tests/fixtures/npm/cases/tsc-default-object/main.ts @@ -0,0 +1,7 @@ +// tsc's CommonJS output for a module with named re-exports AND a default +// export object (`exports.default = { registry: registry_api_1.registry }`, +// the @opentelemetry/api index shape): the named exports still link, read +// through their defineProperty getters. +import { registry, VERSION } from "tscdefault"; + +console.log(VERSION, registry.count("a", 2), registry.count("a", 3), registry.count("b", 1)); diff --git a/tests/fixtures/npm/node_modules/exportsvar/index.d.ts b/tests/fixtures/npm/node_modules/exportsvar/index.d.ts new file mode 100644 index 000000000..8782f2267 --- /dev/null +++ b/tests/fixtures/npm/node_modules/exportsvar/index.d.ts @@ -0,0 +1,6 @@ +export function isPrivate(addr: string): boolean; +export function isLoopback(addr: string): boolean; +export function isPublic(addr: string): boolean; +export function toLong(addr: string): number; +export function isV4Format(ip: string): boolean; +export function normalize(ip: string): string; diff --git a/tests/fixtures/npm/node_modules/exportsvar/lib/ip.js b/tests/fixtures/npm/node_modules/exportsvar/lib/ip.js new file mode 100644 index 000000000..8e782dad2 --- /dev/null +++ b/tests/fixtures/npm/node_modules/exportsvar/lib/ip.js @@ -0,0 +1,35 @@ +'use strict'; + +var ip = exports; + +ip.isPrivate = function(addr) { + return /^(::f{4}:)?10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) || + /^(::f{4}:)?192\.168\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr); +}; + +ip.isLoopback = function(addr) { + return /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/.test(addr) || + /^::1$/.test(addr); +}; + +ip.isPublic = function(addr) { + return !ip.isPrivate(addr) && !ip.isLoopback(addr); +}; + +ip.toLong = function(addr) { + var n = 0; + addr.split('.').forEach(function(octet) { + n <<= 8; + n += parseInt(octet, 10); + }); + return n >>> 0; +}; + +ip.isV4Format = function(ip) { + return /^(\d{1,3}\.){3,3}\d{1,3}$/.test(ip); +}; + +ip.normalize = function(ip) { + var trimmed = ip.trim(); + return trimmed.toLowerCase(); +}; diff --git a/tests/fixtures/npm/node_modules/exportsvar/package.json b/tests/fixtures/npm/node_modules/exportsvar/package.json new file mode 100644 index 000000000..04881bdbd --- /dev/null +++ b/tests/fixtures/npm/node_modules/exportsvar/package.json @@ -0,0 +1,6 @@ +{ + "name": "exportsvar", + "version": "1.0.0", + "main": "lib/ip", + "types": "index.d.ts" +} diff --git a/tests/fixtures/npm/node_modules/tscdefault/build/index.d.ts b/tests/fixtures/npm/node_modules/tscdefault/build/index.d.ts new file mode 100644 index 000000000..7fedd198f --- /dev/null +++ b/tests/fixtures/npm/node_modules/tscdefault/build/index.d.ts @@ -0,0 +1,8 @@ +import { registry } from './registry-api'; +export { registry }; +export declare const VERSION = "1.0.0"; +declare const _default: { + registry: import("./registry").Registry; + version: string; +}; +export default _default; diff --git a/tests/fixtures/npm/node_modules/tscdefault/build/index.js b/tests/fixtures/npm/node_modules/tscdefault/build/index.js new file mode 100644 index 000000000..0dff83d00 --- /dev/null +++ b/tests/fixtures/npm/node_modules/tscdefault/build/index.js @@ -0,0 +1,11 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.VERSION = exports.registry = void 0; +const registry_api_1 = require("./registry-api"); +Object.defineProperty(exports, "registry", { enumerable: true, get: function () { return registry_api_1.registry; } }); +exports.VERSION = "1.0.0"; +// Default export. +exports.default = { + registry: registry_api_1.registry, + version: "1.0.0", +}; diff --git a/tests/fixtures/npm/node_modules/tscdefault/build/registry-api.d.ts b/tests/fixtures/npm/node_modules/tscdefault/build/registry-api.d.ts new file mode 100644 index 000000000..2010f655e --- /dev/null +++ b/tests/fixtures/npm/node_modules/tscdefault/build/registry-api.d.ts @@ -0,0 +1,2 @@ +import { Registry } from './registry'; +export declare const registry: Registry; diff --git a/tests/fixtures/npm/node_modules/tscdefault/build/registry-api.js b/tests/fixtures/npm/node_modules/tscdefault/build/registry-api.js new file mode 100644 index 000000000..209ad4e9a --- /dev/null +++ b/tests/fixtures/npm/node_modules/tscdefault/build/registry-api.js @@ -0,0 +1,5 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.registry = void 0; +const registry_1 = require("./registry"); +exports.registry = registry_1.Registry.getInstance(); diff --git a/tests/fixtures/npm/node_modules/tscdefault/build/registry.d.ts b/tests/fixtures/npm/node_modules/tscdefault/build/registry.d.ts new file mode 100644 index 000000000..e9957d79d --- /dev/null +++ b/tests/fixtures/npm/node_modules/tscdefault/build/registry.d.ts @@ -0,0 +1,5 @@ +export declare class Registry { + private counts; + static getInstance(): Registry; + count(name: string, n: number): number; +} diff --git a/tests/fixtures/npm/node_modules/tscdefault/build/registry.js b/tests/fixtures/npm/node_modules/tscdefault/build/registry.js new file mode 100644 index 000000000..2797e469a --- /dev/null +++ b/tests/fixtures/npm/node_modules/tscdefault/build/registry.js @@ -0,0 +1,20 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Registry = void 0; +class Registry { + constructor() { + this.counts = new Map(); + } + static getInstance() { + if (!this._instance) { + this._instance = new Registry(); + } + return this._instance; + } + count(name, n) { + const next = (this.counts.get(name) ?? 0) + n; + this.counts.set(name, next); + return next; + } +} +exports.Registry = Registry; diff --git a/tests/fixtures/npm/node_modules/tscdefault/package.json b/tests/fixtures/npm/node_modules/tscdefault/package.json new file mode 100644 index 000000000..230541aaf --- /dev/null +++ b/tests/fixtures/npm/node_modules/tscdefault/package.json @@ -0,0 +1,6 @@ +{ + "name": "tscdefault", + "version": "1.0.0", + "main": "build/index.js", + "types": "build/index.d.ts" +} diff --git a/tests/harness/project-config.test.ts b/tests/harness/project-config.test.ts index 14e4c27af..b5d47fae1 100644 --- a/tests/harness/project-config.test.ts +++ b/tests/harness/project-config.test.ts @@ -443,6 +443,25 @@ test("node-types: util.promisify over typed callback-last functions settles like expect(comparableStderr(native.stderr)).toBe(node.stderr); }); +test("node-types: lookup-option unions and narrowed header-record entries under @types/node", async () => { + const outDir = outDirFor("lookup-and-header-entries"); + const entry = join(nodeTypesDir, "lookup-and-header-entries.ts"); + const result = await compile(entry, { + outPath: join(outDir, "lookup-and-header-entries"), + outDir, + sanitize, + }); + expect(result.ok, !result.ok ? JSON.stringify(result.diagnostics, null, 2) : "").toBe(true); + if (!result.ok) return; + const native = await execFileAsync(result.binaryPath); + const node = await execFileAsync(process.execPath, [ + "--disable-warning=MODULE_TYPELESS_PACKAGE_JSON", + entry, + ]); + expect(native.stdout).toBe(node.stdout); + expect(comparableStderr(native.stderr)).toBe(node.stderr); +}); + test("node-types: stream subclasses extend the @types/node stream classes", async () => { const outDir = outDirFor("stream-subclass-hooks"); const entry = join(nodeTypesDir, "stream-subclass-hooks.ts"); From c475aabc2a1c8a8ce75a2738de0088c7218b1ebe Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 08:41:04 -0500 Subject: [PATCH 77/83] Refuse arguments to inherited stream constructors from untyped code Construction from checked-dynamic code (`new F(options)` with F an untyped value holding a ctor-less subclass of a stream class or EventEmitter) dispatched to the class's constructor and completed the arguments against the inherited signature, which takes none here, so the options were dropped. The checked construction dispatch now reports those arguments at run time with the message typed construction reports at compile time, located at the class: a fixed argument count refuses at the call, and a spread call refuses when its argument list is not empty. --- .../frontend/lowering/class-construction.ts | 48 ++++++++++++++++++- .../src/frontend/lowering/lower-classes.ts | 28 +++++++++++ tests/harness/errors.test.ts | 31 ++++++++++++ 3 files changed, 105 insertions(+), 2 deletions(-) diff --git a/packages/compiler/src/frontend/lowering/class-construction.ts b/packages/compiler/src/frontend/lowering/class-construction.ts index 7f4a003c9..30f504b4a 100644 --- a/packages/compiler/src/frontend/lowering/class-construction.ts +++ b/packages/compiler/src/frontend/lowering/class-construction.ts @@ -18,6 +18,7 @@ import type { Lowerer } from "./lowerer.js"; import type { ParamShape } from "./call-signatures.js"; import { classPrototypeData } from "./class-prototypes.js"; import { classInstanceOf } from "./class-dynamic-dispatch.js"; +import { inheritedBuiltinCtorFence } from "./lower-classes.js"; const PREFIX = "%class.construct:"; const PACKED = `${PREFIX}packed`; @@ -380,6 +381,29 @@ export class ClassConstructionDispatch { changed = true; continue; } + const type: IrType = { kind: "classval", className: name }; + const callee = varRef("p.0", DYN, loc); + // A constructor inherited from a runtime stream or EventEmitter + // takes no arguments here: arguments report at run time, as they do + // at compile time for typed construction. + const fence = inheritedBuiltinCtorFence(lowerer, info, loc); + if (fence !== null && fn.name !== PACKED && fn.params.length > 1) { + fn.body.unshift({ + kind: "if", + cond: { + kind: "libCall", + fn: "dyn.classIs", + args: [callee, { kind: "strLit", value: typeKey(type), type: STRING, loc }], + type: BOOL, + loc, + }, + then: [fence], + else_: null, + loc, + }); + changed = true; + continue; + } const args = constructorArguments( lowerer, info.ctorParams, @@ -388,8 +412,6 @@ export class ClassConstructionDispatch { fn.name === PACKED ? varRef("p.1", DYN, loc) : undefined, ); if (!args) continue; - const type: IrType = { kind: "classval", className: name }; - const callee = varRef("p.0", DYN, loc); const value = constructedValue(lowerer, info, { kind: "newValue", callee: { kind: "dynCheck", value: callee, type, loc }, @@ -400,6 +422,28 @@ export class ClassConstructionDispatch { const body: IrStmt[] = [ { kind: "return", value: lowerer.coerceToExpected(value, DYN), loc }, ]; + // A spread call's argument count is known only at run time. + if (fence !== null && fn.name === PACKED) + body.unshift({ + kind: "if", + cond: { + kind: "bin", + op: ">", + left: { + kind: "libCall", + fn: "dyn.arrLen", + args: [varRef("p.1", DYN, loc)], + type: F64, + loc, + }, + right: { kind: "numLit", value: 0, type: F64, loc }, + type: BOOL, + loc, + }, + then: [fence], + else_: null, + loc, + }); fn.body.unshift({ kind: "if", cond: { diff --git a/packages/compiler/src/frontend/lowering/lower-classes.ts b/packages/compiler/src/frontend/lowering/lower-classes.ts index 714588a57..cd0c6b8bd 100644 --- a/packages/compiler/src/frontend/lowering/lower-classes.ts +++ b/packages/compiler/src/frontend/lowering/lower-classes.ts @@ -8725,6 +8725,34 @@ function inheritsBuiltinStreamCtor(lowerer: Lowerer, info: ClassInfo): boolean { return false; } +/** The run-time form of refuseInheritedBuiltinCtorArgs, for construction + * from checked-dynamic code (`new F(options)` with F an untyped value + * holding C), whose argument count is known only to the caller: a + * runtimeFence located at C, or null when C's constructor takes its + * arguments. */ +export function inheritedBuiltinCtorFence( + lowerer: Lowerer, + info: ClassInfo, + loc: SrcLoc, +): IrStmt | null { + let feature: string | null = null; + if (inheritsBuiltinStreamCtor(lowerer, info)) + feature = `new ${info.def.name.replace(/^%/, "")} with arguments through an inherited stream constructor (declare a constructor that passes an inline options object to super(...))`; + else if (lowerer.inheritsBuiltinEmitterCtor(info)) + feature = "EventEmitter constructor options ('captureRejections')"; + if (feature === null) return null; + // Located at the class, whose constructor takes no arguments. + const at = info.decl !== null ? locOf(info.decl) : loc; + const diag = unsupportedDiag("SC1090", at, feature); + lowerer.runtimeFences.push(diag); + const line = + info.decl !== null + ? ts.getLineAndCharacterOfPosition(info.decl.getSourceFile(), at.start).line + 1 + : null; + const where = line !== null ? ` [${diag.code} at ${at.file}:${line}]` : ""; + return { kind: "runtimeFence", code: diag.code, message: `${diag.message}${where}`, loc }; +} + /** `new C(args)` where C's constructor is the inherited runtime stream or * EventEmitter constructor (a ctor-less chain into the base): that * constructor takes zero arguments here (the options bag fences, like the diff --git a/tests/harness/errors.test.ts b/tests/harness/errors.test.ts index 1f3d9729b..f08ed45d7 100644 --- a/tests/harness/errors.test.ts +++ b/tests/harness/errors.test.ts @@ -359,6 +359,37 @@ const arr = tuple.map(() => new Date(0));`, expect(r).toEqual({ stdout: reference.stdout, stderr: reference.stderr, exitCode: 0 }); }); + test.each([ + ["an argument", "make(Upper, { objectMode: true });"], + ["a spread argument", "spread(Upper, [{ objectMode: true }]);"], + ])( + "%s through an untyped factory to an inherited stream constructor is a named deferred fence (JS lane)", + async (_, call) => { + // The constructor a ctor-less stream subclass inherits takes no + // arguments here; typed construction refuses them at compile time, + // and construction from untyped code refuses them at run time + // instead of constructing without them. + const r = await compileAndRun( + `inherited-stream-ctor-${call.startsWith("make") ? "args" : "spread"}`, + `'use strict'; +const { Transform } = require('node:stream'); +class Upper extends Transform {} +function make(F, options) { return new F(options); } +function bare(F) { return new F(); } +function spread(F, args) { return new F(...args); } +console.log('bare', bare(Upper) instanceof Upper, spread(Upper, []) instanceof Upper); +${call} +`, + "cjs", + ); + expect(r.exitCode).toBe(1); + expect(r.stdout).toBe("bare true true\n"); + expect(r.stderr).toMatch( + /^Uncaught Error: new Upper with arguments through an inherited stream constructor .* is not supported yet \[SC1090 at .*inherited-stream-ctor-\w+\.cjs:3\]\n$/, + ); + }, + ); + test("extending a property-assigned class ABOVE its assignment is a named deferred fence (JS lane)", async () => { // The property-assigned class-expression family (corpus 2032/2033 // pins what lowers): `exports.O = class extends exports.I {}` with From 1ef2004c9beb0049762f4724d51eda6479c5cdd5 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 08:46:56 -0500 Subject: [PATCH 78/83] Compare only the 48 bytes SHA-384 writes in the cross-kernel check --- packages/runtime/test/test_sha_kernels.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime/test/test_sha_kernels.c b/packages/runtime/test/test_sha_kernels.c index f9f9b9df7..da7d79235 100644 --- a/packages/runtime/test/test_sha_kernels.c +++ b/packages/runtime/test/test_sha_kernels.c @@ -192,7 +192,7 @@ int main(void) { sha512_of(data, n, chunks[c], sha384, a); select512(0); sha512_of(data, n, chunks[c], sha384, b); - check(memcmp(a, b, 64) == 0, kernel512_names[k], n, chunks[c]); + check(memcmp(a, b, sha384 ? 48 : 64) == 0, kernel512_names[k], n, chunks[c]); } } } From 5e0ad5bbaa005dd3431394b6e33f92289d146a10 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 08:51:47 -0500 Subject: [PATCH 79/83] Compare checker panics without their text in the checker identity check A statement whose lowering meets a checker panic keeps the panic's text in its SC0004 fence. tsgo and tsc-ts reach the same panic for `s.match(re) || []` (6187-js-match-or-empty-array.js) and describe it in their own words, so the check now replaces the text after `panic:` before it compares the IR and the diagnostics. --- scripts/checker-identity.mjs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/scripts/checker-identity.mjs b/scripts/checker-identity.mjs index 35d01cb93..a710d5946 100644 --- a/scripts/checker-identity.mjs +++ b/scripts/checker-identity.mjs @@ -1,6 +1,7 @@ #!/usr/bin/env node // Compiles every corpus program to LLVM IR with each TypeScript 7 API server (tsc-ts, the default, and tsgo with -// SCRIPTC_CHECKER=tsgo) and fails unless both give the same IR and the same diagnostics. +// SCRIPTC_CHECKER=tsgo) and fails unless both give the same IR and the same diagnostics. The text of a checker panic +// is the checker's own (Go's runtime error for tsgo, tsc-ts's for tsc-ts), so it is compared as `panic: …`. // // Usage: node scripts/checker-identity.mjs [--jobs ] [--optimization release|dev] [--filter ] // @@ -73,11 +74,11 @@ async function main() { const [a, b] = checkers.map((checker) => results.get(checker).get(file)); const name = file.slice(corpusDir.length + 1); if (a === undefined || b === undefined) differences.push(`${name}: not compiled`); - else if (a.ok !== b.ok || a.diagnostics !== b.diagnostics) + else if (a.ok !== b.ok || withoutPanicText(a.diagnostics) !== withoutPanicText(b.diagnostics)) differences.push( `${name}: tsgo ${a.ok ? "compiles" : a.diagnostics}; tsc-ts ${b.ok ? "compiles" : b.diagnostics}`, ); - else if (a.ok && !readFileSync(a.llvm).equals(readFileSync(b.llvm))) + else if (a.ok && irWithoutPanicText(a.llvm) !== irWithoutPanicText(b.llvm)) differences.push(`${name}: LLVM IR differs`); } if (differences.length > 0) { @@ -96,6 +97,31 @@ async function main() { } } +/** A diagnostic list with each SC0004 checker panic's text replaced by `…`. */ +function withoutPanicText(diagnostics) { + return diagnostics?.replace(/(SC0004: [^;]*?: panic: )[^;]*/g, "$1…"); +} + +/** A module's LLVM IR with each SC0004 checker panic's text replaced by `…`. A fence statement keeps the + * diagnostic's message in a string constant named for a hash of its contents, and passes its length. */ +function irWithoutPanicText(llvm) { + let ir = readFileSync(llvm, "utf8"); + const constant = + /^(@sc_cs_\d+) = internal constant \[\d+ x i8\] c"(the TypeScript checker crashed answering a query here [^"]*?: panic: )[^"]*?( \[SC0004 at [^"]*)"$/gm; + const names = []; + ir = ir.replace(constant, (_, name, head, tail) => { + names.push(name); + return `@checker_panic_${names.length} = internal constant c"${head}…${tail}"`; + }); + names.forEach((name, index) => { + ir = ir.replace( + new RegExp(`${name}\\b(, i64 \\d+)?`, "g"), + (_, length) => `@checker_panic_${index + 1}${length === undefined ? "" : ", i64 …"}`, + ); + }); + return ir; +} + async function worker([outDir, optimization, ...files]) { const { compile } = await import( pathToFileURL(join(repoRoot, "packages/compiler/dist/index.js")).href From d850e14d505dd8c17ef584014564b5b112e89642 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 08:58:50 -0500 Subject: [PATCH 80/83] Refuse dropped members through conditional and satisfies returns The refusal of callback returns that the inferred result type would cut down found the return by walking up through parentheses and await only, so a return inside a conditional's branch or a `satisfies` expression converted silently and lost the extended members. It now walks through every parent whose value can be the expression's own: parentheses, await, satisfies, non-null assertions, conditional branches, the operands of ??, || and &&, and the right operand of a comma. --- .../compiler/src/frontend/lowering/lowerer.ts | 31 +++++++++++++++++-- ...nferred-callback-drops-conditional.ts.json | 6 ++++ .../inferred-callback-drops-satisfies.ts.json | 6 ++++ .../inferred-callback-drops-conditional.ts | 12 +++++++ .../inferred-callback-drops-satisfies.ts | 13 ++++++++ ...inferred-callback-drops-conditional.ts.txt | 6 ++++ .../inferred-callback-drops-satisfies.ts.txt | 6 ++++ 7 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/inferred-callback-drops-conditional.ts.json create mode 100644 packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/inferred-callback-drops-satisfies.ts.json create mode 100644 tests/diagnostics/inferred-callback-drops-conditional.ts create mode 100644 tests/diagnostics/inferred-callback-drops-satisfies.ts create mode 100644 tests/harness/__snapshots__/inferred-callback-drops-conditional.ts.txt create mode 100644 tests/harness/__snapshots__/inferred-callback-drops-satisfies.ts.txt diff --git a/packages/compiler/src/frontend/lowering/lowerer.ts b/packages/compiler/src/frontend/lowering/lowerer.ts index 3edf33d22..4798f49c5 100644 --- a/packages/compiler/src/frontend/lowering/lowerer.ts +++ b/packages/compiler/src/frontend/lowering/lowerer.ts @@ -13007,8 +13007,7 @@ function refuseInferredCallbackDrops( t.kind === "record" || t.kind === "array" || t.kind === "promise"; if (!structured(actual) || !structured(expected)) return; let site: ts.Node = node; - while (ts.isParenthesizedExpression(site.parent) || ts.isAwaitExpression(site.parent)) - site = site.parent; + while (yieldsOperand(site)) site = site.parent!; let fn: ts.Node | undefined; if (ts.isReturnStatement(site.parent) && site.parent.expression === site) { fn = site.parent.parent; @@ -13031,6 +13030,34 @@ function refuseInferredCallbackDrops( ); } +/** True when the value of `node`'s parent can be `node`'s own value: + * parentheses, `await`, `satisfies`, a non-null assertion, a conditional's + * branch, either operand of `??`, `||` and `&&`, and the right operand of a + * comma. */ +function yieldsOperand(node: ts.Node): boolean { + const p = node.parent; + if (p === undefined) return false; + if ( + ts.isParenthesizedExpression(p) || + ts.isAwaitExpression(p) || + ts.isSatisfiesExpression(p) || + ts.isNonNullExpression(p) + ) + return true; + if (ts.isConditionalExpression(p)) return p.whenTrue === node || p.whenFalse === node; + if (ts.isBinaryExpression(p)) { + const op = p.operatorToken.kind; + if ( + op === ts.SyntaxKind.QuestionQuestionToken || + op === ts.SyntaxKind.BarBarToken || + op === ts.SyntaxKind.AmpersandAmpersandToken + ) + return true; + return op === ts.SyntaxKind.CommaToken && p.right === node; + } + return false; +} + /** Member names a structural conversion from `from` into `to` drops: * declared fields of a source record the target record lacks, through * array elements and promise payloads. */ diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/inferred-callback-drops-conditional.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/inferred-callback-drops-conditional.ts.json new file mode 100644 index 000000000..2014c8620 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/inferred-callback-drops-conditional.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/diagnostics/inferred-callback-drops-conditional.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/inferred-callback-drops-satisfies.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/inferred-callback-drops-satisfies.ts.json new file mode 100644 index 000000000..44c5e8bbc --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/inferred-callback-drops-satisfies.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/diagnostics/inferred-callback-drops-satisfies.ts" + ], + "diags": [] +} diff --git a/tests/diagnostics/inferred-callback-drops-conditional.ts b/tests/diagnostics/inferred-callback-drops-conditional.ts new file mode 100644 index 000000000..f124beed9 --- /dev/null +++ b/tests/diagnostics/inferred-callback-drops-conditional.ts @@ -0,0 +1,12 @@ +type Plain = { name: string; n: number }; +type Item = Plain & { extra?: boolean }; +const plain: Plain[] = [{ name: "a", n: 1 }]; +function run(fn: () => T): T { + return fn(); +} +// The checker unifies both branches into Plain[]; the extended objects keep +// `extra` in Node, so converting them into Plain would drop it silently. +const items: Item[] = run(() => + plain.length === 0 ? plain : plain.map((x) => ({ ...x, extra: true })), +); +console.log(items.length); diff --git a/tests/diagnostics/inferred-callback-drops-satisfies.ts b/tests/diagnostics/inferred-callback-drops-satisfies.ts new file mode 100644 index 000000000..ad58237b8 --- /dev/null +++ b/tests/diagnostics/inferred-callback-drops-satisfies.ts @@ -0,0 +1,13 @@ +type Plain = { name: string; n: number }; +type Item = Plain & { extra?: boolean }; +const plain: Plain[] = [{ name: "a", n: 1 }]; +function run(fn: () => T): T { + return fn(); +} +// `satisfies` checks the return against Plain[] without changing it; the +// extended objects keep `extra` in Node. +const items: Item[] = run(() => { + if (plain.length === 0) return plain; + return plain.map((x) => ({ ...x, extra: true })) satisfies Plain[]; +}); +console.log(items.length); diff --git a/tests/harness/__snapshots__/inferred-callback-drops-conditional.ts.txt b/tests/harness/__snapshots__/inferred-callback-drops-conditional.ts.txt new file mode 100644 index 000000000..a14f19317 --- /dev/null +++ b/tests/harness/__snapshots__/inferred-callback-drops-conditional.ts.txt @@ -0,0 +1,6 @@ +inferred-callback-drops-conditional.ts:10:32 - error SC1090: returning '{ extra: boolean; n: number; name: string }[]' from a callback whose result the checker infers as the narrower '{ n: number; name: string }[]' (the conversion would drop 'extra', which the object keeps at run time — annotate the callback's return type or the call's type argument with a type that declares them) is not supported yet + + 9 | const items: Item[] = run(() => + 10 | plain.length === 0 ? plain : plain.map((x) => ({ ...x, extra: true })), + | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 11 | ); \ No newline at end of file diff --git a/tests/harness/__snapshots__/inferred-callback-drops-satisfies.ts.txt b/tests/harness/__snapshots__/inferred-callback-drops-satisfies.ts.txt new file mode 100644 index 000000000..ea1e37549 --- /dev/null +++ b/tests/harness/__snapshots__/inferred-callback-drops-satisfies.ts.txt @@ -0,0 +1,6 @@ +inferred-callback-drops-satisfies.ts:11:10 - error SC1090: returning '{ extra: boolean; n: number; name: string }[]' from a callback whose result the checker infers as the narrower '{ n: number; name: string }[]' (the conversion would drop 'extra', which the object keeps at run time — annotate the callback's return type or the call's type argument with a type that declares them) is not supported yet + + 10 | if (plain.length === 0) return plain; + 11 | return plain.map((x) => ({ ...x, extra: true })) satisfies Plain[]; + | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 12 | }); \ No newline at end of file From 39e3b4a5dd6947b9b26f2104fec566e33d60ab9a Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 09:34:19 -0500 Subject: [PATCH 81/83] Adopt thenable entries and place tuple slots by position in mixed Promise.all - A checked-dynamic entry of a tuple Promise.all goes through PromiseResolve (`dyn.promiseResolve`): a native promise is itself, a thenable adopts its settlement through its resolve job, and any other value fulfills. Before, a thenable fulfilled the slot with the object. - Tuple fields read their entry by position. Shape fields are sorted by name, so tuples of more than ten entries paired fields with the wrong entries. --- .../compiler/src/backend/llvm/lib-shared.ts | 1 + .../frontend/lowering/builtins/promises.ts | 29 +++++++++---------- .../frontend/lowering/checked-promise-all.ts | 2 +- .../compiler/src/ir/builtin-signatures.ts | 4 +++ packages/compiler/src/ir/runtime-features.ts | 1 + packages/runtime/src/scr_async_dyn.c | 15 ++++++---- packages/runtime/src/scr_dyn.h | 4 +++ tests/corpus/6482-promise-all-tuples.ts | 23 ++++++++++++++- 8 files changed, 56 insertions(+), 23 deletions(-) diff --git a/packages/compiler/src/backend/llvm/lib-shared.ts b/packages/compiler/src/backend/llvm/lib-shared.ts index 44741edc8..9ddcb6240 100644 --- a/packages/compiler/src/backend/llvm/lib-shared.ts +++ b/packages/compiler/src/backend/llvm/lib-shared.ts @@ -922,6 +922,7 @@ export const LIB_FN_SYMS: Record = { "dyn.promiseAll": "scr_dyn_promise_all", "dyn.promiseAllSettled": "scr_dyn_promise_all_settled", "dyn.promiseAny": "scr_dyn_promise_any", + "dyn.promiseResolve": "scr_dyn_promise_resolve", "dyn.typeof": "scr_dyn_typeof", "dyn.objectTag": "scr_dyn_object_tag", "dyn.freeze": "scr_dyn_freeze", diff --git a/packages/compiler/src/frontend/lowering/builtins/promises.ts b/packages/compiler/src/frontend/lowering/builtins/promises.ts index 5d4a2e3f2..6ac1232b9 100644 --- a/packages/compiler/src/frontend/lowering/builtins/promises.ts +++ b/packages/compiler/src/frontend/lowering/builtins/promises.ts @@ -1,4 +1,4 @@ -import { checkedPromiseAll } from "../checked-promise-all.js"; +import { checkedPromiseAll, thenReader } from "../checked-promise-all.js"; import { InternalCompilerError } from "../../../errors.js"; import * as ts from "../../ts7/adapter.js"; import { newFnCtx, type Lowerer } from "../lowerer.js"; @@ -6,7 +6,6 @@ import { varRef } from "../../../ir/build.js"; import { maybePromiseResolve, maybePromiseUnionOf } from "../maybe-promise.js"; import { isJsSourceFile, locOf } from "../../program.js"; import { - BOOL, DYN, type IrExpr, type IrFunction, @@ -31,7 +30,8 @@ import { * checked-dynamic tree. Non-promise entries (numbers, strings, booleans, * unit values — never thenable) pass straight into their slot; a * checked-dynamic entry (a JavaScript function's result) joins through - * the promise it holds, or a promise fulfilled with its plain value. The tuple + * PromiseResolve: the promise it holds, a promise that adopts a thenable, + * or a promise fulfilled with its plain value. The tuple * fulfills one microtask turn after the combinator does. Null when the * result type or an entry has no typed home here. */ function lowerTuplePromiseAll( @@ -68,8 +68,9 @@ function lowerTuplePromiseAll( const ref = (i: number): IrExpr => varRef(`entry.${i}`, elems[i]!.type, loc); const dynPromise: IrType = { kind: "promise", inner: DYN }; const body: IrStmt[] = []; - // Each dyn entry's promise view: the promise it holds, or a promise - // fulfilled with the plain value (Promise.resolve's rule). + // Each dyn entry's promise view, in entry order: the promise it + // holds, a promise that adopts a thenable's settlement, or a promise + // fulfilled with the plain value (PromiseResolve's rule). const views = new Map(); elems.forEach((e, i) => { if (e.type.kind !== "dyn") return; @@ -78,16 +79,9 @@ function lowerTuplePromiseAll( kind: "varDecl", localId: view.id, init: { - kind: "ternary", - cond: { kind: "dynTest", test: "promise", value: ref(i), type: BOOL, loc }, - then: { kind: "dynCheck", value: ref(i), type: dynPromise, loc }, - else_: { - kind: "intrinsic", - name: "promise.resolve", - args: [ref(i)], - type: dynPromise, - loc, - }, + kind: "libCall", + fn: "dyn.promiseResolve", + args: [ref(i), thenReader(lowerer, loc)], type: dynPromise, loc, }, @@ -138,7 +132,10 @@ function lowerTuplePromiseAll( type: VOID, loc, }; - const fields = shape.fields.map((field, i) => { + // Shape fields are sorted by name ("10" before "2"); a tuple field's + // name is its position. + const fields = shape.fields.map((field) => { + const i = Number(field.name); const entry = elems[i]!; let value: IrExpr; const view = views.get(i); diff --git a/packages/compiler/src/frontend/lowering/checked-promise-all.ts b/packages/compiler/src/frontend/lowering/checked-promise-all.ts index c3be91833..6c24a8b2c 100644 --- a/packages/compiler/src/frontend/lowering/checked-promise-all.ts +++ b/packages/compiler/src/frontend/lowering/checked-promise-all.ts @@ -11,7 +11,7 @@ const COMBINATOR_LIB_CALLS = { /** `value.then` through the program's own property dispatch, so a class * instance's `then` method is visible to the runtime's thenable check. */ -function thenReader(lowerer: Lowerer, loc: SrcLoc): IrExpr { +export function thenReader(lowerer: Lowerer, loc: SrcLoc): IrExpr { const name = "%promise.thenOf"; const type = funcOf([DYN], DYN); if (!lowerer.liftedFns.some((fn) => fn.name === name)) { diff --git a/packages/compiler/src/ir/builtin-signatures.ts b/packages/compiler/src/ir/builtin-signatures.ts index c55444e60..55e66f667 100644 --- a/packages/compiler/src/ir/builtin-signatures.ts +++ b/packages/compiler/src/ir/builtin-signatures.ts @@ -239,6 +239,10 @@ export const LIB_FN_SIGS = defineLibFnSignatures({ "dyn.promiseAll": { argTypes: [DYN, DYN], result: { kind: "promise", inner: DYN } }, "dyn.promiseAllSettled": { argTypes: [DYN, DYN], result: { kind: "promise", inner: DYN } }, "dyn.promiseAny": { argTypes: [DYN, DYN], result: { kind: "promise", inner: DYN } }, + /** PromiseResolve over one checked-dynamic value: the promise it holds, + * a promise that adopts a thenable's settlement, or a promise fulfilled + * with the value. The second argument reads `then` as above. */ + "dyn.promiseResolve": { argTypes: [DYN, DYN], result: { kind: "promise", inner: DYN } }, /** Bare `typeof v` on a dyn value AS A STRING (arg: the dyn value, * borrowed; result: an owned string) — the dyn kind's JS answer: * undefined→"undefined", null/object/array/bytes→"object" (JS's oldest diff --git a/packages/compiler/src/ir/runtime-features.ts b/packages/compiler/src/ir/runtime-features.ts index 9abe15345..46e44e0e1 100644 --- a/packages/compiler/src/ir/runtime-features.ts +++ b/packages/compiler/src/ir/runtime-features.ts @@ -21,6 +21,7 @@ const DYN_ASYNC_LIB_FNS: ReadonlySet = new Set([ "dyn.promiseAll", "dyn.promiseAllSettled", "dyn.promiseAny", + "dyn.promiseResolve", "process.onUncaughtException", "process.offUncaughtException", "process.onUnhandledRejection", diff --git a/packages/runtime/src/scr_async_dyn.c b/packages/runtime/src/scr_async_dyn.c index 1cdd466c3..ec4dad794 100644 --- a/packages/runtime/src/scr_async_dyn.c +++ b/packages/runtime/src/scr_async_dyn.c @@ -1437,24 +1437,29 @@ static void scr_dyn_thenable_schedule(ScrPromise *target, const ScrDyn *thenable * a thenable gets a fresh promise and its resolve job, anything else a * fulfilled promise (a throwing `then` read rejects it). The element * joins the combinator right after, so a thenable's job sits between - * the jobs of the entries around it, exactly Node's iteration. */ -static ScrPromise *scr_dyn_promise_entry(ScrCombine *c, ScrDyn *value, const ScrDyn *reader) { + * the jobs of the entries around it, exactly Node's iteration. Without a + * combinator (`c` NULL) this is PromiseResolve for one value. */ +static ScrPromise *scr_dyn_promise_entry(ScrCombine *c, const ScrDyn *value, const ScrDyn *reader) { if (value->kind == SCR_DYN_PROMISE) return scr_promise_retain(value->v.promise); ScrPromise *source = scr_promise_new(); ScrDyn *then = scr_dyn_then_of(value, reader); if (scr_exc_pending()) { scr_promise_reject_pending(source); } else if (then) { - scr_promise_combine_flush(c); + if (c) scr_promise_combine_flush(c); scr_dyn_thenable_schedule(source, value, then, reader); } else { - scr_promise_fulfill_ref(source, scr_dyn_retain(value), scr_dyn_retain_v, scr_dyn_release_v, - scr_dyn_trace_v); + scr_promise_fulfill_ref(source, scr_dyn_retain((ScrDyn *)value), scr_dyn_retain_v, + scr_dyn_release_v, scr_dyn_trace_v); } scr_dyn_release(then); return source; } +ScrPromise *scr_dyn_promise_resolve(const ScrDyn *value, const ScrDyn *reader) { + return scr_dyn_promise_entry(NULL, value, reader); +} + static void scr_dyn_settled_store(ScrArr *a, double i, ScrPromise *src) { ScrDyn *record = scr_dyn_new_obj(); bool fulfilled = !scr_promise_is_rejected(src); diff --git a/packages/runtime/src/scr_dyn.h b/packages/runtime/src/scr_dyn.h index a3d506a10..93170a1e3 100644 --- a/packages/runtime/src/scr_dyn.h +++ b/packages/runtime/src/scr_dyn.h @@ -1309,6 +1309,10 @@ extern SCR_TL ScrDyn *(*scr_dyn_promise_member_fn)(const char *key, size_t len); ScrPromise *scr_dyn_promise_all(const ScrDyn *entries, const ScrDyn *reader); ScrPromise *scr_dyn_promise_all_settled(const ScrDyn *entries, const ScrDyn *reader); ScrPromise *scr_dyn_promise_any(const ScrDyn *entries, const ScrDyn *reader); +/* PromiseResolve over one dyn value (borrowed): a native promise is + * itself, a thenable adopts through its resolve job, a throwing `then` + * read rejects, and any other value fulfills. +1. */ +ScrPromise *scr_dyn_promise_resolve(const ScrDyn *value, const ScrDyn *reader); /* Native predicates for typed reference capsules and boxed errors. */ bool scr_dyn_ref_typeof_is(const ScrDyn *d, const char *name); diff --git a/tests/corpus/6482-promise-all-tuples.ts b/tests/corpus/6482-promise-all-tuples.ts index ff1615604..3c5e38e29 100644 --- a/tests/corpus/6482-promise-all-tuples.ts +++ b/tests/corpus/6482-promise-all-tuples.ts @@ -1,7 +1,8 @@ // Promise.all over an array literal of DIFFERENT promise types (the // checker's tuple overload): destructuring with holes and nested // patterns, void entries, plain values, `any` entries that may or may not -// hold a promise, entry identity, and rejection — +// hold a promise or a thenable, tuples of more than ten entries, entry +// identity, and rejection — // the first rejection in settlement order wins, later ones count as // handled. interface Version { @@ -48,6 +49,18 @@ function untypedRecord(): any { return Promise.resolve({ label: "from a promise" }); } +// A thenable that is not a native promise: Promise.all adopts its +// settlement, calling `then` one microtask after the call. +class Deferred { + then(resolve: (value: string) => void): void { + console.log("then called"); + resolve("adopted"); + } +} +function untypedThenable(): any { + return new Deferred(); +} + async function main(): Promise { const [runtime, { tool: kind, schema: level }] = await Promise.all([version(), scan()]); console.log(runtime.major, kind, level, runtime === shared); @@ -64,6 +77,14 @@ async function main(): Promise { const [count, { label }, plain] = await Promise.all([untypedCount(), untypedRecord(), "p"]); console.log(count + 1, label, plain); + const pending = Promise.all([untypedThenable(), version()]); + console.log("after the call"); + const [adopted, latest] = await pending; + console.log(adopted, latest.major); + + const many = await Promise.all([version(), 1, 2, 3, 4, 5, 6, 7, 8, 9, "ten", scan()]); + console.log(many[0].major, many[2], many[9], many[10], many[11].tool); + try { await Promise.all([delay(20, 1, "slow failure"), delay(5, "s", "fast failure"), delay(1, true)]); } catch (e) { From cb99a06fe59bb77f94405f002d9af86c338006d7 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 09:35:14 -0500 Subject: [PATCH 82/83] Shadow the export alias only in the scope that declares it again The export-alias rewrite skipped a whole function when a let or const anywhere inside it declared the alias's name, so the function's other references to the alias kept the alias name after its declaration was removed. A function now shadows the alias only through its parameters, its own name, its vars and the declarations directly in its body; a block, case block, catch clause or for loop shadows it through its own declarations, and the walk skips just that scope. --- .../frontend/npm-static-cjs-exports-var.ts | 94 +++++++++++++------ .../npm/cases/cjs-exports-var/main.ts | 4 + .../npm/node_modules/exportsvar/index.d.ts | 2 + .../npm/node_modules/exportsvar/lib/ip.js | 14 +++ 4 files changed, 87 insertions(+), 27 deletions(-) diff --git a/packages/compiler/src/frontend/npm-static-cjs-exports-var.ts b/packages/compiler/src/frontend/npm-static-cjs-exports-var.ts index 38972990c..1af4ea1ed 100644 --- a/packages/compiler/src/frontend/npm-static-cjs-exports-var.ts +++ b/packages/compiler/src/frontend/npm-static-cjs-exports-var.ts @@ -62,47 +62,80 @@ function bindsName(name: ts.BindingName | undefined, text: string): boolean { return false; } -/** True when `scope` (a function, or a block or catch clause) declares - * `text` itself, so references inside it name that binding instead of the - * module-level alias: a parameter, a var/let/const or a function or class - * declaration in a function (nested functions excluded), a let/const or - * class in a block, a catch binding. */ -function shadows(scope: ts.Node, text: string): boolean { - if (isFunctionScope(scope)) { - const fn = scope as ts.SignatureDeclaration; - if (fn.parameters.nodes.some((p) => bindsName(p.name, text))) return true; +/** True when one of `statements` declares `text` lexically: a let/const, + * or a class or function declaration. */ +function declaresLexically(statements: readonly ts.Statement[], text: string): boolean { + for (const stmt of statements) { if ( - (ts.isFunctionExpression(scope) || ts.isClassExpression(scope)) && - scope.name?.text === text + ts.isVariableStatement(stmt) && + (stmt.declarationList.flags & ts.NodeFlags.BlockScoped) !== 0 && + stmt.declarationList.declarations.nodes.some((d) => bindsName(d.name, text)) ) return true; - } else if (ts.isCatchClause(scope)) { - if (bindsName(scope.variableDeclaration?.name, text)) return true; - } else if (!ts.isBlock(scope)) { - return false; + if ((ts.isClassDeclaration(stmt) || ts.isFunctionDeclaration(stmt)) && stmt.name?.text === text) + return true; } - const functionScope = isFunctionScope(scope); + return false; +} + +/** True when a function body declares `text` with `var` (or a function + * declaration in a nested block) anywhere outside nested functions. */ +function declaresVar(body: ts.Node, text: string): boolean { let found = false; - ts.walkPreorder(scope, (n) => { + ts.walkPreorder(body, (n) => { if (found) return "stop"; - if (n === scope) return undefined; + if (n === body) return undefined; if (isFunctionScope(n)) { if (ts.isFunctionDeclaration(n) && n.name?.text === text) found = true; return "skip"; } - if (!functionScope && ts.isBlock(n)) return "skip"; /* its own scope */ - if (ts.isVariableDeclaration(n) && bindsName(n.name, text)) { - const list = n.parent; - const blockScoped = - ts.isVariableDeclarationList(list) && (list.flags & ts.NodeFlags.BlockScoped) !== 0; - if (functionScope || blockScoped) found = true; - } - if (ts.isClassDeclaration(n) && n.name?.text === text) found = true; + if ( + ts.isVariableDeclarationList(n) && + (n.flags & ts.NodeFlags.BlockScoped) === 0 && + n.declarations.nodes.some((d) => bindsName(d.name, text)) + ) + found = true; return undefined; }); return found; } +/** True when `scope` declares `text` itself, so references inside it name + * that binding instead of the module-level alias: a function's parameters, + * own name, `var`s and the let/const, class and function declarations of + * its body; a block's or case block's own let/const, class and function + * declarations; a catch binding; a for loop's let/const. A let/const in a + * nested block shadows only that block, which the walk reaches as its own + * scope. */ +function shadows(scope: ts.Node, text: string): boolean { + if (isFunctionScope(scope)) { + const fn = scope as ts.SignatureDeclaration; + if (fn.parameters.nodes.some((p) => bindsName(p.name, text))) return true; + if (ts.isFunctionExpression(scope) && scope.name?.text === text) return true; + const body = (scope as { body?: ts.Node }).body; + if (body === undefined || !ts.isBlock(body)) return false; + return declaresVar(body, text) || declaresLexically(body.statements.nodes, text); + } + if (ts.isBlock(scope)) return declaresLexically(scope.statements.nodes, text); + if (ts.isCaseBlock(scope)) + return declaresLexically( + scope.clauses.nodes.flatMap((clause) => clause.statements.nodes), + text, + ); + if (ts.isCatchClause(scope)) return bindsName(scope.variableDeclaration?.name, text); + if (ts.isForStatement(scope) || ts.isForInStatement(scope) || ts.isForOfStatement(scope)) { + const init = scope.initializer; + return ( + init !== undefined && + ts.isVariableDeclarationList(init) && + (init.flags & ts.NodeFlags.BlockScoped) !== 0 && + init.declarations.nodes.some((d) => bindsName(d.name, text)) + ); + } + if (ts.isClassExpression(scope)) return scope.name?.text === text; + return false; +} + export function normalizeCjsExportsVar(sf: ts.SourceFile): string | null { const source = sf.text; if (!/\b(?:var|let|const)\s+[A-Za-z_$][\w$]*\s*=\s*exports\s*[;,\n]/.test(source)) return null; @@ -149,7 +182,14 @@ export function normalizeCjsExportsVar(sf: ts.SourceFile): string | null { // that binding, not the export object. if ( n !== sf && - (isFunctionScope(n) || ts.isBlock(n) || ts.isCatchClause(n)) && + (isFunctionScope(n) || + ts.isBlock(n) || + ts.isCaseBlock(n) || + ts.isCatchClause(n) || + ts.isForStatement(n) || + ts.isForInStatement(n) || + ts.isForOfStatement(n) || + ts.isClassExpression(n)) && shadows(n, name) ) return "skip"; diff --git a/tests/fixtures/npm/cases/cjs-exports-var/main.ts b/tests/fixtures/npm/cases/cjs-exports-var/main.ts index 60a13b850..ec72ba751 100644 --- a/tests/fixtures/npm/cases/cjs-exports-var/main.ts +++ b/tests/fixtures/npm/cases/cjs-exports-var/main.ts @@ -10,3 +10,7 @@ for (const addr of ["10.0.0.1", "192.168.1.20", "127.0.0.1", "::1", "8.8.8.8"]) console.log(ip.toLong("192.168.1.1")); // Members whose parameter shadows the alias name. console.log(ip.isV4Format("1.2.3.4"), ip.isV4Format("::1"), ip.normalize(" FE80::1 ")); +// Members with a block-local `let` or a loop `const` of the alias name: +// only that block or loop names the local binding. +console.log(ip.classify("FE80::1"), ip.classify("10.1.2.3"), ip.classify("8.8.4.4")); +console.log(ip.countPrivate(["127.0.0.1", "10.0.0.2", "10.9.9.9"]), ip.countPrivate(["10.0.0.1"])); diff --git a/tests/fixtures/npm/node_modules/exportsvar/index.d.ts b/tests/fixtures/npm/node_modules/exportsvar/index.d.ts index 8782f2267..fa65c9064 100644 --- a/tests/fixtures/npm/node_modules/exportsvar/index.d.ts +++ b/tests/fixtures/npm/node_modules/exportsvar/index.d.ts @@ -4,3 +4,5 @@ export function isPublic(addr: string): boolean; export function toLong(addr: string): number; export function isV4Format(ip: string): boolean; export function normalize(ip: string): string; +export function classify(addr: string): string; +export function countPrivate(addrs: string[]): number; diff --git a/tests/fixtures/npm/node_modules/exportsvar/lib/ip.js b/tests/fixtures/npm/node_modules/exportsvar/lib/ip.js index 8e782dad2..074ed8318 100644 --- a/tests/fixtures/npm/node_modules/exportsvar/lib/ip.js +++ b/tests/fixtures/npm/node_modules/exportsvar/lib/ip.js @@ -33,3 +33,17 @@ ip.normalize = function(ip) { var trimmed = ip.trim(); return trimmed.toLowerCase(); }; + +ip.classify = function(addr) { + if (addr.indexOf(':') !== -1) { + let ip = addr.toLowerCase(); + return 'v6 ' + ip; + } + return ip.isPrivate(addr) ? 'private' : 'public'; +}; + +ip.countPrivate = function(addrs) { + var n = 0; + for (const ip of addrs) if (/^10\./.test(ip)) n++; + return n + (ip.isLoopback(addrs[0]) ? 100 : 0); +}; From 2330953ebe7918f43165813c17d9991b16c7124a Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Tue, 20 Oct 2026 09:49:10 -0500 Subject: [PATCH 83/83] Write the byte order mark in a corpus test as an escape --- tests/corpus/6230-date-parse-legacy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/corpus/6230-date-parse-legacy.ts b/tests/corpus/6230-date-parse-legacy.ts index 970050cd0..cc71961c0 100644 --- a/tests/corpus/6230-date-parse-legacy.ts +++ b/tests/corpus/6230-date-parse-legacy.ts @@ -68,7 +68,7 @@ const fixed = [ "Jan 1 9999999 GMT", "2024-01-01T10:00:00\u0000junk", "Jan 1
2024", - "Jan 1 2024", + "\uFEFFJan 1 2024", "Jan 1 2024 été", "0", "12",