LibJS: Parse FunctionExpressions

FunctionExpression is mostly like FunctionDeclaration, except the name
is optional. Share the parsing logic in parse_function_node<NodeType>.

This allows us to do nice things like:

    document.addEventListener("DOMContentLoaded", function() {
        alert("Hello friends!");
    });
This commit is contained in:
Andreas Kling 2020-03-19 11:52:56 +01:00
commit 07679e347c
Notes: sideshowbarker 2024-07-19 08:14:55 +09:00
3 changed files with 21 additions and 5 deletions

View file

@ -154,6 +154,8 @@ class FunctionDeclaration final
: public Statement
, public FunctionNode {
public:
static bool must_have_name() { return true; }
FunctionDeclaration(String name, NonnullRefPtr<ScopeNode> body, Vector<String> parameters = {})
: FunctionNode(move(name), move(body), move(parameters))
{
@ -169,6 +171,8 @@ private:
class FunctionExpression final : public Expression
, public FunctionNode {
public:
static bool must_have_name() { return false; }
FunctionExpression(String name, NonnullRefPtr<ScopeNode> body, Vector<String> parameters = {})
: FunctionNode(move(name), move(body), move(parameters))
{