From 78beadb748a0bc7f533c8899dd872eaa75b893c5 Mon Sep 17 00:00:00 2001 From: Joseph Caburnay Date: Fri, 13 Mar 2020 08:08:32 +0000 Subject: [REV] web: allow patching getters This reverts commit d6b854306cae245343a7acb9edc4178d245a49fe. The reverted commit introduced issues in patching class methods. Introducing getters in the patch function is not actually compatible with the original method patching. Test for patch method is also introduced to guard patch from future invalid modification that happened. A more robust way of patching classes is now being developed. closes odoo/odoo#47557 Signed-off-by: Aaron Bohy (aab) --- addons/web/static/src/js/core/utils.js | 52 ++++++---------------------------- 1 file changed, 8 insertions(+), 44 deletions(-) (limited to 'addons/web/static/src/js/core/utils.js') diff --git a/addons/web/static/src/js/core/utils.js b/addons/web/static/src/js/core/utils.js index 9bc1a287512..243f3f51cd0 100644 --- a/addons/web/static/src/js/core/utils.js +++ b/addons/web/static/src/js/core/utils.js @@ -520,10 +520,6 @@ var utils = { * Patch a class and return a function that remove the patch * when called. * - * In the patch object, super methods and getters are accessible thru: - * - this._super() - for method - * - this._super - for getter - * * @param {Class} C Class to patch * @param {string} patchName * @param {Object} patch @@ -535,8 +531,7 @@ var utils = { metadata = { origMethods: {}, patches: {}, - current: [], - originalDescriptors: {}, + current: [] }; patchMap.set(C.prototype, metadata); } @@ -549,41 +544,20 @@ var utils = { metadata.current.push(patchName); function applyPatch(proto, patch) { - Object.keys(patch).forEach(function(property) { - const protoDescriptor = Object.getOwnPropertyDescriptor(proto, property); - const patchDescriptor = Object.getOwnPropertyDescriptor(patch, property); - if (!protoDescriptor && patchDescriptor) { - if (!(property in metadata.originalDescriptors)) { - metadata.originalDescriptors[property] = undefined; - } - Object.defineProperty(proto, property, patchDescriptor); - return; - } - if (protoDescriptor.value) { - const method = patch[property]; - if (typeof method !== 'function') return; - const original = proto[property]; - if (!(property in metadata.origMethods)) { - metadata.origMethods[property] = original; + Object.keys(patch).forEach(function (methodName) { + const method = patch[methodName]; + if (typeof method === "function") { + const original = proto[methodName]; + if (!(methodName in metadata.origMethods)) { + metadata.origMethods[methodName] = original; } - proto[property] = function(...args) { + proto[methodName] = function (...args) { const previousSuper = this._super; this._super = original; const res = method.call(this, ...args); this._super = previousSuper; return res; }; - } else if (protoDescriptor.get) { - if (!(property in metadata.originalDescriptors)) { - metadata.originalDescriptors[property] = protoDescriptor; - } - const patchDescriptor = Object.getOwnPropertyDescriptor(patch, property); - Object.defineProperty(proto, property, { - get() { - this._super = protoDescriptor.get.apply(this); - return patchDescriptor.get.apply(this); - }, - }); } }); } @@ -761,16 +735,6 @@ var utils = { proto[k] = metadata.origMethods[k]; } - // reset the descriptors - for (let k in metadata.originalDescriptors) { - const descriptor = metadata.originalDescriptors[k]; - if (descriptor === undefined) { - delete proto[k]; - } else { - Object.defineProperty(proto, k, descriptor); - } - } - // apply other patches for (let name of metadata.current) { if (name !== patchName) { -- cgit v1.2.3