diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index 4528d4a4bd..d8af41531c 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -126,7 +126,7 @@ Value ForStatement::execute(Interpreter& interpreter) const { RefPtr wrapper; - if (m_init->is_variable_declaration() && static_cast(m_init.ptr())->declaration_type() != DeclarationType::Var) { + if (m_init && m_init->is_variable_declaration() && static_cast(m_init.ptr())->declaration_type() != DeclarationType::Var) { wrapper = create_ast_node(); interpreter.enter_scope(*wrapper, {}, ScopeType::Block); } diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index 379b8a1038..ef0791622b 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -612,7 +612,7 @@ NonnullRefPtr Parser::parse_for_statement() RefPtr update; switch (m_current_token.type()) { - case TokenType::Semicolon: + case TokenType::ParenClose: break; default: update = parse_expression(0); diff --git a/Libraries/LibJS/Tests/for-basic.js b/Libraries/LibJS/Tests/for-basic.js new file mode 100644 index 0000000000..b7fcb5b1e6 --- /dev/null +++ b/Libraries/LibJS/Tests/for-basic.js @@ -0,0 +1,24 @@ +function assert(x) { if (!x) throw 1; } + +try { + var a = []; + for (var i = 0; i < 3; ++i) { + a.push(i); + } + assert(a.length === 3); + assert(a[0] === 0); + assert(a[1] === 1); + assert(a[2] === 2); + + for (; a.length < 6;) { + a.push('x'); + } + assert(a.length === 6); + assert(a[3] === 'x'); + assert(a[4] === 'x'); + assert(a[5] === 'x'); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +}