cefsharp / CefSharp

.NET (WPF and Windows Forms) bindings for the Chromium Embedded Framework
http://cefsharp.github.io/
Other
9.82k stars 2.92k forks source link

RunScript: a script that runs cleanly but returns a null value causes an exception #36

Closed ataranto closed 13 years ago

ataranto commented 13 years ago

The v8task.v2.patch calls jsTask->HandleError() if result.IsEmpty() is true, raising a ScriptException. Calling someJavaScriptMethodThatReturnsNull() shouldn't raise an exception.

I'd like to just have CefWebBrowser.RunScript() return null if result.IsEmpty() is true.

chillitom commented 13 years ago

makes sense. want to provide a patch and test?

ataranto commented 13 years ago

coming up. what kind of test?

chillitom commented 13 years ago

if it's not a complete pain to set up then a couple of unit tests that RunScript behaves as expected for null/undefined and empty would be great

chillitom commented 13 years ago

The big problem here is how to handle the exception/error path, without this we can't report errors to users. Unfortunately CEF is a little lacking here; Marshall suggested trying to watch for errors in the js console but this seems like quite a hack and would break if errors were sent to the console from within code. You're completely right that empty should be allowed though for cases with no return value. It is a shame that null and undefined are distinct from no value. I'm not sure what to do about this.

ataranto commented 13 years ago

one crude solution would be to have separate api for running javascript that the caller expects to return a value and javascript that does not return a value.

RunScript(); RunScriptAndReturnObject();

i'm not a huge fan of this, though.

chillitom commented 13 years ago

We could petition (or better still patch) Webkit to include a method which returns both the result and any exception that occurred, perhaps even along with a stack trace, err line no etc. if that's possible.

This would be far superior for both the success and error code paths.

chillitom commented 13 years ago

n.b. this might not be feasible due to the several frames of WebFrameImpl between CEF and ScriptController

ataranto commented 13 years ago

datapoint: both cocoa's WebView control and QT's QWebView control have a method that executes javascript and may return null (or equivalent) for both an execution failure or success.