Skip to content

Commit

Permalink
Fixes Reflect.defineProperty property name mismatch (#57)
Browse files Browse the repository at this point in the history
This PR fixes the `Reflect.defineProperty` polyfill, which uses the
result of the `ToPropertyDescriptor` abstract operation incorrectly.
  • Loading branch information
mhassan1 authored Jul 10, 2024
1 parent c05a288 commit 3b924bc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
17 changes: 16 additions & 1 deletion polyfills/Reflect/defineProperty/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@ CreateMethodProperty(Reflect, 'defineProperty', function defineProperty(target,
var desc = ToPropertyDescriptor(attributes);
// 4. Return ? target.[[DefineOwnProperty]](key, desc).
try {
Object.defineProperty(target, key, desc);
var newDesc = {};
var props = [
["[[Value]]", "value"],
["[[Get]]", "get"],
["[[Set]]", "set"],
["[[Writable]]", "writable"],
["[[Enumerable]]", "enumerable"],
["[[Configurable]]", "configurable"]
];
for (var i = 0; i < props.length; i++) {
var prop = props[i];
if (prop[0] in desc) {
newDesc[prop[1]] = desc[prop[0]];
}
}
Object.defineProperty(target, key, newDesc);
return true;
} catch (e) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion polyfills/Reflect/defineProperty/polyfill.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ it('is not enumerable', function () {

it('returns true if defining the property was a success', function () {
var o = {};
proclaim.isTrue(Reflect.defineProperty(o, 'a', {}));
proclaim.isTrue(Reflect.defineProperty(o, 'a', { value: 'hi' }));
proclaim.isTrue(Object.prototype.hasOwnProperty.call(o, 'a'));
proclaim.equal(o.a, 'hi');
});

if ('freeze' in Object && (function () {
Expand Down

0 comments on commit 3b924bc

Please sign in to comment.