mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-12-06 04:56:15 +08:00
perf_hooks: read the constants object's members and value (#4272)
- Reads of `perf_hooks.constants` members, destructuring, and the object held as a value now answer Node's values, with key walks in Node's order. - A write to a spelled member is refused where it runs, as Node's read-only members throw there. - The bundled Node declarations name the object as @types/node does, so programs without @types/node can type it.
This commit is contained in:
@@ -122,6 +122,9 @@
|
||||
// from the same baked table the value lowering builds the
|
||||
// singleton from; crypto-tables.ts is data with no imports.
|
||||
"CRYPTO_CONSTANTS",
|
||||
// The same for perf_hooks.constants: perf-hooks-constants.ts
|
||||
// is the baked number table, data with no imports.
|
||||
"PERF_HOOKS_CONSTANTS",
|
||||
],
|
||||
"message": "a frontend module outside lowering may import lowering only with `import type`; a value import pulls the lowering module graph into every program that loads program.ts.",
|
||||
},
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
{ "chapter": "perf_hooks", "parent": "Class: `PerformanceObserver`", "symbols": ["supportedEntryTypes"], "status": "supported", "evidence": ["tests/corpus/perf-supported-entry-types.ts", "tests/corpus/perf-node-timing-dyn.cjs"] },
|
||||
{ "chapter": "perf_hooks", "symbols": ["perf_hooks.eventLoopUtilization"], "status": "supported", "evidence": ["tests/corpus/perf-event-loop-utilization.ts", "tests/corpus/perf-hooks-surface.ts"] },
|
||||
{ "chapter": "perf_hooks", "symbols": ["perf_hooks.performance"], "status": "partial", "reason": "The performance object lowers through its own members (now, mark, measure, the readers, nodeTiming, timeOrigin, toJSON); passing the object itself to other code has no lowering.", "evidence": ["tests/corpus/perf-event-loop-utilization.ts", "tests/corpus/perf-node-timing-dyn.cjs", "tests/corpus/perf-performance-tojson.ts"] },
|
||||
{ "chapter": "perf_hooks", "symbols": ["perf_hooks.constants"], "status": "supported", "reason": "Every name reads its Node v24 value where it is spelled (the GC kind and flag numbers @types/node declares, and the entry-type and milestone numbers the runtime object also carries), destructuring reads the same values, and the object held as a value walks Node's enumerable GC constants in insertion order through Object.keys, spread and JSON.stringify. Node also carries the entry-type and milestone numbers as own non-enumerable properties: a key walk does not list them here either, while Object.getOwnPropertyNames lists the twelve enumerable names rather than all twenty-six, and a read (or `in`) through a held value answers absent where Node answers the number (each name still bakes where a source spells it). A write through a held value changes that copy where Node throws its TypeError; a write to the object as spelled is refused where the program is built.", "evidence": ["tests/corpus/perf-hooks-constants.mjs", "tests/corpus/perf-hooks-constants.ts"] },
|
||||
{ "chapter": "perf_hooks", "symbols": ["Histogram"], "status": "partial", "reason": "The numeric and BigInt readings, percentile, countAt, cdf/ccdf, the percentiles Maps, the bucket maps (linearBuckets, logBuckets, percentilesAt), the ksTest, cohensD, cliffsD and mannWhitneyTest comparisons, the shape moments (skewness and kurtosis), the EWMA readings, burnRate, and the confidence-interval and Student's t tests (meanCI, percentileCI, welchTest) lower for createHistogram's RecordableHistogram, and the readings and burnRate also for monitorEventLoopDelay's IntervalHistogram (whose bucket maps are not lowered); qrde and export/importHistogram are not implemented.", "evidence": ["tests/corpus/9999-histogram-core.ts", "tests/corpus/9990-histogram-comparison.ts", "tests/corpus/perf-histogram-ewma.ts", "tests/corpus/perf-histogram-inference.ts", "tests/corpus/9998-histogram-recordable.js", "tests/corpus/9995-histogram-upcast.ts", "tests/corpus/9994-histogram-short-lived.ts", "tests/corpus/perf-hooks-event-loop-histogram.ts", "tests/corpus/6397-event-loop-metrics.ts"] },
|
||||
{ "chapter": "perf_hooks", "signatures": ["`perf_hooks.timerify(fn[, options])`", "`performance.timerify(fn[, options])`"], "status": "supported", "evidence": ["tests/corpus/9976-timerify.js"] },
|
||||
{ "chapter": "perf_hooks", "symbols": ["histogram.meanCI", "histogram.percentileCI", "histogram.welchTest"], "status": "supported", "evidence": ["tests/corpus/perf-histogram-inference.ts"] },
|
||||
|
||||
@@ -2121,6 +2121,23 @@ declare module "node:perf_hooks" {
|
||||
): EventLoopUtilization;
|
||||
}
|
||||
export const performance: Performance;
|
||||
/* perf_hooks.constants: the GC kind and flag values a 'gc' entry's detail
|
||||
* carries, as @types/node declares them. Every name reads its Node v24
|
||||
* value at the access site (the zlib.constants stance); the object value
|
||||
* walks the same enumerable GC numbers. */
|
||||
export namespace constants {
|
||||
const NODE_PERFORMANCE_GC_MAJOR: number;
|
||||
const NODE_PERFORMANCE_GC_MINOR: number;
|
||||
const NODE_PERFORMANCE_GC_INCREMENTAL: number;
|
||||
const NODE_PERFORMANCE_GC_WEAKCB: number;
|
||||
const NODE_PERFORMANCE_GC_FLAGS_NO: number;
|
||||
const NODE_PERFORMANCE_GC_FLAGS_CONSTRUCT_RETAINED: number;
|
||||
const NODE_PERFORMANCE_GC_FLAGS_FORCED: number;
|
||||
const NODE_PERFORMANCE_GC_FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING: number;
|
||||
const NODE_PERFORMANCE_GC_FLAGS_ALL_AVAILABLE_GARBAGE: number;
|
||||
const NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY: number;
|
||||
const NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE: number;
|
||||
}
|
||||
/* Event-loop metrics: utilization (idle/active ms since the loop started)
|
||||
* and monitorEventLoopDelay's sampling histogram (nanoseconds). */
|
||||
export interface EventLoopUtilization {
|
||||
|
||||
@@ -6,6 +6,7 @@ import { HTTP2_CONSTANTS } from "../http2-constants.js";
|
||||
import { CRYPTO_CONSTANTS } from "../crypto-tables.js";
|
||||
import { ZLIB_CONSTANTS } from "../zlib-constants.js";
|
||||
import { SQLITE_CONSTANTS } from "../sqlite-constants.js";
|
||||
import { PERF_HOOKS_CONSTANTS, PERF_HOOKS_HIDDEN_CONSTANTS } from "../perf-hooks-constants.js";
|
||||
import {
|
||||
BOOL,
|
||||
DYN,
|
||||
@@ -24,9 +25,11 @@ import { builtinImportOf } from "./module-bindings.js";
|
||||
/** The builtin modules whose `constants` object bakes as literals at
|
||||
* every access site (the fs.constants precedent, scaled up): http2's
|
||||
* full Node v24 table, crypto's OpenSSL-constant table, zlib's table,
|
||||
* and buffer's MAX_LENGTH. The members never materialize, but the object
|
||||
* itself does where a program reads it whole: cryptoConstantsValue and
|
||||
* zlibConstantsValue build each module's singleton from its table. */
|
||||
* buffer's MAX_LENGTH, and perf_hooks' GC, entry-type and milestone
|
||||
* numbers. The members never materialize, but the object itself does
|
||||
* where a program reads it whole: cryptoConstantsValue,
|
||||
* zlibConstantsValue and perfHooksConstantsValue build each module's
|
||||
* singleton from its table. */
|
||||
const BUILTIN_CONSTANTS_TABLES: Record<
|
||||
string,
|
||||
{ table: Record<string, number | string>; hint: string } | undefined
|
||||
@@ -51,6 +54,10 @@ const BUILTIN_CONSTANTS_TABLES: Record<
|
||||
table: SQLITE_CONSTANTS,
|
||||
hint: "the table bakes Node v24's sqlite.constants members as literals — this name is not one of them",
|
||||
},
|
||||
perf_hooks: {
|
||||
table: { ...PERF_HOOKS_CONSTANTS, ...PERF_HOOKS_HIDDEN_CONSTANTS },
|
||||
hint: "the table bakes Node v24's perf_hooks.constants members as literals — this name is not one of them",
|
||||
},
|
||||
};
|
||||
|
||||
/** The module whose baked-constants OBJECT `node` denotes, or null — any
|
||||
@@ -59,7 +66,7 @@ const BUILTIN_CONSTANTS_TABLES: Record<
|
||||
* alias from the module, or `require("http2").constants` inline. The
|
||||
* object itself never materializes; each member read bakes as its
|
||||
* literal. */
|
||||
function builtinConstantsModuleOf(lowerer: Lowerer, node: ts.Expression): string | null {
|
||||
export function builtinConstantsModuleOf(lowerer: Lowerer, node: ts.Expression): string | null {
|
||||
let module: string | null = null;
|
||||
if (ts.isPropertyAccessExpression(node) && !node.questionDotToken) {
|
||||
if (node.name.text !== "constants") return null;
|
||||
@@ -148,38 +155,45 @@ export function builtinConstantBindingOf(lowerer: Lowerer, ident: ts.Identifier)
|
||||
return builtinConstLit(value, locOf(ident));
|
||||
}
|
||||
|
||||
/** `zlib.constants` as a VALUE: the object Node's module answers — one
|
||||
* singleton per program, built on first use with the same table every
|
||||
* member read bakes (lowerBuiltinConstantsProperty), so identity,
|
||||
* `typeof`, `Object.keys`, spread and `JSON.stringify` see Node's object
|
||||
* rather than a fresh literal per use. The record is the `ZlibConstants`
|
||||
* shape the type mapper interns for the declared interface (the fields
|
||||
* match one for one); the members stay baked literals where the program
|
||||
* spells them, and the keys keep Node's insertion order (the table's). */
|
||||
export function zlibConstantsValue(lowerer: Lowerer, loc: SrcLoc): IrExpr {
|
||||
const keys = Object.keys(ZLIB_CONSTANTS);
|
||||
/** A baked constants object as a VALUE (`zlib.constants`,
|
||||
* `crypto.constants`, `perf_hooks.constants`): the object Node's module
|
||||
* answers — one singleton per program, built on first use from the same
|
||||
* table every member read bakes (lowerBuiltinConstantsProperty), so
|
||||
* identity, `typeof`, `Object.keys`, spread and `JSON.stringify` see
|
||||
* Node's object rather than a fresh literal per use. The record's fields
|
||||
* are the table's, in Node's insertion order (which Object.keys
|
||||
* observes); the members stay baked literals where the program spells
|
||||
* them. */
|
||||
function bakedConstantsValue(
|
||||
lowerer: Lowerer,
|
||||
loc: SrcLoc,
|
||||
module: string,
|
||||
table: Record<string, number | string>,
|
||||
): IrExpr {
|
||||
const keys = Object.keys(table);
|
||||
const fieldType = (name: string): IrType => (typeof table[name] === "string" ? STRING : F64);
|
||||
const type: IrType & { kind: "record" } = {
|
||||
kind: "record",
|
||||
shapeId: lowerer.shapes.intern(
|
||||
keys
|
||||
.map((name) => ({ name, type: F64 as IrType }))
|
||||
.map((name) => ({ name, type: fieldType(name) }))
|
||||
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)),
|
||||
false,
|
||||
undefined,
|
||||
keys,
|
||||
),
|
||||
};
|
||||
const name = keyedHelperName(lowerer, "%builtin.zlib.constants", type.shapeId);
|
||||
const name = keyedHelperName(lowerer, `%builtin.${module}.constants`, type.shapeId);
|
||||
if (lowerer.liftedFnNamed(name) === undefined) {
|
||||
const shape = lowerer.shapes.get(type.shapeId);
|
||||
const declared = new Set((shape?.fields ?? []).map((field) => field.name));
|
||||
if (declared.size !== keys.length || keys.some((key) => !declared.has(key)))
|
||||
throw new InternalCompilerError(
|
||||
"zlib.constants table and the ZlibConstants record shape disagree",
|
||||
`${module}.constants table and the constants record shape disagree`,
|
||||
);
|
||||
const fields = keys.map((name) => ({
|
||||
name,
|
||||
value: builtinConstLit(ZLIB_CONSTANTS[name]!, loc),
|
||||
value: builtinConstLit(table[name]!, loc),
|
||||
}));
|
||||
// Node inserts the keys in table order and Object.keys observes it:
|
||||
// the literal carries the order.
|
||||
@@ -190,7 +204,7 @@ export function zlibConstantsValue(lowerer: Lowerer, loc: SrcLoc): IrExpr {
|
||||
const storage = `%g.${name}`;
|
||||
const ready = `${storage}%ready`;
|
||||
lowerer.globalsList.push(
|
||||
{ id: storage, name: "zlib.constants", type, mutable: true },
|
||||
{ id: storage, name: `${module}.constants`, type, mutable: true },
|
||||
{ id: ready, name: "%ready", type: BOOL, mutable: true },
|
||||
);
|
||||
const body: IrStmt[] = [
|
||||
@@ -224,83 +238,24 @@ export function zlibConstantsValue(lowerer: Lowerer, loc: SrcLoc): IrExpr {
|
||||
return { kind: "call", callee: name, args: [], type, loc };
|
||||
}
|
||||
|
||||
/** `crypto.constants` as a VALUE: the object Node's module answers — one
|
||||
* singleton per program, built on first use from the same table every
|
||||
* member read bakes (lowerBuiltinConstantsProperty), so identity,
|
||||
* `typeof`, `Object.keys`, spread and `JSON.stringify` see Node's object
|
||||
* rather than a fresh literal per use. The record is the `CryptoConstants`
|
||||
* shape the type mapper interns for the declared interface (the fields
|
||||
* match one for one: the number constants and the two cipher-list
|
||||
* strings); the members stay baked literals where the program spells
|
||||
* them, and the keys keep Node's insertion order (the table's). */
|
||||
/** `zlib.constants` as a value: the same fields the type mapper interns
|
||||
* for the declared `ZlibConstants` interface (all numbers). */
|
||||
export function zlibConstantsValue(lowerer: Lowerer, loc: SrcLoc): IrExpr {
|
||||
return bakedConstantsValue(lowerer, loc, "zlib", ZLIB_CONSTANTS);
|
||||
}
|
||||
|
||||
/** `crypto.constants` as a value: the number constants and the two
|
||||
* cipher-list strings the `CryptoConstants` shape carries. */
|
||||
export function cryptoConstantsValue(lowerer: Lowerer, loc: SrcLoc): IrExpr {
|
||||
const keys = Object.keys(CRYPTO_CONSTANTS);
|
||||
const fieldType = (name: string): IrType =>
|
||||
typeof CRYPTO_CONSTANTS[name] === "string" ? STRING : F64;
|
||||
const type: IrType & { kind: "record" } = {
|
||||
kind: "record",
|
||||
shapeId: lowerer.shapes.intern(
|
||||
keys
|
||||
.map((name) => ({ name, type: fieldType(name) }))
|
||||
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)),
|
||||
false,
|
||||
undefined,
|
||||
keys,
|
||||
),
|
||||
};
|
||||
const name = keyedHelperName(lowerer, "%builtin.crypto.constants", type.shapeId);
|
||||
if (lowerer.liftedFnNamed(name) === undefined) {
|
||||
const shape = lowerer.shapes.get(type.shapeId);
|
||||
const declared = new Set((shape?.fields ?? []).map((field) => field.name));
|
||||
if (declared.size !== keys.length || keys.some((key) => !declared.has(key)))
|
||||
throw new InternalCompilerError(
|
||||
"crypto.constants table and the CryptoConstants record shape disagree",
|
||||
);
|
||||
const fields = keys.map((name) => ({
|
||||
name,
|
||||
value: builtinConstLit(CRYPTO_CONSTANTS[name]!, loc),
|
||||
}));
|
||||
// Node inserts the keys in table order and Object.keys observes it:
|
||||
// the literal carries the order.
|
||||
const literal = lowerer.keyOrder.literal(
|
||||
{ kind: "recordLit", fields, type, loc },
|
||||
keys.map((name) => ({ kind: "key" as const, name, definite: true })),
|
||||
);
|
||||
const storage = `%g.${name}`;
|
||||
const ready = `${storage}%ready`;
|
||||
lowerer.globalsList.push(
|
||||
{ id: storage, name: "crypto.constants", type, mutable: true },
|
||||
{ id: ready, name: "%ready", type: BOOL, mutable: true },
|
||||
);
|
||||
const body: IrStmt[] = [
|
||||
{
|
||||
kind: "if",
|
||||
cond: varRef(ready, BOOL, loc),
|
||||
then: [{ kind: "return", value: varRef(storage, type, loc), loc }],
|
||||
else_: null,
|
||||
loc,
|
||||
},
|
||||
{ kind: "varDecl", localId: "constants.0", init: literal, loc },
|
||||
{ kind: "assign", localId: storage, value: varRef("constants.0", type, loc), loc },
|
||||
{
|
||||
kind: "assign",
|
||||
localId: ready,
|
||||
value: { kind: "boolLit", value: true, type: BOOL, loc },
|
||||
loc,
|
||||
},
|
||||
{ kind: "return", value: varRef(storage, type, loc), loc },
|
||||
];
|
||||
lowerer.liftedFns.push({
|
||||
name,
|
||||
params: [],
|
||||
locals: [{ id: "constants.0", name: "constants", type, mutable: false }],
|
||||
returnType: type,
|
||||
body,
|
||||
loc,
|
||||
});
|
||||
}
|
||||
lowerer.noteEdge(name);
|
||||
return { kind: "call", callee: name, args: [], type, loc };
|
||||
return bakedConstantsValue(lowerer, loc, "crypto", CRYPTO_CONSTANTS);
|
||||
}
|
||||
|
||||
/** `perf_hooks.constants` as a value: the enumerable GC numbers Node's
|
||||
* object walks (the entry-type and milestone ones are own but
|
||||
* non-enumerable, so they stay out of the record; each still bakes where
|
||||
* a source spells it). */
|
||||
export function perfHooksConstantsValue(lowerer: Lowerer, loc: SrcLoc): IrExpr {
|
||||
return bakedConstantsValue(lowerer, loc, "perf_hooks", PERF_HOOKS_CONSTANTS);
|
||||
}
|
||||
|
||||
/** `constants.X_OK` where `constants` is a named fs import: the access-
|
||||
|
||||
@@ -10,7 +10,11 @@ import { hostGlobalValue } from "../host-global-values.js";
|
||||
import { predeclareForwardClass } from "../lower-stmts.js";
|
||||
import { cjsMemberAliasValueOf } from "../cjs-exports-object.js";
|
||||
import { builtinClassValue, builtinMemberClassKind } from "../builtins/builtin-class-dyn.js";
|
||||
import { cryptoConstantsValue, zlibConstantsValue } from "../builtins/constants.js";
|
||||
import {
|
||||
cryptoConstantsValue,
|
||||
perfHooksConstantsValue,
|
||||
zlibConstantsValue,
|
||||
} from "../builtins/constants.js";
|
||||
import { legacyUrlClassValue } from "../builtins/url.js";
|
||||
import { captureRejectionSymbolValue } from "../lower-event-emitter.js";
|
||||
import { lowerWebStreamClassRead } from "../builtins/web-streams-dyn.js";
|
||||
@@ -867,6 +871,10 @@ export function lowerIdentifierExpr(lowerer: Lowerer, expr: ts.Identifier, loc:
|
||||
// property form answers.
|
||||
if (bi.module === "crypto" && bi.member === "constants")
|
||||
return cryptoConstantsValue(lowerer, loc);
|
||||
// `import { constants } from "node:perf_hooks"`: the same singleton
|
||||
// the property form answers.
|
||||
if (bi.module === "perf_hooks" && bi.member === "constants")
|
||||
return perfHooksConstantsValue(lowerer, loc);
|
||||
// `import { O_RDONLY } from "constants"`: the member of the frozen
|
||||
// module object.
|
||||
if (bi.module === "constants") {
|
||||
|
||||
@@ -434,6 +434,7 @@ import { npmStaticAccessorExport, npmStaticPackageOfPath } from "../npm-static.j
|
||||
import { constructorStaticValue } from "./builtin-statics.js";
|
||||
import { packageBuiltWithoutMember } from "./package-built-values.js";
|
||||
import { fenceNodeModuleMutation, isNodeModuleHandle } from "./lower-node-module.js";
|
||||
import { builtinConstantsModuleOf } from "./builtins/constants.js";
|
||||
import { fencePerfEntryKeyWalk } from "./builtins/performance.js";
|
||||
import { fenceX509KeyWalk } from "./builtins/x509.js";
|
||||
import { classGeneratorIterable } from "./class-iterables.js";
|
||||
@@ -12522,6 +12523,16 @@ export function lowerExprStatement(
|
||||
return lowerer.fieldSetStmt(callableWrite, value, locOf(expr), expr.left);
|
||||
}
|
||||
}
|
||||
// A member of a baked constants object is read-only in Node
|
||||
// (`perf_hooks.constants.NODE_PERFORMANCE_GC_MAJOR = 1` throws its
|
||||
// TypeError), and the table's names bake where a source spells
|
||||
// them rather than living on the value: the write is refused like
|
||||
// any other non-variable. zlib.constants and crypto.constants reach
|
||||
// the same refusal below (their readonly fields give no field
|
||||
// target).
|
||||
if (builtinConstantsModuleOf(lowerer, expr.left.expression) !== null) {
|
||||
lowerer.unsupported("SC1090", expr.left, "assignment to non-variables");
|
||||
}
|
||||
const target = lowerer.fieldTarget(expr.left);
|
||||
if (target) {
|
||||
checkRefusedAccessorWrite(lowerer, target, expr.left.name);
|
||||
|
||||
@@ -795,6 +795,7 @@ import {
|
||||
builtinConstantBindingOf,
|
||||
builtinConstantsDestructureDecl,
|
||||
cryptoConstantsValue,
|
||||
perfHooksConstantsValue,
|
||||
zlibConstantsValue,
|
||||
} from "./builtins/constants.js";
|
||||
import { lowerStringStaticCall, lowerStringLastIndexOfCall } from "./builtins/strings.js";
|
||||
@@ -25829,6 +25830,10 @@ export class Lowerer {
|
||||
if (bi.member === "constants" && bi.module === "zlib") return zlibConstantsValue(this, loc);
|
||||
// `crypto.constants` as a value: the same story over the OpenSSL table.
|
||||
if (bi.member === "constants" && bi.module === "crypto") return cryptoConstantsValue(this, loc);
|
||||
// `perf_hooks.constants` as a value: the enumerable GC numbers (the
|
||||
// entry-type and milestone names are non-enumerable in Node).
|
||||
if (bi.member === "constants" && bi.module === "perf_hooks")
|
||||
return perfHooksConstantsValue(this, loc);
|
||||
// `stream.Readable` as a JavaScript value: the runtime's untyped
|
||||
// constructor, the base a constructor function inherits.
|
||||
if (isJsSourceFile(ts.getSourceFileOfNode(expr))) {
|
||||
@@ -26188,6 +26193,8 @@ export class Lowerer {
|
||||
return fsConstantsObject(loc);
|
||||
if (module === "zlib" && member === "constants") return zlibConstantsValue(this, loc);
|
||||
if (module === "crypto" && member === "constants") return cryptoConstantsValue(this, loc);
|
||||
if (module === "perf_hooks" && member === "constants")
|
||||
return perfHooksConstantsValue(this, loc);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/* node:perf_hooks constants — the values Node v24.22.1 carries on the
|
||||
* perf_hooks.constants object, generated with
|
||||
* node -p "require('perf_hooks').constants"
|
||||
* plus Object.getOwnPropertyNames/descriptors for the enumerability, and
|
||||
* baked as literals at every access site (the zlib.constants precedent).
|
||||
* The first table is the object's own enumerable properties, in its
|
||||
* insertion order (what Object.keys, spread and JSON.stringify observe on
|
||||
* the value form); the second table's names are own but non-enumerable,
|
||||
* so they answer a read by name and stay out of every key walk. */
|
||||
|
||||
export const PERF_HOOKS_CONSTANTS: Record<string, number> = {
|
||||
"NODE_PERFORMANCE_GC_MAJOR": 4,
|
||||
"NODE_PERFORMANCE_GC_MINOR": 1,
|
||||
"NODE_PERFORMANCE_GC_MINOR_MARK_SWEEP": 2,
|
||||
"NODE_PERFORMANCE_GC_INCREMENTAL": 8,
|
||||
"NODE_PERFORMANCE_GC_WEAKCB": 16,
|
||||
"NODE_PERFORMANCE_GC_FLAGS_NO": 0,
|
||||
"NODE_PERFORMANCE_GC_FLAGS_CONSTRUCT_RETAINED": 2,
|
||||
"NODE_PERFORMANCE_GC_FLAGS_FORCED": 4,
|
||||
"NODE_PERFORMANCE_GC_FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING": 8,
|
||||
"NODE_PERFORMANCE_GC_FLAGS_ALL_AVAILABLE_GARBAGE": 16,
|
||||
"NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY": 32,
|
||||
"NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE": 64,
|
||||
};
|
||||
|
||||
export const PERF_HOOKS_HIDDEN_CONSTANTS: Record<string, number> = {
|
||||
"NODE_PERFORMANCE_ENTRY_TYPE_GC": 0,
|
||||
"NODE_PERFORMANCE_ENTRY_TYPE_HTTP": 1,
|
||||
"NODE_PERFORMANCE_ENTRY_TYPE_HTTP2": 2,
|
||||
"NODE_PERFORMANCE_ENTRY_TYPE_NET": 3,
|
||||
"NODE_PERFORMANCE_ENTRY_TYPE_DNS": 4,
|
||||
"NODE_PERFORMANCE_ENTRY_TYPE_QUIC": 5,
|
||||
"NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN_TIMESTAMP": 0,
|
||||
"NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN": 1,
|
||||
"NODE_PERFORMANCE_MILESTONE_ENVIRONMENT": 2,
|
||||
"NODE_PERFORMANCE_MILESTONE_NODE_START": 3,
|
||||
"NODE_PERFORMANCE_MILESTONE_V8_START": 4,
|
||||
"NODE_PERFORMANCE_MILESTONE_LOOP_START": 5,
|
||||
"NODE_PERFORMANCE_MILESTONE_LOOP_EXIT": 6,
|
||||
"NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE": 7,
|
||||
};
|
||||
@@ -2,6 +2,7 @@ import { InternalCompilerError } from "../errors.js";
|
||||
import { keyedHelperName, type KeyedNameOwner } from "./lowering/stable-names.js";
|
||||
import { contentDigest, type HeldContents, type JournalTable } from "./lowering/unit-journal.js";
|
||||
import { CRYPTO_CONSTANTS } from "./lowering/crypto-tables.js";
|
||||
import { PERF_HOOKS_CONSTANTS } from "./lowering/perf-hooks-constants.js";
|
||||
import { mockCallRecordType, mockContextType } from "./mock-shapes.js";
|
||||
import * as ts from "./ts7/adapter.js";
|
||||
import { bodyReadsArguments } from "./arguments-usage.js";
|
||||
@@ -6968,6 +6969,35 @@ function mapTypeInner(type: ts.Type, ctx: TypeMapperCtx): IrType | null {
|
||||
),
|
||||
};
|
||||
}
|
||||
// @types/node declares perf_hooks.constants as a namespace of number
|
||||
// constants too. The value form carries the enumerable GC numbers Node's
|
||||
// object walks (the table also holds the GC_MINOR_MARK_SWEEP the
|
||||
// declarations omit), so the shape comes from that table —
|
||||
// perfHooksConstantsValue builds the value from it — in Node's insertion
|
||||
// order.
|
||||
if (
|
||||
psym !== undefined &&
|
||||
psymName === "constants" &&
|
||||
psym.declarations.some(
|
||||
(d) =>
|
||||
ts.isModuleDeclaration(d) &&
|
||||
ctx.isStdlibFile(ts.getSourceFileOfNode(d)) &&
|
||||
isDeclaredInAmbientModule(d, "perf_hooks"),
|
||||
)
|
||||
) {
|
||||
const keys = Object.keys(PERF_HOOKS_CONSTANTS);
|
||||
return {
|
||||
kind: "record",
|
||||
shapeId: ctx.shapes.intern(
|
||||
keys
|
||||
.map((name) => ({ name, type: F64 as IrType }))
|
||||
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)),
|
||||
false,
|
||||
undefined,
|
||||
keys,
|
||||
),
|
||||
};
|
||||
}
|
||||
// net's 'drop' payload: the record the runtime's address object checks
|
||||
// into, each optional field with its declared type.
|
||||
if (isStdlibInterface("DropArgument")) {
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"order": [
|
||||
"<repo>/tests/corpus/perf-hooks-constants.mjs"
|
||||
],
|
||||
"diags": []
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"order": [
|
||||
"<repo>/tests/corpus/perf-hooks-constants.ts"
|
||||
],
|
||||
"diags": []
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// perf_hooks.constants: every name reads Node's value at the access site
|
||||
// (the GC kind and flag numbers @types/node declares, plus the entry-type
|
||||
// and milestone numbers the runtime object also carries), destructuring
|
||||
// reads the same values, and the object itself walks the enumerable GC
|
||||
// numbers in Node's insertion order through Object.keys, spread and
|
||||
// JSON.stringify. The entry-type and milestone members are own but
|
||||
// non-enumerable in Node, so a key walk does not list them. Writing a
|
||||
// member throws in Node (the object's members are read-only), and the
|
||||
// compiled program throws there too; only whether it threw is compared, as
|
||||
// Node's TypeError and the compiler's refusal are different classes.
|
||||
import { constants } from "node:perf_hooks";
|
||||
import * as ph from "node:perf_hooks";
|
||||
import { createRequire } from "node:module";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
console.log(
|
||||
"gc kinds",
|
||||
constants.NODE_PERFORMANCE_GC_MAJOR,
|
||||
constants.NODE_PERFORMANCE_GC_MINOR,
|
||||
constants.NODE_PERFORMANCE_GC_MINOR_MARK_SWEEP,
|
||||
constants.NODE_PERFORMANCE_GC_INCREMENTAL,
|
||||
constants.NODE_PERFORMANCE_GC_WEAKCB,
|
||||
);
|
||||
console.log(
|
||||
"gc flags",
|
||||
constants.NODE_PERFORMANCE_GC_FLAGS_NO,
|
||||
constants.NODE_PERFORMANCE_GC_FLAGS_CONSTRUCT_RETAINED,
|
||||
constants.NODE_PERFORMANCE_GC_FLAGS_FORCED,
|
||||
constants.NODE_PERFORMANCE_GC_FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING,
|
||||
constants.NODE_PERFORMANCE_GC_FLAGS_ALL_AVAILABLE_GARBAGE,
|
||||
constants.NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY,
|
||||
constants.NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE,
|
||||
);
|
||||
console.log(
|
||||
"entry types",
|
||||
constants.NODE_PERFORMANCE_ENTRY_TYPE_GC,
|
||||
constants.NODE_PERFORMANCE_ENTRY_TYPE_HTTP,
|
||||
constants.NODE_PERFORMANCE_ENTRY_TYPE_HTTP2,
|
||||
constants.NODE_PERFORMANCE_ENTRY_TYPE_NET,
|
||||
constants.NODE_PERFORMANCE_ENTRY_TYPE_DNS,
|
||||
constants.NODE_PERFORMANCE_ENTRY_TYPE_QUIC,
|
||||
);
|
||||
console.log(
|
||||
"milestones",
|
||||
constants.NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN_TIMESTAMP,
|
||||
constants.NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN,
|
||||
constants.NODE_PERFORMANCE_MILESTONE_ENVIRONMENT,
|
||||
constants.NODE_PERFORMANCE_MILESTONE_NODE_START,
|
||||
constants.NODE_PERFORMANCE_MILESTONE_V8_START,
|
||||
constants.NODE_PERFORMANCE_MILESTONE_LOOP_START,
|
||||
constants.NODE_PERFORMANCE_MILESTONE_LOOP_EXIT,
|
||||
constants.NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE,
|
||||
);
|
||||
|
||||
const { NODE_PERFORMANCE_GC_MAJOR, NODE_PERFORMANCE_ENTRY_TYPE_DNS } = constants;
|
||||
console.log("destructured", NODE_PERFORMANCE_GC_MAJOR, NODE_PERFORMANCE_ENTRY_TYPE_DNS);
|
||||
|
||||
let threw = false;
|
||||
try {
|
||||
constants.NODE_PERFORMANCE_GC_MAJOR = 1;
|
||||
} catch {
|
||||
threw = true;
|
||||
}
|
||||
console.log("write threw", threw, constants.NODE_PERFORMANCE_GC_MAJOR);
|
||||
|
||||
const held = constants;
|
||||
console.log("keys", JSON.stringify(Object.keys(held)));
|
||||
console.log("spread", JSON.stringify({ ...held }));
|
||||
console.log("json", JSON.stringify(held));
|
||||
console.log("identity", held === constants, typeof constants);
|
||||
console.log("stored read", held.NODE_PERFORMANCE_GC_MINOR, held.NODE_PERFORMANCE_GC_FLAGS_NO);
|
||||
|
||||
const viaNamespace = ph.constants;
|
||||
const viaRequire = require("node:perf_hooks").constants;
|
||||
console.log("namespace", viaNamespace.NODE_PERFORMANCE_GC_MAJOR, ph.constants.NODE_PERFORMANCE_GC_WEAKCB);
|
||||
console.log("require", viaRequire.NODE_PERFORMANCE_GC_FLAGS_FORCED);
|
||||
console.log(
|
||||
"same value",
|
||||
JSON.stringify(viaNamespace) === JSON.stringify(constants),
|
||||
JSON.stringify(viaRequire) === JSON.stringify(constants),
|
||||
);
|
||||
@@ -0,0 +1,21 @@
|
||||
// perf_hooks.constants through the declared types: the GC kind and flag
|
||||
// numbers @types/node names read as their Node v24 values, a destructured
|
||||
// binding reads the same, and the object keeps the declarations' type when
|
||||
// it is held in a variable, so its members still read the same numbers.
|
||||
import { constants } from "node:perf_hooks";
|
||||
import * as ph from "node:perf_hooks";
|
||||
|
||||
const major: number = constants.NODE_PERFORMANCE_GC_MAJOR;
|
||||
const flags: number = constants.NODE_PERFORMANCE_GC_FLAGS_FORCED;
|
||||
const { NODE_PERFORMANCE_GC_MINOR, NODE_PERFORMANCE_GC_WEAKCB } = constants;
|
||||
const held: typeof constants = constants;
|
||||
console.log(
|
||||
"values",
|
||||
major,
|
||||
flags,
|
||||
NODE_PERFORMANCE_GC_MINOR,
|
||||
NODE_PERFORMANCE_GC_WEAKCB,
|
||||
held.NODE_PERFORMANCE_GC_INCREMENTAL,
|
||||
);
|
||||
console.log("namespace", ph.constants.NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE);
|
||||
console.log("keys", JSON.stringify(Object.keys(held)));
|
||||
@@ -0,0 +1 @@
|
||||
{ "plain": 1500 }
|
||||
@@ -0,0 +1 @@
|
||||
{ "plain": 1500 }
|
||||
Reference in New Issue
Block a user