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 | 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/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
5 function hook(obj, name, func, cleanup) | 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 origDesc = Object.getOwnPropertyDescriptor(obj, name); |
9 let origSet = obj.__lookupSetter__(name); | |
10 let dumbOverrideAttempt = false; | 9 let dumbOverrideAttempt = false; |
11 | 10 |
12 let newFunc = function() | 11 let newFunc = function() |
13 { | 12 { |
14 let params = arguments; | 13 let params = arguments; |
15 try | 14 try |
16 { | 15 { |
17 let result = func.apply(this, params); | 16 let result = func.apply(this, params); |
18 if (typeof result == "object") | 17 if (typeof result == "object") |
19 params = result; | 18 params = result; |
(...skipping 15 matching lines...) Expand all Loading... |
35 }; | 34 }; |
36 newFunc.toString = function() | 35 newFunc.toString = function() |
37 { | 36 { |
38 dumbOverrideAttempt = true; | 37 dumbOverrideAttempt = true; |
39 return orig.toString(); | 38 return orig.toString(); |
40 }; | 39 }; |
41 newFunc.toSource = function() | 40 newFunc.toSource = function() |
42 { | 41 { |
43 dumbOverrideAttempt = true; | 42 dumbOverrideAttempt = true; |
44 return orig.toSource(); | 43 return orig.toSource(); |
45 } | 44 }; |
46 | 45 |
47 obj.__defineGetter__(name, function() | 46 Object.defineProperty(obj, name, { |
48 { | 47 get: function() |
49 dumbOverrideAttempt = false; | |
50 return newFunc; | |
51 }); | |
52 | |
53 obj.__defineSetter__(name, function(value) | |
54 { | |
55 if (dumbOverrideAttempt) | |
56 { | 48 { |
57 orig = value; | 49 dumbOverrideAttempt = false; |
58 } | 50 return newFunc; |
59 else | 51 }, |
| 52 set: function(value) |
60 { | 53 { |
61 delete obj[name]; | 54 if (dumbOverrideAttempt) |
62 obj[name] = value; | 55 { |
63 } | 56 orig = value; |
| 57 } |
| 58 else |
| 59 { |
| 60 delete obj[name]; |
| 61 obj[name] = value; |
| 62 } |
| 63 }, |
| 64 enumerable: true, |
| 65 configurable: true |
64 }); | 66 }); |
65 | 67 |
66 return function() | 68 return function() |
67 { | 69 { |
68 delete obj[name]; | 70 Object.defineProperty(obj, name, origDesc); |
69 obj[name] = orig; | |
70 if (origGet) | |
71 { | |
72 obj.__defineGetter__(name, origGet); | |
73 } | |
74 if (origSet) | |
75 { | |
76 obj.__defineSetter__(name, origSet); | |
77 } | |
78 }; | 71 }; |
79 } | 72 } |
80 exports.hook = hook; | 73 exports.hook = hook; |
OLD | NEW |