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
13 changes: 12 additions & 1 deletion crates/perry-hir/src/lower/const_fold_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,18 @@ pub(crate) fn try_const_fold_function_construct_kind(
// their non-codegen interpreter fallback. Only the trivial empty-body no-op
// is refused; real literal bodies (`return 42`, the `return this` globalThis
// polyfill) still fold, preserving spec behavior by default.
if body_src.trim().is_empty() && crate::eval_classifier::eval_csp_probe_unavailable() {
//
// The probe passes AT LEAST ONE string argument (`new Function("")`). A
// ZERO-argument `new Function()` is not a codegen request at all — it
// constructs the empty function `anonymous() {}` — so it must NEVER throw,
// even under the default CSP mode (#5835 Intl-ctor test regressed here:
// `new Function()` used purely as a constructable-with-settable-prototype
// scaffold for `Reflect.construct` began throwing). Gate the refusal on
// there actually being an argument.
if !consts.is_empty()
&& body_src.trim().is_empty()
&& crate::eval_classifier::eval_csp_probe_unavailable()
{
return synth_throwing_iife(
ctx,
"throw new TypeError(\"Function: runtime dynamic code generation is \
Expand Down
96 changes: 96 additions & 0 deletions crates/perry/tests/issue_5835_zero_arg_function_ctor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//! #5835 regression: a ZERO-argument `new Function()` must construct the empty
//! function `anonymous() {}`, never the CSP "dynamic code generation is
//! unavailable" throw.
//!
//! The throw exists to make the zod-style codegen capability probe
//! (`new Function("")`, which passes an argument) fail so zod takes its
//! non-codegen interpreter fallback. It was firing for ARGUMENT-LESS
//! `new Function()` too — which is not a codegen request at all, just an empty
//! constructable — and regressed `test_gap_intl_ctor_mechanics_5835` (which
//! uses `new Function()` as a settable-prototype scaffold for
//! `Reflect.construct`). The refusal is now gated on there being ≥1 argument.

use std::path::PathBuf;
use std::process::Command;

fn perry_bin() -> PathBuf {
PathBuf::from(env!("CARGO_BIN_EXE_perry"))
}

fn run(dir: &std::path::Path, src: &str) -> String {
let entry = dir.join("main.ts");
let out = dir.join("main_bin");
std::fs::write(&entry, src).expect("write");
let c = Command::new(perry_bin())
.current_dir(dir)
.arg("compile")
.arg(&entry)
.arg("-o")
.arg(&out)
.arg("--no-cache")
.output()
.expect("compile");
assert!(
c.status.success(),
"compile failed\n{}",
String::from_utf8_lossy(&c.stderr)
);
let r = Command::new(&out).current_dir(dir).output().expect("run");
assert!(
r.status.success(),
"run failed\n{}",
String::from_utf8_lossy(&r.stderr)
);
String::from_utf8_lossy(&r.stdout).into_owned()
}

#[test]
fn zero_arg_new_function_is_an_empty_callable() {
let dir = tempfile::tempdir().expect("tempdir");
let out = run(
dir.path(),
r#"
const f: any = new Function();
f.prototype = {};
console.log(typeof f, (() => { try { f(); return "ok"; } catch { return "threw"; } })(), typeof f.prototype);
"#,
);
assert_eq!(out, "function ok object\n");
}

#[test]
fn zero_arg_function_called_form_is_an_empty_callable() {
let dir = tempfile::tempdir().expect("tempdir");
let out = run(
dir.path(),
"const f: any = Function();\nconsole.log(typeof f);\n",
);
assert_eq!(out, "function\n");
}

/// The argument-bearing codegen probe must still be refused so zod-style JIT
/// feature-detection takes its non-codegen fallback (default CSP behavior).
#[test]
fn empty_string_arg_probe_still_throws() {
let dir = tempfile::tempdir().expect("tempdir");
let out = run(
dir.path(),
r#"
try { new Function(""); console.log("no-throw"); }
catch { console.log("threw"); }
"#,
);
assert_eq!(out, "threw\n");
}

/// A real literal body still folds and runs — the fix must not disturb the
/// spec-preserving `CreateDynamicFunction` path for statically-known sources.
#[test]
fn real_literal_body_still_folds() {
let dir = tempfile::tempdir().expect("tempdir");
let out = run(
dir.path(),
"const f: any = new Function(\"a\", \"b\", \"return a + b\");\nconsole.log(f(2, 3));\n",
);
assert_eq!(out, "5\n");
}
Loading