diff options
author | Jack Karamanian <karamanian.jack@gmail.com> | 2020-06-08 13:31:21 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-06-29 17:54:54 +0200 |
commit | 7533fd8b025937b2a9ab834a6bbee2be7309de8b (patch) | |
tree | c6f2a77266260b539468a3b0f9d6d733857dc74c /Libraries/LibJS/AST.h | |
parent | a535d58cac6cfef7c85cd9ee8419189d58560afb (diff) | |
download | serenity-7533fd8b025937b2a9ab834a6bbee2be7309de8b.zip |
LibJS: Initial class implementation; allow super expressions in object
literal methods; add EnvrionmentRecord fields and methods to
LexicalEnvironment
Adding EnvrionmentRecord's fields and methods lets us throw an exception
when |this| is not initialized, which occurs when the super constructor
in a derived class has not yet been called, or when |this| has already
been initialized (the super constructor was already called).
Diffstat (limited to 'Libraries/LibJS/AST.h')
-rw-r--r-- | Libraries/LibJS/AST.h | 89 |
1 files changed, 88 insertions, 1 deletions
diff --git a/Libraries/LibJS/AST.h b/Libraries/LibJS/AST.h index d6084121f1..2e1cdbebd5 100644 --- a/Libraries/LibJS/AST.h +++ b/Libraries/LibJS/AST.h @@ -63,6 +63,7 @@ public: virtual bool is_variable_declaration() const { return false; } virtual bool is_call_expression() const { return false; } virtual bool is_new_expression() const { return false; } + virtual bool is_super_expression() const { return false; } protected: ASTNode() { } @@ -581,6 +582,8 @@ public: { } + StringView value() const { return m_value; } + virtual Value execute(Interpreter&, GlobalObject&) const override; virtual void dump(int indent) const override; @@ -642,6 +645,87 @@ private: FlyString m_string; }; +class ClassMethod final : public ASTNode { +public: + enum class Kind { + Method, + Getter, + Setter, + }; + + ClassMethod(NonnullRefPtr<Expression> key, NonnullRefPtr<FunctionExpression> function, Kind kind, bool is_static) + : m_key(move(key)) + , m_function(move(function)) + , m_kind(kind) + , m_is_static(is_static) + { + } + + const Expression& key() const { return *m_key; } + Kind kind() const { return m_kind; } + bool is_static() const { return m_is_static; } + + virtual Value execute(Interpreter&, GlobalObject&) const override; + virtual void dump(int indent) const override; + +private: + virtual const char* class_name() const override { return "ClassMethod"; } + + NonnullRefPtr<Expression> m_key; + NonnullRefPtr<FunctionExpression> m_function; + Kind m_kind; + bool m_is_static; +}; + +class SuperExpression final : public Expression { +public: + virtual Value execute(Interpreter&, GlobalObject&) const override; + virtual void dump(int indent) const override; + +private: + virtual bool is_super_expression() const override { return true; } + virtual const char* class_name() const override { return "SuperExpression"; } +}; + +class ClassExpression final : public Expression { +public: + ClassExpression(String name, RefPtr<FunctionExpression> constructor, RefPtr<Expression> super_class, NonnullRefPtrVector<ClassMethod> methods) + : m_name(move(name)) + , m_constructor(move(constructor)) + , m_super_class(move(super_class)) + , m_methods(move(methods)) + { + } + + StringView name() const { return m_name; } + + virtual Value execute(Interpreter&, GlobalObject&) const override; + virtual void dump(int indent) const override; + +private: + virtual const char* class_name() const override { return "ClassExpression"; } + + String m_name; + RefPtr<FunctionExpression> m_constructor; + RefPtr<Expression> m_super_class; + NonnullRefPtrVector<ClassMethod> m_methods; +}; + +class ClassDeclaration final : public Declaration { +public: + ClassDeclaration(NonnullRefPtr<ClassExpression> class_expression) + : m_class_expression(move(class_expression)) + { + } + + virtual Value execute(Interpreter&, GlobalObject&) const override; + virtual void dump(int indent) const override; + +private: + virtual const char* class_name() const override { return "ClassDeclaration"; } + NonnullRefPtr<ClassExpression> m_class_expression; +}; + class SpreadExpression final : public Expression { public: explicit SpreadExpression(NonnullRefPtr<Expression> target) @@ -836,10 +920,11 @@ public: Spread, }; - ObjectProperty(NonnullRefPtr<Expression> key, RefPtr<Expression> value, Type property_type) + ObjectProperty(NonnullRefPtr<Expression> key, RefPtr<Expression> value, Type property_type, bool is_method) : m_key(move(key)) , m_value(move(value)) , m_property_type(property_type) + , m_is_method(is_method) { } @@ -851,6 +936,7 @@ public: } Type type() const { return m_property_type; } + bool is_method() const { return m_is_method; } virtual void dump(int indent) const override; virtual Value execute(Interpreter&, GlobalObject&) const override; @@ -861,6 +947,7 @@ private: NonnullRefPtr<Expression> m_key; RefPtr<Expression> m_value; Type m_property_type; + bool m_is_method { false }; }; class ObjectExpression : public Expression { |