dcodeIO/ProtoBuf.js — pull request #5 by bertdouglas (2013-03-19 21:07:38+00:00)
Title: Float32 test case "float1"

During testing with data from the Google C++ protobuf library, I found that Endian-ness of float32 and float64 values is backwards. I made a test case for float32. Please pull.

To make the tests pass, I changed some lines in ByteBuffer.js as shown below. This produces the correct results, but it is probably not the best way to change the code.

--- ByteBuffer.js.save	2013-03-19 14:01:53.938562777 -0700
+++ ByteBuffer.js	2013-03-19 14:02:34.262542756 -0700
@@ -826,7 +826,7 @@
ByteBuffer.prototype.writeFloat32 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=4)-4;
this.ensureCapacity(offset+4);
- this.view.setFloat32(offset, value, this.littleEndian);
+ this.view.setFloat32(offset, value, ! this.littleEndian);
return this;
};

@@ -842,7 +842,7 @@
if (this.array == null || offset+4 > this.array.byteLength) {
throw(new Error("Cannot read float32 from "+this+": Capacity overflow"));
}
- return this.view.getFloat32(offset, this.littleEndian);
+ return this.view.getFloat32(offset, ! this.littleEndian);
};

/**
@@ -875,7 +875,7 @@
ByteBuffer.prototype.writeFloat64 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=8)-8;
this.ensureCapacity(offset+8);
- this.view.setFloat64(offset, value, this.littleEndian);
+ this.view.setFloat64(offset, value, ! this.littleEndian);
return this;
};

@@ -891,7 +891,7 @@
if (this.array == null || offset+8 > this.array.byteLength) {
throw(new Error("Cannot read float64 from "+this+": Capacity overflow"));
}
- return this.view.getFloat64(offset, this.littleEndian);
+ return this.view.getFloat64(offset, ! this.littleEndian);
};

/**