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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 _BufferAndOffset _ensureFastAndSerializableBuffer( |
72 List buffer, int offset, int bytes) { | 72 List buffer, int offset, int bytes) { |
73 if (buffer is Uint8List || | 73 if (buffer is Uint8List || |
74 buffer is Int8List || | 74 buffer is Int8List || |
| 75 buffer is Uint16List || |
| 76 buffer is Int16List || |
75 _BufferUtils._isBuiltinList(buffer)) { | 77 _BufferUtils._isBuiltinList(buffer)) { |
76 return new _BufferAndOffset(buffer, offset); | 78 return new _BufferAndOffset(buffer, offset); |
77 } | 79 } |
78 var newBuffer = new Uint8List(bytes); | 80 var newBuffer = new Uint8List(bytes); |
79 int j = offset; | 81 int j = offset; |
80 for (int i = 0; i < bytes; i++) { | 82 for (int i = 0; i < bytes; i++) { |
81 int value = buffer[j]; | 83 int value = buffer[j]; |
82 if (value is! int) { | 84 if (value is! int) { |
83 throw new ArgumentError("List element is not an integer at index $j"); | 85 throw new ArgumentError("List element is not an integer at index $j"); |
84 } | 86 } |
85 newBuffer[i] = value; | 87 newBuffer[i] = value; |
86 j++; | 88 j++; |
87 } | 89 } |
88 return new _BufferAndOffset(newBuffer, 0); | 90 return new _BufferAndOffset(newBuffer, 0); |
89 } | 91 } |
90 | 92 |
91 | 93 |
92 // TODO(ager): The only reason for the class here is that | 94 // TODO(ager): The only reason for the class here is that |
93 // we cannot patch a top-level function. | 95 // we cannot patch a top-level function. |
94 class _BufferUtils { | 96 class _BufferUtils { |
95 // Check if a List is a builtin VM List type. Returns true | 97 // Check if a List is a builtin VM List type. Returns true |
96 // if the List is a builtin VM List type and false if it is | 98 // if the List is a builtin VM List type and false if it is |
97 // a user defined List type. | 99 // a user defined List type. |
98 external static bool _isBuiltinList(List buffer); | 100 external static bool _isBuiltinList(List buffer); |
99 } | 101 } |
100 | 102 |
101 class _IOCrypto { | 103 class _IOCrypto { |
102 external static Uint8List getRandomBytes(int count); | 104 external static Uint8List getRandomBytes(int count); |
103 } | 105 } |
OLD | NEW |