| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <mojo/result.h> | |
| 6 #include <mojo/system/buffer.h> | |
| 7 #include <mojo/system/data_pipe.h> | |
| 8 #include <mojo/system/handle.h> | |
| 9 #include <mojo/system/message_pipe.h> | |
| 10 #include <mojo/system/time.h> | |
| 11 #include <stdio.h> | |
| 12 #include <string.h> | |
| 13 | |
| 14 #include <memory> | |
| 15 #include <vector> | |
| 16 | |
| 17 #include "dart/runtime/include/dart_api.h" | |
| 18 #include "mojo/public/cpp/environment/logging.h" | |
| 19 #include "mojo/public/cpp/system/macros.h" | |
| 20 #include "mojo/public/cpp/system/wait.h" | |
| 21 #include "mojo/public/platform/dart/dart_handle_watcher.h" | |
| 22 | |
| 23 namespace mojo { | |
| 24 namespace dart { | |
| 25 | |
| 26 #define REGISTER_FUNCTION(name, count) \ | |
| 27 { "" #name, name, count }, | |
| 28 #define DECLARE_FUNCTION(name, count) \ | |
| 29 extern void name(Dart_NativeArguments args); | |
| 30 | |
| 31 #define MOJO_NATIVE_LIST(V) \ | |
| 32 V(MojoSharedBuffer_Create, 2) \ | |
| 33 V(MojoSharedBuffer_Duplicate, 2) \ | |
| 34 V(MojoSharedBuffer_Map, 4) \ | |
| 35 V(MojoSharedBuffer_GetInformation, 1) \ | |
| 36 V(MojoDataPipe_Create, 3) \ | |
| 37 V(MojoDataPipe_WriteData, 4) \ | |
| 38 V(MojoDataPipe_BeginWriteData, 2) \ | |
| 39 V(MojoDataPipe_EndWriteData, 2) \ | |
| 40 V(MojoDataPipe_ReadData, 4) \ | |
| 41 V(MojoDataPipe_BeginReadData, 2) \ | |
| 42 V(MojoDataPipe_EndReadData, 2) \ | |
| 43 V(MojoMessagePipe_Create, 1) \ | |
| 44 V(MojoMessagePipe_Write, 5) \ | |
| 45 V(MojoMessagePipe_Read, 5) \ | |
| 46 V(MojoMessagePipe_QueryAndRead, 3) \ | |
| 47 V(Mojo_GetTimeTicksNow, 0) \ | |
| 48 V(MojoHandle_Close, 1) \ | |
| 49 V(MojoHandle_Wait, 3) \ | |
| 50 V(MojoHandle_RegisterFinalizer, 2) \ | |
| 51 V(MojoHandle_WaitMany, 3) \ | |
| 52 V(MojoHandleWatcher_SendControlData, 5) | |
| 53 | |
| 54 MOJO_NATIVE_LIST(DECLARE_FUNCTION); | |
| 55 | |
| 56 static struct NativeEntries { | |
| 57 const char* name; | |
| 58 Dart_NativeFunction function; | |
| 59 int argument_count; | |
| 60 } MojoEntries[] = {MOJO_NATIVE_LIST(REGISTER_FUNCTION)}; | |
| 61 | |
| 62 Dart_NativeFunction MojoNativeLookup(Dart_Handle name, | |
| 63 int argument_count, | |
| 64 bool* auto_setup_scope) { | |
| 65 const char* function_name = nullptr; | |
| 66 Dart_Handle result = Dart_StringToCString(name, &function_name); | |
| 67 DART_CHECK_VALID(result); | |
| 68 assert(function_name != nullptr); | |
| 69 assert(auto_setup_scope != nullptr); | |
| 70 *auto_setup_scope = true; | |
| 71 size_t num_entries = MOJO_ARRAYSIZE(MojoEntries); | |
| 72 for (size_t i = 0; i < num_entries; ++i) { | |
| 73 const struct NativeEntries& entry = MojoEntries[i]; | |
| 74 if (!strcmp(function_name, entry.name) && | |
| 75 (entry.argument_count == argument_count)) { | |
| 76 return entry.function; | |
| 77 } | |
| 78 } | |
| 79 return nullptr; | |
| 80 } | |
| 81 | |
| 82 const uint8_t* MojoNativeSymbol(Dart_NativeFunction nf) { | |
| 83 size_t num_entries = MOJO_ARRAYSIZE(MojoEntries); | |
| 84 for (size_t i = 0; i < num_entries; ++i) { | |
| 85 const struct NativeEntries& entry = MojoEntries[i]; | |
| 86 if (entry.function == nf) { | |
| 87 return reinterpret_cast<const uint8_t*>(entry.name); | |
| 88 } | |
| 89 } | |
| 90 return nullptr; | |
| 91 } | |
| 92 | |
| 93 static void SetNullReturn(Dart_NativeArguments arguments) { | |
| 94 Dart_SetReturnValue(arguments, Dart_Null()); | |
| 95 } | |
| 96 | |
| 97 static void SetInvalidArgumentReturn(Dart_NativeArguments arguments) { | |
| 98 Dart_SetIntegerReturnValue( | |
| 99 arguments, static_cast<int64_t>(MOJO_RESULT_INVALID_ARGUMENT)); | |
| 100 } | |
| 101 | |
| 102 static Dart_Handle SignalsStateToDart(const MojoHandleSignalsState& state) { | |
| 103 Dart_Handle list = Dart_NewList(2); | |
| 104 Dart_Handle arg0 = Dart_NewInteger(state.satisfied_signals); | |
| 105 Dart_Handle arg1 = Dart_NewInteger(state.satisfiable_signals); | |
| 106 Dart_ListSetAt(list, 0, arg0); | |
| 107 Dart_ListSetAt(list, 1, arg1); | |
| 108 return list; | |
| 109 } | |
| 110 | |
| 111 #define CHECK_INTEGER_ARGUMENT(args, num, result, failure) \ | |
| 112 { \ | |
| 113 Dart_Handle __status; \ | |
| 114 __status = Dart_GetNativeIntegerArgument(args, num, result); \ | |
| 115 if (Dart_IsError(__status)) { \ | |
| 116 Set##failure##Return(arguments); \ | |
| 117 return; \ | |
| 118 } \ | |
| 119 } \ | |
| 120 | |
| 121 struct CloserCallbackPeer { | |
| 122 MojoHandle handle; | |
| 123 }; | |
| 124 | |
| 125 static void MojoHandleCloserCallback(void* isolate_data, | |
| 126 Dart_WeakPersistentHandle handle, | |
| 127 void* peer) { | |
| 128 CloserCallbackPeer* callback_peer = | |
| 129 reinterpret_cast<CloserCallbackPeer*>(peer); | |
| 130 if (callback_peer->handle != MOJO_HANDLE_INVALID) { | |
| 131 MojoClose(callback_peer->handle); | |
| 132 } | |
| 133 delete callback_peer; | |
| 134 } | |
| 135 | |
| 136 // Setup a weak persistent handle for a MojoHandle that calls MojoClose on the | |
| 137 // handle when the MojoHandle is GC'd or the VM is going down. | |
| 138 void MojoHandle_RegisterFinalizer(Dart_NativeArguments arguments) { | |
| 139 Dart_Handle mojo_handle_instance = Dart_GetNativeArgument(arguments, 0); | |
| 140 if (!Dart_IsInstance(mojo_handle_instance)) { | |
| 141 SetInvalidArgumentReturn(arguments); | |
| 142 return; | |
| 143 } | |
| 144 | |
| 145 int64_t raw_handle = static_cast<int64_t>(MOJO_HANDLE_INVALID); | |
| 146 CHECK_INTEGER_ARGUMENT(arguments, 1, &raw_handle, InvalidArgument); | |
| 147 if (raw_handle == static_cast<int64_t>(MOJO_HANDLE_INVALID)) { | |
| 148 SetInvalidArgumentReturn(arguments); | |
| 149 return; | |
| 150 } | |
| 151 | |
| 152 // Add the handle to this isolate's set. | |
| 153 MojoHandle handle = static_cast<MojoHandle>(raw_handle); | |
| 154 | |
| 155 // Set up a finalizer. | |
| 156 CloserCallbackPeer* callback_peer = new CloserCallbackPeer(); | |
| 157 callback_peer->handle = handle; | |
| 158 Dart_NewWeakPersistentHandle(mojo_handle_instance, | |
| 159 reinterpret_cast<void*>(callback_peer), | |
| 160 sizeof(CloserCallbackPeer), | |
| 161 MojoHandleCloserCallback); | |
| 162 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); | |
| 163 } | |
| 164 | |
| 165 void Mojo_GetTimeTicksNow(Dart_NativeArguments arguments) { | |
| 166 MojoTimeTicks ticks = MojoGetTimeTicksNow(); | |
| 167 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(ticks)); | |
| 168 } | |
| 169 | |
| 170 void MojoHandle_Close(Dart_NativeArguments arguments) { | |
| 171 int64_t raw_handle; | |
| 172 CHECK_INTEGER_ARGUMENT(arguments, 0, &raw_handle, InvalidArgument); | |
| 173 | |
| 174 // Remove the handle from this isolate's set. | |
| 175 MojoHandle handle = static_cast<MojoHandle>(raw_handle); | |
| 176 MojoResult res = MojoClose(handle); | |
| 177 | |
| 178 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); | |
| 179 } | |
| 180 | |
| 181 void MojoHandle_Wait(Dart_NativeArguments arguments) { | |
| 182 int64_t handle = 0; | |
| 183 int64_t signals = 0; | |
| 184 int64_t deadline = 0; | |
| 185 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); | |
| 186 CHECK_INTEGER_ARGUMENT(arguments, 1, &signals, InvalidArgument); | |
| 187 CHECK_INTEGER_ARGUMENT(arguments, 2, &deadline, InvalidArgument); | |
| 188 | |
| 189 MojoHandleSignalsState state; | |
| 190 MojoResult r = mojo::Wait(mojo::Handle(static_cast<MojoHandle>(handle)), | |
| 191 static_cast<MojoHandleSignals>(signals), | |
| 192 static_cast<MojoDeadline>(deadline), &state); | |
| 193 | |
| 194 // The return value is structured as a list of length 2: | |
| 195 // [0] MojoResult | |
| 196 // [1] MojoHandleSignalsState. (may be null) | |
| 197 Dart_Handle list = Dart_NewList(2); | |
| 198 Dart_ListSetAt(list, 0, Dart_NewInteger(r)); | |
| 199 if (mojo::WaitManyResult(r).AreSignalsStatesValid()) { | |
| 200 Dart_ListSetAt(list, 1, SignalsStateToDart(state)); | |
| 201 } else { | |
| 202 Dart_ListSetAt(list, 1, Dart_Null()); | |
| 203 } | |
| 204 Dart_SetReturnValue(arguments, list); | |
| 205 } | |
| 206 | |
| 207 void MojoHandle_WaitMany(Dart_NativeArguments arguments) { | |
| 208 int64_t deadline = 0; | |
| 209 Dart_Handle handles = Dart_GetNativeArgument(arguments, 0); | |
| 210 Dart_Handle signals = Dart_GetNativeArgument(arguments, 1); | |
| 211 CHECK_INTEGER_ARGUMENT(arguments, 2, &deadline, InvalidArgument); | |
| 212 | |
| 213 if (!Dart_IsList(handles) || !Dart_IsList(signals)) { | |
| 214 SetInvalidArgumentReturn(arguments); | |
| 215 return; | |
| 216 } | |
| 217 | |
| 218 intptr_t handles_len = 0; | |
| 219 intptr_t signals_len = 0; | |
| 220 Dart_ListLength(handles, &handles_len); | |
| 221 Dart_ListLength(signals, &signals_len); | |
| 222 if (handles_len != signals_len) { | |
| 223 SetInvalidArgumentReturn(arguments); | |
| 224 return; | |
| 225 } | |
| 226 | |
| 227 std::vector<mojo::Handle> mojo_handles(handles_len); | |
| 228 std::vector<MojoHandleSignals> mojo_signals(handles_len); | |
| 229 | |
| 230 for (int i = 0; i < handles_len; i++) { | |
| 231 Dart_Handle dart_handle = Dart_ListGetAt(handles, i); | |
| 232 Dart_Handle dart_signal = Dart_ListGetAt(signals, i); | |
| 233 if (!Dart_IsInteger(dart_handle) || !Dart_IsInteger(dart_signal)) { | |
| 234 SetInvalidArgumentReturn(arguments); | |
| 235 return; | |
| 236 } | |
| 237 int64_t mojo_handle = 0; | |
| 238 int64_t mojo_signal = 0; | |
| 239 Dart_IntegerToInt64(dart_handle, &mojo_handle); | |
| 240 Dart_IntegerToInt64(dart_signal, &mojo_signal); | |
| 241 mojo_handles[i] = mojo::Handle(mojo_handle); | |
| 242 mojo_signals[i] = static_cast<MojoHandleSignals>(mojo_signal); | |
| 243 } | |
| 244 | |
| 245 std::vector<MojoHandleSignalsState> states(handles_len); | |
| 246 mojo::WaitManyResult wmr = mojo::WaitMany( | |
| 247 mojo_handles, mojo_signals, static_cast<MojoDeadline>(deadline), &states); | |
| 248 | |
| 249 // The return value is structured as a list of length 3: | |
| 250 // [0] MojoResult | |
| 251 // [1] index of handle that caused a return (may be null) | |
| 252 // [2] list of MojoHandleSignalsState. (may be null) | |
| 253 Dart_Handle list = Dart_NewList(3); | |
| 254 Dart_ListSetAt(list, 0, Dart_NewInteger(wmr.result)); | |
| 255 if (wmr.IsIndexValid()) | |
| 256 Dart_ListSetAt(list, 1, Dart_NewInteger(wmr.index)); | |
| 257 else | |
| 258 Dart_ListSetAt(list, 1, Dart_Null()); | |
| 259 if (wmr.AreSignalsStatesValid()) { | |
| 260 Dart_Handle stateList = Dart_NewList(handles_len); | |
| 261 for (int i = 0; i < handles_len; i++) { | |
| 262 Dart_ListSetAt(stateList, i, SignalsStateToDart(states[i])); | |
| 263 } | |
| 264 Dart_ListSetAt(list, 2, stateList); | |
| 265 } else { | |
| 266 Dart_ListSetAt(list, 2, Dart_Null()); | |
| 267 } | |
| 268 Dart_SetReturnValue(arguments, list); | |
| 269 } | |
| 270 | |
| 271 void MojoSharedBuffer_Create(Dart_NativeArguments arguments) { | |
| 272 int64_t num_bytes = 0; | |
| 273 int64_t flags = 0; | |
| 274 CHECK_INTEGER_ARGUMENT(arguments, 0, &num_bytes, Null); | |
| 275 CHECK_INTEGER_ARGUMENT(arguments, 1, &flags, Null); | |
| 276 | |
| 277 MojoCreateSharedBufferOptions options; | |
| 278 options.struct_size = sizeof(MojoCreateSharedBufferOptions); | |
| 279 options.flags = static_cast<MojoCreateSharedBufferOptionsFlags>(flags); | |
| 280 | |
| 281 MojoHandle out = MOJO_HANDLE_INVALID;; | |
| 282 MojoResult res = MojoCreateSharedBuffer( | |
| 283 &options, static_cast<int32_t>(num_bytes), &out); | |
| 284 | |
| 285 Dart_Handle list = Dart_NewList(2); | |
| 286 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 287 Dart_ListSetAt(list, 1, Dart_NewInteger(out)); | |
| 288 Dart_SetReturnValue(arguments, list); | |
| 289 } | |
| 290 | |
| 291 void MojoSharedBuffer_Duplicate(Dart_NativeArguments arguments) { | |
| 292 int64_t handle = 0; | |
| 293 int64_t flags = 0; | |
| 294 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); | |
| 295 CHECK_INTEGER_ARGUMENT(arguments, 1, &flags, Null); | |
| 296 | |
| 297 MojoDuplicateBufferHandleOptions options; | |
| 298 options.struct_size = sizeof(MojoDuplicateBufferHandleOptions); | |
| 299 options.flags = static_cast<MojoDuplicateBufferHandleOptionsFlags>(flags); | |
| 300 | |
| 301 MojoHandle out = MOJO_HANDLE_INVALID;; | |
| 302 MojoResult res = MojoDuplicateBufferHandle( | |
| 303 static_cast<MojoHandle>(handle), &options, &out); | |
| 304 | |
| 305 Dart_Handle list = Dart_NewList(2); | |
| 306 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 307 Dart_ListSetAt(list, 1, Dart_NewInteger(out)); | |
| 308 Dart_SetReturnValue(arguments, list); | |
| 309 } | |
| 310 | |
| 311 static void MojoBufferUnmapCallback(void* isolate_data, | |
| 312 Dart_WeakPersistentHandle handle, | |
| 313 void* peer) { | |
| 314 MojoUnmapBuffer(peer); | |
| 315 } | |
| 316 | |
| 317 void MojoSharedBuffer_Map(Dart_NativeArguments arguments) { | |
| 318 int64_t handle = 0; | |
| 319 int64_t offset = 0; | |
| 320 int64_t num_bytes = 0; | |
| 321 int64_t flags = 0; | |
| 322 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); | |
| 323 CHECK_INTEGER_ARGUMENT(arguments, 1, &offset, Null); | |
| 324 CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, Null); | |
| 325 CHECK_INTEGER_ARGUMENT(arguments, 3, &flags, Null); | |
| 326 | |
| 327 void* out; | |
| 328 MojoResult res = MojoMapBuffer(static_cast<MojoHandle>(handle), | |
| 329 offset, | |
| 330 num_bytes, | |
| 331 &out, | |
| 332 static_cast<MojoMapBufferFlags>(flags)); | |
| 333 | |
| 334 Dart_Handle list = Dart_NewList(2); | |
| 335 Dart_Handle typed_data; | |
| 336 if (res == MOJO_RESULT_OK) { | |
| 337 typed_data = Dart_NewExternalTypedData( | |
| 338 Dart_TypedData_kByteData, out, num_bytes); | |
| 339 Dart_NewWeakPersistentHandle(typed_data, out, num_bytes, | |
| 340 MojoBufferUnmapCallback); | |
| 341 } else { | |
| 342 typed_data = Dart_Null(); | |
| 343 } | |
| 344 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 345 Dart_ListSetAt(list, 1, typed_data); | |
| 346 Dart_SetReturnValue(arguments, list); | |
| 347 } | |
| 348 | |
| 349 void MojoSharedBuffer_GetInformation(Dart_NativeArguments arguments) { | |
| 350 int64_t handle = 0; | |
| 351 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); | |
| 352 | |
| 353 MojoBufferInformation buffer_information; | |
| 354 | |
| 355 MojoResult res = MojoGetBufferInformation(static_cast<MojoHandle>(handle), | |
| 356 &buffer_information, | |
| 357 sizeof(buffer_information)); | |
| 358 | |
| 359 Dart_Handle list = Dart_NewList(3); | |
| 360 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 361 if (res == MOJO_RESULT_OK) { | |
| 362 Dart_ListSetAt(list, 1, Dart_NewInteger(buffer_information.flags)); | |
| 363 Dart_ListSetAt(list, 2, Dart_NewInteger(buffer_information.num_bytes)); | |
| 364 } | |
| 365 | |
| 366 Dart_SetReturnValue(arguments, list); | |
| 367 } | |
| 368 | |
| 369 void MojoDataPipe_Create(Dart_NativeArguments arguments) { | |
| 370 int64_t element_bytes = 0; | |
| 371 int64_t capacity_bytes = 0; | |
| 372 int64_t flags = 0; | |
| 373 CHECK_INTEGER_ARGUMENT(arguments, 0, &element_bytes, Null); | |
| 374 CHECK_INTEGER_ARGUMENT(arguments, 1, &capacity_bytes, Null); | |
| 375 CHECK_INTEGER_ARGUMENT(arguments, 2, &flags, Null); | |
| 376 | |
| 377 MojoCreateDataPipeOptions options; | |
| 378 options.struct_size = sizeof(MojoCreateDataPipeOptions); | |
| 379 options.flags = static_cast<MojoCreateDataPipeOptionsFlags>(flags); | |
| 380 options.element_num_bytes = static_cast<uint32_t>(element_bytes); | |
| 381 options.capacity_num_bytes = static_cast<uint32_t>(capacity_bytes); | |
| 382 | |
| 383 MojoHandle producer = MOJO_HANDLE_INVALID; | |
| 384 MojoHandle consumer = MOJO_HANDLE_INVALID; | |
| 385 MojoResult res = MojoCreateDataPipe(&options, &producer, &consumer); | |
| 386 | |
| 387 Dart_Handle list = Dart_NewList(3); | |
| 388 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 389 Dart_ListSetAt(list, 1, Dart_NewInteger(producer)); | |
| 390 Dart_ListSetAt(list, 2, Dart_NewInteger(consumer)); | |
| 391 Dart_SetReturnValue(arguments, list); | |
| 392 } | |
| 393 | |
| 394 void MojoDataPipe_WriteData(Dart_NativeArguments arguments) { | |
| 395 int64_t handle = 0; | |
| 396 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); | |
| 397 | |
| 398 Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1); | |
| 399 if (!Dart_IsTypedData(typed_data)) { | |
| 400 SetNullReturn(arguments); | |
| 401 return; | |
| 402 } | |
| 403 | |
| 404 int64_t num_bytes = 0; | |
| 405 CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, Null); | |
| 406 if (num_bytes <= 0) { | |
| 407 SetNullReturn(arguments); | |
| 408 return; | |
| 409 } | |
| 410 | |
| 411 int64_t flags = 0; | |
| 412 CHECK_INTEGER_ARGUMENT(arguments, 3, &flags, Null); | |
| 413 | |
| 414 Dart_TypedData_Type type; | |
| 415 void* data; | |
| 416 intptr_t data_length; | |
| 417 Dart_TypedDataAcquireData(typed_data, &type, &data, &data_length); | |
| 418 uint32_t length = static_cast<uint32_t>(num_bytes); | |
| 419 MojoResult res = MojoWriteData( | |
| 420 static_cast<MojoHandle>(handle), | |
| 421 data, | |
| 422 &length, | |
| 423 static_cast<MojoWriteDataFlags>(flags)); | |
| 424 Dart_TypedDataReleaseData(typed_data); | |
| 425 | |
| 426 Dart_Handle list = Dart_NewList(2); | |
| 427 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 428 Dart_ListSetAt(list, 1, Dart_NewInteger(length)); | |
| 429 Dart_SetReturnValue(arguments, list); | |
| 430 } | |
| 431 | |
| 432 void MojoDataPipe_BeginWriteData(Dart_NativeArguments arguments) { | |
| 433 int64_t handle = 0; | |
| 434 int64_t flags = 0; | |
| 435 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); | |
| 436 CHECK_INTEGER_ARGUMENT(arguments, 1, &flags, Null); | |
| 437 | |
| 438 void* buffer; | |
| 439 uint32_t size; | |
| 440 MojoResult res = MojoBeginWriteData( | |
| 441 static_cast<MojoHandle>(handle), | |
| 442 &buffer, | |
| 443 &size, | |
| 444 static_cast<MojoWriteDataFlags>(flags)); | |
| 445 | |
| 446 Dart_Handle list = Dart_NewList(2); | |
| 447 Dart_Handle typed_data; | |
| 448 if (res == MOJO_RESULT_OK) { | |
| 449 typed_data = Dart_NewExternalTypedData( | |
| 450 Dart_TypedData_kByteData, buffer, size); | |
| 451 } else { | |
| 452 typed_data = Dart_Null(); | |
| 453 } | |
| 454 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 455 Dart_ListSetAt(list, 1, typed_data); | |
| 456 Dart_SetReturnValue(arguments, list); | |
| 457 } | |
| 458 | |
| 459 void MojoDataPipe_EndWriteData(Dart_NativeArguments arguments) { | |
| 460 int64_t handle = 0; | |
| 461 int64_t num_bytes_written = 0; | |
| 462 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); | |
| 463 CHECK_INTEGER_ARGUMENT(arguments, 1, &num_bytes_written, InvalidArgument); | |
| 464 | |
| 465 MojoResult res = MojoEndWriteData( | |
| 466 static_cast<MojoHandle>(handle), | |
| 467 static_cast<uint32_t>(num_bytes_written)); | |
| 468 | |
| 469 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); | |
| 470 } | |
| 471 | |
| 472 void MojoDataPipe_ReadData(Dart_NativeArguments arguments) { | |
| 473 int64_t handle = 0; | |
| 474 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); | |
| 475 | |
| 476 Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1); | |
| 477 if (!Dart_IsTypedData(typed_data) && !Dart_IsNull(typed_data)) { | |
| 478 SetNullReturn(arguments); | |
| 479 return; | |
| 480 } | |
| 481 // When querying the amount of data available to read from the pipe, | |
| 482 // null is passed in for typed_data. | |
| 483 | |
| 484 int64_t num_bytes = 0; | |
| 485 CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, Null); | |
| 486 | |
| 487 int64_t flags = 0; | |
| 488 CHECK_INTEGER_ARGUMENT(arguments, 3, &flags, Null); | |
| 489 | |
| 490 Dart_TypedData_Type typ; | |
| 491 void* data = nullptr; | |
| 492 intptr_t bdlen = 0; | |
| 493 if (!Dart_IsNull(typed_data)) { | |
| 494 Dart_TypedDataAcquireData(typed_data, &typ, &data, &bdlen); | |
| 495 } | |
| 496 uint32_t len = static_cast<uint32_t>(num_bytes); | |
| 497 MojoResult res = MojoReadData( | |
| 498 static_cast<MojoHandle>(handle), | |
| 499 data, | |
| 500 &len, | |
| 501 static_cast<MojoReadDataFlags>(flags)); | |
| 502 if (!Dart_IsNull(typed_data)) { | |
| 503 Dart_TypedDataReleaseData(typed_data); | |
| 504 } | |
| 505 | |
| 506 Dart_Handle list = Dart_NewList(2); | |
| 507 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 508 Dart_ListSetAt(list, 1, Dart_NewInteger(len)); | |
| 509 Dart_SetReturnValue(arguments, list); | |
| 510 } | |
| 511 | |
| 512 void MojoDataPipe_BeginReadData(Dart_NativeArguments arguments) { | |
| 513 int64_t handle = 0; | |
| 514 int64_t flags = 0; | |
| 515 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); | |
| 516 CHECK_INTEGER_ARGUMENT(arguments, 1, &flags, Null); | |
| 517 | |
| 518 void* buffer; | |
| 519 uint32_t size; | |
| 520 MojoResult res = MojoBeginReadData( | |
| 521 static_cast<MojoHandle>(handle), | |
| 522 const_cast<const void**>(&buffer), | |
| 523 &size, | |
| 524 static_cast<MojoWriteDataFlags>(flags)); | |
| 525 | |
| 526 Dart_Handle list = Dart_NewList(2); | |
| 527 Dart_Handle typed_data; | |
| 528 if (res == MOJO_RESULT_OK) { | |
| 529 typed_data = Dart_NewExternalTypedData( | |
| 530 Dart_TypedData_kByteData, buffer, size); | |
| 531 } else { | |
| 532 typed_data = Dart_Null(); | |
| 533 } | |
| 534 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 535 Dart_ListSetAt(list, 1, typed_data); | |
| 536 Dart_SetReturnValue(arguments, list); | |
| 537 } | |
| 538 | |
| 539 void MojoDataPipe_EndReadData(Dart_NativeArguments arguments) { | |
| 540 int64_t handle = 0; | |
| 541 int64_t num_bytes_read = 0; | |
| 542 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); | |
| 543 CHECK_INTEGER_ARGUMENT(arguments, 1, &num_bytes_read, InvalidArgument); | |
| 544 | |
| 545 MojoResult res = MojoEndReadData( | |
| 546 static_cast<MojoHandle>(handle), | |
| 547 static_cast<uint32_t>(num_bytes_read)); | |
| 548 | |
| 549 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); | |
| 550 } | |
| 551 | |
| 552 void MojoMessagePipe_Create(Dart_NativeArguments arguments) { | |
| 553 int64_t flags = 0; | |
| 554 CHECK_INTEGER_ARGUMENT(arguments, 0, &flags, Null); | |
| 555 | |
| 556 MojoCreateMessagePipeOptions options; | |
| 557 options.struct_size = sizeof(MojoCreateMessagePipeOptions); | |
| 558 options.flags = static_cast<MojoCreateMessagePipeOptionsFlags>(flags); | |
| 559 | |
| 560 MojoHandle end1 = MOJO_HANDLE_INVALID; | |
| 561 MojoHandle end2 = MOJO_HANDLE_INVALID; | |
| 562 MojoResult res = MojoCreateMessagePipe(&options, &end1, &end2); | |
| 563 | |
| 564 Dart_Handle list = Dart_NewList(3); | |
| 565 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 566 Dart_ListSetAt(list, 1, Dart_NewInteger(end1)); | |
| 567 Dart_ListSetAt(list, 2, Dart_NewInteger(end2)); | |
| 568 Dart_SetReturnValue(arguments, list); | |
| 569 } | |
| 570 | |
| 571 void MojoMessagePipe_Write(Dart_NativeArguments arguments) { | |
| 572 int64_t handle = 0; | |
| 573 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); | |
| 574 | |
| 575 Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1); | |
| 576 if (!Dart_IsTypedData(typed_data) && !Dart_IsNull(typed_data)) { | |
| 577 SetInvalidArgumentReturn(arguments); | |
| 578 return; | |
| 579 } | |
| 580 | |
| 581 int64_t num_bytes = 0; | |
| 582 CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, InvalidArgument); | |
| 583 if ((Dart_IsNull(typed_data) && (num_bytes != 0)) || | |
| 584 (!Dart_IsNull(typed_data) && (num_bytes <= 0))) { | |
| 585 SetInvalidArgumentReturn(arguments); | |
| 586 return; | |
| 587 } | |
| 588 | |
| 589 Dart_Handle handles = Dart_GetNativeArgument(arguments, 3); | |
| 590 if (!Dart_IsList(handles) && !Dart_IsNull(handles)) { | |
| 591 SetInvalidArgumentReturn(arguments); | |
| 592 return; | |
| 593 } | |
| 594 | |
| 595 int64_t flags = 0; | |
| 596 CHECK_INTEGER_ARGUMENT(arguments, 4, &flags, InvalidArgument); | |
| 597 | |
| 598 // Grab the data if there is any. | |
| 599 Dart_TypedData_Type typ; | |
| 600 void* bytes = nullptr; | |
| 601 intptr_t bdlen = 0; | |
| 602 if (!Dart_IsNull(typed_data)) { | |
| 603 Dart_TypedDataAcquireData(typed_data, &typ, &bytes, &bdlen); | |
| 604 } | |
| 605 | |
| 606 // Grab the handles if there are any. | |
| 607 std::unique_ptr<MojoHandle[]> mojo_handles; | |
| 608 intptr_t handles_len = 0; | |
| 609 if (!Dart_IsNull(handles)) { | |
| 610 Dart_ListLength(handles, &handles_len); | |
| 611 if (handles_len > 0) { | |
| 612 mojo_handles.reset(new MojoHandle[handles_len]); | |
| 613 } | |
| 614 for (int i = 0; i < handles_len; i++) { | |
| 615 Dart_Handle dart_handle = Dart_ListGetAt(handles, i); | |
| 616 if (!Dart_IsInteger(dart_handle)) { | |
| 617 SetInvalidArgumentReturn(arguments); | |
| 618 return; | |
| 619 } | |
| 620 int64_t mojo_handle = 0; | |
| 621 Dart_IntegerToInt64(dart_handle, &mojo_handle); | |
| 622 mojo_handles[i] = static_cast<MojoHandle>(mojo_handle); | |
| 623 } | |
| 624 } | |
| 625 | |
| 626 MojoResult res = MojoWriteMessage( | |
| 627 static_cast<MojoHandle>(handle), | |
| 628 const_cast<const void*>(bytes), | |
| 629 static_cast<uint32_t>(num_bytes), | |
| 630 mojo_handles.get(), | |
| 631 static_cast<uint32_t>(handles_len), | |
| 632 static_cast<MojoWriteMessageFlags>(flags)); | |
| 633 | |
| 634 // Release the data. | |
| 635 if (!Dart_IsNull(typed_data)) { | |
| 636 Dart_TypedDataReleaseData(typed_data); | |
| 637 } | |
| 638 | |
| 639 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); | |
| 640 } | |
| 641 | |
| 642 void MojoMessagePipe_Read(Dart_NativeArguments arguments) { | |
| 643 int64_t handle = 0; | |
| 644 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); | |
| 645 | |
| 646 Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1); | |
| 647 if (!Dart_IsTypedData(typed_data) && !Dart_IsNull(typed_data)) { | |
| 648 SetNullReturn(arguments); | |
| 649 return; | |
| 650 } | |
| 651 // When querying the amount of data available to read from the pipe, | |
| 652 // null is passed in for typed_data. | |
| 653 | |
| 654 int64_t num_bytes = 0; | |
| 655 CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, Null); | |
| 656 if ((Dart_IsNull(typed_data) && (num_bytes != 0)) || | |
| 657 (!Dart_IsNull(typed_data) && (num_bytes <= 0))) { | |
| 658 SetNullReturn(arguments); | |
| 659 return; | |
| 660 } | |
| 661 | |
| 662 Dart_Handle handles = Dart_GetNativeArgument(arguments, 3); | |
| 663 if (!Dart_IsList(handles) && !Dart_IsNull(handles)) { | |
| 664 SetNullReturn(arguments); | |
| 665 return; | |
| 666 } | |
| 667 | |
| 668 int64_t flags = 0; | |
| 669 CHECK_INTEGER_ARGUMENT(arguments, 4, &flags, Null); | |
| 670 | |
| 671 // Grab the data if there is any. | |
| 672 Dart_TypedData_Type typ; | |
| 673 void* bytes = nullptr; | |
| 674 intptr_t byte_data_len = 0; | |
| 675 if (!Dart_IsNull(typed_data)) { | |
| 676 Dart_TypedDataAcquireData(typed_data, &typ, &bytes, &byte_data_len); | |
| 677 } | |
| 678 uint32_t blen = static_cast<uint32_t>(num_bytes); | |
| 679 | |
| 680 // Grab the handles if there are any. | |
| 681 std::unique_ptr<MojoHandle[]> mojo_handles; | |
| 682 intptr_t handles_len = 0; | |
| 683 if (!Dart_IsNull(handles)) { | |
| 684 Dart_ListLength(handles, &handles_len); | |
| 685 mojo_handles.reset(new MojoHandle[handles_len]); | |
| 686 } | |
| 687 uint32_t hlen = static_cast<uint32_t>(handles_len); | |
| 688 | |
| 689 MojoResult res = MojoReadMessage( | |
| 690 static_cast<MojoHandle>(handle), | |
| 691 bytes, | |
| 692 &blen, | |
| 693 mojo_handles.get(), | |
| 694 &hlen, | |
| 695 static_cast<MojoReadMessageFlags>(flags)); | |
| 696 | |
| 697 // Release the data. | |
| 698 if (!Dart_IsNull(typed_data)) { | |
| 699 Dart_TypedDataReleaseData(typed_data); | |
| 700 } | |
| 701 | |
| 702 if (!Dart_IsNull(handles)) { | |
| 703 for (int i = 0; i < handles_len; i++) { | |
| 704 Dart_ListSetAt(handles, i, Dart_NewInteger(mojo_handles[i])); | |
| 705 } | |
| 706 } | |
| 707 | |
| 708 Dart_Handle list = Dart_NewList(3); | |
| 709 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 710 Dart_ListSetAt(list, 1, Dart_NewInteger(blen)); | |
| 711 Dart_ListSetAt(list, 2, Dart_NewInteger(hlen)); | |
| 712 Dart_SetReturnValue(arguments, list); | |
| 713 } | |
| 714 | |
| 715 void ByteArrayFinalizer(void* isolate_callback_data, | |
| 716 Dart_WeakPersistentHandle handle, | |
| 717 void* peer) { | |
| 718 uint8_t* byte_array = reinterpret_cast<uint8_t*>(peer); | |
| 719 delete[] byte_array; | |
| 720 } | |
| 721 | |
| 722 void HandleArrayFinalizer(void* isolate_callback_data, | |
| 723 Dart_WeakPersistentHandle handle, | |
| 724 void* peer) { | |
| 725 uint32_t* handle_array = reinterpret_cast<uint32_t*>(peer); | |
| 726 delete[] handle_array; | |
| 727 } | |
| 728 | |
| 729 void MojoMessagePipe_QueryAndRead(Dart_NativeArguments arguments) { | |
| 730 Dart_Handle err; | |
| 731 int64_t dart_handle; | |
| 732 int64_t flags = 0; | |
| 733 CHECK_INTEGER_ARGUMENT(arguments, 0, &dart_handle, Null); | |
| 734 CHECK_INTEGER_ARGUMENT(arguments, 1, &flags, Null); | |
| 735 Dart_Handle result = Dart_GetNativeArgument(arguments, 2); | |
| 736 | |
| 737 Dart_Handle data = Dart_ListGetAt(result, 1); | |
| 738 Dart_Handle handles = Dart_ListGetAt(result, 2); | |
| 739 | |
| 740 // Query the number of bytes and handles available. | |
| 741 uint32_t blen = 0; | |
| 742 uint32_t hlen = 0; | |
| 743 MojoResult res = | |
| 744 MojoReadMessage(static_cast<MojoHandle>(dart_handle), nullptr, &blen, | |
| 745 nullptr, &hlen, MOJO_READ_MESSAGE_FLAG_NONE); | |
| 746 | |
| 747 if ((res != MOJO_RESULT_OK) && (res != MOJO_RESULT_RESOURCE_EXHAUSTED)) { | |
| 748 Dart_ListSetAt(result, 0, Dart_NewInteger(res)); | |
| 749 Dart_ListSetAt(result, 1, data); | |
| 750 Dart_ListSetAt(result, 2, handles); | |
| 751 Dart_ListSetAt(result, 3, Dart_NewInteger(0)); | |
| 752 Dart_ListSetAt(result, 4, Dart_NewInteger(0)); | |
| 753 return; | |
| 754 } | |
| 755 | |
| 756 Dart_TypedData_Type typ; | |
| 757 void* bytes = nullptr; | |
| 758 intptr_t bytes_len = 0; | |
| 759 if ((blen > 0) && Dart_IsNull(data)) { | |
| 760 uint8_t* new_byte_data = new uint8_t[blen]; | |
| 761 data = Dart_NewExternalTypedData(Dart_TypedData_kByteData, new_byte_data, | |
| 762 blen); | |
| 763 MOJO_DCHECK(!Dart_IsError(data)); | |
| 764 Dart_NewWeakPersistentHandle(data, new_byte_data, blen, ByteArrayFinalizer); | |
| 765 } else if (blen > 0) { | |
| 766 err = Dart_TypedDataAcquireData(data, &typ, &bytes, &bytes_len); | |
| 767 MOJO_DCHECK(!Dart_IsError(err)); | |
| 768 err = Dart_TypedDataReleaseData(data); | |
| 769 MOJO_DCHECK(!Dart_IsError(err)); | |
| 770 if (static_cast<uintptr_t>(bytes_len) < blen) { | |
| 771 uint8_t* new_byte_data = new uint8_t[blen]; | |
| 772 data = Dart_NewExternalTypedData(Dart_TypedData_kByteData, new_byte_data, | |
| 773 blen); | |
| 774 MOJO_DCHECK(!Dart_IsError(data)); | |
| 775 Dart_NewWeakPersistentHandle(data, new_byte_data, blen, | |
| 776 ByteArrayFinalizer); | |
| 777 } | |
| 778 } | |
| 779 | |
| 780 void* handle_bytes = nullptr; | |
| 781 intptr_t handles_len = 0; | |
| 782 if ((hlen > 0) && Dart_IsNull(handles)) { | |
| 783 uint32_t* new_handle_data = new uint32_t[hlen]; | |
| 784 handles = Dart_NewExternalTypedData(Dart_TypedData_kUint32, new_handle_data, | |
| 785 hlen); | |
| 786 MOJO_DCHECK(!Dart_IsError(handles)); | |
| 787 Dart_NewWeakPersistentHandle(handles, new_handle_data, | |
| 788 hlen * sizeof(uint32_t), HandleArrayFinalizer); | |
| 789 } else if (hlen > 0) { | |
| 790 err = Dart_TypedDataAcquireData(handles, &typ, &handle_bytes, &handles_len); | |
| 791 MOJO_DCHECK(!Dart_IsError(err)); | |
| 792 err = Dart_TypedDataReleaseData(handles); | |
| 793 MOJO_DCHECK(!Dart_IsError(err)); | |
| 794 if (static_cast<uintptr_t>(handles_len) < hlen) { | |
| 795 uint32_t* new_handle_data = new uint32_t[hlen]; | |
| 796 handles = Dart_NewExternalTypedData(Dart_TypedData_kUint32, | |
| 797 new_handle_data, hlen); | |
| 798 MOJO_DCHECK(!Dart_IsError(handles)); | |
| 799 Dart_NewWeakPersistentHandle(handles, new_handle_data, | |
| 800 hlen * sizeof(uint32_t), | |
| 801 HandleArrayFinalizer); | |
| 802 } | |
| 803 } | |
| 804 | |
| 805 if (blen > 0) { | |
| 806 err = Dart_TypedDataAcquireData(data, &typ, &bytes, &bytes_len); | |
| 807 MOJO_DCHECK(!Dart_IsError(err)); | |
| 808 } | |
| 809 | |
| 810 if (hlen > 0) { | |
| 811 err = Dart_TypedDataAcquireData(handles, &typ, &handle_bytes, &handles_len); | |
| 812 MOJO_DCHECK(!Dart_IsError(err)); | |
| 813 } | |
| 814 | |
| 815 res = MojoReadMessage(static_cast<MojoHandle>(dart_handle), bytes, &blen, | |
| 816 reinterpret_cast<MojoHandle*>(handle_bytes), &hlen, | |
| 817 static_cast<MojoReadMessageFlags>(flags)); | |
| 818 | |
| 819 if (blen > 0) { | |
| 820 err = Dart_TypedDataReleaseData(data); | |
| 821 MOJO_DCHECK(!Dart_IsError(err)); | |
| 822 } | |
| 823 | |
| 824 if (hlen > 0) { | |
| 825 err = Dart_TypedDataReleaseData(handles); | |
| 826 MOJO_DCHECK(!Dart_IsError(err)); | |
| 827 } | |
| 828 | |
| 829 Dart_ListSetAt(result, 0, Dart_NewInteger(res)); | |
| 830 Dart_ListSetAt(result, 1, data); | |
| 831 Dart_ListSetAt(result, 2, handles); | |
| 832 Dart_ListSetAt(result, 3, Dart_NewInteger(blen)); | |
| 833 Dart_ListSetAt(result, 4, Dart_NewInteger(hlen)); | |
| 834 } | |
| 835 | |
| 836 | |
| 837 void MojoHandleWatcher_SendControlData(Dart_NativeArguments arguments) { | |
| 838 int64_t control_handle = 0; | |
| 839 int64_t command_code; | |
| 840 int64_t handle_or_deadline = 0; | |
| 841 CHECK_INTEGER_ARGUMENT(arguments, 0, &control_handle, InvalidArgument); | |
| 842 CHECK_INTEGER_ARGUMENT(arguments, 1, &command_code, InvalidArgument); | |
| 843 CHECK_INTEGER_ARGUMENT(arguments, 2, &handle_or_deadline, InvalidArgument); | |
| 844 | |
| 845 Dart_Handle send_port_handle = Dart_GetNativeArgument(arguments, 3); | |
| 846 Dart_Port send_port_id = ILLEGAL_PORT; | |
| 847 if (!Dart_IsNull(send_port_handle)) { | |
| 848 Dart_Handle result = Dart_SendPortGetId(send_port_handle, &send_port_id); | |
| 849 if (Dart_IsError(result)) { | |
| 850 SetInvalidArgumentReturn(arguments); | |
| 851 return; | |
| 852 } | |
| 853 } | |
| 854 | |
| 855 int64_t signals = 0; | |
| 856 CHECK_INTEGER_ARGUMENT(arguments, 4, &signals, InvalidArgument); | |
| 857 | |
| 858 HandleWatcherCommand command = | |
| 859 HandleWatcherCommand::FromDart(command_code, | |
| 860 handle_or_deadline, | |
| 861 send_port_id, | |
| 862 signals); | |
| 863 MojoResult res = MojoWriteMessage( | |
| 864 control_handle, | |
| 865 reinterpret_cast<const void*>(&command), | |
| 866 sizeof(command), | |
| 867 nullptr, | |
| 868 0, | |
| 869 0); | |
| 870 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); | |
| 871 } | |
| 872 | |
| 873 } // namespace dart | |
| 874 } // namespace mojo | |
| OLD | NEW |