OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "bin/file.h" | 5 #include "bin/file.h" |
6 | 6 |
7 #include "bin/builtin.h" | 7 #include "bin/builtin.h" |
8 #include "bin/dartutils.h" | 8 #include "bin/dartutils.h" |
9 #include "bin/io_buffer.h" | 9 #include "bin/io_buffer.h" |
10 #include "bin/thread.h" | 10 #include "bin/thread.h" |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 } | 247 } |
248 delete[] buffer; | 248 delete[] buffer; |
249 Dart_ExitScope(); | 249 Dart_ExitScope(); |
250 } | 250 } |
251 | 251 |
252 | 252 |
253 void FUNCTION_NAME(File_WriteFrom)(Dart_NativeArguments args) { | 253 void FUNCTION_NAME(File_WriteFrom)(Dart_NativeArguments args) { |
254 Dart_EnterScope(); | 254 Dart_EnterScope(); |
255 File* file = GetFilePointer(Dart_GetNativeArgument(args, 0)); | 255 File* file = GetFilePointer(Dart_GetNativeArgument(args, 0)); |
256 ASSERT(file != NULL); | 256 ASSERT(file != NULL); |
| 257 |
257 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 258 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
258 ASSERT(Dart_IsList(buffer_obj)); | 259 |
259 // Offset and length arguments are checked in Dart code to be | 260 // Offset and length arguments are checked in Dart code to be |
260 // integers and have the property that (offset + length) <= | 261 // integers and have the property that (offset + length) <= |
261 // list.length. Therefore, it is safe to extract their value as | 262 // list.length. Therefore, it is safe to extract their value as |
262 // intptr_t. | 263 // intptr_t. |
263 intptr_t start = | 264 intptr_t start = |
264 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | 265 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); |
265 intptr_t end = | 266 intptr_t end = |
266 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); | 267 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); |
| 268 |
| 269 // The buffer object passed in has to be an Int8List or Uint8List object. |
| 270 // Acquire a direct pointer to the data area of the buffer object. |
| 271 Dart_TypedData_Type type; |
267 intptr_t length = end - start; | 272 intptr_t length = end - start; |
268 intptr_t buffer_len = 0; | 273 intptr_t buffer_len = 0; |
269 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len); | 274 void* buffer = NULL; |
| 275 Dart_Handle result = |
| 276 Dart_TypedDataAcquireData(buffer_obj, &type, &buffer, &buffer_len); |
270 if (Dart_IsError(result)) Dart_PropagateError(result); | 277 if (Dart_IsError(result)) Dart_PropagateError(result); |
| 278 |
| 279 ASSERT(type == Dart_TypedData_kUint8 || type == Dart_TypedData_kInt8); |
271 ASSERT(end <= buffer_len); | 280 ASSERT(end <= buffer_len); |
272 uint8_t* buffer = new uint8_t[length]; | 281 ASSERT(buffer != NULL); |
273 result = Dart_ListGetAsBytes(buffer_obj, start, buffer, length); | 282 |
274 if (Dart_IsError(result)) { | 283 // Write the data out into the file. |
275 delete[] buffer; | |
276 Dart_PropagateError(result); | |
277 } | |
278 int64_t bytes_written = file->Write(reinterpret_cast<void*>(buffer), length); | 284 int64_t bytes_written = file->Write(reinterpret_cast<void*>(buffer), length); |
| 285 |
| 286 // Release the direct pointer acquired above. |
| 287 result = Dart_TypedDataReleaseData(buffer_obj); |
| 288 if (Dart_IsError(result)) Dart_PropagateError(result); |
| 289 |
279 if (bytes_written != length) { | 290 if (bytes_written != length) { |
280 Dart_Handle err = DartUtils::NewDartOSError(); | 291 Dart_Handle err = DartUtils::NewDartOSError(); |
281 if (Dart_IsError(err)) Dart_PropagateError(err); | 292 if (Dart_IsError(err)) Dart_PropagateError(err); |
282 Dart_SetReturnValue(args, err); | 293 Dart_SetReturnValue(args, err); |
283 } | 294 } |
284 delete[] buffer; | |
285 Dart_ExitScope(); | 295 Dart_ExitScope(); |
286 } | 296 } |
287 | 297 |
288 | 298 |
289 void FUNCTION_NAME(File_Position)(Dart_NativeArguments args) { | 299 void FUNCTION_NAME(File_Position)(Dart_NativeArguments args) { |
290 Dart_EnterScope(); | 300 Dart_EnterScope(); |
291 File* file = GetFilePointer(Dart_GetNativeArgument(args, 0)); | 301 File* file = GetFilePointer(Dart_GetNativeArgument(args, 0)); |
292 ASSERT(file != NULL); | 302 ASSERT(file != NULL); |
293 intptr_t return_value = file->Position(); | 303 intptr_t return_value = file->Position(); |
294 if (return_value >= 0) { | 304 if (return_value >= 0) { |
(...skipping 1025 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1320 if (service_port != ILLEGAL_PORT) { | 1330 if (service_port != ILLEGAL_PORT) { |
1321 // Return a send port for the service port. | 1331 // Return a send port for the service port. |
1322 Dart_Handle send_port = Dart_NewSendPort(service_port); | 1332 Dart_Handle send_port = Dart_NewSendPort(service_port); |
1323 Dart_SetReturnValue(args, send_port); | 1333 Dart_SetReturnValue(args, send_port); |
1324 } | 1334 } |
1325 Dart_ExitScope(); | 1335 Dart_ExitScope(); |
1326 } | 1336 } |
1327 | 1337 |
1328 } // namespace bin | 1338 } // namespace bin |
1329 } // namespace dart | 1339 } // namespace dart |
OLD | NEW |