You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by he...@apache.org on 2012/10/26 09:29:47 UTC

svn commit: r1402414 - in /thrift/trunk/lib/nodejs: lib/thrift/binary.js lib/thrift/binary_parser.js test/ test/binary.test.js

Author: henrique
Date: Fri Oct 26 07:29:47 2012
New Revision: 1402414

URL: http://svn.apache.org/viewvc?rev=1402414&view=rev
Log:
Thrift-1353: Switch to performance branch, get rid of BinaryParser
Client: Node.js
Patch: Wade Simmons

add missing files and licenses


Added:
    thrift/trunk/lib/nodejs/lib/thrift/binary.js
    thrift/trunk/lib/nodejs/test/
    thrift/trunk/lib/nodejs/test/binary.test.js
Removed:
    thrift/trunk/lib/nodejs/lib/thrift/binary_parser.js

Added: thrift/trunk/lib/nodejs/lib/thrift/binary.js
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/nodejs/lib/thrift/binary.js?rev=1402414&view=auto
==============================================================================
--- thrift/trunk/lib/nodejs/lib/thrift/binary.js (added)
+++ thrift/trunk/lib/nodejs/lib/thrift/binary.js Fri Oct 26 07:29:47 2012
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+var POW_8 = Math.pow(2, 8)
+var POW_16 = Math.pow(2, 16)
+var POW_24 = Math.pow(2, 24)
+var POW_32 = Math.pow(2, 32)
+var POW_40 = Math.pow(2, 40)
+var POW_48 = Math.pow(2, 48)
+var POW_52 = Math.pow(2, 52)
+var POW_1022 = Math.pow(2, 1022)
+
+exports.readI16 = function(buff, off) {
+  off = off || 0;
+  var v = buff[off + 1];
+  v += buff[off] << 8;
+  if (buff[off] & 128) {
+    v -= POW_16;
+  }
+  return v;
+}
+
+exports.readI32 = function(buff, off) {
+  off = off || 0;
+  var v = buff[off + 3];
+  v += buff[off + 2] << 8;
+  v += buff[off + 1] << 16;
+  v += buff[off] * POW_24;
+  if (buff[off] & 0x80) {
+    v -= POW_32;
+  }
+  return v;
+}
+
+exports.writeI16 = function(buff, v) {
+  buff[1] = v & 0xff;
+  v >>= 8;
+  buff[0] = v & 0xff;
+  return buff;
+}
+
+exports.writeI32 = function(buff, v) {
+  buff[3] = v & 0xff;
+  v >>= 8;
+  buff[2] = v & 0xff;
+  v >>= 8;
+  buff[1] = v & 0xff;
+  v >>= 8;
+  buff[0] = v & 0xff;
+  return buff;
+}
+
+exports.readDouble = function(buff, off) {
+  off = off || 0;
+  var signed = buff[off] & 0x80;
+  var e = (buff[off+1] & 0xF0) >> 4;
+  e += (buff[off] & 0x7F) << 4;
+
+  var m = buff[off+7];
+  m += buff[off+6] << 8;
+  m += buff[off+5] << 16;
+  m += buff[off+4] * POW_24;
+  m += buff[off+3] * POW_32;
+  m += buff[off+2] * POW_40;
+  m += (buff[off+1] & 0x0F) * POW_48;
+
+  switch (e) {
+    case 0:
+      e = -1022
+      break;
+    case 2047:
+      return m ? NaN : (signed ? -Infinity : Infinity);
+    default:
+      m += POW_52;
+      e -= 1023;
+  }
+
+  if (signed) {
+    m *= -1;
+  }
+
+  return m * Math.pow(2, e - 52);
+}
+
+/*
+ * Based on code from the jspack module:
+ * http://code.google.com/p/jspack/
+ */
+exports.writeDouble = function(buff, v) {
+  var m, e, c;
+
+  buff[0] = (v < 0 ? 0x80 : 0x00)
+
+  v = Math.abs(v)
+  if (v !== v) {
+    // NaN, use QNaN IEEE format
+    m = 2251799813685248;
+    e = 2047;
+  } else if (v === Infinity) {
+    m = 0;
+    e = 2047;
+  } else {
+    e = Math.floor(Math.log(v) / Math.LN2)
+    c = Math.pow(2, -e)
+    if (v * c < 1) {
+      e--;
+      c *= 2;
+    }
+
+    if (e + 1023 >= 2047)
+    {
+      // Overflow
+      m = 0;
+      e = 2047;
+    }
+    else if (e + 1023 >= 1)
+    {
+      // Normalized - term order matters, as Math.pow(2, 52-e) and v*Math.pow(2, 52) can overflow
+      m = (v*c-1) * POW_52;
+      e += 1023;
+    }
+    else
+    {
+      // Denormalized - also catches the '0' case, somewhat by chance
+      m = (v * POW_1022) * POW_52;
+      e = 0;
+    }
+  }
+
+  buff[1] = (e << 4) & 0xf0;
+  buff[0] |= (e >> 4) & 0x7f;
+
+  buff[7] = m & 0xff;
+  m = Math.floor(m / POW_8);
+  buff[6] = m & 0xff;
+  m = Math.floor(m / POW_8);
+  buff[5] = m & 0xff;
+  m = Math.floor(m / POW_8);
+  buff[4] = m & 0xff;
+  m >>= 8;
+  buff[3] = m & 0xff;
+  m >>= 8;
+  buff[2] = m & 0xff;
+  m >>= 8;
+  buff[1] |= m & 0x0f;
+
+  return buff;
+}

Added: thrift/trunk/lib/nodejs/test/binary.test.js
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/nodejs/test/binary.test.js?rev=1402414&view=auto
==============================================================================
--- thrift/trunk/lib/nodejs/test/binary.test.js (added)
+++ thrift/trunk/lib/nodejs/test/binary.test.js Fri Oct 26 07:29:47 2012
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+var assert = require('assert');
+var binary = require('thrift/binary');
+
+module.exports = {
+  "Should read I16": function() {
+    assert.equal(0, binary.readI16([0x00, 0x00]));
+    assert.equal(1, binary.readI16([0x00, 0x01]));
+    assert.equal(-1, binary.readI16([0xff, 0xff]));
+
+    // Min I16
+    assert.equal(-32768, binary.readI16([0x80, 0x00]));
+    // Max I16
+    assert.equal(32767, binary.readI16([0x7f, 0xff]));
+  },
+
+  "Should write I16": function() {
+    assert.deepEqual([0x00, 0x00], binary.writeI16([], 0));
+    assert.deepEqual([0x00, 0x01], binary.writeI16([], 1));
+    assert.deepEqual([0xff, 0xff], binary.writeI16([], -1));
+
+    // Min I16
+    assert.deepEqual([0x80, 0x00], binary.writeI16([], -32768));
+    // Max I16
+    assert.deepEqual([0x7f, 0xff], binary.writeI16([], 32767));
+  },
+
+  "Should read I32": function() {
+    assert.equal(0, binary.readI32([0x00, 0x00, 0x00, 0x00]));
+    assert.equal(1, binary.readI32([0x00, 0x00, 0x00, 0x01]));
+    assert.equal(-1, binary.readI32([0xff, 0xff, 0xff, 0xff]));
+
+    // Min I32
+    assert.equal(-2147483648, binary.readI32([0x80, 0x00, 0x00, 0x00]));
+    // Max I32
+    assert.equal(2147483647, binary.readI32([0x7f, 0xff, 0xff, 0xff]));
+  },
+
+  "Should write I32": function() {
+    assert.deepEqual([0x00, 0x00, 0x00, 0x00], binary.writeI32([], 0));
+    assert.deepEqual([0x00, 0x00, 0x00, 0x01], binary.writeI32([], 1));
+    assert.deepEqual([0xff, 0xff, 0xff, 0xff], binary.writeI32([], -1));
+
+    // Min I32
+    assert.deepEqual([0x80, 0x00, 0x00, 0x00], binary.writeI32([], -2147483648));
+    // Max I32
+    assert.deepEqual([0x7f, 0xff, 0xff, 0xff], binary.writeI32([], 2147483647));
+  },
+
+  "Should read doubles": function() {
+    assert.equal(0, binary.readDouble([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
+    assert.equal(0, binary.readDouble([0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
+    assert.equal(1, binary.readDouble([0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
+    assert.equal(2, binary.readDouble([0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
+    assert.equal(-2, binary.readDouble([0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
+
+    assert.equal(Math.PI, binary.readDouble([0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18]))
+
+    assert.equal(Infinity, binary.readDouble([0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
+    assert.equal(-Infinity, binary.readDouble([0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
+
+    assert.ok(isNaN(binary.readDouble([0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])))
+
+    assert.equal(1/3, binary.readDouble([0x3f, 0xd5, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55]))
+
+    // Min subnormal positive double
+    assert.equal(4.9406564584124654e-324, binary.readDouble([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]))
+    // Min normal positive double
+    assert.equal(2.2250738585072014e-308, binary.readDouble([0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
+    // Max positive double
+    assert.equal(1.7976931348623157e308, binary.readDouble([0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]))
+  },
+
+  "Should write doubles": function() {
+    assert.deepEqual([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 0));
+    assert.deepEqual([0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 1));
+    assert.deepEqual([0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 2));
+    assert.deepEqual([0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], -2));
+
+    assert.deepEqual([0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18], binary.writeDouble([], Math.PI));
+
+    assert.deepEqual([0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], Infinity));
+    assert.deepEqual([0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], -Infinity));
+
+    assert.deepEqual([0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], NaN));
+
+    assert.deepEqual([0x3f, 0xd5, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55], binary.writeDouble([], 1/3));
+
+    // Min subnormal positive double
+    assert.deepEqual([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01], binary.writeDouble([], 4.9406564584124654e-324)); 
+    // Min normal positive double
+    assert.deepEqual([0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 2.2250738585072014e-308)); 
+    // Max positive double
+    assert.deepEqual([0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], binary.writeDouble([], 1.7976931348623157e308)); 
+  }
+}