Skip to content

Commit 1c42cfc

Browse files
jdorfmanljharb
authored andcommitted
Add tests for Function.prototype.caller and arguments properties
1 parent ada0636 commit 1c42cfc

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*---
2+
description: Function.prototype caller and arguments properties are accessor properties with ThrowTypeError
3+
esid: sec-function.prototype.caller
4+
info: |
5+
Function instances do not inherit the "caller" and "arguments" accessors
6+
from Function.prototype. The accessors exist only on Function.prototype.
7+
---*/
8+
9+
const callerDesc = Object.getOwnPropertyDescriptor(Function.prototype, "caller");
10+
const argumentsDesc = Object.getOwnPropertyDescriptor(Function.prototype, "arguments");
11+
12+
assert.sameValue(typeof callerDesc.get, "function");
13+
assert.sameValue(typeof callerDesc.set, "function");
14+
assert.sameValue(callerDesc.get, callerDesc.set, "caller getter/setter should be same function");
15+
16+
assert.sameValue(typeof argumentsDesc.get, "function");
17+
assert.sameValue(typeof argumentsDesc.set, "function");
18+
assert.sameValue(argumentsDesc.get, argumentsDesc.set, "arguments getter/setter should be same function");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*---
2+
description: Function.prototype caller and arguments properties use the same ThrowTypeError across realms
3+
features: [cross-realm]
4+
---*/
5+
6+
const otherRealm = $262.createRealm();
7+
const otherFunctionProto = otherRealm.evaluate('Function.prototype');
8+
9+
const mainCallerDesc = Object.getOwnPropertyDescriptor(Function.prototype, "caller");
10+
const otherCallerDesc = Object.getOwnPropertyDescriptor(otherFunctionProto, "caller");
11+
12+
assert.sameValue(mainCallerDesc.get, otherCallerDesc.get, "caller getter should be same across realms");
13+
assert.sameValue(mainCallerDesc.set, otherCallerDesc.set, "caller setter should be same across realms");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*---
2+
description: Function properties behave consistently in module context
3+
flags: [module]
4+
---*/
5+
6+
function normalFunc() {}
7+
function strictFunc() { }
8+
9+
assert(!Object.hasOwnProperty.call(normalFunc, "caller"), "normal function should not have caller");
10+
assert(!Object.hasOwnProperty.call(strictFunc, "caller"), "strict function should not have caller");
11+
assert(!Object.hasOwnProperty.call(normalFunc, "arguments"), "normal function should not have arguments");
12+
assert(!Object.hasOwnProperty.call(strictFunc, "arguments"), "strict function should not have arguments");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*---
2+
description: Function properties behavior in strict vs non-strict contexts
3+
flags: [noStrict]
4+
---*/
5+
6+
function nonStrictFunc() {
7+
return nonStrictFunc.caller;
8+
}
9+
10+
function strictFunc() {
11+
return strictFunc.caller;
12+
}
13+
14+
assert(!Object.hasOwnProperty.call(nonStrictFunc, "caller"), "non-strict function should not have own caller property");
15+
assert(!Object.hasOwnProperty.call(strictFunc, "caller"), "strict function should not have own caller property");
16+
17+
assert.throws(TypeError, () => nonStrictFunc(), "accessing caller should throw");
18+
assert.throws(TypeError, () => strictFunc(), "accessing caller should throw in strict mode");

0 commit comments

Comments
 (0)