From ed3d553d826963837dee5db9cf6cc88b2d5c62cc Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 11 Feb 2013 17:34:56 +0100 Subject: [PATCH] typed arrays: make call-as-function work for ctors Turn call-as-function calls into constructor calls. Makes the following snippet work: var buf = ArrayBuffer(32); // no 'new' but does the right thing --- src/v8_typed_array.cc | 6 +++--- test/simple/test-typed-arrays.js | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/v8_typed_array.cc b/src/v8_typed_array.cc index 2fe0196231c..f44503edc02 100644 --- a/src/v8_typed_array.cc +++ b/src/v8_typed_array.cc @@ -88,7 +88,7 @@ class ArrayBuffer { static v8::Handle V8New(const v8::Arguments& args) { if (!args.IsConstructCall()) - return ThrowTypeError("Constructor cannot be called as a function."); + return node::FromConstructorTemplate(GetTemplate(), args); // To match Chrome, we allow "new ArrayBuffer()". // if (args.Length() != 1) @@ -241,7 +241,7 @@ class TypedArray { private: static v8::Handle V8New(const v8::Arguments& args) { if (!args.IsConstructCall()) - return ThrowTypeError("Constructor cannot be called as a function."); + return node::FromConstructorTemplate(GetTemplate(), args); // To match Chrome, we allow "new Float32Array()". // if (args.Length() != 1) @@ -613,7 +613,7 @@ class DataView { private: static v8::Handle V8New(const v8::Arguments& args) { if (!args.IsConstructCall()) - return ThrowTypeError("Constructor cannot be called as a function."); + return node::FromConstructorTemplate(GetTemplate(), args); if (args.Length() < 1) return ThrowError("Wrong number of arguments."); diff --git a/test/simple/test-typed-arrays.js b/test/simple/test-typed-arrays.js index 3f74c58586b..9b55ee87c93 100644 --- a/test/simple/test-typed-arrays.js +++ b/test/simple/test-typed-arrays.js @@ -50,8 +50,14 @@ var assert = require('assert'); obj = new DataView(obj.buffer || obj); assert.equal(obj.toString(), '[object DataView]'); assert.equal(Object.prototype.toString.call(obj), '[object DataView]'); + + // Calling constructor as function should work. + clazz(32); }); +// Calling constructor as function should work. +DataView(ArrayBuffer(32)); + var buffer = new ArrayBuffer(16); var uint8 = new Uint8Array(buffer); var uint16 = new Uint16Array(buffer);