为什么Opera和Safari中的某些形式的间接评估失败? [英] Why do some forms of indirect eval fail in Opera and Safari?

查看:92
本文介绍了为什么Opera和Safari中的某些形式的间接评估失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些形式的间接eval在Opera中会失败,并且有人告诉我在Safari中会失败.

[eval][0]('')

未处理的错误:调用了与此对象无效的eval

请注意,它在评估一个空字符串时会失败...据我所知,它应该返回undefined.

其他形式,例如(0,eval)(''),似乎可以正常工作.

运行kangax的测试套件中的示例,我看到了在Opera中会以无效此对象"消息失败的几种形式出现,但在Chrome或Firefox中不会出现.

谁能解释为什么会这样?是什么决定了这种行为?似乎是故意的,它符合我不知道的标准的某些部分吗?

相关-关于间接评估与直接评估的 SO讨论. /p>

也-为什么是(0 || eval)在Opera中不被视为是间接的?

解决方案

这仅仅是因为ECMAScript 262第三版为异常的eval()调用指定了一个有点奇怪的可选异常.请参阅规范本部分的最后部分: http://bclary.com/2004/11/07/#a-15.1 .2.1

如果eval属性的值以非直接调用的方式(即,不通过将其名称明确用作标识符(即CallExpression中的MemberExpression)的方式)使用,或者分配了eval属性,可能会引发EvalError异常.

我不确定这句话背后的原因是什么,但是如您所见,如果在某个对象上定义了eval的引用并从那里调用,则Opera的Carakan引擎和Safari的ES引擎都选择实现异常.

ECMAScript标准的更高版本已删除此功能(首先在规范中具有可选例外是一个非常糟糕的主意),Opera将在将来的Carakan版本中进行调整.由于存在StackOverflow上的这些问题,该问题已在内部得到修复;)

Some forms of indirect eval fail in Opera, and, I'm told, in Safari.

[eval][0]('')

Unhandled Error: eval called with invalid this object

Notice that it fails when evaluating an empty string... as far as I can tell it should return undefined.

Other forms, like (0,eval)(''), seem to work fine.

Running the examples from this test suite by kangax, I see several forms that fail with the "invalid this object" message in Opera, but not Chrome or Firefox.

Can anyone explain why this happens? What dictates this behavior? It seems intentional, does it adhere to some part of the standard I don't know about?

Related -- SO discussion about indirect vs. direct eval.

Also -- Why is (0 || eval) not treated as indirect in Opera?

解决方案

This is simply because ECMAScript 262 3rd edition specified a somewhat odd optional exception for unusual eval() calls. See the last part of this section of the specification: http://bclary.com/2004/11/07/#a-15.1.2.1

If value of the eval property is used in any way other than a direct call (that is, other than by the explicit use of its name as an Identifier which is the MemberExpression in a CallExpression), or if the eval property is assigned to, an EvalError exception may be thrown.

I'm not entirely sure what the reasoning behind this sentence is, but as you see both Opera's Carakan engine and Safari's ES engine have chosen to implement an exception if a reference to eval is defined on some object and called from there.

Later editions of the ECMAScript standard have removed this (having an optional exception in a spec is a very bad idea in the first place) and Opera will be adapting in some future Carakan-version. It's already been fixed internally due to these questions on StackOverflow ;)

这篇关于为什么Opera和Safari中的某些形式的间接评估失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆