| OLD | NEW | 
 | (Empty) | 
|   1 /* This Source Code Form is subject to the terms of the Mozilla Public |  | 
|   2  * License, v. 2.0. If a copy of the MPL was not distributed with this |  | 
|   3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |  | 
|   4  |  | 
|   5 function hook(obj, name, func, cleanup) |  | 
|   6 { |  | 
|   7   let orig = obj[name]; |  | 
|   8   let origDesc = Object.getOwnPropertyDescriptor(obj, name); |  | 
|   9   let dumbOverrideAttempt = false; |  | 
|  10  |  | 
|  11   let newFunc = function() |  | 
|  12   { |  | 
|  13     let params = arguments; |  | 
|  14     try |  | 
|  15     { |  | 
|  16       let result = func.apply(this, params); |  | 
|  17       if (typeof result == "object") |  | 
|  18         params = result; |  | 
|  19     } |  | 
|  20     catch(e) |  | 
|  21     { |  | 
|  22       Cu.reportError(e); |  | 
|  23     } |  | 
|  24  |  | 
|  25     try |  | 
|  26     { |  | 
|  27       return orig.apply(this, params); |  | 
|  28     } |  | 
|  29     finally |  | 
|  30     { |  | 
|  31       if (typeof cleanup == "function") |  | 
|  32         cleanup(); |  | 
|  33     } |  | 
|  34   }; |  | 
|  35   newFunc.toString = function() |  | 
|  36   { |  | 
|  37     dumbOverrideAttempt = true; |  | 
|  38     return orig.toString(); |  | 
|  39   }; |  | 
|  40   newFunc.toSource = function() |  | 
|  41   { |  | 
|  42     dumbOverrideAttempt = true; |  | 
|  43     return orig.toSource(); |  | 
|  44   }; |  | 
|  45  |  | 
|  46   Object.defineProperty(obj, name, { |  | 
|  47     get: function() |  | 
|  48     { |  | 
|  49       dumbOverrideAttempt = false; |  | 
|  50       return newFunc; |  | 
|  51     }, |  | 
|  52     set: function(value) |  | 
|  53     { |  | 
|  54       if (dumbOverrideAttempt) |  | 
|  55       { |  | 
|  56         orig = value; |  | 
|  57       } |  | 
|  58       else |  | 
|  59       { |  | 
|  60         delete obj[name]; |  | 
|  61         obj[name] = value; |  | 
|  62       } |  | 
|  63     }, |  | 
|  64     enumerable: true, |  | 
|  65     configurable: true |  | 
|  66   }); |  | 
|  67  |  | 
|  68   return function() |  | 
|  69   { |  | 
|  70     Object.defineProperty(obj, name, origDesc); |  | 
|  71   }; |  | 
|  72 } |  | 
|  73 exports.hook = hook; |  | 
| OLD | NEW |