From 71d53988a352a1e98fbec0e87744d102afa8b3d0 Mon Sep 17 00:00:00 2001 From: "JiaLi.Passion" Date: Wed, 18 Oct 2017 00:25:27 +0900 Subject: [PATCH] fix(patch): fix #927, shoud not throw error when defineProperty with frozen desc --- lib/browser/define-property.ts | 12 +++++++++--- test/browser/define-property.spec.ts | 12 ++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/browser/define-property.ts b/lib/browser/define-property.ts index 22a286449..a1357f1d4 100644 --- a/lib/browser/define-property.ts +++ b/lib/browser/define-property.ts @@ -69,12 +69,18 @@ function isUnconfigurable(obj: any, prop: any) { } function rewriteDescriptor(obj: any, prop: string, desc: any) { - desc.configurable = true; + // issue-927, if the desc is frozen, don't try to change the desc + if (!Object.isFrozen(desc)) { + desc.configurable = true; + } if (!desc.configurable) { - if (!obj[unconfigurablesKey]) { + // issue-927, if the obj is frozen, don't try to set the desc to obj + if (!obj[unconfigurablesKey] && !Object.isFrozen(obj)) { _defineProperty(obj, unconfigurablesKey, {writable: true, value: {}}); } - obj[unconfigurablesKey][prop] = true; + if (obj[unconfigurablesKey]) { + obj[unconfigurablesKey][prop] = true; + } } return desc; } diff --git a/test/browser/define-property.spec.ts b/test/browser/define-property.spec.ts index ed6babd93..fb11387c8 100644 --- a/test/browser/define-property.spec.ts +++ b/test/browser/define-property.spec.ts @@ -14,4 +14,16 @@ describe('defineProperty', function() { .not.toThrow(); }); + it('should not throw error when try to defineProperty with a frozen desc', function() { + const obj = {}; + const desc = Object.freeze({value: null, writable: true}); + Object.defineProperty(obj, 'prop', desc); + }); + + it('should not throw error when try to defineProperty with a frozen obj', function() { + const obj = {}; + Object.freeze(obj); + Object.defineProperty(obj, 'prop', {configurable: true, writable: true, value: 'value'}); + }); + }); \ No newline at end of file