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

Unified Diff: src/x64/full-codegen-x64.cc

Issue 791603003: WIP context-allocation for “this” in arrow functions Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: WIP v3 Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: src/x64/full-codegen-x64.cc
diff --git a/src/x64/full-codegen-x64.cc b/src/x64/full-codegen-x64.cc
index 24747ee9e85b11d7e3fed1b50e70db0d2073e9b2..41bac14e3761317b0c3d694a42d50bde3934b5a9 100644
--- a/src/x64/full-codegen-x64.cc
+++ b/src/x64/full-codegen-x64.cc
@@ -113,8 +113,10 @@ void FullCodeGenerator::Generate() {
// Sloppy mode functions and builtins need to replace the receiver with the
// global proxy when called as functions (without an explicit receiver
- // object).
- if (info->strict_mode() == SLOPPY && !info->is_native()) {
+ // object). Arrow functions need to replace the receiver with the receiver
+ // of the parent scope, so this does not need to be done for those.
wingo 2015/01/15 10:09:06 It's not necessarily the receiver from the parent
aperez 2015/01/15 16:58:03 Done.
+ if (info->strict_mode() == SLOPPY && !info->is_native() &&
+ !info->function()->is_arrow()) {
Label ok;
// +1 for return address.
StackArgumentsAccessor args(rsp, info->scope()->num_parameters());
@@ -140,6 +142,19 @@ void FullCodeGenerator::Generate() {
__ Prologue(info->IsCodePreAgingActive());
info->AddNoFrameRange(0, masm_->pc_offset());
+ // For arrow functions the value of the receiver is stored in the context,
+ // so in the case of a function that actually uses "this", the receiver is
+ // replaced by the one resulting from looking it up in the context.
+ if (info->function()->is_arrow() && info->scope()->uses_this()) {
+ DCHECK(info->scope()->scope_type() == ARROW_SCOPE);
+ Comment cmnt(masm_, "[ Patch receiver for arrow function");
+ StackArgumentsAccessor args(rbp, info->scope()->num_parameters());
+ __ Push(rsi); // Context
+ __ Push(isolate()->factory()->this_string());
+ __ CallRuntime(Runtime::kLoadLookupSlot, 2);
+ __ movp(args.GetReceiverOperand(), rax);
+ }
+
{ Comment cmnt(masm_, "[ Allocate locals");
int locals_count = info->scope()->num_stack_slots();
// Generators allocate locals, if any, in context slots.
@@ -207,6 +222,30 @@ void FullCodeGenerator::Generate() {
__ movp(Operand(rbp, StandardFrameConstants::kContextOffset), rax);
// Copy any necessary parameters into the context.
+ if (info->scope()->inner_uses_this()) {
wingo 2015/01/15 10:09:06 Let's factor this into the loop below
aperez 2015/01/15 16:58:03 Done.
+ Comment cmnt(masm_, "[ Save receiver in context for arrow function");
+ DCHECK(info->scope()->receiver()->IsContextSlot());
+ // Put the "this" in the context after the rest of the function
+ // parameters, to avoid having to change the case that handles those.
+ int parameter_offset = StandardFrameConstants::kCallerSPOffset +
+ info->scope()->num_parameters() * kPointerSize;
+ __ movp(rax, Operand(rbp, parameter_offset));
+
+ // Store it in the context.
+ int context_offset = Context::SlotOffset(scope()->receiver()->index());
+ __ movp(Operand(rsi, context_offset), rax);
+
+ if (need_write_barrier) {
+ __ RecordWriteContextSlot(
+ rsi, context_offset, rax, rbx, kDontSaveFPRegs);
+ } else if (FLAG_debug_code) {
+ Label done;
+ __ JumpIfInNewSpace(rsi, rax, &done, Label::kNear);
+ __ Abort(kExpectedNewSpaceObject);
+ __ bind(&done);
+ }
+ }
+
int num_parameters = info->scope()->num_parameters();
for (int i = 0; i < num_parameters; i++) {
Variable* var = scope()->parameter(i);
@@ -2721,7 +2760,8 @@ void FullCodeGenerator::EmitCallWithLoadIC(Call* expr) {
PrepareForBailout(callee, NO_REGISTERS);
}
// Push undefined as receiver. This is patched in the method prologue if it
- // is a sloppy mode method.
+ // is a sloppy mode method, or by the function prologue it is an arrow
wingo 2015/01/15 10:09:06 "if it is"
aperez 2015/01/15 16:58:03 Done.
+ // function.
__ Push(isolate()->factory()->undefined_value());
} else {
// Load the function from the receiver.
« src/scopes.cc ('K') | « src/scopes.cc ('k') | test/cctest/test-parsing.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698