diff --git a/CefSharp.Test/BrowserTest.cs b/CefSharp.Test/BrowserTest.cs index 8d93803ad..ff797490a 100644 --- a/CefSharp.Test/BrowserTest.cs +++ b/CefSharp.Test/BrowserTest.cs @@ -7,16 +7,18 @@ namespace CefSharp.Test [TestFixture] public class BrowserTest { - [Test] - public void FooTest() + [TestCase("'2'", "2")] + [TestCase("2+2", 4)] + public void EvaluateScriptTest(string script, object result) { - Assert.Pass(); + Assert.AreEqual(result, Fixture.Browser.EvaluateScript(script)); } - [Test] - public void RunScriptText() + [TestCase("!!!")] + public void EvaluateScriptExceptionTest(string script) { - Assert.AreEqual("4", Fixture.Browser.EvaluateScript("2+2")); + Assert.Throws(() => + Fixture.Browser.EvaluateScript(script)); } [Test] diff --git a/CefSharp.WinForms/WebView.cpp b/CefSharp.WinForms/WebView.cpp index 707093781..0cc31667f 100644 --- a/CefSharp.WinForms/WebView.cpp +++ b/CefSharp.WinForms/WebView.cpp @@ -127,20 +127,19 @@ namespace WinForms _scriptCore->Execute(frame, toNative(script)); } - String^ WebView::EvaluateScript(String^ script) + Object^ WebView::EvaluateScript(String^ script) { return EvaluateScript(script, TimeSpan::MaxValue); } - String^ WebView::EvaluateScript(String^ script, TimeSpan timeout) + Object^ WebView::EvaluateScript(String^ script, TimeSpan timeout) { WaitForInitialized(); CefRefPtr browser = _clientAdapter->GetCefBrowser(); CefRefPtr frame = browser->GetMainFrame(); - CefString result = _scriptCore->Evaluate(frame, toNative(script), timeout.TotalMilliseconds); - return toClr(result); + return _scriptCore->Evaluate(frame, toNative(script), timeout.TotalMilliseconds); } void WebView::SetNavState(bool isLoading, bool canGoBack, bool canGoForward) diff --git a/CefSharp.WinForms/WebView.h b/CefSharp.WinForms/WebView.h index 6fcbd5890..ea8c249a7 100644 --- a/CefSharp.WinForms/WebView.h +++ b/CefSharp.WinForms/WebView.h @@ -154,8 +154,8 @@ namespace WinForms virtual void Print(); void ExecuteScript(String^ script); - String^ EvaluateScript(String^ script); - String^ EvaluateScript(String^ script, TimeSpan timeout); + Object^ EvaluateScript(String^ script); + Object^ EvaluateScript(String^ script, TimeSpan timeout); virtual void SetNavState(bool isLoading, bool canGoBack, bool canGoForward); diff --git a/CefSharp.Wpf/WebView.cpp b/CefSharp.Wpf/WebView.cpp index eb04176d3..70859a6ff 100644 --- a/CefSharp.Wpf/WebView.cpp +++ b/CefSharp.Wpf/WebView.cpp @@ -224,20 +224,19 @@ namespace Wpf _scriptCore->Execute(frame, toNative(script)); } - String^ WebView::EvaluateScript(String^ script) + Object^ WebView::EvaluateScript(String^ script) { return EvaluateScript(script, TimeSpan::MaxValue); } - String^ WebView::EvaluateScript(String^ script, TimeSpan timeout) + Object^ WebView::EvaluateScript(String^ script, TimeSpan timeout) { WaitForInitialized(); CefRefPtr browser = _clientAdapter->GetCefBrowser(); CefRefPtr frame = browser->GetMainFrame(); - CefString result = _scriptCore->Evaluate(frame, toNative(script), timeout.TotalMilliseconds); - return toClr(result); + return _scriptCore->Evaluate(frame, toNative(script), timeout.TotalMilliseconds); } void WebView::SetNavState(bool isLoading, bool canGoBack, bool canGoForward) diff --git a/CefSharp.Wpf/WebView.h b/CefSharp.Wpf/WebView.h index 1d42f7c0e..5ffdc5efd 100644 --- a/CefSharp.Wpf/WebView.h +++ b/CefSharp.Wpf/WebView.h @@ -169,8 +169,8 @@ namespace Wpf virtual void Print(); void ExecuteScript(String^ script); - String^ EvaluateScript(String^ script); - String^ EvaluateScript(String^ script, TimeSpan timeout); + Object^ EvaluateScript(String^ script); + Object^ EvaluateScript(String^ script, TimeSpan timeout); virtual void SetNavState(bool isLoading, bool canGoBack, bool canGoForward); diff --git a/CefSharp/SchemeHandler.h b/CefSharp/SchemeHandler.h index c841c0104..6dab319fb 100644 --- a/CefSharp/SchemeHandler.h +++ b/CefSharp/SchemeHandler.h @@ -6,7 +6,8 @@ using namespace System; using namespace System::IO; -namespace CefSharp { +namespace CefSharp +{ public interface class ISchemeHandler { /// @@ -27,7 +28,6 @@ namespace CefSharp { gcroot _stream; CefString _mime_type; - int SizeFromStream(); public: diff --git a/CefSharp/ScriptCore.cpp b/CefSharp/ScriptCore.cpp index 945ad7031..9a2fd672b 100644 --- a/CefSharp/ScriptCore.cpp +++ b/CefSharp/ScriptCore.cpp @@ -20,24 +20,30 @@ namespace CefSharp CefRefPtr global = context->GetGlobal(); CefRefPtr eval = global->GetValue("eval"); CefRefPtr arg = CefV8Value::CreateString(script); + CefRefPtr result; + CefRefPtr exception; CefV8ValueList args; args.push_back(arg); - CefRefPtr result; - CefRefPtr exception; - - if (eval->ExecuteFunctionWithContext(context, global, args, result, exception, false)) + if (eval->ExecuteFunctionWithContext(context, global, args, + result, exception, false)) { - if (exception.get()) + if (exception) { - _success = false; - _result = exception->GetMessage(); + CefString message = exception->GetMessage(); + _exception = gcnew ScriptException(toClr(message)); } - else if (result.get()) + else { - _success = true; - _result = result->GetStringValue(); + try + { + _result = convertFromCef(result); + } + catch (...) + { + + } } } @@ -60,7 +66,7 @@ namespace CefSharp } } - CefString ScriptCore::Evaluate(CefRefPtr frame, CefString script, double timeout) + gcroot ScriptCore::Evaluate(CefRefPtr frame, CefString script, double timeout) { AutoLock lock_scope(this); @@ -79,9 +85,9 @@ namespace CefSharp } } - if (!_success) + if (_exception) { - throw gcnew ScriptException(toClr(_result)); + throw _exception; } else { diff --git a/CefSharp/ScriptCore.h b/CefSharp/ScriptCore.h index 5c2cb37d1..10c281bd7 100644 --- a/CefSharp/ScriptCore.h +++ b/CefSharp/ScriptCore.h @@ -8,8 +8,8 @@ namespace CefSharp private: HANDLE _event; - bool _success; - CefString _result; + gcroot _result; + gcroot _exception; void UIT_Execute(CefRefPtr frame, CefString script); void UIT_Evaluate(CefRefPtr frame, CefString script); @@ -21,7 +21,7 @@ namespace CefSharp } DECL void Execute(CefRefPtr frame, CefString script); - DECL CefString Evaluate(CefRefPtr frame, CefString script, double timeout); + DECL gcroot Evaluate(CefRefPtr frame, CefString script, double timeout); IMPLEMENT_LOCKING(ScriptCore); IMPLEMENT_REFCOUNTING(ScriptCore);