From ea609795c8d6f8970ed55342a930d9532e9fef11 Mon Sep 17 00:00:00 2001 From: Per Lundberg Date: Sat, 13 Jul 2013 00:16:47 +0300 Subject: [PATCH] Started doing some light refactorings here to make the code a bit more maintainable: Smaller, more concise methods. The FindBestMethod() is up next. --- .../JavascriptBinding/BindingHandler.cpp | 42 ++++++++++--------- .../JavascriptBinding/BindingHandler.h | 2 + 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/CefSharp/Internals/JavascriptBinding/BindingHandler.cpp b/CefSharp/Internals/JavascriptBinding/BindingHandler.cpp index 282016c2c..faf129ec7 100644 --- a/CefSharp/Internals/JavascriptBinding/BindingHandler.cpp +++ b/CefSharp/Internals/JavascriptBinding/BindingHandler.cpp @@ -21,19 +21,7 @@ namespace CefSharp javascriptWrapper->SetUserData(static_cast>(unmanagedWrapper)); auto handler = static_cast(new BindingHandler()); - - // Build a list of methods on the bound object - auto methods = obj->GetType()->GetMethods(BindingFlags::Instance | BindingFlags::Public); - auto methodNames = gcnew HashSet(); - - for each(auto method in methods) - { - // "Special name"-methods are things like property getters and setters, which we don't want to include in the list. - if (method->IsSpecialName) continue; - - methodNames->Add(method->Name); - } - + auto methodNames = GetMethodNames(obj->GetType()); CreateJavascriptMethods(handler, javascriptWrapper, methodNames); unmanagedWrapper->Properties = GetProperties(obj->GetType()); @@ -47,7 +35,7 @@ namespace CefSharp auto unmanagedWrapper = static_cast(object->GetUserData().get()); Object^ self = unmanagedWrapper->Get(); - if(self == nullptr) + if (self == nullptr) { exception = "Binding's CLR object is null."; return true; @@ -58,7 +46,7 @@ namespace CefSharp Type^ type = self->GetType(); auto methods = type->GetMember(methodName, MemberTypes::Method, BindingFlags::Instance | BindingFlags::Public); - if(methods->Length == 0) + if (methods->Length == 0) { exception = toNative("No method named " + methodName + "."); return true; @@ -69,12 +57,12 @@ namespace CefSharp auto suppliedArguments = gcnew array(arguments.size()); try { - for(int i = 0; i < suppliedArguments->Length; i++) + for (int i = 0; i < suppliedArguments->Length; i++) { suppliedArguments[i] = convertFromCef(arguments[i]); } } - catch(System::Exception^ err) + catch (System::Exception^ err) { exception = toNative(err->Message); return true; @@ -93,11 +81,11 @@ namespace CefSharp retval = convertToCef(result, bestMethod->ReturnType); return true; } - catch(System::Reflection::TargetInvocationException^ err) + catch (System::Reflection::TargetInvocationException^ err) { exception = toNative(err->InnerException->Message); } - catch(System::Exception^ err) + catch (System::Exception^ err) { exception = toNative(err->Message); } @@ -241,6 +229,22 @@ namespace CefSharp return Convert::ChangeType(value, conversionType); } + HashSet^ BindingHandler::GetMethodNames(Type^ type) + { + auto methods = type->GetMethods(BindingFlags::Instance | BindingFlags::Public); + auto result = gcnew HashSet(); + + for each(auto method in methods) + { + // "Special name"-methods are things like property getters and setters, which we don't want to include in the list. + if (method->IsSpecialName) continue; + + result->Add(method->Name); + } + + return result; + } + void BindingHandler::FindBestMethod(array^ methods, array^ suppliedArguments, MethodInfo^% bestMethod, array^% bestMethodArguments) { auto bestMethodCost = -1; diff --git a/CefSharp/Internals/JavascriptBinding/BindingHandler.h b/CefSharp/Internals/JavascriptBinding/BindingHandler.h index 9c53ca0d1..7cbd5181f 100644 --- a/CefSharp/Internals/JavascriptBinding/BindingHandler.h +++ b/CefSharp/Internals/JavascriptBinding/BindingHandler.h @@ -19,6 +19,8 @@ namespace CefSharp private: virtual bool Execute(const CefString& name, CefRefPtr object, const CefV8ValueList& arguments, CefRefPtr& retval, CefString& exception); + static HashSet^ GetMethodNames(Type^ type); + static bool IsNullableType(Type^ type); static int GetChangeTypeCost(Object^ value, Type^ conversionType); static Object^ ChangeType(Object^ value, Type^ conversionType);