Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Revert "Test TypedArray's [[Set]] internal method with altered receiver"" #4200

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (C) 2021 Alexey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-integer-indexed-exotic-objects-set-p-v-receiver
description: >
If receiver is altered, `true` is returned for canonical numeric strings that are invalid indices.
Value is not coerced.
info: |
[[Set]] ( P, V, Receiver )

[...]
2. If Type(P) is String, then
a. Let numericIndex be ! CanonicalNumericIndexString(P).
b. If numericIndex is not undefined, then
i. If ! SameValue(O, Receiver) is true
[...]
ii. 1. Else if ! IsValidIntegerIndex(_O_, _numericIndex_) is *false*, return *true*.
includes: [testBigIntTypedArray.js]
features: [BigInt, TypedArray, Proxy]
---*/

var valueOfCalls = 0;
var value = {
valueOf: function() {
++valueOfCalls;
return 2n;
},
};

testWithBigIntTypedArrayConstructors(function(TA) {
var target, receiver;

[1, 1.5, -1].forEach(function(key) {
Object.defineProperty(TA.prototype, key, {
get: function() { throw new Test262Error(key + " getter should be unreachable!"); },
set: function(_v) { throw new Test262Error(key + " setter should be unreachable!"); },
configurable: true,
});


target = new TA([0n]);
receiver = Object.create(target);
receiver[key] = value;
assert(!target.hasOwnProperty(key), "target[" + key + "] should not be created (receiver: empty object)");
assert(!receiver.hasOwnProperty(key), "receiver[" + key + "] should not be created (receiver: empty object)");


var proxyTrapCalls = 0;
target = new TA([0n]);
receiver = new Proxy(Object.create(target), {
defineProperty(_target, key, desc) {
++proxyTrapCalls;
Object.defineProperty(_target, key, desc);
return true;
},
});
receiver[key] = value;
assert(!target.hasOwnProperty(key), "target[" + key + "] should not be created (receiver: proxy of an empty object)");
assert(!receiver.hasOwnProperty(key), "receiver[" + key + "] should not be created (receiver: proxy of an empty object)");
assert.sameValue(proxyTrapCalls, 0, "Proxy's [[DefineOwnProperty]] exotic method should not be called (key: " + key + ")");


target = new TA([0n]);
receiver = Object.preventExtensions(Object.create(target));
receiver[key] = value;
assert(!target.hasOwnProperty(key), "target[" + key + "] should not be created (receiver: non-extensible empty object)");
assert(!receiver.hasOwnProperty(key), "receiver[" + key + "] should not be created (receiver: non-extensible empty object)");


assert(delete TA.prototype[key]);
});


target = new TA([0n]);
receiver = Object.setPrototypeOf([], target);
receiver[1] = value;
assert(!target.hasOwnProperty(1), "target[1] should not be created (receiver: regular array)");
assert(!receiver.hasOwnProperty(1), "receiver[1] should not be created (receiver: regular array)");
assert.sameValue(receiver.length, 0, "Array's [[DefineOwnProperty]] exotic method should not be called");


target = new TA([0n]);
receiver = Object.setPrototypeOf(new String(""), target);
receiver[1] = value;
assert(!target.hasOwnProperty(1), "target[1] should not be created (receiver: empty String object)");
assert(!receiver.hasOwnProperty(1), "receiver[1] should remain unchanged (receiver: empty String object)");
});

assert.sameValue(valueOfCalls, 0, "value should not be coerced");
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (C) 2021 Alexey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-integer-indexed-exotic-objects-set-p-v-receiver
description: >
If receiver is altered, `true` is returned for canonical numeric strings that are invalid indices.
Value is not coerced.
info: |
[[Set]] ( P, V, Receiver )

[...]
2. If Type(P) is String, then
a. Let numericIndex be ! CanonicalNumericIndexString(P).
b. If numericIndex is not undefined, then
i. If ! SameValue(O, Receiver) is true
[...]
ii. 1. Else if ! IsValidIntegerIndex(_O_, _numericIndex_) is *false*, return *true*.
includes: [testBigIntTypedArray.js]
features: [BigInt, TypedArray, Reflect]
---*/

var valueOfCalls = 0;
var value = {
valueOf: function() {
++valueOfCalls;
return 2n;
},
};

testWithBigIntTypedArrayConstructors(function(TA) {
var target, receiver;

[1, 1.5, -1].forEach(function(key) {
Object.defineProperty(TA.prototype, key, {
get: function() { throw new Test262Error(key + " getter should be unreachable!"); },
set: function(_v) { throw new Test262Error(key + " setter should be unreachable!"); },
configurable: true,
});


target = new TA([0n]);
receiver = {};
assert(Reflect.set(target, key, value, receiver), "Reflect.set should succeed (key: " + key + ", receiver: empty object)");
assert(!target.hasOwnProperty(key), "target[" + key + "] should not be created (receiver: empty object)");
assert(!receiver.hasOwnProperty(key), "receiver[" + key + "] should not be created (receiver: empty object)");


target = new TA([0n]);
receiver = new TA([1n]);
assert(Reflect.set(target, key, value, receiver), "Reflect.set should succeed (key: " + key + ", receiver: another typed array of the same length)");
assert(!target.hasOwnProperty(key), "target[" + key + "] should not be created (receiver: another typed array of the same length)");
assert(!receiver.hasOwnProperty(key), "receiver[" + key + "] should not be created (receiver: another typed array of the same length)");


target = new TA([0n]);
receiver = Object.defineProperty({}, key, {
get: function() { return 1n; },
set: function(_v) { throw new Test262Error(key + " setter should be unreachable!"); },
configurable: true,
});
assert(Reflect.set(target, key, value, receiver), "Reflect.set should succeed (receiver: plain object with " + key + " accessor)");
assert(!target.hasOwnProperty(key), "target[" + key + "] should not be created (receiver: plain object with " + key + " accessor)");
assert.sameValue(receiver[key], 1n, "receiver[" + key + "] should remain unchanged (receiver: plain object with " + key + " accessor)");


target = new TA([0n]);
receiver = Object.defineProperty({}, key, { value: 1n, writable: false, configurable: true });
assert(Reflect.set(target, key, value, receiver), "Reflect.set should succeed (receiver: plain object with non-writable " + key + ")");
assert(!target.hasOwnProperty(key), "target[" + key + "] should not be created (receiver: plain object with non-writable " + key + ")");
assert.sameValue(receiver[key], 1n, "receiver[" + key + "] should remain unchanged (receiver: plain object with non-writable " + key + ")");


target = new TA([0n]);
receiver = Object.preventExtensions({});
assert(Reflect.set(target, key, value, receiver), "Reflect.set should fail (key: " + key + ", receiver: non-extensible empty object)");
assert(!target.hasOwnProperty(key), "target[" + key + "] should not be created (receiver: non-extensible empty object)");
assert(!receiver.hasOwnProperty(key), "receiver[" + key + "] should not be created (receiver: non-extensible empty object)");


assert(delete TA.prototype[key]);
});


target = new TA([0n]);
receiver = new TA([1n, 1n]);
assert(Reflect.set(target, 1, value, receiver), "Reflect.set should succeed (receiver: another typed array of greater length)");
assert(!target.hasOwnProperty(1), "target[1] should not be created (receiver: another typed array of greater length)");
assert.sameValue(receiver[1], 1n, "receiver[1] should remain unchanged (receiver: another typed array of greater length)");
});

assert.sameValue(valueOfCalls, 0, "value should not be coerced");
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (C) 2021 Alexey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-integer-indexed-exotic-objects-set-p-v-receiver
description: >
If receiver is altered, OrdinarySet result is returned for valid indices.
Value is not coerced.
info: |
[[Set]] ( P, V, Receiver )

[...]
2. If Type(P) is String, then
a. Let numericIndex be ! CanonicalNumericIndexString(P).
b. If numericIndex is not undefined, then
[...]
3. Return ? OrdinarySet(O, P, V, Receiver).
includes: [testBigIntTypedArray.js]
features: [BigInt, TypedArray, Proxy]
---*/

var valueOfCalls = 0;
var value = {
valueOf: function() {
++valueOfCalls;
return 2n;
},
};

testWithBigIntTypedArrayConstructors(function(TA) {
var target, receiver;

Object.defineProperty(TA.prototype, 0, {
get: function() { throw new Test262Error("0 getter should be unreachable!"); },
set: function(_v) { throw new Test262Error("0 setter should be unreachable!"); },
configurable: true,
});


target = new TA([0n]);
receiver = Object.create(target);
receiver[0] = value;
assert.sameValue(target[0], 0n, "target[0] should remain unchanged (receiver: empty object)");
assert.sameValue(receiver[0], value, "receiver[0] should be updated (receiver: empty object)");


var proxyTrapCalls = 0;
target = new TA([0n]);
receiver = new Proxy(Object.create(target), {
defineProperty(_target, key, desc) {
++proxyTrapCalls;
Object.defineProperty(_target, key, desc);
return true;
},
});
receiver[0] = value;
assert.sameValue(target[0], 0n, "target[0] should remain unchanged (receiver: proxy of an empty object)");
assert.sameValue(receiver[0], value, "receiver[0] should be created (receiver: proxy of an empty object)");
assert.sameValue(proxyTrapCalls, 1, "Proxy's [[DefineOwnProperty]] exotic method should be called");


target = new TA([0n]);
receiver = Object.preventExtensions(Object.create(target));
assert.throws(TypeError, function() { "use strict"; receiver[0] = value; },
"setting receiver[0] should throw in strict mode (receiver: non-extensible empty object)");
assert.sameValue(target[0], 0n, "target[0] should remain unchanged (receiver: non-extensible empty object)");
assert(!receiver.hasOwnProperty(0), "receiver[0] should not be created (receiver: non-extensible empty object)");


target = new TA([0n]);
receiver = Object.setPrototypeOf([], target);
receiver[0] = value;
assert.sameValue(target[0], 0n, "target[0] should remain unchanged (receiver: regular array)");
assert.sameValue(receiver[0], value, "receiver[0] should be created (receiver: regular array)");
assert.sameValue(receiver.length, 1, "Array's [[DefineOwnProperty]] exotic method should be called");


target = new TA([0n]);
receiver = Object.setPrototypeOf(new String(""), target);
receiver[0] = value;
assert.sameValue(target[0], 0n, "target[0] should remain unchanged (receiver: empty String object)");
assert.sameValue(receiver[0], value, "receiver[0] should be created (receiver: empty String object)");


assert(delete TA.prototype[0]);
});

assert.sameValue(valueOfCalls, 0, "value should not be coerced");
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (C) 2021 Alexey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-integer-indexed-exotic-objects-set-p-v-receiver
description: >
If receiver is altered, OrdinarySet result is returned for valid indices.
Value is not coerced.
info: |
[[Set]] ( P, V, Receiver )

[...]
2. If Type(P) is String, then
a. Let numericIndex be ! CanonicalNumericIndexString(P).
b. If numericIndex is not undefined, then
[...]
3. Return ? OrdinarySet(O, P, V, Receiver).
includes: [testBigIntTypedArray.js]
features: [BigInt, TypedArray, Reflect]
---*/

var valueOfCalls = 0;
var value = {
valueOf: function() {
++valueOfCalls;
return 2n;
},
};

testWithBigIntTypedArrayConstructors(function(TA) {
var target, receiver;

Object.defineProperty(TA.prototype, 0, {
get: function() { throw new Test262Error("0 getter should be unreachable!"); },
set: function(_v) { throw new Test262Error("0 setter should be unreachable!"); },
configurable: true,
});


target = new TA([0n]);
receiver = {};
assert(Reflect.set(target, 0, value, receiver), "Reflect.set should succeed (receiver: empty object)");
assert.sameValue(target[0], 0n, "target[0] should remain unchanged (receiver: empty object)");
assert.sameValue(receiver[0], value, "receiver[0] should be created (receiver: empty object)");


target = new TA([0n]);
receiver = new TA([1n]);
assert(Reflect.set(target, 0, Object(2n), receiver), "Reflect.set should succeed (receiver: another typed array of the same length)");
assert.sameValue(target[0], 0n, "target[0] should remain unchanged (receiver: another typed array of the same length)");
assert.sameValue(receiver[0], 2n, "receiver[0] should be updated (receiver: another typed array of the same length)");


target = new TA([0n, 0n]);
receiver = new TA([1n]);
assert(!Reflect.set(target, 1, value, receiver), "Reflect.set should fail (receiver: another typed array of shorter length)");
assert.sameValue(target[1], 0n, "target[1] should remain unchanged (receiver: another typed array of shorter length)");
assert(!receiver.hasOwnProperty(1), "receiver[1] should not be created (receiver: another typed array of shorter length)");


target = new TA([0n]);
receiver = Object.preventExtensions({});
assert(!Reflect.set(target, 0, value, receiver), "Reflect.set should fail (receiver: non-extensible empty object)");
assert.sameValue(target[0], 0n, "target[0] should remain unchanged (receiver: non-extensible empty object)");
assert(!receiver.hasOwnProperty(0), "receiver[0] should not be created (receiver: non-extensible empty object)");


target = new TA([0n]);
receiver = {
get 0() { return 1n; },
set 0(_v) { throw new Test262Error("0 setter should be unreachable!"); },
};
assert(!Reflect.set(target, 0, value, receiver), "Reflect.set should fail (receiver: plain object with 0 accessor)");
assert.sameValue(target[0], 0n, "target[0] should remain unchanged (receiver: plain object with 0 accessor)");
assert.sameValue(receiver[0], 1n, "receiver[0] should remain unchanged (receiver: plain object with 0 accessor)");


target = new TA([0n]);
receiver = Object.defineProperty({}, 0, { value: 1n, writable: false, configurable: true });
assert(!Reflect.set(target, 0, value, receiver), "Reflect.set should fail (receiver: plain object with non-writable 0)");
assert.sameValue(target[0], 0n, "target[0] should remain unchanged (receiver: plain object with non-writable 0)");
assert.sameValue(receiver[0], 1n, "receiver[0] should remain unchanged (receiver: plain object with non-writable 0)");


assert(delete TA.prototype[0]);
});

assert.sameValue(valueOfCalls, 0, "value should not be coerced");
Loading
Loading