| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of dart.io; | 5 part of dart.io; |
| 6 | 6 |
| 7 // Constants used when working with native ports. | 7 // Constants used when working with native ports. |
| 8 const int _SUCCESS_RESPONSE = 0; | 8 const int _SUCCESS_RESPONSE = 0; |
| 9 const int _ILLEGAL_ARGUMENT_RESPONSE = 1; | 9 const int _ILLEGAL_ARGUMENT_RESPONSE = 1; |
| 10 const int _OSERROR_RESPONSE = 2; | 10 const int _OSERROR_RESPONSE = 2; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 /** | 52 /** |
| 53 * Error code supplied by the operating system. Will have the value | 53 * Error code supplied by the operating system. Will have the value |
| 54 * [noErrorCode] if there is no error code associated with the error. | 54 * [noErrorCode] if there is no error code associated with the error. |
| 55 */ | 55 */ |
| 56 final int errorCode; | 56 final int errorCode; |
| 57 } | 57 } |
| 58 | 58 |
| 59 | 59 |
| 60 // Object for holding a buffer and an offset. | 60 // Object for holding a buffer and an offset. |
| 61 class _BufferAndOffset { | 61 class _BufferAndStart { |
| 62 _BufferAndOffset(List this.buffer, int this.offset); | 62 _BufferAndStart(List this.buffer, int this.start); |
| 63 List buffer; | 63 List buffer; |
| 64 int offset; | 64 int start; |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Ensure that the input List can be serialized through a native port. | 67 // Ensure that the input List can be serialized through a native port. |
| 68 // Only builtin Lists can be serialized through. If user-defined Lists | 68 // Only builtin Lists can be serialized through. If user-defined Lists |
| 69 // get here, the contents is copied to a Uint8List. This has the added | 69 // get here, the contents is copied to a Uint8List. This has the added |
| 70 // benefit that it is faster to access from the C code as well. | 70 // benefit that it is faster to access from the C code as well. |
| 71 _BufferAndOffset _ensureFastAndSerializableBuffer( | 71 _BufferAndStart _ensureFastAndSerializableBuffer( |
| 72 List buffer, int offset, int bytes) { | 72 List buffer, int start, int end) { |
| 73 if (buffer is Uint8List || | 73 if (buffer is Uint8List || |
| 74 buffer is Int8List || | 74 buffer is Int8List || |
| 75 buffer is Uint16List || | 75 buffer is Uint16List || |
| 76 buffer is Int16List || | 76 buffer is Int16List || |
| 77 buffer is Uint32List || | 77 buffer is Uint32List || |
| 78 buffer is Int32List || | 78 buffer is Int32List || |
| 79 buffer is Uint64List || | 79 buffer is Uint64List || |
| 80 buffer is Int64List || | 80 buffer is Int64List || |
| 81 buffer is ByteData || | 81 buffer is ByteData || |
| 82 buffer is Float32List || | 82 buffer is Float32List || |
| 83 buffer is Float64List || | 83 buffer is Float64List || |
| 84 _BufferUtils._isBuiltinList(buffer)) { | 84 _BufferUtils._isBuiltinList(buffer)) { |
| 85 return new _BufferAndOffset(buffer, offset); | 85 return new _BufferAndStart(buffer, start); |
| 86 } | 86 } |
| 87 var newBuffer = new Uint8List(bytes); | 87 int length = end - start; |
| 88 int j = offset; | 88 var newBuffer = new Uint8List(length); |
| 89 for (int i = 0; i < bytes; i++) { | 89 int j = start; |
| 90 for (int i = 0; i < length; i++) { |
| 90 int value = buffer[j]; | 91 int value = buffer[j]; |
| 91 if (value is! int) { | 92 if (value is! int) { |
| 92 throw new ArgumentError("List element is not an integer at index $j"); | 93 throw new ArgumentError("List element is not an integer at index $j"); |
| 93 } | 94 } |
| 94 newBuffer[i] = value; | 95 newBuffer[i] = value; |
| 95 j++; | 96 j++; |
| 96 } | 97 } |
| 97 return new _BufferAndOffset(newBuffer, 0); | 98 return new _BufferAndStart(newBuffer, 0); |
| 98 } | 99 } |
| 99 | 100 |
| 100 | 101 |
| 101 // TODO(ager): The only reason for the class here is that | 102 // TODO(ager): The only reason for the class here is that |
| 102 // we cannot patch a top-level function. | 103 // we cannot patch a top-level function. |
| 103 class _BufferUtils { | 104 class _BufferUtils { |
| 104 // Check if a List is a builtin VM List type. Returns true | 105 // Check if a List is a builtin VM List type. Returns true |
| 105 // if the List is a builtin VM List type and false if it is | 106 // if the List is a builtin VM List type and false if it is |
| 106 // a user defined List type. | 107 // a user defined List type. |
| 107 external static bool _isBuiltinList(List buffer); | 108 external static bool _isBuiltinList(List buffer); |
| 108 } | 109 } |
| 109 | 110 |
| 110 class _IOCrypto { | 111 class _IOCrypto { |
| 111 external static Uint8List getRandomBytes(int count); | 112 external static Uint8List getRandomBytes(int count); |
| 112 } | 113 } |
| OLD | NEW |