You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constobj={m(){}};console.log(obj);// => { m: [Function: m] }// Mitigate the assignment override mistake for Function.prototype.constructor,// allowing the prototype to be frozen.// cf. https://github.com/endojs/endo/blob/b3f0c567/packages/ses/src/enable-property-overrides.js{const{ defineProperty, hasOwn }=Object;constBuiltinFunction=Function;constBuiltinFunctionPrototype=BuiltinFunction.prototype;constconstructorKey="constructor";Object.defineProperty(BuiltinFunctionPrototype,constructorKey,{get(){returnBuiltinFunction;},// A setter is not necessary to trigger this bug, but demonstrates the// motivation for defining such accessors.set(value){if(this===BuiltinFunctionPrototype)throwTypeError();if(hasOwn(this,constructorKey)){this.constructor=value;}else{constdesc={ value,writable: true,enumerable: true,configurable: true};defineProperty(this,constructorKey,desc);}},});}console.log(obj);// => { m: {} }
How often does it reproduce? Is there a required condition?
always
What is the expected behavior? Why is that the expected behavior?
I expect console.log({ m(){} }) output to always indicate that the value for property "m" is a function, even when Function.prototype.constructor is an accessor that won't be invoked.
What do you see instead?
Defining Function.prototype.constructor as an accessor replaces the useful output with text that makes it look like functions are plain objects.
-{ m: [Function: m] }+{ m: {} }
Additional information
My proposed fix is updating lib/internal/util/inspect.js formatRaw to privilege the "is function" check over "constructor is Object" while preserving their rendering details:
The way the constructor is replaced is tricky. Accessing the getter would trigger side effects and it's as such impossible to identify the constructor properly. We could explore to improve getConstructorName() to detect these as "unknown".
The suggestion for fixing functions is something we can do. The current order was for performance reasons, while it's not a big overhead to get the type of an argument.
Version
v22.11.0
Platform
Subsystem
util
What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?
always
What is the expected behavior? Why is that the expected behavior?
I expect
console.log({ m(){} })
output to always indicate that the value for property "m" is a function, even whenFunction.prototype.constructor
is an accessor that won't be invoked.What do you see instead?
Defining
Function.prototype.constructor
as an accessor replaces the useful output with text that makes it look like functions are plain objects.Additional information
My proposed fix is updating lib/internal/util/inspect.js
formatRaw
to privilege the "is function" check over "constructor is Object" while preserving their rendering details:node/lib/internal/util/inspect.js
Lines 957 to 969 in 01b9a54
Doing so will still affect the console/inspect output, but in a way that no longer fails to indicates functionness:
(although I am also open to tweaking that as well).
The text was updated successfully, but these errors were encountered: