计算对 eval 的调用次数

发布于 2024-08-12 14:26:11 字数 441 浏览 2 评论 0原文

我想计算我们的 javascript 应用程序中对 eval 的调用次数。

我想出了以下内容,但它会产生错误。这些错误很难追踪,而且我对应用程序的了解有限。

你能告诉我我的代码有什么问题吗?

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);
}

或者您知道计算 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没有你我更好 2024-08-19 14:26:11

不幸的是,没有一种无懈可击的方法来模拟/包装 eval,因为它不是一个正常的函数。它可以访问(读和写)直接调用者的局部变量。

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

这是你无法通过原生 JavaScript 函数模拟的魔力。您无法从 myWrappedEval 获取调用者中的 a 值或写回更改后的值。虽然您可以使用 arguments.caller 获取调用者函数,但无法读取函数调用的局部变量。

您必须在源代码中找到的每次使用 eval 之前手动插入对 increment 的调用。或者,更好的做法是,删除对 eval 的所有调用,并用更好的代码替换它,使调用总数为零。 ;-)

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. ;-)

梦过后 2024-08-19 14:26:11

我想出了一个基本的解决方案。它似乎有效,至少我可以收集一些数据,但我不能 100% 确定它是正确的。

所以我只是这样做:

eval = eval;

这样,firebug 的分析器就能够计算对 eval 的调用,但不会对计数进行分组,因此我必须复制并粘贴到Excel,并构建一个数据透视表才能获取数据。

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.

独守阴晴ぅ圆缺 2024-08-19 14:26:11

唯一跳出来的是你的参数名称“string”

尝试将其更改为“str”或其他名称。有些浏览器可能不喜欢“String”,因为它实际上是一个类名(尤其是有时经常忽略大小写的 IE)

注意我测试了您的代码,它可以在 Firefox 3.5(安装了 Firebug 插件)中工作

如果您不这样做定义了 console 对象,或者在所述控制台对象上定义了 log 方法 - 您将收到错误。 (例如,如果未安装 Firebug 或类似软件)

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)

滿滿的愛 2024-08-19 14:26:11

尝试这个修改后的版本

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);
}
  • string重命名为str
  • 使用警报而不是console.log(并非在所有浏览器中都可用)
  • 更改了对警报的调用使用字符串连接
  • 将新的 eval 函数更改为匿名(删除了 eval 名称)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文