From 2636cac6e4705714305fd13e6ac7b0d93f75e225 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 2 Apr 2020 22:08:14 +0100 Subject: [PATCH] LibJS: Remove UndefinedLiteral, add undefined to global object There is no such thing as a "undefined literal" in JS - undefined is just a property on the global object with a value of undefined. This is pretty similar to NaN. var undefined = "foo"; is a perfectly fine AssignmentExpression :^) --- Libraries/LibJS/AST.cpp | 11 ----------- Libraries/LibJS/AST.h | 13 ------------- Libraries/LibJS/Lexer.cpp | 1 - Libraries/LibJS/Parser.cpp | 4 ---- Libraries/LibJS/Runtime/GlobalObject.cpp | 1 + Libraries/LibJS/Token.h | 1 - 6 files changed, 1 insertion(+), 30 deletions(-) diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index a06dc9b04e..f5fd51e366 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -490,12 +490,6 @@ void BooleanLiteral::dump(int indent) const printf("BooleanLiteral %s\n", m_value ? "true" : "false"); } -void UndefinedLiteral::dump(int indent) const -{ - print_indent(indent); - printf("undefined\n"); -} - void NullLiteral::dump(int indent) const { print_indent(indent); @@ -812,11 +806,6 @@ Value BooleanLiteral::execute(Interpreter&) const return Value(m_value); } -Value UndefinedLiteral::execute(Interpreter&) const -{ - return {}; -} - Value NullLiteral::execute(Interpreter&) const { return js_null(); diff --git a/Libraries/LibJS/AST.h b/Libraries/LibJS/AST.h index 1eef238e00..5bc59aeca8 100644 --- a/Libraries/LibJS/AST.h +++ b/Libraries/LibJS/AST.h @@ -440,19 +440,6 @@ private: virtual const char* class_name() const override { return "NullLiteral"; } }; -class UndefinedLiteral final : public Literal { -public: - explicit UndefinedLiteral() - { - } - - virtual Value execute(Interpreter&) const override; - virtual void dump(int indent) const override; - -private: - virtual const char* class_name() const override { return "UndefinedLiteral"; } -}; - class Identifier final : public Expression { public: explicit Identifier(const FlyString& string) diff --git a/Libraries/LibJS/Lexer.cpp b/Libraries/LibJS/Lexer.cpp index f4ffaa3fba..78ce72c135 100644 --- a/Libraries/LibJS/Lexer.cpp +++ b/Libraries/LibJS/Lexer.cpp @@ -69,7 +69,6 @@ Lexer::Lexer(StringView source) s_keywords.set("true", TokenType::BoolLiteral); s_keywords.set("try", TokenType::Try); s_keywords.set("typeof", TokenType::Typeof); - s_keywords.set("undefined", TokenType::UndefinedLiteral); s_keywords.set("var", TokenType::Var); s_keywords.set("void", TokenType::Void); s_keywords.set("while", TokenType::While); diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index 60102e37ee..31da30916b 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -329,9 +329,6 @@ NonnullRefPtr Parser::parse_primary_expression() case TokenType::NullLiteral: consume(); return create_ast_node(); - case TokenType::UndefinedLiteral: - consume(); - return create_ast_node(); case TokenType::CurlyOpen: return parse_object_expression(); case TokenType::Function: @@ -821,7 +818,6 @@ bool Parser::match_expression() const return type == TokenType::BoolLiteral || type == TokenType::NumericLiteral || type == TokenType::StringLiteral - || type == TokenType::UndefinedLiteral || type == TokenType::NullLiteral || type == TokenType::Identifier || type == TokenType::New diff --git a/Libraries/LibJS/Runtime/GlobalObject.cpp b/Libraries/LibJS/Runtime/GlobalObject.cpp index 39db8c82b4..335001127f 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -21,6 +21,7 @@ GlobalObject::GlobalObject() // FIXME: These are read-only in ES5 put("NaN", js_nan()); put("Infinity", js_infinity()); + put("undefined", js_undefined()); put("console", heap().allocate()); put("Date", heap().allocate()); diff --git a/Libraries/LibJS/Token.h b/Libraries/LibJS/Token.h index 6e81c4fcf1..2291fc1965 100644 --- a/Libraries/LibJS/Token.h +++ b/Libraries/LibJS/Token.h @@ -114,7 +114,6 @@ namespace JS { __ENUMERATE_JS_TOKEN(Tilde) \ __ENUMERATE_JS_TOKEN(Try) \ __ENUMERATE_JS_TOKEN(Typeof) \ - __ENUMERATE_JS_TOKEN(UndefinedLiteral) \ __ENUMERATE_JS_TOKEN(UnsignedShiftRight) \ __ENUMERATE_JS_TOKEN(UnsignedShiftRightEquals) \ __ENUMERATE_JS_TOKEN(UnterminatedStringLiteral) \