typed arrays: set class name
authorBen Noordhuis <info@bnoordhuis.nl>
Tue, 17 Jan 2012 15:20:37 +0000 (16:20 +0100)
committerBen Noordhuis <info@bnoordhuis.nl>
Tue, 17 Jan 2012 15:20:39 +0000 (16:20 +0100)
Make obj.toString and Object.prototype.toString work correctly for typed arrays.

src/v8_typed_array.cc
test/simple/test-typed-arrays-typenames.js [new file with mode: 0644]

index 00eff87..4b80da0 100644 (file)
@@ -140,6 +140,7 @@ class TypedArray {
     v8::HandleScope scope;
     ft_cache = v8::Persistent<v8::FunctionTemplate>::New(
         v8::FunctionTemplate::New(&TypedArray<TBytes, TEAType>::V8New));
+    ft_cache->SetClassName(v8::String::New(TypeName()));
     v8::Local<v8::ObjectTemplate> instance = ft_cache->InstanceTemplate();
     instance->SetInternalFieldCount(0);
 
@@ -376,6 +377,20 @@ class TypedArray {
     return TypedArray<TBytes, TEAType>::GetTemplate()->
         GetFunction()->NewInstance(3, argv);
   }
+
+  static const char* TypeName() {
+    switch (TEAType) {
+      case v8::kExternalByteArray: return "Int8Array";
+      case v8::kExternalUnsignedByteArray: return "Uint8Array";
+      case v8::kExternalShortArray: return "Int16Array";
+      case v8::kExternalUnsignedShortArray: return "Uint16Array";
+      case v8::kExternalIntArray: return "Int32Array";
+      case v8::kExternalUnsignedIntArray: return "Uint32Array";
+      case v8::kExternalFloatArray: return "Float32Array";
+      case v8::kExternalDoubleArray: return "Float64Array";
+    }
+    abort();
+  }
 };
 
 class Int8Array : public TypedArray<1, v8::kExternalByteArray> { };
diff --git a/test/simple/test-typed-arrays-typenames.js b/test/simple/test-typed-arrays-typenames.js
new file mode 100644 (file)
index 0000000..a78c759
--- /dev/null
@@ -0,0 +1,48 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common');
+var assert = require('assert');
+
+// TODO: merge with test-typed-arrays.js some time in the future.
+// That file only exists in master right now.
+[
+  'ArrayBuffer',
+  'Int8Array',
+  'Uint8Array',
+  'Int16Array',
+  'Uint16Array',
+  'Int32Array',
+  'Uint32Array',
+  'Float32Array',
+  'Float64Array'
+].forEach(function(name) {
+  var expected = '[object ' + name + ']';
+  var clazz = global[name];
+  var obj = new clazz(1);
+
+  assert.equal(obj.toString(), expected);
+  assert.equal(Object.prototype.toString.call(obj), expected);
+
+  obj = new DataView(obj);
+  assert.equal(obj.toString(), '[object DataView]');
+  assert.equal(Object.prototype.toString.call(obj), '[object DataView]');
+});