Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(409)

Side by Side Diff: src/compiler/ast-graph-builder.cc

Issue 1097283003: Resolve references to "this" the same way as normal variables (Closed) Base URL: https://chromium.googlesource.com/v8/v8@master
Patch Set: Add TODO to fix fat-fingered "this" scoping in script context Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/compiler/ast-graph-builder.h" 5 #include "src/compiler/ast-graph-builder.h"
6 6
7 #include "src/compiler.h" 7 #include "src/compiler.h"
8 #include "src/compiler/ast-loop-assignment-analyzer.h" 8 #include "src/compiler/ast-loop-assignment-analyzer.h"
9 #include "src/compiler/control-builders.h" 9 #include "src/compiler/control-builders.h"
10 #include "src/compiler/js-type-feedback.h" 10 #include "src/compiler/js-type-feedback.h"
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 463
464 // Initialize the incoming context. 464 // Initialize the incoming context.
465 CreateFunctionContext(constant_context); 465 CreateFunctionContext(constant_context);
466 ContextScope incoming(this, scope, function_context_.get()); 466 ContextScope incoming(this, scope, function_context_.get());
467 467
468 // Initialize control scope. 468 // Initialize control scope.
469 ControlScope control(this); 469 ControlScope control(this);
470 470
471 // Build receiver check for sloppy mode if necessary. 471 // Build receiver check for sloppy mode if necessary.
472 // TODO(mstarzinger/verwaest): Should this be moved back into the CallIC? 472 // TODO(mstarzinger/verwaest): Should this be moved back into the CallIC?
473 Node* original_receiver = env.Lookup(scope->receiver()); 473 Node* patched_receiver = nullptr;
474 Node* patched_receiver = BuildPatchReceiverToGlobalProxy(original_receiver); 474 if (scope->has_this_declaration()) {
475 env.Bind(scope->receiver(), patched_receiver); 475 Node* original_receiver = NewNode(common()->Parameter(0), graph()->start());
476 patched_receiver = BuildPatchReceiverToGlobalProxy(original_receiver);
477 if (scope->receiver()->IsStackAllocated()) {
478 env.Bind(scope->receiver(), patched_receiver);
479 }
480 }
476 481
477 // Build function context only if there are context allocated variables. 482 // Build function context only if there are context allocated variables.
478 int heap_slots = info()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS; 483 int heap_slots = info()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
479 if (heap_slots > 0) { 484 if (heap_slots > 0) {
480 // Push a new inner context scope for the function. 485 // Push a new inner context scope for the function.
481 Node* inner_context = BuildLocalFunctionContext(function_context_.get()); 486 Node* inner_context =
487 BuildLocalFunctionContext(function_context_.get(), patched_receiver);
482 ContextScope top_context(this, scope, inner_context); 488 ContextScope top_context(this, scope, inner_context);
483 CreateGraphBody(stack_check); 489 CreateGraphBody(stack_check);
484 } else { 490 } else {
485 // Simply use the outer function context in building the graph. 491 // Simply use the outer function context in building the graph.
486 CreateGraphBody(stack_check); 492 CreateGraphBody(stack_check);
487 } 493 }
488 494
489 // Finish the basic structure of the graph. 495 // Finish the basic structure of the graph.
490 graph()->SetEnd(graph()->NewNode(common()->End(), exit_control())); 496 graph()->SetEnd(graph()->NewNode(common()->End(), exit_control()));
491 497
(...skipping 1680 matching lines...) Expand 10 before | Expand all | Expand 10 after
2172 if (possibly_eval && args->length() > 0) { 2178 if (possibly_eval && args->length() > 0) {
2173 int arg_count = args->length(); 2179 int arg_count = args->length();
2174 2180
2175 // Extract callee and source string from the environment. 2181 // Extract callee and source string from the environment.
2176 Node* callee = environment()->Peek(arg_count + 1); 2182 Node* callee = environment()->Peek(arg_count + 1);
2177 Node* source = environment()->Peek(arg_count - 1); 2183 Node* source = environment()->Peek(arg_count - 1);
2178 2184
2179 // Create node to ask for help resolving potential eval call. This will 2185 // Create node to ask for help resolving potential eval call. This will
2180 // provide a fully resolved callee and the corresponding receiver. 2186 // provide a fully resolved callee and the corresponding receiver.
2181 Node* function = GetFunctionClosure(); 2187 Node* function = GetFunctionClosure();
2182 Node* receiver = environment()->Lookup(info()->scope()->receiver()); 2188 // TODO(wingo): ResolvePossibleDirectEval doesn't really need a receiver,
2189 // now that eval scopes don't have "this" declarations. Remove this hack
2190 // once ResolvePossibleDirectEval changes.
2191 Node* receiver;
2192 {
2193 Variable* variable = info()->scope()->LookupThis();
2194 if (variable->IsStackAllocated()) {
2195 receiver = environment()->Lookup(variable);
2196 } else {
2197 DCHECK(variable->IsContextSlot());
2198 int depth = current_scope()->ContextChainLength(variable->scope());
2199 bool immutable = variable->maybe_assigned() == kNotAssigned;
2200 const Operator* op =
2201 javascript()->LoadContext(depth, variable->index(), immutable);
2202 receiver = NewNode(op, current_context());
2203 }
2204 }
2183 Node* language = jsgraph()->Constant(language_mode()); 2205 Node* language = jsgraph()->Constant(language_mode());
2184 Node* position = jsgraph()->Constant(info()->scope()->start_position()); 2206 Node* position = jsgraph()->Constant(info()->scope()->start_position());
2185 const Operator* op = 2207 const Operator* op =
2186 javascript()->CallRuntime(Runtime::kResolvePossiblyDirectEval, 6); 2208 javascript()->CallRuntime(Runtime::kResolvePossiblyDirectEval, 6);
2187 Node* pair = 2209 Node* pair =
2188 NewNode(op, callee, source, function, receiver, language, position); 2210 NewNode(op, callee, source, function, receiver, language, position);
2189 PrepareFrameState(pair, expr->EvalOrLookupId(), 2211 PrepareFrameState(pair, expr->EvalOrLookupId(),
2190 OutputFrameStateCombine::PokeAt(arg_count + 1)); 2212 OutputFrameStateCombine::PokeAt(arg_count + 1));
2191 Node* new_callee = NewNode(common()->Projection(0), pair); 2213 Node* new_callee = NewNode(common()->Projection(0), pair);
2192 Node* new_receiver = NewNode(common()->Projection(1), pair); 2214 Node* new_receiver = NewNode(common()->Projection(1), pair);
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
2666 receiver_check.Then(); 2688 receiver_check.Then();
2667 Node* proxy = BuildLoadGlobalProxy(); 2689 Node* proxy = BuildLoadGlobalProxy();
2668 environment()->Push(proxy); 2690 environment()->Push(proxy);
2669 receiver_check.Else(); 2691 receiver_check.Else();
2670 environment()->Push(receiver); 2692 environment()->Push(receiver);
2671 receiver_check.End(); 2693 receiver_check.End();
2672 return environment()->Pop(); 2694 return environment()->Pop();
2673 } 2695 }
2674 2696
2675 2697
2676 Node* AstGraphBuilder::BuildLocalFunctionContext(Node* context) { 2698 Node* AstGraphBuilder::BuildLocalFunctionContext(Node* context,
2699 Node* patched_receiver) {
2700 Scope* scope = info()->scope();
2677 Node* closure = GetFunctionClosure(); 2701 Node* closure = GetFunctionClosure();
2678 2702
2679 // Allocate a new local context. 2703 // Allocate a new local context.
2680 Node* local_context = 2704 Node* local_context =
2681 info()->scope()->is_script_scope() 2705 scope->is_script_scope()
2682 ? BuildLocalScriptContext(info()->scope()) 2706 ? BuildLocalScriptContext(scope)
2683 : NewNode(javascript()->CreateFunctionContext(), closure); 2707 : NewNode(javascript()->CreateFunctionContext(), closure);
2684 2708
2709 if (scope->has_this_declaration() && scope->receiver()->IsContextSlot()) {
2710 DCHECK_NOT_NULL(patched_receiver);
2711 // Context variable (at bottom of the context chain).
2712 Variable* variable = scope->receiver();
2713 DCHECK_EQ(0, scope->ContextChainLength(variable->scope()));
2714 const Operator* op = javascript()->StoreContext(0, variable->index());
2715 NewNode(op, local_context, patched_receiver);
2716 }
2717
2685 // Copy parameters into context if necessary. 2718 // Copy parameters into context if necessary.
2686 int num_parameters = info()->scope()->num_parameters(); 2719 int num_parameters = scope->num_parameters();
2687 for (int i = 0; i < num_parameters; i++) { 2720 for (int i = 0; i < num_parameters; i++) {
2688 Variable* variable = info()->scope()->parameter(i); 2721 Variable* variable = scope->parameter(i);
2689 if (!variable->IsContextSlot()) continue; 2722 if (!variable->IsContextSlot()) continue;
2690 // Temporary parameter node. The parameter indices are shifted by 1 2723 // Temporary parameter node. The parameter indices are shifted by 1
2691 // (receiver is parameter index -1 but environment index 0). 2724 // (receiver is parameter index -1 but environment index 0).
2692 Node* parameter = NewNode(common()->Parameter(i + 1), graph()->start()); 2725 Node* parameter = NewNode(common()->Parameter(i + 1), graph()->start());
2693 // Context variable (at bottom of the context chain). 2726 // Context variable (at bottom of the context chain).
2694 DCHECK_EQ(0, info()->scope()->ContextChainLength(variable->scope())); 2727 DCHECK_EQ(0, scope->ContextChainLength(variable->scope()));
2695 const Operator* op = javascript()->StoreContext(0, variable->index()); 2728 const Operator* op = javascript()->StoreContext(0, variable->index());
2696 NewNode(op, local_context, parameter); 2729 NewNode(op, local_context, parameter);
2697 } 2730 }
2698 2731
2699 return local_context; 2732 return local_context;
2700 } 2733 }
2701 2734
2702 2735
2703 Node* AstGraphBuilder::BuildLocalScriptContext(Scope* scope) { 2736 Node* AstGraphBuilder::BuildLocalScriptContext(Scope* scope) {
2704 Node* closure = GetFunctionClosure(); 2737 Node* closure = GetFunctionClosure();
(...skipping 878 matching lines...) Expand 10 before | Expand all | Expand 10 after
3583 // Phi does not exist yet, introduce one. 3616 // Phi does not exist yet, introduce one.
3584 value = NewPhi(inputs, value, control); 3617 value = NewPhi(inputs, value, control);
3585 value->ReplaceInput(inputs - 1, other); 3618 value->ReplaceInput(inputs - 1, other);
3586 } 3619 }
3587 return value; 3620 return value;
3588 } 3621 }
3589 3622
3590 } // namespace compiler 3623 } // namespace compiler
3591 } // namespace internal 3624 } // namespace internal
3592 } // namespace v8 3625 } // namespace v8
OLDNEW
« no previous file with comments | « src/compiler/ast-graph-builder.h ('k') | src/contexts.cc » ('j') | src/parser.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698