2013-03-01 13:28:55 +00:00
|
|
|
// Copyright 2013 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (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: --harmony-symbols --harmony-collections
|
|
|
|
// Flags: --expose-gc --allow-natives-syntax
|
|
|
|
|
|
|
|
var symbols = []
|
|
|
|
|
|
|
|
// Test different forms of constructor calls, all equivalent.
|
|
|
|
function TestNew() {
|
|
|
|
function IndirectSymbol() { return new Symbol }
|
|
|
|
function indirect() { return new IndirectSymbol() }
|
2013-03-22 16:33:50 +00:00
|
|
|
for (var i = 0; i < 2; ++i) {
|
|
|
|
for (var j = 0; j < 5; ++j) {
|
|
|
|
symbols.push(Symbol())
|
2013-03-22 16:51:28 +00:00
|
|
|
symbols.push(Symbol(undefined))
|
|
|
|
symbols.push(Symbol("66"))
|
|
|
|
symbols.push(Symbol(66))
|
2013-03-22 16:33:50 +00:00
|
|
|
symbols.push(Symbol(Symbol()))
|
|
|
|
symbols.push((new Symbol).valueOf())
|
|
|
|
symbols.push((new Symbol()).valueOf())
|
|
|
|
symbols.push((new Symbol(Symbol())).valueOf())
|
|
|
|
symbols.push(Object(Symbol()).valueOf())
|
|
|
|
symbols.push((indirect()).valueOf())
|
|
|
|
}
|
|
|
|
%OptimizeFunctionOnNextCall(indirect)
|
|
|
|
indirect() // Call once before GC throws away type feedback.
|
|
|
|
gc() // Promote existing symbols and then allocate some more.
|
2013-03-01 13:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TestNew()
|
|
|
|
|
|
|
|
|
|
|
|
function TestType() {
|
|
|
|
for (var i in symbols) {
|
2013-03-22 16:33:50 +00:00
|
|
|
assertEquals("symbol", typeof symbols[i])
|
|
|
|
assertTrue(typeof symbols[i] === "symbol")
|
|
|
|
assertEquals(null, %_ClassOf(symbols[i]))
|
|
|
|
assertEquals("Symbol", %_ClassOf(new Symbol(symbols[i])))
|
|
|
|
assertEquals("Symbol", %_ClassOf(Object(symbols[i])))
|
2013-03-01 13:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TestType()
|
|
|
|
|
|
|
|
|
2013-03-22 16:33:50 +00:00
|
|
|
function TestPrototype() {
|
|
|
|
assertSame(Object.prototype, Symbol.prototype.__proto__)
|
|
|
|
assertSame(Symbol.prototype, Symbol().__proto__)
|
|
|
|
assertSame(Symbol.prototype, Symbol(Symbol()).__proto__)
|
|
|
|
assertSame(Symbol.prototype, (new Symbol).__proto__)
|
|
|
|
assertSame(Symbol.prototype, (new Symbol()).__proto__)
|
|
|
|
assertSame(Symbol.prototype, (new Symbol(Symbol())).__proto__)
|
|
|
|
assertSame(Symbol.prototype, Object(Symbol()).__proto__)
|
|
|
|
for (var i in symbols) {
|
|
|
|
assertSame(Symbol.prototype, symbols[i].__proto__)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TestPrototype()
|
|
|
|
|
|
|
|
|
2013-03-22 16:51:28 +00:00
|
|
|
function TestName() {
|
|
|
|
for (var i in symbols) {
|
|
|
|
var name = symbols[i].name
|
|
|
|
assertTrue(name === undefined || name === "66")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TestName()
|
|
|
|
|
|
|
|
|
2013-03-22 16:33:50 +00:00
|
|
|
function TestToString() {
|
|
|
|
for (var i in symbols) {
|
|
|
|
assertThrows(function() { String(symbols[i]) }, TypeError)
|
|
|
|
assertThrows(function() { symbols[i] + "" }, TypeError)
|
|
|
|
assertThrows(function() { symbols[i].toString() }, TypeError)
|
|
|
|
assertThrows(function() { (new Symbol(symbols[i])).toString() }, TypeError)
|
|
|
|
assertThrows(function() { Object(symbols[i]).toString() }, TypeError)
|
|
|
|
assertEquals("[object Symbol]", Object.prototype.toString.call(symbols[i]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TestToString()
|
|
|
|
|
|
|
|
|
|
|
|
function TestToBoolean() {
|
|
|
|
for (var i in symbols) {
|
|
|
|
assertTrue(Boolean(symbols[i]).valueOf())
|
|
|
|
assertFalse(!symbols[i])
|
|
|
|
assertTrue(!!symbols[i])
|
|
|
|
assertTrue(symbols[i] && true)
|
|
|
|
assertFalse(!symbols[i] && false)
|
|
|
|
assertTrue(!symbols[i] || true)
|
|
|
|
assertEquals(1, symbols[i] ? 1 : 2)
|
|
|
|
assertEquals(2, !symbols[i] ? 1 : 2)
|
|
|
|
if (!symbols[i]) assertUnreachable();
|
|
|
|
if (symbols[i]) {} else assertUnreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TestToBoolean()
|
|
|
|
|
|
|
|
|
|
|
|
function TestToNumber() {
|
|
|
|
for (var i in symbols) {
|
|
|
|
assertSame(NaN, Number(symbols[i]).valueOf())
|
|
|
|
assertSame(NaN, symbols[i] + 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TestToNumber()
|
|
|
|
|
|
|
|
|
2013-03-01 13:28:55 +00:00
|
|
|
function TestEquality() {
|
|
|
|
// Every symbol should equal itself.
|
|
|
|
for (var i in symbols) {
|
|
|
|
assertSame(symbols[i], symbols[i])
|
|
|
|
assertEquals(symbols[i], symbols[i])
|
|
|
|
assertTrue(Object.is(symbols[i], symbols[i]))
|
|
|
|
assertTrue(symbols[i] === symbols[i])
|
|
|
|
assertTrue(symbols[i] == symbols[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// All symbols should be distinct.
|
|
|
|
for (var i = 0; i < symbols.length; ++i) {
|
|
|
|
for (var j = i + 1; j < symbols.length; ++j) {
|
|
|
|
assertFalse(Object.is(symbols[i], symbols[j]))
|
|
|
|
assertFalse(symbols[i] === symbols[j])
|
|
|
|
assertFalse(symbols[i] == symbols[j])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TestEquality()
|
|
|
|
|
|
|
|
|
|
|
|
function TestGet() {
|
|
|
|
for (var i in symbols) {
|
2013-03-22 16:33:50 +00:00
|
|
|
assertThrows(function() { symbols[i].toString() }, TypeError)
|
|
|
|
assertEquals(symbols[i], symbols[i].valueOf())
|
2013-03-01 13:28:55 +00:00
|
|
|
assertEquals(undefined, symbols[i].a)
|
|
|
|
assertEquals(undefined, symbols[i]["a" + "b"])
|
|
|
|
assertEquals(undefined, symbols[i]["" + "1"])
|
|
|
|
assertEquals(undefined, symbols[i][62])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TestGet()
|
|
|
|
|
|
|
|
|
|
|
|
function TestSet() {
|
|
|
|
for (var i in symbols) {
|
|
|
|
symbols[i].toString = 0
|
2013-03-22 16:33:50 +00:00
|
|
|
assertThrows(function() { symbols[i].toString() }, TypeError)
|
|
|
|
symbols[i].valueOf = 0
|
|
|
|
assertEquals(symbols[i], symbols[i].valueOf())
|
2013-03-01 13:28:55 +00:00
|
|
|
symbols[i].a = 0
|
|
|
|
assertEquals(undefined, symbols[i].a)
|
|
|
|
symbols[i]["a" + "b"] = 0
|
|
|
|
assertEquals(undefined, symbols[i]["a" + "b"])
|
|
|
|
symbols[i][62] = 0
|
|
|
|
assertEquals(undefined, symbols[i][62])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TestSet()
|
|
|
|
|
|
|
|
|
2013-03-05 17:24:08 +00:00
|
|
|
function TestCollections() {
|
|
|
|
var set = new Set
|
2013-03-04 15:00:57 +00:00
|
|
|
var map = new Map
|
2013-03-05 17:24:08 +00:00
|
|
|
var weakmap = new WeakMap
|
2013-03-01 13:28:55 +00:00
|
|
|
for (var i in symbols) {
|
2013-03-05 17:24:08 +00:00
|
|
|
set.add(symbols[i])
|
2013-03-01 13:28:55 +00:00
|
|
|
map.set(symbols[i], i)
|
2013-03-05 17:24:08 +00:00
|
|
|
weakmap.set(symbols[i], i)
|
2013-03-01 13:28:55 +00:00
|
|
|
}
|
2013-03-05 17:24:08 +00:00
|
|
|
assertEquals(symbols.length, set.size)
|
|
|
|
assertEquals(symbols.length, map.size)
|
2013-03-01 13:28:55 +00:00
|
|
|
for (var i in symbols) {
|
2013-03-05 17:24:08 +00:00
|
|
|
assertTrue(set.has(symbols[i]))
|
2013-03-01 13:28:55 +00:00
|
|
|
assertTrue(map.has(symbols[i]))
|
2013-03-05 17:24:08 +00:00
|
|
|
assertTrue(weakmap.has(symbols[i]))
|
2013-03-01 13:28:55 +00:00
|
|
|
assertEquals(i, map.get(symbols[i]))
|
2013-03-05 17:24:08 +00:00
|
|
|
assertEquals(i, weakmap.get(symbols[i]))
|
2013-03-01 13:28:55 +00:00
|
|
|
}
|
2013-03-05 17:24:08 +00:00
|
|
|
for (var i in symbols) {
|
|
|
|
assertTrue(set.delete(symbols[i]))
|
|
|
|
assertTrue(map.delete(symbols[i]))
|
|
|
|
assertTrue(weakmap.delete(symbols[i]))
|
|
|
|
}
|
|
|
|
assertEquals(0, set.size)
|
|
|
|
assertEquals(0, map.size)
|
2013-03-01 13:28:55 +00:00
|
|
|
}
|
2013-03-05 17:24:08 +00:00
|
|
|
TestCollections()
|
2013-03-04 15:00:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function TestKeySet(obj) {
|
|
|
|
// Set the even symbols via assignment.
|
|
|
|
for (var i = 0; i < symbols.length; i += 2) {
|
|
|
|
obj[symbols[i]] = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function TestKeyDefine(obj) {
|
|
|
|
// Set the odd symbols via defineProperty (as non-enumerable).
|
|
|
|
for (var i = 1; i < symbols.length; i += 2) {
|
|
|
|
Object.defineProperty(obj, symbols[i], {value: i, configurable: true})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function TestKeyGet(obj) {
|
|
|
|
var obj2 = Object.create(obj)
|
|
|
|
for (var i in symbols) {
|
|
|
|
assertEquals(i|0, obj[symbols[i]])
|
|
|
|
assertEquals(i|0, obj2[symbols[i]])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function TestKeyHas() {
|
|
|
|
for (var i in symbols) {
|
|
|
|
assertTrue(symbols[i] in obj)
|
|
|
|
assertTrue(Object.hasOwnProperty.call(obj, symbols[i]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function TestKeyEnum(obj) {
|
2013-03-06 13:55:21 +00:00
|
|
|
for (var name in obj) {
|
2013-03-22 16:33:50 +00:00
|
|
|
assertTrue(typeof name !== "symbol")
|
2013-03-04 15:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-07 15:43:47 +00:00
|
|
|
function TestKeyNames(obj) {
|
2013-03-06 13:55:21 +00:00
|
|
|
assertEquals(0, Object.keys(obj).length)
|
2013-03-07 15:43:47 +00:00
|
|
|
|
|
|
|
var names = Object.getOwnPropertyNames(obj)
|
|
|
|
assertTrue(symbols.length <= names.length)
|
|
|
|
// TODO(rossberg): once we have iterators, the following would be:
|
|
|
|
// var expected = new Set(symbols)
|
|
|
|
var expected = new Set
|
|
|
|
for (var i = 0; i < symbols.length; ++i) expected.add(symbols[i])
|
|
|
|
for (var i = 0; i < names.length; ++i) {
|
|
|
|
var name = names[i]
|
2013-03-22 16:33:50 +00:00
|
|
|
if (typeof name === 'symbol') {
|
2013-03-07 15:43:47 +00:00
|
|
|
assertTrue(expected.has(name))
|
|
|
|
expected.delete(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assertEquals(0, expected.size)
|
2013-03-04 15:00:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function TestKeyDescriptor(obj) {
|
|
|
|
for (var i in symbols) {
|
|
|
|
var desc = Object.getOwnPropertyDescriptor(obj, symbols[i]);
|
|
|
|
assertEquals(i|0, desc.value)
|
|
|
|
assertTrue(desc.configurable)
|
|
|
|
assertEquals(i % 2 == 0, desc.writable)
|
|
|
|
assertEquals(i % 2 == 0, desc.enumerable)
|
|
|
|
assertEquals(i % 2 == 0,
|
|
|
|
Object.prototype.propertyIsEnumerable.call(obj, symbols[i]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function TestKeyDelete(obj) {
|
|
|
|
for (var i in symbols) {
|
|
|
|
delete obj[symbols[i]]
|
|
|
|
}
|
|
|
|
for (var i in symbols) {
|
|
|
|
assertEquals(undefined, Object.getOwnPropertyDescriptor(obj, symbols[i]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var objs = [{}, [], Object.create(null), Object(1), new Map, function(){}]
|
|
|
|
|
|
|
|
for (var i in objs) {
|
|
|
|
var obj = objs[i]
|
|
|
|
TestKeySet(obj)
|
|
|
|
TestKeyDefine(obj)
|
|
|
|
TestKeyGet(obj)
|
|
|
|
TestKeyHas(obj)
|
|
|
|
TestKeyEnum(obj)
|
2013-03-07 15:43:47 +00:00
|
|
|
TestKeyNames(obj)
|
2013-03-04 15:00:57 +00:00
|
|
|
TestKeyDescriptor(obj)
|
|
|
|
TestKeyDelete(obj)
|
|
|
|
}
|