Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
typed arrays: set class name
Make obj.toString and Object.prototype.toString work correctly for typed arrays.
  • Loading branch information
bnoordhuis committed Jan 17, 2012
1 parent 2541009 commit 549443a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/v8_typed_array.cc
Expand Up @@ -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);

Expand Down Expand Up @@ -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> { };
Expand Down
48 changes: 48 additions & 0 deletions test/simple/test-typed-arrays-typenames.js
@@ -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]');
});

0 comments on commit 549443a

Please sign in to comment.