diff --git a/CefSharp/ScriptCore.cpp b/CefSharp/ScriptCore.cpp index 0d48a3a04..899e109e0 100644 --- a/CefSharp/ScriptCore.cpp +++ b/CefSharp/ScriptCore.cpp @@ -5,12 +5,12 @@ namespace CefSharp { - bool TryGetMainFrame(CefRefPtr browser, CefRefPtr& mainFrame) + bool ScriptCore::TryGetMainFrame(CefRefPtr browser, CefRefPtr& frame) { if (browser != nullptr) { - mainFrame = browser->GetMainFrame(); - return mainFrame != nullptr; + frame = browser->GetMainFrame(); + return frame != nullptr; } else { @@ -30,47 +30,48 @@ namespace CefSharp void ScriptCore::UIT_Evaluate(CefRefPtr browser, CefString script) { CefRefPtr mainFrame; - if (!TryGetMainFrame(browser, mainFrame)) + if (TryGetMainFrame(browser, mainFrame)) { - return; - } + CefRefPtr context = mainFrame->GetV8Context(); - CefRefPtr context = mainFrame->GetV8Context(); - - if (context.get() && - context->Enter()) - { - CefRefPtr global = context->GetGlobal(); - CefRefPtr eval = global->GetValue("eval"); - CefRefPtr arg = CefV8Value::CreateString(script); - CefRefPtr result; - CefRefPtr exception; - - CefV8ValueList args; - args.push_back(arg); - - if (eval->ExecuteFunctionWithContext(context, global, args, - result, exception, false)) + if (context.get() && context->Enter()) { - if (exception) - { - CefString message = exception->GetMessage(); - _exception = toClr(message); - } - else - { - try - { - _result = convertFromCef(result); - } - catch (Exception^ ex) - { - _exception = ex->Message; - } - } - } + CefRefPtr global = context->GetGlobal(); + CefRefPtr eval = global->GetValue("eval"); + CefRefPtr arg = CefV8Value::CreateString(script); + CefRefPtr result; + CefRefPtr exception; - context->Exit(); + CefV8ValueList args; + args.push_back(arg); + + if (eval->ExecuteFunctionWithContext(context, global, args, + result, exception, false)) + { + if (exception) + { + CefString message = exception->GetMessage(); + _exceptionMessage = toClr(message); + } + else + { + try + { + _result = convertFromCef(result); + } + catch (Exception^ ex) + { + _exceptionMessage = ex->Message; + } + } + } + + context->Exit(); + } + } + else + { + _exceptionMessage = "Failed to obtain reference to main frame"; } SetEvent(_event); @@ -93,7 +94,7 @@ namespace CefSharp { AutoLock lock_scope(this); _result = nullptr; - _exception = nullptr; + _exceptionMessage = nullptr; if (CefCurrentlyOn(TID_UI)) { @@ -114,9 +115,9 @@ namespace CefSharp throw gcnew ScriptException("Script error"); } - if (_exception) + if (_exceptionMessage) { - throw gcnew ScriptException(_exception); + throw gcnew ScriptException(_exceptionMessage); } else { diff --git a/CefSharp/ScriptCore.h b/CefSharp/ScriptCore.h index d0b03a937..775aafaa4 100644 --- a/CefSharp/ScriptCore.h +++ b/CefSharp/ScriptCore.h @@ -9,8 +9,9 @@ namespace CefSharp HANDLE _event; gcroot _result; - gcroot _exception; + gcroot _exceptionMessage; + bool TryGetMainFrame(CefRefPtr browser, CefRefPtr& frame); void UIT_Execute(CefRefPtr browser, CefString script); void UIT_Evaluate(CefRefPtr browser, CefString script);