Commit 603acc3f authored by rmcilroy's avatar rmcilroy Committed by Commit bot

[Interpreter] Ensure that block breaks are within the correct context scope.

Fixes a bug where the context would be popped before labeled block break target
location.

BUG=v8:4280
LOG=N

Review URL: https://codereview.chromium.org/1601153002

Cr-Commit-Position: refs/heads/master@{#33388}
parent e68ffc76
...@@ -422,24 +422,24 @@ void BytecodeGenerator::MakeBytecodeBody() { ...@@ -422,24 +422,24 @@ void BytecodeGenerator::MakeBytecodeBody() {
void BytecodeGenerator::VisitBlock(Block* stmt) { void BytecodeGenerator::VisitBlock(Block* stmt) {
BlockBuilder block_builder(this->builder()); // Visit declarations and statements.
ControlScopeForBreakable execution_control(this, stmt, &block_builder); if (stmt->scope() != nullptr && stmt->scope()->NeedsContext()) {
VisitNewLocalBlockContext(stmt->scope());
if (stmt->scope() == NULL) { ContextScope scope(this, stmt->scope());
// Visit statements in the same scope, no declarations. VisitBlockDeclarationsAndStatements(stmt);
VisitStatements(stmt->statements());
} else { } else {
// Visit declarations and statements in a block scope. VisitBlockDeclarationsAndStatements(stmt);
if (stmt->scope()->NeedsContext()) { }
VisitNewLocalBlockContext(stmt->scope()); }
ContextScope scope(this, stmt->scope());
VisitDeclarations(stmt->scope()->declarations());
VisitStatements(stmt->statements()); void BytecodeGenerator::VisitBlockDeclarationsAndStatements(Block* stmt) {
} else { BlockBuilder block_builder(builder());
VisitDeclarations(stmt->scope()->declarations()); ControlScopeForBreakable execution_control(this, stmt, &block_builder);
VisitStatements(stmt->statements()); if (stmt->scope() != nullptr) {
} VisitDeclarations(stmt->scope()->declarations());
} }
VisitStatements(stmt->statements());
if (stmt->labels() != nullptr) block_builder.EndBlock(); if (stmt->labels() != nullptr) block_builder.EndBlock();
} }
......
...@@ -79,6 +79,7 @@ class BytecodeGenerator final : public AstVisitor { ...@@ -79,6 +79,7 @@ class BytecodeGenerator final : public AstVisitor {
void VisitNewTargetVariable(Variable* variable); void VisitNewTargetVariable(Variable* variable);
void VisitNewLocalFunctionContext(); void VisitNewLocalFunctionContext();
void VisitBuildLocalActivationContext(); void VisitBuildLocalActivationContext();
void VisitBlockDeclarationsAndStatements(Block* stmt);
void VisitNewLocalBlockContext(Scope* scope); void VisitNewLocalBlockContext(Scope* scope);
void VisitFunctionClosureForContext(); void VisitFunctionClosureForContext();
void VisitSetHomeObject(Register value, Register home_object, void VisitSetHomeObject(Register value, Register home_object,
......
...@@ -2188,7 +2188,10 @@ TEST(BreakableBlocks) { ...@@ -2188,7 +2188,10 @@ TEST(BreakableBlocks) {
InitializedHandleScope handle_scope; InitializedHandleScope handle_scope;
BytecodeGeneratorHelper helper; BytecodeGeneratorHelper helper;
ExpectedSnippet<int> snippets[] = { int closure = Register::function_closure().index();
int context = Register::function_context().index();
ExpectedSnippet<InstanceType> snippets[] = {
{"var x = 0;\n" {"var x = 0;\n"
"label: {\n" "label: {\n"
" x = x + 1;\n" " x = x + 1;\n"
...@@ -2266,6 +2269,37 @@ TEST(BreakableBlocks) { ...@@ -2266,6 +2269,37 @@ TEST(BreakableBlocks) {
B(Ldar), R(0), // B(Ldar), R(0), //
B(Return), // B(Return), //
}}, }},
{"outer: {\n"
" let y = 10;"
" function f() { return y; }\n"
" break outer;\n"
"}\n",
5 * kPointerSize,
1,
39,
{
B(LdaConstant), U8(0), //
B(Star), R(3), //
B(Ldar), R(closure), //
B(Star), R(4), //
B(CallRuntime), U16(Runtime::kPushBlockContext), R(3), U8(2), //
B(PushContext), R(2), //
B(LdaTheHole), //
B(StaContextSlot), R(2), U8(4), //
B(CreateClosure), U8(1), U8(0), //
B(Star), R(0), //
B(LdaSmi8), U8(10), //
B(StaContextSlot), R(2), U8(4), //
B(Ldar), R(0), //
B(Star), R(1), //
B(Jump), U8(2), //
B(PopContext), R(context), //
B(LdaUndefined), //
B(Return), //
},
2,
{InstanceType::FIXED_ARRAY_TYPE,
InstanceType::SHARED_FUNCTION_INFO_TYPE}},
}; };
for (size_t i = 0; i < arraysize(snippets); i++) { for (size_t i = 0; i < arraysize(snippets); i++) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment