OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdio.h> | 5 #include <stdio.h> |
6 #include <string.h> | 6 #include <string.h> |
7 #include <set> | 7 #include <set> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 18 matching lines...) Expand all Loading... | |
29 V(MojoDataPipe_BeginWriteData, 3) \ | 29 V(MojoDataPipe_BeginWriteData, 3) \ |
30 V(MojoDataPipe_EndWriteData, 2) \ | 30 V(MojoDataPipe_EndWriteData, 2) \ |
31 V(MojoDataPipe_ReadData, 4) \ | 31 V(MojoDataPipe_ReadData, 4) \ |
32 V(MojoDataPipe_BeginReadData, 3) \ | 32 V(MojoDataPipe_BeginReadData, 3) \ |
33 V(MojoDataPipe_EndReadData, 2) \ | 33 V(MojoDataPipe_EndReadData, 2) \ |
34 V(MojoMessagePipe_Create, 1) \ | 34 V(MojoMessagePipe_Create, 1) \ |
35 V(MojoMessagePipe_Write, 5) \ | 35 V(MojoMessagePipe_Write, 5) \ |
36 V(MojoMessagePipe_Read, 5) \ | 36 V(MojoMessagePipe_Read, 5) \ |
37 V(MojoHandle_Close, 1) \ | 37 V(MojoHandle_Close, 1) \ |
38 V(MojoHandle_Wait, 3) \ | 38 V(MojoHandle_Wait, 3) \ |
39 V(MojoHandle_Register, 1) \ | 39 V(MojoHandle_Register, 2) \ |
40 V(MojoHandle_WaitMany, 3) \ | 40 V(MojoHandle_WaitMany, 3) \ |
41 V(MojoHandleWatcher_SendControlData, 4) \ | 41 V(MojoHandleWatcher_SendControlData, 4) \ |
42 V(MojoHandleWatcher_RecvControlData, 1) \ | 42 V(MojoHandleWatcher_RecvControlData, 1) \ |
43 V(MojoHandleWatcher_SetControlHandle, 1) \ | 43 V(MojoHandleWatcher_SetControlHandle, 1) \ |
44 V(MojoHandleWatcher_GetControlHandle, 0) | 44 V(MojoHandleWatcher_GetControlHandle, 0) |
45 | 45 |
46 MOJO_NATIVE_LIST(DECLARE_FUNCTION); | 46 MOJO_NATIVE_LIST(DECLARE_FUNCTION); |
47 | 47 |
48 static struct NativeEntries { | 48 static struct NativeEntries { |
49 const char* name; | 49 const char* name; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 | 84 |
85 static void SetNullReturn(Dart_NativeArguments arguments) { | 85 static void SetNullReturn(Dart_NativeArguments arguments) { |
86 Dart_SetReturnValue(arguments, Dart_Null()); | 86 Dart_SetReturnValue(arguments, Dart_Null()); |
87 } | 87 } |
88 | 88 |
89 static void SetInvalidArgumentReturn(Dart_NativeArguments arguments) { | 89 static void SetInvalidArgumentReturn(Dart_NativeArguments arguments) { |
90 Dart_SetIntegerReturnValue( | 90 Dart_SetIntegerReturnValue( |
91 arguments, static_cast<int64_t>(MOJO_RESULT_INVALID_ARGUMENT)); | 91 arguments, static_cast<int64_t>(MOJO_RESULT_INVALID_ARGUMENT)); |
92 } | 92 } |
93 | 93 |
94 static Dart_Handle MojoLib() { | 94 static Dart_Handle SignalsStateToDart(const MojoHandleSignalsState& state) { |
95 Dart_Handle core_lib_name = Dart_NewStringFromCString("dart:mojo.core"); | 95 Dart_Handle list = Dart_NewList(2); |
zra
2015/03/20 21:56:25
This library is no longer in the snapshot.
| |
96 return Dart_LookupLibrary(core_lib_name); | 96 Dart_Handle arg0 = Dart_NewInteger(state.satisfied_signals); |
97 } | 97 Dart_Handle arg1 = Dart_NewInteger(state.satisfiable_signals); |
98 | 98 Dart_ListSetAt(list, 0, arg0); |
99 static Dart_Handle SignalsStateToDart(Dart_Handle klass, | 99 Dart_ListSetAt(list, 1, arg1); |
100 const MojoHandleSignalsState& state) { | 100 return list; |
101 Dart_Handle arg1 = Dart_NewInteger(state.satisfied_signals); | |
102 Dart_Handle arg2 = Dart_NewInteger(state.satisfiable_signals); | |
103 Dart_Handle args[] = {arg1, arg2}; | |
104 return Dart_New(klass, Dart_Null(), 2, args); | |
105 } | 101 } |
106 | 102 |
107 #define CHECK_INTEGER_ARGUMENT(args, num, result, failure) \ | 103 #define CHECK_INTEGER_ARGUMENT(args, num, result, failure) \ |
108 { \ | 104 { \ |
109 Dart_Handle __status; \ | 105 Dart_Handle __status; \ |
110 __status = Dart_GetNativeIntegerArgument(args, num, result); \ | 106 __status = Dart_GetNativeIntegerArgument(args, num, result); \ |
111 if (Dart_IsError(__status)) { \ | 107 if (Dart_IsError(__status)) { \ |
112 Set##failure##Return(arguments); \ | 108 Set##failure##Return(arguments); \ |
113 return; \ | 109 return; \ |
114 } \ | 110 } \ |
(...skipping 13 matching lines...) Expand all Loading... | |
128 if (res == MOJO_RESULT_OK) { | 124 if (res == MOJO_RESULT_OK) { |
129 // If this finalizer callback successfully closes a handle, it means that | 125 // If this finalizer callback successfully closes a handle, it means that |
130 // the handle has leaked from the Dart code, which is an error. | 126 // the handle has leaked from the Dart code, which is an error. |
131 LOG(ERROR) << "Handle Finalizer closing handle:\n\tisolate: " | 127 LOG(ERROR) << "Handle Finalizer closing handle:\n\tisolate: " |
132 << "\n\thandle: " << callback_peer->handle; | 128 << "\n\thandle: " << callback_peer->handle; |
133 } | 129 } |
134 } | 130 } |
135 delete callback_peer; | 131 delete callback_peer; |
136 } | 132 } |
137 | 133 |
138 // Setup a weak persistent handle for a Dart MojoHandle that calls MojoClose | 134 // Setup a weak persistent handle for a MojoHandle that calls MojoClose on the |
139 // on the handle when the MojoHandle is GC'd or the VM is going down. | 135 // handle when the MojoHandle is GC'd or the VM is going down. |
140 void MojoHandle_Register(Dart_NativeArguments arguments) { | 136 void MojoHandle_Register(Dart_NativeArguments arguments) { |
141 // An instance of Dart class MojoHandle. | |
142 Dart_Handle mojo_handle_instance = Dart_GetNativeArgument(arguments, 0); | 137 Dart_Handle mojo_handle_instance = Dart_GetNativeArgument(arguments, 0); |
143 if (!Dart_IsInstance(mojo_handle_instance)) { | 138 if (!Dart_IsInstance(mojo_handle_instance)) { |
144 SetInvalidArgumentReturn(arguments); | 139 SetInvalidArgumentReturn(arguments); |
145 return; | 140 return; |
146 } | 141 } |
147 // TODO(zra): Here, we could check that mojo_handle_instance is really a | |
148 // MojoHandle instance, but with the Dart API it's not too easy to get a Type | |
149 // object from the class name outside of the root library. For now, we'll rely | |
150 // on the existence of the right fields to be sufficient. | |
151 | |
152 Dart_Handle raw_mojo_handle_instance = Dart_GetField( | |
153 mojo_handle_instance, Dart_NewStringFromCString("_handle")); | |
154 if (Dart_IsError(raw_mojo_handle_instance)) { | |
155 SetInvalidArgumentReturn(arguments); | |
156 return; | |
157 } | |
158 | |
159 Dart_Handle mojo_handle = Dart_GetField( | |
160 raw_mojo_handle_instance, Dart_NewStringFromCString("h")); | |
161 if (Dart_IsError(mojo_handle)) { | |
162 SetInvalidArgumentReturn(arguments); | |
163 return; | |
164 } | |
165 | 142 |
166 int64_t raw_handle = static_cast<int64_t>(MOJO_HANDLE_INVALID); | 143 int64_t raw_handle = static_cast<int64_t>(MOJO_HANDLE_INVALID); |
167 Dart_Handle result = Dart_IntegerToInt64(mojo_handle, &raw_handle); | 144 CHECK_INTEGER_ARGUMENT(arguments, 1, &raw_handle, InvalidArgument); |
168 if (Dart_IsError(result)) { | |
169 SetInvalidArgumentReturn(arguments); | |
170 return; | |
171 } | |
172 | |
173 if (raw_handle == static_cast<int64_t>(MOJO_HANDLE_INVALID)) { | 145 if (raw_handle == static_cast<int64_t>(MOJO_HANDLE_INVALID)) { |
174 SetInvalidArgumentReturn(arguments); | 146 SetInvalidArgumentReturn(arguments); |
175 return; | 147 return; |
176 } | 148 } |
177 | 149 |
178 // Add the handle to this isolate's set. | 150 // Add the handle to this isolate's set. |
179 MojoHandle handle = static_cast<MojoHandle>(raw_handle); | 151 MojoHandle handle = static_cast<MojoHandle>(raw_handle); |
180 Dart_Isolate isolate = Dart_CurrentIsolate(); | 152 Dart_Isolate isolate = Dart_CurrentIsolate(); |
181 void* data = Dart_IsolateData(isolate); | 153 void* data = Dart_IsolateData(isolate); |
182 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(data); | 154 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(data); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
214 int64_t deadline = 0; | 186 int64_t deadline = 0; |
215 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); | 187 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); |
216 CHECK_INTEGER_ARGUMENT(arguments, 1, &signals, InvalidArgument); | 188 CHECK_INTEGER_ARGUMENT(arguments, 1, &signals, InvalidArgument); |
217 CHECK_INTEGER_ARGUMENT(arguments, 2, &deadline, InvalidArgument); | 189 CHECK_INTEGER_ARGUMENT(arguments, 2, &deadline, InvalidArgument); |
218 | 190 |
219 MojoHandleSignalsState state; | 191 MojoHandleSignalsState state; |
220 MojoResult r = mojo::Wait(mojo::Handle(static_cast<MojoHandle>(handle)), | 192 MojoResult r = mojo::Wait(mojo::Handle(static_cast<MojoHandle>(handle)), |
221 static_cast<MojoHandleSignals>(signals), | 193 static_cast<MojoHandleSignals>(signals), |
222 static_cast<MojoDeadline>(deadline), &state); | 194 static_cast<MojoDeadline>(deadline), &state); |
223 | 195 |
224 Dart_Handle klass = Dart_GetClass( | |
225 MojoLib(), Dart_NewStringFromCString("MojoHandleSignalsState")); | |
zra
2015/03/20 21:56:25
Instead of making an instance of the State class f
| |
226 DART_CHECK_VALID(klass); | |
227 | |
228 // The return value is structured as a list of length 2: | 196 // The return value is structured as a list of length 2: |
229 // [0] MojoResult | 197 // [0] MojoResult |
230 // [1] MojoHandleSignalsState. (may be null) | 198 // [1] MojoHandleSignalsState. (may be null) |
231 Dart_Handle list = Dart_NewList(2); | 199 Dart_Handle list = Dart_NewList(2); |
232 Dart_ListSetAt(list, 0, Dart_NewInteger(r)); | 200 Dart_ListSetAt(list, 0, Dart_NewInteger(r)); |
233 if (mojo::WaitManyResult(r).AreSignalsStatesValid()) { | 201 if (mojo::WaitManyResult(r).AreSignalsStatesValid()) { |
234 Dart_ListSetAt(list, 1, SignalsStateToDart(klass, state)); | 202 Dart_ListSetAt(list, 1, SignalsStateToDart(state)); |
235 } else { | 203 } else { |
236 Dart_ListSetAt(list, 1, Dart_Null()); | 204 Dart_ListSetAt(list, 1, Dart_Null()); |
237 } | 205 } |
238 Dart_SetReturnValue(arguments, list); | 206 Dart_SetReturnValue(arguments, list); |
239 } | 207 } |
240 | 208 |
241 void MojoHandle_WaitMany(Dart_NativeArguments arguments) { | 209 void MojoHandle_WaitMany(Dart_NativeArguments arguments) { |
242 int64_t deadline = 0; | 210 int64_t deadline = 0; |
243 Dart_Handle handles = Dart_GetNativeArgument(arguments, 0); | 211 Dart_Handle handles = Dart_GetNativeArgument(arguments, 0); |
244 Dart_Handle signals = Dart_GetNativeArgument(arguments, 1); | 212 Dart_Handle signals = Dart_GetNativeArgument(arguments, 1); |
(...skipping 28 matching lines...) Expand all Loading... | |
273 Dart_IntegerToInt64(dart_handle, &mojo_handle); | 241 Dart_IntegerToInt64(dart_handle, &mojo_handle); |
274 Dart_IntegerToInt64(dart_signal, &mojo_signal); | 242 Dart_IntegerToInt64(dart_signal, &mojo_signal); |
275 mojo_handles[i] = mojo::Handle(mojo_handle); | 243 mojo_handles[i] = mojo::Handle(mojo_handle); |
276 mojo_signals[i] = static_cast<MojoHandleSignals>(mojo_signal); | 244 mojo_signals[i] = static_cast<MojoHandleSignals>(mojo_signal); |
277 } | 245 } |
278 | 246 |
279 std::vector<MojoHandleSignalsState> states(handles_len); | 247 std::vector<MojoHandleSignalsState> states(handles_len); |
280 mojo::WaitManyResult wmr = mojo::WaitMany( | 248 mojo::WaitManyResult wmr = mojo::WaitMany( |
281 mojo_handles, mojo_signals, static_cast<MojoDeadline>(deadline), &states); | 249 mojo_handles, mojo_signals, static_cast<MojoDeadline>(deadline), &states); |
282 | 250 |
283 Dart_Handle klass = Dart_GetClass( | |
284 MojoLib(), Dart_NewStringFromCString("MojoHandleSignalsState")); | |
285 DART_CHECK_VALID(klass); | |
286 | |
287 // The return value is structured as a list of length 3: | 251 // The return value is structured as a list of length 3: |
288 // [0] MojoResult | 252 // [0] MojoResult |
289 // [1] index of handle that caused a return (may be null) | 253 // [1] index of handle that caused a return (may be null) |
290 // [2] list of MojoHandleSignalsState. (may be null) | 254 // [2] list of MojoHandleSignalsState. (may be null) |
291 Dart_Handle list = Dart_NewList(3); | 255 Dart_Handle list = Dart_NewList(3); |
292 Dart_ListSetAt(list, 0, Dart_NewInteger(wmr.result)); | 256 Dart_ListSetAt(list, 0, Dart_NewInteger(wmr.result)); |
293 if (wmr.IsIndexValid()) | 257 if (wmr.IsIndexValid()) |
294 Dart_ListSetAt(list, 1, Dart_NewInteger(wmr.index)); | 258 Dart_ListSetAt(list, 1, Dart_NewInteger(wmr.index)); |
295 else | 259 else |
296 Dart_ListSetAt(list, 1, Dart_Null()); | 260 Dart_ListSetAt(list, 1, Dart_Null()); |
297 if (wmr.AreSignalsStatesValid()) { | 261 if (wmr.AreSignalsStatesValid()) { |
298 Dart_Handle stateList = Dart_NewList(handles_len); | 262 Dart_Handle stateList = Dart_NewList(handles_len); |
299 for (int i = 0; i < handles_len; i++) { | 263 for (int i = 0; i < handles_len; i++) { |
300 Dart_ListSetAt(stateList, i, SignalsStateToDart(klass, states[i])); | 264 Dart_ListSetAt(stateList, i, SignalsStateToDart(states[i])); |
301 } | 265 } |
302 Dart_ListSetAt(list, 2, stateList); | 266 Dart_ListSetAt(list, 2, stateList); |
303 } else { | 267 } else { |
304 Dart_ListSetAt(list, 2, Dart_Null()); | 268 Dart_ListSetAt(list, 2, Dart_Null()); |
305 } | 269 } |
306 Dart_SetReturnValue(arguments, list); | 270 Dart_SetReturnValue(arguments, list); |
307 } | 271 } |
308 | 272 |
309 void MojoSharedBuffer_Create(Dart_NativeArguments arguments) { | 273 void MojoSharedBuffer_Create(Dart_NativeArguments arguments) { |
310 int64_t num_bytes = 0; | 274 int64_t num_bytes = 0; |
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
816 mojo_control_handle = control_handle; | 780 mojo_control_handle = control_handle; |
817 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); | 781 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); |
818 } | 782 } |
819 | 783 |
820 void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) { | 784 void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) { |
821 Dart_SetIntegerReturnValue(arguments, mojo_control_handle); | 785 Dart_SetIntegerReturnValue(arguments, mojo_control_handle); |
822 } | 786 } |
823 | 787 |
824 } // namespace dart | 788 } // namespace dart |
825 } // namespace mojo | 789 } // namespace mojo |
OLD | NEW |