mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 03:08:11 +00:00
LibJS: Parse "this" as ThisExpression
This commit is contained in:
parent
110ca6b0b6
commit
f8f65053bd
6 changed files with 37 additions and 17 deletions
|
@ -662,6 +662,16 @@ void Identifier::dump(int indent) const
|
|||
printf("Identifier \"%s\"\n", m_string.characters());
|
||||
}
|
||||
|
||||
Value ThisExpression::execute(Interpreter& interpreter) const
|
||||
{
|
||||
return interpreter.this_value();
|
||||
}
|
||||
|
||||
void ThisExpression::dump(int indent) const
|
||||
{
|
||||
ASTNode::dump(indent);
|
||||
}
|
||||
|
||||
Value AssignmentExpression::execute(Interpreter& interpreter) const
|
||||
{
|
||||
auto rhs_result = m_rhs->execute(interpreter);
|
||||
|
|
|
@ -502,6 +502,15 @@ private:
|
|||
FlyString m_string;
|
||||
};
|
||||
|
||||
class ThisExpression final : public Expression {
|
||||
public:
|
||||
virtual Value execute(Interpreter&) const override;
|
||||
virtual void dump(int indent) const override;
|
||||
|
||||
private:
|
||||
virtual const char* class_name() const override { return "ThisExpression"; }
|
||||
};
|
||||
|
||||
class CallExpression : public Expression {
|
||||
public:
|
||||
CallExpression(NonnullRefPtr<Expression> callee, NonnullRefPtrVector<Expression> arguments = {})
|
||||
|
|
|
@ -158,10 +158,6 @@ void Interpreter::set_variable(const FlyString& name, Value value, bool first_as
|
|||
|
||||
Optional<Value> Interpreter::get_variable(const FlyString& name)
|
||||
{
|
||||
static FlyString this_name = "this";
|
||||
if (name == this_name)
|
||||
return this_value();
|
||||
|
||||
for (ssize_t i = m_scope_stack.size() - 1; i >= 0; --i) {
|
||||
auto& scope = m_scope_stack.at(i);
|
||||
auto value = scope.variables.get(name);
|
||||
|
|
|
@ -66,6 +66,7 @@ Lexer::Lexer(StringView source)
|
|||
s_keywords.set("null", TokenType::NullLiteral);
|
||||
s_keywords.set("return", TokenType::Return);
|
||||
s_keywords.set("switch", TokenType::Switch);
|
||||
s_keywords.set("this", TokenType::This);
|
||||
s_keywords.set("throw", TokenType::Throw);
|
||||
s_keywords.set("true", TokenType::BoolLiteral);
|
||||
s_keywords.set("try", TokenType::Try);
|
||||
|
|
|
@ -317,6 +317,9 @@ NonnullRefPtr<Expression> Parser::parse_primary_expression()
|
|||
consume(TokenType::ParenClose);
|
||||
return expression;
|
||||
}
|
||||
case TokenType::This:
|
||||
consume();
|
||||
return create_ast_node<ThisExpression>();
|
||||
case TokenType::Identifier: {
|
||||
auto arrow_function_result = try_parse_arrow_function_expression(false);
|
||||
if (!arrow_function_result.is_null()) {
|
||||
|
@ -407,7 +410,6 @@ NonnullRefPtr<ObjectExpression> Parser::parse_object_expression()
|
|||
continue;
|
||||
}
|
||||
|
||||
|
||||
if (match(TokenType::Colon)) {
|
||||
consume(TokenType::Colon);
|
||||
properties.set(property_name, parse_expression(0));
|
||||
|
@ -905,6 +907,7 @@ bool Parser::match_expression() const
|
|||
|| type == TokenType::BracketOpen
|
||||
|| type == TokenType::ParenOpen
|
||||
|| type == TokenType::Function
|
||||
|| type == TokenType::This
|
||||
|| match_unary_prefixed_expression();
|
||||
}
|
||||
|
||||
|
|
|
@ -111,6 +111,7 @@ namespace JS {
|
|||
__ENUMERATE_JS_TOKEN(SlashEquals) \
|
||||
__ENUMERATE_JS_TOKEN(StringLiteral) \
|
||||
__ENUMERATE_JS_TOKEN(Switch) \
|
||||
__ENUMERATE_JS_TOKEN(This) \
|
||||
__ENUMERATE_JS_TOKEN(Throw) \
|
||||
__ENUMERATE_JS_TOKEN(Tilde) \
|
||||
__ENUMERATE_JS_TOKEN(Try) \
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue