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 // Constants used when working with native ports. | 5 // Constants used when working with native ports. |
6 const int _SUCCESS_RESPONSE = 0; | 6 const int _SUCCESS_RESPONSE = 0; |
7 const int _ILLEGAL_ARGUMENT_RESPONSE = 1; | 7 const int _ILLEGAL_ARGUMENT_RESPONSE = 1; |
8 const int _OSERROR_RESPONSE = 2; | 8 const int _OSERROR_RESPONSE = 2; |
9 const int _FILE_CLOSED_RESPONSE = 3; | 9 const int _FILE_CLOSED_RESPONSE = 3; |
10 | 10 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
48 final String message; | 48 final String message; |
49 | 49 |
50 /** | 50 /** |
51 * Error code supplied by the operating system. Will have the value | 51 * Error code supplied by the operating system. Will have the value |
52 * [noErrorCode] if there is no error code associated with the error. | 52 * [noErrorCode] if there is no error code associated with the error. |
53 */ | 53 */ |
54 final int errorCode; | 54 final int errorCode; |
55 } | 55 } |
56 | 56 |
57 | 57 |
58 // Check if a List is a builtin VM List type. Returns true | |
59 // if the List is a builtin VM List type and false if it is | |
60 // a user defined List type. | |
61 bool _isBuiltinList(List buffer) native "Common_IsBuiltinList"; | |
62 | |
63 | |
64 // Object for holding a buffer and an offset. | 58 // Object for holding a buffer and an offset. |
65 class _BufferAndOffset { | 59 class _BufferAndOffset { |
66 _BufferAndOffset(List this.buffer, int this.offset); | 60 _BufferAndOffset(List this.buffer, int this.offset); |
67 List buffer; | 61 List buffer; |
68 int offset; | 62 int offset; |
69 } | 63 } |
70 | 64 |
71 | |
72 // Ensure that the input List can be serialized through a native port. | 65 // Ensure that the input List can be serialized through a native port. |
73 // Only builtin Lists can be serialized through. If user-defined Lists | 66 // Only builtin Lists can be serialized through. If user-defined Lists |
74 // get here, the contents is copied to a Uint8List. This has the added | 67 // get here, the contents is copied to a Uint8List. This has the added |
75 // benefit that it is faster to access from the C code as well. | 68 // benefit that it is faster to access from the C code as well. |
76 _BufferAndOffset _ensureFastAndSerializableBuffer( | 69 _BufferAndOffset _ensureFastAndSerializableBuffer( |
77 List buffer, int offset, int bytes) { | 70 List buffer, int offset, int bytes) { |
78 if (buffer is Uint8List || _isBuiltinList(buffer)) { | 71 if (buffer is Uint8List || _BufferUtils._isBuiltinList(buffer)) { |
79 return new _BufferAndOffset(buffer, offset); | 72 return new _BufferAndOffset(buffer, offset); |
80 } | 73 } |
81 var newBuffer = new Uint8List(bytes); | 74 var newBuffer = new Uint8List(bytes); |
82 int j = offset; | 75 int j = offset; |
83 for (int i = 0; i < bytes; i++) { | 76 for (int i = 0; i < bytes; i++) { |
84 int value = buffer[j]; | 77 int value = buffer[j]; |
85 if (value is! int) { | 78 if (value is! int) { |
86 throw new FileIOException("List element is not an integer at index $j"); | 79 throw new ArgumentError("List element is not an integer at index $j"); |
87 } | 80 } |
88 newBuffer[i] = value; | 81 newBuffer[i] = value; |
89 j++; | 82 j++; |
90 } | 83 } |
91 return new _BufferAndOffset(newBuffer, 0); | 84 return new _BufferAndOffset(newBuffer, 0); |
92 } | 85 } |
86 | |
87 | |
88 // TODO(ager): The only reason for the class here is that | |
89 // we cannot patch a top-level function. | |
90 class _BufferUtils { | |
Anders Johnsen
2012/10/30 10:43:06
Maybe make it abstract then.
| |
91 // Check if a List is a builtin VM List type. Returns true | |
92 // if the List is a builtin VM List type and false if it is | |
93 // a user defined List type. | |
94 external static bool _isBuiltinList(List buffer); | |
95 } | |
OLD | NEW |