diff --git a/CefSharp.Example/BoundObject.cs b/CefSharp.Example/BoundObject.cs index 986e2a5cf..95b3d1153 100644 --- a/CefSharp.Example/BoundObject.cs +++ b/CefSharp.Example/BoundObject.cs @@ -14,6 +14,10 @@ namespace CefSharp.Example result += str; } return result; + } + + public void EchoVoid() + { } } } \ No newline at end of file diff --git a/CefSharp.Tests/BindObjectTests.cs b/CefSharp.Tests/BindObjectTests.cs index 0eaee5237..9e154f483 100644 --- a/CefSharp.Tests/BindObjectTests.cs +++ b/CefSharp.Tests/BindObjectTests.cs @@ -68,6 +68,45 @@ } } + [Test] + public void ScriptExceptionMustBeThrownTest() + { + Assert.Throws(() => + { + RunScript("return bound.__UnknownMethod();"); + }); + } + + [Test] + public void ScriptExceptionMustBeThrownStringTest() + { + try + { + RunScript("throw 'my js exception message';"); + } + catch (ScriptException ex) + { + Assert.AreEqual("my js exception message", ex.Message); + return; + } + Assert.Fail(); + } + + [Test] + public void ScriptExceptionMustBeThrownObjectTest() + { + try + { + RunScript("throw {'type':'exception type', 'message':'my js exception message'};"); + } + catch (ScriptException ex) + { + Assert.AreEqual("[object Object]", ex.Message); + return; + } + Assert.Fail(); + } + [Test] public void Int_Echo_Int_Test() { @@ -98,8 +137,8 @@ sb.Append('\"'); var testJsString = sb.ToString(); var testString = sbc.ToString(); - - var result2 = RunScript("return bound.EchoString("+testJsString+");"); + + var result2 = RunScript("return bound.EchoString(" + testJsString + ");"); Assert.AreEqual(testString, result2); } @@ -121,5 +160,119 @@ } #endregion + #region Object Members + [Test] + public void MethodIsMemberFunction() + { + var result = RunScript("return typeof(bound.EchoVoid);"); + Assert.AreEqual("function", result); + } + + [Test] + public void UndefinedMethodIsUndefined() + { + var result = RunScript("return bound.UndefinedMethod;"); + Assert.AreEqual("undefined", result); + } + #endregion + + #region JavaScript to CLR Type Conversion + + [Test] + [TestCase("VoidRetValIsUndefined", "bound.EchoVoid() === undefined", false)] + +/* +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| Boolean | *false | *false | + | - | - | - | - | - | +*/ + [TestCase("Boolean_Undefined", "bound.EchoBoolean(undefined) === false", false)] + [TestCase("Boolean_Null", "bound.EchoBoolean(null) === false", false)] + [TestCase("Boolean_Boolean_True", "bound.EchoBoolean(true) === true", false)] + [TestCase("Boolean_Boolean_False", "bound.EchoBoolean(false) === false", false)] + [TestCase("Boolean_Number_0", "bound.EchoBoolean(0) === false", true)] + [TestCase("Boolean_Number_1", "bound.EchoBoolean(1) === true", true)] + [TestCase("Boolean_String", "bound.EchoBoolean('0') === true", true)] + [TestCase("Boolean_Date", "bound.EchoBoolean(new Date()) === true", true)] + [TestCase("Boolean_Array", "bound.EchoBoolean([]) === true", true)] + [TestCase("Boolean_Object", "bound.EchoBoolean({}) === true", true)] + +/* +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| Boolean? | *null | *null | + | SAME | SAME | SAME | SAME | SAME | +*/ + [TestCase("Boolean?_Undefined", "bound.EchoNullableBoolean(undefined) === null", false)] + [TestCase("Boolean?_Null", "bound.EchoNullableBoolean(null) === null", false)] + [TestCase("Boolean?_Boolean_True", "bound.EchoNullableBoolean(true) === true", false)] + [TestCase("Boolean?_Boolean_False", "bound.EchoNullableBoolean(false) === false", false)] +/* +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| SByte | - | - | - | + | - | - | - | - | +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| Int16 | - | - | - | + | - | - | - | - | +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| Int32 | - | - | - | + | - | - | - | - | +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| Int64 | - | - | - | + | - | - | - | - | +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| Byte | - | - | - | + | - | - | - | - | +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| UInt16 | - | - | - | + | - | - | - | - | +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| UInt32 | - | - | - | + | - | - | - | - | +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| UInt64 | - | - | - | + | - | - | - | - | +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| Single | - | - | - | + | - | - | - | - | +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| Double | - | - | - | + | - | - | - | - | +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| Char | - | - | - | + | - | - | - | - | +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| DateTime | - | - | - | - | - | + | - | - | +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| Decimal | - | - | - | + | - | - | - | - | +|=CLR Type |=JavaScript values and objects | +| |=undefined |=null |=Boolean |=Number |=String |=Date |=Array |=Object | +| String | - | + | - | - | + | - | - | - | +*/ + public void ExpectScriptExpr(string name, string jsExpr, bool expectScriptException) + { + string script = "return ((" + jsExpr + ")? 'pass' : 'fail');"; + string result = null; + try + { + result = RunScript(script, 1000); + } + catch (TimeoutException te) + { + Assert.Fail("TestCase '{0}' with script '{1}' execution timed out. \r\n{2}", name, script, te.ToString()); + } + catch (ScriptException se) + { + if (expectScriptException) + { + Assert.Pass(se.Message); + } + else throw; + } + Assert.AreEqual("pass", result, "TestCase '{0}' with script '{1}' fails.", name, script); + } + + #endregion } } diff --git a/CefSharp.Tests/BoundObject.cs b/CefSharp.Tests/BoundObject.cs index 211d49ad6..1b6a3ee8f 100644 --- a/CefSharp.Tests/BoundObject.cs +++ b/CefSharp.Tests/BoundObject.cs @@ -7,6 +7,22 @@ namespace CefSharp.Tests { public class BoundObject { + public void EchoVoid() + { + } + + public bool EchoBoolean(bool arg0) + { + return arg0; + } + + public bool? EchoNullableBoolean(bool? arg0) + { + return arg0; + } + + + public int EchoInt(int arg0) { return arg0; diff --git a/CefSharp/BindingHandler.cpp b/CefSharp/BindingHandler.cpp index 63f846c8f..8320917b5 100644 --- a/CefSharp/BindingHandler.cpp +++ b/CefSharp/BindingHandler.cpp @@ -4,15 +4,17 @@ namespace CefSharp { - CefRefPtr BindingHandler::ConvertToCef(Object^ obj) + CefRefPtr BindingHandler::ConvertToCef(Type^ type, Object^ obj) { + if(type == Void::typeid) + { + return CefV8Value::CreateUndefined(); + } if(obj == nullptr) { return CefV8Value::CreateNull(); } - Type^ type = obj->GetType(); - if (type == Boolean::typeid) { return CefV8Value::CreateBool(safe_cast(obj)); @@ -38,7 +40,7 @@ namespace CefSharp { if (obj->IsNull() || obj->IsUndefined()) { - return nullptr; + return nullptr; } if (obj->IsBool()) return gcnew System::Boolean(obj->GetBoolValue()); @@ -77,7 +79,15 @@ namespace CefSharp array^ suppliedArguments = gcnew array(arguments.size()); for(int i = 0; i < suppliedArguments->Length; i++) { - suppliedArguments[i] = ConvertFromCef(arguments[i]); + try + { + suppliedArguments[i] = ConvertFromCef(arguments[i]); + } + catch(System::Exception^ err) + { + exception = convertFromString(err->Message); + return true; + } } MethodInfo^ bestMethod; @@ -145,7 +155,7 @@ namespace CefSharp try { Object^ result = bestMethod->Invoke(self, bestMethodArguments); - retval = ConvertToCef(result); + retval = ConvertToCef(bestMethod->ReturnType, result); return true; } catch(System::Reflection::TargetInvocationException^ err) diff --git a/CefSharp/BindingHandler.h b/CefSharp/BindingHandler.h index 2f765dd09..f87f53e93 100644 --- a/CefSharp/BindingHandler.h +++ b/CefSharp/BindingHandler.h @@ -28,7 +28,7 @@ namespace CefSharp class BindingHandler : public CefThreadSafeBase { - CefRefPtr ConvertToCef(Object^ obj); + CefRefPtr ConvertToCef(Type^ type, Object^ obj); Object^ ConvertFromCef(CefRefPtr obj); virtual bool Execute(const CefString& name, CefRefPtr object, const CefV8ValueList& arguments, CefRefPtr& retval, CefString& exception); public: diff --git a/CefSharp/BrowserControl.cpp b/CefSharp/BrowserControl.cpp index 6ba36bd72..40a5b956e 100644 --- a/CefSharp/BrowserControl.cpp +++ b/CefSharp/BrowserControl.cpp @@ -1,6 +1,7 @@ #include "stdafx.h" #include "BrowserControl.h" +#include "ScriptException.h" namespace CefSharp { @@ -74,7 +75,7 @@ namespace CefSharp { return _jsResult; } - throw gcnew Exception("RunScript Exception:" + _jsError); + throw gcnew ScriptException(_jsError); } void BrowserControl::OnReady() diff --git a/CefSharp/CefSharp.vcproj b/CefSharp/CefSharp.vcproj index 1c6993ab1..b196b0d75 100644 --- a/CefSharp/CefSharp.vcproj +++ b/CefSharp/CefSharp.vcproj @@ -1,7 +1,7 @@ + + @@ -326,6 +330,10 @@ RelativePath=".\SchemeHandler.h" > + + diff --git a/CefSharp/ScriptException.cpp b/CefSharp/ScriptException.cpp new file mode 100644 index 000000000..0d39ff256 --- /dev/null +++ b/CefSharp/ScriptException.cpp @@ -0,0 +1,5 @@ +#include "stdafx.h" + +namespace CefSharp +{ +} \ No newline at end of file diff --git a/CefSharp/ScriptException.h b/CefSharp/ScriptException.h new file mode 100644 index 000000000..0f7daaf31 --- /dev/null +++ b/CefSharp/ScriptException.h @@ -0,0 +1,28 @@ +#include "stdafx.h" +#pragma once + +using namespace System; +using namespace System::Runtime::Serialization; + +namespace CefSharp +{ + public ref class ScriptException : public Exception + { + public: ScriptException() + : Exception() + {} + + public: ScriptException(String^ message) + : Exception(message) + {} + + public: ScriptException(String^ message, Exception^ innerException) + : Exception(message, innerException) + {} + + public: ScriptException(SerializationInfo^ info, StreamingContext context) + : Exception(info, context) + {} + }; + +} \ No newline at end of file