From 08ee4d3a5c1790f371240f54dddc41baef7ce040 Mon Sep 17 00:00:00 2001 From: "dslomov@chromium.org" Date: Fri, 24 Oct 2014 19:28:23 +0000 Subject: [PATCH] Add remaining @@toStringTag symbols to builtins R=dslomov@chromium.org Review URL: https://codereview.chromium.org/664333003 Patch from Caitlin Potter . Cr-Commit-Position: refs/heads/master@{#24885} git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24885 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/array-iterator.js | 2 ++ src/collection-iterator.js | 4 ++++ src/heap-snapshot-generator.cc | 4 ++-- src/json.js | 2 ++ src/math.js | 2 ++ src/runtime/runtime-function.cc | 27 +++++++++++++++++++++++---- src/string-iterator.js | 2 ++ src/typedarray.js | 12 +++++++++++- test/mjsunit/es6/array-iterator.js | 11 ++++++++++- test/mjsunit/es6/collection-iterator.js | 20 +++++++++++++++++++- test/mjsunit/es6/json.js | 15 +++++++++++++++ test/mjsunit/es6/math.js | 15 +++++++++++++++ test/mjsunit/es6/string-iterator.js | 7 +++++++ test/mjsunit/harmony/typedarrays.js | 24 ++++++++++++++++++++++++ 14 files changed, 138 insertions(+), 9 deletions(-) create mode 100644 test/mjsunit/es6/json.js create mode 100644 test/mjsunit/es6/math.js diff --git a/src/array-iterator.js b/src/array-iterator.js index 5ced9da..864d5c1 100644 --- a/src/array-iterator.js +++ b/src/array-iterator.js @@ -112,6 +112,8 @@ function SetUpArrayIterator() { %FunctionSetName(ArrayIteratorIterator, '[Symbol.iterator]'); %AddNamedProperty(ArrayIterator.prototype, symbolIterator, ArrayIteratorIterator, DONT_ENUM); + %AddNamedProperty(ArrayIterator.prototype, symbolToStringTag, + "Array Iterator", READ_ONLY | DONT_ENUM); } SetUpArrayIterator(); diff --git a/src/collection-iterator.js b/src/collection-iterator.js index 2bccc8d..92d45a9 100644 --- a/src/collection-iterator.js +++ b/src/collection-iterator.js @@ -77,6 +77,8 @@ function SetUpSetIterator() { %FunctionSetName(SetIteratorSymbolIterator, '[Symbol.iterator]'); %AddNamedProperty(SetIterator.prototype, symbolIterator, SetIteratorSymbolIterator, DONT_ENUM); + %AddNamedProperty(SetIterator.prototype, symbolToStringTag, + "Set Iterator", READ_ONLY | DONT_ENUM); } SetUpSetIterator(); @@ -174,6 +176,8 @@ function SetUpMapIterator() { %FunctionSetName(MapIteratorSymbolIterator, '[Symbol.iterator]'); %AddNamedProperty(MapIterator.prototype, symbolIterator, MapIteratorSymbolIterator, DONT_ENUM); + %AddNamedProperty(MapIterator.prototype, symbolToStringTag, + "Map Iterator", READ_ONLY | DONT_ENUM); } SetUpMapIterator(); diff --git a/src/heap-snapshot-generator.cc b/src/heap-snapshot-generator.cc index c4f9600..680e1cc 100644 --- a/src/heap-snapshot-generator.cc +++ b/src/heap-snapshot-generator.cc @@ -1711,11 +1711,11 @@ bool V8HeapExplorer::ExtractAccessorPairProperty( AccessorPair* accessors = AccessorPair::cast(callback_obj); Object* getter = accessors->getter(); if (!getter->IsOddball()) { - SetPropertyReference(js_obj, entry, String::cast(key), getter, "get %s"); + SetPropertyReference(js_obj, entry, Name::cast(key), getter, "get %s"); } Object* setter = accessors->setter(); if (!setter->IsOddball()) { - SetPropertyReference(js_obj, entry, String::cast(key), setter, "set %s"); + SetPropertyReference(js_obj, entry, Name::cast(key), setter, "set %s"); } return true; } diff --git a/src/json.js b/src/json.js index f767f4a..e2b7dc8 100644 --- a/src/json.js +++ b/src/json.js @@ -220,6 +220,8 @@ function JSONStringify(value, replacer, space) { function SetUpJSON() { %CheckIsBootstrapping(); + %AddNamedProperty($JSON, symbolToStringTag, "JSON", READ_ONLY | DONT_ENUM); + // Set up non-enumerable properties of the JSON object. InstallFunctions($JSON, DONT_ENUM, $Array( "parse", JSONParse, diff --git a/src/math.js b/src/math.js index 13d030c..860b62f 100644 --- a/src/math.js +++ b/src/math.js @@ -321,6 +321,8 @@ function SetUpMath() { %AddNamedProperty(global, "Math", $Math, DONT_ENUM); %FunctionSetInstanceClassName(MathConstructor, 'Math'); + %AddNamedProperty($Math, symbolToStringTag, "Math", READ_ONLY | DONT_ENUM); + // Set up math constants. InstallConstants($Math, $Array( // ECMA-262, section 15.8.1.1. diff --git a/src/runtime/runtime-function.cc b/src/runtime/runtime-function.cc index b57064f..e25b659 100644 --- a/src/runtime/runtime-function.cc +++ b/src/runtime/runtime-function.cc @@ -67,13 +67,32 @@ RUNTIME_FUNCTION(Runtime_FunctionGetName) { } +static Handle NameToFunctionName(Handle name) { + Handle stringName(name->GetHeap()->empty_string()); + + // TODO(caitp): Follow proper rules in section 9.2.11 (SetFunctionName) + if (name->IsSymbol()) { + Handle description(Handle::cast(name)->name(), + name->GetIsolate()); + if (description->IsString()) { + stringName = Handle::cast(description); + } + } else { + stringName = Handle::cast(name); + } + + return stringName; +} + + RUNTIME_FUNCTION(Runtime_FunctionSetName) { - SealHandleScope shs(isolate); + HandleScope scope(isolate); DCHECK(args.length() == 2); - CONVERT_ARG_CHECKED(JSFunction, f, 0); - CONVERT_ARG_CHECKED(String, name, 1); - f->shared()->set_name(name); + CONVERT_ARG_HANDLE_CHECKED(JSFunction, f, 0); + CONVERT_ARG_HANDLE_CHECKED(Name, name, 1); + + f->shared()->set_name(*NameToFunctionName(name)); return isolate->heap()->undefined_value(); } diff --git a/src/string-iterator.js b/src/string-iterator.js index cb578e7..dcaddaf 100644 --- a/src/string-iterator.js +++ b/src/string-iterator.js @@ -87,6 +87,8 @@ function SetUpStringIterator() { %FunctionSetName(StringIteratorIterator, '[Symbol.iterator]'); %AddNamedProperty(StringIterator.prototype, symbolIterator, StringIteratorIterator, DONT_ENUM); + %AddNamedProperty(StringIterator.prototype, symbolToStringTag, + "String Iterator", READ_ONLY | DONT_ENUM); } SetUpStringIterator(); diff --git a/src/typedarray.js b/src/typedarray.js index c149b35..4420bce 100644 --- a/src/typedarray.js +++ b/src/typedarray.js @@ -291,6 +291,13 @@ function TypedArraySet(obj, offset) { } } +function TypedArrayGetToStringTag() { + if (!%IsTypedArray(this)) return; + var name = %_ClassOf(this); + if (IS_UNDEFINED(name)) return; + return name; +} + // ------------------------------------------------------------------- function SetupTypedArrays() { @@ -310,7 +317,8 @@ macro SETUP_TYPED_ARRAY(ARRAY_ID, NAME, ELEMENT_SIZE) InstallGetter(global.NAME.prototype, "byteOffset", NAME_GetByteOffset); InstallGetter(global.NAME.prototype, "byteLength", NAME_GetByteLength); InstallGetter(global.NAME.prototype, "length", NAME_GetLength); - + InstallGetter(global.NAME.prototype, symbolToStringTag, + TypedArrayGetToStringTag); InstallFunctions(global.NAME.prototype, DONT_ENUM, $Array( "subarray", NAMESubArray, "set", TypedArraySet @@ -437,6 +445,8 @@ function SetupDataView() { // Set up constructor property on the DataView prototype. %AddNamedProperty($DataView.prototype, "constructor", $DataView, DONT_ENUM); + %AddNamedProperty( + $DataView.prototype, symbolToStringTag, "DataView", READ_ONLY|DONT_ENUM); InstallGetter($DataView.prototype, "buffer", DataViewGetBufferJS); InstallGetter($DataView.prototype, "byteOffset", DataViewGetByteOffset); diff --git a/test/mjsunit/es6/array-iterator.js b/test/mjsunit/es6/array-iterator.js index 96122cd..767991e 100644 --- a/test/mjsunit/es6/array-iterator.js +++ b/test/mjsunit/es6/array-iterator.js @@ -25,7 +25,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// Flags: --allow-natives-syntax +// Flags: --allow-natives-syntax --harmony-tostring var NONE = 0; @@ -158,6 +158,15 @@ function TestArrayIteratorPrototype() { Object.getOwnPropertyNames(ArrayIteratorPrototype)); assertHasOwnProperty(ArrayIteratorPrototype, 'next', DONT_ENUM); assertHasOwnProperty(ArrayIteratorPrototype, Symbol.iterator, DONT_ENUM); + + assertEquals("[object Array Iterator]", + Object.prototype.toString.call(iterator)); + assertEquals("Array Iterator", ArrayIteratorPrototype[Symbol.toStringTag]); + var desc = Object.getOwnPropertyDescriptor( + ArrayIteratorPrototype, Symbol.toStringTag); + assertTrue(desc.configurable); + assertFalse(desc.writable); + assertEquals("Array Iterator", desc.value); } TestArrayIteratorPrototype(); diff --git a/test/mjsunit/es6/collection-iterator.js b/test/mjsunit/es6/collection-iterator.js index 5503fe5..18b3f1a 100644 --- a/test/mjsunit/es6/collection-iterator.js +++ b/test/mjsunit/es6/collection-iterator.js @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// Flags: --allow-natives-syntax +// Flags: --allow-natives-syntax --harmony-tostring (function TestSetIterator() { @@ -19,6 +19,15 @@ assertEquals(new Set().values().__proto__, SetIteratorPrototype); assertEquals(new Set().entries().__proto__, SetIteratorPrototype); + + assertEquals("[object Set Iterator]", + Object.prototype.toString.call(iter)); + assertEquals("Set Iterator", SetIteratorPrototype[Symbol.toStringTag]); + var desc = Object.getOwnPropertyDescriptor( + SetIteratorPrototype, Symbol.toStringTag); + assertTrue(desc.configurable); + assertFalse(desc.writable); + assertEquals("Set Iterator", desc.value); })(); @@ -120,6 +129,15 @@ assertEquals(new Map().values().__proto__, MapIteratorPrototype); assertEquals(new Map().keys().__proto__, MapIteratorPrototype); assertEquals(new Map().entries().__proto__, MapIteratorPrototype); + + assertEquals("[object Map Iterator]", + Object.prototype.toString.call(iter)); + assertEquals("Map Iterator", MapIteratorPrototype[Symbol.toStringTag]); + var desc = Object.getOwnPropertyDescriptor( + MapIteratorPrototype, Symbol.toStringTag); + assertTrue(desc.configurable); + assertFalse(desc.writable); + assertEquals("Map Iterator", desc.value); })(); diff --git a/test/mjsunit/es6/json.js b/test/mjsunit/es6/json.js new file mode 100644 index 0000000..3fad083 --- /dev/null +++ b/test/mjsunit/es6/json.js @@ -0,0 +1,15 @@ +// Copyright 2014 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Flags: --harmony-tostring + +function testJSONToString() { + assertEquals('[object JSON]', "" + JSON); + assertEquals("JSON", JSON[Symbol.toStringTag]); + var desc = Object.getOwnPropertyDescriptor(JSON, Symbol.toStringTag); + assertTrue(desc.configurable); + assertFalse(desc.writable); + assertEquals("JSON", desc.value); +} +testJSONToString(); diff --git a/test/mjsunit/es6/math.js b/test/mjsunit/es6/math.js new file mode 100644 index 0000000..3f76f11 --- /dev/null +++ b/test/mjsunit/es6/math.js @@ -0,0 +1,15 @@ +// Copyright 2014 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Flags: --harmony-tostring + +function testMathToString() { + assertEquals('[object Math]', "" + Math); + assertEquals("Math", Math[Symbol.toStringTag]); + var desc = Object.getOwnPropertyDescriptor(Math, Symbol.toStringTag); + assertTrue(desc.configurable); + assertFalse(desc.writable); + assertEquals("Math", desc.value); +} +testMathToString(); diff --git a/test/mjsunit/es6/string-iterator.js b/test/mjsunit/es6/string-iterator.js index e6bea6d..769f549 100644 --- a/test/mjsunit/es6/string-iterator.js +++ b/test/mjsunit/es6/string-iterator.js @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +// Flags: --harmony-tostring function TestStringPrototypeIterator() { assertTrue(String.prototype.hasOwnProperty(Symbol.iterator)); @@ -59,6 +60,12 @@ function TestStringIteratorPrototype() { assertArrayEquals(['next'], Object.getOwnPropertyNames(StringIteratorPrototype)); assertEquals('[object String Iterator]', "" + iterator); + assertEquals("String Iterator", StringIteratorPrototype[Symbol.toStringTag]); + var desc = Object.getOwnPropertyDescriptor( + StringIteratorPrototype, Symbol.toStringTag); + assertTrue(desc.configurable); + assertFalse(desc.writable); + assertEquals("String Iterator", desc.value); } TestStringIteratorPrototype(); diff --git a/test/mjsunit/harmony/typedarrays.js b/test/mjsunit/harmony/typedarrays.js index 537fba3..a4d6e79 100644 --- a/test/mjsunit/harmony/typedarrays.js +++ b/test/mjsunit/harmony/typedarrays.js @@ -265,6 +265,17 @@ function TestTypedArray(constr, elementSize, typicalElement) { assertSame(0, aNoParam.length); assertSame(0, aNoParam.byteLength); assertSame(0, aNoParam.byteOffset); + + var a = new constr(ab, 64*elementSize, 128); + assertEquals("[object " + constr.name + "]", + Object.prototype.toString.call(a)); + var desc = Object.getOwnPropertyDescriptor( + constr.prototype, Symbol.toStringTag); + assertTrue(desc.configurable); + assertFalse(desc.enumerable); + assertFalse(!!desc.writable); + assertFalse(!!desc.set); + assertEquals("function", typeof desc.get); } TestTypedArray(Uint8Array, 1, 0xFF); @@ -654,6 +665,19 @@ function TestDataViewPropertyTypeChecks() { TestDataViewPropertyTypeChecks(); + +function TestDataViewToStringTag() { + var a = new DataView(new ArrayBuffer(10)); + assertEquals("[object DataView]", Object.prototype.toString.call(a)); + var desc = Object.getOwnPropertyDescriptor( + DataView.prototype, Symbol.toStringTag); + assertTrue(desc.configurable); + assertFalse(desc.enumerable); + assertFalse(desc.writable); + assertEquals("DataView", desc.value); +} + + // General tests for properties // Test property attribute [[Enumerable]] -- 2.7.4