English 中文(简体)
Count calls to eval
原标题:

I would like to count the number of calls made to eval in our javascript application.

I came up with the following, but it generates errors. These errors are hard to track, and my knowledge of the app is limited.

Can you tell what is wrong with my code ?

increment = function (){
  var me = arguments.callee;
  if (!me.count) me.count = 0;
  return ++me.count;
}

var oldEval = eval;

eval = function eval(string){       
  console.log( eval number  , increment());
  return oldEval(string);
}

Or to you know an alternative way to count the use of eval ?

thanks

Olivier

最佳回答

I came up with a rudimentary solution. It seems to work, at least I can gather some data, but I"m not 100% sure it is correct.

so I just do :

eval = eval;

With that, firebug s profiler is able to count the calls to eval, but does not group that counting, therefore I have to copy and paste to excel, and build a pivot Table in order to get the data.

问题回答

Unfortunately there is not a watertight way of emulating/wrapping eval, because it isn t a normal function. It can access (both read and write) the local variables of the direct caller.

function testeval() {
    var a= 2;
    eval( a*= 2 );
    alert(a); // 4
};

This is magic you can t emulate from a native JavaScript function. You d have no way from myWrappedEval to get the value of a in the caller or write back the changed value. Whilst you can get hold of the caller function using arguments.caller, there s no way to read the locals of the function invocation.

You ll have to manually insert a call to increment just before each usage of eval you can find in the source. Or, better, remove every call to eval and replace it with better code, making the call total zero. ;-)

The only thing that jumps out is your argument name "string"

Try changing it to "str" or something. Some browsers might not like "String" since it is actually a Class name (esp. IE that often ignores case sensitivity sometimes)

Note I tested your code, and it works in Firefox 3.5 (with the Firebug addon installed)

If you don t have a console object defined, or a log method on said console object - you will get errors. (e.g. if Firebug or similar is not installed)

Try this modified version

var increment = function () {
  var me = arguments.callee;
  if (!me.count) me.count = 0;
  return ++me.count;
}

var oldEval = eval;

eval = function (str) {
  alert( eval number   + increment());
  return oldEval(str);
}
  • Renamed string to str
  • used alert instead of console.log (which isn t available in all browser)
  • Changed call to alert to use string concatenation
  • changed new eval function to be anonymous (removed eval name)




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签