Scripts: test that eval code with a forbidden super or new.target runs none of it

The tests from #10023 for `super` and `new.target` in global eval,
indirect eval and `Function` bodies check only that a SyntaxError is
thrown. The interpreter also throws one at run time when `super` has no
home object or constructor (class_ops.rs:450 and :555), so a case the
parser let through would still pass them if it threw only at run time.

Two tests in es2015_tests.rs put `x = 1` ahead of the forbidden form:
- eval_code_with_a_forbidden_super_or_new_target_runs_none_of_it covers
  13 cases of `super.y`, `super(...)` and `new.target` in global eval,
  indirect eval, direct eval in a plain function, and direct eval in a
  method or constructor that does not allow the form. Each leaves `x`
  at 0 and throws a SyntaxError.
- a_function_body_with_super_is_not_made covers 5 `Function` calls with
  `super` in the body or a parameter default. Each throws a SyntaxError
  from `Function` itself, so no function is returned and `x` stays 0.

Every case was already rejected by the parser; the tests change no
behavior.
This commit is contained in:
dotCooCoo committed 2026-11-29 17:41:06 -08:00
1 parent cdc14eb007
commit 26a70bb546
1 file changed
+55
+55
View File
@@ -1714,6 +1714,61 @@ fn a_function_body_reads_its_own_new_target() {
truth("class A { m() { return Function('return new.target')(); } } new A().m() === undefined");
}
/// Eval code that uses `super` or `new.target` where it may not is a `SyntaxError` before any of
/// it runs, so `x = 1` ahead of the forbidden form leaves `x` at 0 (ECMA-262 2015 sections
/// 18.2.1.1 and 15.1.1).
#[test]
fn eval_code_with_a_forbidden_super_or_new_target_runs_none_of_it() {
let a = "class A { constructor(v) { this.a = v; } } ";
let calls = [
"eval('x = 1; super.y');".to_string(),
"class C { m() { (0, eval)('x = 1; super.y'); } } new C().m();".to_string(),
"function F() { eval('x = 1; super.y'); } F();".to_string(),
"class C { m() { (function () { eval('x = 1; super.y'); })(); } } new C().m();".to_string(),
"eval('x = 1; super()');".to_string(),
"class C { constructor() { eval('x = 1; super()'); } } new C();".to_string(),
format!("{a}class B extends A {{ m() {{ eval('x = 1; super(1)'); }} }} new B(1).m();"),
format!("{a}class B extends A {{ constructor() {{ (0, eval)('x = 1; super(1)'); }} }} new B();"),
format!("{a}class B extends A {{ constructor() {{ (function () {{ eval('x = 1; super(1)'); }})(); }} }} new B();"),
"function F() { eval('x = 1; super()'); } F();".to_string(),
"eval('x = 1; new.target');".to_string(),
"(() => eval('x = 1; new.target'))();".to_string(),
"function F() { (0, eval)('x = 1; new.target'); } new F();".to_string(),
];
let ran: Vec<&String> = calls
.iter()
.filter(|call| {
let src = format!("var x = 0, e; try {{ {call} }} catch (err) {{ e = err; }} x === 0 && e instanceof SyntaxError");
run(&src) != JsValue::Bool(true)
})
.collect();
assert!(ran.is_empty(), "eval code ran or did not throw a SyntaxError: {ran:#?}");
}
/// The `Function` constructor throws the `SyntaxError` for `super` in the body or a parameter
/// itself, so it returns no function to call and `x = 1` in the body never runs (ECMA-262 2015
/// section 19.2.1.1.1).
#[test]
fn a_function_body_with_super_is_not_made() {
let a = "class A { constructor(v) { this.a = v; } } ";
let calls = [
"class C { m() { f = Function('x = 1; super.y'); } } new C().m();".to_string(),
"class C { m() { f = Function('a = super.y', 'x = 1'); } } new C().m();".to_string(),
"f = Function('x = 1; super.y');".to_string(),
format!("{a}class B extends A {{ constructor() {{ super(1); f = Function('x = 1; super(1)'); }} }} new B();"),
"f = Function('x = 1; super()');".to_string(),
];
let made: Vec<&String> = calls
.iter()
.filter(|call| {
let src =
format!("var x = 0, f, e; try {{ {call} f(); }} catch (err) {{ e = err; }} f === undefined && x === 0 && e instanceof SyntaxError");
run(&src) != JsValue::Bool(true)
})
.collect();
assert!(made.is_empty(), "Function made a function or did not throw a SyntaxError: {made:#?}");
}
#[test]
fn deep_class_chains_stay_within_the_limits() {
let src = "var C = class {}; for (var i = 0; i < 2000; i++) { C = class extends C {}; } new C();";