diff --git a/crates/js/src/es2015_tests.rs b/crates/js/src/es2015_tests.rs index f134176bfc..28fb8c3408 100644 --- a/crates/js/src/es2015_tests.rs +++ b/crates/js/src/es2015_tests.rs @@ -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();";