Bug 1700052 part 16 - Add tests for accessor properties. r=jonco

Differential Revision: https://phabricator.services.mozilla.com/D110263
This commit is contained in:
Jan de Mooij 2021-04-07 07:16:09 +00:00
parent 3040f90784
commit 11292be321
4 changed files with 273 additions and 0 deletions

View File

@ -49,6 +49,7 @@ tags = webrtc
[test_named_getter_enumerability.html]
[test_Object.prototype_props.html]
[test_proxy_expandos.html]
[test_proxy_accessors.html]
[test_returnUnion.html]
skip-if = debug == false
[test_usvstring.html]

View File

@ -0,0 +1,78 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1700052
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 1700052</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1700052">Mozilla Bug 1700052</a>
<p id="display"></p>
<form id="theform"></form>
<pre id="test">
<script>
function expandoTests() {
// Get a DOM proxy with an expando object. Define/redefine a "foo" getter/setter
// and ensure the right function is called.
var obj = document.getElementById("theform");
var count1 = 0, count2 = 0, count3 = 0;
var fun1 = function() { count1++; };
var fun2 = function() { count2++; };
var fun3 = function() { count3++; };
Object.defineProperty(obj, "foo", {configurable: true, get: fun1, set: fun1});
for (var i = 0; i < 100; i++) {
obj.foo;
obj.foo = i;
if (i === 50) {
Object.defineProperty(obj, "foo", {configurable: true, get: fun2, set: fun2});
} else if (i === 80) {
Object.defineProperty(obj, "foo", {configurable: true, get: fun3, set: fun3});
}
}
is(count1, 102, "call count for fun1 must match");
is(count2, 60, "call count for fun2 must match");
is(count3, 38, "call count for fun3 must match");
}
expandoTests();
function unshadowedTests() {
// Same as above, but for non-shadowing properties on the prototype.
var obj = document.getElementById("theform");
var proto = Object.getPrototypeOf(obj);
var count1 = 0, count2 = 0;
var fun1 = function() { count1++; };
var fun2 = function() { count2++; };
for (var i = 0; i < 100; i++) {
obj.name;
obj.name = "test";
if (i === 50) {
let desc = Object.getOwnPropertyDescriptor(proto, "name");
desc.get = desc.set = fun1;
Object.defineProperty(proto, "name", desc);
}
if (i === 90) {
let desc = Object.getOwnPropertyDescriptor(proto, "name");
desc.get = desc.set = fun2;
Object.defineProperty(proto, "name", desc);
}
}
is(count1, 80, "call count for fun1 must match");
is(count2, 18, "call count for fun2 must match");
}
unshadowedTests();
</script>
</pre>
</body>
</html>

View File

@ -0,0 +1,64 @@
// Objects with same shape but different getter/setter.
function testOwnProp() {
var count = 0;
var objects = [{get x() { count += 1; }, set x(v) { count += 2; }},
{get x() { count += 3; }, set x(v) { count += 4; }},
{get x() { count += 5; }, set x(v) { count += 6; }}];
assertEq(shapeOf(objects[0]), shapeOf(objects[1]));
assertEq(shapeOf(objects[0]), shapeOf(objects[2]));
for (var i = 0; i < 150; i++) {
var o = objects[i % objects.length];
o.x++;
}
assertEq(count, 1050);
}
testOwnProp();
// Ensure optimized TypedArray length properly deoptimizes when the getter is
// redefined.
function testTypedArrayLength() {
var ta = new Int32Array(10);
var lengthHolder = Object.getPrototypeOf(Int32Array.prototype);
for (var i = 0; i < 150; i++) {
assertEq(ta.length, i <= 100 ? 10 : (i <= 130 ? "foo" : "bar"));
assertEq(ta.byteLength, 40);
assertEq(ta.byteOffset, 0);
if (i === 100) {
var desc = Object.getOwnPropertyDescriptor(lengthHolder, "length");
desc.get = () => "foo";
Object.defineProperty(lengthHolder, "length", desc);
}
if (i === 130) {
var desc = Object.getOwnPropertyDescriptor(lengthHolder, "length");
desc.get = () => "bar";
Object.defineProperty(lengthHolder, "length", desc);
}
}
}
testTypedArrayLength();
// Native accessors on the global object. Redefine a few times and ensure the
// right function is called. Use |useWindowProxy: false| to allow optimizing all
// native getters.
function testGlobalProp() {
var g = newGlobal({useWindowProxy: false});
g.evaluate("" + function test() {
var arr = [Math, Object];
var expected = 0;
for (var i = 0; i < 150; i++) {
assertEq(timesAccessed, i <= 100 ? i + 1 : (i > 130 ? Infinity : NaN));
if (i === 100) {
var desc = Object.getOwnPropertyDescriptor(this, "timesAccessed");
desc.get = Math.abs;
Object.defineProperty(this, "timesAccessed", desc);
}
if (i === 130) {
var desc = Object.getOwnPropertyDescriptor(this, "timesAccessed");
desc.get = Math.min;
Object.defineProperty(this, "timesAccessed", desc);
}
}
});
g.evaluate("test()");
}
testGlobalProp();

View File

@ -0,0 +1,130 @@
function testRedefineGetter() {
var callGetter = function(o) {
return o.x;
};
var proto = {get foo() {}, bar: 1};
var obj = Object.create(proto);
// Add "x" getter on the prototype. Warm up the IC.
var count1 = 0;
Object.defineProperty(proto, "x", {get: function(v) {
count1++;
}, configurable: true});
for (var i = 0; i < 20; i++) {
callGetter(obj);
}
assertEq(count1, 20);
// Redefine "x" with a different getter. Ensure the new getter is called.
var count2 = 0;
Object.defineProperty(proto, "x", {get: function() {
count2++;
}, configurable: true});
for (var i = 0; i < 20; i++) {
callGetter(obj);
}
assertEq(count1, 20);
assertEq(count2, 20);
}
testRedefineGetter();
function testRedefineSetter() {
var callSetter = function(o) {
o.x = 1;
};
var proto = {get foo() {}, bar: 1};
var obj = Object.create(proto);
// Add "x" setter on the prototype. Warm up the IC.
var count1 = 0;
Object.defineProperty(proto, "x", {set: function(v) {
count1++;
}, configurable: true});
for (var i = 0; i < 20; i++) {
callSetter(obj);
}
assertEq(count1, 20);
// Redefine "x" with a different setter. Ensure the new setter is called.
var count2 = 0;
Object.defineProperty(proto, "x", {set: function() {
count2++;
}, configurable: true});
for (var i = 0; i < 20; i++) {
callSetter(obj);
}
assertEq(count1, 20);
assertEq(count2, 20);
}
testRedefineSetter();
function testDeleteAdd() {
var callGetter = function(o) {
return o.x;
};
var proto = {get foo() {}, bar: 1};
var obj = Object.create(proto);
// Add "x" getter on the prototype. Warm up the IC.
var count1 = 0;
Object.defineProperty(proto, "x", {get: function() {
count1++;
}, configurable: true});
for (var i = 0; i < 20; i++) {
callGetter(obj);
}
assertEq(count1, 20);
// Delete the getter.
delete proto.x;
// Add "x" back with a different getter. Ensure the new getter is called.
var count2 = 0;
Object.defineProperty(proto, "x", {get: function() {
count2++;
}, configurable: true});
for (var i = 0; i < 20; i++) {
callGetter(obj);
}
assertEq(count1, 20);
assertEq(count2, 20);
}
testDeleteAdd();
function testAccessorToDataAndBack() {
var callGetter = function(o) {
return o.x;
};
var proto = {get foo() {}, bar: 1};
var obj = Object.create(proto);
// Add "x" getter on the prototype. Warm up the IC.
var count1 = 0;
Object.defineProperty(proto, "x", {get: function() {
count1++;
}, configurable: true});
for (var i = 0; i < 20; i++) {
callGetter(obj);
}
assertEq(count1, 20);
// Turn the getter into a data property.
Object.defineProperty(proto, "x", {configurable: true, value: 123});
// Turn the data property into a (different) getter. Ensure the new getter is
// called.
var count2 = 0;
Object.defineProperty(proto, "x", {get: function() {
count2++;
}, configurable: true});
for (var i = 0; i < 20; i++) {
callGetter(obj);
}
assertEq(count1, 20);
assertEq(count2, 20);
}
testAccessorToDataAndBack();