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

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

Issue 380333002: Add VM class for Map/LinkedHashMap. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 4 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/object.h ('k') | runtime/vm/object_store.h » ('j') | 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 "vm/object.h" 5 #include "vm/object.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/assembler.h" 9 #include "vm/assembler.h"
10 #include "vm/cpu.h" 10 #include "vm/cpu.h"
(...skipping 1137 matching lines...) Expand 10 before | Expand all | Expand 10 after
1148 lib.Register(); 1148 lib.Register();
1149 isolate->object_store()->set_bootstrap_library(ObjectStore::kMirrors, 1149 isolate->object_store()->set_bootstrap_library(ObjectStore::kMirrors,
1150 lib); 1150 lib);
1151 } 1151 }
1152 ASSERT(!lib.IsNull()); 1152 ASSERT(!lib.IsNull());
1153 ASSERT(lib.raw() == Library::MirrorsLibrary()); 1153 ASSERT(lib.raw() == Library::MirrorsLibrary());
1154 1154
1155 cls = Class::New<MirrorReference>(); 1155 cls = Class::New<MirrorReference>();
1156 RegisterPrivateClass(cls, Symbols::_MirrorReference(), lib); 1156 RegisterPrivateClass(cls, Symbols::_MirrorReference(), lib);
1157 1157
1158 // Pre-register the collection library so we can place the vm class
1159 // LinkedHashMap there rather than the core library.
1160 lib = Library::LookupLibrary(Symbols::DartCollection());
1161 if (lib.IsNull()) {
1162 lib = Library::NewLibraryHelper(Symbols::DartCollection(), true);
1163 lib.SetLoadRequested();
1164 lib.Register();
1165 isolate->object_store()->set_bootstrap_library(ObjectStore::kCollection,
1166 lib);
1167 }
1168 ASSERT(!lib.IsNull());
1169 ASSERT(lib.raw() == Library::CollectionLibrary());
1170
1171 cls = Class::New<LinkedHashMap>();
1172 object_store->set_linked_hash_map_class(cls);
1173 cls.set_type_arguments_field_offset(LinkedHashMap::type_arguments_offset());
1174 cls.set_num_type_arguments(2);
1175 cls.set_num_own_type_arguments(2);
1176 RegisterPrivateClass(cls, Symbols::_LinkedHashMap(), lib);
1177 pending_classes.Add(cls);
1178
1158 // Pre-register the profiler library so we can place the vm class 1179 // Pre-register the profiler library so we can place the vm class
1159 // UserTag there rather than the core library. 1180 // UserTag there rather than the core library.
1160 lib = Library::LookupLibrary(Symbols::DartProfiler()); 1181 lib = Library::LookupLibrary(Symbols::DartProfiler());
1161 if (lib.IsNull()) { 1182 if (lib.IsNull()) {
1162 lib = Library::NewLibraryHelper(Symbols::DartProfiler(), true); 1183 lib = Library::NewLibraryHelper(Symbols::DartProfiler(), true);
1163 lib.SetLoadRequested(); 1184 lib.SetLoadRequested();
1164 lib.Register(); 1185 lib.Register();
1165 isolate->object_store()->set_bootstrap_library(ObjectStore::kProfiler, 1186 isolate->object_store()->set_bootstrap_library(ObjectStore::kProfiler,
1166 lib); 1187 lib);
1167 } 1188 }
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
1426 1447
1427 cls = Class::New<Array>(); 1448 cls = Class::New<Array>();
1428 object_store->set_array_class(cls); 1449 object_store->set_array_class(cls);
1429 1450
1430 cls = Class::New<Array>(kImmutableArrayCid); 1451 cls = Class::New<Array>(kImmutableArrayCid);
1431 object_store->set_immutable_array_class(cls); 1452 object_store->set_immutable_array_class(cls);
1432 1453
1433 cls = Class::New<GrowableObjectArray>(); 1454 cls = Class::New<GrowableObjectArray>();
1434 object_store->set_growable_object_array_class(cls); 1455 object_store->set_growable_object_array_class(cls);
1435 1456
1457 cls = Class::New<LinkedHashMap>();
1458 object_store->set_linked_hash_map_class(cls);
1459
1436 cls = Class::New<Float32x4>(); 1460 cls = Class::New<Float32x4>();
1437 object_store->set_float32x4_class(cls); 1461 object_store->set_float32x4_class(cls);
1438 1462
1439 cls = Class::New<Int32x4>(); 1463 cls = Class::New<Int32x4>();
1440 object_store->set_int32x4_class(cls); 1464 object_store->set_int32x4_class(cls);
1441 1465
1442 cls = Class::New<Float64x2>(); 1466 cls = Class::New<Float64x2>();
1443 object_store->set_float64x2_class(cls); 1467 object_store->set_float64x2_class(cls);
1444 1468
1445 #define REGISTER_TYPED_DATA_CLASS(clazz) \ 1469 #define REGISTER_TYPED_DATA_CLASS(clazz) \
(...skipping 16646 matching lines...) Expand 10 before | Expand all | Expand 10 after
18092 JSONObject jselement(&jsarr); 18116 JSONObject jselement(&jsarr);
18093 jselement.AddProperty("index", index); 18117 jselement.AddProperty("index", index);
18094 18118
18095 Object& element = Object::Handle(At(index)); 18119 Object& element = Object::Handle(At(index));
18096 jselement.AddProperty("value", element); 18120 jselement.AddProperty("value", element);
18097 } 18121 }
18098 } 18122 }
18099 } 18123 }
18100 18124
18101 18125
18126 // Equivalent to Dart's operator "==" and hashCode.
18127 class DefaultHashTraits {
18128 public:
18129 static bool IsMatch(const Object& a, const Object& b) {
18130 if (a.IsNull() || b.IsNull()) {
18131 return (a.IsNull() && b.IsNull());
18132 } else {
18133 return Instance::Cast(a).OperatorEquals(Instance::Cast(b));
18134 }
18135 }
18136 static uword Hash(const Object& obj) {
18137 if (obj.IsNull()) {
18138 return 0;
18139 }
18140 // TODO(koda): Ensure VM classes only produce Smi hash codes, and remove
18141 // non-Smi cases once Dart-side implementation is complete.
18142 Isolate* isolate = Isolate::Current();
18143 REUSABLE_INSTANCE_HANDLESCOPE(isolate);
18144 Instance& hash_code = isolate->InstanceHandle();
18145 hash_code ^= Instance::Cast(obj).HashCode();
18146 if (hash_code.IsSmi()) {
18147 // May waste some bits on 64-bit, to ensure consistency with non-Smi case.
18148 return static_cast<uword>(Smi::Cast(hash_code).Value() & 0xFFFFFFFF);
18149 } else if (hash_code.IsInteger()) {
18150 return static_cast<uword>(
18151 Integer::Cast(hash_code).AsTruncatedUint32Value());
18152 } else {
18153 return 0;
18154 }
18155 }
18156 };
18157 typedef EnumIndexHashMap<DefaultHashTraits> EnumIndexDefaultMap;
18158
18159
18160 intptr_t LinkedHashMap::Length() const {
18161 EnumIndexDefaultMap map(Array::Handle(data()));
18162 intptr_t result = map.NumOccupied();
18163 {
18164 RawArray* array = map.Release();
18165 ASSERT(array == data());
18166 }
18167 return result;
18168 }
18169
18170
18171 void LinkedHashMap::InsertOrUpdate(const Object& key,
18172 const Object& value) const {
18173 ASSERT(!IsNull());
18174 EnumIndexDefaultMap map(Array::Handle(data()));
18175 if (!map.UpdateOrInsert(key, value)) {
18176 SetModified();
18177 }
18178 StorePointer(&raw_ptr()->data_, map.Release());
18179 }
18180
18181
18182 RawObject* LinkedHashMap::LookUp(const Object& key) const {
18183 ASSERT(!IsNull());
18184 EnumIndexDefaultMap map(Array::Handle(data()));
18185 const Object& result = Object::Handle(map.GetOrNull(key));
18186 {
18187 RawArray* array = map.Release();
18188 ASSERT(array == data());
18189 }
18190 return result.raw();
18191 }
18192
18193
18194 bool LinkedHashMap::Contains(const Object& key) const {
18195 ASSERT(!IsNull());
18196 EnumIndexDefaultMap map(Array::Handle(data()));
18197 bool result = map.ContainsKey(key);
18198 {
18199 RawArray* array = map.Release();
18200 ASSERT(array == data());
18201 }
18202 return result;
18203 }
18204
18205
18206 RawObject* LinkedHashMap::Remove(const Object& key) const {
18207 ASSERT(!IsNull());
18208 EnumIndexDefaultMap map(Array::Handle(data()));
18209 // TODO(koda): Make 'Remove' also return the old value.
18210 const Object& result = Object::Handle(map.GetOrNull(key));
18211 if (map.Remove(key)) {
18212 SetModified();
18213 }
18214 StorePointer(&raw_ptr()->data_, map.Release());
18215 return result.raw();
18216 }
18217
18218
18219 void LinkedHashMap::Clear() const {
18220 ASSERT(!IsNull());
18221 if (Length() != 0) {
18222 EnumIndexDefaultMap map(Array::Handle(data()));
18223 map.Initialize();
18224 SetModified();
18225 StorePointer(&raw_ptr()->data_, map.Release());
18226 }
18227 }
18228
18229
18230 RawArray* LinkedHashMap::ToArray() const {
18231 EnumIndexDefaultMap map(Array::Handle(data()));
18232 const Array& result = Array::Handle(HashTables::ToArray(map, true));
18233 RawArray* array = map.Release();
18234 ASSERT(array == data());
18235 return result.raw();
18236 }
18237
18238
18239 void LinkedHashMap::SetModified() const {
18240 StorePointer(&raw_ptr()->cme_mark_, Instance::null());
18241 }
18242
18243
18244 RawInstance* LinkedHashMap::GetModificationMark(bool create) const {
18245 if (create && raw_ptr()->cme_mark_ == Instance::null()) {
18246 Isolate* isolate = Isolate::Current();
18247 const Class& object_class =
18248 Class::Handle(isolate, isolate->object_store()->object_class());
18249 const Instance& current =
18250 Instance::Handle(isolate, Instance::New(object_class));
18251 StorePointer(&raw_ptr()->cme_mark_, current.raw());
18252 }
18253 return raw_ptr()->cme_mark_;
18254 }
18255
18256
18257 RawLinkedHashMap* LinkedHashMap::New(Heap::Space space) {
18258 ASSERT(Isolate::Current()->object_store()->linked_hash_map_class()
18259 != Class::null());
18260 static const intptr_t kInitialCapacity = 4;
18261 const Array& data =
18262 Array::Handle(HashTables::New<EnumIndexDefaultMap>(kInitialCapacity,
18263 space));
18264 LinkedHashMap& result = LinkedHashMap::Handle();
18265 {
18266 RawObject* raw = Object::Allocate(LinkedHashMap::kClassId,
18267 LinkedHashMap::InstanceSize(),
18268 space);
18269 NoGCScope no_gc;
18270 result ^= raw;
18271 result.SetData(data);
18272 result.SetModified();
18273 }
18274 return result.raw();
18275 }
18276
18277
18278 const char* LinkedHashMap::ToCString() const {
18279 // TODO(koda): Print key/value pairs.
18280 return "_LinkedHashMap";
18281 }
18282
18283
18284 void LinkedHashMap::PrintJSONImpl(JSONStream* stream, bool ref) const {
18285 // TODO(koda): Print key/value pairs.
18286 Instance::PrintJSONImpl(stream, ref);
18287 }
18288
18289
18102 RawFloat32x4* Float32x4::New(float v0, float v1, float v2, float v3, 18290 RawFloat32x4* Float32x4::New(float v0, float v1, float v2, float v3,
18103 Heap::Space space) { 18291 Heap::Space space) {
18104 ASSERT(Isolate::Current()->object_store()->float32x4_class() != 18292 ASSERT(Isolate::Current()->object_store()->float32x4_class() !=
18105 Class::null()); 18293 Class::null());
18106 Float32x4& result = Float32x4::Handle(); 18294 Float32x4& result = Float32x4::Handle();
18107 { 18295 {
18108 RawObject* raw = Object::Allocate(Float32x4::kClassId, 18296 RawObject* raw = Object::Allocate(Float32x4::kClassId,
18109 Float32x4::InstanceSize(), 18297 Float32x4::InstanceSize(),
18110 space); 18298 space);
18111 NoGCScope no_gc; 18299 NoGCScope no_gc;
(...skipping 1052 matching lines...) Expand 10 before | Expand all | Expand 10 after
19164 return tag_label.ToCString(); 19352 return tag_label.ToCString();
19165 } 19353 }
19166 19354
19167 19355
19168 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const { 19356 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const {
19169 Instance::PrintJSONImpl(stream, ref); 19357 Instance::PrintJSONImpl(stream, ref);
19170 } 19358 }
19171 19359
19172 19360
19173 } // namespace dart 19361 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/object.h ('k') | runtime/vm/object_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698