Skip to content

Commit 7c37969

Browse files
authored
Function.prototype: Add tests "caller" and "arguments" properties (#4355)
Fixes #4340 Fixes #674
1 parent e99213a commit 7c37969

File tree

5 files changed

+110
-55
lines changed

5 files changed

+110
-55
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (C) 2024 Justin Dorfman. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-addrestrictedfunctionproperties
5+
description: >
6+
Function.prototype.arguments is an accessor property whose set and get
7+
functions are both %ThrowTypeError%.
8+
info: |
9+
2. Let _thrower_ be _realm_.[[Intrinsics]].[[%ThrowTypeError%]].
10+
3. Perform ! DefinePropertyOrThrow(_F_, *"caller"*, PropertyDescriptor { [[Get]]: _thrower_, [[Set]]: _thrower_, [[Enumerable]]: *false*, [[Configurable]]: *true* }).
11+
4. Perform ! DefinePropertyOrThrow(_F_, *"arguments"*, PropertyDescriptor { [[Get]]: _thrower_, [[Set]]: _thrower_, [[Enumerable]]: *false*, [[Configurable]]: *true* }).
12+
includes: [propertyHelper.js, wellKnownIntrinsicObjects.js]
13+
---*/
14+
15+
const argumentsDesc = Object.getOwnPropertyDescriptor(Function.prototype, 'arguments');
16+
17+
verifyProperty(
18+
Function.prototype,
19+
"arguments",
20+
{ enumerable: false, configurable: true },
21+
{ restore: true }
22+
);
23+
24+
assert.sameValue(typeof argumentsDesc.get, "function",
25+
"Function.prototype.arguments has function getter");
26+
assert.sameValue(typeof argumentsDesc.set, "function",
27+
"Function.prototype.arguments has function setter");
28+
assert.sameValue(argumentsDesc.get, argumentsDesc.set,
29+
"Function.prototype.arguments property getter/setter are the same function");
30+
31+
var throwTypeError;
32+
WellKnownIntrinsicObjects.forEach(function(record) {
33+
if (record.name === "%ThrowTypeError%") {
34+
throwTypeError = record.value;
35+
}
36+
});
37+
if (throwTypeError) {
38+
assert.sameValue(argumentsDesc.set, throwTypeError, "Function.prototype.arguments getter is %ThrowTypeError%");
39+
}
40+
assert.throws(TypeError, function() {
41+
return Function.prototype.arguments;
42+
});
43+
assert.throws(TypeError, function() {
44+
Function.prototype.arguments = arguments;
45+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (C) 2024 Justin Dorfman. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-addrestrictedfunctionproperties
5+
description: >
6+
Function.prototype.arguments and Function.prototype.arguments are both
7+
accessor properties whose set and get functions are both %ThrowTypeError%.
8+
info: |
9+
2. Let _thrower_ be _realm_.[[Intrinsics]].[[%ThrowTypeError%]].
10+
3. Perform ! DefinePropertyOrThrow(_F_, *"caller"*, PropertyDescriptor { [[Get]]: _thrower_, [[Set]]: _thrower_, [[Enumerable]]: *false*, [[Configurable]]: *true* }).
11+
4. Perform ! DefinePropertyOrThrow(_F_, *"arguments"*, PropertyDescriptor { [[Get]]: _thrower_, [[Set]]: _thrower_, [[Enumerable]]: *false*, [[Configurable]]: *true* }).
12+
---*/
13+
14+
const callerDesc = Object.getOwnPropertyDescriptor(Function.prototype, "caller");
15+
const argumentsDesc = Object.getOwnPropertyDescriptor(Function.prototype, "arguments");
16+
17+
// Other tests at ../{arguments,caller}/prop-desc.js already assert that each
18+
// getter/setter pair use a single function (and when possible, that the
19+
// function is %ThrowTypeError%), so this test only needs to assert equality
20+
// *across* the pairs.
21+
assert.sameValue(callerDesc.get, argumentsDesc.get,
22+
"Function.prototype.arguments and Function.prototype.caller accessor functions should match (%ThrowTypeError%)");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (C) 2024 Justin Dorfman. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-function.prototype.caller
5+
description: >
6+
Function.prototype.caller property descriptor
7+
info: |
8+
Function.prototype.caller is an accessor property whose set and get
9+
accessor functions are both %ThrowTypeError%.
10+
includes: [propertyHelper.js, wellKnownIntrinsicObjects.js]
11+
---*/
12+
13+
const callerDesc = Object.getOwnPropertyDescriptor(Function.prototype, 'caller');
14+
15+
verifyProperty(
16+
Function.prototype,
17+
"caller",
18+
{ enumerable: false, configurable: true },
19+
{ restore: true }
20+
);
21+
22+
assert.sameValue(typeof callerDesc.get, "function",
23+
"Function.prototype.caller has function getter");
24+
assert.sameValue(typeof callerDesc.set, "function",
25+
"Function.prototype.caller has function setter");
26+
assert.sameValue(callerDesc.get, callerDesc.set,
27+
"Caller property getter/setter are the same function");
28+
29+
var throwTypeError;
30+
WellKnownIntrinsicObjects.forEach(function(record) {
31+
if (record.name === "%ThrowTypeError%") {
32+
throwTypeError = record.value;
33+
}
34+
});
35+
if (throwTypeError) {
36+
assert.sameValue(callerDesc.set, throwTypeError, "Function.prototype.caller getter is %ThrowTypeError%");
37+
}
38+
assert.throws(TypeError, function() {
39+
return Function.prototype.caller;
40+
});
41+
assert.throws(TypeError, function fn() {
42+
Function.prototype.caller = fn;
43+
});

test/built-ins/Function/prototype/restricted-property-arguments.js

-27
This file was deleted.

test/built-ins/Function/prototype/restricted-property-caller.js

-28
This file was deleted.

0 commit comments

Comments
 (0)