From 0e01a74f59c448b6b04810feefa49f50cd9d2b58 Mon Sep 17 00:00:00 2001 From: raskad <32105367+raskad@users.noreply.github.com> Date: Tue, 21 Mar 2023 18:43:38 +0000 Subject: [PATCH] Add early errors to dynamic function constructors (#2716) This Pull Request fixes #2673. It changes the following: - Add early errors to dynamic function constructors. - Add tests that check for syntax errors when `super` is passed to the function constructor. --- boa_engine/src/builtins/function/mod.rs | 14 ++++++++++++++ boa_engine/src/builtins/function/tests.rs | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/boa_engine/src/builtins/function/mod.rs b/boa_engine/src/builtins/function/mod.rs index 31fc830333..3299939550 100644 --- a/boa_engine/src/builtins/function/mod.rs +++ b/boa_engine/src/builtins/function/mod.rs @@ -584,6 +584,20 @@ impl BuiltInFunctionObject { .into()); } + // It is a Syntax Error if FunctionBody Contains SuperProperty is true. + if contains(&body, ContainsSymbol::SuperProperty) { + return Err(JsNativeError::syntax() + .with_message("invalid `super` reference") + .into()); + } + + // It is a Syntax Error if FunctionBody Contains SuperCall is true. + if contains(&body, ContainsSymbol::SuperCall) { + return Err(JsNativeError::syntax() + .with_message("invalid `super` call") + .into()); + } + // It is a Syntax Error if any element of the BoundNames of FormalParameters // also occurs in the LexicallyDeclaredNames of FunctionBody. // https://tc39.es/ecma262/#sec-function-definitions-static-semantics-early-errors diff --git a/boa_engine/src/builtins/function/tests.rs b/boa_engine/src/builtins/function/tests.rs index 15c2a7ed3f..f7a5cf10eb 100644 --- a/boa_engine/src/builtins/function/tests.rs +++ b/boa_engine/src/builtins/function/tests.rs @@ -175,3 +175,19 @@ fn closure_capture_clone() { TestAction::assert_eq("closure()", "Hello world!"), ]); } + +#[test] +fn function_constructor_early_errors_super() { + run_test_actions([ + TestAction::assert_native_error( + "Function('super()')()", + ErrorKind::Syntax, + "invalid `super` call", + ), + TestAction::assert_native_error( + "Function('super.a')()", + ErrorKind::Syntax, + "invalid `super` reference", + ), + ]); +}