Browse Source

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 :^)
Linus Groh 5 năm trước cách đây
mục cha
commit
2636cac6e4

+ 0 - 11
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();

+ 0 - 13
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)

+ 0 - 1
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);

+ 0 - 4
Libraries/LibJS/Parser.cpp

@@ -329,9 +329,6 @@ NonnullRefPtr<Expression> Parser::parse_primary_expression()
     case TokenType::NullLiteral:
         consume();
         return create_ast_node<NullLiteral>();
-    case TokenType::UndefinedLiteral:
-        consume();
-        return create_ast_node<UndefinedLiteral>();
     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

+ 1 - 0
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<ConsoleObject>());
     put("Date", heap().allocate<DateConstructor>());

+ 0 - 1
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)   \