Index: src/parsing/parameter-initializer-rewriter.cc |
diff --git a/src/parsing/parameter-initializer-rewriter.cc b/src/parsing/parameter-initializer-rewriter.cc |
index ed553a7e0b83b59f6dc2ee51e5555bae8749aed0..cbbf8deec7997c0f1bc5665c394c848ac3676baa 100644 |
--- a/src/parsing/parameter-initializer-rewriter.cc |
+++ b/src/parsing/parameter-initializer-rewriter.cc |
@@ -4,6 +4,10 @@ |
#include "src/parsing/parameter-initializer-rewriter.h" |
+#include <algorithm> |
+#include <utility> |
+#include <vector> |
+ |
#include "src/ast/ast.h" |
#include "src/ast/ast-expression-visitor.h" |
#include "src/ast/scopes.h" |
@@ -21,6 +25,7 @@ class Rewriter final : public AstExpressionVisitor { |
: AstExpressionVisitor(stack_limit, initializer), |
old_scope_(old_scope), |
new_scope_(new_scope) {} |
+ ~Rewriter(); |
private: |
void VisitExpression(Expression* expr) override {} |
@@ -35,8 +40,26 @@ class Rewriter final : public AstExpressionVisitor { |
Scope* old_scope_; |
Scope* new_scope_; |
+ std::vector<std::pair<Variable*, int>> temps_; |
+}; |
+ |
+struct LessThanSecond { |
+ bool operator()(const std::pair<Variable*, int>& left, |
+ const std::pair<Variable*, int>& right) { |
+ return left.second < right.second; |
+ } |
}; |
+Rewriter::~Rewriter() { |
+ if (!temps_.empty()) { |
+ // Ensure that we add temporaries in the order they appeared in old_scope_. |
+ std::sort(temps_.begin(), temps_.end(), LessThanSecond()); |
+ for (auto var_and_index : temps_) { |
+ var_and_index.first->set_scope(new_scope_); |
+ new_scope_->AddTemporary(var_and_index.first); |
+ } |
+ } |
+} |
void Rewriter::VisitFunctionLiteral(FunctionLiteral* function_literal) { |
function_literal->scope()->ReplaceOuterScope(new_scope_); |
@@ -67,9 +90,9 @@ void Rewriter::VisitVariableProxy(VariableProxy* proxy) { |
if (proxy->is_resolved()) { |
Variable* var = proxy->var(); |
if (var->mode() != TEMPORARY) return; |
- if (old_scope_->RemoveTemporary(var)) { |
- var->set_scope(new_scope_); |
- new_scope_->AddTemporary(var); |
+ int index = old_scope_->RemoveTemporary(var); |
+ if (index >= 0) { |
+ temps_.push_back(std::make_pair(var, index)); |
} |
} else if (old_scope_->RemoveUnresolved(proxy)) { |
new_scope_->AddUnresolved(proxy); |