xronos /Microsoft.Scripting/Runtime/DynamicEventInfo.cs

Language C# Lines 449
MD5 Hash 5e393364e7f9fb98feab448e473f2552 Estimated Cost $7,845 (why?)
Repository https://bitbucket.org/stefanrusek/xronos View Raw File
  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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

#if CODEPLEX_40
using System;
#else
using System; using Microsoft;
#endif
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
#if !CODEPLEX_40
using Microsoft.Runtime.CompilerServices;
#endif

using System.Runtime.InteropServices;
#if CODEPLEX_40
using System.Dynamic;
#else
#endif
using IronPython.Runtime.Operations;
using Microsoft.Scripting;
using Microsoft.Scripting.Generation;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;

namespace IronPython.Runtime.Types {

    /// <summary>
    /// The unbound representation of an event property
    /// </summary>
    [PythonType("event#")]
    public sealed class ReflectedEvent : PythonTypeSlot, ICodeFormattable {
        private readonly bool _clsOnly;
        private readonly EventInfo/*!*/ _eventInfo;
        private WeakDictionary<object, NormalHandlerList/*!*/> _handlerLists;
        private static readonly object _staticTarget = new object();

        internal ReflectedEvent(EventInfo/*!*/ eventInfo, bool clsOnly) {
            Assert.NotNull(eventInfo);

            _clsOnly = clsOnly;
            _eventInfo = eventInfo;
        }

        #region Internal APIs

        internal override bool TryGetValue(CodeContext/*!*/ context, object instance, PythonType owner, out object value) {
            Assert.NotNull(context, owner);

            value = new BoundEvent(this, instance, owner);
            return true;
        }

        internal override bool GetAlwaysSucceeds {
            get {
                return true;
            }
        }

        internal override bool TrySetValue(CodeContext/*!*/ context, object instance, PythonType owner, object value) {
            Assert.NotNull(context);
            BoundEvent et = value as BoundEvent;

            if (et == null || EventInfosDiffer(et)) {
                BadEventChange bea = value as BadEventChange;

                if (bea != null) {
                    PythonType dt = bea.Owner as PythonType;
                    if (dt != null) {
                        if (bea.Instance == null) {
                            throw new MissingMemberException(String.Format("attribute '{1}' of '{0}' object is read-only", dt.Name, SymbolTable.StringToId(_eventInfo.Name)));
                        } else {
                            throw new MissingMemberException(String.Format("'{0}' object has no attribute '{1}'", dt.Name, SymbolTable.StringToId(_eventInfo.Name)));
                        }
                    }
                }

                throw ReadOnlyException(DynamicHelpers.GetPythonTypeFromType(Info.DeclaringType));
            }

            return true;
        }

        private bool EventInfosDiffer(BoundEvent et) {
            // if they're the same object they're the same...
            if (et.Event.Info == this.Info) {
                return false;
            }

            // otherwise compare based upon type & metadata token (they
            // differ by ReflectedType)
            if (et.Event.Info.DeclaringType != Info.DeclaringType ||
                et.Event.Info.MetadataToken != Info.MetadataToken) {
                return true;
            }

            return false;
        }

        internal override bool IsSetDescriptor(CodeContext/*!*/ context, PythonType owner) {
            return true;
        }

        internal override bool TryDeleteValue(CodeContext/*!*/ context, object instance, PythonType owner) {
            Assert.NotNull(context, owner);
            throw ReadOnlyException(DynamicHelpers.GetPythonTypeFromType(Info.DeclaringType));
        }

        internal override bool IsAlwaysVisible {
            get {
                return !_clsOnly;
            }
        }

        private HandlerList/*!*/ GetStubList(object instance) {
#if !SILVERLIGHT
            if (instance != null && ComOps.IsComObject(instance)) {
                return GetComStubList(instance);
            }
#endif

            if (_handlerLists == null) {
                System.Threading.Interlocked.CompareExchange(ref _handlerLists, new WeakDictionary<object, NormalHandlerList>(), null);
            }

            if (instance == null) {
                // targetting a static method, we'll use a random object
                // as our place holder here...
                instance = _staticTarget;
            }

            lock (_handlerLists) {
                NormalHandlerList result;
                if (_handlerLists.TryGetValue(instance, out result)) {
                    return result;
                }

                result = new NormalHandlerList();
                _handlerLists[instance] = result;
                return result;
            }
        }

#if !SILVERLIGHT
        /// <summary>
        /// Gets the stub list for a COM Object.  For COM objects we store the stub list
        /// directly on the object using the Marshal APIs.  This allows us to not have
        /// any circular references to deal with via weak references which are challenging
        /// in the face of COM.
        /// </summary>
        private HandlerList/*!*/ GetComStubList(object/*!*/ instance) {
            HandlerList hl = (HandlerList)Marshal.GetComObjectData(instance, this);
            if (hl == null) {
                lock (_staticTarget) {
                    hl = (HandlerList)Marshal.GetComObjectData(instance, this);
                    if (hl == null) {
                        hl = new ComHandlerList();
                        if (!Marshal.SetComObjectData(instance, this, hl)) {
                            throw new COMException("Failed to set COM Object Data");
                        }
                    }
                }
            }

            return hl;
        }
#endif

        #endregion

        #region Public Python APIs

        public EventInfo/*!*/ Info {
            [PythonHidden]
            get {
                return _eventInfo;
            }
        }

        /// <summary>
        /// BoundEvent is the object that gets returned when the user gets an event object.  An
        /// BoundEvent tracks where the event was received from and is used to verify we get
        /// a proper add when dealing w/ statics events.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] // TODO: fix
        public class BoundEvent {
            private readonly ReflectedEvent/*!*/ _event;
            private readonly PythonType/*!*/ _ownerType;
            private readonly object _instance;

            public ReflectedEvent/*!*/ Event {
                get {
                    return _event;
                }
            }

            public BoundEvent(ReflectedEvent/*!*/ reflectedEvent, object instance, PythonType/*!*/ ownerType) {
                Assert.NotNull(reflectedEvent, ownerType);

                _event = reflectedEvent;
                _instance = instance;
                _ownerType = ownerType;
            }

            // this one's correct, InPlaceAdd is wrong but we still have some dependencies on the wrong name.
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2225:OperatorOverloadsHaveNamedAlternates")] // TODO: fix
            [SpecialName]
            public object op_AdditionAssignment(CodeContext/*!*/ context, object func) {
                return InPlaceAdd(context, func);
            }

            [SpecialName]
            public object InPlaceAdd(CodeContext/*!*/ context, object func) {
                if (func == null) {
                    throw PythonOps.TypeError("event addition expected callable object, got None");
                }

                MethodInfo add = _event.Info.GetAddMethod(true);
                if (add.IsStatic) {
                    if (_ownerType != DynamicHelpers.GetPythonTypeFromType(_event.Info.DeclaringType)) {
                        // mutating static event, only allow this from the type we're mutating, not sub-types
                        return new BadEventChange(_ownerType, _instance);
                    }
                }

                Delegate handler;
                HandlerList stubs;

                // we can add event directly (signature does match):
                if (_event.Info.EventHandlerType.IsAssignableFrom(func.GetType())) {
                    handler = (Delegate)func;
                    stubs = null;
                } else {
                    // create signature converting stub:
                    handler = BinderOps.GetDelegate(context.LanguageContext, func, _event.Info.EventHandlerType);
                    stubs = _event.GetStubList(_instance);
                }

                bool privateBinding = context.LanguageContext.DomainManager.Configuration.PrivateBinding;

                // wire the handler up:
                if (!add.DeclaringType.IsPublic) {
                    add = CompilerHelpers.GetCallableMethod(add, privateBinding);
                }

                if ((add.IsPublic && add.DeclaringType.IsPublic) || privateBinding) {
                    add.Invoke(_instance, new object[] { handler });
                } else {
                    throw new ArgumentTypeException("cannot add to private event");
                }

                if (stubs != null) {
                    // remember the stub so that we could search for it on removal:
                    stubs.AddHandler(func, handler);
                }

                return this;
            }

            [SpecialName]
            public object InPlaceSubtract(CodeContext/*!*/ context, object func) {
                Assert.NotNull(context);
                if (func == null) {
                    throw PythonOps.TypeError("event subtraction expected callable object, got None");
                }

                MethodInfo remove = _event.Info.GetRemoveMethod(true);
                if (remove.IsStatic) {
                    if (_ownerType != DynamicHelpers.GetPythonTypeFromType(_event.Info.DeclaringType)) {
                        // mutating static event, only allow this from the type we're mutating, not sub-types
                        return new BadEventChange(_ownerType, _instance);
                    }
                }

                bool privateBinding = context.LanguageContext.DomainManager.Configuration.PrivateBinding;

                if (!remove.DeclaringType.IsPublic) {
                    remove = CompilerHelpers.GetCallableMethod(remove, privateBinding);
                }

                bool isRemovePublic = remove.IsPublic && remove.DeclaringType.IsPublic;
                if (isRemovePublic || privateBinding) {

                    Delegate handler;

                    if (_event.Info.EventHandlerType.IsAssignableFrom(func.GetType())) {
                        handler = (Delegate)func;
                    } else {
                        handler = _event.GetStubList(_instance).RemoveHandler(context, func);
                    }

                    if (handler != null) {
                        remove.Invoke(_instance, new object[] { handler });
                    }
                } else {
                    throw new ArgumentTypeException("cannot subtract from private event");
                }

                return this;
            }
        }

        public void __set__(CodeContext context, object instance, object value) {
            TrySetValue(context, instance, DynamicHelpers.GetPythonType(instance), value);
        }

        public new void __delete__(CodeContext context, object instance) {
            TryDeleteValue(context, instance, DynamicHelpers.GetPythonType(instance));
        }

        #endregion

        #region Private Helpers

        private class BadEventChange {
            private readonly PythonType/*!*/ _ownerType;
            private readonly object _instance;

            public BadEventChange(PythonType/*!*/ ownerType, object instance) {
                _ownerType = ownerType;
                _instance = instance;
            }

            public PythonType Owner {
                get {
                    return _ownerType;
                }
            }

            public object Instance {
                get {
                    return _instance;
                }
            }
        }

        /// <summary>
        /// Holds on a list of delegates hooked to the event. 
        /// We need the list because we cannot enumerate the delegates hooked to CLR event and we need to do so in 
        /// handler removal (we need to do custom delegate comparison there). If BCL enables the enumeration we could remove this.
        /// </summary>
        private abstract class HandlerList {
            public abstract void AddHandler(object callableObject, Delegate/*!*/ handler);
            public abstract Delegate RemoveHandler(CodeContext/*!*/ context, object callableObject);
        }

#if !SILVERLIGHT
        private sealed class ComHandlerList : HandlerList {
            /// <summary>
            /// Storage for the handlers - a key value pair of the callable object and the delegate handler.
            /// 
            /// The delegate handler is closed over the callable object.  Therefore as long as the object is alive the
            /// delegate will stay alive and so will the callable object.  That means it's fine to have a weak reference
            /// to both of these objects.
            /// </summary>
            private readonly CopyOnWriteList<KeyValuePair<object, object>> _handlers = new CopyOnWriteList<KeyValuePair<object, object>>();

            public override void AddHandler(object callableObject, Delegate/*!*/ handler) {
                Assert.NotNull(handler);
                _handlers.Add(new KeyValuePair<object, object>(callableObject, handler));
            }

            public override Delegate RemoveHandler(CodeContext context, object callableObject) {
                Assert.NotNull(context);

                List<KeyValuePair<object, object>> copyOfHandlers = _handlers.GetCopyForRead();
                for (int i = copyOfHandlers.Count - 1; i >= 0; i--) {
                    object key = copyOfHandlers[i].Key;
                    object value = copyOfHandlers[i].Value;

                    if (PythonOps.EqualRetBool(context, key, callableObject)) {
                        Delegate handler = (Delegate)value;
                        _handlers.RemoveAt(i);
                        return handler;
                    }
                }

                return null;
            }
        }
#endif

        private sealed class NormalHandlerList : HandlerList {
            /// <summary>
            /// Storage for the handlers - a key value pair of the callable object and the delegate handler.
            /// 
            /// The delegate handler is closed over the callable object.  Therefore as long as the object is alive the
            /// delegate will stay alive and so will the callable object.  That means it's fine to have a weak reference
            /// to both of these objects.
            /// </summary>
            private readonly CopyOnWriteList<KeyValuePair<WeakReference/*!*/, WeakReference/*!*/>> _handlers = new CopyOnWriteList<KeyValuePair<WeakReference/*!*/, WeakReference/*!*/>>();

            public NormalHandlerList() {
            }

            public override void AddHandler(object callableObject, Delegate/*!*/ handler) {
                Assert.NotNull(handler);
                _handlers.Add(new KeyValuePair<WeakReference/*!*/, WeakReference/*!*/>(new WeakReference(callableObject), new WeakReference(handler)));
            }

            public override Delegate RemoveHandler(CodeContext/*!*/ context, object callableObject) {
                Assert.NotNull(context);

                List<KeyValuePair<WeakReference, WeakReference>> copyOfHandlers = _handlers.GetCopyForRead();
                for (int i = copyOfHandlers.Count - 1; i >= 0; i--) {
                    object key = copyOfHandlers[i].Key.Target;
                    object value = copyOfHandlers[i].Value.Target;

                    if (key != null && value != null && PythonOps.EqualRetBool(context, key, callableObject)) {
                        Delegate handler = (Delegate)value;
                        _handlers.RemoveAt(i);
                        return handler;
                    }
                }

                return null;
            }
        }

        private MissingMemberException/*!*/ ReadOnlyException(PythonType/*!*/ dt) {
            Assert.NotNull(dt);
            return new MissingMemberException(String.Format("attribute '{1}' of '{0}' object is read-only", dt.Name, SymbolTable.StringToId(_eventInfo.Name)));
        }

        #endregion

        #region ICodeFormattable Members

        public string/*!*/ __repr__(CodeContext/*!*/ context) {
            return string.Format("<event# {0} on {1}>", Info.Name, Info.DeclaringType.Name);
        }

        #endregion
    }
}
Back to Top