Skip to content

fix(compiler): lower Math.PI and Math.E statically - #254

Open
yowainwright wants to merge 1 commit into
vercel-labs:mainfrom
yowainwright:fix/static-math-constants
Open

fix(compiler): lower Math.PI and Math.E statically#254
yowainwright wants to merge 1 commit into
vercel-labs:mainfrom
yowainwright:fix/static-math-constants

Conversation

@yowainwright

Copy link
Copy Markdown

Description

Updates Math.PI and Math.E to work in default static builds.
Now common math code compiles without adding the dynamic runtime.

result

Before:
- error SC2012: 'Math.PI' runs in the embedded dynamic engine...
- error SC2012: 'Math.E' runs in the embedded dynamic engine...

After:
+ 3.141592653590 2.718281828459 5.859874482049 8.539734222674

Demo

The output of the code below will result in the result above:

node --input-type=module <<'EOF'
import { spawnSync } from "node:child_process";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";

const dir = mkdtempSync(".scriptc-proof-");
const source = join(dir, "math-constants.ts");
const release = process.env.SCRIPTC_RELEASE_SPEC ?? "scriptc@0.0.23";
const run = (args) => spawnSync("mise", ["exec", "node@24.15.0", "--", ...args], { encoding: "utf8" });

try {
  writeFileSync(source, `const pi = Math.PI;
  const e = Math.E;
  console.log(pi.toFixed(12), e.toFixed(12), (pi + e).toFixed(12), (pi * e).toFixed(12));
  `);

  const before = run(["npm", "exec", "--yes", "--package", release, "--", "scriptc", "run", source, "--out", join(dir,
  "before"), "--no-keep-c"]);
  const after = run(["pnpm", "--silent", "scriptc", "run", source, "--out", join(dir, "after"), "--no-keep-c"]);

  console.log("Before:");
  console.log([before.stdout, before.stderr].join("\n").split("\n").filter((line) =>
  line.includes("SC2012")).join("\n"));
  console.log("\nAfter:");
  console.log(after.stdout.trim());
} finally {
  rmSync(dir, { recursive: true, force: true });
}
EOF

Tests

pnpm --filter @scriptc/compiler build
pnpm vitest run tests/harness/surface-manifest.test.ts
pnpm vitest run tests/harness/differential.test.ts --testNamePattern 1538-math-static-scalar.ts
SCRIPTC_SAN=1 pnpm vitest run tests/harness/differential.test.ts --testNamePattern 1538-math-static-scalar.ts
pnpm vitest run tests/harness/diagnostics.test.ts --testNamePattern dynamic-surface

@vercel

vercel Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

@yowainwright is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant