| 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* malloc_allocator(uint8_t* ptr, | 19 static uint8_t* malloc_allocator(uint8_t* ptr, |
| 20 intptr_t old_size, | 20 intptr_t old_size, |
| 21 intptr_t new_size) { | 21 intptr_t new_size) { |
| 22 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); | 22 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); |
| 23 return reinterpret_cast<uint8_t*>(new_ptr); | 23 return reinterpret_cast<uint8_t*>(new_ptr); |
| 24 } | 24 } |
| 25 | 25 |
| 26 | |
| 27 class IsolateSaver { | 26 class IsolateSaver { |
| 28 public: | 27 public: |
| 29 explicit IsolateSaver(Isolate* current_isolate) | 28 explicit IsolateSaver(Isolate* current_isolate) |
| 30 : saved_isolate_(current_isolate) { | 29 : saved_isolate_(current_isolate) { |
| 31 if (current_isolate != NULL) { | 30 if (current_isolate != NULL) { |
| 32 ASSERT(current_isolate == Isolate::Current()); | 31 ASSERT(current_isolate == Isolate::Current()); |
| 33 Dart_ExitIsolate(); | 32 Dart_ExitIsolate(); |
| 34 } | 33 } |
| 35 } | 34 } |
| 36 ~IsolateSaver() { | 35 ~IsolateSaver() { |
| 37 if (saved_isolate_ != NULL) { | 36 if (saved_isolate_ != NULL) { |
| 38 Dart_Isolate I = reinterpret_cast<Dart_Isolate>(saved_isolate_); | 37 Dart_Isolate I = reinterpret_cast<Dart_Isolate>(saved_isolate_); |
| 39 Dart_EnterIsolate(I); | 38 Dart_EnterIsolate(I); |
| 40 } | 39 } |
| 41 } | 40 } |
| 42 | 41 |
| 43 private: | 42 private: |
| 44 Isolate* saved_isolate_; | 43 Isolate* saved_isolate_; |
| 45 | 44 |
| 46 DISALLOW_COPY_AND_ASSIGN(IsolateSaver); | 45 DISALLOW_COPY_AND_ASSIGN(IsolateSaver); |
| 47 }; | 46 }; |
| 48 | 47 |
| 49 | |
| 50 static bool PostCObjectHelper(Dart_Port port_id, Dart_CObject* message) { | 48 static bool PostCObjectHelper(Dart_Port port_id, Dart_CObject* message) { |
| 51 uint8_t* buffer = NULL; | 49 uint8_t* buffer = NULL; |
| 52 ApiMessageWriter writer(&buffer, malloc_allocator); | 50 ApiMessageWriter writer(&buffer, malloc_allocator); |
| 53 bool success = writer.WriteCMessage(message); | 51 bool success = writer.WriteCMessage(message); |
| 54 | 52 |
| 55 if (!success) { | 53 if (!success) { |
| 56 free(buffer); | 54 free(buffer); |
| 57 return success; | 55 return success; |
| 58 } | 56 } |
| 59 | 57 |
| 60 // Post the message at the given port. | 58 // Post the message at the given port. |
| 61 return PortMap::PostMessage(new Message( | 59 return PortMap::PostMessage(new Message( |
| 62 port_id, buffer, writer.BytesWritten(), Message::kNormalPriority)); | 60 port_id, buffer, writer.BytesWritten(), Message::kNormalPriority)); |
| 63 } | 61 } |
| 64 | 62 |
| 65 | |
| 66 DART_EXPORT bool Dart_PostCObject(Dart_Port port_id, Dart_CObject* message) { | 63 DART_EXPORT bool Dart_PostCObject(Dart_Port port_id, Dart_CObject* message) { |
| 67 return PostCObjectHelper(port_id, message); | 64 return PostCObjectHelper(port_id, message); |
| 68 } | 65 } |
| 69 | 66 |
| 70 | |
| 71 DART_EXPORT bool Dart_PostInteger(Dart_Port port_id, int64_t message) { | 67 DART_EXPORT bool Dart_PostInteger(Dart_Port port_id, int64_t message) { |
| 72 if (Smi::IsValid(message)) { | 68 if (Smi::IsValid(message)) { |
| 73 return PortMap::PostMessage( | 69 return PortMap::PostMessage( |
| 74 new Message(port_id, Smi::New(message), Message::kNormalPriority)); | 70 new Message(port_id, Smi::New(message), Message::kNormalPriority)); |
| 75 } | 71 } |
| 76 Dart_CObject cobj; | 72 Dart_CObject cobj; |
| 77 cobj.type = Dart_CObject_kInt64; | 73 cobj.type = Dart_CObject_kInt64; |
| 78 cobj.value.as_int64 = message; | 74 cobj.value.as_int64 = message; |
| 79 return PostCObjectHelper(port_id, &cobj); | 75 return PostCObjectHelper(port_id, &cobj); |
| 80 } | 76 } |
| 81 | 77 |
| 82 | |
| 83 DART_EXPORT Dart_Port Dart_NewNativePort(const char* name, | 78 DART_EXPORT Dart_Port Dart_NewNativePort(const char* name, |
| 84 Dart_NativeMessageHandler handler, | 79 Dart_NativeMessageHandler handler, |
| 85 bool handle_concurrently) { | 80 bool handle_concurrently) { |
| 86 if (name == NULL) { | 81 if (name == NULL) { |
| 87 name = "<UnnamedNativePort>"; | 82 name = "<UnnamedNativePort>"; |
| 88 } | 83 } |
| 89 if (handler == NULL) { | 84 if (handler == NULL) { |
| 90 OS::PrintErr("%s expects argument 'handler' to be non-null.\n", | 85 OS::PrintErr("%s expects argument 'handler' to be non-null.\n", |
| 91 CURRENT_FUNC); | 86 CURRENT_FUNC); |
| 92 return ILLEGAL_PORT; | 87 return ILLEGAL_PORT; |
| 93 } | 88 } |
| 94 // Start the native port without a current isolate. | 89 // Start the native port without a current isolate. |
| 95 IsolateSaver saver(Isolate::Current()); | 90 IsolateSaver saver(Isolate::Current()); |
| 96 | 91 |
| 97 NativeMessageHandler* nmh = new NativeMessageHandler(name, handler); | 92 NativeMessageHandler* nmh = new NativeMessageHandler(name, handler); |
| 98 Dart_Port port_id = PortMap::CreatePort(nmh); | 93 Dart_Port port_id = PortMap::CreatePort(nmh); |
| 99 PortMap::SetPortState(port_id, PortMap::kLivePort); | 94 PortMap::SetPortState(port_id, PortMap::kLivePort); |
| 100 nmh->Run(Dart::thread_pool(), NULL, NULL, 0); | 95 nmh->Run(Dart::thread_pool(), NULL, NULL, 0); |
| 101 return port_id; | 96 return port_id; |
| 102 } | 97 } |
| 103 | 98 |
| 104 | |
| 105 DART_EXPORT bool Dart_CloseNativePort(Dart_Port native_port_id) { | 99 DART_EXPORT bool Dart_CloseNativePort(Dart_Port native_port_id) { |
| 106 // Close the native port without a current isolate. | 100 // Close the native port without a current isolate. |
| 107 IsolateSaver saver(Isolate::Current()); | 101 IsolateSaver saver(Isolate::Current()); |
| 108 | 102 |
| 109 // TODO(turnidge): Check that the port is native before trying to close. | 103 // TODO(turnidge): Check that the port is native before trying to close. |
| 110 return PortMap::ClosePort(native_port_id); | 104 return PortMap::ClosePort(native_port_id); |
| 111 } | 105 } |
| 112 | 106 |
| 113 | |
| 114 // --- Verification tools --- | 107 // --- Verification tools --- |
| 115 | 108 |
| 116 static void CompileAll(Thread* thread, Dart_Handle* result) { | 109 static void CompileAll(Thread* thread, Dart_Handle* result) { |
| 117 ASSERT(thread != NULL); | 110 ASSERT(thread != NULL); |
| 118 const Error& error = Error::Handle(thread->zone(), Library::CompileAll()); | 111 const Error& error = Error::Handle(thread->zone(), Library::CompileAll()); |
| 119 if (error.IsNull()) { | 112 if (error.IsNull()) { |
| 120 *result = Api::Success(); | 113 *result = Api::Success(); |
| 121 } else { | 114 } else { |
| 122 *result = Api::NewHandle(thread, error.raw()); | 115 *result = Api::NewHandle(thread, error.raw()); |
| 123 } | 116 } |
| 124 } | 117 } |
| 125 | 118 |
| 126 | |
| 127 DART_EXPORT Dart_Handle Dart_CompileAll() { | 119 DART_EXPORT Dart_Handle Dart_CompileAll() { |
| 128 DARTSCOPE(Thread::Current()); | 120 DARTSCOPE(Thread::Current()); |
| 129 Dart_Handle result = Api::CheckAndFinalizePendingClasses(T); | 121 Dart_Handle result = Api::CheckAndFinalizePendingClasses(T); |
| 130 if (::Dart_IsError(result)) { | 122 if (::Dart_IsError(result)) { |
| 131 return result; | 123 return result; |
| 132 } | 124 } |
| 133 CHECK_CALLBACK_STATE(T); | 125 CHECK_CALLBACK_STATE(T); |
| 134 CompileAll(T, &result); | 126 CompileAll(T, &result); |
| 135 return result; | 127 return result; |
| 136 } | 128 } |
| 137 | 129 |
| 138 | |
| 139 static void ParseAll(Thread* thread, Dart_Handle* result) { | 130 static void ParseAll(Thread* thread, Dart_Handle* result) { |
| 140 ASSERT(thread != NULL); | 131 ASSERT(thread != NULL); |
| 141 const Error& error = Error::Handle(thread->zone(), Library::ParseAll(thread)); | 132 const Error& error = Error::Handle(thread->zone(), Library::ParseAll(thread)); |
| 142 if (error.IsNull()) { | 133 if (error.IsNull()) { |
| 143 *result = Api::Success(); | 134 *result = Api::Success(); |
| 144 } else { | 135 } else { |
| 145 *result = Api::NewHandle(thread, error.raw()); | 136 *result = Api::NewHandle(thread, error.raw()); |
| 146 } | 137 } |
| 147 } | 138 } |
| 148 | 139 |
| 149 | |
| 150 DART_EXPORT Dart_Handle Dart_ParseAll() { | 140 DART_EXPORT Dart_Handle Dart_ParseAll() { |
| 151 DARTSCOPE(Thread::Current()); | 141 DARTSCOPE(Thread::Current()); |
| 152 Dart_Handle result = Api::CheckAndFinalizePendingClasses(T); | 142 Dart_Handle result = Api::CheckAndFinalizePendingClasses(T); |
| 153 if (::Dart_IsError(result)) { | 143 if (::Dart_IsError(result)) { |
| 154 return result; | 144 return result; |
| 155 } | 145 } |
| 156 CHECK_CALLBACK_STATE(T); | 146 CHECK_CALLBACK_STATE(T); |
| 157 ParseAll(T, &result); | 147 ParseAll(T, &result); |
| 158 return result; | 148 return result; |
| 159 } | 149 } |
| 160 | 150 |
| 161 } // namespace dart | 151 } // namespace dart |
| OLD | NEW |