kiwi-schema
Advanced tools
Comparing version 0.0.10 to 0.0.11
66
kiwi.js
@@ -18,4 +18,4 @@ var kiwi = exports || kiwi || {}, exports; | ||
(function() { | ||
var uint8 = new Uint8Array(4); | ||
var float32 = new Float32Array(uint8.buffer); | ||
var int32 = new Int32Array(1); | ||
var float32 = new Float32Array(int32.buffer); | ||
@@ -49,13 +49,29 @@ function ByteBuffer(data) { | ||
ByteBuffer.prototype.readFloat = function() { | ||
ByteBuffer.prototype.readVarFloat = function() { | ||
var index = this._index; | ||
var data = this._data; | ||
if (index + 4 > data.length) { | ||
var length = data.length; | ||
// Optimization: use a single byte to store zero | ||
if (index + 1 > length) { | ||
throw new Error('Index out of bounds'); | ||
} | ||
uint8[0] = data[index]; | ||
uint8[1] = data[index + 1]; | ||
uint8[2] = data[index + 2]; | ||
uint8[3] = data[index + 3]; | ||
var first = data[index]; | ||
if (first === 0) { | ||
this._index = index + 1; | ||
return 0; | ||
} | ||
// Endian-independent 32-bit read | ||
if (index + 4 > length) { | ||
throw new Error('Index out of bounds'); | ||
} | ||
var bits = first | (data[index + 1] << 8) | (data[index + 2] << 16) | (data[index + 3] << 24); | ||
this._index = index + 4; | ||
// Move the exponent back into place | ||
bits = (bits << 23) | (bits >>> 9); | ||
// Reinterpret as a floating-point number | ||
int32[0] = bits; | ||
return float32[0]; | ||
@@ -137,11 +153,25 @@ }; | ||
ByteBuffer.prototype.writeFloat = function(value) { | ||
ByteBuffer.prototype.writeVarFloat = function(value) { | ||
var index = this.length; | ||
// Reinterpret as an integer | ||
float32[0] = value; | ||
var bits = int32[0]; | ||
// Move the exponent to the first 8 bits | ||
bits = (bits >>> 23) | (bits << 9); | ||
// Optimization: use a single byte to store zero and denormals (check for an exponent of 0) | ||
if ((bits & 255) === 0) { | ||
this.writeByte(0); | ||
return; | ||
} | ||
// Endian-independent 32-bit write | ||
this._growBy(4); | ||
var data = this._data; | ||
float32[0] = value; | ||
data[index] = uint8[0]; | ||
data[index + 1] = uint8[1]; | ||
data[index + 2] = uint8[2]; | ||
data[index + 3] = uint8[3]; | ||
data[index] = bits; | ||
data[index + 1] = bits >> 8; | ||
data[index + 2] = bits >> 16; | ||
data[index + 3] = bits >> 24; | ||
}; | ||
@@ -527,3 +557,3 @@ | ||
case 'float': { | ||
code = 'bb.readFloat()'; | ||
code = 'bb.readVarFloat()'; | ||
break; | ||
@@ -618,3 +648,3 @@ } | ||
case 'float': { | ||
code = 'bb.writeFloat(value);'; | ||
code = 'bb.writeVarFloat(value);'; | ||
break; | ||
@@ -1006,3 +1036,3 @@ } | ||
case 'float': { | ||
code = '_bb.writeFloat(' + value + ');'; | ||
code = '_bb.writeVarFloat(' + value + ');'; | ||
break; | ||
@@ -1120,3 +1150,3 @@ } | ||
case 'float': { | ||
code = '_bb.readFloat(' + value + ')'; | ||
code = '_bb.readVarFloat(' + value + ')'; | ||
break; | ||
@@ -1123,0 +1153,0 @@ } |
{ | ||
"name": "kiwi-schema", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "kiwi.js", |
@@ -11,3 +11,3 @@ # Kiwi Message Format | ||
* *uint*: A 32-bit integer value stored using a variable-length encoding optimized for storing small non-negative numbers. Will use at most 5 bytes. | ||
* *float*: A 32-bit floating-point number. Will always use 4 bytes. | ||
* *float*: A 32-bit floating-point number. Normally uses 4 bytes but a value of zero uses 1 byte ([denormal numbers](https://en.wikipedia.org/wiki/Denormal_number) become zero when encoded). | ||
* *string*: A UTF-8 length-prefixed string. Will use at least 1 byte. | ||
@@ -14,0 +14,0 @@ * *T[]*: Any type can be made into an array using the `[]` suffix. |
@@ -151,3 +151,3 @@ var test = exports || test || {}, exports; | ||
result["x"] = bb.readFloat(); | ||
result["x"] = bb.readVarFloat(); | ||
return result; | ||
@@ -162,3 +162,3 @@ }; | ||
if (value != null) { | ||
bb.writeFloat(value); | ||
bb.writeVarFloat(value); | ||
} else { | ||
@@ -419,3 +419,3 @@ throw new Error("Missing required field \"x\""); | ||
case 1: | ||
result["x"] = bb.readFloat(); | ||
result["x"] = bb.readVarFloat(); | ||
break; | ||
@@ -436,3 +436,3 @@ | ||
bb.writeVarUint(1); | ||
bb.writeFloat(value); | ||
bb.writeVarFloat(value); | ||
} | ||
@@ -711,3 +711,3 @@ bb.writeVarUint(0); | ||
var length = bb.readVarUint(); | ||
while (length-- > 0) values.push(bb.readFloat()); | ||
while (length-- > 0) values.push(bb.readVarFloat()); | ||
return result; | ||
@@ -726,3 +726,3 @@ }; | ||
value = values[i]; | ||
bb.writeFloat(value); | ||
bb.writeVarFloat(value); | ||
} | ||
@@ -995,3 +995,3 @@ } else { | ||
var length = bb.readVarUint(); | ||
while (length-- > 0) values.push(bb.readFloat()); | ||
while (length-- > 0) values.push(bb.readVarFloat()); | ||
break; | ||
@@ -1016,3 +1016,3 @@ | ||
value = values[i]; | ||
bb.writeFloat(value); | ||
bb.writeVarFloat(value); | ||
} | ||
@@ -1019,0 +1019,0 @@ } |
@@ -84,10 +84,13 @@ var assert = require('assert'); | ||
assert.deepEqual(Buffer(schema.encodeFloatStruct({x: i})), Buffer(o)); | ||
assert.deepEqual(schema.decodeFloatStruct(new Uint8Array(o)), {x: i}); | ||
assert.deepEqual(JSON.stringify(schema.decodeFloatStruct(new Uint8Array(o))), JSON.stringify({x: i})); | ||
} | ||
check(0, [0, 0, 0, 0]); | ||
check(1, [0, 0, 128, 63]); | ||
check(-1, [0, 0, 128, 191]); | ||
check(3.1415927410125732, [219, 15, 73, 64]); | ||
check(-3.1415927410125732, [219, 15, 73, 192]); | ||
check(0, [0]); | ||
check(1, [127, 0, 0, 0]); | ||
check(-1, [127, 1, 0, 0]); | ||
check(3.1415927410125732, [128, 182, 31, 146]); | ||
check(-3.1415927410125732, [128, 183, 31, 146]); | ||
check(Infinity, [255, 0, 0, 0]); | ||
check(-Infinity, [255, 1, 0, 0]); | ||
check(NaN, [255, 0, 0, 128]); | ||
}); | ||
@@ -176,3 +179,3 @@ | ||
check({}, [0]); | ||
check({x: 3.1415927410125732}, [1, 219, 15, 73, 64, 0]); | ||
check({x: 3.1415927410125732}, [1, 128, 182, 31, 146, 0]); | ||
}); | ||
@@ -179,0 +182,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
145424
2302