OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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_native_api.h" | 5 #include "include/dart_native_api.h" |
6 | 6 |
7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
8 #include "vm/dart_api_impl.h" | 8 #include "vm/dart_api_impl.h" |
9 #include "vm/dart_api_message.h" | 9 #include "vm/dart_api_message.h" |
10 #include "vm/dart_api_state.h" | 10 #include "vm/dart_api_state.h" |
11 #include "vm/message.h" | 11 #include "vm/message.h" |
12 #include "vm/native_message_handler.h" | 12 #include "vm/native_message_handler.h" |
13 #include "vm/port.h" | 13 #include "vm/port.h" |
14 | 14 |
15 namespace dart { | 15 namespace dart { |
16 | 16 |
17 // --- Message sending/receiving from native code --- | 17 // --- Message sending/receiving from native code --- |
18 | 18 |
19 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { | 19 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
20 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); | 20 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); |
21 return reinterpret_cast<uint8_t*>(new_ptr); | 21 return reinterpret_cast<uint8_t*>(new_ptr); |
22 } | 22 } |
23 | 23 |
24 | 24 |
| 25 class IsolateSaver { |
| 26 public: |
| 27 explicit IsolateSaver(Isolate* current_isolate) |
| 28 : saved_isolate_(current_isolate) { |
| 29 if (current_isolate != NULL) { |
| 30 ASSERT(current_isolate == Isolate::Current()); |
| 31 Thread::ExitIsolate(); |
| 32 } |
| 33 } |
| 34 ~IsolateSaver() { |
| 35 if (saved_isolate_ != NULL) { |
| 36 Thread::EnterIsolate(saved_isolate_); |
| 37 } |
| 38 } |
| 39 private: |
| 40 Isolate* saved_isolate_; |
| 41 |
| 42 DISALLOW_COPY_AND_ASSIGN(IsolateSaver); |
| 43 }; |
| 44 |
| 45 |
25 DART_EXPORT bool Dart_PostCObject(Dart_Port port_id, Dart_CObject* message) { | 46 DART_EXPORT bool Dart_PostCObject(Dart_Port port_id, Dart_CObject* message) { |
26 uint8_t* buffer = NULL; | 47 uint8_t* buffer = NULL; |
27 ApiMessageWriter writer(&buffer, allocator); | 48 ApiMessageWriter writer(&buffer, allocator); |
28 bool success = writer.WriteCMessage(message); | 49 bool success = writer.WriteCMessage(message); |
29 | 50 |
30 if (!success) return success; | 51 if (!success) return success; |
31 | 52 |
32 // Post the message at the given port. | 53 // Post the message at the given port. |
33 return PortMap::PostMessage(new Message( | 54 return PortMap::PostMessage(new Message( |
34 port_id, buffer, writer.BytesWritten(), Message::kNormalPriority)); | 55 port_id, buffer, writer.BytesWritten(), Message::kNormalPriority)); |
35 } | 56 } |
36 | 57 |
37 | 58 |
38 DART_EXPORT Dart_Port Dart_NewNativePort(const char* name, | 59 DART_EXPORT Dart_Port Dart_NewNativePort(const char* name, |
39 Dart_NativeMessageHandler handler, | 60 Dart_NativeMessageHandler handler, |
40 bool handle_concurrently) { | 61 bool handle_concurrently) { |
41 if (name == NULL) { | 62 if (name == NULL) { |
42 name = "<UnnamedNativePort>"; | 63 name = "<UnnamedNativePort>"; |
43 } | 64 } |
44 if (handler == NULL) { | 65 if (handler == NULL) { |
45 OS::PrintErr("%s expects argument 'handler' to be non-null.\n", | 66 OS::PrintErr("%s expects argument 'handler' to be non-null.\n", |
46 CURRENT_FUNC); | 67 CURRENT_FUNC); |
47 return ILLEGAL_PORT; | 68 return ILLEGAL_PORT; |
48 } | 69 } |
49 // Start the native port without a current isolate. | 70 // Start the native port without a current isolate. |
50 IsolateSaver saver(Isolate::Current()); | 71 IsolateSaver saver(Isolate::Current()); |
51 Thread::ExitIsolate(); | |
52 | 72 |
53 NativeMessageHandler* nmh = new NativeMessageHandler(name, handler); | 73 NativeMessageHandler* nmh = new NativeMessageHandler(name, handler); |
54 Dart_Port port_id = PortMap::CreatePort(nmh); | 74 Dart_Port port_id = PortMap::CreatePort(nmh); |
55 nmh->Run(Dart::thread_pool(), NULL, NULL, 0); | 75 nmh->Run(Dart::thread_pool(), NULL, NULL, 0); |
56 return port_id; | 76 return port_id; |
57 } | 77 } |
58 | 78 |
59 | 79 |
60 DART_EXPORT bool Dart_CloseNativePort(Dart_Port native_port_id) { | 80 DART_EXPORT bool Dart_CloseNativePort(Dart_Port native_port_id) { |
61 // Close the native port without a current isolate. | 81 // Close the native port without a current isolate. |
62 IsolateSaver saver(Isolate::Current()); | 82 IsolateSaver saver(Isolate::Current()); |
63 Thread::ExitIsolate(); | |
64 | 83 |
65 // TODO(turnidge): Check that the port is native before trying to close. | 84 // TODO(turnidge): Check that the port is native before trying to close. |
66 return PortMap::ClosePort(native_port_id); | 85 return PortMap::ClosePort(native_port_id); |
67 } | 86 } |
68 | 87 |
69 | 88 |
70 // --- Verification tools --- | 89 // --- Verification tools --- |
71 | 90 |
72 static void CompileAll(Thread* thread, Dart_Handle* result) { | 91 static void CompileAll(Thread* thread, Dart_Handle* result) { |
73 ASSERT(thread != NULL); | 92 ASSERT(thread != NULL); |
(...skipping 11 matching lines...) Expand all Loading... |
85 Dart_Handle result = Api::CheckAndFinalizePendingClasses(I); | 104 Dart_Handle result = Api::CheckAndFinalizePendingClasses(I); |
86 if (::Dart_IsError(result)) { | 105 if (::Dart_IsError(result)) { |
87 return result; | 106 return result; |
88 } | 107 } |
89 CHECK_CALLBACK_STATE(T); | 108 CHECK_CALLBACK_STATE(T); |
90 CompileAll(T, &result); | 109 CompileAll(T, &result); |
91 return result; | 110 return result; |
92 } | 111 } |
93 | 112 |
94 } // namespace dart | 113 } // namespace dart |
OLD | NEW |