v8/test/mjsunit/es6/computed-property-names-object-literals-methods.js
Igor Sheludko 42ba9ef724 [runtime] Use custom maps for function closures ...
... that have computed name and/or require home object.

This should give us the opportunity to implement initialization
of name and home object values in a stub.

Bug: v8:6459
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng
Change-Id: I47a1a2c185e120e86c793733cce737811f895291
Reviewed-on: https://chromium-review.googlesource.com/512802
Reviewed-by: Ben Titzer <titzer@chromium.org>
Reviewed-by: Andreas Rossberg <rossberg@chromium.org>
Reviewed-by: Ross McIlroy <rmcilroy@chromium.org>
Reviewed-by: Camillo Bruni <cbruni@chromium.org>
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46638}
2017-07-13 13:23:06 +00:00

133 lines
3.0 KiB
JavaScript

// 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-async-iteration
function ID(x) {
return x;
}
(function TestMethodComputedNameString() {
var object = {
a() { return 'A'},
['b']() { return 'B'; },
c() { return 'C'; },
[ID('d')]() { return 'D'; },
};
assertEquals('A', object.a());
assertEquals('B', object.b());
assertEquals('C', object.c());
assertEquals('D', object.d());
assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(object));
})();
(function TestMethodComputedNameNumber() {
var object = {
a() { return 'A'; },
[1]() { return 'B'; },
c() { return 'C'; },
[ID(2)]() { return 'D'; },
};
assertEquals('A', object.a());
assertEquals('B', object[1]());
assertEquals('C', object.c());
assertEquals('D', object[2]());
// Array indexes first.
assertArrayEquals(['1', '2', 'a', 'c'], Object.keys(object));
})();
(function TestMethodComputedNameSymbol() {
var sym1 = Symbol();
var sym2 = Symbol();
var object = {
a() { return 'A'; },
[sym1]() { return 'B'; },
c() { return 'C'; },
[ID(sym2)]() { return 'D'; },
};
assertEquals('A', object.a());
assertEquals('B', object[sym1]());
assertEquals('C', object.c());
assertEquals('D', object[sym2]());
assertArrayEquals(['a', 'c'], Object.keys(object));
assertArrayEquals([sym1, sym2], Object.getOwnPropertySymbols(object));
})();
function assertIteratorResult(value, done, result) {
assertEquals({ value: value, done: done}, result);
}
(function TestGeneratorComputedName() {
var object = {
*['a']() {
yield 1;
yield 2;
},
async ['b']() {
return 42;
},
async *['c']() {
yield 1;
yield 2;
}
};
assertEquals('a', object.a.name);
assertEquals('b', object.b.name);
assertEquals('c', object.c.name);
var iter = object.a();
assertIteratorResult(1, false, iter.next());
assertIteratorResult(2, false, iter.next());
assertIteratorResult(undefined, true, iter.next());
assertArrayEquals(['a', 'b', 'c'], Object.keys(object));
})();
(function TestToNameSideEffects() {
var counter = 0;
var key1 = {
toString: function() {
assertEquals(0, counter++);
return 'b';
}
};
var key2 = {
toString: function() {
assertEquals(1, counter++);
return 'd';
}
};
var object = {
a() { return 'A'; },
[key1]() { return 'B'; },
c() { return 'C'; },
[key2]() { return 'D'; },
};
assertEquals(2, counter);
assertEquals('A', object.a());
assertEquals('B', object.b());
assertEquals('C', object.c());
assertEquals('D', object.d());
assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(object));
})();
(function TestDuplicateKeys() {
'use strict';
// ES5 does not allow duplicate keys.
// ES6 does but we haven't changed our code yet.
var object = {
a() { return 1; },
['a']() { return 2; },
};
assertEquals(2, object.a());
})();