使用 nsIScriptContext 或 SpiderMonkey API 在 XULRunner 中查找并报告 Javascript 编译错误

发布于 2024-10-01 01:59:45 字数 1165 浏览 1 评论 0原文

我有一个 wxWebConnect 的实验版本(在 wxWidgets 中嵌入 XULRunner),位于 https://github.com/jonmmorgan/wxwebconnect/ 。它提供了一个方法

wxString wxWebControl::ExecuteScriptWithResult(const wxString& js_code)

,使用 nsIScriptContext::EvaluateStringWithValue 执行 Javascript 字符串,使用 JS_ValueToString 将值转换为字符串,然后返回该字符串。

但是,我发现在当前的实现中,如果 Javascript 字符串在语法上无效(例如 WebControl.ExecuteScriptWithResult('{a:2')),则在尝试将结果转换为字符串时程序会崩溃。我发现了 aIsUndefined 参数,我可以用它来确定脚本执行是否成功并防止崩溃。然而,这并不能帮助我找到实际的错误是什么,我认为最好报告一下(就像 Firebug 告诉我“SyntaxError:复合语句中缺少 }”)。据我所知,即使使用 nsIScriptContext::CompileScript 之类的东西也只会告诉我编译是否成功,并且不会给我提供获取实际语法错误的方法。

遵循 http://chadaustin.me/2009/02 /evaluating-javascript-in-an-embedded-xulrunnergecko-window/,我尝试使用 JS_GetPendingException,但它似乎永远不会返回 true。即使当我运行 webcontrol.ExecuteScriptWithResult("throw new Error('a');") 它仍然没有挂起的异常,但确实说结果未定义。

还有其他好方法可以从 nsIScriptContext 或 SpiderMonkey API 获取编译或执行错误吗?知道为什么 JS_GetPendingException 似乎没有这样做吗?

I have an experimental version of wxWebConnect (embedding XULRunner in wxWidgets) at https://github.com/jonmmorgan/wxwebconnect/. It offers a method

wxString wxWebControl::ExecuteScriptWithResult(const wxString& js_code)

which executes the Javascript string using nsIScriptContext::EvaluateStringWithValue, converts the value to a string with JS_ValueToString, and then returns the string.

However, I found with the current implementation that if the Javascript string is syntactically invalid (e.g. WebControl.ExecuteScriptWithResult('{a:2')) then the program would crash when trying to convert the result to a string. I discovered the aIsUndefined parameter, which I can use to determine whether script execution succeeded or not and prevent the crash. However, this doesn't help me find what the actual error was, and I think it would be good to report that (like Firebug tells me "SyntaxError: missing } in compound statement"). As far as I can see, even using something like nsIScriptContext::CompileScript will just tell me whether the compilation succeeded or not, and doesn't give me a way to get at the actual syntax error.

Following http://chadaustin.me/2009/02/evaluating-javascript-in-an-embedded-xulrunnergecko-window/, I tried using JS_GetPendingException, but it never seems to return true. Even when I run webcontrol.ExecuteScriptWithResult("throw new Error('a');") it still doesn't have a pending exception, but does say the result is undefined.

Is there any other good way to get a compilation or execution error from nsIScriptContext or the SpiderMonkey API? Any idea why JS_GetPendingException doesn't seem to do this?

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

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

发布评论

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

评论(1

想你的星星会说话 2024-10-08 01:59:45

我实际上找到了这个问题的答案:
在 nsJSContext 中,它注册了一个错误报告函数。作为错误报告器功能的一部分,它清除未决的异常。如果您注册自己的错误报告器,它会覆盖该错误报告器并允许您实际对错误执行某些操作。这确实意味着您的错误不会出现在错误控制台中。我不知道您是否会因覆盖内置错误处理程序而丢失任何其他重要功能。

I have actually found an answer to this:
In nsJSContext, it registers an error reporter function. As part of that error reporter function, it clears pending exceptions. If you register your own error reporter it overrides that and allows you to actually do something with the error. This does mean that your error won't turn up in the Error Console. I don't know if you lose any other important functionality by overriding the builtin error handler.

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