OLD | NEW |
---|---|
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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_api.h" | 5 #include "include/dart_api.h" |
6 | 6 |
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/compiler.h" | 9 #include "vm/compiler.h" |
10 #include "vm/dart.h" | 10 #include "vm/dart.h" |
11 #include "vm/dart_api_impl.h" | 11 #include "vm/dart_api_impl.h" |
12 #include "vm/dart_api_state.h" | 12 #include "vm/dart_api_state.h" |
13 #include "vm/dart_entry.h" | 13 #include "vm/dart_entry.h" |
14 #include "vm/debuginfo.h" | 14 #include "vm/debuginfo.h" |
15 #include "vm/exceptions.h" | 15 #include "vm/exceptions.h" |
16 #include "vm/growable_array.h" | 16 #include "vm/growable_array.h" |
17 #include "vm/longjump.h" | 17 #include "vm/longjump.h" |
18 #include "vm/message_queue.h" | |
18 #include "vm/native_entry.h" | 19 #include "vm/native_entry.h" |
19 #include "vm/object.h" | 20 #include "vm/object.h" |
20 #include "vm/object_store.h" | 21 #include "vm/object_store.h" |
21 #include "vm/port.h" | 22 #include "vm/port.h" |
22 #include "vm/resolver.h" | 23 #include "vm/resolver.h" |
23 #include "vm/snapshot.h" | 24 #include "vm/snapshot.h" |
24 #include "vm/stack_frame.h" | 25 #include "vm/stack_frame.h" |
25 #include "vm/timer.h" | 26 #include "vm/timer.h" |
26 #include "vm/verifier.h" | 27 #include "vm/verifier.h" |
27 | 28 |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
188 ASSERT(state.IsValidPersistentHandle(object)); | 189 ASSERT(state.IsValidPersistentHandle(object)); |
189 return reinterpret_cast<PersistentHandle*>(object); | 190 return reinterpret_cast<PersistentHandle*>(object); |
190 } | 191 } |
191 | 192 |
192 | 193 |
193 Dart_Isolate Api::CastIsolate(Isolate* isolate) { | 194 Dart_Isolate Api::CastIsolate(Isolate* isolate) { |
194 return reinterpret_cast<Dart_Isolate>(isolate); | 195 return reinterpret_cast<Dart_Isolate>(isolate); |
195 } | 196 } |
196 | 197 |
197 | 198 |
198 Dart_Message Api::CastMessage(uint8_t* message) { | |
199 return reinterpret_cast<Dart_Message>(message); | |
200 } | |
201 | |
202 | |
203 Dart_Handle Api::Success() { | 199 Dart_Handle Api::Success() { |
204 Isolate* isolate = Isolate::Current(); | 200 Isolate* isolate = Isolate::Current(); |
205 ASSERT(isolate != NULL); | 201 ASSERT(isolate != NULL); |
206 ApiState* state = isolate->api_state(); | 202 ApiState* state = isolate->api_state(); |
207 ASSERT(state != NULL); | 203 ASSERT(state != NULL); |
208 PersistentHandle* true_handle = state->True(); | 204 PersistentHandle* true_handle = state->True(); |
209 return reinterpret_cast<Dart_Handle>(true_handle); | 205 return reinterpret_cast<Dart_Handle>(true_handle); |
210 } | 206 } |
211 | 207 |
212 | 208 |
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
660 FATAL1("%s expects argument 'isolate' to be non-null.", CURRENT_FUNC); | 656 FATAL1("%s expects argument 'isolate' to be non-null.", CURRENT_FUNC); |
661 } | 657 } |
662 Isolate* iso = reinterpret_cast<Isolate*>(isolate); | 658 Isolate* iso = reinterpret_cast<Isolate*>(isolate); |
663 iso->ScheduleInterrupts(Isolate::kApiInterrupt); | 659 iso->ScheduleInterrupts(Isolate::kApiInterrupt); |
664 } | 660 } |
665 | 661 |
666 | 662 |
667 // --- Messages and Ports --- | 663 // --- Messages and Ports --- |
668 | 664 |
669 | 665 |
670 DART_EXPORT void Dart_SetMessageCallbacks( | 666 DART_EXPORT void Dart_SetMessageNotifyCallback( |
671 Dart_PostMessageCallback post_message_callback, | 667 Dart_MessageNotifyCallback message_notify_callback) { |
672 Dart_ClosePortCallback close_port_callback) { | |
673 Isolate* isolate = Isolate::Current(); | 668 Isolate* isolate = Isolate::Current(); |
674 CHECK_ISOLATE(isolate); | 669 CHECK_ISOLATE(isolate); |
675 ASSERT(post_message_callback != NULL); | 670 isolate->set_message_notify_callback(message_notify_callback); |
676 ASSERT(close_port_callback != NULL); | |
677 isolate->set_post_message_callback(post_message_callback); | |
678 isolate->set_close_port_callback(close_port_callback); | |
679 } | 671 } |
680 | 672 |
681 | 673 |
682 DART_EXPORT Dart_Handle Dart_RunLoop() { | 674 DART_EXPORT Dart_Handle Dart_RunLoop() { |
683 Isolate* isolate = Isolate::Current(); | 675 Isolate* isolate = Isolate::Current(); |
684 DARTSCOPE(isolate); | 676 DARTSCOPE(isolate); |
685 | 677 |
686 LongJump* base = isolate->long_jump_base(); | 678 LongJump* base = isolate->long_jump_base(); |
687 LongJump jump; | 679 LongJump jump; |
688 Dart_Handle result; | 680 Dart_Handle result; |
(...skipping 20 matching lines...) Expand all Loading... | |
709 ASSERT(snapshot->IsMessageSnapshot()); | 701 ASSERT(snapshot->IsMessageSnapshot()); |
710 | 702 |
711 // Read object back from the snapshot. | 703 // Read object back from the snapshot. |
712 SnapshotReader reader(snapshot, Isolate::Current()); | 704 SnapshotReader reader(snapshot, Isolate::Current()); |
713 Instance& instance = Instance::Handle(); | 705 Instance& instance = Instance::Handle(); |
714 instance ^= reader.ReadObject(); | 706 instance ^= reader.ReadObject(); |
715 return instance.raw(); | 707 return instance.raw(); |
716 } | 708 } |
717 | 709 |
718 | 710 |
719 DART_EXPORT Dart_Handle Dart_HandleMessage(Dart_Port dest_port_id, | 711 DART_EXPORT Dart_Handle Dart_HandleMessage() { |
720 Dart_Port reply_port_id, | 712 Isolate* isolate = Isolate::Current(); |
721 Dart_Message dart_message) { | 713 // Process all OOB messages and at most one normal message. |
722 DARTSCOPE(Isolate::Current()); | 714 Message* message = NULL; |
723 const Instance& msg = Instance::Handle(DeserializeMessage(dart_message)); | 715 Message::Priority priority = Message::kNormalPriority; |
724 // TODO(turnidge): Should this call be wrapped in a longjmp? | 716 do { |
725 const Object& result = | 717 DARTSCOPE(isolate); |
726 Object::Handle(DartLibraryCalls::HandleMessage(dest_port_id, | 718 message = isolate->message_queue()->DequeueNoWait(); |
727 reply_port_id, | 719 if (message == NULL) { |
728 msg)); | 720 break; |
729 if (result.IsError()) { | 721 } |
730 return Api::NewLocalHandle(result); | 722 priority = message->priority(); |
731 } | 723 if (priority == Message::kOOBPriority) { |
732 ASSERT(result.IsNull()); | 724 UNIMPLEMENTED(); |
Ivan Posva
2012/01/23 22:00:43
Can you briefly describe in a comment what should
turnidge
2012/01/23 22:32:52
Done.
| |
725 } | |
726 const Instance& msg = | |
727 Instance::Handle(DeserializeMessage(message->data())); | |
728 const Object& result = Object::Handle( | |
729 DartLibraryCalls::HandleMessage( | |
730 message->dest_port(), message->reply_port(), msg)); | |
731 delete message; | |
732 if (result.IsError()) { | |
733 return Api::NewLocalHandle(result); | |
734 } | |
735 ASSERT(result.IsNull()); | |
736 } while (priority == Message::kOOBPriority); | |
Ivan Posva
2012/01/23 22:00:43
How about:
while (priority >= Message::kOOBPriorit
turnidge
2012/01/23 22:32:52
Anton didn't like the >= because it is unclear whi
| |
733 return Api::Success(); | 737 return Api::Success(); |
734 } | 738 } |
735 | 739 |
736 | 740 |
737 DART_EXPORT bool Dart_HasLivePorts() { | 741 DART_EXPORT bool Dart_HasLivePorts() { |
738 Isolate* isolate = Isolate::Current(); | 742 Isolate* isolate = Isolate::Current(); |
739 ASSERT(isolate); | 743 ASSERT(isolate); |
740 return isolate->live_ports() > 0; | 744 return isolate->live_ports() > 0; |
741 } | 745 } |
742 | 746 |
743 | 747 |
744 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { | 748 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
745 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); | 749 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); |
746 return reinterpret_cast<uint8_t*>(new_ptr); | 750 return reinterpret_cast<uint8_t*>(new_ptr); |
747 } | 751 } |
748 | 752 |
749 | 753 |
750 DART_EXPORT bool Dart_PostIntArray(Dart_Port port_id, | 754 DART_EXPORT bool Dart_PostIntArray(Dart_Port port_id, |
751 intptr_t len, | 755 intptr_t len, |
752 intptr_t* data) { | 756 intptr_t* data) { |
753 uint8_t* buffer = NULL; | 757 uint8_t* buffer = NULL; |
754 MessageWriter writer(&buffer, &allocator); | 758 MessageWriter writer(&buffer, &allocator); |
755 | 759 |
756 writer.WriteMessage(len, data); | 760 writer.WriteMessage(len, data); |
757 | 761 |
758 // Post the message at the given port. | 762 // Post the message at the given port. |
759 return PortMap::PostMessage(port_id, kNoReplyPort, Api::CastMessage(buffer)); | 763 return PortMap::PostMessage(new Message( |
764 port_id, Message::kIllegalPort, buffer, Message::kNormalPriority)); | |
760 } | 765 } |
761 | 766 |
762 | 767 |
763 DART_EXPORT bool Dart_Post(Dart_Port port_id, Dart_Handle handle) { | 768 DART_EXPORT bool Dart_Post(Dart_Port port_id, Dart_Handle handle) { |
764 Isolate* isolate = Isolate::Current(); | 769 Isolate* isolate = Isolate::Current(); |
765 CHECK_ISOLATE(isolate); | 770 CHECK_ISOLATE(isolate); |
766 DARTSCOPE_NOCHECKS(isolate); | 771 DARTSCOPE_NOCHECKS(isolate); |
767 const Object& object = Object::Handle(Api::UnwrapHandle(handle)); | 772 const Object& object = Object::Handle(Api::UnwrapHandle(handle)); |
768 uint8_t* data = NULL; | 773 uint8_t* data = NULL; |
769 SnapshotWriter writer(Snapshot::kMessage, &data, &allocator); | 774 SnapshotWriter writer(Snapshot::kMessage, &data, &allocator); |
770 writer.WriteObject(object.raw()); | 775 writer.WriteObject(object.raw()); |
771 writer.FinalizeBuffer(); | 776 writer.FinalizeBuffer(); |
772 return PortMap::PostMessage(port_id, kNoReplyPort, Api::CastMessage(data)); | 777 return PortMap::PostMessage(new Message( |
778 port_id, Message::kIllegalPort, data, Message::kNormalPriority)); | |
773 } | 779 } |
774 | 780 |
775 | 781 |
776 DART_EXPORT Dart_Handle Dart_NewSendPort(Dart_Port port_id) { | 782 DART_EXPORT Dart_Handle Dart_NewSendPort(Dart_Port port_id) { |
777 Isolate* isolate = Isolate::Current(); | 783 Isolate* isolate = Isolate::Current(); |
778 DARTSCOPE(isolate); | 784 DARTSCOPE(isolate); |
779 const String& class_name = String::Handle(String::NewSymbol("SendPortImpl")); | 785 const String& class_name = String::Handle(String::NewSymbol("SendPortImpl")); |
780 const String& function_name = String::Handle(String::NewSymbol("_create")); | 786 const String& function_name = String::Handle(String::NewSymbol("_create")); |
781 const int kNumArguments = 1; | 787 const int kNumArguments = 1; |
782 const Array& kNoArgumentNames = Array::Handle(); | 788 const Array& kNoArgumentNames = Array::Handle(); |
(...skipping 1719 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2502 } | 2508 } |
2503 delete debug_region; | 2509 delete debug_region; |
2504 } else { | 2510 } else { |
2505 *buffer = NULL; | 2511 *buffer = NULL; |
2506 *buffer_size = 0; | 2512 *buffer_size = 0; |
2507 } | 2513 } |
2508 } | 2514 } |
2509 | 2515 |
2510 | 2516 |
2511 } // namespace dart | 2517 } // namespace dart |
OLD | NEW |