Skip to content

Commit

Permalink
[Fix] Object.defineProperty: when shimmed in Chrome <= 36, properly…
Browse files Browse the repository at this point in the history
… handle writability

See babel/babel#14056 (comment)
  • Loading branch information
ljharb committed Dec 21, 2021
1 parent 78af7d8 commit 2677bb6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
15 changes: 13 additions & 2 deletions es5-shim.js
Expand Up @@ -135,11 +135,22 @@
$Object.defineProperty(F, 'prototype', { value: sentinel, writable: false });
if ((new F()).toString !== toStringSentinel) {
var $dP = $Object.defineProperty;
var $gOPD = $Object.getOwnPropertyDescriptor;
defineProperties($Object, {
defineProperty: function defineProperty(o, k, d) {
var key = $String(k);
if (key === 'prototype' && 'writable' in d && 'value' in d) {
o[key] = d.value; // eslint-disable-line no-param-reassign
if (typeof o === 'function' && key === 'prototype') {
var desc = $gOPD(o, key);
if (desc.writable && !d.writable && 'value' in d) {
try {
o[key] = d.value; // eslint-disable-line no-param-reassign
} catch (e) { /**/ }
}
return $dP(o, key, {
configurable: 'configurable' in d ? d.configurable : desc.configurable,
enumerable: 'enumerable' in d ? d.enumerable : desc.enumerable,
writable: d.writable
});
}
return $dP(o, key, d);
}
Expand Down
21 changes: 20 additions & 1 deletion tests/spec/s-object.js
Expand Up @@ -12,6 +12,7 @@ var supportsDescriptors = Object.defineProperty && (function () {
return false;
}
}());
var ifSupportsDescriptorsIt = supportsDescriptors ? it : xit;
var ifWindowIt = typeof window === 'undefined' ? xit : it;
var extensionsPreventible = typeof Object.preventExtensions === 'function' && (function () {
var obj = {};
Expand Down Expand Up @@ -248,7 +249,7 @@ describe('Object', function () {
}).not.toThrow();
});

(supportsDescriptors ? it : xit)('allows setting a nonwritable prototype', function () {
ifSupportsDescriptorsIt('allows setting a nonwritable prototype', function () {
var F = function () {};
expect(F.prototype).toEqual(Object.getOwnPropertyDescriptor(F, 'prototype').value);
expect((new F()).toString).toEqual(Object.prototype.toString);
Expand All @@ -264,6 +265,24 @@ describe('Object', function () {
expect(F.prototype).toEqual(Object.getOwnPropertyDescriptor(F, 'prototype').value);
expect((new F()).toString).toEqual(toStringSentinel);
});

ifSupportsDescriptorsIt('properly makes a prototype non-writable', function () {
var O = { prototype: 1 };
expect(O.prototype).toEqual(Object.getOwnPropertyDescriptor(O, 'prototype').value);
expect(O.prototype).toEqual(1);

expect(function () {
Object.defineProperty(O, 'prototype', { writable: false, configurable: true });
}).not.toThrow();

var sentinel = {};
expect(function () {
Object.defineProperty(O, 'prototype', { value: sentinel });
}).not.toThrow();

expect(O.prototype).toEqual(Object.getOwnPropertyDescriptor(O, 'prototype').value);
expect(O.prototype).toEqual(sentinel);
});
});

describe('.getOwnPropertyDescriptor()', function () {
Expand Down

0 comments on commit 2677bb6

Please sign in to comment.