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