| 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 // These must match the constants in runtime/bin/dartutils.h class CObject. | 8 // These must match the constants in runtime/bin/dartutils.h class CObject. |
| 9 const int _SUCCESS_RESPONSE = 0; | 9 const int _SUCCESS_RESPONSE = 0; |
| 10 const int _ILLEGAL_ARGUMENT_RESPONSE = 1; | 10 const int _ILLEGAL_ARGUMENT_RESPONSE = 1; |
| 11 const int _OSERROR_RESPONSE = 2; | 11 const int _OSERROR_RESPONSE = 2; |
| 12 const int _FILE_CLOSED_RESPONSE = 3; | 12 const int _FILE_CLOSED_RESPONSE = 3; |
| 13 | 13 |
| 14 const int _ERROR_RESPONSE_ERROR_TYPE = 0; | 14 const int _ERROR_RESPONSE_ERROR_TYPE = 0; |
| 15 const int _OSERROR_RESPONSE_ERROR_CODE = 1; | 15 const int _OSERROR_RESPONSE_ERROR_CODE = 1; |
| 16 const int _OSERROR_RESPONSE_MESSAGE = 2; | 16 const int _OSERROR_RESPONSE_MESSAGE = 2; |
| 17 | 17 |
| 18 // Functions used to receive exceptions from native ports. | 18 // Functions used to receive exceptions from native ports. |
| 19 bool _isErrorResponse(response) { | 19 bool _isErrorResponse(response) => |
| 20 return response is List && response[0] != _SUCCESS_RESPONSE; | 20 response is List && response[0] != _SUCCESS_RESPONSE; |
| 21 } | |
| 22 | 21 |
| 23 /** | 22 /** |
| 24 * Returns an Exception or an Error | 23 * Returns an Exception or an Error |
| 25 */ | 24 */ |
| 26 _exceptionFromResponse(response, String message, String path) { | 25 _exceptionFromResponse(response, String message, String path) { |
| 27 assert(_isErrorResponse(response)); | 26 assert(_isErrorResponse(response)); |
| 28 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { | 27 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { |
| 29 case _ILLEGAL_ARGUMENT_RESPONSE: | 28 case _ILLEGAL_ARGUMENT_RESPONSE: |
| 30 return new ArgumentError(); | 29 return new ArgumentError(); |
| 31 case _OSERROR_RESPONSE: | 30 case _OSERROR_RESPONSE: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 47 } | 46 } |
| 48 | 47 |
| 49 /** | 48 /** |
| 50 * An [OSError] object holds information about an error from the | 49 * An [OSError] object holds information about an error from the |
| 51 * operating system. | 50 * operating system. |
| 52 */ | 51 */ |
| 53 class OSError { | 52 class OSError { |
| 54 /** Constant used to indicate that no OS error code is available. */ | 53 /** Constant used to indicate that no OS error code is available. */ |
| 55 static const int noErrorCode = -1; | 54 static const int noErrorCode = -1; |
| 56 | 55 |
| 57 /** Creates an OSError object from a message and an errorCode. */ | |
| 58 const OSError([String this.message = "", int this.errorCode = noErrorCode]); | |
| 59 | |
| 60 /** Converts an OSError object to a string representation. */ | |
| 61 String toString() { | |
| 62 StringBuffer sb = new StringBuffer(); | |
| 63 sb.write("OS Error"); | |
| 64 if (!message.isEmpty) { | |
| 65 sb.write(": "); | |
| 66 sb.write(message); | |
| 67 if (errorCode != noErrorCode) { | |
| 68 sb.write(", errno = "); | |
| 69 sb.write(errorCode.toString()); | |
| 70 } | |
| 71 } else if (errorCode != noErrorCode) { | |
| 72 sb.write(": errno = "); | |
| 73 sb.write(errorCode.toString()); | |
| 74 } | |
| 75 return sb.toString(); | |
| 76 } | |
| 77 | |
| 78 /** | 56 /** |
| 79 * Error message supplied by the operating system. null if no message is | 57 * Error message supplied by the operating system. null if no message is |
| 80 * associated with the error. | 58 * associated with the error. |
| 81 */ | 59 */ |
| 82 final String message; | 60 final String message; |
| 83 | 61 |
| 84 /** | 62 /** |
| 85 * Error code supplied by the operating system. Will have the value | 63 * Error code supplied by the operating system. Will have the value |
| 86 * [noErrorCode] if there is no error code associated with the error. | 64 * [noErrorCode] if there is no error code associated with the error. |
| 87 */ | 65 */ |
| 88 final int errorCode; | 66 final int errorCode; |
| 67 |
| 68 /** Creates an OSError object from a message and an errorCode. */ |
| 69 const OSError([this.message = "", this.errorCode = noErrorCode]); |
| 70 |
| 71 /** Converts an OSError object to a string representation. */ |
| 72 String toString() { |
| 73 StringBuffer sb = new StringBuffer(); |
| 74 sb.write("OS Error"); |
| 75 if (!message.isEmpty) { |
| 76 sb..write(": ") |
| 77 ..write(message); |
| 78 if (errorCode != noErrorCode) { |
| 79 sb..write(", errno = ") |
| 80 ..write(errorCode.toString()); |
| 81 } |
| 82 } else if (errorCode != noErrorCode) { |
| 83 sb..write(": errno = ") |
| 84 ..write(errorCode.toString()); |
| 85 } |
| 86 return sb.toString(); |
| 87 } |
| 89 } | 88 } |
| 90 | 89 |
| 91 | 90 |
| 92 // Object for holding a buffer and an offset. | 91 // Object for holding a buffer and an offset. |
| 93 class _BufferAndStart { | 92 class _BufferAndStart { |
| 94 _BufferAndStart(List this.buffer, int this.start); | |
| 95 List buffer; | 93 List buffer; |
| 96 int start; | 94 int start; |
| 95 _BufferAndStart(this.buffer, this.start); |
| 97 } | 96 } |
| 98 | 97 |
| 99 // Ensure that the input List can be serialized through a native port. | 98 // Ensure that the input List can be serialized through a native port. |
| 100 // Only Int8List and Uint8List Lists are serialized directly. | 99 // Only Int8List and Uint8List Lists are serialized directly. |
| 101 // All other lists are first copied into a Uint8List. This has the added | 100 // All other lists are first copied into a Uint8List. This has the added |
| 102 // benefit that it is faster to access from the C code as well. | 101 // benefit that it is faster to access from the C code as well. |
| 103 _BufferAndStart _ensureFastAndSerializableByteData( | 102 _BufferAndStart _ensureFastAndSerializableByteData( |
| 104 List buffer, int start, int end) { | 103 List buffer, int start, int end) { |
| 105 if (buffer is Uint8List || buffer is Int8List) { | 104 if (buffer is Uint8List || buffer is Int8List) { |
| 106 return new _BufferAndStart(buffer, start); | 105 return new _BufferAndStart(buffer, start); |
| 107 } | 106 } |
| 108 int length = end - start; | 107 int length = end - start; |
| 109 var newBuffer = new Uint8List(length); | 108 var newBuffer = new Uint8List(length); |
| 110 int j = start; | 109 int j = start; |
| 111 for (int i = 0; i < length; i++) { | 110 for (int i = 0; i < length; i++) { |
| 112 int value = buffer[j]; | 111 int value = buffer[j]; |
| 113 if (value is! int) { | 112 if (value is! int) { |
| 114 throw new ArgumentError("List element is not an integer at index $j"); | 113 throw new ArgumentError("List element is not an integer at index $j"); |
| 115 } | 114 } |
| 116 newBuffer[i] = value; | 115 newBuffer[i] = value; |
| 117 j++; | 116 j++; |
| 118 } | 117 } |
| 119 return new _BufferAndStart(newBuffer, 0); | 118 return new _BufferAndStart(newBuffer, 0); |
| 120 } | 119 } |
| 121 | 120 |
| 122 | 121 |
| 123 class _IOCrypto { | 122 class _IOCrypto { |
| 124 external static Uint8List getRandomBytes(int count); | 123 external static Uint8List getRandomBytes(int count); |
| 125 } | 124 } |
| OLD | NEW |