OLD | NEW |
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/native_message_handler.h" | 5 #include "vm/native_message_handler.h" |
6 | 6 |
| 7 #include "vm/dart_api_message.h" |
7 #include "vm/isolate.h" | 8 #include "vm/isolate.h" |
8 #include "vm/message.h" | 9 #include "vm/message.h" |
| 10 #include "vm/snapshot.h" |
9 #include "vm/thread.h" | 11 #include "vm/thread.h" |
10 | 12 |
11 namespace dart { | 13 namespace dart { |
12 | 14 |
13 NativeMessageHandler::NativeMessageHandler(const char* name, | 15 NativeMessageHandler::NativeMessageHandler(const char* name, |
14 Dart_NativeMessageHandler func) | 16 Dart_NativeMessageHandler func) |
15 : name_(strdup(name)), | 17 : name_(strdup(name)), |
16 func_(func) { | 18 func_(func) { |
17 // A NativeMessageHandler always has one live port. | 19 // A NativeMessageHandler always has one live port. |
18 increment_live_ports(); | 20 increment_live_ports(); |
19 } | 21 } |
20 | 22 |
21 | 23 |
22 NativeMessageHandler::~NativeMessageHandler() { | 24 NativeMessageHandler::~NativeMessageHandler() { |
23 free(name_); | 25 free(name_); |
24 } | 26 } |
25 | 27 |
26 | 28 |
27 #if defined(DEBUG) | 29 #if defined(DEBUG) |
28 void NativeMessageHandler::CheckAccess() { | 30 void NativeMessageHandler::CheckAccess() { |
29 ASSERT(Isolate::Current() == NULL); | 31 ASSERT(Isolate::Current() == NULL); |
30 } | 32 } |
31 #endif | 33 #endif |
32 | 34 |
33 | 35 |
| 36 static uint8_t* zone_allocator( |
| 37 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
| 38 ApiZone* zone = ApiNativeScope::Current()->zone(); |
| 39 return reinterpret_cast<uint8_t*>( |
| 40 zone->Reallocate(reinterpret_cast<uword>(ptr), old_size, new_size)); |
| 41 } |
| 42 |
| 43 |
34 static void RunWorker(uword parameter) { | 44 static void RunWorker(uword parameter) { |
35 NativeMessageHandler* handler = | 45 NativeMessageHandler* handler = |
36 reinterpret_cast<NativeMessageHandler*>(parameter); | 46 reinterpret_cast<NativeMessageHandler*>(parameter); |
37 #if defined(DEBUG) | 47 #if defined(DEBUG) |
38 handler->CheckAccess(); | 48 handler->CheckAccess(); |
39 #endif | 49 #endif |
40 | 50 |
41 while (handler->HasLivePorts()) { | 51 while (handler->HasLivePorts()) { |
42 Message* message = handler->queue()->Dequeue(0); | 52 Message* message = handler->queue()->Dequeue(0); |
43 if (message != NULL) { | 53 if (message != NULL) { |
44 if (message->priority() >= Message::kOOBPriority) { | 54 if (message->priority() >= Message::kOOBPriority) { |
45 // TODO(turnidge): Out of band messages will not go through | 55 // TODO(turnidge): Out of band messages will not go through |
46 // the regular message handler. Instead they will be | 56 // the regular message handler. Instead they will be |
47 // dispatched to special vm code. Implement. | 57 // dispatched to special vm code. Implement. |
48 UNIMPLEMENTED(); | 58 UNIMPLEMENTED(); |
49 } | 59 } |
50 // TODO(sgjesse): Once CMessageReader::ReadObject is committed, | 60 // Enter a native scope for handling the message. This will create a |
51 // use that here and pass the resulting data object to the | 61 // zone for allocating the objects for decoding the message. |
52 // handler instead. | 62 ApiNativeScope scope; |
| 63 |
| 64 int32_t length = reinterpret_cast<int32_t*>( |
| 65 message->data())[Snapshot::kLengthIndex]; |
| 66 ApiMessageReader reader(message->data() + Snapshot::kHeaderSize, |
| 67 length, |
| 68 zone_allocator); |
| 69 Dart_CObject* object = reader.ReadMessage(); |
53 (*handler->func())(message->dest_port(), | 70 (*handler->func())(message->dest_port(), |
54 message->reply_port(), | 71 message->reply_port(), |
55 message->data()); | 72 object); |
56 delete message; | 73 delete message; |
57 } | 74 } |
58 } | 75 } |
59 } | 76 } |
60 | 77 |
61 | 78 |
62 void NativeMessageHandler::StartWorker() { | 79 void NativeMessageHandler::StartWorker() { |
63 int result = Thread::Start(RunWorker, reinterpret_cast<uword>(this)); | 80 int result = Thread::Start(RunWorker, reinterpret_cast<uword>(this)); |
64 if (result != 0) { | 81 if (result != 0) { |
65 FATAL1("Failed to start native message handler worker thread %d", result); | 82 FATAL1("Failed to start native message handler worker thread %d", result); |
66 } | 83 } |
67 } | 84 } |
68 | 85 |
69 | 86 |
70 } // namespace dart | 87 } // namespace dart |
OLD | NEW |