js/core: Update stringify.js

It seems like the implementation of Uint8Array is influx w.r.t. browser and node.
It may be that it's getting forEach on it in the browser now.

Anyway, here's some bugfixing. I hope it works on all Jenkins nodes.

Change-Id: I1429528a3517bc4327982646e07ab86d55f81bc9
diff --git a/src/vdl/stringify.js b/src/vdl/stringify.js
index ed60283..42aa44b 100644
--- a/src/vdl/stringify.js
+++ b/src/vdl/stringify.js
@@ -35,7 +35,13 @@
   var seenObj = { id: seen.size };
   seen.set(val, seenObj);
 
-  if (Array.isArray(val)) {
+  // TODO(alexfandrianto): UintXArray and the other TypedArray seem to be in
+  // flux right now, with respect to their node and browser implementations.
+  // TypedArray doesn't seem to exist in 'node', but it looks like it's being
+  // added in the browser. For now, we will check if the internal buffer is an
+  // ArrayBuffer to identify TypedArray in both node and browser.
+  // https://github.com/vanadium/issues/issues/692
+  if (Array.isArray(val) || val.buffer instanceof ArrayBuffer) {
     var arrStr = '[';
     for (var ai = 0; ai < val.length; ai++) {
       if (ai > 0) {
@@ -52,7 +58,7 @@
   // Extract val's keys and values in a consistent order.
   var keys = [];
   var values = [];
-  if (val.forEach !== undefined) {
+  if (val instanceof Set || val instanceof Map) {
     // We have to make sure to print maps and sets in sorted key order.
     // While Set and Map have an iteration order equivalent to their insertion
     // order, we still want non-matching insertion orders to have matching
diff --git a/src/vom/binary-reader.js b/src/vom/binary-reader.js
index fd17058..8ee317e 100644
--- a/src/vom/binary-reader.js
+++ b/src/vom/binary-reader.js
@@ -59,7 +59,7 @@
 BinaryReader.prototype.readByteArray = function(amt) {
   var arr = this.buf.subarray(this.pos, this.pos + amt);
   this.pos += amt;
-  if (this.pos > this.buf) {
+  if (this.pos > this.buf.length) {
     return Promise.reject(
       new Error('Failed to read ' + amt + ' bytes. Hit EOF.'));
   }
diff --git a/test/vdl/test-stringify.js b/test/vdl/test-stringify.js
index b9a894b..c9df294 100644
--- a/test/vdl/test-stringify.js
+++ b/test/vdl/test-stringify.js
@@ -106,6 +106,18 @@
         repeatedObject, repeatedObject
       ],
       expected: '[{"a":5},{"a":5}]'
+    },
+    {
+      input: new Uint8Array([4, 10]),
+      expected: '[4,10]'
+    },
+    {
+      input: new Uint16Array([2560, 10]),
+      expected: '[2560,10]'
+    },
+    {
+      input: new Date(0),
+      expected: 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)'
     }
   ];
   for (var i = 0; i < tests.length; i++) {