mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
LibJS: Implement typeof operator
This commit is contained in:
parent
0a71533aff
commit
46a897b59b
Notes:
sideshowbarker
2024-07-19 08:16:15 +09:00
Author: https://github.com/deoxxa Commit: https://github.com/SerenityOS/serenity/commit/46a897b59b6 Pull-request: https://github.com/SerenityOS/serenity/pull/1479
4 changed files with 27 additions and 1 deletions
1
Base/home/anon/js/typeof.js
Normal file
1
Base/home/anon/js/typeof.js
Normal file
|
@ -0,0 +1 @@
|
|||
console.log(typeof undefined, typeof true, typeof 'a', typeof 1, typeof {});
|
|
@ -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);
|
||||
|
|
|
@ -309,6 +309,7 @@ private:
|
|||
enum class UnaryOp {
|
||||
BitwiseNot,
|
||||
Not,
|
||||
Typeof,
|
||||
};
|
||||
|
||||
class UnaryExpression : public Expression {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue