Oh, I thought I mentioned that. It’s Coffeescript, http://coffeescript.org/. Here’s the Javascript translation:
Code: Select all// Generated by CoffeeScript 1.3.1
var pow, read_float32, read_float64, read_int32, read_n_bytes, read_uint32;
pow = Math.pow;
read_float64 = (function() {
var exp_offset, leftshift_32, rightshift_52, rightshift_exp_min;
leftshift_32 = pow(2, 32);
rightshift_52 = pow(2, -52);
rightshift_exp_min = pow(2, -(0x400 - 2));
exp_offset = -(0x400 - 1);
return function(file) {
var coef_high, coef_low, coefficient, exponent, high_bits, sign;
high_bits = read_int32(file);
sign = (high_bits >> 31) | 1;
exponent = high_bits << 1 >>> 21;
coef_high = high_bits & 0xfffff;
coef_low = read_uint32(file);
if (exponent === 0x7ff) {
if (coef_high || coef_low) {
return NaN;
} else {
return sign * Infinity;
}
} else if (!(exponent || coef_high || coef_low)) {
return sign * 0;
}
coefficient = (coef_high * leftshift_32 + coef_low) * rightshift_52;
if (!exponent) {
return sign * coefficient * rightshift_exp_min;
} else {
return sign * (1 + coefficient) * (pow(2, exponent + exp_offset));
}
};
})();
read_float32 = (function() {
var exp_offset, rightshift_23, rightshift_exp_min;
rightshift_23 = pow(2, -23);
rightshift_exp_min = pow(2, -(0x80 - 2));
exp_offset = -(0x80 - 1);
return function(file) {
var bits, coefficient, exponent, sign;
bits = read_int32(file);
sign = (bits >> 31) | 1;
exponent = bits << 1 >>> 24;
coefficient = bits & 0x7fffff;
if (exponent === 0xff) {
if (coefficient) {
return NaN;
} else {
return sign * Infinity;
}
} else if (!(exponent || coefficient)) {
return sign * 0;
}
coefficient *= rightshift_23;
if (!exponent) {
return sign * coefficient * rightshift_exp_min;
} else {
return sign * (1 + coefficient) * (pow(2, exponent + exp_offset));
}
};
})();
read_n_bytes = function(file, n) {
var bytes;
bytes = file.read(n);
if ((bytes != null ? bytes.length : void 0) !== n) {
throw new Error("Can't read " + n + " byte(s) from the file");
}
return bytes;
};
read_int32 = function(file) {
var bytes;
bytes = read_n_bytes(file, 4);
return ((bytes.charCodeAt(0)) << 24) + ((bytes.charCodeAt(1)) << 16) + ((bytes.charCodeAt(2)) << 8) + bytes.charCodeAt(3);
};
read_uint32 = function(file) {
var int32;
int32 = read_int32(file);
if (int32 > 0) {
return int32;
} else {
return int32 + 0x100000000;
}
};
[ANN] Public release of JSON Action Manager 1.0
[ANN] Public release of JSON Action Manager 1.0
Another JavaScript float packing/unpacking library can be found here:
https://github.com/pgriess/node-jspack/ ... /jspack.js ... /jspack.js
https://github.com/pgriess/node-jspack/ ... /jspack.js ... /jspack.js