Skip to content

Commit

Permalink
special casing for IReference[String] and KeyValuePair (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
Scottj1s authored Apr 17, 2020
1 parent 9ebe644 commit 90d9954
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
6 changes: 3 additions & 3 deletions UnitTest/TestComponentCSharp_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void TestKeyValuePair()
TestObject.RaiseStringPairChanged();
}

[Fact(Skip = "System.ExecutionEngineException in Marshal.PtrToStructure<T>(vftblPtr.Vftbl)")]
[Fact]
public void TestObjectCasting()
{
var expected = new KeyValuePair<string, string>("key", "value");
Expand Down Expand Up @@ -168,7 +168,7 @@ public void TestStringMap()
}
}

[Fact(Skip = "System.ExecutionEngineException in Marshal.PtrToStructure<T>(vftblPtr.Vftbl)")]
[Fact]
public void TestPropertySet()
{
var map = new Dictionary<string, string> { ["foo"] = "bar", ["hello"] = "world" };
Expand All @@ -184,7 +184,7 @@ public void TestPropertySet()
}
}

[Fact(Skip = "System.ExecutionEngineException in Marshal.PtrToStructure<T>(vftblPtr.Vftbl)")]
[Fact]
public void TestValueSet()
{
var map = new Dictionary<string, string> { ["foo"] = "bar", ["hello"] = "world" };
Expand Down
19 changes: 19 additions & 0 deletions WinRT.Runtime/ComWrappersSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ private static bool IsIReferenceArray(Type implementationType)
return implementationType.FullName.StartsWith("Windows.Foundation.IReferenceArray`1");
}

private static Func<IInspectable, object> CreateKeyValuePairFactory(Type type)
{
var parms = new[] { Expression.Parameter(typeof(IInspectable), "obj") };
return Expression.Lambda<Func<IInspectable, object>>(
Expression.Call(type.GetHelperType().GetMethod("CreateRcw", BindingFlags.Public | BindingFlags.Static),
parms), parms).Compile();
}

private static Func<IInspectable, object> CreateNullableTFactory(Type implementationType)
{
Type helperType = implementationType.GetHelperType();
Expand Down Expand Up @@ -252,8 +260,19 @@ private static Func<IInspectable, object> CreateArrayFactory(Type implementation

internal static Func<IInspectable, object> CreateTypedRcwFactory(string runtimeClassName)
{
// PropertySet and ValueSet can return IReference<String> but Nullable<String> is illegal
if (runtimeClassName == "Windows.Foundation.IReference`1<String>")
{
return (IInspectable obj) => new ABI.System.Nullable<String>(obj.ObjRef).Value;
}

var (implementationType, _) = TypeNameSupport.FindTypeByName(runtimeClassName.AsSpan());

if (implementationType.IsGenericType && implementationType.GetGenericTypeDefinition() == typeof(System.Collections.Generic.KeyValuePair<,>))
{
return CreateKeyValuePairFactory(implementationType);
}

if (implementationType.IsValueType)
{
if (IsNullableT(implementationType))
Expand Down
1 change: 1 addition & 0 deletions WinRT.Runtime/IInspectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public static IInspectable FromAbi(IntPtr thisPtr) =>
public static implicit operator IInspectable(IObjectReference obj) => obj.As<Vftbl>();
public static implicit operator IInspectable(ObjectReference<Vftbl> obj) => new IInspectable(obj);
public ObjectReference<I> As<I>() => _obj.As<I>();
public IObjectReference ObjRef { get => _obj; }
public IInspectable(IObjectReference obj) : this(obj.As<Vftbl>()) { }
public IInspectable(ObjectReference<Vftbl> obj)
{
Expand Down
6 changes: 6 additions & 0 deletions WinRT.Runtime/Projections/KeyValuePair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public static IObjectReference CreateMarshaler(global::System.Collections.Generi
public static IntPtr GetAbi(IObjectReference objRef) =>
objRef?.ThisPtr ?? IntPtr.Zero;

public static object CreateRcw(IInspectable obj)
{
var pair = new KeyValuePair<K, V>(obj.As<Vftbl>());
return (object)new global::System.Collections.Generic.KeyValuePair<K, V>(pair.Key, pair.Value);
}

public static global::System.Collections.Generic.KeyValuePair<K, V> FromAbi(IntPtr thisPtr)
{
var pair = new KeyValuePair<K, V>(KeyValuePair<K, V>._FromAbi(thisPtr));
Expand Down
6 changes: 5 additions & 1 deletion WinRT.Runtime/TypeNameSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ private static (string genericTypeName, Type[] genericTypes, int remaining) Pars
continue;
}
else if (remainingTypeName[0] == '>')
{
{
// Skip the space after nested '>'
var skip = (remainingTypeName.Length > 1 && remainingTypeName[1] == ' ') ? 2 : 1;
remainingIndex += skip;
remainingTypeName = remainingTypeName.Slice(skip);
break;
}
else
Expand Down
2 changes: 2 additions & 0 deletions WinUI/WinUIDesktopSample.Package/NoBuild.Targets
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
project from the command line.
-->
<Target Name="Build" />
<Target Name="Rebuild" />
<Target Name="Clean" />
</Project>

0 comments on commit 90d9954

Please sign in to comment.