使用try / catch进行eval抛出错误

时间:2011-11-29 05:46:36

标签: javascript actionscript-3 actionscript air eval

我有一个空中应用程序,用户在textarea中键入javascript,并在mx:HTML组件中进行评估,但即使在eval周围使用try / catch,也在eval中的代码周围,和一个HTMLUncaughtScriptExceptionEvent处理程序,它仍然会抛出一个错误。 htmlWindow是html.htmlLoader.window

try { htmlWindow.eval("try {" + script.text + "} catch (error:Error) { trace(error) }); } catch (error:Error) { trace(error) }

我在script中输入

中的文字后,该行上的应用程序错误
ReferenceError: Can't find variable: d
    at Main/reloadHTML()[C:\Users\Christian\Adobe Flash Builder 4.5\JavaScript plus Scratch\src\Main.mxml:264]
    at Main/__script_change()[C:\Users\Christian\Adobe Flash Builder 4.5\JavaScript plus Scratch\src\Main.mxml:324]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.core::UIComponent/dispatchEvent()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:13128]
    at spark.components.supportClasses::SkinnableTextBase/textDisplay_changeHandler()[E:\dev\4.5.1\frameworks\projects\spark\src\spark\components\supportClasses\SkinnableTextBase.as:2265]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.core::UIComponent/dispatchEvent()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:13128]
    at spark.components::RichEditableText/textContainerManager_flowOperationCompleteHandler()[E:\dev\4.5.1\frameworks\projects\spark\src\spark\components\RichEditableText.as:4808]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flashx.textLayout.container::TextContainerManager/dispatchEvent()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\container\TextContainerManager.as:1553]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flashx.textLayout.elements::TextFlow/dispatchEvent()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\elements\TextFlow.as:859]
    at flashx.textLayout.edit::EditManager/finalizeDo()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\edit\EditManager.as:669]
    at flashx.textLayout.edit::EditManager/doOperation()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\edit\EditManager.as:613]
    at flashx.textLayout.edit::EditManager/flushPendingOperations()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\edit\EditManager.as:873]
    at flashx.textLayout.edit::SelectionManager/enterFrameHandler()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\edit\SelectionManager.as:1859]`

有什么方法可以阻止错误?我尝试了script.change="html.htmlText = '<script>' + script.text + '</script'>",但我有htmlWindow.log = log; htmlWindow.rotateSprite = rotateSprite;,如果我在更改之前复制粘贴log('test'),它可以正常工作,如果我之前或之后更改它,它永远不会再有效,所以我希望让这个eval工作。

1 个答案:

答案 0 :(得分:0)

我不确定您是否可以将此代码100%免除异常。用户可以编写完全无效的代码,如zlçrza ;à"çé°$,这些代码不会引发异常,只会使JS编译器崩溃。你能解释为什么你让用户直接输入javascript代码吗?也许你需要以其他方式解决这个问题?