Skip to content

Commit

Permalink
Add tests for Function.prototype.caller and arguments properties
Browse files Browse the repository at this point in the history
  • Loading branch information
jdorfman committed Dec 12, 2024
1 parent dc0082c commit d89d02c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*---
description: Function.prototype caller and arguments properties are accessor properties with ThrowTypeError
esid: sec-function.prototype.caller
info: |
Function instances do not inherit the "caller" and "arguments" accessors
from Function.prototype. The accessors exist only on Function.prototype.
---*/

const callerDesc = Object.getOwnPropertyDescriptor(Function.prototype, "caller");
const argumentsDesc = Object.getOwnPropertyDescriptor(Function.prototype, "arguments");

assert.sameValue(typeof callerDesc.get, "function");
assert.sameValue(typeof callerDesc.set, "function");
assert.sameValue(callerDesc.get, callerDesc.set, "caller getter/setter should be same function");

assert.sameValue(typeof argumentsDesc.get, "function");
assert.sameValue(typeof argumentsDesc.set, "function");
assert.sameValue(argumentsDesc.get, argumentsDesc.set, "arguments getter/setter should be same function");
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*---
description: Function.prototype caller and arguments properties use the same ThrowTypeError across realms
features: [cross-realm]
---*/

const otherRealm = $262.createRealm();
const otherFunctionProto = otherRealm.evaluate('Function.prototype');

const mainCallerDesc = Object.getOwnPropertyDescriptor(Function.prototype, "caller");
const otherCallerDesc = Object.getOwnPropertyDescriptor(otherFunctionProto, "caller");

assert.sameValue(mainCallerDesc.get, otherCallerDesc.get, "caller getter should be same across realms");
assert.sameValue(mainCallerDesc.set, otherCallerDesc.set, "caller setter should be same across realms");
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*---
description: Function properties behave consistently in module context
flags: [module]
---*/

function normalFunc() {}
function strictFunc() { }

assert(!Object.hasOwnProperty.call(normalFunc, "caller"), "normal function should not have caller");
assert(!Object.hasOwnProperty.call(strictFunc, "caller"), "strict function should not have caller");
assert(!Object.hasOwnProperty.call(normalFunc, "arguments"), "normal function should not have arguments");
assert(!Object.hasOwnProperty.call(strictFunc, "arguments"), "strict function should not have arguments");
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*---
description: Function properties behavior in strict vs non-strict contexts
flags: [noStrict]
---*/

function nonStrictFunc() {
return nonStrictFunc.caller;
}

function strictFunc() {
return strictFunc.caller;
}

assert(!Object.hasOwnProperty.call(nonStrictFunc, "caller"), "non-strict function should not have own caller property");
assert(!Object.hasOwnProperty.call(strictFunc, "caller"), "strict function should not have own caller property");

assert.throws(TypeError, () => nonStrictFunc(), "accessing caller should throw");
assert.throws(TypeError, () => strictFunc(), "accessing caller should throw in strict mode");

0 comments on commit d89d02c

Please sign in to comment.