New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make ByteBuffer an active object with a useful interface, not an opaque capability. #16130
Comments
I like this idea in general. We must be careful: ByteBuffer must correspond to ArrayBuffer in the Typed Array Specification. http://www.khronos.org/registry/typedarray/specs/latest/#5 As proposed, the signatures are slightly wrong: the last parameter is a length measured in the view unit, defaulting to the maximum legal view; for example, you get a view of 2 Uint32 values out of a 10 byte buffer starting at offset 0. Maybe the methods should still contain the word 'view' abstract class ByteBuffer { factory Uint8List.view(ByteBuffer buffer, [int offsetInBytes = 0, int length]) => If we make this change, I would want to mark the T.view constructors as deprecated, and find some way to document for each T that a T can be created from a ByteBuffer. One thing that is slightly more tricky: dart2js could easily see an explicit length argument to the constructor and do better type analysis and eliminate bounds checks from fixed-sized views. This is more difficult with the indirection. |
It appears that this suggestion is implemented in https://code.google.com/p/dart/source/detail?r=37974
/** This contains much less information than the matching factory constructor /** The asXXX method should contain the same information, perhaps simplifying the factory constructors to say they are the same as the asXXX methods. I would also suggest improving the original descriptions with an additional paraphrasing of the constraints, i.e. the view is within the bounds of the buffer, the view is must be aligned to the element type. |
cc @lrhn. |
That's done. |
Currently the ByteBuffer object has no useful methods, and the interface cannot be implemented by new classes because the system depends on an internal shared secret between ByteBuffer and the .view constructors.
How about instead having:
class ByteBuffer {
...
Int8List asInt8List([int start = 0, int end]);
Uint8List asUint8List([int start = 0, int end]);
...
Float64List asFloat64List([int start = 0, int end]);
ByteData asByteData([int start = 0, int end]);
}
and make the view constructors go that way instead:
factory Uint8List.view(ByteBuffer buffer, [int start = 0, int end]) =>
buffer.asUint8List(start, end);
That would mean that anyone can create a new ByteBuffer implementation (or mock), and that there doesn't need to be an assumption in the view constructors on the actual implementation of the buffer.
(The view constructors are irrelevant now, but we can keep them for backwards compatibility).
The text was updated successfully, but these errors were encountered: