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

Side by Side Diff: runtime/vm/snapshot_test.cc

Issue 9303031: Add support for lists and backward references when decoding a message to a Dart_CObject object (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased to r3830 Created 8 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « runtime/vm/snapshot.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "include/dart_debugger_api.h" 5 #include "include/dart_debugger_api.h"
6 #include "platform/assert.h" 6 #include "platform/assert.h"
7 #include "vm/bigint_operations.h" 7 #include "vm/bigint_operations.h"
8 #include "vm/class_finalizer.h" 8 #include "vm/class_finalizer.h"
9 #include "vm/dart_api_impl.h" 9 #include "vm/dart_api_impl.h"
10 #include "vm/dart_api_state.h" 10 #include "vm/dart_api_state.h"
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 // Read object back from the snapshot. 57 // Read object back from the snapshot.
58 SnapshotReader reader(snapshot, Isolate::Current()); 58 SnapshotReader reader(snapshot, Isolate::Current());
59 const Object& serialized_object = Object::Handle(reader.ReadObject()); 59 const Object& serialized_object = Object::Handle(reader.ReadObject());
60 EXPECT(Equals(null_object, serialized_object)); 60 EXPECT(Equals(null_object, serialized_object));
61 61
62 // Read object back from the snapshot into a C structure. 62 // Read object back from the snapshot into a C structure.
63 CMessageReader mreader(buffer + Snapshot::kHeaderSize, 63 CMessageReader mreader(buffer + Snapshot::kHeaderSize,
64 writer.BytesWritten(), 64 writer.BytesWritten(),
65 &allocator); 65 &allocator);
66 Dart_CObject* cobject = mreader.ReadObject(); 66 Dart_CObject* cobject = mreader.ReadObject();
67 EXPECT_NOTNULL(cobject);
67 EXPECT_EQ(Dart_CObject::kNull, cobject->type); 68 EXPECT_EQ(Dart_CObject::kNull, cobject->type);
68 free(cobject); 69 free(cobject);
69 } 70 }
70 71
71 72
72 TEST_CASE(SerializeSmi1) { 73 TEST_CASE(SerializeSmi1) {
73 // Write snapshot with object content. 74 // Write snapshot with object content.
74 uint8_t* buffer; 75 uint8_t* buffer;
75 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); 76 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
76 const Smi& smi = Smi::Handle(Smi::New(124)); 77 const Smi& smi = Smi::Handle(Smi::New(124));
77 writer.WriteObject(smi.raw()); 78 writer.WriteObject(smi.raw());
78 writer.FinalizeBuffer(); 79 writer.FinalizeBuffer();
79 80
80 // Create a snapshot object using the buffer. 81 // Create a snapshot object using the buffer.
81 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 82 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
82 83
83 // Read object back from the snapshot. 84 // Read object back from the snapshot.
84 SnapshotReader reader(snapshot, Isolate::Current()); 85 SnapshotReader reader(snapshot, Isolate::Current());
85 const Object& serialized_object = Object::Handle(reader.ReadObject()); 86 const Object& serialized_object = Object::Handle(reader.ReadObject());
86 EXPECT(Equals(smi, serialized_object)); 87 EXPECT(Equals(smi, serialized_object));
87 88
88 // Read object back from the snapshot into a C structure. 89 // Read object back from the snapshot into a C structure.
89 CMessageReader mreader(buffer + Snapshot::kHeaderSize, 90 CMessageReader mreader(buffer + Snapshot::kHeaderSize,
90 writer.BytesWritten(), 91 writer.BytesWritten(),
91 &allocator); 92 &allocator);
92 Dart_CObject* cobject = mreader.ReadObject(); 93 Dart_CObject* cobject = mreader.ReadObject();
94 EXPECT_NOTNULL(cobject);
93 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); 95 EXPECT_EQ(Dart_CObject::kInt32, cobject->type);
94 EXPECT_EQ(smi.Value(), cobject->value.as_int32); 96 EXPECT_EQ(smi.Value(), cobject->value.as_int32);
95 free(cobject); 97 free(cobject);
96 } 98 }
97 99
98 100
99 TEST_CASE(SerializeSmi2) { 101 TEST_CASE(SerializeSmi2) {
100 // Write snapshot with object content. 102 // Write snapshot with object content.
101 uint8_t* buffer; 103 uint8_t* buffer;
102 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); 104 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
103 const Smi& smi = Smi::Handle(Smi::New(-1)); 105 const Smi& smi = Smi::Handle(Smi::New(-1));
104 writer.WriteObject(smi.raw()); 106 writer.WriteObject(smi.raw());
105 writer.FinalizeBuffer(); 107 writer.FinalizeBuffer();
106 108
107 // Create a snapshot object using the buffer. 109 // Create a snapshot object using the buffer.
108 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 110 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
109 111
110 // Read object back from the snapshot. 112 // Read object back from the snapshot.
111 SnapshotReader reader(snapshot, Isolate::Current()); 113 SnapshotReader reader(snapshot, Isolate::Current());
112 const Object& serialized_object = Object::Handle(reader.ReadObject()); 114 const Object& serialized_object = Object::Handle(reader.ReadObject());
113 EXPECT(Equals(smi, serialized_object)); 115 EXPECT(Equals(smi, serialized_object));
114 116
115 // Read object back from the snapshot into a C structure. 117 // Read object back from the snapshot into a C structure.
116 CMessageReader mreader(buffer + Snapshot::kHeaderSize, 118 CMessageReader mreader(buffer + Snapshot::kHeaderSize,
117 writer.BytesWritten(), 119 writer.BytesWritten(),
118 &allocator); 120 &allocator);
119 Dart_CObject* cobject = mreader.ReadObject(); 121 Dart_CObject* cobject = mreader.ReadObject();
122 EXPECT_NOTNULL(cobject);
120 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); 123 EXPECT_EQ(Dart_CObject::kInt32, cobject->type);
121 EXPECT_EQ(smi.Value(), cobject->value.as_int32); 124 EXPECT_EQ(smi.Value(), cobject->value.as_int32);
122 free(cobject); 125 free(cobject);
123 } 126 }
124 127
125 128
126 TEST_CASE(SerializeDouble) { 129 TEST_CASE(SerializeDouble) {
127 // Write snapshot with object content. 130 // Write snapshot with object content.
128 uint8_t* buffer; 131 uint8_t* buffer;
129 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); 132 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
130 const Double& dbl = Double::Handle(Double::New(101.29)); 133 const Double& dbl = Double::Handle(Double::New(101.29));
131 writer.WriteObject(dbl.raw()); 134 writer.WriteObject(dbl.raw());
132 writer.FinalizeBuffer(); 135 writer.FinalizeBuffer();
133 136
134 // Create a snapshot object using the buffer. 137 // Create a snapshot object using the buffer.
135 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 138 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
136 139
137 // Read object back from the snapshot. 140 // Read object back from the snapshot.
138 SnapshotReader reader(snapshot, Isolate::Current()); 141 SnapshotReader reader(snapshot, Isolate::Current());
139 const Object& serialized_object = Object::Handle(reader.ReadObject()); 142 const Object& serialized_object = Object::Handle(reader.ReadObject());
140 EXPECT(Equals(dbl, serialized_object)); 143 EXPECT(Equals(dbl, serialized_object));
141 144
142 // Read object back from the snapshot into a C structure. 145 // Read object back from the snapshot into a C structure.
143 CMessageReader mreader(buffer + Snapshot::kHeaderSize, 146 CMessageReader mreader(buffer + Snapshot::kHeaderSize,
144 writer.BytesWritten(), 147 writer.BytesWritten(),
145 &allocator); 148 &allocator);
146 Dart_CObject* cobject = mreader.ReadObject(); 149 Dart_CObject* cobject = mreader.ReadObject();
150 EXPECT_NOTNULL(cobject);
147 EXPECT_EQ(Dart_CObject::kDouble, cobject->type); 151 EXPECT_EQ(Dart_CObject::kDouble, cobject->type);
148 EXPECT_EQ(dbl.value(), cobject->value.as_double); 152 EXPECT_EQ(dbl.value(), cobject->value.as_double);
149 free(cobject); 153 free(cobject);
150 } 154 }
151 155
152 156
153 TEST_CASE(SerializeBool) { 157 TEST_CASE(SerializeBool) {
154 // Write snapshot with object content. 158 // Write snapshot with object content.
155 uint8_t* buffer; 159 uint8_t* buffer;
156 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); 160 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
157 const Bool& bool1 = Bool::Handle(Bool::True()); 161 const Bool& bool1 = Bool::Handle(Bool::True());
158 const Bool& bool2 = Bool::Handle(Bool::False()); 162 const Bool& bool2 = Bool::Handle(Bool::False());
159 writer.WriteObject(bool1.raw()); 163 writer.WriteObject(bool1.raw());
160 writer.WriteObject(bool2.raw()); 164 writer.WriteObject(bool2.raw());
161 writer.FinalizeBuffer(); 165 writer.FinalizeBuffer();
162 166
163 // Create a snapshot object using the buffer. 167 // Create a snapshot object using the buffer.
164 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 168 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
165 169
166 // Read object back from the snapshot. 170 // Read object back from the snapshot.
167 SnapshotReader reader(snapshot, Isolate::Current()); 171 SnapshotReader reader(snapshot, Isolate::Current());
168 EXPECT(Bool::True() == reader.ReadObject()); 172 EXPECT(Bool::True() == reader.ReadObject());
169 EXPECT(Bool::False() == reader.ReadObject()); 173 EXPECT(Bool::False() == reader.ReadObject());
170 174
171 // Read object back from the snapshot into a C structure. 175 // Read object back from the snapshot into a C structure.
172 CMessageReader mreader(buffer + Snapshot::kHeaderSize, 176 CMessageReader mreader(buffer + Snapshot::kHeaderSize,
173 writer.BytesWritten(), 177 writer.BytesWritten(),
174 &allocator); 178 &allocator);
175 Dart_CObject* cobject1 = mreader.ReadObject(); 179 Dart_CObject* cobject1 = mreader.ReadObject();
180 EXPECT_NOTNULL(cobject1);
176 EXPECT_EQ(Dart_CObject::kBool, cobject1->type); 181 EXPECT_EQ(Dart_CObject::kBool, cobject1->type);
177 EXPECT_EQ(true, cobject1->value.as_bool); 182 EXPECT_EQ(true, cobject1->value.as_bool);
178 Dart_CObject* cobject2 = mreader.ReadObject(); 183 Dart_CObject* cobject2 = mreader.ReadObject();
184 EXPECT_NOTNULL(cobject2);
179 EXPECT_EQ(Dart_CObject::kBool, cobject2->type); 185 EXPECT_EQ(Dart_CObject::kBool, cobject2->type);
180 EXPECT_EQ(false, cobject2->value.as_bool); 186 EXPECT_EQ(false, cobject2->value.as_bool);
181 free(cobject1); 187 free(cobject1);
182 free(cobject2); 188 free(cobject2);
183 } 189 }
184 190
185 191
186 TEST_CASE(SerializeBigint) { 192 TEST_CASE(SerializeBigint) {
187 // Write snapshot with object content. 193 // Write snapshot with object content.
188 uint8_t* buffer; 194 uint8_t* buffer;
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after
686 for (int i = 0; i < kArrayLength; i++) { 692 for (int i = 0; i < kArrayLength; i++) {
687 Dart_CObject* element = value->value.as_array.values[i]; 693 Dart_CObject* element = value->value.as_array.values[i];
688 EXPECT_EQ(Dart_CObject::kInt32, element->type); 694 EXPECT_EQ(Dart_CObject::kInt32, element->type);
689 EXPECT_EQ(i + 1, element->value.as_int32); 695 EXPECT_EQ(i + 1, element->value.as_int32);
690 free(element); 696 free(element);
691 } 697 }
692 free(value); 698 free(value);
693 } 699 }
694 700
695 701
702 // Helper function to call a top level Dart function, serialize the
703 // result and deserialize the result into a Dart_CObject structure.
704 static Dart_CObject* GetDeserializedDartObject(Dart_Handle lib,
705 const char* dart_function) {
706 Dart_Handle result;
707 result = Dart_InvokeStatic(lib,
708 Dart_NewString(""),
709 Dart_NewString(dart_function),
710 0,
711 NULL);
712 EXPECT_VALID(result);
713
714 // Serialize the list into a message.
715 uint8_t* buffer;
716 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
717 const Object& list = Object::Handle(Api::UnwrapHandle(result));
718 writer.WriteObject(list.raw());
719 writer.FinalizeBuffer();
720
721 // Read object back from the snapshot into a C structure.
722 CMessageReader reader(buffer + Snapshot::kHeaderSize,
723 writer.BytesWritten(),
724 &allocator);
725 Dart_CObject* value = reader.ReadObject();
726 free(buffer);
727 return value;
728 }
729
730
696 UNIT_TEST_CASE(DartGeneratedMessages) { 731 UNIT_TEST_CASE(DartGeneratedMessages) {
697 const int kArrayLength = 10;
698 static const char* kCustomIsolateScriptChars = 732 static const char* kCustomIsolateScriptChars =
699 "getSmi() {\n" 733 "getSmi() {\n"
700 " return 42;\n" 734 " return 42;\n"
701 "}\n" 735 "}\n"
702 "getString() {\n" 736 "getString() {\n"
703 " return \"Hello, world!\";\n" 737 " return \"Hello, world!\";\n"
704 "}\n" 738 "}\n"
705 "getList() {\n" 739 "getList() {\n"
706 " return [1,2,3,4,5,6,7,8,9,10];\n" 740 " return new List(kArrayLength);\n"
707 "}\n"; 741 "}\n";
708 742
709 TestCase::CreateTestIsolate(); 743 TestCase::CreateTestIsolate();
710 Isolate* isolate = Isolate::Current(); 744 Isolate* isolate = Isolate::Current();
711 EXPECT(isolate != NULL); 745 EXPECT(isolate != NULL);
712 Dart_EnterScope(); 746 Dart_EnterScope();
713 747
714 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, 748 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars,
715 NULL); 749 NULL);
716 EXPECT_VALID(lib); 750 EXPECT_VALID(lib);
717 Dart_Handle smi_result; 751 Dart_Handle smi_result;
718 smi_result = Dart_InvokeStatic(lib, 752 smi_result = Dart_InvokeStatic(lib,
719 Dart_NewString(""), 753 Dart_NewString(""),
720 Dart_NewString("getSmi"), 754 Dart_NewString("getSmi"),
721 0, 755 0,
722 NULL); 756 NULL);
723 EXPECT_VALID(smi_result); 757 EXPECT_VALID(smi_result);
724 Dart_Handle string_result; 758 Dart_Handle string_result;
725 string_result = Dart_InvokeStatic(lib, 759 string_result = Dart_InvokeStatic(lib,
726 Dart_NewString(""), 760 Dart_NewString(""),
727 Dart_NewString("getString"), 761 Dart_NewString("getString"),
728 0, 762 0,
729 NULL); 763 NULL);
730 EXPECT_VALID(string_result); 764 EXPECT_VALID(string_result);
731 EXPECT(Dart_IsString(string_result)); 765 EXPECT(Dart_IsString(string_result));
732 Dart_Handle list_result; 766
733 list_result = Dart_InvokeStatic(lib,
734 Dart_NewString(""),
735 Dart_NewString("getList"),
736 0,
737 NULL);
738 EXPECT_VALID(list_result);
739 EXPECT(Dart_IsList(list_result));
740 intptr_t result_len = 0;
741 EXPECT_VALID(Dart_ListLength(list_result, &result_len));
742 EXPECT_EQ(kArrayLength, result_len);
743 { 767 {
744 DARTSCOPE_NOCHECKS(isolate); 768 DARTSCOPE_NOCHECKS(isolate);
745 769
746 { 770 {
747 uint8_t* buffer; 771 uint8_t* buffer;
748 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); 772 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
749 Smi& smi = Smi::Handle(); 773 Smi& smi = Smi::Handle();
750 smi ^= Api::UnwrapHandle(smi_result); 774 smi ^= Api::UnwrapHandle(smi_result);
751 writer.WriteObject(smi.raw()); 775 writer.WriteObject(smi.raw());
752 writer.FinalizeBuffer(); 776 writer.FinalizeBuffer();
753 777
754 // Read object back from the snapshot into a C structure. 778 // Read object back from the snapshot into a C structure.
755 CMessageReader mreader(buffer + Snapshot::kHeaderSize, 779 CMessageReader mreader(buffer + Snapshot::kHeaderSize,
756 writer.BytesWritten(), 780 writer.BytesWritten(),
757 &allocator); 781 &allocator);
758 Dart_CObject* value = mreader.ReadObject(); 782 Dart_CObject* value = mreader.ReadObject();
783 EXPECT_NOTNULL(value);
759 EXPECT_EQ(Dart_CObject::kInt32, value->type); 784 EXPECT_EQ(Dart_CObject::kInt32, value->type);
760 EXPECT_EQ(42, value->value.as_int32); 785 EXPECT_EQ(42, value->value.as_int32);
761 free(value); 786 free(value);
762 free(buffer); 787 free(buffer);
763 } 788 }
764 { 789 {
765 uint8_t* buffer; 790 uint8_t* buffer;
766 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); 791 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
767 String& str = String::Handle(); 792 String& str = String::Handle();
768 str ^= Api::UnwrapHandle(string_result); 793 str ^= Api::UnwrapHandle(string_result);
769 writer.WriteObject(str.raw()); 794 writer.WriteObject(str.raw());
770 writer.FinalizeBuffer(); 795 writer.FinalizeBuffer();
771 796
772 // Read object back from the snapshot into a C structure. 797 // Read object back from the snapshot into a C structure.
773 CMessageReader mreader(buffer + Snapshot::kHeaderSize, 798 CMessageReader mreader(buffer + Snapshot::kHeaderSize,
774 writer.BytesWritten(), 799 writer.BytesWritten(),
775 &allocator); 800 &allocator);
776 Dart_CObject* value = mreader.ReadObject(); 801 Dart_CObject* value = mreader.ReadObject();
802 EXPECT_NOTNULL(value);
777 EXPECT_EQ(Dart_CObject::kString, value->type); 803 EXPECT_EQ(Dart_CObject::kString, value->type);
778 EXPECT_STREQ("Hello, world!", value->value.as_string); 804 EXPECT_STREQ("Hello, world!", value->value.as_string);
779 free(value); 805 free(value);
780 free(buffer); 806 free(buffer);
781 } 807 }
782 {
783 uint8_t* buffer;
784 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
785 const Object& list = Object::Handle(Api::UnwrapHandle(list_result));
786 writer.WriteObject(list.raw());
787 writer.FinalizeBuffer();
788 // TODO(sgjesse): Make this work!
789 free(buffer);
790 }
791 } 808 }
792 Dart_ExitScope(); 809 Dart_ExitScope();
810 Dart_ShutdownIsolate();
811 }
812
813
814 UNIT_TEST_CASE(DartGeneratedListMessages) {
815 const int kArrayLength = 10;
816 static const char* kScriptChars =
817 "final int kArrayLength = 10;\n"
818 "getList() {\n"
819 " return new List(kArrayLength);\n"
820 "}\n"
821 "getIntList() {\n"
822 " var list = new List<int>(kArrayLength);\n"
823 " for (var i = 0; i < kArrayLength; i++) list[i] = i;\n"
824 " return list;\n"
825 "}\n"
826 "getStringList() {\n"
827 " var list = new List<String>(kArrayLength);\n"
828 " for (var i = 0; i < kArrayLength; i++) list[i] = i.toString();\n"
829 " return list;\n"
830 "}\n"
831 "getMixedList() {\n"
832 " var list = new List(kArrayLength);\n"
833 " list[0] = 0;\n"
834 " list[1] = '1';\n"
835 " list[2] = 2.2;\n"
836 " list[3] = true;\n"
837 " return list;\n"
838 "}\n";
839
840 TestCase::CreateTestIsolate();
841 Isolate* isolate = Isolate::Current();
842 EXPECT(isolate != NULL);
843 Dart_EnterScope();
844
845 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
846 EXPECT_VALID(lib);
847
848 {
849 DARTSCOPE_NOCHECKS(isolate);
850
851 {
852 // Generate a list of nulls from Dart code.
853 Dart_CObject* value = GetDeserializedDartObject(lib, "getList");
854 EXPECT_NOTNULL(value);
855 EXPECT_EQ(Dart_CObject::kArray, value->type);
856 EXPECT_EQ(kArrayLength, value->value.as_array.length);
857 for (int i = 0; i < kArrayLength; i++) {
858 EXPECT_EQ(Dart_CObject::kNull, value->value.as_array.values[i]->type);
859 free(value->value.as_array.values[i]);
860 }
861 free(value);
862 }
863 {
864 // Generate a list of ints from Dart code.
865 Dart_CObject* value = GetDeserializedDartObject(lib, "getIntList");
866 EXPECT_NOTNULL(value);
867 EXPECT_EQ(Dart_CObject::kArray, value->type);
868 EXPECT_EQ(kArrayLength, value->value.as_array.length);
869 for (int i = 0; i < kArrayLength; i++) {
870 EXPECT_EQ(Dart_CObject::kInt32, value->value.as_array.values[i]->type);
871 EXPECT_EQ(i, value->value.as_array.values[i]->value.as_int32);
872 free(value->value.as_array.values[i]);
873 }
874 free(value);
875 }
876 {
877 // Generate a list of strings from Dart code.
878 Dart_CObject* value = GetDeserializedDartObject(lib, "getStringList");
879 EXPECT_NOTNULL(value);
880 EXPECT_EQ(Dart_CObject::kArray, value->type);
881 EXPECT_EQ(kArrayLength, value->value.as_array.length);
882 for (int i = 0; i < kArrayLength; i++) {
883 EXPECT_EQ(Dart_CObject::kString, value->value.as_array.values[i]->type);
884 char buffer[3];
885 snprintf(buffer, sizeof(buffer), "%d", i);
886 EXPECT_STREQ(buffer, value->value.as_array.values[i]->value.as_string);
887 free(value->value.as_array.values[i]);
888 }
889 free(value);
890 }
891 {
892 // Generate a list of objects of different types from Dart code.
893 Dart_CObject* value = GetDeserializedDartObject(lib, "getMixedList");
894 EXPECT_NOTNULL(value);
895 EXPECT_EQ(Dart_CObject::kArray, value->type);
896 EXPECT_EQ(kArrayLength, value->value.as_array.length);
897
898 EXPECT_EQ(Dart_CObject::kInt32, value->value.as_array.values[0]->type);
899 EXPECT_EQ(0, value->value.as_array.values[0]->value.as_int32);
900 EXPECT_EQ(Dart_CObject::kString, value->value.as_array.values[1]->type);
901 EXPECT_STREQ("1", value->value.as_array.values[1]->value.as_string);
902 EXPECT_EQ(Dart_CObject::kDouble, value->value.as_array.values[2]->type);
903 EXPECT_EQ(2.2, value->value.as_array.values[2]->value.as_double);
904 EXPECT_EQ(Dart_CObject::kBool, value->value.as_array.values[3]->type);
905 EXPECT_EQ(true, value->value.as_array.values[3]->value.as_bool);
906
907 for (int i = 0; i < kArrayLength; i++) {
908 if (i > 3) {
909 EXPECT_EQ(Dart_CObject::kNull, value->value.as_array.values[i]->type);
910 }
911 free(value->value.as_array.values[i]);
912 }
913 free(value);
914 }
915 }
916 Dart_ExitScope();
917 Dart_ShutdownIsolate();
918 }
919
920
921 UNIT_TEST_CASE(DartGeneratedListMessagesWithBackref) {
922 const int kArrayLength = 10;
923 static const char* kScriptChars =
924 "final int kArrayLength = 10;\n"
925 "getStringList() {\n"
926 " var s = 'Hello, world!';\n"
927 " var list = new List<String>(kArrayLength);\n"
928 " for (var i = 0; i < kArrayLength; i++) list[i] = s;\n"
929 " return list;\n"
930 "}\n"
931 "getDoubleList() {\n"
932 " var d = 3.14;\n"
933 " var list = new List<double>(kArrayLength);\n"
934 " for (var i = 0; i < kArrayLength; i++) list[i] = d;\n"
935 " return list;\n"
936 "}\n"
937 "getMixedList() {\n"
938 " var list = new List(kArrayLength);\n"
939 " for (var i = 0; i < kArrayLength; i++) {\n"
940 " list[i] = ((i % 2) == 0) ? 'A' : 2.72;\n"
941 " }\n"
942 " return list;\n"
943 "}\n"
944 "getSelfRefList() {\n"
945 " var list = new List(kArrayLength);\n"
946 " for (var i = 0; i < kArrayLength; i++) {\n"
947 " list[i] = list;\n"
948 " }\n"
949 " return list;\n"
950 "}\n";
951
952 TestCase::CreateTestIsolate();
953 Isolate* isolate = Isolate::Current();
954 EXPECT(isolate != NULL);
955 Dart_EnterScope();
956
957 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
958 EXPECT_VALID(lib);
959
960 {
961 DARTSCOPE_NOCHECKS(isolate);
962
963 {
964 // Generate a list of strings from Dart code.
965 Dart_CObject* object = GetDeserializedDartObject(lib, "getStringList");
966 EXPECT_NOTNULL(object);
967 EXPECT_EQ(Dart_CObject::kArray, object->type);
968 EXPECT_EQ(kArrayLength, object->value.as_array.length);
969 for (int i = 0; i < kArrayLength; i++) {
970 Dart_CObject* element = object->value.as_array.values[i];
971 EXPECT_EQ(object->value.as_array.values[0], element);
972 EXPECT_EQ(Dart_CObject::kString, element->type);
973 EXPECT_STREQ("Hello, world!", element->value.as_string);
974 }
975 free(object->value.as_array.values[0]);
976 free(object);
977 }
978 {
979 // Generate a list of doubles from Dart code.
980 Dart_CObject* object = GetDeserializedDartObject(lib, "getDoubleList");
981 EXPECT_NOTNULL(object);
982 EXPECT_EQ(Dart_CObject::kArray, object->type);
983 EXPECT_EQ(kArrayLength, object->value.as_array.length);
984 for (int i = 0; i < kArrayLength; i++) {
985 Dart_CObject* element = object->value.as_array.values[i];
986 EXPECT_EQ(object->value.as_array.values[0], element);
987 EXPECT_EQ(Dart_CObject::kDouble, element->type);
988 EXPECT_EQ(3.14, element->value.as_double);
989 }
990 free(object->value.as_array.values[0]);
991 free(object);
992 }
993 {
994 // Generate a list of objects of different types from Dart code.
995 Dart_CObject* object = GetDeserializedDartObject(lib, "getMixedList");
996 EXPECT_NOTNULL(object);
997 EXPECT_EQ(Dart_CObject::kArray, object->type);
998 EXPECT_EQ(kArrayLength, object->value.as_array.length);
999 for (int i = 0; i < kArrayLength; i++) {
1000 Dart_CObject* element = object->value.as_array.values[i];
1001 if ((i % 2) == 0) {
1002 EXPECT_EQ(object->value.as_array.values[0], element);
1003 EXPECT_EQ(Dart_CObject::kString, element->type);
1004 EXPECT_STREQ("A", element->value.as_string);
1005 } else {
1006 EXPECT_EQ(object->value.as_array.values[1], element);
1007 EXPECT_EQ(Dart_CObject::kDouble, element->type);
1008 EXPECT_STREQ(2.72, element->value.as_double);
1009 }
1010 }
1011 free(object->value.as_array.values[0]);
1012 free(object->value.as_array.values[1]);
1013 free(object);
1014 }
1015 {
1016 // Generate a list of objects of different types from Dart code.
1017 Dart_CObject* object = GetDeserializedDartObject(lib, "getSelfRefList");
1018 EXPECT_NOTNULL(object);
1019 EXPECT_EQ(Dart_CObject::kArray, object->type);
1020 EXPECT_EQ(kArrayLength, object->value.as_array.length);
1021 for (int i = 0; i < kArrayLength; i++) {
1022 Dart_CObject* element = object->value.as_array.values[i];
1023 EXPECT_EQ(Dart_CObject::kArray, element->type);
1024 EXPECT_EQ(object, element);
1025 }
1026 free(object);
1027 }
1028 }
1029 Dart_ExitScope();
793 Dart_ShutdownIsolate(); 1030 Dart_ShutdownIsolate();
794 } 1031 }
795 1032
796 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 1033 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
797 1034
798 } // namespace dart 1035 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/snapshot.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698