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; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 | 90 |
91 | 91 |
92 // Object for holding a buffer and an offset. | 92 // Object for holding a buffer and an offset. |
93 class _BufferAndStart { | 93 class _BufferAndStart { |
94 _BufferAndStart(List this.buffer, int this.start); | 94 _BufferAndStart(List this.buffer, int this.start); |
95 List buffer; | 95 List buffer; |
96 int start; | 96 int start; |
97 } | 97 } |
98 | 98 |
99 // Ensure that the input List can be serialized through a native port. | 99 // Ensure that the input List can be serialized through a native port. |
100 // Only builtin Lists can be serialized through. If user-defined Lists | 100 // Only Int8List and Uint8List Lists are serialized directly. |
101 // get here, the contents is copied to a Uint8List. This has the added | 101 // 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. | 102 // benefit that it is faster to access from the C code as well. |
103 _BufferAndStart _ensureFastAndSerializableData( | 103 _BufferAndStart _ensureFastAndSerializableByteData( |
104 List buffer, int start, int end) { | 104 List buffer, int start, int end) { |
105 if (buffer is Uint8List || | 105 if (buffer is Uint8List || buffer is Int8List) { |
106 buffer is Int8List || | |
107 buffer is Uint16List || | |
108 buffer is Int16List || | |
109 buffer is Uint32List || | |
110 buffer is Int32List || | |
111 buffer is Uint64List || | |
112 buffer is Int64List || | |
113 buffer is ByteData || | |
114 buffer is Float32List || | |
115 buffer is Float64List || | |
116 _BufferUtils._isBuiltinList(buffer)) { | |
117 return new _BufferAndStart(buffer, start); | 106 return new _BufferAndStart(buffer, start); |
118 } | 107 } |
119 int length = end - start; | 108 int length = end - start; |
120 var newBuffer = new Uint8List(length); | 109 var newBuffer = new Uint8List(length); |
121 int j = start; | 110 int j = start; |
122 for (int i = 0; i < length; i++) { | 111 for (int i = 0; i < length; i++) { |
123 int value = buffer[j]; | 112 int value = buffer[j]; |
124 if (value is! int) { | 113 if (value is! int) { |
125 throw new ArgumentError("List element is not an integer at index $j"); | 114 throw new ArgumentError("List element is not an integer at index $j"); |
126 } | 115 } |
127 newBuffer[i] = value; | 116 newBuffer[i] = value; |
128 j++; | 117 j++; |
129 } | 118 } |
130 return new _BufferAndStart(newBuffer, 0); | 119 return new _BufferAndStart(newBuffer, 0); |
131 } | 120 } |
132 | 121 |
133 | 122 |
134 _BufferAndStart _ensureFastAndSerializableByteData( | |
135 List buffer, int start, int end) { | |
136 if (buffer is Uint8List) { | |
137 return new _BufferAndStart(buffer, start); | |
138 } | |
139 int length = end - start; | |
140 var newBuffer = new Uint8List(length); | |
141 int j = start; | |
142 for (int i = 0; i < length; i++) { | |
143 int value = buffer[j]; | |
144 if (value is! int || | |
145 value < 0 || 255 < value) { | |
146 throw new ArgumentError( | |
147 "List element is not a byte value (value $value at index $j)"); | |
148 } | |
149 newBuffer[i] = value; | |
150 j++; | |
151 } | |
152 return new _BufferAndStart(newBuffer, 0); | |
153 } | |
154 | |
155 | |
156 // TODO(ager): The only reason for the class here is that | |
157 // we cannot patch a top-level function. | |
158 class _BufferUtils { | |
159 // Check if a List is a builtin VM List type. Returns true | |
160 // if the List is a builtin VM List type and false if it is | |
161 // a user defined List type. | |
162 external static bool _isBuiltinList(List buffer); | |
163 } | |
164 | |
165 class _IOCrypto { | 123 class _IOCrypto { |
166 external static Uint8List getRandomBytes(int count); | 124 external static Uint8List getRandomBytes(int count); |
167 } | 125 } |
OLD | NEW |