Skip to content

Commit

Permalink
LibJS: Implement (no-op) debugger statement
Browse files Browse the repository at this point in the history
  • Loading branch information
linusg authored and awesomekling committed May 1, 2020
1 parent ea83986 commit 43c1fa9
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions Libraries/LibGUI/JSSyntaxHighlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ static TextStyle style_for_token_type(Gfx::Palette palette, JS::TokenType type)
case JS::TokenType::Class:
case JS::TokenType::Const:
case JS::TokenType::Delete:
case JS::TokenType::Debugger:
case JS::TokenType::Function:
case JS::TokenType::In:
case JS::TokenType::Instanceof:
Expand Down
6 changes: 6 additions & 0 deletions Libraries/LibJS/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,12 @@ Value SequenceExpression::execute(Interpreter& interpreter) const
return last_value;
}

Value DebuggerStatement::execute(Interpreter&) const
{
dbg() << "Sorry, no JavaScript debugger available (yet)!";
return js_undefined();
}

void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
{
m_variables.append(move(variables));
Expand Down
10 changes: 10 additions & 0 deletions Libraries/LibJS/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -923,4 +923,14 @@ class ContinueStatement final : public Statement {
virtual const char* class_name() const override { return "ContinueStatement"; }
};

class DebuggerStatement final : public Statement {
public:
DebuggerStatement() {}

virtual Value execute(Interpreter&) const override;

private:
virtual const char* class_name() const override { return "DebuggerStatement"; }
};

}
1 change: 1 addition & 0 deletions Libraries/LibJS/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Lexer::Lexer(StringView source)
s_keywords.set("class", TokenType::Class);
s_keywords.set("const", TokenType::Const);
s_keywords.set("continue", TokenType::Continue);
s_keywords.set("debugger", TokenType::Debugger);
s_keywords.set("default", TokenType::Default);
s_keywords.set("delete", TokenType::Delete);
s_keywords.set("do", TokenType::Do);
Expand Down
12 changes: 11 additions & 1 deletion Libraries/LibJS/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ NonnullRefPtr<Statement> Parser::parse_statement()
return parse_do_while_statement();
case TokenType::While:
return parse_while_statement();
case TokenType::Debugger:
return parse_debugger_statement();
default:
if (match_expression()) {
auto expr = parse_expression(0);
Expand Down Expand Up @@ -1044,6 +1046,13 @@ NonnullRefPtr<ForStatement> Parser::parse_for_statement()
return create_ast_node<ForStatement>(move(init), move(test), move(update), move(body));
}

NonnullRefPtr<DebuggerStatement> Parser::parse_debugger_statement()
{
consume(TokenType::Debugger);
consume_or_insert_semicolon();
return create_ast_node<DebuggerStatement>();
}

bool Parser::match(TokenType type) const
{
return m_parser_state.m_current_token.type() == type;
Expand Down Expand Up @@ -1156,7 +1165,8 @@ bool Parser::match_statement() const
|| type == TokenType::Switch
|| type == TokenType::Break
|| type == TokenType::Continue
|| type == TokenType::Var;
|| type == TokenType::Var
|| type == TokenType::Debugger;
}

bool Parser::match_identifier_name() const
Expand Down
1 change: 1 addition & 0 deletions Libraries/LibJS/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Parser {
NonnullRefPtr<ContinueStatement> parse_continue_statement();
NonnullRefPtr<DoWhileStatement> parse_do_while_statement();
NonnullRefPtr<WhileStatement> parse_while_statement();
NonnullRefPtr<DebuggerStatement> parse_debugger_statement();
NonnullRefPtr<ConditionalExpression> parse_conditional_expression(NonnullRefPtr<Expression> test);

NonnullRefPtr<Expression> parse_expression(int min_precedence, Associativity associate = Associativity::Right);
Expand Down
9 changes: 9 additions & 0 deletions Libraries/LibJS/Tests/debugger-statement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("test-common.js");

try {
debugger;

console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
1 change: 1 addition & 0 deletions Libraries/LibJS/Token.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace JS {
__ENUMERATE_JS_TOKEN(Continue) \
__ENUMERATE_JS_TOKEN(CurlyClose) \
__ENUMERATE_JS_TOKEN(CurlyOpen) \
__ENUMERATE_JS_TOKEN(Debugger) \
__ENUMERATE_JS_TOKEN(Default) \
__ENUMERATE_JS_TOKEN(Delete) \
__ENUMERATE_JS_TOKEN(Do) \
Expand Down
1 change: 1 addition & 0 deletions Userland/js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ int main(int argc, char** argv)
break;
case JS::TokenType::Class:
case JS::TokenType::Const:
case JS::TokenType::Debugger:
case JS::TokenType::Delete:
case JS::TokenType::Function:
case JS::TokenType::In:
Expand Down

0 comments on commit 43c1fa9

Please sign in to comment.