Permalink
Browse files

pluck dataview polyfill out of buffer.js, in preparation for further …

…refactoring
  • Loading branch information...
1 parent 4de8ffc commit 627ed8d171b5e4ab74449c78b1eb4c17a2581f37 Carlos Scheidegger committed Mar 15, 2013
View
@@ -8,6 +8,7 @@ rcloud_bundle.js: \
websockets.js \
rserve.js \
rclient.js \
+ polyfill-dataview.js \
buffer.js \
rcloud.js \
notebook/_begin.js \
View
@@ -15,72 +15,6 @@
}
})();
- var data_types = ['Int32', 'Int16', 'Uint32', 'Uint16',
- 'Float32', 'Float64'];
- var setters = ['setInt32', 'setInt16', 'setUint32', 'setUint16',
- 'setFloat32', 'setFloat64'];
- var getters = ['getInt32', 'getInt16', 'getUint32', 'getUint16',
- 'getFloat32', 'getFloat64'];
-
- if (!global.DataView) {
- console.log("polyfilling DataView");
-
- var helpers = {};
- for (var i=0; i<data_types.length; ++i) {
- var cls = this[data_types[i] + 'Array'];
- var sz = cls.BYTES_PER_ELEMENT;
- var buf = new ArrayBuffer(sz);
- var original_array = new cls(buf);
- var byte_array = new Uint8Array(buf);
- helpers[data_types[i]] = (function(original_array, byte_array) {
- return function(output, sz, ix, v) {
- original_array[0] = v;
- for (var i=0; i<sz; ++i) {
- output[ix + i] = byte_array[i];
- }
- };
- })(original_array, byte_array);
- }
-
- function MyDataView(buffer, byteOffset, byteLength) {
- this.buffer = buffer;
- this.byteOffset = _.isUndefined(byteOffset) ? 0 : byteOffset;
- this.byteLength = _.isUndefined(byteLength) ? buffer.byteLength : byteLength;
- this.view = new jDataView(buffer, byteOffset, byteLength, _is_little_endian);
- this.byte_array = new Uint8Array(buffer);
- }
-
- var proto = {};
- MyDataView.prototype = proto;
- for (i=0; i<data_types.length; ++i) {
- var getter = 'get' + data_types[i];
- proto[getter] = (function(name) {
- return function(i) { return this.view[name](i); };
- })(getter);
- var setter = 'set' + data_types[i];
- var sz = this[data_types[i] + 'Array'].BYTES_PER_ELEMENT;
- proto[setter] = (function(sz, name) {
- return function(byteOffset, v) {
- console.log(name);
- console.log(helpers);
- helpers[name](this.byte_array, sz, byteOffset, v);
- };
- })(sz, data_types[i]);
- }
-
- proto.setUint8 = function(ix, v) {
- this.byte_array[ix] = v;
- };
- proto.setInt8 = function(ix, v) {
- if (v < 0) v += 256;
- this.byte_array[ix] = v;
- };
- proto.getInt8 = function(ix) { return this.view.GetInt8(ix); };
- proto.getUint8 = function(ix) { /* return this.view.GetUint8(ix); // <-- doesn't work in FF! */ this.byte_array[ix]; };
-
- global.DataView = MyDataView;
- }
-
global.EndianAwareDataView = (function() {
var proto = {
@@ -0,0 +1,83 @@
+(function(global) {
+ var _is_little_endian;
+
+ (function() {
+ var x = new ArrayBuffer(4);
+ var bytes = new Uint8Array(x),
+ words = new Uint32Array(x);
+ bytes[0] = 1;
+ if (words[0] === 1) {
+ _is_little_endian = true;
+ } else if (words[0] === 16777216) {
+ _is_little_endian = false;
+ } else {
+ throw "we're bizarro endian, refusing to continue";
+ }
+ })();
+
+ if (!global.DataView) {
+ var data_types = ['Int32', 'Int16', 'Uint32', 'Uint16',
+ 'Float32', 'Float64'];
+ var setters = ['setInt32', 'setInt16', 'setUint32', 'setUint16',
+ 'setFloat32', 'setFloat64'];
+ var getters = ['getInt32', 'getInt16', 'getUint32', 'getUint16',
+ 'getFloat32', 'getFloat64'];
+
+ console.log("polyfilling DataView");
+
+ var helpers = {};
+ for (var i=0; i<data_types.length; ++i) {
+ var cls = this[data_types[i] + 'Array'];
+ var sz = cls.BYTES_PER_ELEMENT;
+ var buf = new ArrayBuffer(sz);
+ var original_array = new cls(buf);
+ var byte_array = new Uint8Array(buf);
+ helpers[data_types[i]] = (function(original_array, byte_array) {
+ return function(output, sz, ix, v) {
+ original_array[0] = v;
+ for (var i=0; i<sz; ++i) {
+ output[ix + i] = byte_array[i];
+ }
+ };
+ })(original_array, byte_array);
+ }
+
+ function MyDataView(buffer, byteOffset, byteLength) {
+ this.buffer = buffer;
+ this.byteOffset = _.isUndefined(byteOffset) ? 0 : byteOffset;
+ this.byteLength = _.isUndefined(byteLength) ? buffer.byteLength : byteLength;
+ this.view = new jDataView(buffer, byteOffset, byteLength, _is_little_endian);
+ this.byte_array = new Uint8Array(buffer);
+ }
+
+ var proto = {};
+ MyDataView.prototype = proto;
+ for (i=0; i<data_types.length; ++i) {
+ var getter = 'get' + data_types[i];
+ proto[getter] = (function(name) {
+ return function(i) { return this.view[name](i); };
+ })(getter);
+ var setter = 'set' + data_types[i];
+ var sz = this[data_types[i] + 'Array'].BYTES_PER_ELEMENT;
+ proto[setter] = (function(sz, name) {
+ return function(byteOffset, v) {
+ console.log(name);
+ console.log(helpers);
+ helpers[name](this.byte_array, sz, byteOffset, v);
+ };
+ })(sz, data_types[i]);
+ }
+
+ proto.setUint8 = function(ix, v) {
+ this.byte_array[ix] = v;
+ };
+ proto.setInt8 = function(ix, v) {
+ if (v < 0) v += 256;
+ this.byte_array[ix] = v;
+ };
+ proto.getInt8 = function(ix) { return this.view.GetInt8(ix); };
+ proto.getUint8 = function(ix) { /* return this.view.GetUint8(ix); // <-- doesn't work in FF! */ return this.byte_array[ix]; };
+
+ global.DataView = MyDataView;
+ }
+})(this);
View
@@ -56,7 +56,7 @@ RClient = {
login: token + "\n" + token
});
- var _debug = true;
+ var _debug = opts.debug || false;
var _capturing_answers = false;
var _capturing_callback = undefined;
View
@@ -699,7 +699,6 @@ function RserveError(message, status_code) {
RserveError.prototype = Object.create(Error);
RserveError.prototype.constructor = RserveError;
-
var Rsrv = {
PAR_TYPE: function(x) { return x & 255; },
PAR_LEN: function(x) { return x >> 8; },
@@ -1767,14 +1766,14 @@ RClient = {
}
})();
- var data_types = ['Int32', 'Int16', 'Uint32', 'Uint16',
- 'Float32', 'Float64'];
- var setters = ['setInt32', 'setInt16', 'setUint32', 'setUint16',
- 'setFloat32', 'setFloat64'];
- var getters = ['getInt32', 'getInt16', 'getUint32', 'getUint16',
- 'getFloat32', 'getFloat64'];
-
if (!global.DataView) {
+ var data_types = ['Int32', 'Int16', 'Uint32', 'Uint16',
+ 'Float32', 'Float64'];
+ var setters = ['setInt32', 'setInt16', 'setUint32', 'setUint16',
+ 'setFloat32', 'setFloat64'];
+ var getters = ['getInt32', 'getInt16', 'getUint32', 'getUint16',
+ 'getFloat32', 'getFloat64'];
+
console.log("polyfilling DataView");
var helpers = {};
@@ -1828,10 +1827,27 @@ RClient = {
this.byte_array[ix] = v;
};
proto.getInt8 = function(ix) { return this.view.GetInt8(ix); };
- proto.getUint8 = function(ix) { /* return this.view.GetUint8(ix); // <-- doesn't work in FF! */ this.byte_array[ix]; };
+ proto.getUint8 = function(ix) { /* return this.view.GetUint8(ix); // <-- doesn't work in FF! */ return this.byte_array[ix]; };
global.DataView = MyDataView;
}
+})(this);
+(function(global) {
+ var _is_little_endian;
+
+ (function() {
+ var x = new ArrayBuffer(4);
+ var bytes = new Uint8Array(x),
+ words = new Uint32Array(x);
+ bytes[0] = 1;
+ if (words[0] === 1) {
+ _is_little_endian = true;
+ } else if (words[0] === 16777216) {
+ _is_little_endian = false;
+ } else {
+ throw "we're bizarro endian, refusing to continue";
+ }
+ })();
global.EndianAwareDataView = (function() {

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -33,6 +33,7 @@ function transpose(ar) {
function main_init() {
rclient = RClient.create({
+ debug: false,
host: "ws://"+location.hostname+":8081/",
on_connect: function() {
$("#new-md-cell-button").click(function() {

0 comments on commit 627ed8d

Please sign in to comment.