Commit e5e1454b authored by rmcilroy's avatar rmcilroy Committed by Commit bot

[Interpreter] Add support for new.target

BUG=v8:4280
LOG=N

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

Cr-Commit-Position: refs/heads/master@{#31862}
parent 6e981a32
......@@ -2050,9 +2050,6 @@ void BytecodeGenerator::VisitThisFunctionVariable(Variable* variable) {
void BytecodeGenerator::VisitNewTargetVariable(Variable* variable) {
if (variable == nullptr) return;
// TODO(rmcilroy): Remove once we have tests which exercise this code path.
UNIMPLEMENTED();
// Store the closure we were called with in the this_function_var.
builder()->CallRuntime(Runtime::kGetOriginalConstructor, Register(), 0);
VisitVariableAssignment(variable, FeedbackVectorSlot::Invalid());
......
......@@ -5188,6 +5188,32 @@ TEST(ThisFunction) {
}
}
TEST(NewTarget) {
InitializedHandleScope handle_scope;
BytecodeGeneratorHelper helper;
ExpectedSnippet<int> snippets[] = {
{"return new.target;",
1 * kPointerSize,
1,
10,
{
B(CallRuntime), U16(Runtime::kGetOriginalConstructor), R(0), //
U8(0), //
B(Star), R(0), //
B(Ldar), R(0), //
B(Return), //
}},
};
for (size_t i = 0; i < arraysize(snippets); i++) {
Handle<BytecodeArray> bytecode_array =
helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
CheckBytecodeArrayEqual(snippets[i], bytecode_array);
}
}
} // namespace interpreter
} // namespace internal
} // namespace v8
......@@ -2933,6 +2933,24 @@ TEST(InterpreterThisFunction) {
CHECK(return_value->SameValue(*factory->NewStringFromStaticChars("f")));
}
TEST(InterpreterNewTarget) {
HandleAndZoneScope handles;
i::Isolate* isolate = handles.main_isolate();
i::Factory* factory = isolate->factory();
// TODO(rmcilroy): Add tests that we get the original constructor for
// superclass constructors once we have class support.
InterpreterTester tester(handles.main_isolate(),
"function f() { this.a = new.target; }");
auto callable = tester.GetCallable<>();
callable().ToHandleChecked();
Handle<Object> new_target_name = v8::Utils::OpenHandle(
*CompileRun("(function() { return (new f()).a.name; })();"));
CHECK(new_target_name->SameValue(*factory->NewStringFromStaticChars("f")));
}
} // namespace interpreter
} // namespace internal
} // namespace v8
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