-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathIWinRTObject.net5.cs
More file actions
348 lines (310 loc) · 14.5 KB
/
IWinRTObject.net5.cs
File metadata and controls
348 lines (310 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
#if NET8_0_OR_GREATER
using System.Runtime.InteropServices.Marshalling;
#endif
using WinRT.Interop;
namespace WinRT
{
#if EMBED
internal
#else
public
#endif
interface IWinRTObject : IDynamicInterfaceCastable
#if NET8_0_OR_GREATER
, IUnmanagedVirtualMethodTableProvider
#endif
{
bool IDynamicInterfaceCastable.IsInterfaceImplemented(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented)
{
if (!FeatureSwitches.EnableIDynamicInterfaceCastableSupport)
{
return false;
}
return IsInterfaceImplementedFallback(interfaceType, throwIfNotImplemented);
}
internal sealed bool IsInterfaceImplementedFallback(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented)
{
if (!FeatureSwitches.EnableIDynamicInterfaceCastableSupport)
{
return throwIfNotImplemented ?
throw new NotSupportedException($"Support for 'IDynamicInterfaceCastable' is disabled (make sure that the 'CsWinRTEnableIDynamicInterfaceCastableSupport' property is not set to 'false').") : false;
}
if (QueryInterfaceCache.ContainsKey(interfaceType))
{
return true;
}
#if NET8_0_OR_GREATER
bool vtableLookup = LookupGeneratedVTableInfo(interfaceType, out _, out int qiResult);
if (vtableLookup)
{
return true;
}
else if (qiResult < 0 && throwIfNotImplemented)
{
// A qiResult of less than zero means the call to QueryInterface has failed.
ExceptionHelpers.ThrowExceptionForHR(qiResult);
}
#endif
Type type = Type.GetTypeFromHandle(interfaceType);
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IReadOnlyCollection<>))
{
#if NET
if (!RuntimeFeature.IsDynamicCodeCompiled)
{
return throwIfNotImplemented ? throw new NotSupportedException($"'IDynamicInterfaceCastable' is not supported for generic type '{type}'.") : false;
}
#endif
#pragma warning disable IL3050 // https://github.com/dotnet/runtime/issues/97273
Type itemType = type.GetGenericArguments()[0];
if (itemType.IsGenericType && itemType.GetGenericTypeDefinition() == typeof(KeyValuePair<,>))
{
Type iReadOnlyDictionary = typeof(IReadOnlyDictionary<,>).MakeGenericType(itemType.GetGenericArguments());
if (IsInterfaceImplemented(iReadOnlyDictionary.TypeHandle, false))
{
if (QueryInterfaceCache.TryGetValue(iReadOnlyDictionary.TypeHandle, out var typedObjRef) && !QueryInterfaceCache.TryAdd(interfaceType, typedObjRef))
{
typedObjRef.Dispose();
}
return true;
}
}
Type iReadOnlyList = typeof(IReadOnlyList<>).MakeGenericType(new[] { itemType });
#pragma warning restore IL3050
if (IsInterfaceImplemented(iReadOnlyList.TypeHandle, throwIfNotImplemented))
{
if (QueryInterfaceCache.TryGetValue(iReadOnlyList.TypeHandle, out var typedObjRef) && !QueryInterfaceCache.TryAdd(interfaceType, typedObjRef))
{
typedObjRef.Dispose();
}
return true;
}
return false;
}
else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(System.Collections.Generic.ICollection<>))
{
#if NET
if (!RuntimeFeature.IsDynamicCodeCompiled)
{
return throwIfNotImplemented ? throw new NotSupportedException($"'IDynamicInterfaceCastable' is not supported for generic type '{type}'.") : false;
}
#endif
#pragma warning disable IL3050 // https://github.com/dotnet/runtime/issues/97273
Type itemType = type.GetGenericArguments()[0];
if (itemType.IsGenericType && itemType.GetGenericTypeDefinition() == typeof(KeyValuePair<,>))
{
Type iDictionary = typeof(IDictionary<,>).MakeGenericType(itemType.GetGenericArguments());
if (IsInterfaceImplemented(iDictionary.TypeHandle, false))
{
if (QueryInterfaceCache.TryGetValue(iDictionary.TypeHandle, out var typedObjRef) && !QueryInterfaceCache.TryAdd(interfaceType, typedObjRef))
{
typedObjRef.Dispose();
}
return true;
}
}
Type iList = typeof(IList<>).MakeGenericType(new[] { itemType });
#pragma warning restore IL3050
if (IsInterfaceImplemented(iList.TypeHandle, throwIfNotImplemented))
{
if (QueryInterfaceCache.TryGetValue(iList.TypeHandle, out var typedObjRef) && !QueryInterfaceCache.TryAdd(interfaceType, typedObjRef))
{
typedObjRef.Dispose();
}
return true;
}
return false;
}
else if (type == typeof(System.Collections.IEnumerable))
{
Type iEnum = typeof(System.Collections.Generic.IEnumerable<object>);
if (IsInterfaceImplemented(iEnum.TypeHandle, false))
{
if (QueryInterfaceCache.TryGetValue(iEnum.TypeHandle, out var typedObjRef) && !QueryInterfaceCache.TryAdd(interfaceType, typedObjRef))
{
typedObjRef.Dispose();
}
return true;
}
}
else if (type == typeof(System.Collections.ICollection))
{
Type iList = typeof(global::System.Collections.IList);
if (IsInterfaceImplemented(iList.TypeHandle, false))
{
if (QueryInterfaceCache.TryGetValue(iList.TypeHandle, out var typedObjRef) && !QueryInterfaceCache.TryAdd(interfaceType, typedObjRef))
{
typedObjRef.Dispose();
}
return true;
}
}
// Make sure we are going to do a QI on a WinRT interface,
// otherwise we can get a helper type for a non WinRT type.
if (!Projections.IsTypeWindowsRuntimeType(type))
{
return false;
}
Type helperType = type.FindHelperType(throwIfNotImplemented);
if (helperType is null || !helperType.IsInterface)
{
return false;
}
int hr = NativeObject.TryAs<IUnknownVftbl>(GuidGenerator.GetIID(helperType), out var objRef);
if (hr < 0)
{
if (throwIfNotImplemented)
{
ExceptionHelpers.ThrowExceptionForHR(hr);
}
return false;
}
if (typeof(System.Collections.IEnumerable).IsAssignableFrom(type))
{
RuntimeTypeHandle projectIEnum = typeof(System.Collections.IEnumerable).TypeHandle;
AdditionalTypeData.GetOrAdd(projectIEnum, static (_, info) => new ABI.System.Collections.IEnumerable.AdaptiveFromAbiHelper(info.Type, info.This), new AdditionalTypeDataParams { Type = type, This = this });
}
bool hasMovedObjRefOwnership = false;
try
{
var vftblType = helperType.FindVftblType();
// If there is no nested vftbl type, we want to add the object reference with the IID from the helper type
// to the cache. Rather than doing 'QueryInterface' again with the same IID, on the object reference we
// already have, we can just store that same instance in the cache, and suppress the 'objRef.Dispose()'
// call for it. This avoids that extra call, plus the overhead of allocating a new object reference.
// For all other cases, we dispose the object reference as usual at the end of this scope.
if (vftblType is null)
{
hasMovedObjRefOwnership = QueryInterfaceCache.TryAdd(interfaceType, objRef);
return true;
}
#if NET
if (!RuntimeFeature.IsDynamicCodeCompiled)
{
return throwIfNotImplemented ? throw new NotSupportedException($"Cannot construct an object reference for vtable type '{vftblType}'.") : false;
}
#endif
#if NET8_0_OR_GREATER
[RequiresDynamicCode(AttributeMessages.MarshallingOrGenericInstantiationsRequiresDynamicCode)]
#endif
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "If the 'Vftbl' type is kept, we can assume all its metadata will also have been rooted.")]
[MethodImpl(MethodImplOptions.NoInlining)]
static IObjectReference GetObjectReferenceViaVftbl(IObjectReference objRef, Type vftblType)
{
return (IObjectReference)typeof(IObjectReference).GetMethod("As", Type.EmptyTypes).MakeGenericMethod(vftblType).Invoke(objRef, null);
}
#pragma warning disable IL3050 // https://github.com/dotnet/runtime/issues/97273
IObjectReference typedObjRef = GetObjectReferenceViaVftbl(objRef, vftblType);
#pragma warning restore IL3050
if (!QueryInterfaceCache.TryAdd(interfaceType, typedObjRef))
{
typedObjRef.Dispose();
}
return true;
}
finally
{
if (!hasMovedObjRefOwnership)
{
objRef.Dispose();
}
}
}
#if NET8_0_OR_GREATER
internal sealed unsafe bool LookupGeneratedVTableInfo(RuntimeTypeHandle interfaceType, [NotNullWhen(true)] out IIUnknownCacheStrategy.TableInfo? result, out int qiResult)
{
result = null;
qiResult = 0;
if (AdditionalTypeData.TryGetValue(interfaceType, out object value))
{
if (value is IIUnknownCacheStrategy.TableInfo tableInfo)
{
result = tableInfo;
return true;
}
return false;
}
if (StrategyBasedComWrappers.DefaultIUnknownInterfaceDetailsStrategy.GetIUnknownDerivedDetails(interfaceType) is IIUnknownDerivedDetails details)
{
qiResult = NativeObject.TryAs(details.Iid, out ObjectReference<IUnknownVftbl> objRef);
if (qiResult < 0)
return false;
var obj = (void***)objRef.ThisPtr;
result = new IIUnknownCacheStrategy.TableInfo()
{
ThisPtr = obj,
Table = *obj,
ManagedType = details.Implementation.TypeHandle
};
if (!AdditionalTypeData.TryAdd(interfaceType, result))
{
bool found = AdditionalTypeData.TryGetValue(interfaceType, out object newInfo);
System.Diagnostics.Debug.Assert(found);
result = (IIUnknownCacheStrategy.TableInfo)newInfo;
objRef.Dispose();
}
else
{
QueryInterfaceCache.TryAdd(interfaceType, objRef);
}
return true;
}
return false;
}
#endif
RuntimeTypeHandle IDynamicInterfaceCastable.GetInterfaceImplementation(RuntimeTypeHandle interfaceType)
{
#if NET8_0_OR_GREATER
if (AdditionalTypeData.TryGetValue(interfaceType, out object value) && value is IIUnknownCacheStrategy.TableInfo tableInfo)
{
return tableInfo.ManagedType;
}
#endif
var type = Type.GetTypeFromHandle(interfaceType);
var helperType = type.GetHelperType();
if (helperType.IsInterface)
return helperType.TypeHandle;
return default;
}
#if NET8_0_OR_GREATER
unsafe VirtualMethodTableInfo IUnmanagedVirtualMethodTableProvider.GetVirtualMethodTableInfoForKey(Type type)
{
if (!LookupGeneratedVTableInfo(type.TypeHandle, out IIUnknownCacheStrategy.TableInfo? result, out int qiHResult))
{
Marshal.ThrowExceptionForHR(qiHResult);
}
return new(result.Value.ThisPtr, result.Value.Table);
}
#endif
IObjectReference NativeObject { get; }
bool HasUnwrappableNativeObject { get; }
protected ConcurrentDictionary<RuntimeTypeHandle, IObjectReference> QueryInterfaceCache { get; }
IObjectReference GetObjectReferenceForType(RuntimeTypeHandle type)
{
return GetObjectReferenceForTypeFallback(type);
}
internal sealed IObjectReference GetObjectReferenceForTypeFallback(RuntimeTypeHandle type)
{
if (IsInterfaceImplemented(type, true))
{
return QueryInterfaceCache[type];
}
throw new Exception("Interface '" + Type.GetTypeFromHandle(type) + "' is not implemented.");
}
ConcurrentDictionary<RuntimeTypeHandle, object> AdditionalTypeData { get; }
}
// Custom type to avoid rooting value tuple metadata (saves a few KBs of size)
internal struct AdditionalTypeDataParams
{
public Type Type;
public IWinRTObject This;
}
}