Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegisterAsyncJsObject managed methods can now return arrays of structs #1981

Merged
merged 4 commits into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ namespace CefSharp
{
auto frameId = GetInt64(argList, 0);
auto callbackId = GetInt64(argList, 1);

JavascriptRootObjectWrapper^ rootObjectWrapper;
browserWrapper->JavascriptRootObjectWrappers->TryGetValue(frameId, rootObjectWrapper);

Expand All @@ -405,17 +405,44 @@ namespace CefSharp
JavascriptAsyncMethodCallback^ callback;
if (rootObjectWrapper->TryGetAndRemoveMethodCallback(callbackId, callback))
{
auto success = argList->GetBool(2);
if (success)

try
{
callback->Success(DeserializeV8Object(argList, 3));
auto frame = browser->GetFrame(frameId);
if (frame.get())
{
auto context = frame->GetV8Context();

if (context.get() && context->Enter())
{
try
{
auto success = argList->GetBool(2);
if (success)
{
callback->Success(DeserializeV8Object(argList, 3));
}
else
{
callback->Fail(argList->GetString(3));
}
}
finally
{
context->Exit();
}
}
else
{
callback->Fail("Unable to Enter Context");
}
}
}
else
finally
{
callback->Fail(argList->GetString(3));
//dispose
delete callback;
}
//dispose
delete callback;
}
}
handled = true;
Expand Down
14 changes: 14 additions & 0 deletions CefSharp.Example/AsyncBoundObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

namespace CefSharp.Example
{
public struct JsObject
{
public string Value;
}

public class AsyncBoundObject
{
//We expect an exception here, so tell VS to ignore
Expand All @@ -33,5 +38,14 @@ public void DoSomething()
{
Thread.Sleep(1000);
}

public JsObject[] ObjectArray(string name)
{
return new[]
{
new JsObject() { Value = "Item1" },
new JsObject() { Value = "Item2" }
};
}
}
}
12 changes: 12 additions & 0 deletions CefSharp.Example/Resources/BindingTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,23 @@
writeAsyncResult(call, end);
});
}

function asyncObjectArray()
{
var call = "Async call (ObjectArray): " + Date();
boundAsync.objectArray('CefSharp').then(function (res)
{
var end = "Result: [ " + res.map(function (item) { return item.Value }) + " ] (" + Date() + ")";
writeAsyncResult(call, end);
});
}

asyncError();
asyncDivOk();
asyncDivFail();
asyncDoSomething();
asyncHello();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an asyncHello method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

asyncObjectArray();
</script>
</p>
<p>
Expand Down
4 changes: 2 additions & 2 deletions CefSharp/Internals/JavascriptObjectRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public bool TryCallMethod(long objectId, string name, object[] parameters, out o
}
catch (Exception e)
{
throw new InvalidOperationException("Could not execute method: " + name + "(" + String.Join(", ", parameters) + ")" + " - Missing Parameters: " + missingParams, e);
throw new InvalidOperationException("Could not execute method: " + name + "(" + String.Join(", ", parameters) + ") " + (missingParams > 0 ? "- Missing Parameters: " + missingParams : ""), e);
}

if(result != null && IsComplexType(result.GetType()))
Expand Down Expand Up @@ -397,7 +397,7 @@ private static bool IsComplexType(Type type)
baseType = Nullable.GetUnderlyingType(type);
}

if (baseType == null || baseType.Namespace.StartsWith("System"))
if (baseType == null || baseType.IsArray || baseType.Namespace.StartsWith("System"))
{
return false;
}
Expand Down