Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Created November 29, 2011 20:11
Show Gist options
  • Save WebReflection/1406250 to your computer and use it in GitHub Desktop.
Save WebReflection/1406250 to your computer and use it in GitHub Desktop.
Some thoughts on MS code used "to impress" with modern demos ...
// I know it's for demo purpose from MS:
// http://ie.microsoft.com/testdrive/HTML5/TypedArrays/js/binaryReader.js
BinaryReader.prototype = {
readUint8: function () { var result = this.dataView.getUint8(this.streamPosition, true); this._movePointerTo(this.streamPosition + 1); return result; },
readInt8: function () { var result = this.dataView.getInt8(this.streamPosition, true); this._movePointerTo(this.streamPosition + 1); return result; },
readUint16: function () { var result = this.dataView.getUint16(this.streamPosition, true); this._movePointerTo(this.streamPosition + 2); return result; },
readInt16: function () { var result = this.dataView.getInt16(this.streamPosition, true); this._movePointerTo(this.streamPosition + 2); return result; },
readUint32: function () { var result = this.dataView.getUint32(this.streamPosition, true); this._movePointerTo(this.streamPosition + 4); return result; },
readInt32: function () { var result = this.dataView.getInt32(this.streamPosition, true); this._movePointerTo(this.streamPosition + 4); return result; }
};
// but I wonder if in Microsoft
// they would ever use something like this ...
// bandwidth optimized over DRY code anybody ?
(function(BRP){
function create(bytes, signed) {
var
which = (signed ? "Int" : "Uint") + bytes,
type = "get" + which
;
bytes /= 8;
BRP["read" + which] = function () {
var result = this.dataView[type](this.streamPosition, true);
this._movePointerTo(this.streamPosition + bytes);
return result;
};
}
for (var i = 8; i < 64; i *= 2) {
create(i, 0);
create(i, 1);
}
}(BinaryReader.prototype));
// ... also isn't about the time to use these new ES5 features?
// binary data won't be backward compatible in any case
// getters without setters through direct prototype assignment
// ... in 2012 ? what about instance.int8 or instance.uint32 ?
(function(BRP){
function create(bytes, signed) {
var
which = (signed ? "Int" : "Uint") + bytes,
type = "get" + which
;
bytes /= 8;
Object.defineProperty(BRP, which.toLowerCase(), {
get: function () {
var result = this.dataView[type](this.streamPosition, true);
this._movePointerTo(this.streamPosition + bytes);
return result;
}
});
}
for (var i = 8; i < 64; i *= 2) {
create(i, 0);
create(i, 1);
}
}(BinaryReader.prototype));
// or if you really dislike stateful getters ...
// instance.int8() and instance.uint32() here
// what you get, if you cannot set ?
// you may play with instance.asInt8() if you want
// you got the concept, I hope ...
(function(BRP){
function create(bytes, signed) {
var
which = (signed ? "Int" : "Uint") + bytes,
type = "get" + which
;
bytes /= 8;
BRP[which.toLowerCase()] = function () {
var result = this.dataView[type](this.streamPosition, true);
this._movePointerTo(this.streamPosition + bytes);
return result;
};
}
for (var i = 8; i < 64; i *= 2) {
create(i, 0);
create(i, 1);
}
}(BinaryReader.prototype));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment