1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 21:57:35 +00:00

LibJS: Implement void operator

This commit is contained in:
Linus Groh 2020-04-15 17:55:03 +01:00 committed by Andreas Kling
parent beda751d33
commit d30db07048
4 changed files with 34 additions and 1 deletions

View file

@ -391,7 +391,11 @@ Value UnaryExpression::execute(Interpreter& interpreter) const
return js_string(interpreter, "object"); return js_string(interpreter, "object");
case Value::Type::Boolean: case Value::Type::Boolean:
return js_string(interpreter, "boolean"); return js_string(interpreter, "boolean");
default:
ASSERT_NOT_REACHED();
} }
case UnaryOp::Void:
return js_undefined();
} }
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
@ -539,6 +543,9 @@ void UnaryExpression::dump(int indent) const
case UnaryOp::Typeof: case UnaryOp::Typeof:
op_string = "typeof "; op_string = "typeof ";
break; break;
case UnaryOp::Void:
op_string = "void ";
break;
} }
print_indent(indent); print_indent(indent);

View file

@ -395,6 +395,7 @@ enum class UnaryOp {
Plus, Plus,
Minus, Minus,
Typeof, Typeof,
Void,
}; };
class UnaryExpression : public Expression { class UnaryExpression : public Expression {

View file

@ -185,6 +185,7 @@ Associativity Parser::operator_associativity(TokenType type) const
case TokenType::EqualsEqualsEquals: case TokenType::EqualsEqualsEquals:
case TokenType::ExclamationMarkEqualsEquals: case TokenType::ExclamationMarkEqualsEquals:
case TokenType::Typeof: case TokenType::Typeof:
case TokenType::Void:
case TokenType::Ampersand: case TokenType::Ampersand:
case TokenType::Caret: case TokenType::Caret:
case TokenType::Pipe: case TokenType::Pipe:
@ -416,6 +417,9 @@ NonnullRefPtr<Expression> Parser::parse_unary_prefixed_expression()
case TokenType::Typeof: case TokenType::Typeof:
consume(); consume();
return create_ast_node<UnaryExpression>(UnaryOp::Typeof, parse_expression(precedence, associativity)); return create_ast_node<UnaryExpression>(UnaryOp::Typeof, parse_expression(precedence, associativity));
case TokenType::Void:
consume();
return create_ast_node<UnaryExpression>(UnaryOp::Void, parse_expression(precedence, associativity));
default: default:
m_parser_state.m_has_errors = true; m_parser_state.m_has_errors = true;
expected("primary expression (missing switch case)"); expected("primary expression (missing switch case)");
@ -968,7 +972,8 @@ bool Parser::match_unary_prefixed_expression() const
|| type == TokenType::Tilde || type == TokenType::Tilde
|| type == TokenType::Plus || type == TokenType::Plus
|| type == TokenType::Minus || type == TokenType::Minus
|| type == TokenType::Typeof; || type == TokenType::Typeof
|| type == TokenType::Void;
} }
bool Parser::match_secondary_expression() const bool Parser::match_secondary_expression() const

View file

@ -0,0 +1,20 @@
load("test-common.js");
try {
assert(void "" === undefined);
assert(void "foo" === undefined);
assert(void 1 === undefined);
assert(void 42 === undefined);
assert(void true === undefined);
assert(void false === undefined);
assert(void null === undefined);
assert(void undefined === undefined);
assert(void function () {} === undefined);
assert(void (() => {}) === undefined);
assert(void (() => "hello friends")() === undefined);
assert((() => void "hello friends")() === undefined);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}