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