v8/test/mjsunit/es6/class-computed-property-names-super.js
Marja Hölttä 31d2bb8670 Reland2 [super] Store home object in Context instead of JSFunction
Fix 1: Track Scope::needs_home_object and Scope::uses_super_property
accurately. When "eval" is seen, figure out whether it can access
"super" and if yes, set the corresponding home object as needed.

Fix 2: The object literal scope shouldn't be entered for things
inside spreads.

Original: https://chromium-review.googlesource.com/c/v8/v8/+/2563275
Previous reland: https://chromium-review.googlesource.com/c/v8/v8/+/2637220

This saves memory (the home object doesn't need to be stored for each
method, but only once per class) and hopefully makes the home object
a constant in the optimized code.

Detailed documentation of the changes:
https://docs.google.com/document/d/1ZVXcoQdf9IdMsnRI9iyUjyq9NDoEyx9nA3XqMgwflMs/edit?usp=sharing

Bug: v8:9237
Bug: chromium:1167918
Bug: chromium:1167981
Bug: chromium:1167988
Bug: chromium:1168055
Bug: chromium:1171195
Bug: chromium:1171600
Change-Id: I9686e0d90cd0c1128757eca440a88748897ee91e
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2655509
Commit-Queue: Marja Hölttä <marja@chromium.org>
Reviewed-by: Leszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/master@{#72422}
2021-01-29 09:19:23 +00:00

166 lines
5.4 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: --allow-natives-syntax
function ID(x) {
return x;
}
function assertMethodDescriptor(object, name) {
var descr = Object.getOwnPropertyDescriptor(object, name);
assertTrue(descr.configurable);
assertFalse(descr.enumerable);
assertTrue(descr.writable);
assertEquals('function', typeof descr.value);
assertFalse('prototype' in descr.value);
assertEquals("" + name, descr.value.name);
}
function assertGetterDescriptor(object, name) {
var descr = Object.getOwnPropertyDescriptor(object, name);
assertTrue(descr.configurable);
assertFalse(descr.enumerable);
assertEquals('function', typeof descr.get);
assertFalse('prototype' in descr.get);
assertEquals(undefined, descr.set);
assertEquals("get " + name, descr.get.name);
}
function assertSetterDescriptor(object, name) {
var descr = Object.getOwnPropertyDescriptor(object, name);
assertTrue(descr.configurable);
assertFalse(descr.enumerable);
assertEquals(undefined, descr.get);
assertEquals('function', typeof descr.set);
assertFalse('prototype' in descr.set);
assertEquals("set " + name, descr.set.name);
}
function assertAccessorDescriptor(object, name) {
var descr = Object.getOwnPropertyDescriptor(object, name);
assertTrue(descr.configurable);
assertFalse(descr.enumerable);
assertEquals('function', typeof descr.get);
assertEquals('function', typeof descr.set);
assertFalse('prototype' in descr.get);
assertFalse('prototype' in descr.set);
assertEquals("get " + name, descr.get.name);
assertEquals("set " + name, descr.set.name);
}
(function TestComputedMethodSuper() {
class Base {
m() {
return ' base m';
}
}
class Derived extends Base {
['a']() { return 'a' + super.m(); }
[ID('b')]() { return 'b' + super.m(); }
[0]() { return '0' + super.m(); }
[ID(1)]() { return '1' + super.m(); }
[ID(2147483649)]() { return '2147483649' + super.m(); }
[ID(4294967294)]() { return '4294967294' + super.m(); }
[ID(4294967295)]() { return '4294967295' + super.m(); }
}
assertMethodDescriptor(Derived.prototype, "a");
assertMethodDescriptor(Derived.prototype, "b");
assertMethodDescriptor(Derived.prototype, 0);
assertMethodDescriptor(Derived.prototype, 1);
assertMethodDescriptor(Derived.prototype, 2147483649);
assertMethodDescriptor(Derived.prototype, 4294967294);
assertMethodDescriptor(Derived.prototype, 4294967295);
assertEquals('a base m', new Derived().a());
assertEquals('b base m', new Derived().b());
assertEquals('0 base m', new Derived()[0]());
assertEquals('1 base m', new Derived()[1]());
assertEquals('2147483649 base m', new Derived()[2147483649]());
assertEquals('4294967294 base m', new Derived()[4294967294]());
assertEquals('4294967295 base m', new Derived()[4294967295]());
})();
(function TestComputedGetterSuper() {
class Base {
m() {
return ' base m';
}
}
class Derived extends Base {
get ['a']() { return 'a' + super.m(); }
get [ID('b')]() { return 'b' + super.m(); }
get [0]() { return '0' + super.m(); }
get [ID(1)]() { return '1' + super.m(); }
get [ID(2147483649)]() { return '2147483649' + super.m(); }
get [ID(4294967294)]() { return '4294967294' + super.m(); }
get [ID(4294967295)]() { return '4294967295' + super.m(); }
}
assertGetterDescriptor(Derived.prototype, "a");
assertGetterDescriptor(Derived.prototype, "b");
assertGetterDescriptor(Derived.prototype, 0);
assertGetterDescriptor(Derived.prototype, 1);
assertGetterDescriptor(Derived.prototype, 2147483649);
assertGetterDescriptor(Derived.prototype, 4294967294);
assertGetterDescriptor(Derived.prototype, 4294967295);
assertEquals('a base m', new Derived().a);
assertEquals('b base m', new Derived().b);
assertEquals('0 base m', new Derived()[0]);
assertEquals('1 base m', new Derived()[1]);
assertEquals('2147483649 base m', new Derived()[2147483649]);
assertEquals('4294967294 base m', new Derived()[4294967294]);
assertEquals('4294967295 base m', new Derived()[4294967295]);
})();
(function TestComputedSetterSuper() {
var value;
class Base {
m(name, v) {
value = name + ' ' + v;
}
}
class Derived extends Base {
set ['a'](v) { super.m('a', v); }
set [ID('b')](v) { super.m('b', v); }
set [0](v) { super.m('0', v); }
set [ID(1)](v) { super.m('1', v); }
set [ID(2147483649)](v) { super.m('2147483649', v); }
set [ID(4294967294)](v) { super.m('4294967294', v); }
set [ID(4294967295)](v) { super.m('4294967295', v); }
}
assertSetterDescriptor(Derived.prototype, "a");
assertSetterDescriptor(Derived.prototype, "b");
assertSetterDescriptor(Derived.prototype, 0);
assertSetterDescriptor(Derived.prototype, 1);
assertSetterDescriptor(Derived.prototype, 2147483649);
assertSetterDescriptor(Derived.prototype, 4294967294);
assertSetterDescriptor(Derived.prototype, 4294967295);
new Derived().a = 2;
assertEquals('a 2', value);
new Derived().b = 3;
assertEquals('b 3', value);
new Derived()[0] = 4;
assertEquals('0 4', value);
new Derived()[1] = 5;
assertEquals('1 5', value);
new Derived()[2147483649] = 6;
assertEquals('2147483649 6', value);
new Derived()[4294967294] = 7;
assertEquals('4294967294 7', value);
new Derived()[4294967295] = 8;
assertEquals('4294967295 8', value);
})();