OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "include/dart_native_api.h" |
| 6 |
| 7 #include "platform/assert.h" |
| 8 #include "vm/dart_api_impl.h" |
| 9 #include "vm/dart_api_message.h" |
| 10 #include "vm/dart_api_state.h" |
| 11 #include "vm/message.h" |
| 12 #include "vm/native_message_handler.h" |
| 13 #include "vm/port.h" |
| 14 |
| 15 namespace dart { |
| 16 |
| 17 // --- Message sending/receiving from native code --- |
| 18 |
| 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); |
| 21 return reinterpret_cast<uint8_t*>(new_ptr); |
| 22 } |
| 23 |
| 24 |
| 25 DART_EXPORT bool Dart_PostCObject(Dart_Port port_id, Dart_CObject* message) { |
| 26 uint8_t* buffer = NULL; |
| 27 ApiMessageWriter writer(&buffer, allocator); |
| 28 bool success = writer.WriteCMessage(message); |
| 29 |
| 30 if (!success) return success; |
| 31 |
| 32 // Post the message at the given port. |
| 33 return PortMap::PostMessage(new Message( |
| 34 port_id, Message::kIllegalPort, buffer, writer.BytesWritten(), |
| 35 Message::kNormalPriority)); |
| 36 } |
| 37 |
| 38 |
| 39 DART_EXPORT Dart_Port Dart_NewNativePort(const char* name, |
| 40 Dart_NativeMessageHandler handler, |
| 41 bool handle_concurrently) { |
| 42 if (name == NULL) { |
| 43 name = "<UnnamedNativePort>"; |
| 44 } |
| 45 if (handler == NULL) { |
| 46 OS::PrintErr("%s expects argument 'handler' to be non-null.\n", |
| 47 CURRENT_FUNC); |
| 48 return ILLEGAL_PORT; |
| 49 } |
| 50 // Start the native port without a current isolate. |
| 51 IsolateSaver saver(Isolate::Current()); |
| 52 Isolate::SetCurrent(NULL); |
| 53 |
| 54 NativeMessageHandler* nmh = new NativeMessageHandler(name, handler); |
| 55 Dart_Port port_id = PortMap::CreatePort(nmh); |
| 56 nmh->Run(Dart::thread_pool(), NULL, NULL, 0); |
| 57 return port_id; |
| 58 } |
| 59 |
| 60 |
| 61 DART_EXPORT bool Dart_CloseNativePort(Dart_Port native_port_id) { |
| 62 // Close the native port without a current isolate. |
| 63 IsolateSaver saver(Isolate::Current()); |
| 64 Isolate::SetCurrent(NULL); |
| 65 |
| 66 // TODO(turnidge): Check that the port is native before trying to close. |
| 67 return PortMap::ClosePort(native_port_id); |
| 68 } |
| 69 |
| 70 |
| 71 // --- Profiling support --- |
| 72 |
| 73 // TODO(7565): Dartium should use the new VM flag "generate_pprof_symbols" for |
| 74 // pprof profiling. Then these symbols should be removed. |
| 75 |
| 76 DART_EXPORT void Dart_InitPprofSupport() { } |
| 77 |
| 78 DART_EXPORT void Dart_GetPprofSymbolInfo(void** buffer, int* buffer_size) { |
| 79 *buffer = NULL; |
| 80 *buffer_size = 0; |
| 81 } |
| 82 |
| 83 |
| 84 // --- Heap Profiler --- |
| 85 |
| 86 DART_EXPORT Dart_Handle Dart_HeapProfile(Dart_FileWriteCallback callback, |
| 87 void* stream) { |
| 88 Isolate* isolate = Isolate::Current(); |
| 89 CHECK_ISOLATE(isolate); |
| 90 if (callback == NULL) { |
| 91 RETURN_NULL_ERROR(callback); |
| 92 } |
| 93 isolate->heap()->Profile(callback, stream); |
| 94 return Api::Success(); |
| 95 } |
| 96 |
| 97 |
| 98 // --- Verification tools --- |
| 99 |
| 100 static void CompileAll(Isolate* isolate, Dart_Handle* result) { |
| 101 ASSERT(isolate != NULL); |
| 102 const Error& error = Error::Handle(isolate, Library::CompileAll()); |
| 103 if (error.IsNull()) { |
| 104 *result = Api::Success(); |
| 105 } else { |
| 106 *result = Api::NewHandle(isolate, error.raw()); |
| 107 } |
| 108 } |
| 109 |
| 110 |
| 111 DART_EXPORT Dart_Handle Dart_CompileAll() { |
| 112 Isolate* isolate = Isolate::Current(); |
| 113 DARTSCOPE(isolate); |
| 114 Dart_Handle result = Api::CheckIsolateState(isolate); |
| 115 if (::Dart_IsError(result)) { |
| 116 return result; |
| 117 } |
| 118 CHECK_CALLBACK_STATE(isolate); |
| 119 CompileAll(isolate, &result); |
| 120 return result; |
| 121 } |
| 122 |
| 123 |
| 124 DART_EXPORT Dart_Handle Dart_CheckFunctionFingerprints() { |
| 125 Isolate* isolate = Isolate::Current(); |
| 126 DARTSCOPE(isolate); |
| 127 Dart_Handle result = Api::CheckIsolateState(isolate); |
| 128 if (::Dart_IsError(result)) { |
| 129 return result; |
| 130 } |
| 131 CHECK_CALLBACK_STATE(isolate); |
| 132 Library::CheckFunctionFingerprints(); |
| 133 return result; |
| 134 } |
| 135 |
| 136 } // namespace dart |
OLD | NEW |