-
Notifications
You must be signed in to change notification settings - Fork 472
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
Add tests for Function.prototype.caller and arguments properties #4355
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, nice! Then this PR can be largely refactored to modernize those existing tests. test/built-ins/Function/prototype/restricted-property-arguments.js → test/built-ins/Function/prototype/arguments/prop-desc.js // Copyright (C) 2024 Justin Dorfman. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-addrestrictedfunctionproperties
description: >
Function.prototype.arguments
info: |
2. Let _thrower_ be _realm_.[[Intrinsics]].[[%ThrowTypeError%]].
3. Perform ! DefinePropertyOrThrow(_F_, *"caller"*, PropertyDescriptor { [[Get]]: _thrower_, [[Set]]: _thrower_, [[Enumerable]]: *false*, [[Configurable]]: *true* }).
4. Perform ! DefinePropertyOrThrow(_F_, *"arguments"*, PropertyDescriptor { [[Get]]: _thrower_, [[Set]]: _thrower_, [[Enumerable]]: *false*, [[Configurable]]: *true* }).
includes: [propertyHelper.js, wellKnownIntrinsicObjects.js]
---*/
verifyProperty(Function.prototype, "arguments", {
enumerable: false,
configurable: true
});
var descriptor = Object.getOwnPropertyDescriptor(Function.prototype, "caller");
assert.sameValue(typeof descriptor.get, "function", "Function.prototype.arguments has a getter");
assert.sameValue(typeof descriptor.set, "function", "Function.prototype.arguments has a setter");
assert.sameValue(descriptor.get, descriptor.set, "Function.prototype.arguments getter and setter are identical");
var throwTypeError;
WellKnownIntrinsicObjects.forEach(function(record) {
if (record.name === "%ThrowTypeError%") {
throwTypeError = record.value;
}
});
if (throwTypeError) {
assert.sameValue(descriptor.set, throwTypeError, "Function.prototype.arguments getter is %ThrowTypeError%");
}
assert.throws(TypeError, function() {
return Function.prototype.arguments;
});
assert.throws(TypeError, function() {
Function.prototype.arguments = {};
}); test/built-ins/Function/prototype/restricted-property-caller.js → test/built-ins/Function/prototype/caller/prop-desc.js By the way @anba, how are you finding the existing coverage? It's something that I struggle with, especially in the case of test subdirectories that heavily use old patterns. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gibson042 I added the refactored tests. For the other files I'm not sure what to do. Should I still address the issues or will they be removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gibson042 @anba Should I still address the other reviews/suggestions? |
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is incorrect: |
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() { } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Functions are implicitly in strict-mode when defined in modules. |
||
|
||
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The strict-mode case is already covered by: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should modernize that as well, introducing a helper like
and removing all other restricted-properties files. |
||
|
||
assert.throws(TypeError, () => nonStrictFunc(), "accessing caller should throw"); | ||
assert.throws(TypeError, () => strictFunc(), "accessing caller should throw in strict mode"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sec-function.prototype.caller
does not appear to be a valid anchor: https://tc39.es/ecma262/#sec-function.prototype.callerSee also CONTRIBUTING.md: Test Case Style.