Skip to content

Commit c3bd741

Browse files
GeorgNeisCommit Bot
authored andcommitted
Fix "this" value in lazily-parsed module functions.
When preparsing top-level functions in a module, we didn't track unresolved variables. Consequently, "this" ended up referencing the global "this", which has the wrong value (in a module "this" is supposed to be the undefined value). This patch fixes that. This also lets us stop forcing context allocation of all variables in module scopes, which the patch takes care of as well. Bug: chromium:791334 Change-Id: Ifac1f1adc033f3facfb3d29dd4bca32ee27bffcf Reviewed-on: https://chromium-review.googlesource.com/808938 Reviewed-by: Marja Hölttä <marja@chromium.org> Reviewed-by: Adam Klein <adamk@chromium.org> Reviewed-by: Aleksey Kozyatinskiy <kozyatinskiy@chromium.org> Commit-Queue: Georg Neis <neis@chromium.org> Cr-Commit-Position: refs/heads/master@{#50025}
1 parent ccd15ea commit c3bd741

File tree

10 files changed

+475
-520
lines changed

10 files changed

+475
-520
lines changed

src/ast/scopes.cc

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type)
147147
DCHECK_NE(SCRIPT_SCOPE, scope_type);
148148
SetDefaults();
149149
set_language_mode(outer_scope->language_mode());
150-
force_context_allocation_ =
151-
!is_function_scope() && outer_scope->has_forced_context_allocation();
152150
outer_scope_->AddInnerScope(this);
153151
}
154152

@@ -1370,12 +1368,8 @@ bool Scope::AllowsLazyParsingWithoutUnresolvedVariables(
13701368
if (s->is_catch_scope()) continue;
13711369
// With scopes do not introduce variables that need allocation.
13721370
if (s->is_with_scope()) continue;
1373-
// Module scopes context-allocate all variables, and have no
1374-
// {this} or {arguments} variables whose existence depends on
1375-
// references to them.
1376-
if (s->is_module_scope()) continue;
1377-
// Only block scopes and function scopes should disallow preparsing.
1378-
DCHECK(s->is_block_scope() || s->is_function_scope());
1371+
DCHECK(s->is_module_scope() || s->is_block_scope() ||
1372+
s->is_function_scope());
13791373
return false;
13801374
}
13811375
return true;
@@ -1734,9 +1728,6 @@ void Scope::Print(int n) {
17341728
if (scope->was_lazily_parsed()) Indent(n1, "// lazily parsed\n");
17351729
if (scope->ShouldEagerCompile()) Indent(n1, "// will be compiled\n");
17361730
}
1737-
if (has_forced_context_allocation()) {
1738-
Indent(n1, "// forces context allocation\n");
1739-
}
17401731
if (num_stack_slots_ > 0) {
17411732
Indent(n1, "// ");
17421733
PrintF("%d stack slots\n", num_stack_slots_);
@@ -2111,11 +2102,8 @@ bool Scope::MustAllocateInContext(Variable* var) {
21112102
// an eval() call or a runtime with lookup), it must be allocated in the
21122103
// context.
21132104
//
2114-
// Exceptions: If the scope as a whole has forced context allocation, all
2115-
// variables will have context allocation, even temporaries. Otherwise
2116-
// temporary variables are always stack-allocated. Catch-bound variables are
2105+
// Temporary variables are always stack-allocated. Catch-bound variables are
21172106
// always context-allocated.
2118-
if (has_forced_context_allocation()) return true;
21192107
if (var->mode() == TEMPORARY) return false;
21202108
if (is_catch_scope()) return true;
21212109
if ((is_script_scope() || is_eval_scope()) &&

src/ast/scopes.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,6 @@ class V8_EXPORT_PRIVATE Scope : public NON_EXPORTED_BASE(ZoneObject) {
334334
bool is_hidden() const { return is_hidden_; }
335335
void set_is_hidden() { is_hidden_ = true; }
336336

337-
// In some cases we want to force context allocation for a whole scope.
338-
void ForceContextAllocation() {
339-
DCHECK(!already_resolved_);
340-
force_context_allocation_ = true;
341-
}
342-
bool has_forced_context_allocation() const {
343-
return force_context_allocation_;
344-
}
345337
void ForceContextAllocationForParameters() {
346338
DCHECK(!already_resolved_);
347339
force_context_allocation_for_parameters_ = true;

src/parsing/parser.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,6 @@ FunctionLiteral* Parser::DoParseProgram(ParseInfo* info) {
690690
var->AllocateTo(VariableLocation::PARAMETER, 0);
691691

692692
PrepareGeneratorVariables();
693-
scope->ForceContextAllocation();
694693
Expression* initial_yield =
695694
BuildInitialYield(kNoSourcePosition, kGeneratorFunction);
696695
body->Add(

0 commit comments

Comments
 (0)