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