summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorConrad Pankoff <deoxxa@fknsrs.biz>2020-03-18 06:33:32 +1100
committerAndreas Kling <kling@serenityos.org>2020-03-17 21:28:02 +0100
commit46a897b59b600703effc02fc07e31323c8967d5f (patch)
tree68b2241393220e1d2c25a6f8e0f2f407db4dc2cf /Libraries
parent0a71533afff8ccef5801cdbfc9c424c3a6f0330a (diff)
downloadserenity-46a897b59b600703effc02fc07e31323c8967d5f.zip
LibJS: Implement typeof operator
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/AST.cpp20
-rw-r--r--Libraries/LibJS/AST.h1
-rw-r--r--Libraries/LibJS/Parser.cpp6
3 files changed, 26 insertions, 1 deletions
diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp
index d1e9d53828..a815a6715a 100644
--- a/Libraries/LibJS/AST.cpp
+++ b/Libraries/LibJS/AST.cpp
@@ -199,6 +199,23 @@ Value UnaryExpression::execute(Interpreter& interpreter) const
return bitwise_not(lhs_result);
case UnaryOp::Not:
return Value(!lhs_result.to_boolean());
+ case UnaryOp::Typeof:
+ switch (lhs_result.type()) {
+ case Value::Type::Undefined:
+ return js_string(interpreter.heap(), "undefined");
+ case Value::Type::Null:
+ // yes, this is on purpose. yes, this is how javascript works.
+ // yes, it's silly.
+ return js_string(interpreter.heap(), "object");
+ case Value::Type::Number:
+ return js_string(interpreter.heap(), "number");
+ case Value::Type::String:
+ return js_string(interpreter.heap(), "string");
+ case Value::Type::Object:
+ return js_string(interpreter.heap(), "object");
+ case Value::Type::Boolean:
+ return js_string(interpreter.heap(), "boolean");
+ }
}
ASSERT_NOT_REACHED();
@@ -318,6 +335,9 @@ void UnaryExpression::dump(int indent) const
case UnaryOp::Not:
op_string = "!";
break;
+ case UnaryOp::Typeof:
+ op_string = "typeof ";
+ break;
}
print_indent(indent);
diff --git a/Libraries/LibJS/AST.h b/Libraries/LibJS/AST.h
index 2cf5ed32bd..b71ed57732 100644
--- a/Libraries/LibJS/AST.h
+++ b/Libraries/LibJS/AST.h
@@ -309,6 +309,7 @@ private:
enum class UnaryOp {
BitwiseNot,
Not,
+ Typeof,
};
class UnaryExpression : public Expression {
diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp
index 754b251caa..dbc307ba4e 100644
--- a/Libraries/LibJS/Parser.cpp
+++ b/Libraries/LibJS/Parser.cpp
@@ -254,6 +254,9 @@ NonnullOwnPtr<Expression> Parser::parse_unary_prefixed_expression()
case TokenType::Tilde:
consume();
return make<UnaryExpression>(UnaryOp::BitwiseNot, parse_primary_expression());
+ case TokenType::Typeof:
+ consume();
+ return make<UnaryExpression>(UnaryOp::Typeof, parse_primary_expression());
default:
m_has_errors = true;
expected("primary expression (missing switch case)");
@@ -532,7 +535,8 @@ bool Parser::match_unary_prefixed_expression() const
return type == TokenType::PlusPlus
|| type == TokenType::MinusMinus
|| type == TokenType::ExclamationMark
- || type == TokenType::Tilde;
+ || type == TokenType::Tilde
+ || type == TokenType::Typeof;
}
bool Parser::match_secondary_expression() const