Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(657)

Unified Diff: runtime/lib/byte_array.dart

Issue 9195031: Add ByteArray interface and provide internal and external implementations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: runtime/lib/byte_array.dart
diff --git a/runtime/lib/byte_array.dart b/runtime/lib/byte_array.dart
new file mode 100644
index 0000000000000000000000000000000000000000..316fb83d9bd9729d320aea02159d2f56dc2ae113
--- /dev/null
+++ b/runtime/lib/byte_array.dart
@@ -0,0 +1,487 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+interface ByteArray default InternalByteArray {
+ ByteArray(int length);
+
+ int get length();
+
+ int getInt8(int byteOffset);
+
+ void setInt8(int byteOffset, int value);
+
+ int getUint8(int byteOffset);
+
+ void setUint8(int byteOffset, int value);
+
+ int getInt16(int byteOffset);
+
+ void setInt16(int byteOffset, int value);
+
+ int getUint16(int byteOffset);
siva 2012/01/20 22:51:04 byteOffset is confusing? It is really byteOffset o
Søren Gjesse 2012/01/23 10:38:51 I think that the index/offset should be sepcified
Søren Gjesse 2012/01/23 10:38:51 Should it be possible to write in both little and
cshapiro 2012/01/24 02:14:23 It is possible although there is no support from t
cshapiro 2012/01/24 02:14:23 It is a byte offset, not a scaled index. The orig
cshapiro 2012/01/24 02:14:23 For the record, we decided to provide an interface
siva 2012/01/25 17:22:47 Ok got it. On 2012/01/24 02:14:23, cshapiro wrot
+
+ void setUint16(int byteOffset, int value);
+
+ int getInt32(int byteOffset);
+
+ void setInt32(int byteOffset, int value);
+
+ int getUint32(int byteOffset);
+
+ void setUint32(int byteOffset, int value);
+
+ int getInt64(int byteOffset);
+
+ void setInt64(int byteOffset, int value);
+
+ int getUint64(int byteOffset);
+
+ void setUint64(int byteOffset, int value);
+
+ double getFloat32(int byteOffset);
+
+ void setFloat32(int byteOffset, double value);
+
+ double getFloat64(int byteOffset);
+
+ void setFloat64(int byteOffset, double value);
+}
+
+
+class ByteArrayBase {
Søren Gjesse 2012/01/23 10:38:51 Shouldn't there be an "implements Iteratable, List
cshapiro 2012/01/24 02:14:23 Effectively, yes. The version of this file that w
+
+ // Iterable interface
+
+ Iterator iterator() {
+ return new ByteArrayIterator(this);
+ }
+
+ // Collection interface
+
+ int get length() {
+ return _length();
+ }
+
+ bool every(bool f(int element)) {
+ return Collections.every(this, f);
+ }
+
+ Collection map(f(int element)) {
Søren Gjesse 2012/01/23 10:38:51 Long line.
cshapiro 2012/01/24 02:14:23 Fixed.
+ return Collections.map(this, new GrowableObjectArray.withCapacity(length), f);
+ }
+
+ Collection filter(bool f(int element)) {
+ return Collections.filter(this, new GrowableObjectArray(), f);
+ }
+
+ void forEach(f(int element)) {
+ Collections.forEach(this, f);
+ }
+
+ bool isEmpty() {
+ return this.length === 0;
+ }
+
+ bool some(bool f(int element)) {
+ return Collections.some(this, f);
+ }
+
+ // List interface
+
+ int operator[](int index) {
+ return getUint8(index);
+ }
+
+ void operator[]=(int index, int value) {
+ setUint8(index, value);
+ }
+
+ void set length(int newLength) {
+ throw const UnsupportedOperationException("Cannot add to a byte array");
+ }
+
+ void add(int element) {
+ throw const UnsupportedOperationException("Cannot add to a byte array");
+ }
+
+ void addLast(int element) {
+ add(element);
siva 2012/01/20 22:51:04 why not throw here instead of calling 'add'.
cshapiro 2012/01/24 02:14:23 Good point. I like the idea of throwing rather th
+ }
+
+ void addAll(Collection elements) {
+ throw const UnsupportedOperationException("Cannot add to a byte array");
+ }
+
+ void sort(int compare(int a, b)) {
+ DualPivotQuicksort.sort(this, compare);
+ }
+
+ int indexOf(int element, [int start = 0]) {
+ return Arrays.indexOf(this, element, start, this.length);
+ }
+
+ int lastIndexOf(int element, [int start = null]) {
+ if (start === null) start = length - 1;
+ return Arrays.lastIndexOf(this, element, start);
+ }
+
+ void clear() {
+ throw const UnsupportedOperationException("Cannot clear a byte array");
+ }
+
+ int removeLast() {
+ throw const UnsupportedOperationException(
+ "Cannot remove from a byte array");
+ }
+
+ int last() {
+ return this[length - 1];
+ }
+
+ // Implementation
+
+ int _length() native "ByteArray_getLength";
+}
+
+
+class ByteArrayIterator implements Iterator {
+ ByteArrayIterator(List byteArray)
+ : _byteArray = byteArray, _length = byteArray.length, _pos = 0 {
+ assert(byteArray is ByteArray);
+ }
+
+ bool hasNext() {
+ return _length > _pos;
+ }
+
+ int next() {
+ if (!hasNext()) {
+ throw const NoMoreElementsException();
+ }
+ return _byteArray[_pos++];
+ }
+
+ final List _byteArray;
+
+ final int _length;
+
+ int _pos;
+}
+
+
+class InternalByteArray extends ByteArrayBase implements ByteArray {
+ factory InternalByteArray(int length) {
+ return _allocate(length);
+ }
+
+ // ByteArray interface
+
+ int getInt8(int byteOffset) {
+ return _getInt8(byteOffset);
+ }
+
+ void setInt8(int byteOffset, int value) {
siva 2012/01/20 22:51:04 why do we need these dart wrappers here, why not j
cshapiro 2012/01/24 02:14:23 This was Ivan's suggestion. I may have misunderst
siva 2012/01/25 17:22:47 I agree with Ivan's premise that native methods sh
+ _setInt8(byteOffset, value);
+ }
+
+ int getUint8(int byteOffset) {
+ return _getUint8(byteOffset);
+ }
+
+ void setUint8(int byteOffset, int value) {
+ _setUint8(byteOffset, value);
+ }
+
+ int getInt16(int byteOffset) {
+ return _getInt16(byteOffset);
+ }
+
+ void setInt16(int byteOffset, int value) {
+ _setInt16(byteOffset, value);
+ }
+
+ int getUint16(int byteOffset) {
+ return _getInt16(byteOffset);
+ }
+
+ void setUint16(int byteOffset, int value) {
+ _setUint16(byteOffset, value);
+ }
+
+ int getInt32(int byteOffset) {
+ return _getInt32(byteOffset);
+ }
+
+ void setInt32(int byteOffset, int value) {
+ _setInt32(byteOffset, value);
+ }
+
+ int getUint32(int byteOffset) {
+ return _getUint32(byteOffset);
+ }
+
+ void setUint32(int byteOffset, int value) {
+ _setUint32(byteOffset, value);
+ }
+
+ int getInt64(int byteOffset) {
+ return _getInt64(byteOffset);
+ }
+
+ void setInt64(int byteOffset, int value) {
+ _setInt64(byteOffset, value);
+ }
+
+ int getUint64(int byteOffset) {
+ return _getUint64(byteOffset);
+ }
+
+ void setUint64(int byteOffset, int value) {
+ _setUint64(byteOffset, value);
+ }
+
+ double getFloat32(int byteOffset) {
+ return _getFloat32(byteOffset);
+ }
+
+ void setFloat32(int byteOffset, double value) {
+ _setFloat32(byteOffset, value);
+ }
+
+ double getFloat64(int byteOffset) {
+ return _getFloat64(byteOffset);
+ }
+
+ void setFloat64(int byteOffset, double value) {
+ _setFloat64(byteOffset, value);
+ }
+
+ // Implementation
+
+ static InternalByteArray _allocate(int length)
+ native "InternalByteArray_allocate";
+
+ int _getInt8(int byteOffset)
+ native "InternalByteArray_getInt8";
+
+ void _setInt8(int byteOffset, int value)
+ native "InternalByteArray_setInt8";
+
+ int _getUint8(int byteOffset)
+ native "InternalByteArray_getUint8";
+
+ void _setUint8(int byteOffset, int value)
+ native "InternalByteArray_setUint8";
+
+ int _getInt16(int byteOffset)
+ native "InternalByteArray_getInt16";
+
+ void _setInt16(int byteOffset, int value)
+ native "InternalByteArray_setInt16";
+
+ int _getUint16(int byteOffset)
+ native "InternalByteArray_getUint16";
+
+ void _setUint16(int byteOffset, int value)
+ native "InternalByteArray_setUint16";
+
+ int _getInt32(int byteOffset)
+ native "InternalByteArray_getInt32";
+
+ void _setInt32(int byteOffset, int value)
+ native "InternalByteArray_setInt32";
+
+ int _getUint32(int byteOffset)
+ native "InternalByteArray_getUint32";
+
+ void _setUint32(int byteOffset, int value)
+ native "InternalByteArray_setUint32";
+
+ int _getInt64(int byteOffset)
+ native "InternalByteArray_getInt64";
+
+ void _setInt64(int byteOffset, int value)
+ native "InternalByteArray_setInt64";
+
+ int _getUint64(int byteOffset)
+ native "InternalByteArray_getUint64";
+
+ void _setUint64(int byteOffset, int value)
+ native "InternalByteArray_setUint64";
+
+ double _getFloat32(int byteOffset)
+ native "InternalByteArray_getFloat32";
+
+ void _setFloat32(int byteOffset, double value)
+ native "InternalByteArray_setFloat32";
+
+ double _getFloat64(int byteOffset)
+ native "InternalByteArray_getFloat64";
+
+ void _setFloat64(int byteOffset, double value)
+ native "InternalByteArray_setFloat64";
+}
+
+
+class ExternalByteArray extends ByteArrayBase implements ByteArray {
+ // Collection interface
+
+ int get length() {
+ return _length();
+ }
+
+ // List interface
+
+ int operator[](int index) {
+ return _getUint8(index);
+ }
+
+ void operator[]=(int index, int value) {
+ _setUint8(index, value);
+ }
+
+ // ByteArray interface
+
+ int getInt8(int byteOffset) {
+ return _getInt8(byteOffset);
+ }
+
+ void setInt8(int byteOffset, int value) {
+ _setInt8(byteOffset, value);
+ }
+
+ int getUint8(int byteOffset) {
+ return _getUint8(byteOffset);
+ }
+
+ void setUint8(int byteOffset, int value) {
+ _setUint8(byteOffset, value);
+ }
+
+ int getInt16(int byteOffset) {
+ return _getInt16(byteOffset);
+ }
+
+ void setInt16(int byteOffset, int value) {
+ _setInt16(byteOffset, value);
+ }
+
+ int getUint16(int byteOffset) {
+ return _getInt16(byteOffset);
+ }
+
+ void setUint16(int byteOffset, int value) {
+ _setUint16(byteOffset, value);
+ }
+
+ int getInt32(int byteOffset) {
+ return _getInt32(byteOffset);
+ }
+
+ void setInt32(int byteOffset, int value) {
+ _setInt32(byteOffset, value);
+ }
+
+ int getUint32(int byteOffset) {
+ return _getUint32(byteOffset);
+ }
+
+ void setUint32(int byteOffset, int value) {
+ _setUint32(byteOffset, value);
+ }
+
+ int getInt64(int byteOffset) {
+ return _getInt64(byteOffset);
+ }
+
+ void setInt64(int byteOffset, int value) {
+ _setInt64(byteOffset, value);
+ }
+
+ int getUint64(int byteOffset) {
+ return _getUint64(byteOffset);
+ }
+
+ void setUint64(int byteOffset, int value) {
+ _setUint64(byteOffset, value);
+ }
+
+ double getFloat32(int byteOffset) {
+ return _getFloat32(byteOffset);
+ }
+
+ void setFloat32(int byteOffset, double value) {
+ _setFloat32(byteOffset, value);
+ }
+
+ double getFloat64(int byteOffset) {
+ return _getFloat64(byteOffset);
+ }
+
+ void setFloat64(int byteOffset, double value) {
+ _setFloat64(byteOffset, value);
+ }
+
+ // Implementation
+
+ int _getInt8(int byteOffset)
+ native "ExternalByteArray_getInt8";
+
+ void _setInt8(int byteOffset, int value)
+ native "ExternalByteArray_setInt8";
+
+ int _getUint8(int byteOffset)
+ native "ExternalByteArray_getUint8";
+
+ void _setUint8(int byteOffset, int value)
+ native "ExternalByteArray_setUint8";
+
+ int _getInt16(int byteOffset)
+ native "ExternalByteArray_getInt16";
+
+ void _setInt16(int byteOffset, int value)
+ native "ExternalByteArray_setInt16";
+
+ int _getUint16(int byteOffset)
+ native "ExternalByteArray_getUint16";
+
+ void _setUint16(int byteOffset, int value)
+ native "ExternalByteArray_setUint16";
+
+ int _getInt32(int byteOffset)
+ native "ExternalByteArray_getInt32";
+
+ void _setInt32(int byteOffset, int value)
+ native "ExternalByteArray_setInt32";
+
+ int _getUint32(int byteOffset)
+ native "ExternalByteArray_getUint32";
+
+ void _setUint32(int byteOffset, int value)
+ native "ExternalByteArray_setUint32";
+
+ int _getInt64(int byteOffset)
+ native "ExternalByteArray_getInt64";
+
+ void _setInt64(int byteOffset, int value)
+ native "ExternalByteArray_setInt64";
+
+ int _getUint64(int byteOffset)
+ native "ExternalByteArray_getUint64";
+
+ void _setUint64(int byteOffset, int value)
+ native "ExternalByteArray_setUint64";
+
+ double _getFloat32(int byteOffset)
+ native "ExternalByteArray_getFloat32";
+
+ void _setFloat32(int byteOffset, double value)
+ native "ExternalByteArray_setFloat32";
+
+ double _getFloat64(int byteOffset)
+ native "ExternalByteArray_getFloat64";
+
+ void _setFloat64(int byteOffset, double value)
+ native "ExternalByteArray_setFloat64";
+}

Powered by Google App Engine
This is Rietveld 408576698