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

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

Issue 9195031: Add ByteArray interface and provide internal and external implementations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: address remaining review comments Created 8 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 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 "platform/assert.h" 7 #include "platform/assert.h"
8 #include "vm/assembler.h" 8 #include "vm/assembler.h"
9 #include "vm/bigint_operations.h" 9 #include "vm/bigint_operations.h"
10 #include "vm/bootstrap.h" 10 #include "vm/bootstrap.h"
(...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 532
533 cls = Class::New<JSRegExp>(); 533 cls = Class::New<JSRegExp>();
534 object_store->set_jsregexp_class(cls); 534 object_store->set_jsregexp_class(cls);
535 RegisterClass(cls, "JSSyntaxRegExp", impl_script, core_impl_lib); 535 RegisterClass(cls, "JSSyntaxRegExp", impl_script, core_impl_lib);
536 pending_classes.Add(&Class::ZoneHandle(cls.raw())); 536 pending_classes.Add(&Class::ZoneHandle(cls.raw()));
537 537
538 // Initialize the base interfaces used by the core VM classes. 538 // Initialize the base interfaces used by the core VM classes.
539 const Script& script = Script::Handle(Bootstrap::LoadScript()); 539 const Script& script = Script::Handle(Bootstrap::LoadScript());
540 540
541 // Allocate and initialize the Object class and type. 541 // Allocate and initialize the Object class and type.
542 // The Object and ByteBuffer classes are the only pre-allocated 542 // The Object and ExternalByteArray classes are the only pre-allocated
543 // non-interface classes in the core library. 543 // non-interface classes in the core library.
544 cls = Class::New<Instance>(); 544 cls = Class::New<Instance>();
545 object_store->set_object_class(cls); 545 object_store->set_object_class(cls);
546 cls.set_name(String::Handle(String::NewSymbol("Object"))); 546 cls.set_name(String::Handle(String::NewSymbol("Object")));
547 cls.set_script(script); 547 cls.set_script(script);
548 core_lib.AddClass(cls); 548 core_lib.AddClass(cls);
549 pending_classes.Add(&Class::ZoneHandle(cls.raw())); 549 pending_classes.Add(&Class::ZoneHandle(cls.raw()));
550 type = Type::NewNonParameterizedType(cls); 550 type = Type::NewNonParameterizedType(cls);
551 object_store->set_object_type(type); 551 object_store->set_object_type(type);
552 552
553 cls = Class::New<ByteBuffer>(); 553 cls = Class::New<InternalByteArray>();
554 object_store->set_byte_buffer_class(cls); 554 object_store->set_internal_byte_array_class(cls);
555 cls.set_name(String::Handle(String::NewSymbol("ByteBuffer"))); 555 cls.set_name(String::Handle(core_lib.PrivateName("_InternalByteArray")));
556 cls.set_script(script); 556 cls.set_script(script);
557 core_lib.AddClass(cls); 557 core_lib.AddClass(cls);
558 558
559 cls = Class::New<ExternalByteArray>();
560 object_store->set_external_byte_array_class(cls);
561 cls.set_name(String::Handle(core_lib.PrivateName("_ExternalByteArray")));
562 cls.set_script(script);
563 core_lib.AddClass(cls);
564
559 // Set the super type of class Stacktrace to Object type so that the 565 // Set the super type of class Stacktrace to Object type so that the
560 // 'toString' method is implemented. 566 // 'toString' method is implemented.
561 cls = object_store->stacktrace_class(); 567 cls = object_store->stacktrace_class();
562 cls.set_super_type(type); 568 cls.set_super_type(type);
563 569
564 cls = CreateAndRegisterInterface("Function", script, core_lib); 570 cls = CreateAndRegisterInterface("Function", script, core_lib);
565 pending_classes.Add(&Class::ZoneHandle(cls.raw())); 571 pending_classes.Add(&Class::ZoneHandle(cls.raw()));
566 type = Type::NewNonParameterizedType(cls); 572 type = Type::NewNonParameterizedType(cls);
567 object_store->set_function_interface(type); 573 object_store->set_function_interface(type);
568 574
(...skipping 20 matching lines...) Expand all
589 cls = CreateAndRegisterInterface("bool", script, core_lib); 595 cls = CreateAndRegisterInterface("bool", script, core_lib);
590 pending_classes.Add(&Class::ZoneHandle(cls.raw())); 596 pending_classes.Add(&Class::ZoneHandle(cls.raw()));
591 type = Type::NewNonParameterizedType(cls); 597 type = Type::NewNonParameterizedType(cls);
592 object_store->set_bool_interface(type); 598 object_store->set_bool_interface(type);
593 599
594 cls = CreateAndRegisterInterface("List", script, core_lib); 600 cls = CreateAndRegisterInterface("List", script, core_lib);
595 pending_classes.Add(&Class::ZoneHandle(cls.raw())); 601 pending_classes.Add(&Class::ZoneHandle(cls.raw()));
596 type = Type::NewNonParameterizedType(cls); 602 type = Type::NewNonParameterizedType(cls);
597 object_store->set_list_interface(type); 603 object_store->set_list_interface(type);
598 604
605 cls = CreateAndRegisterInterface("ByteArray", script, core_lib);
606 pending_classes.Add(&Class::ZoneHandle(cls.raw()));
607 type = Type::NewNonParameterizedType(cls);
608 object_store->set_byte_array_interface(type);
609
599 // The classes 'Null' and 'void' are not registered in the class dictionary, 610 // The classes 'Null' and 'void' are not registered in the class dictionary,
600 // because their names are reserved keywords. Their names are not heap 611 // because their names are reserved keywords. Their names are not heap
601 // allocated, because the classes reside in the VM isolate. 612 // allocated, because the classes reside in the VM isolate.
602 // The corresponding types are stored in the object store. 613 // The corresponding types are stored in the object store.
603 cls = null_class_; 614 cls = null_class_;
604 type = Type::NewNonParameterizedType(cls); 615 type = Type::NewNonParameterizedType(cls);
605 object_store->set_null_type(type); 616 object_store->set_null_type(type);
606 617
607 cls = void_class_; 618 cls = void_class_;
608 type = Type::NewNonParameterizedType(cls); 619 type = Type::NewNonParameterizedType(cls);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
660 cls = Class::New<Array>(); 671 cls = Class::New<Array>();
661 object_store->set_array_class(cls); 672 object_store->set_array_class(cls);
662 673
663 Array& empty_array = Array::Handle(); 674 Array& empty_array = Array::Handle();
664 empty_array = Array::New(0); 675 empty_array = Array::New(0);
665 object_store->set_empty_array(empty_array); 676 object_store->set_empty_array(empty_array);
666 677
667 cls = Class::New<ImmutableArray>(); 678 cls = Class::New<ImmutableArray>();
668 object_store->set_immutable_array_class(cls); 679 object_store->set_immutable_array_class(cls);
669 680
670 cls = Class::New<ByteBuffer>(); 681 cls = Class::New<InternalByteArray>();
671 object_store->set_byte_buffer_class(cls); 682 object_store->set_internal_byte_array_class(cls);
683
684 cls = Class::New<ExternalByteArray>();
685 object_store->set_external_byte_array_class(cls);
672 686
673 cls = Class::New<Instance>(); 687 cls = Class::New<Instance>();
674 object_store->set_object_class(cls); 688 object_store->set_object_class(cls);
675 689
676 cls = Class::New<Smi>(); 690 cls = Class::New<Smi>();
677 object_store->set_smi_class(cls); 691 object_store->set_smi_class(cls);
678 692
679 cls = Class::New<Mint>(); 693 cls = Class::New<Mint>();
680 object_store->set_mint_class(cls); 694 object_store->set_mint_class(cls);
681 695
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after
1219 return object_store->external_four_byte_string_class(); 1233 return object_store->external_four_byte_string_class();
1220 case kBool: 1234 case kBool:
1221 ASSERT(object_store->bool_class() != Class::null()); 1235 ASSERT(object_store->bool_class() != Class::null());
1222 return object_store->bool_class(); 1236 return object_store->bool_class();
1223 case kArray: 1237 case kArray:
1224 ASSERT(object_store->array_class() != Class::null()); 1238 ASSERT(object_store->array_class() != Class::null());
1225 return object_store->array_class(); 1239 return object_store->array_class();
1226 case kImmutableArray: 1240 case kImmutableArray:
1227 ASSERT(object_store->immutable_array_class() != Class::null()); 1241 ASSERT(object_store->immutable_array_class() != Class::null());
1228 return object_store->immutable_array_class(); 1242 return object_store->immutable_array_class();
1229 case kByteBuffer: 1243 case kInternalByteArray:
1230 ASSERT(object_store->byte_buffer_class() != Class::null()); 1244 ASSERT(object_store->internal_byte_array_class() != Class::null());
1231 return object_store->byte_buffer_class(); 1245 return object_store->internal_byte_array_class();
1246 case kExternalByteArray:
1247 ASSERT(object_store->external_byte_array_class() != Class::null());
1248 return object_store->external_byte_array_class();
1232 case kStacktrace: 1249 case kStacktrace:
1233 ASSERT(object_store->stacktrace_class() != Class::null()); 1250 ASSERT(object_store->stacktrace_class() != Class::null());
1234 return object_store->stacktrace_class(); 1251 return object_store->stacktrace_class();
1235 case kJSRegExp: 1252 case kJSRegExp:
1236 ASSERT(object_store->jsregexp_class() != Class::null()); 1253 ASSERT(object_store->jsregexp_class() != Class::null());
1237 return object_store->jsregexp_class(); 1254 return object_store->jsregexp_class();
1238 case kClosure: 1255 case kClosure:
1239 return Class::New<Closure>(); 1256 return Class::New<Closure>();
1240 case kInstance: 1257 case kInstance:
1241 return Class::New<Instance>(); 1258 return Class::New<Instance>();
(...skipping 3303 matching lines...) Expand 10 before | Expand all | Expand 10 after
4545 lib_key = lib_url.Hash(); 4562 lib_key = lib_url.Hash();
4546 if (lib_key == key) { 4563 if (lib_key == key) {
4547 return true; 4564 return true;
4548 } 4565 }
4549 lib = lib.next_registered(); 4566 lib = lib.next_registered();
4550 } 4567 }
4551 return false; 4568 return false;
4552 } 4569 }
4553 4570
4554 4571
4572 RawString* Library::PrivateName(const char* name) {
4573 ASSERT(name[0] == '_');
4574 ASSERT(strchr(name, '@') == NULL);
4575 String& str = String::Handle();
4576 str = String::New(name);
4577 str = String::Concat(str, String::Handle(this->private_key()));
4578 str = String::NewSymbol(str);
4579 return str.raw();
4580 }
4581
4582
4555 void Library::Register() const { 4583 void Library::Register() const {
4556 ASSERT(Library::LookupLibrary(String::Handle(url())) == Library::null()); 4584 ASSERT(Library::LookupLibrary(String::Handle(url())) == Library::null());
4557 raw_ptr()->next_registered_ = 4585 raw_ptr()->next_registered_ =
4558 Isolate::Current()->object_store()->registered_libraries(); 4586 Isolate::Current()->object_store()->registered_libraries();
4559 Isolate::Current()->object_store()->set_registered_libraries(*this); 4587 Isolate::Current()->object_store()->set_registered_libraries(*this);
4560 } 4588 }
4561 4589
4562 4590
4563 RawLibrary* Library::CoreLibrary() { 4591 RawLibrary* Library::CoreLibrary() {
4564 return Isolate::Current()->object_store()->core_library(); 4592 return Isolate::Current()->object_store()->core_library();
(...skipping 2861 matching lines...) Expand 10 before | Expand all | Expand 10 after
7426 Class& cls = Class::Handle(object_store->immutable_array_class()); 7454 Class& cls = Class::Handle(object_store->immutable_array_class());
7427 return reinterpret_cast<RawImmutableArray*>(Array::New(cls, len, space)); 7455 return reinterpret_cast<RawImmutableArray*>(Array::New(cls, len, space));
7428 } 7456 }
7429 7457
7430 7458
7431 const char* ImmutableArray::ToCString() const { 7459 const char* ImmutableArray::ToCString() const {
7432 return "ImmutableArray"; 7460 return "ImmutableArray";
7433 } 7461 }
7434 7462
7435 7463
7436 RawByteBuffer* ByteBuffer::New(uint8_t* data, 7464 intptr_t ByteArray::Length() const {
7437 intptr_t len, 7465 // ByteArray is an abstract class.
7438 Heap::Space space) { 7466 UNREACHABLE();
7467 return 0;
7468 }
7469
7470
7471 const char* ByteArray::ToCString() const {
7472 // ByteArray is an abstract class.
7473 UNREACHABLE();
7474 return "ByteArray";
7475 }
7476
7477
7478 RawInternalByteArray* InternalByteArray::New(intptr_t len,
7479 Heap::Space space) {
7439 Isolate* isolate = Isolate::Current(); 7480 Isolate* isolate = Isolate::Current();
7440 const Class& byte_buffer_class = 7481 const Class& internal_byte_array_class =
7441 Class::Handle(isolate->object_store()->byte_buffer_class()); 7482 Class::Handle(isolate->object_store()->internal_byte_array_class());
7442 ByteBuffer& result = ByteBuffer::Handle(); 7483 InternalByteArray& result = InternalByteArray::Handle();
7443 { 7484 {
7444 RawObject* raw = Object::Allocate(byte_buffer_class, 7485 RawObject* raw = Object::Allocate(internal_byte_array_class,
7445 ByteBuffer::InstanceSize(), 7486 InternalByteArray::InstanceSize(len),
7487 space);
7488 NoGCScope no_gc;
7489 result ^= raw;
7490 result.SetLength(len);
7491 memset(result.Addr<uint8_t>(0), 0, len);
7492 }
7493 return result.raw();
7494 }
7495
7496
7497 RawInternalByteArray* InternalByteArray::New(const uint8_t* data,
7498 intptr_t len,
7499 Heap::Space space) {
7500 InternalByteArray& result =
7501 InternalByteArray::Handle(InternalByteArray::New(len, space));
7502 {
7503 NoGCScope no_gc;
7504 memmove(result.Addr<uint8_t>(0), data, len);
7505 }
7506 return result.raw();
7507 }
7508
7509
7510 const char* InternalByteArray::ToCString() const {
7511 return "_InternalByteArray";
7512 }
7513
7514
7515 RawExternalByteArray* ExternalByteArray::New(uint8_t* data,
7516 intptr_t len,
7517 Heap::Space space) {
7518 Isolate* isolate = Isolate::Current();
7519 const Class& external_byte_array_class =
7520 Class::Handle(isolate->object_store()->external_byte_array_class());
7521 ExternalByteArray& result = ExternalByteArray::Handle();
7522 {
7523 RawObject* raw = Object::Allocate(external_byte_array_class,
7524 ExternalByteArray::InstanceSize(),
7446 space); 7525 space);
7447 NoGCScope no_gc; 7526 NoGCScope no_gc;
7448 result ^= raw; 7527 result ^= raw;
7449 result.SetLength(len); 7528 result.SetLength(len);
7450 result.SetData(data); 7529 result.SetData(data);
7451 } 7530 }
7452 return result.raw(); 7531 return result.raw();
7453 } 7532 }
7454 7533
7455 7534
7456 bool ByteBuffer::Equals(const Instance& other) const { 7535 const char* ExternalByteArray::ToCString() const {
7457 if (this->raw() == other.raw()) { 7536 return "_ExternalByteArray";
7458 // Both handles point to the same raw instance.
7459 return true;
7460 }
7461
7462 if (!other.IsByteBuffer() || other.IsNull()) {
7463 return false;
7464 }
7465
7466 ByteBuffer& other_array = ByteBuffer::Handle();
7467 other_array ^= other.raw();
7468
7469 intptr_t len = this->Length();
7470 if (len != other_array.Length()) {
7471 return false;
7472 }
7473
7474 return memcmp(this->Addr<uint8_t>(0), other_array.Addr<uint8_t>(0), len) == 0;
7475 } 7537 }
7476 7538
7477 7539
7478 const char* ByteBuffer::ToCString() const {
7479 return "ByteBuffer";
7480 }
7481
7482
7483 RawClosure* Closure::New(const Function& function, 7540 RawClosure* Closure::New(const Function& function,
7484 const Context& context, 7541 const Context& context,
7485 Heap::Space space) { 7542 Heap::Space space) {
7486 Isolate* isolate = Isolate::Current(); 7543 Isolate* isolate = Isolate::Current();
7487 ASSERT(context.isolate() == isolate); 7544 ASSERT(context.isolate() == isolate);
7488 7545
7489 const Class& cls = Class::Handle(function.signature_class()); 7546 const Class& cls = Class::Handle(function.signature_class());
7490 Closure& result = Closure::Handle(); 7547 Closure& result = Closure::Handle();
7491 { 7548 {
7492 RawObject* raw = Object::Allocate(cls, Closure::InstanceSize(), space); 7549 RawObject* raw = Object::Allocate(cls, Closure::InstanceSize(), space);
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
7782 const String& str = String::Handle(pattern()); 7839 const String& str = String::Handle(pattern());
7783 const char* format = "JSRegExp: pattern=%s flags=%s"; 7840 const char* format = "JSRegExp: pattern=%s flags=%s";
7784 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); 7841 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags());
7785 char* chars = reinterpret_cast<char*>( 7842 char* chars = reinterpret_cast<char*>(
7786 Isolate::Current()->current_zone()->Allocate(len + 1)); 7843 Isolate::Current()->current_zone()->Allocate(len + 1));
7787 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); 7844 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags());
7788 return chars; 7845 return chars;
7789 } 7846 }
7790 7847
7791 } // namespace dart 7848 } // 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