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 <stdio.h> |
| 6 #include <string.h> |
| 7 |
| 8 #include "dart_api.h" |
| 9 |
| 10 #include "mojo/public/c/system/core.h" |
| 11 #include "mojo/public/platform/native/system_thunks.h" |
| 12 |
| 13 static Dart_NativeFunction ResolveName( |
| 14 Dart_Handle name, int argc, bool* auto_setup_scope); |
| 15 |
| 16 DART_EXPORT Dart_Handle mojo_dart_system_Init(Dart_Handle parent_library) { |
| 17 if (Dart_IsError(parent_library)) { |
| 18 return parent_library; |
| 19 } |
| 20 |
| 21 Dart_Handle result_code = Dart_SetNativeResolver( |
| 22 parent_library, ResolveName, NULL); |
| 23 if (Dart_IsError(result_code)) { |
| 24 return result_code; |
| 25 } |
| 26 |
| 27 return Dart_Null(); |
| 28 } |
| 29 |
| 30 |
| 31 static Dart_Handle HandleError(Dart_Handle handle) { |
| 32 if (Dart_IsError(handle)) { |
| 33 Dart_PropagateError(handle); |
| 34 } |
| 35 return handle; |
| 36 } |
| 37 |
| 38 |
| 39 static void SetNullReturnExitScope(Dart_NativeArguments arguments) { |
| 40 Dart_SetReturnValue(arguments, Dart_Null()); |
| 41 Dart_ExitScope(); |
| 42 } |
| 43 |
| 44 |
| 45 static void SetInvalidArgumentReturnExitScope(Dart_NativeArguments arguments) { |
| 46 Dart_SetReturnValue(arguments, Dart_NewInteger(MOJO_RESULT_INVALID_ARGUMENT)); |
| 47 Dart_ExitScope(); |
| 48 } |
| 49 |
| 50 |
| 51 #define CHECK_INTEGER_ARGUMENT(args, num, result, failure) \ |
| 52 { \ |
| 53 Dart_Handle __status; \ |
| 54 __status = Dart_GetNativeIntegerArgument(args, num, result); \ |
| 55 if (Dart_IsError(__status)) { \ |
| 56 Set##failure##ReturnExitScope(arguments); \ |
| 57 return; \ |
| 58 } \ |
| 59 } \ |
| 60 |
| 61 |
| 62 extern "C" { |
| 63 extern size_t MojoSetSystemThunks(const MojoSystemThunks* system_thunks); |
| 64 } |
| 65 |
| 66 |
| 67 static void MojoSystemThunks_Set(Dart_NativeArguments arguments) { |
| 68 Dart_EnterScope(); |
| 69 int64_t thunks_addr = 0; |
| 70 CHECK_INTEGER_ARGUMENT(arguments, 0, &thunks_addr, Null); |
| 71 |
| 72 MojoSystemThunks* thunks = reinterpret_cast<MojoSystemThunks*>(thunks_addr); |
| 73 MojoSetSystemThunks(thunks); |
| 74 |
| 75 Dart_SetReturnValue(arguments, Dart_Null()); |
| 76 Dart_ExitScope(); |
| 77 } |
| 78 |
| 79 |
| 80 static void MojoHandle_Close(Dart_NativeArguments arguments) { |
| 81 Dart_EnterScope(); |
| 82 int64_t handle; |
| 83 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); |
| 84 |
| 85 MojoResult res = MojoClose(static_cast<MojoHandle>(handle)); |
| 86 |
| 87 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); |
| 88 Dart_ExitScope(); |
| 89 } |
| 90 |
| 91 |
| 92 static void MojoHandle_Wait(Dart_NativeArguments arguments) { |
| 93 Dart_EnterScope(); |
| 94 int64_t handle = 0; |
| 95 int64_t signals = 0; |
| 96 int64_t deadline = 0; |
| 97 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); |
| 98 CHECK_INTEGER_ARGUMENT(arguments, 1, &signals, InvalidArgument); |
| 99 CHECK_INTEGER_ARGUMENT(arguments, 2, &deadline, InvalidArgument); |
| 100 |
| 101 MojoResult r = MojoWait(static_cast<MojoHandle>(handle), |
| 102 static_cast<MojoHandleSignals>(signals), |
| 103 static_cast<MojoDeadline>(deadline)); |
| 104 |
| 105 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(r)); |
| 106 Dart_ExitScope(); |
| 107 } |
| 108 |
| 109 |
| 110 static void MojoHandle_WaitMany(Dart_NativeArguments arguments) { |
| 111 Dart_EnterScope(); |
| 112 int64_t num_handles = 0; |
| 113 int64_t deadline = 0; |
| 114 Dart_Handle handles = Dart_GetNativeArgument(arguments, 0); |
| 115 Dart_Handle signals = Dart_GetNativeArgument(arguments, 1); |
| 116 CHECK_INTEGER_ARGUMENT(arguments, 2, &num_handles, InvalidArgument); |
| 117 CHECK_INTEGER_ARGUMENT(arguments, 3, &deadline, InvalidArgument); |
| 118 |
| 119 if (!Dart_IsList(handles) || !Dart_IsList(signals)) { |
| 120 SetInvalidArgumentReturnExitScope(arguments); |
| 121 return; |
| 122 } |
| 123 |
| 124 intptr_t handles_len = 0; |
| 125 intptr_t signals_len = 0; |
| 126 Dart_ListLength(handles, &handles_len); |
| 127 Dart_ListLength(signals, &signals_len); |
| 128 if ((handles_len != num_handles) || (signals_len != num_handles)) { |
| 129 SetInvalidArgumentReturnExitScope(arguments); |
| 130 return; |
| 131 } |
| 132 |
| 133 MojoHandle* mojo_handles = new MojoHandle[num_handles]; |
| 134 MojoHandleSignals* mojo_signals = new MojoHandleSignals[num_handles]; |
| 135 |
| 136 for (int i = 0; i < num_handles; i++) { |
| 137 Dart_Handle dart_handle = Dart_ListGetAt(handles, i); |
| 138 Dart_Handle dart_signal = Dart_ListGetAt(signals, i); |
| 139 if (!Dart_IsInteger(dart_handle) || !Dart_IsInteger(dart_signal)) { |
| 140 delete[] mojo_handles; |
| 141 delete[] mojo_signals; |
| 142 SetInvalidArgumentReturnExitScope(arguments); |
| 143 return; |
| 144 } |
| 145 int64_t mojo_handle = 0; |
| 146 int64_t mojo_signal = 0; |
| 147 Dart_IntegerToInt64(dart_handle, &mojo_handle); |
| 148 Dart_IntegerToInt64(dart_signal, &mojo_signal); |
| 149 mojo_handles[i] = static_cast<MojoHandle>(mojo_handle); |
| 150 mojo_signals[i] = static_cast<MojoHandleSignals>(mojo_signal); |
| 151 } |
| 152 |
| 153 MojoResult res = MojoWaitMany(mojo_handles, mojo_signals, |
| 154 static_cast<uint32_t>(num_handles), |
| 155 static_cast<MojoDeadline>(deadline)); |
| 156 |
| 157 delete[] mojo_handles; |
| 158 delete[] mojo_signals; |
| 159 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); |
| 160 Dart_ExitScope(); |
| 161 } |
| 162 |
| 163 |
| 164 static void MojoSharedBuffer_Create(Dart_NativeArguments arguments) { |
| 165 Dart_EnterScope(); |
| 166 int64_t num_bytes = 0; |
| 167 int64_t flags = 0; |
| 168 CHECK_INTEGER_ARGUMENT(arguments, 0, &num_bytes, Null); |
| 169 CHECK_INTEGER_ARGUMENT(arguments, 1, &flags, Null); |
| 170 |
| 171 MojoCreateSharedBufferOptions options; |
| 172 options.struct_size = sizeof(MojoCreateSharedBufferOptions); |
| 173 options.flags = static_cast<MojoCreateSharedBufferOptionsFlags>(flags); |
| 174 |
| 175 MojoHandle out = MOJO_HANDLE_INVALID;; |
| 176 MojoResult res = MojoCreateSharedBuffer( |
| 177 &options, static_cast<int32_t>(num_bytes), &out); |
| 178 |
| 179 Dart_Handle list = Dart_NewList(2); |
| 180 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); |
| 181 Dart_ListSetAt(list, 1, Dart_NewInteger(out)); |
| 182 Dart_SetReturnValue(arguments, list); |
| 183 Dart_ExitScope(); |
| 184 } |
| 185 |
| 186 |
| 187 static void MojoSharedBuffer_Duplicate(Dart_NativeArguments arguments) { |
| 188 Dart_EnterScope(); |
| 189 int64_t handle = 0; |
| 190 int64_t flags = 0; |
| 191 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); |
| 192 CHECK_INTEGER_ARGUMENT(arguments, 1, &flags, Null); |
| 193 |
| 194 MojoDuplicateBufferHandleOptions options; |
| 195 options.struct_size = sizeof(MojoDuplicateBufferHandleOptions); |
| 196 options.flags = static_cast<MojoDuplicateBufferHandleOptionsFlags>(flags); |
| 197 |
| 198 MojoHandle out = MOJO_HANDLE_INVALID;; |
| 199 MojoResult res = MojoDuplicateBufferHandle( |
| 200 static_cast<MojoHandle>(handle), &options, &out); |
| 201 |
| 202 Dart_Handle list = Dart_NewList(2); |
| 203 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); |
| 204 Dart_ListSetAt(list, 1, Dart_NewInteger(out)); |
| 205 Dart_SetReturnValue(arguments, list); |
| 206 Dart_ExitScope(); |
| 207 } |
| 208 |
| 209 |
| 210 static void MojoSharedBuffer_Map(Dart_NativeArguments arguments) { |
| 211 Dart_EnterScope(); |
| 212 int64_t handle = 0; |
| 213 int64_t offset = 0; |
| 214 int64_t num_bytes = 0; |
| 215 int64_t flags = 0; |
| 216 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); |
| 217 CHECK_INTEGER_ARGUMENT(arguments, 1, &offset, Null); |
| 218 CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, Null); |
| 219 CHECK_INTEGER_ARGUMENT(arguments, 3, &flags, Null); |
| 220 |
| 221 void* out; |
| 222 MojoResult res = MojoMapBuffer(static_cast<MojoHandle>(handle), |
| 223 offset, |
| 224 num_bytes, |
| 225 &out, |
| 226 static_cast<MojoMapBufferFlags>(flags)); |
| 227 |
| 228 Dart_Handle list = Dart_NewList(2); |
| 229 Dart_Handle typed_data; |
| 230 if (res == MOJO_RESULT_OK) { |
| 231 typed_data = Dart_NewExternalTypedData( |
| 232 Dart_TypedData_kByteData, out, num_bytes); |
| 233 } else { |
| 234 typed_data = Dart_Null(); |
| 235 } |
| 236 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); |
| 237 Dart_ListSetAt(list, 1, typed_data); |
| 238 Dart_SetReturnValue(arguments, list); |
| 239 Dart_ExitScope(); |
| 240 } |
| 241 |
| 242 |
| 243 static void MojoSharedBuffer_Unmap(Dart_NativeArguments arguments) { |
| 244 Dart_EnterScope(); |
| 245 Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 0); |
| 246 if (!Dart_IsTypedData(typed_data)) { |
| 247 SetInvalidArgumentReturnExitScope(arguments); |
| 248 return; |
| 249 } |
| 250 |
| 251 Dart_TypedData_Type typ; |
| 252 void *data; |
| 253 intptr_t len; |
| 254 Dart_TypedDataAcquireData(typed_data, &typ, &data, &len); |
| 255 MojoResult res = MojoUnmapBuffer(data); |
| 256 Dart_TypedDataReleaseData(typed_data); |
| 257 |
| 258 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); |
| 259 Dart_ExitScope(); |
| 260 } |
| 261 |
| 262 |
| 263 static void MojoDataPipe_Create(Dart_NativeArguments arguments) { |
| 264 Dart_EnterScope(); |
| 265 int64_t element_bytes = 0; |
| 266 int64_t capacity_bytes = 0; |
| 267 int64_t flags = 0; |
| 268 CHECK_INTEGER_ARGUMENT(arguments, 0, &element_bytes, Null); |
| 269 CHECK_INTEGER_ARGUMENT(arguments, 1, &capacity_bytes, Null); |
| 270 CHECK_INTEGER_ARGUMENT(arguments, 2, &flags, Null); |
| 271 |
| 272 MojoCreateDataPipeOptions options; |
| 273 options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| 274 options.flags = static_cast<MojoCreateDataPipeOptionsFlags>(flags); |
| 275 options.element_num_bytes = static_cast<uint32_t>(element_bytes); |
| 276 options.capacity_num_bytes = static_cast<uint32_t>(capacity_bytes); |
| 277 |
| 278 MojoHandle producer = MOJO_HANDLE_INVALID; |
| 279 MojoHandle consumer = MOJO_HANDLE_INVALID; |
| 280 MojoResult res = MojoCreateDataPipe(&options, &producer, &consumer); |
| 281 |
| 282 Dart_Handle list = Dart_NewList(3); |
| 283 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); |
| 284 Dart_ListSetAt(list, 1, Dart_NewInteger(producer)); |
| 285 Dart_ListSetAt(list, 2, Dart_NewInteger(consumer)); |
| 286 Dart_SetReturnValue(arguments, list); |
| 287 Dart_ExitScope(); |
| 288 } |
| 289 |
| 290 |
| 291 static void MojoDataPipe_WriteData(Dart_NativeArguments arguments) { |
| 292 Dart_EnterScope(); |
| 293 int64_t handle = 0; |
| 294 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); |
| 295 |
| 296 Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1); |
| 297 if (!Dart_IsTypedData(typed_data)) { |
| 298 SetNullReturnExitScope(arguments); |
| 299 return; |
| 300 } |
| 301 |
| 302 int64_t num_bytes = 0; |
| 303 CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, Null); |
| 304 |
| 305 int64_t flags = 0; |
| 306 CHECK_INTEGER_ARGUMENT(arguments, 3, &flags, Null); |
| 307 |
| 308 Dart_TypedData_Type typ; |
| 309 void *data; |
| 310 intptr_t bdlen; |
| 311 Dart_TypedDataAcquireData(typed_data, &typ, &data, &bdlen); |
| 312 uint32_t len = static_cast<uint32_t>(num_bytes); |
| 313 MojoResult res = MojoWriteData( |
| 314 static_cast<MojoHandle>(handle), |
| 315 data, |
| 316 &len, |
| 317 static_cast<MojoWriteDataFlags>(flags)); |
| 318 Dart_TypedDataReleaseData(typed_data); |
| 319 |
| 320 Dart_Handle list = Dart_NewList(2); |
| 321 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); |
| 322 Dart_ListSetAt(list, 1, Dart_NewInteger(len)); |
| 323 Dart_SetReturnValue(arguments, list); |
| 324 Dart_ExitScope(); |
| 325 } |
| 326 |
| 327 |
| 328 static void MojoDataPipe_BeginWriteData(Dart_NativeArguments arguments) { |
| 329 Dart_EnterScope(); |
| 330 int64_t handle = 0; |
| 331 int64_t buffer_bytes = 0; |
| 332 int64_t flags = 0; |
| 333 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); |
| 334 CHECK_INTEGER_ARGUMENT(arguments, 1, &buffer_bytes, Null); |
| 335 CHECK_INTEGER_ARGUMENT(arguments, 2, &flags, Null); |
| 336 |
| 337 void *buffer; |
| 338 uint32_t size = static_cast<uint32_t>(buffer_bytes); |
| 339 MojoResult res = MojoBeginWriteData( |
| 340 static_cast<MojoHandle>(handle), |
| 341 &buffer, |
| 342 &size, |
| 343 static_cast<MojoWriteDataFlags>(flags)); |
| 344 |
| 345 Dart_Handle list = Dart_NewList(2); |
| 346 Dart_Handle typed_data; |
| 347 if (res == MOJO_RESULT_OK) { |
| 348 typed_data = Dart_NewExternalTypedData( |
| 349 Dart_TypedData_kByteData, buffer, size); |
| 350 } else { |
| 351 typed_data = Dart_Null(); |
| 352 } |
| 353 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); |
| 354 Dart_ListSetAt(list, 1, typed_data); |
| 355 Dart_SetReturnValue(arguments, list); |
| 356 Dart_ExitScope(); |
| 357 } |
| 358 |
| 359 |
| 360 static void MojoDataPipe_EndWriteData(Dart_NativeArguments arguments) { |
| 361 Dart_EnterScope(); |
| 362 int64_t handle = 0; |
| 363 int64_t num_bytes_written = 0; |
| 364 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); |
| 365 CHECK_INTEGER_ARGUMENT(arguments, 1, &num_bytes_written, InvalidArgument); |
| 366 |
| 367 MojoResult res = MojoEndWriteData( |
| 368 static_cast<MojoHandle>(handle), |
| 369 static_cast<uint32_t>(num_bytes_written)); |
| 370 |
| 371 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); |
| 372 Dart_ExitScope(); |
| 373 } |
| 374 |
| 375 |
| 376 static void MojoDataPipe_ReadData(Dart_NativeArguments arguments) { |
| 377 Dart_EnterScope(); |
| 378 int64_t handle = 0; |
| 379 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); |
| 380 |
| 381 Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1); |
| 382 if (!Dart_IsTypedData(typed_data) && !Dart_IsNull(typed_data)) { |
| 383 SetNullReturnExitScope(arguments); |
| 384 return; |
| 385 } |
| 386 |
| 387 int64_t num_bytes = 0; |
| 388 CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, Null); |
| 389 |
| 390 int64_t flags = 0; |
| 391 CHECK_INTEGER_ARGUMENT(arguments, 3, &flags, Null); |
| 392 |
| 393 Dart_TypedData_Type typ; |
| 394 void* data = NULL; |
| 395 intptr_t bdlen = 0; |
| 396 if (!Dart_IsNull(typed_data)) { |
| 397 Dart_TypedDataAcquireData(typed_data, &typ, &data, &bdlen); |
| 398 } |
| 399 uint32_t len = static_cast<uint32_t>(num_bytes); |
| 400 MojoResult res = MojoReadData( |
| 401 static_cast<MojoHandle>(handle), |
| 402 data, |
| 403 &len, |
| 404 static_cast<MojoReadDataFlags>(flags)); |
| 405 if (!Dart_IsNull(typed_data)) { |
| 406 Dart_TypedDataReleaseData(typed_data); |
| 407 } |
| 408 |
| 409 Dart_Handle list = Dart_NewList(2); |
| 410 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); |
| 411 Dart_ListSetAt(list, 1, Dart_NewInteger(len)); |
| 412 Dart_SetReturnValue(arguments, list); |
| 413 Dart_ExitScope(); |
| 414 } |
| 415 |
| 416 |
| 417 static void MojoDataPipe_BeginReadData(Dart_NativeArguments arguments) { |
| 418 Dart_EnterScope(); |
| 419 int64_t handle = 0; |
| 420 int64_t buffer_bytes = 0; |
| 421 int64_t flags = 0; |
| 422 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); |
| 423 CHECK_INTEGER_ARGUMENT(arguments, 1, &buffer_bytes, Null); |
| 424 CHECK_INTEGER_ARGUMENT(arguments, 2, &flags, Null); |
| 425 |
| 426 void* buffer; |
| 427 uint32_t size = static_cast<uint32_t>(buffer_bytes); |
| 428 MojoResult res = MojoBeginReadData( |
| 429 static_cast<MojoHandle>(handle), |
| 430 const_cast<const void**>(&buffer), |
| 431 &size, |
| 432 static_cast<MojoWriteDataFlags>(flags)); |
| 433 |
| 434 Dart_Handle list = Dart_NewList(2); |
| 435 Dart_Handle typed_data; |
| 436 if (res == MOJO_RESULT_OK) { |
| 437 typed_data = Dart_NewExternalTypedData( |
| 438 Dart_TypedData_kByteData, buffer, size); |
| 439 } else { |
| 440 typed_data = Dart_Null(); |
| 441 } |
| 442 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); |
| 443 Dart_ListSetAt(list, 1, typed_data); |
| 444 Dart_SetReturnValue(arguments, list); |
| 445 Dart_ExitScope(); |
| 446 } |
| 447 |
| 448 |
| 449 static void MojoDataPipe_EndReadData(Dart_NativeArguments arguments) { |
| 450 Dart_EnterScope(); |
| 451 int64_t handle = 0; |
| 452 int64_t num_bytes_read = 0; |
| 453 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); |
| 454 CHECK_INTEGER_ARGUMENT(arguments, 1, &num_bytes_read, InvalidArgument); |
| 455 |
| 456 MojoResult res = MojoEndReadData( |
| 457 static_cast<MojoHandle>(handle), |
| 458 static_cast<uint32_t>(num_bytes_read)); |
| 459 |
| 460 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); |
| 461 Dart_ExitScope(); |
| 462 } |
| 463 |
| 464 |
| 465 static void MojoMessagePipe_Create(Dart_NativeArguments arguments) { |
| 466 Dart_EnterScope(); |
| 467 int64_t flags = 0; |
| 468 CHECK_INTEGER_ARGUMENT(arguments, 0, &flags, Null); |
| 469 |
| 470 MojoCreateMessagePipeOptions options; |
| 471 options.struct_size = sizeof(MojoCreateMessagePipeOptions); |
| 472 options.flags = static_cast<MojoCreateMessagePipeOptionsFlags>(flags); |
| 473 |
| 474 MojoHandle end1 = MOJO_HANDLE_INVALID; |
| 475 MojoHandle end2 = MOJO_HANDLE_INVALID; |
| 476 MojoResult res = MojoCreateMessagePipe(&options, &end1, &end2); |
| 477 |
| 478 Dart_Handle list = Dart_NewList(3); |
| 479 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); |
| 480 Dart_ListSetAt(list, 1, Dart_NewInteger(end1)); |
| 481 Dart_ListSetAt(list, 2, Dart_NewInteger(end2)); |
| 482 Dart_SetReturnValue(arguments, list); |
| 483 Dart_ExitScope(); |
| 484 } |
| 485 |
| 486 |
| 487 static void MojoMessagePipe_Write(Dart_NativeArguments arguments) { |
| 488 Dart_EnterScope(); |
| 489 int64_t handle = 0; |
| 490 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); |
| 491 |
| 492 Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1); |
| 493 if (!Dart_IsTypedData(typed_data) && !Dart_IsNull(typed_data)) { |
| 494 SetInvalidArgumentReturnExitScope(arguments); |
| 495 return; |
| 496 } |
| 497 |
| 498 int64_t num_bytes = 0; |
| 499 CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, InvalidArgument); |
| 500 |
| 501 Dart_Handle handles = Dart_GetNativeArgument(arguments, 3); |
| 502 if (!Dart_IsList(handles) && !Dart_IsNull(handles)) { |
| 503 SetInvalidArgumentReturnExitScope(arguments); |
| 504 return; |
| 505 } |
| 506 |
| 507 int64_t flags = 0; |
| 508 CHECK_INTEGER_ARGUMENT(arguments, 4, &flags, InvalidArgument); |
| 509 |
| 510 // Grab the data if there is any. |
| 511 Dart_TypedData_Type typ; |
| 512 void* bytes = NULL; |
| 513 intptr_t bdlen = 0; |
| 514 if (!Dart_IsNull(typed_data)) { |
| 515 Dart_TypedDataAcquireData(typed_data, &typ, &bytes, &bdlen); |
| 516 } |
| 517 |
| 518 // Grab the handles if there are any. |
| 519 MojoHandle* mojo_handles = NULL; |
| 520 intptr_t handles_len = 0; |
| 521 if (!Dart_IsNull(handles)) { |
| 522 Dart_ListLength(handles, &handles_len); |
| 523 mojo_handles = new MojoHandle[handles_len]; |
| 524 for (int i = 0; i < handles_len; i++) { |
| 525 Dart_Handle dart_handle = Dart_ListGetAt(handles, i); |
| 526 if (!Dart_IsInteger(dart_handle)) { |
| 527 delete[] mojo_handles; |
| 528 SetInvalidArgumentReturnExitScope(arguments); |
| 529 return; |
| 530 } |
| 531 int64_t mojo_handle = 0; |
| 532 Dart_IntegerToInt64(dart_handle, &mojo_handle); |
| 533 mojo_handles[i] = static_cast<MojoHandle>(mojo_handle); |
| 534 } |
| 535 } |
| 536 |
| 537 MojoResult res = MojoWriteMessage( |
| 538 static_cast<MojoHandle>(handle), |
| 539 const_cast<const void*>(bytes), |
| 540 static_cast<uint32_t>(num_bytes), |
| 541 mojo_handles, |
| 542 static_cast<uint32_t>(handles_len), |
| 543 static_cast<MojoWriteMessageFlags>(flags)); |
| 544 |
| 545 // Release the data. |
| 546 if (!Dart_IsNull(typed_data)) { |
| 547 Dart_TypedDataReleaseData(typed_data); |
| 548 } |
| 549 |
| 550 if (!Dart_IsNull(handles)) { |
| 551 delete[] mojo_handles; |
| 552 } |
| 553 |
| 554 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); |
| 555 Dart_ExitScope(); |
| 556 } |
| 557 |
| 558 |
| 559 static void MojoMessagePipe_Read(Dart_NativeArguments arguments) { |
| 560 Dart_EnterScope(); |
| 561 int64_t handle = 0; |
| 562 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); |
| 563 |
| 564 Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1); |
| 565 if (!Dart_IsTypedData(typed_data) && !Dart_IsNull(typed_data)) { |
| 566 SetNullReturnExitScope(arguments); |
| 567 return; |
| 568 } |
| 569 |
| 570 int64_t num_bytes = 0; |
| 571 CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, Null); |
| 572 |
| 573 Dart_Handle handles = Dart_GetNativeArgument(arguments, 3); |
| 574 if (!Dart_IsList(handles) && !Dart_IsNull(handles)) { |
| 575 SetNullReturnExitScope(arguments); |
| 576 return; |
| 577 } |
| 578 |
| 579 int64_t flags = 0; |
| 580 CHECK_INTEGER_ARGUMENT(arguments, 4, &flags, Null); |
| 581 |
| 582 // Grab the data if there is any. |
| 583 Dart_TypedData_Type typ; |
| 584 void* bytes = NULL; |
| 585 intptr_t byte_data_len = 0; |
| 586 if (!Dart_IsNull(typed_data)) { |
| 587 Dart_TypedDataAcquireData(typed_data, &typ, &bytes, &byte_data_len); |
| 588 } |
| 589 uint32_t blen = static_cast<uint32_t>(num_bytes); |
| 590 |
| 591 // Grab the handles if there are any. |
| 592 MojoHandle* mojo_handles = NULL; |
| 593 intptr_t handles_len = 0; |
| 594 if (!Dart_IsNull(handles)) { |
| 595 Dart_ListLength(handles, &handles_len); |
| 596 mojo_handles = new MojoHandle[handles_len]; |
| 597 } |
| 598 uint32_t hlen = static_cast<uint32_t>(handles_len); |
| 599 |
| 600 MojoResult res = MojoReadMessage( |
| 601 static_cast<MojoHandle>(handle), |
| 602 bytes, |
| 603 &blen, |
| 604 mojo_handles, |
| 605 &hlen, |
| 606 static_cast<MojoReadMessageFlags>(flags)); |
| 607 |
| 608 // Release the data. |
| 609 if (!Dart_IsNull(typed_data)) { |
| 610 Dart_TypedDataReleaseData(typed_data); |
| 611 } |
| 612 |
| 613 if (!Dart_IsNull(handles)) { |
| 614 for (int i = 0; i < handles_len; i++) { |
| 615 Dart_ListSetAt(handles, i, Dart_NewInteger(mojo_handles[i])); |
| 616 } |
| 617 delete[] mojo_handles; |
| 618 } |
| 619 |
| 620 Dart_Handle list = Dart_NewList(3); |
| 621 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); |
| 622 Dart_ListSetAt(list, 1, Dart_NewInteger(blen)); |
| 623 Dart_ListSetAt(list, 2, Dart_NewInteger(hlen)); |
| 624 Dart_SetReturnValue(arguments, list); |
| 625 Dart_ExitScope(); |
| 626 } |
| 627 |
| 628 |
| 629 struct ControlData { |
| 630 int64_t handle; |
| 631 Dart_Port port; |
| 632 int64_t data; |
| 633 }; |
| 634 |
| 635 |
| 636 static void MojoHandleWatcher_SendControlData(Dart_NativeArguments arguments) { |
| 637 Dart_EnterScope(); |
| 638 int64_t control_handle = 0; |
| 639 CHECK_INTEGER_ARGUMENT(arguments, 0, &control_handle, InvalidArgument); |
| 640 |
| 641 int64_t client_handle = 0; |
| 642 CHECK_INTEGER_ARGUMENT(arguments, 1, &client_handle, InvalidArgument); |
| 643 |
| 644 Dart_Handle send_port_handle = Dart_GetNativeArgument(arguments, 2); |
| 645 Dart_Port send_port_id = 0; |
| 646 if (!Dart_IsNull(send_port_handle)) { |
| 647 Dart_Handle result = Dart_SendPortGetId(send_port_handle, &send_port_id); |
| 648 if (Dart_IsError(result)) { |
| 649 SetInvalidArgumentReturnExitScope(arguments); |
| 650 return; |
| 651 } |
| 652 } |
| 653 |
| 654 int64_t data = 0; |
| 655 CHECK_INTEGER_ARGUMENT(arguments, 3, &data, InvalidArgument); |
| 656 |
| 657 ControlData cd; |
| 658 cd.handle = client_handle; |
| 659 cd.port = send_port_id; |
| 660 cd.data = data; |
| 661 const void* bytes = reinterpret_cast<const void*>(&cd); |
| 662 MojoResult res = MojoWriteMessage( |
| 663 control_handle, bytes, sizeof(cd), NULL, 0, 0); |
| 664 |
| 665 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); |
| 666 Dart_ExitScope(); |
| 667 } |
| 668 |
| 669 |
| 670 static void MojoHandleWatcher_RecvControlData(Dart_NativeArguments arguments) { |
| 671 Dart_EnterScope(); |
| 672 int64_t control_handle = 0; |
| 673 CHECK_INTEGER_ARGUMENT(arguments, 0, &control_handle, Null); |
| 674 |
| 675 ControlData cd; |
| 676 void* bytes = reinterpret_cast<void*>(&cd); |
| 677 uint32_t num_bytes = sizeof(cd); |
| 678 uint32_t num_handles = 0; |
| 679 MojoResult res = MojoReadMessage( |
| 680 control_handle, bytes, &num_bytes, NULL, &num_handles, 0); |
| 681 if (res != MOJO_RESULT_OK) { |
| 682 SetNullReturnExitScope(arguments); |
| 683 return; |
| 684 } |
| 685 |
| 686 Dart_Handle list = Dart_NewList(3); |
| 687 Dart_ListSetAt(list, 0, Dart_NewInteger(cd.handle)); |
| 688 Dart_ListSetAt(list, 1, Dart_NewSendPort(cd.port)); |
| 689 Dart_ListSetAt(list, 2, Dart_NewInteger(cd.data)); |
| 690 Dart_SetReturnValue(arguments, list); |
| 691 Dart_ExitScope(); |
| 692 } |
| 693 |
| 694 |
| 695 static int64_t _mojo_control_handle = MOJO_HANDLE_INVALID; |
| 696 static void MojoHandleWatcher_SetControlHandle(Dart_NativeArguments arguments) { |
| 697 Dart_EnterScope(); |
| 698 int64_t control_handle; |
| 699 CHECK_INTEGER_ARGUMENT(arguments, 0, &control_handle, InvalidArgument); |
| 700 |
| 701 if (_mojo_control_handle == MOJO_HANDLE_INVALID) { |
| 702 _mojo_control_handle = control_handle; |
| 703 } |
| 704 |
| 705 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); |
| 706 Dart_ExitScope(); |
| 707 } |
| 708 |
| 709 |
| 710 static void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) { |
| 711 Dart_EnterScope(); |
| 712 Dart_SetIntegerReturnValue(arguments, _mojo_control_handle); |
| 713 Dart_ExitScope(); |
| 714 } |
| 715 |
| 716 |
| 717 struct FunctionLookup { |
| 718 const char* name; |
| 719 Dart_NativeFunction function; |
| 720 }; |
| 721 |
| 722 |
| 723 #define HELPER_FUNCTIONS(V) \ |
| 724 V(MojoSystemThunks_Set) \ |
| 725 V(MojoHandle_Close) \ |
| 726 V(MojoHandle_Wait) \ |
| 727 V(MojoHandle_WaitMany) \ |
| 728 V(MojoSharedBuffer_Create) \ |
| 729 V(MojoSharedBuffer_Duplicate) \ |
| 730 V(MojoSharedBuffer_Map) \ |
| 731 V(MojoSharedBuffer_Unmap) \ |
| 732 V(MojoDataPipe_Create) \ |
| 733 V(MojoDataPipe_WriteData) \ |
| 734 V(MojoDataPipe_BeginWriteData) \ |
| 735 V(MojoDataPipe_EndWriteData) \ |
| 736 V(MojoDataPipe_ReadData) \ |
| 737 V(MojoDataPipe_BeginReadData) \ |
| 738 V(MojoDataPipe_EndReadData) \ |
| 739 V(MojoMessagePipe_Create) \ |
| 740 V(MojoMessagePipe_Write) \ |
| 741 V(MojoMessagePipe_Read) \ |
| 742 V(MojoHandleWatcher_SendControlData) \ |
| 743 V(MojoHandleWatcher_RecvControlData) \ |
| 744 V(MojoHandleWatcher_SetControlHandle) \ |
| 745 V(MojoHandleWatcher_GetControlHandle) \ |
| 746 |
| 747 |
| 748 #define FUNCTION_STRING_MAP(name) {#name, name}, |
| 749 |
| 750 FunctionLookup function_list[] = { |
| 751 HELPER_FUNCTIONS(FUNCTION_STRING_MAP) |
| 752 {NULL, NULL}}; |
| 753 |
| 754 #undef FUNCTION_STRING_MAP |
| 755 |
| 756 |
| 757 FunctionLookup no_scope_function_list[] = { |
| 758 {NULL, NULL} |
| 759 }; |
| 760 |
| 761 Dart_NativeFunction ResolveName(Dart_Handle name, |
| 762 int argc, |
| 763 bool* auto_setup_scope) { |
| 764 if (!Dart_IsString(name)) { |
| 765 return NULL; |
| 766 } |
| 767 Dart_NativeFunction result = NULL; |
| 768 if (auto_setup_scope == NULL) { |
| 769 return NULL; |
| 770 } |
| 771 |
| 772 Dart_EnterScope(); |
| 773 const char* cname; |
| 774 HandleError(Dart_StringToCString(name, &cname)); |
| 775 |
| 776 for (int i=0; function_list[i].name != NULL; ++i) { |
| 777 if (strcmp(function_list[i].name, cname) == 0) { |
| 778 *auto_setup_scope = true; |
| 779 result = function_list[i].function; |
| 780 break; |
| 781 } |
| 782 } |
| 783 |
| 784 if (result != NULL) { |
| 785 Dart_ExitScope(); |
| 786 return result; |
| 787 } |
| 788 |
| 789 for (int i=0; no_scope_function_list[i].name != NULL; ++i) { |
| 790 if (strcmp(no_scope_function_list[i].name, cname) == 0) { |
| 791 *auto_setup_scope = false; |
| 792 result = no_scope_function_list[i].function; |
| 793 break; |
| 794 } |
| 795 } |
| 796 |
| 797 Dart_ExitScope(); |
| 798 return result; |
| 799 } |
OLD | NEW |