[Interpreter] Add ForOf support.

Adds support for ForOf to the interpreter.

BUG=v8:4685
LOG=N

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

Cr-Commit-Position: refs/heads/master@{#33470}
This commit is contained in:
rmcilroy 2016-01-22 07:54:24 -08:00 committed by Commit bot
parent 56e1b13ea1
commit 380f8c8f46
4 changed files with 455 additions and 1 deletions

View File

@ -1082,7 +1082,21 @@ void BytecodeGenerator::VisitForInStatement(ForInStatement* stmt) {
void BytecodeGenerator::VisitForOfStatement(ForOfStatement* stmt) {
UNIMPLEMENTED();
LoopBuilder loop_builder(builder());
ControlScopeForIteration control_scope(this, stmt, &loop_builder);
VisitForEffect(stmt->assign_iterator());
loop_builder.LoopHeader();
loop_builder.Next();
VisitForEffect(stmt->next_result());
VisitForAccumulatorValue(stmt->result_done());
loop_builder.BreakIfTrue();
VisitForEffect(stmt->assign_each());
Visit(stmt->body());
loop_builder.JumpToHeader();
loop_builder.EndLoop();
}

View File

@ -2294,6 +2294,99 @@ TEST(BytecodeGraphBuilderForIn) {
}
TEST(BytecodeGraphBuilderForOf) {
HandleAndZoneScope scope;
Isolate* isolate = scope.main_isolate();
Zone* zone = scope.main_zone();
Factory* factory = isolate->factory();
ExpectedSnippet<0> snippets[] = {
{" var r = 0;\n"
" for (var a of [0,6,7,9]) { r += a; }\n"
" return r;\n",
{handle(Smi::FromInt(22), isolate)}},
{" var r = '';\n"
" for (var a of 'foobar') { r = a + r; }\n"
" return r;\n",
{factory->NewStringFromStaticChars("raboof")}},
{" var a = [1, 2, 3];\n"
" a.name = 4;\n"
" var r = 0;\n"
" for (var x of a) { r += x; }\n"
" return r;\n",
{handle(Smi::FromInt(6), isolate)}},
{" var r = '';\n"
" var data = [1, 2, 3]; \n"
" for (a of data) { delete data[0]; r += a; } return r;",
{factory->NewStringFromStaticChars("123")}},
{" var r = '';\n"
" var data = [1, 2, 3]; \n"
" for (a of data) { delete data[2]; r += a; } return r;",
{factory->NewStringFromStaticChars("12undefined")}},
{" var r = '';\n"
" var data = [1, 2, 3]; \n"
" for (a of data) { delete data; r += a; } return r;",
{factory->NewStringFromStaticChars("123")}},
{" var r = '';\n"
" var input = 'foobar';\n"
" for (var a of input) {\n"
" if (a == 'b') break;\n"
" r += a;\n"
" }\n"
" return r;\n",
{factory->NewStringFromStaticChars("foo")}},
{" var r = '';\n"
" var input = 'foobar';\n"
" for (var a of input) {\n"
" if (a == 'b') continue;\n"
" r += a;\n"
" }\n"
" return r;\n",
{factory->NewStringFromStaticChars("fooar")}},
{" var r = '';\n"
" var data = [1, 2, 3, 4]; \n"
" for (a of data) { data[2] = 567; r += a; }\n"
" return r;\n",
{factory->NewStringFromStaticChars("125674")}},
{" var r = '';\n"
" var data = [1, 2, 3, 4]; \n"
" for (a of data) { data[4] = 567; r += a; }\n"
" return r;\n",
{factory->NewStringFromStaticChars("1234567")}},
{" var r = '';\n"
" var data = [1, 2, 3, 4]; \n"
" for (a of data) { data[5] = 567; r += a; }\n"
" return r;\n",
{factory->NewStringFromStaticChars("1234undefined567")}},
{" var r = '';\n"
" var obj = new Object();\n"
" obj[Symbol.iterator] = function() { return {\n"
" index: 3,\n"
" data: ['a', 'b', 'c', 'd'],"
" next: function() {"
" return {"
" done: this.index == -1,\n"
" value: this.index < 0 ? undefined : this.data[this.index--]\n"
" }\n"
" }\n"
" }}\n"
" for (a of obj) { r += a }\n"
" return r;\n",
{factory->NewStringFromStaticChars("dcba")}},
};
for (size_t i = 0; i < arraysize(snippets); i++) {
ScopedVector<char> script(1024);
SNPrintF(script, "function %s() { %s }\n%s();", kFunctionName,
snippets[i].code_snippet, kFunctionName);
BytecodeGraphTester tester(isolate, zone, script.start());
auto callable = tester.GetCallable<>();
Handle<Object> return_value = callable().ToHandleChecked();
CHECK(return_value->SameValue(*snippets[i].return_value()));
}
}
TEST(JumpWithConstantsAndWideConstants) {
HandleAndZoneScope scope;
auto isolate = scope.main_isolate();

View File

@ -5740,6 +5740,243 @@ TEST(ForIn) {
}
TEST(ForOf) {
InitializedHandleScope handle_scope;
BytecodeGeneratorHelper helper;
Zone zone;
int array_literal_flags =
ArrayLiteral::kDisableMementos | ArrayLiteral::kShallowElements;
int object_literal_flags =
ObjectLiteral::kFastElements | ObjectLiteral::kDisableMementos;
FeedbackVectorSpec feedback_spec(&zone);
FeedbackVectorSlot slot1 = feedback_spec.AddCallICSlot();
FeedbackVectorSlot slot2 = feedback_spec.AddKeyedLoadICSlot();
FeedbackVectorSlot slot3 = feedback_spec.AddCallICSlot();
FeedbackVectorSlot slot4 = feedback_spec.AddLoadICSlot();
FeedbackVectorSlot slot5 = feedback_spec.AddLoadICSlot();
FeedbackVectorSlot slot6 = feedback_spec.AddLoadICSlot();
FeedbackVectorSlot slot7 = feedback_spec.AddStoreICSlot();
FeedbackVectorSlot slot8 = feedback_spec.AddLoadICSlot();
Handle<i::TypeFeedbackVector> vector =
i::NewTypeFeedbackVector(helper.isolate(), &feedback_spec);
ExpectedSnippet<InstanceType, 8> snippets[] = {
{"for (var p of [0, 1, 2]) {}",
7 * kPointerSize,
1,
82,
{
B(CreateArrayLiteral), U8(0), U8(0), U8(array_literal_flags), //
B(Star), R(5), //
B(LdaConstant), U8(1), //
B(KeyedLoadICSloppy), R(5), U8(vector->GetIndex(slot2)), //
B(Star), R(4), //
B(Call), R(4), R(5), U8(0), U8(vector->GetIndex(slot1)), //
B(Star), R(1), //
B(Ldar), R(1), //
B(Star), R(6), //
B(LoadICSloppy), R(6), U8(2), U8(vector->GetIndex(slot4)), //
B(Star), R(5), //
B(Call), R(5), R(6), U8(0), U8(vector->GetIndex(slot3)), //
B(Star), R(2), //
B(Star), R(4), //
B(CallRuntime), U16(Runtime::kInlineIsJSReceiver), R(4), U8(1), //
B(LogicalNot), //
B(JumpIfFalse), U8(11), //
B(Ldar), R(2), //
B(Star), R(4), //
B(CallRuntime), U16(Runtime::kThrowIteratorResultNotAnObject), //
R(4), U8(1), //
B(Ldar), R(2), //
B(Star), R(4), //
B(LoadICSloppy), R(4), U8(3), U8(vector->GetIndex(slot5)), //
B(JumpIfToBooleanTrue), U8(16), //
B(Ldar), R(2), //
B(Star), R(4), //
B(LoadICSloppy), R(4), U8(4), U8(vector->GetIndex(slot6)), //
B(Star), R(0), //
B(Star), R(3), //
B(Jump), U8(-58), //
B(LdaUndefined), //
B(Return), //
},
5,
{InstanceType::FIXED_ARRAY_TYPE, InstanceType::SYMBOL_TYPE,
InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
{"var x = 'potatoes';\n"
"for (var p of x) { return p; }",
8 * kPointerSize,
1,
81,
{
B(LdaConstant), U8(0), //
B(Star), R(3), //
B(Star), R(6), //
B(LdaConstant), U8(1), //
B(KeyedLoadICSloppy), R(6), U8(vector->GetIndex(slot2)), //
B(Star), R(5), //
B(Call), R(5), R(6), U8(0), U8(vector->GetIndex(slot1)), //
B(Star), R(1), //
B(Ldar), R(1), //
B(Star), R(7), //
B(LoadICSloppy), R(7), U8(2), U8(vector->GetIndex(slot4)), //
B(Star), R(6), //
B(Call), R(6), R(7), U8(0), U8(vector->GetIndex(slot3)), //
B(Star), R(2), //
B(Star), R(5), //
B(CallRuntime), U16(Runtime::kInlineIsJSReceiver), R(5), U8(1), //
B(LogicalNot), //
B(JumpIfFalse), U8(11), //
B(Ldar), R(2), //
B(Star), R(5), //
B(CallRuntime), U16(Runtime::kThrowIteratorResultNotAnObject), //
R(5), U8(1), //
B(Ldar), R(2), //
B(Star), R(5), //
B(LoadICSloppy), R(5), U8(3), U8(vector->GetIndex(slot5)), //
B(JumpIfToBooleanTrue), U8(15), //
B(Ldar), R(2), //
B(Star), R(5), //
B(LoadICSloppy), R(5), U8(4), U8(vector->GetIndex(slot6)), //
B(Star), R(0), //
B(Star), R(4), //
B(Return), //
B(LdaUndefined), //
B(Return), //
},
5,
{InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
InstanceType::SYMBOL_TYPE,
InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
{"for (var x of [10, 20, 30]) {\n"
" if (x == 10) continue;\n"
" if (x == 20) break;\n"
"}",
7 * kPointerSize,
1,
104,
{
B(CreateArrayLiteral), U8(0), U8(0), U8(array_literal_flags), //
B(Star), R(5), //
B(LdaConstant), U8(1), //
B(KeyedLoadICSloppy), R(5), U8(vector->GetIndex(slot2)), //
B(Star), R(4), //
B(Call), R(4), R(5), U8(0), U8(vector->GetIndex(slot1)), //
B(Star), R(1), //
B(Ldar), R(1), //
B(Star), R(6), //
B(LoadICSloppy), R(6), U8(2), U8(vector->GetIndex(slot4)), //
B(Star), R(5), //
B(Call), R(5), R(6), U8(0), U8(vector->GetIndex(slot3)), //
B(Star), R(2), //
B(Star), R(4), //
B(CallRuntime), U16(Runtime::kInlineIsJSReceiver), R(4), U8(1), //
B(LogicalNot), //
B(JumpIfFalse), U8(11), //
B(Ldar), R(2), //
B(Star), R(4), //
B(CallRuntime), U16(Runtime::kThrowIteratorResultNotAnObject), //
R(4), U8(1), //
B(Ldar), R(2), //
B(Star), R(4), //
B(LoadICSloppy), R(4), U8(3), U8(vector->GetIndex(slot5)), //
B(JumpIfToBooleanTrue), U8(38), //
B(Ldar), R(2), //
B(Star), R(4), //
B(LoadICSloppy), R(4), U8(4), U8(vector->GetIndex(slot6)), //
B(Star), R(0), //
B(Star), R(3), //
B(Star), R(4), //
B(LdaSmi8), U8(10), //
B(TestEqual), R(4), //
B(JumpIfFalse), U8(4), //
B(Jump), U8(-66), //
B(Ldar), R(3), //
B(Star), R(4), //
B(LdaSmi8), U8(20), //
B(TestEqual), R(4), //
B(JumpIfFalse), U8(4), //
B(Jump), U8(4), //
B(Jump), U8(-80), //
B(LdaUndefined), //
B(Return), //
},
5,
{InstanceType::FIXED_ARRAY_TYPE, InstanceType::SYMBOL_TYPE,
InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
{"var x = { 'a': 1, 'b': 2 };\n"
"for (x['a'] of [1,2,3]) { return x['a']; }",
6 * kPointerSize,
1,
101,
{
B(CreateObjectLiteral), U8(0), U8(0), U8(object_literal_flags), //
B(Star), R(3), //
B(Star), R(2), //
B(CreateArrayLiteral), U8(1), U8(1), U8(array_literal_flags), //
B(Star), R(4), //
B(LdaConstant), U8(2), //
B(KeyedLoadICSloppy), R(4), U8(vector->GetIndex(slot2)), //
B(Star), R(3), //
B(Call), R(3), R(4), U8(0), U8(vector->GetIndex(slot1)), //
B(Star), R(0), //
B(Ldar), R(0), //
B(Star), R(5), //
B(LoadICSloppy), R(5), U8(3), U8(vector->GetIndex(slot4)), //
B(Star), R(4), //
B(Call), R(4), R(5), U8(0), U8(vector->GetIndex(slot3)), //
B(Star), R(1), //
B(Star), R(3), //
B(CallRuntime), U16(Runtime::kInlineIsJSReceiver), R(3), U8(1), //
B(LogicalNot), //
B(JumpIfFalse), U8(11), //
B(Ldar), R(1), //
B(Star), R(3), //
B(CallRuntime), U16(Runtime::kThrowIteratorResultNotAnObject), //
R(3), U8(1), //
B(Ldar), R(1), //
B(Star), R(3), //
B(LoadICSloppy), R(3), U8(4), U8(vector->GetIndex(slot5)), //
B(JumpIfToBooleanTrue), U8(27), //
B(Ldar), R(2), //
B(Star), R(3), //
B(Ldar), R(1), //
B(Star), R(4), //
B(LoadICSloppy), R(4), U8(5), U8(vector->GetIndex(slot6)), //
B(StoreICSloppy), R(3), U8(6), U8(vector->GetIndex(slot7)), //
B(Ldar), R(2), //
B(Star), R(3), //
B(LoadICSloppy), R(3), U8(6), U8(vector->GetIndex(slot8)), //
B(Return), //
B(LdaUndefined), //
B(Return), //
},
7,
{InstanceType::FIXED_ARRAY_TYPE,
InstanceType::FIXED_ARRAY_TYPE,
InstanceType::SYMBOL_TYPE,
InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE,
InstanceType::ONE_BYTE_INTERNALIZED_STRING_TYPE}},
};
for (size_t i = 0; i < arraysize(snippets); i++) {
Handle<BytecodeArray> bytecode_array =
helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
CheckBytecodeArrayEqual(snippets[i], bytecode_array);
}
}
TEST(Conditional) {
InitializedHandleScope handle_scope;
BytecodeGeneratorHelper helper;

View File

@ -2837,6 +2837,116 @@ TEST(InterpreterForIn) {
}
TEST(InterpreterForOf) {
HandleAndZoneScope handles;
i::Isolate* isolate = handles.main_isolate();
i::Factory* factory = isolate->factory();
std::pair<const char*, Handle<Object>> for_of[] = {
{"function f() {\n"
" var r = 0;\n"
" for (var a of [0,6,7,9]) { r += a; }\n"
" return r;\n"
"}",
handle(Smi::FromInt(22), isolate)},
{"function f() {\n"
" var r = '';\n"
" for (var a of 'foobar') { r = a + r; }\n"
" return r;\n"
"}",
factory->NewStringFromStaticChars("raboof")},
{"function f() {\n"
" var a = [1, 2, 3];\n"
" a.name = 4;\n"
" var r = 0;\n"
" for (var x of a) { r += x; }\n"
" return r;\n"
"}",
handle(Smi::FromInt(6), isolate)},
{"function f() {\n"
" var r = '';\n"
" var data = [1, 2, 3]; \n"
" for (a of data) { delete data[0]; r += a; } return r; }",
factory->NewStringFromStaticChars("123")},
{"function f() {\n"
" var r = '';\n"
" var data = [1, 2, 3]; \n"
" for (a of data) { delete data[2]; r += a; } return r; }",
factory->NewStringFromStaticChars("12undefined")},
{"function f() {\n"
" var r = '';\n"
" var data = [1, 2, 3]; \n"
" for (a of data) { delete data; r += a; } return r; }",
factory->NewStringFromStaticChars("123")},
{"function f() {\n"
" var r = '';\n"
" var input = 'foobar';\n"
" for (var a of input) {\n"
" if (a == 'b') break;\n"
" r += a;\n"
" }\n"
" return r;\n"
"}",
factory->NewStringFromStaticChars("foo")},
{"function f() {\n"
" var r = '';\n"
" var input = 'foobar';\n"
" for (var a of input) {\n"
" if (a == 'b') continue;\n"
" r += a;\n"
" }\n"
" return r;\n"
"}",
factory->NewStringFromStaticChars("fooar")},
{"function f() {\n"
" var r = '';\n"
" var data = [1, 2, 3, 4]; \n"
" for (a of data) { data[2] = 567; r += a; }\n"
" return r;\n"
"}",
factory->NewStringFromStaticChars("125674")},
{"function f() {\n"
" var r = '';\n"
" var data = [1, 2, 3, 4]; \n"
" for (a of data) { data[4] = 567; r += a; }\n"
" return r;\n"
"}",
factory->NewStringFromStaticChars("1234567")},
{"function f() {\n"
" var r = '';\n"
" var data = [1, 2, 3, 4]; \n"
" for (a of data) { data[5] = 567; r += a; }\n"
" return r;\n"
"}",
factory->NewStringFromStaticChars("1234undefined567")},
{"function f() {\n"
" var r = '';\n"
" var obj = new Object();\n"
" obj[Symbol.iterator] = function() { return {\n"
" index: 3,\n"
" data: ['a', 'b', 'c', 'd'],"
" next: function() {"
" return {"
" done: this.index == -1,\n"
" value: this.index < 0 ? undefined : this.data[this.index--]\n"
" }\n"
" }\n"
" }}\n"
" for (a of obj) { r += a }\n"
" return r;\n"
"}",
factory->NewStringFromStaticChars("dcba")},
};
for (size_t i = 0; i < arraysize(for_of); i++) {
InterpreterTester tester(handles.main_isolate(), for_of[i].first);
auto callable = tester.GetCallable<>();
Handle<Object> return_val = callable().ToHandleChecked();
CHECK(return_val->SameValue(*for_of[i].second));
}
}
TEST(InterpreterSwitch) {
HandleAndZoneScope handles;
i::Isolate* isolate = handles.main_isolate();