1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 05:05:08 +00:00

LibJS: Add function default arguments

Adds the ability for function arguments to have default values. This
works for standard functions as well as arrow functions. Default values
are not printed in a <function>.toString() call, as nodes cannot print
their source string representation.
This commit is contained in:
Matthew Olsson 2020-05-02 11:46:39 -07:00 committed by Andreas Kling
parent 751f813f6a
commit 5e66f1900b
8 changed files with 184 additions and 35 deletions

View file

@ -26,19 +26,20 @@
#pragma once
#include <LibJS/AST.h>
#include <LibJS/Runtime/Function.h>
namespace JS {
class ScriptFunction final : public Function {
public:
static ScriptFunction* create(GlobalObject&, const FlyString& name, const Statement& body, Vector<FlyString> parameters, LexicalEnvironment* parent_environment);
static ScriptFunction* create(GlobalObject&, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, LexicalEnvironment* parent_environment);
ScriptFunction(const FlyString& name, const Statement& body, Vector<FlyString> parameters, LexicalEnvironment* parent_environment, Object& prototype);
ScriptFunction(const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, LexicalEnvironment* parent_environment, Object& prototype);
virtual ~ScriptFunction();
const Statement& body() const { return m_body; }
const Vector<FlyString>& parameters() const { return m_parameters; };
const Vector<FunctionNode::Parameter>& parameters() const { return m_parameters; };
virtual Value call(Interpreter&) override;
virtual Value construct(Interpreter&) override;
@ -56,7 +57,7 @@ private:
FlyString m_name;
NonnullRefPtr<Statement> m_body;
const Vector<FlyString> m_parameters;
const Vector<FunctionNode::Parameter> m_parameters;
LexicalEnvironment* m_parent_environment { nullptr };
};