Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/compiler/src/frontend/lowering/builtins/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as ts from "../../ts7/adapter.js";
import { type Lowerer, own } from "../lowerer.js";
import { lowerCheckedPredicateValue } from "../lower-builtin-values.js";
import { locOf } from "../../program.js";
import { BOOL, F64, type IrExpr, type IrLibFn } from "../../../ir/ir.js";
import { BOOL, F64, STRING, type IrExpr, type IrLibFn } from "../../../ir/ir.js";
import { lowerBuiltinValuePreservingUndefined, lowerOptionalNumberPredicate } from "./arguments.js";
import { tryLowerExpression } from "../expressions/try-lower-expression.js";
import { lowerStaticNumberGlobal } from "../lower-calls.js";
Expand Down Expand Up @@ -88,6 +88,22 @@ export function lowerNumberStaticCall(
const parsed = lowerStaticNumberGlobal(lowerer, member, call, call.arguments, loc);
if (parsed !== null) return parsed;
}
// A checked-dynamic argument (`Number.parseInt(env.FORCE_COLOR, 10)`
// over a JavaScript dictionary read): the parsers' own ToString,
// exactly the global parseFloat's dyn form.
if (probed?.type.kind === "dyn") {
const text: IrExpr = {
kind: "libCall",
fn: "dyn.toStringCoerce",
args: [probed],
type: STRING,
loc,
};
if (member === "parseFloat")
return { kind: "libCall", fn: "num.parseFloat", args: [text], type: F64, loc };
const radix = lowerer.lowerExprExpecting(call.arguments[1]!, F64);
return { kind: "libCall", fn: "num.parseInt", args: [text, radix], type: F64, loc };
}
const argText =
probed !== null
? lowerer.fmt(probed.type)
Expand Down
35 changes: 35 additions & 0 deletions packages/compiler/src/frontend/lowering/builtins/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,41 @@ export function lowerProcessStreamProperty(
* read in source order). Method members referenced without a call are
* rejected specifically. Null for non-process receivers (the chain keeps
* trying other property lowerings). */
/** The value of a DATA member of the process global taken off by
* destructuring (`const { env, argv } = process` in JavaScript, the
* supports-color opening): the same values the spelled `process.env` /
* `process.argv` reads produce, typed by the binding. Null for members
* this does not cover (methods keep the destructuring fence). */
export function processDataMemberValue(
lowerer: Lowerer,
member: string,
binding: ts.Node,
loc: SrcLoc,
): IrExpr | null {
switch (member) {
case "env": {
const mapped = lowerer.mapTypeOf(lowerer.typeOf(binding));
if (mapped?.kind !== "record") return null;
const helper = envSnapshotHelper(lowerer, mapped.shapeId, loc);
return helper === null ? null : { kind: "call", callee: helper, args: [], type: mapped, loc };
}
case "argv":
return { kind: "libCall", fn: "process.argv", args: [], type: arrayOf(STRING), loc };
case "platform":
return { kind: "libCall", fn: "process.platform", args: [], type: STRING, loc };
case "arch":
return { kind: "libCall", fn: "process.arch", args: [], type: STRING, loc };
case "pid":
return { kind: "libCall", fn: "process.pid", args: [], type: F64, loc };
case "execPath":
return { kind: "libCall", fn: "process.execPath", args: [], type: STRING, loc };
case "versions":
return { kind: "libCall", fn: "process.versions", args: [], type: DYN, loc };
default:
return null;
}
}

export function lowerProcessProperty(
lowerer: Lowerer,
expr: ts.PropertyAccessExpression,
Expand Down
38 changes: 38 additions & 0 deletions packages/compiler/src/frontend/lowering/js-dynamic-objects.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { afterAll, expect, test } from "vitest";
import * as ts from "../ts7/adapter.js";
import { closeSourceParser, parseSourceFile } from "../ts7/source-parser-node.js";
import { isValueReturningConstructorClass } from "./js-dynamic-objects.js";

afterAll(closeSourceParser);

function classify(source: string): boolean {
const file = parseSourceFile("classes.js", source, "js");
let decl: ts.ClassDeclaration | undefined;
ts.walkPreorder(file, (node) => {
if (!decl && ts.isClassDeclaration(node)) decl = node;
});
if (!decl) throw new Error("fixture has no class");
return isValueReturningConstructorClass(decl);
}

test("a constructor-only class returning a value is claimed", () => {
expect(classify(`class Chalk { constructor(options) { return chalkFactory(options); } }`)).toBe(
true,
);
expect(classify(`class C { constructor(a) { if (a) return { a }; return [a]; } ; }`)).toBe(true);
expect(classify(`class C { constructor() { const f = () => 1; return { f }; } }`)).toBe(true);
});

test("classes that keep their instance layout are left alone", () => {
// No value return, or only nested functions return.
expect(classify(`class C { constructor() { return; } }`)).toBe(false);
expect(classify(`class C { constructor() { function f() { return 1; } f(); } }`)).toBe(false);
expect(classify(`class C { constructor() { const f = () => { return 1; }; f(); } }`)).toBe(false);
// Other members, heritage, or a constructor that observes `this`.
expect(classify(`class C { constructor() { return {}; } m() {} }`)).toBe(false);
expect(classify(`class C { x = 1; constructor() { return {}; } }`)).toBe(false);
expect(classify(`class C extends B { constructor() { super(); return {}; } }`)).toBe(false);
expect(classify(`class C { constructor() { this.a = 1; return {}; } }`)).toBe(false);
expect(classify(`class C { constructor() { const g = () => this; return { g }; } }`)).toBe(false);
expect(classify(`class C {}`)).toBe(false);
});
Loading
Loading