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 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 ASSERT(state.IsValidWeakPersistentHandle(object)); | 195 ASSERT(state.IsValidWeakPersistentHandle(object)); |
195 return reinterpret_cast<WeakPersistentHandle*>(object); | 196 return reinterpret_cast<WeakPersistentHandle*>(object); |
196 } | 197 } |
197 | 198 |
198 | 199 |
199 Dart_Isolate Api::CastIsolate(Isolate* isolate) { | 200 Dart_Isolate Api::CastIsolate(Isolate* isolate) { |
200 return reinterpret_cast<Dart_Isolate>(isolate); | 201 return reinterpret_cast<Dart_Isolate>(isolate); |
201 } | 202 } |
202 | 203 |
203 | 204 |
204 Dart_Message Api::CastMessage(uint8_t* message) { | |
205 return reinterpret_cast<Dart_Message>(message); | |
206 } | |
207 | |
208 | |
209 Dart_Handle Api::Success() { | 205 Dart_Handle Api::Success() { |
210 Isolate* isolate = Isolate::Current(); | 206 Isolate* isolate = Isolate::Current(); |
211 ASSERT(isolate != NULL); | 207 ASSERT(isolate != NULL); |
212 ApiState* state = isolate->api_state(); | 208 ApiState* state = isolate->api_state(); |
213 ASSERT(state != NULL); | 209 ASSERT(state != NULL); |
214 PersistentHandle* true_handle = state->True(); | 210 PersistentHandle* true_handle = state->True(); |
215 return reinterpret_cast<Dart_Handle>(true_handle); | 211 return reinterpret_cast<Dart_Handle>(true_handle); |
216 } | 212 } |
217 | 213 |
218 | 214 |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 FATAL1("%s expects argument 'isolate' to be non-null.", CURRENT_FUNC); | 617 FATAL1("%s expects argument 'isolate' to be non-null.", CURRENT_FUNC); |
622 } | 618 } |
623 Isolate* iso = reinterpret_cast<Isolate*>(isolate); | 619 Isolate* iso = reinterpret_cast<Isolate*>(isolate); |
624 iso->ScheduleInterrupts(Isolate::kApiInterrupt); | 620 iso->ScheduleInterrupts(Isolate::kApiInterrupt); |
625 } | 621 } |
626 | 622 |
627 | 623 |
628 // --- Messages and Ports --- | 624 // --- Messages and Ports --- |
629 | 625 |
630 | 626 |
631 DART_EXPORT void Dart_SetMessageCallbacks( | 627 DART_EXPORT void Dart_SetMessageNotifyCallback( |
632 Dart_PostMessageCallback post_message_callback, | 628 Dart_MessageNotifyCallback message_notify_callback) { |
633 Dart_ClosePortCallback close_port_callback) { | |
634 Isolate* isolate = Isolate::Current(); | 629 Isolate* isolate = Isolate::Current(); |
635 CHECK_ISOLATE(isolate); | 630 CHECK_ISOLATE(isolate); |
636 ASSERT(post_message_callback != NULL); | 631 isolate->set_message_notify_callback(message_notify_callback); |
637 ASSERT(close_port_callback != NULL); | |
638 isolate->set_post_message_callback(post_message_callback); | |
639 isolate->set_close_port_callback(close_port_callback); | |
640 } | 632 } |
641 | 633 |
642 | 634 |
643 DART_EXPORT Dart_Handle Dart_RunLoop() { | 635 DART_EXPORT Dart_Handle Dart_RunLoop() { |
644 Isolate* isolate = Isolate::Current(); | 636 Isolate* isolate = Isolate::Current(); |
645 DARTSCOPE(isolate); | 637 DARTSCOPE(isolate); |
646 | 638 |
647 LongJump* base = isolate->long_jump_base(); | 639 LongJump* base = isolate->long_jump_base(); |
648 LongJump jump; | 640 LongJump jump; |
649 Dart_Handle result; | 641 Dart_Handle result; |
(...skipping 20 matching lines...) Expand all Loading... |
670 ASSERT(snapshot->IsMessageSnapshot()); | 662 ASSERT(snapshot->IsMessageSnapshot()); |
671 | 663 |
672 // Read object back from the snapshot. | 664 // Read object back from the snapshot. |
673 SnapshotReader reader(snapshot, Isolate::Current()); | 665 SnapshotReader reader(snapshot, Isolate::Current()); |
674 Instance& instance = Instance::Handle(); | 666 Instance& instance = Instance::Handle(); |
675 instance ^= reader.ReadObject(); | 667 instance ^= reader.ReadObject(); |
676 return instance.raw(); | 668 return instance.raw(); |
677 } | 669 } |
678 | 670 |
679 | 671 |
680 DART_EXPORT Dart_Handle Dart_HandleMessage(Dart_Port dest_port_id, | 672 DART_EXPORT Dart_Handle Dart_HandleMessage() { |
681 Dart_Port reply_port_id, | 673 Isolate* isolate = Isolate::Current(); |
682 Dart_Message dart_message) { | 674 // Process all OOB messages and at most one normal message. |
683 DARTSCOPE(Isolate::Current()); | 675 Message* message = NULL; |
684 const Instance& msg = Instance::Handle(DeserializeMessage(dart_message)); | 676 Message::Priority priority = Message::kNormalPriority; |
685 // TODO(turnidge): Should this call be wrapped in a longjmp? | 677 do { |
686 const Object& result = | 678 DARTSCOPE(isolate); |
687 Object::Handle(DartLibraryCalls::HandleMessage(dest_port_id, | 679 message = isolate->message_queue()->DequeueNoWait(); |
688 reply_port_id, | 680 if (message == NULL) { |
689 msg)); | 681 break; |
690 if (result.IsError()) { | 682 } |
691 return Api::NewLocalHandle(result); | 683 priority = message->priority(); |
692 } | 684 if (priority == Message::kOOBPriority) { |
693 ASSERT(result.IsNull()); | 685 // TODO(turnidge): Out of band messages will not go through the |
| 686 // regular message handler. Instead they will be dispatched to |
| 687 // special vm code. Implement. |
| 688 UNIMPLEMENTED(); |
| 689 } |
| 690 const Instance& msg = |
| 691 Instance::Handle(DeserializeMessage(message->data())); |
| 692 const Object& result = Object::Handle( |
| 693 DartLibraryCalls::HandleMessage( |
| 694 message->dest_port(), message->reply_port(), msg)); |
| 695 delete message; |
| 696 if (result.IsError()) { |
| 697 return Api::NewLocalHandle(result); |
| 698 } |
| 699 ASSERT(result.IsNull()); |
| 700 } while (priority >= Message::kOOBPriority); |
694 return Api::Success(); | 701 return Api::Success(); |
695 } | 702 } |
696 | 703 |
697 | 704 |
698 DART_EXPORT bool Dart_HasLivePorts() { | 705 DART_EXPORT bool Dart_HasLivePorts() { |
699 Isolate* isolate = Isolate::Current(); | 706 Isolate* isolate = Isolate::Current(); |
700 ASSERT(isolate); | 707 ASSERT(isolate); |
701 return isolate->live_ports() > 0; | 708 return isolate->live_ports() > 0; |
702 } | 709 } |
703 | 710 |
704 | 711 |
705 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { | 712 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
706 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); | 713 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); |
707 return reinterpret_cast<uint8_t*>(new_ptr); | 714 return reinterpret_cast<uint8_t*>(new_ptr); |
708 } | 715 } |
709 | 716 |
710 | 717 |
711 DART_EXPORT bool Dart_PostIntArray(Dart_Port port_id, | 718 DART_EXPORT bool Dart_PostIntArray(Dart_Port port_id, |
712 intptr_t len, | 719 intptr_t len, |
713 intptr_t* data) { | 720 intptr_t* data) { |
714 uint8_t* buffer = NULL; | 721 uint8_t* buffer = NULL; |
715 MessageWriter writer(&buffer, &allocator); | 722 MessageWriter writer(&buffer, &allocator); |
716 | 723 |
717 writer.WriteMessage(len, data); | 724 writer.WriteMessage(len, data); |
718 | 725 |
719 // Post the message at the given port. | 726 // Post the message at the given port. |
720 return PortMap::PostMessage(port_id, kNoReplyPort, Api::CastMessage(buffer)); | 727 return PortMap::PostMessage(new Message( |
| 728 port_id, Message::kIllegalPort, buffer, Message::kNormalPriority)); |
721 } | 729 } |
722 | 730 |
723 | 731 |
724 DART_EXPORT bool Dart_Post(Dart_Port port_id, Dart_Handle handle) { | 732 DART_EXPORT bool Dart_Post(Dart_Port port_id, Dart_Handle handle) { |
725 Isolate* isolate = Isolate::Current(); | 733 Isolate* isolate = Isolate::Current(); |
726 CHECK_ISOLATE(isolate); | 734 CHECK_ISOLATE(isolate); |
727 DARTSCOPE_NOCHECKS(isolate); | 735 DARTSCOPE_NOCHECKS(isolate); |
728 const Object& object = Object::Handle(Api::UnwrapHandle(handle)); | 736 const Object& object = Object::Handle(Api::UnwrapHandle(handle)); |
729 uint8_t* data = NULL; | 737 uint8_t* data = NULL; |
730 SnapshotWriter writer(Snapshot::kMessage, &data, &allocator); | 738 SnapshotWriter writer(Snapshot::kMessage, &data, &allocator); |
731 writer.WriteObject(object.raw()); | 739 writer.WriteObject(object.raw()); |
732 writer.FinalizeBuffer(); | 740 writer.FinalizeBuffer(); |
733 return PortMap::PostMessage(port_id, kNoReplyPort, Api::CastMessage(data)); | 741 return PortMap::PostMessage(new Message( |
| 742 port_id, Message::kIllegalPort, data, Message::kNormalPriority)); |
734 } | 743 } |
735 | 744 |
736 | 745 |
737 DART_EXPORT Dart_Handle Dart_NewSendPort(Dart_Port port_id) { | 746 DART_EXPORT Dart_Handle Dart_NewSendPort(Dart_Port port_id) { |
738 Isolate* isolate = Isolate::Current(); | 747 Isolate* isolate = Isolate::Current(); |
739 DARTSCOPE(isolate); | 748 DARTSCOPE(isolate); |
740 const String& class_name = String::Handle(String::NewSymbol("SendPortImpl")); | 749 const String& class_name = String::Handle(String::NewSymbol("SendPortImpl")); |
741 const String& function_name = String::Handle(String::NewSymbol("_create")); | 750 const String& function_name = String::Handle(String::NewSymbol("_create")); |
742 const int kNumArguments = 1; | 751 const int kNumArguments = 1; |
743 const Array& kNoArgumentNames = Array::Handle(); | 752 const Array& kNoArgumentNames = Array::Handle(); |
(...skipping 1721 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2465 } | 2474 } |
2466 delete debug_region; | 2475 delete debug_region; |
2467 } else { | 2476 } else { |
2468 *buffer = NULL; | 2477 *buffer = NULL; |
2469 *buffer_size = 0; | 2478 *buffer_size = 0; |
2470 } | 2479 } |
2471 } | 2480 } |
2472 | 2481 |
2473 | 2482 |
2474 } // namespace dart | 2483 } // namespace dart |
OLD | NEW |