Skip to content

Commit

Permalink
Add early errors to dynamic function constructors (#2716)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
raskad committed Mar 21, 2023
1 parent cb4e49a commit 0e01a74
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions boa_engine/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions boa_engine/src/builtins/function/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
]);
}

0 comments on commit 0e01a74

Please sign in to comment.