Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/hooks.js

Issue 8382011: Applied changes from emailed code review (Closed)
Patch Set: Created Sept. 28, 2012, 11:09 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/appIntegration.js ('k') | lib/main.js » ('j') | lib/survey.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 file,
3 * 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 origGet = obj.__lookupGetter__(name);
9 let origSet = obj.__lookupSetter__(name);
10 let dumbOverrideAttempt = false;
11
12 let newFunc = function()
13 {
14 let params = arguments;
15 try
16 {
17 let result = func.apply(this, params);
18 if (typeof result == "object")
19 params = result;
20 }
21 catch(e)
22 {
23 Cu.reportError(e);
24 }
25
26 try
27 {
28 return orig.apply(this, params);
29 }
30 finally
31 {
32 if (typeof cleanup == "function")
33 cleanup();
34 }
35 };
36 newFunc.toString = function()
37 {
38 dumbOverrideAttempt = true;
39 return orig.toString();
40 };
41
42 obj.__defineGetter__(name, function()
43 {
44 dumbOverrideAttempt = false;
45 return newFunc;
46 });
47
48 obj.__defineSetter__(name, function(value)
49 {
50 if (dumbOverrideAttempt)
51 {
52 orig = value;
53 }
54 else
55 {
56 delete obj[name];
57 obj[name] = value;
58 }
59 });
60
61 return function()
62 {
63 delete obj[name];
64 obj[name] = orig;
65 if (origGet)
66 {
67 obj.__defineGetter__(name, origGet);
68 }
69 if (origSet)
70 {
71 obj.__defineSetter__(name, origSet);
72 }
73 };
74 }
75 exports.hook = hook;
OLDNEW
« no previous file with comments | « lib/appIntegration.js ('k') | lib/main.js » ('j') | lib/survey.js » ('J')

Powered by Google App Engine
This is Rietveld