summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/CLRIReferenceImpl.cs
blob: e379d38cf31bd5fdfd9b9d2cfd06eccb07a6e935 (plain)
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

//

using System;
using System.Collections;
using System.Diagnostics.Contracts;
using System.Reflection;
using System.Security;

namespace System.Runtime.InteropServices.WindowsRuntime
{
    internal sealed class CLRIReferenceImpl<T> : CLRIPropertyValueImpl, IReference<T>, IGetProxyTarget 
    {
        private T _value;

        public CLRIReferenceImpl(PropertyType type, T obj)
            : base(type, obj)
        {
            BCLDebug.Assert(obj != null, "Must not be null");
            _value = obj;
        }

        public T Value {
            get { return _value; }
        }

        public override string ToString()
        {
            if (_value != null)
            {
                return _value.ToString();
            }
            else
            {
                return base.ToString();
            }
        }

        object IGetProxyTarget.GetTarget()
        {
            return (object)_value;
        }

        // We have T in an IReference<T>.  Need to QI for IReference<T> with the appropriate GUID, call
        // the get_Value property, allocate an appropriately-sized managed object, marshal the native object
        // to the managed object, and free the native method.  Also we want the return value boxed (aka normal value type boxing).
        //
        // This method is called by VM. Mark the method with FriendAccessAllowed attribute to ensure that the unreferenced method
        // optimization skips it and the code will be saved into NGen image.
        [System.Runtime.CompilerServices.FriendAccessAllowed]
        internal static Object UnboxHelper(Object wrapper)
        {
            Contract.Requires(wrapper != null);
            IReference<T> reference = (IReference<T>) wrapper;
            Contract.Assert(reference != null, "CLRIReferenceImpl::UnboxHelper - QI'ed for IReference<"+typeof(T)+">, but that failed.");
            return reference.Value;
        }
    }

    // T can be any WinRT-compatible type
    internal sealed class CLRIReferenceArrayImpl<T> : CLRIPropertyValueImpl,
                                                      IGetProxyTarget, 
                                                      IReferenceArray<T>, 
                                                      IList                     // Jupiter data binding needs IList/IEnumerable
    {
        private T[] _value;
        private IList _list;

        public CLRIReferenceArrayImpl(PropertyType type, T[] obj)
            : base(type, obj)
        {
            BCLDebug.Assert(obj != null, "Must not be null");

            _value = obj;

            _list = (IList) _value;
        }

        public T[] Value {
            get { return _value; }
        }

        public override string ToString()
        {
            if (_value != null)
            {
                return _value.ToString();
            }
            else
            {
                return base.ToString();
            }
        }

        //
        // IEnumerable methods. Used by data-binding in Jupiter when you try to data bind
        // against a managed array
        //
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)_value).GetEnumerator();
        }

        //
        // IList & ICollection methods. 
        // This enables two-way data binding and index access in Jupiter
        //
        Object IList.this[int index] {
            get
            {
                return _list[index];
            }

            set
            {
                _list[index] = value;
            }
        }
    
        int IList.Add(Object value)
        {
            return _list.Add(value);
        }
    
        bool IList.Contains(Object value)
        {
            return _list.Contains(value);
        }
    
        void IList.Clear()
        {
            _list.Clear();
        }

        bool IList.IsReadOnly 
        { 
            get
            {
                return _list.IsReadOnly;
            }
        }
    
        bool IList.IsFixedSize
        {
            get
            {
                return _list.IsFixedSize;
            }
        }

        int IList.IndexOf(Object value)
        {
            return _list.IndexOf(value);
        }
    
        void IList.Insert(int index, Object value)
        {
            _list.Insert(index, value);
        }
    
        void IList.Remove(Object value)
        {
            _list.Remove(value);
        }
    
        void IList.RemoveAt(int index)
        {
            _list.RemoveAt(index);
        }

        void ICollection.CopyTo(Array array, int index)
        {
            _list.CopyTo(array, index);
        }
        
        int ICollection.Count
        { 
            get
            {
                return _list.Count;
            }
        }

        Object ICollection.SyncRoot
        { 
            get
            {
                return _list.SyncRoot;
            }
        }
            
        bool ICollection.IsSynchronized
        { 
            get
            {
                return _list.IsSynchronized;
            }
        }

        object IGetProxyTarget.GetTarget()
        {
            return (object)_value;
        }
        
        // We have T in an IReferenceArray<T>.  Need to QI for IReferenceArray<T> with the appropriate GUID, call
        // the get_Value property, allocate an appropriately-sized managed object, marshal the native object
        // to the managed object, and free the native method.
        //
        // This method is called by VM. Mark the method with FriendAccessAllowed attribute to ensure that the unreferenced method
        // optimization skips it and the code will be saved into NGen image.
        [System.Runtime.CompilerServices.FriendAccessAllowed]
        internal static Object UnboxHelper(Object wrapper)
        {
            Contract.Requires(wrapper != null);
            IReferenceArray<T> reference = (IReferenceArray<T>)wrapper;
            Contract.Assert(reference != null, "CLRIReferenceArrayImpl::UnboxHelper - QI'ed for IReferenceArray<" + typeof(T) + ">, but that failed.");
            T[] marshaled = reference.Value;
            return marshaled;
        }
    }

    // For creating instances of Windows Runtime's IReference<T> and IReferenceArray<T>.
    internal static class IReferenceFactory
    {
        internal static readonly Type s_pointType = Type.GetType("Windows.Foundation.Point, " + AssemblyRef.SystemRuntimeWindowsRuntime);
        internal static readonly Type s_rectType = Type.GetType("Windows.Foundation.Rect, " + AssemblyRef.SystemRuntimeWindowsRuntime);
        internal static readonly Type s_sizeType = Type.GetType("Windows.Foundation.Size, " + AssemblyRef.SystemRuntimeWindowsRuntime);

        [SecuritySafeCritical]
        internal static Object CreateIReference(Object obj)
        {
            Contract.Requires(obj != null, "Null should not be boxed.");
            Contract.Ensures(Contract.Result<Object>() != null);

            Type type = obj.GetType();

            if (type.IsArray)
                return CreateIReferenceArray((Array) obj);

            if (type == typeof(int))
                return new CLRIReferenceImpl<int>(PropertyType.Int32, (int)obj);
            if (type == typeof(String))
                return new CLRIReferenceImpl<String>(PropertyType.String, (String)obj);
            if (type == typeof(byte))
                return new CLRIReferenceImpl<byte>(PropertyType.UInt8, (byte)obj);
            if (type == typeof(short))
                return new CLRIReferenceImpl<short>(PropertyType.Int16, (short)obj);
            if (type == typeof(ushort))
                return new CLRIReferenceImpl<ushort>(PropertyType.UInt16, (ushort)obj);
            if (type == typeof(uint))
                return new CLRIReferenceImpl<uint>(PropertyType.UInt32, (uint)obj);
            if (type == typeof(long))
                return new CLRIReferenceImpl<long>(PropertyType.Int64, (long)obj);
            if (type == typeof(ulong))
                return new CLRIReferenceImpl<ulong>(PropertyType.UInt64, (ulong)obj);
            if (type == typeof(float))
                return new CLRIReferenceImpl<float>(PropertyType.Single, (float)obj);
            if (type == typeof(double))
                return new CLRIReferenceImpl<double>(PropertyType.Double, (double)obj);
            if (type == typeof(char))
                return new CLRIReferenceImpl<char>(PropertyType.Char16, (char)obj);
            if (type == typeof(bool))
                return new CLRIReferenceImpl<bool>(PropertyType.Boolean, (bool)obj);
            if (type == typeof(Guid))
                return new CLRIReferenceImpl<Guid>(PropertyType.Guid, (Guid)obj);
            if (type == typeof(DateTimeOffset))
                return new CLRIReferenceImpl<DateTimeOffset>(PropertyType.DateTime, (DateTimeOffset)obj);
            if (type == typeof(TimeSpan))
                return new CLRIReferenceImpl<TimeSpan>(PropertyType.TimeSpan, (TimeSpan)obj);
            if (type == typeof(Object))
                return new CLRIReferenceImpl<Object>(PropertyType.Inspectable, (Object)obj);
            if (type == typeof(RuntimeType))
            {   // If the type is System.RuntimeType, we want to use System.Type marshaler (it's parent of the type)
                return new CLRIReferenceImpl<Type>(PropertyType.Other, (Type)obj);
            }

            // Handle arbitrary WinRT-compatible value types, and recognize a few special types.
            PropertyType? propType = null;
            if (type == s_pointType)
            {
                propType = PropertyType.Point;
            }
            else if (type == s_rectType)
            {
                propType = PropertyType.Rect;
            }
            else if (type == s_sizeType)
            {
                propType = PropertyType.Size;
            }
            else if (type.IsValueType || obj is Delegate)
            {
                propType = PropertyType.Other;
            }

            if (propType.HasValue)
            {
                Type specificType = typeof(CLRIReferenceImpl<>).MakeGenericType(type);
                return Activator.CreateInstance(specificType, new Object[] { propType.Value, obj });
            }

            Contract.Assert(false, "We should not see non-WinRT type here");
            return null;
        }

        [SecuritySafeCritical]
        internal static Object CreateIReferenceArray(Array obj)
        {
            Contract.Requires(obj != null);
            Contract.Requires(obj.GetType().IsArray);
            Contract.Ensures(Contract.Result<Object>() != null);

            Type type = obj.GetType().GetElementType();
            
            Contract.Assert(obj.Rank == 1 && obj.GetLowerBound(0) == 0 && !type.IsArray);

            if (type == typeof(int))
                return new CLRIReferenceArrayImpl<int>(PropertyType.Int32Array, (int[])obj);
            if (type == typeof(String))
                return new CLRIReferenceArrayImpl<String>(PropertyType.StringArray, (String[])obj);
            if (type == typeof(byte))
                return new CLRIReferenceArrayImpl<byte>(PropertyType.UInt8Array, (byte[])obj);
            if (type == typeof(short))
                return new CLRIReferenceArrayImpl<short>(PropertyType.Int16Array, (short[])obj);
            if (type == typeof(ushort))
                return new CLRIReferenceArrayImpl<ushort>(PropertyType.UInt16Array, (ushort[])obj);
            if (type == typeof(uint))
                return new CLRIReferenceArrayImpl<uint>(PropertyType.UInt32Array, (uint[])obj);
            if (type == typeof(long))
                return new CLRIReferenceArrayImpl<long>(PropertyType.Int64Array, (long[])obj);
            if (type == typeof(ulong))
                return new CLRIReferenceArrayImpl<ulong>(PropertyType.UInt64Array, (ulong[])obj);
            if (type == typeof(float))
                return new CLRIReferenceArrayImpl<float>(PropertyType.SingleArray, (float[])obj);
            if (type == typeof(double))
                return new CLRIReferenceArrayImpl<double>(PropertyType.DoubleArray, (double[])obj);
            if (type == typeof(char))
                return new CLRIReferenceArrayImpl<char>(PropertyType.Char16Array, (char[])obj);
            if (type == typeof(bool))
                return new CLRIReferenceArrayImpl<bool>(PropertyType.BooleanArray, (bool[])obj);
            if (type == typeof(Guid))
                return new CLRIReferenceArrayImpl<Guid>(PropertyType.GuidArray, (Guid[])obj);
            if (type == typeof(DateTimeOffset))
                return new CLRIReferenceArrayImpl<DateTimeOffset>(PropertyType.DateTimeArray, (DateTimeOffset[])obj);
            if (type == typeof(TimeSpan))
                return new CLRIReferenceArrayImpl<TimeSpan>(PropertyType.TimeSpanArray, (TimeSpan[])obj);
            if (type == typeof(Type))
            {   // Note: The array type will be System.Type, not System.RuntimeType
                return new CLRIReferenceArrayImpl<Type>(PropertyType.OtherArray, (Type[])obj);
            }

            PropertyType? propType = null;
            if (type == s_pointType)
            {
                propType = PropertyType.PointArray;
            }
            else if (type == s_rectType)
            {
                propType = PropertyType.RectArray;
            }
            else if (type == s_sizeType)
            {
                propType = PropertyType.SizeArray;
            }
            else if (type.IsValueType)
            {
                // note that KeyValuePair`2 is a reference type on the WinRT side so the array
                // must be wrapped with CLRIReferenceArrayImpl<Object>
                if (type.IsGenericType &&
                    type.GetGenericTypeDefinition() == typeof(System.Collections.Generic.KeyValuePair<,>))
                {
                    Object[] objArray = new Object[obj.Length];
                    for (int i = 0; i < objArray.Length; i++)
                    {
                        objArray[i] = obj.GetValue(i);
                    }
                    obj = objArray;
                }
                else
                {
                    propType = PropertyType.OtherArray;
                }
            }
            else if (typeof(Delegate).IsAssignableFrom(type))
            {
                propType = PropertyType.OtherArray;
            }


            if (propType.HasValue)
            {
                // All WinRT value type will be Property.Other
                Type specificType = typeof(CLRIReferenceArrayImpl<>).MakeGenericType(type);
                return Activator.CreateInstance(specificType, new Object[] { propType.Value, obj });
            }
            else
            {
                // All WinRT reference type (including arbitary managed type) will be PropertyType.ObjectArray
                return new CLRIReferenceArrayImpl<Object>(PropertyType.InspectableArray, (Object[])obj);
            }
        }
    }
}