1 // Copyright (c) 2011 Google, Inc.
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * @fileoverview A general purpose object proxy that logs all method calls.
24 * @author konigsberg@google.com (Robert Konigsberg)
27 var Proxy
= function(delegate
) {
28 this.delegate__
= delegate
;
30 this.propertiesToTrack__
= [];
32 for (var propname
in delegate
) {
33 var type
= typeof(delegate
[propname
]);
35 // Functions are passed through to the delegate, and are logged
37 if (type
== "function") {
38 function makeFunc(name
) {
40 this.log__(name
, arguments
);
41 this.delegate__
[name
].apply(this.delegate__
, arguments
);
44 this[propname
] = makeFunc(propname
);
45 } else if (type
== "string" || type
== "number") {
46 // String and number properties are just passed through to the delegate.
47 this.propertiesToTrack__
.push(propname
);
48 function makeSetter(name
) {
50 this.delegate__
[name
] = x
;
53 this.__defineSetter__(propname
, makeSetter(propname
));
55 function makeGetter(name
) {
57 return this.delegate__
[name
];
60 this.__defineGetter__(propname
, makeGetter(propname
));
65 Proxy
.prototype.log__
= function(name
, args
) {
67 for (var propIdx
in this.propertiesToTrack__
) {
68 var prop
= this.propertiesToTrack__
[propIdx
];
69 properties
[prop
] = this.delegate__
[prop
];
71 var call
= { name
: name
, args
: args
, properties
: properties
};
72 this.calls__
.push(call
);