Skip to content

Commit 0bea1b9

Browse files
mattcolegatetobespc
authored andcommitted
Allow trace probe to instrument functions of any number of params (#620)
1 parent 19a9db6 commit 0bea1b9

File tree

1 file changed

+3
-69
lines changed

1 file changed

+3
-69
lines changed

probes/trace-probe.js

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -68,73 +68,6 @@ function instrument(target, name, method, fullName) {
6868
var lastMethodArg = methodargs[methodargs.length - 1].replace(/ /g, '');
6969
if (lastMethodArg == '') lastMethodArg = 'undefined';
7070

71-
function generateF(expectedArgCount, fn) {
72-
switch (expectedArgCount) {
73-
case 0:
74-
return function() {
75-
return fn.apply(this, arguments);
76-
};
77-
case 1:
78-
return function(a) {
79-
return fn.apply(this, arguments);
80-
};
81-
case 2:
82-
return function(a, b) {
83-
return fn.apply(this, arguments);
84-
};
85-
case 3:
86-
return function(a, b, c) {
87-
return fn.apply(this, arguments);
88-
};
89-
case 4:
90-
return function(a, b, c, d) {
91-
return fn.apply(this, arguments);
92-
};
93-
case 5:
94-
return function(a, b, c, d, e) {
95-
return fn.apply(this, arguments);
96-
};
97-
case 6:
98-
return function(a, b, c, d, e, f) {
99-
return fn.apply(this, arguments);
100-
};
101-
case 7:
102-
return function(a, b, c, d, e, f, g) {
103-
return fn.apply(this, arguments);
104-
};
105-
case 8:
106-
return function(a, b, c, d, e, f, g, h) {
107-
return fn.apply(this, arguments);
108-
};
109-
case 9:
110-
return function(a, b, c, d, e, f, g, h, i) {
111-
return fn.apply(this, arguments);
112-
};
113-
114-
// Slow case for functions with > 10 args
115-
default:
116-
var ident = 'a';
117-
var argumentList = [];
118-
for (var i = 0; i < expectedArgCount; i++) {
119-
argumentList[i] = ident;
120-
ident = incrementIdentifier(ident);
121-
}
122-
/* eslint no-eval: 0 */
123-
return eval('x = function(' + argumentList.join(',') + ') {return fn.apply(this,arguments);};');
124-
}
125-
126-
function incrementIdentifier(identifier) {
127-
var charArr = identifier.split('');
128-
var lastChar = charArr[charArr.length - 1];
129-
if (lastChar == 'z') {
130-
return identifier + 'a';
131-
} else {
132-
var chopped = identifier.substring(0, identifier.length - 1);
133-
return chopped + String.fromCharCode(lastChar.charCodeAt(0) + 1);
134-
}
135-
}
136-
}
137-
13871
var f = function() {
13972
var req = request.startMethod(fullName);
14073
var args = arguments;
@@ -216,8 +149,9 @@ function instrument(target, name, method, fullName) {
216149
return res;
217150
};
218151
// use a function replace to call our 'f' function.
219-
// we ned to use 'generateF' to call f with the correct number of arguments
220-
target[name] = generateF(method.length, f);
152+
target[name] = function() {
153+
return f.apply(this, arguments);
154+
};
221155
target[name].prototype = method.prototype;
222156
}
223157

0 commit comments

Comments
 (0)