private void AddProperty(ObjectInstance instance, string name, DelegateWrapper delegateWrapper) { JsValue getter = JsValue.FromObject(_engine.JintEngine, delegateWrapper); JsValue setter = JsValue.Null; instance.DefineOwnProperty(name, new PropertyDescriptor(getter, setter, true, false), true); }
/// <summary> /// Removes the last instance of delegate from the event (if it exists). Only removes events that were added from the current /// dispatcher thread (if they were added from one), so make sure to remove from the same thread that added. /// </summary> public void remove(TEvent value) { if (value == null) { return; } Dispatcher dispatcher = getDispatcherOrNull(); lock (_removeLock) // because events are intrinsically threadsafe, and dispatchers are thread-local, the only time this lock matters is when removing non-dispatcher events { EventHandler <TArgs> evt = _event; if (evt != null) { Delegate[] invList = evt.GetInvocationList(); for (int i = invList.Length - 1; i >= 0; i--) // Need to go backwards since that's what event -= something does. { DelegateWrapper wrapper = (DelegateWrapper)invList[i].Target; // need to use Equals instead of == for delegates if (wrapper.handler.Equals(value) && wrapper.dispatcher == dispatcher) { _event -= wrapper.invoke; return; } } } } }
internal void AddDelegate(Delegate d) { int count = 0; if (_delegateWrappers != null) { count = _delegateWrappers.Length; } for (int i = 0; i < count; i++) { if (_delegateWrappers[i].Delegate.GetType() == d.GetType()) { _delegateWrappers[i].Delegate = Delegate.Combine(_delegateWrappers[i].Delegate, d); return; } } DelegateWrapper [] newDelegateWrappers = new DelegateWrapper[count + 1]; if (count > 0) { _delegateWrappers.CopyTo(newDelegateWrappers, 0); } DelegateWrapper wrapper = new DelegateWrapper(d); newDelegateWrappers[count] = wrapper; _delegateWrappers = newDelegateWrappers; }
public void RemoveDelegates(Func <Delegate, bool> condition) { lock (_delegateWrappers) { // Find delegate wrapper indexes. Iterate in reverse such that the list to remove is sorted by high to low index. List <int> toRemove = new List <int>(); for (int i = _delegateWrappers.Count - 1; i >= 0; i--) { DelegateWrapper wrapper = _delegateWrappers[i]; Delegate[] invocationList = wrapper.Delegate.GetInvocationList(); foreach (Delegate delegateMaybe in invocationList) { if (condition(delegateMaybe)) { Delegate?newDelegate = Delegate.Remove(wrapper !.Delegate, delegateMaybe); if (newDelegate != null) { wrapper.Delegate = newDelegate; } else { toRemove.Add(i); } } } } foreach (int idx in toRemove) { _delegateWrappers.RemoveAt(idx); } } }
private static object InvokeInternal(Delegate d, object[] args) { var wrapper = new DelegateWrapper(d, args); invoker.InvokeOnMainThread(wrapper.Invoke); return(wrapper.Result); }
private JsValue ScheduleTimeout(JsValue[] args, bool repeat) { if (args.Length == 0) { return(JsValue.Undefined); } JsValue vCb = args[0]; JsValue vTimeout = args.Length > 1 ? args[1] : new JsValue(0); JsValue[] cbArgs = args.Length > 2 ? args.Skip(2).ToArray() : new JsValue[0]; ICallable cb; if (vCb.IsString()) { cb = new DelegateWrapper(_engine, new Action(() => _engine.Eval.Call(JsValue.Undefined, cbArgs))); } else { cb = vCb.TryCast <ICallable>(); } if (cb == null) { throw new JavaScriptException(_engine.TypeError, "argument 1 requires a function or a string"); } int timeout = TypeConverter.ToInt32(vTimeout); //Logger.Debug("timeout {0}", timeout); //Logger.Debug("val {0}", vTimeout); Item item = new Item(cb, timeout, cbArgs, repeat); return(Schedule(item)); }
public IEval Finally(IEval child) { Func <String, bool> f = (word) => { return(base.Disjunction(child, base.Next(Finally(child))).Eval(word)); }; return(DelegateWrapper.WrapAs <IEval> (f)); }
public virtual ISubscription CreateSubscription( object subscriber, DelegateWrapper delegateWrapper, IHandler handler, IList <ISubscriptionMatcher> subscriptionMatchers) { return(new Subscription(subscriber, delegateWrapper, handler, subscriptionMatchers, this.ExtensionHost)); }
public IEval Variable(string name) { Func <String, bool> f = (word) => { return(word.Length > 0 && word[0].ToString().Equals(name)); }; return(DelegateWrapper.WrapAs <IEval>(f)); }
public IEval Until(IEval left, IEval right) { Func <String, bool> f = (word) => { return(Disjunction(right, Conjunction(left, (Next(Until(left, right))))).Eval(word)); }; return(DelegateWrapper.WrapAs <IEval>(f)); }
public IEval Proposition(bool val) { Func <String, bool> f = (word) => { return(val); }; return(DelegateWrapper.WrapAs <IEval>(f)); }
public IEval Next(IEval child) { Func <String, bool> f = (word) => { return(word.Length != 0 && child.Eval(word.Substring(1))); }; return(DelegateWrapper.WrapAs <IEval>(f)); }
public IEval Negation(IEval child) { Func <String, bool> f = (word) => { return(!child.Eval(word)); }; return(DelegateWrapper.WrapAs <IEval>(f)); }
private CliApiBuilder AddProperty(string name, DelegateWrapper delegateWrapper) { JsValue getter = JsValue.FromObject(_engine, delegateWrapper); JsValue setter = JsValue.Null; _instance.DefineOwnProperty(name, new PropertyDescriptor(getter, setter, true, false), true); return(this); }
public void LoadModule(CliModuleBase module) { var cliModuleAttribute = module.GetType().GetCustomAttribute <CliModuleAttribute>(); CliConsole.WriteLine($"module ({cliModuleAttribute.ModuleName})"); ModuleNames.Add(cliModuleAttribute.ModuleName); MethodsByModules[cliModuleAttribute.ModuleName] = new List <string>(); var methods = module.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (MethodInfo methodInfo in methods.OrderBy(m => m.Name)) { var cliProperty = methodInfo.GetCustomAttribute <CliPropertyAttribute>(); var cliFunction = methodInfo.GetCustomAttribute <CliFunctionAttribute>(); bool isProperty = cliProperty != null; string objectName = cliProperty?.ObjectName ?? cliFunction?.ObjectName; string itemName = cliProperty?.PropertyName ?? cliFunction?.FunctionName; if (objectName == null) { throw new InvalidDataException($"Method {methodInfo.Name} of {module.GetType().Name} should be decorated with one of {nameof(CliPropertyAttribute)} or {nameof(CliFunctionAttribute)}"); } ObjectInstance instance; if (!_objects.ContainsKey(objectName)) { instance = _engine.JintEngine.Object.Construct(Arguments.Empty); _engine.JintEngine.SetValue(objectName, instance); _objects[objectName] = instance; } instance = _objects[objectName]; var @delegate = CreateDelegate(methodInfo, module); DelegateWrapper nativeDelegate = new DelegateWrapper(_engine.JintEngine, @delegate); if (isProperty) { CliConsole.WriteKeyword($" {objectName}"); CliConsole.WriteLine($".{itemName}"); MethodsByModules[objectName].Add(itemName); AddProperty(instance, itemName, nativeDelegate); } else { CliConsole.WriteKeyword($" {objectName}"); CliConsole.WriteLine($".{itemName}({string.Join(", ", methodInfo.GetParameters().Select(p => p.Name))})"); MethodsByModules[objectName].Add(itemName + "("); AddMethod(instance, itemName, nativeDelegate); } } CliConsole.WriteLine(); }
public DelegateWrapper Wrap(Delegate d) { DelegateWrapper wrapper = null; RunOnJsThread(() => { wrapper = new DelegateWrapper(jsEngine, d); }); return(wrapper); }
public void NotSameInterceptor() { Func <int, int, int> calcFunc = (a, b) => a + b; var calcService = DelegateWrapper.GetCalcInterfaceFromDelegate(calcFunc); var intrc1 = calcService as IProxyTargetAccessor; Func <int, int, int> calcFunc2 = (a, b) => a * b; var calcService2 = DelegateWrapper.GetCalcInterfaceFromDelegate(calcFunc2); var intrc2 = calcService2 as IProxyTargetAccessor; Assert.NotSame(intrc1, intrc2); }
internal void RemoveDelegate(Delegate d) { int count = _delegateWrappers.Length; int removeIdx = -1; for (int i = 0; i < count; i++) { if (_delegateWrappers[i].Delegate.GetType() == d.GetType()) { removeIdx = i; break; } } if (removeIdx < 0) { return; } Delegate newDelegate = Delegate.Remove(_delegateWrappers[removeIdx].Delegate, d); if (newDelegate != null) { _delegateWrappers[removeIdx].Delegate = newDelegate; return; } // now remove the found entry from the _delegates array if (count == 1) { _delegateWrappers = null; return; } DelegateWrapper [] newDelegateWrappers = new DelegateWrapper[count - 1]; int j = 0; while (j < removeIdx) { newDelegateWrappers[j] = _delegateWrappers[j]; j++; } while (j < count - 1) { newDelegateWrappers[j] = _delegateWrappers[j + 1]; j++; } _delegateWrappers = newDelegateWrappers; }
private void AddSubscription(string topic, object subscriber, IHandler handler, IEnumerable <ISubscriptionMatcher> matchers, MethodInfo methodInfo) { IEventTopic eventTopic = this.eventTopicHost.GetEventTopic(topic); handler.Initialize(subscriber, methodInfo, this.extensionHost); DelegateWrapper delegateWrapper = GetDelegateWrapper(methodInfo); ISubscription subscription = this.factory.CreateSubscription( subscriber, delegateWrapper, handler, matchers != null ? new List <ISubscriptionMatcher>(matchers) : new List <ISubscriptionMatcher>()); eventTopic.AddSubscription(subscription); }
public void AddDelegate(Delegate d, bool wrapArgs = false) { lock (_delegateWrappers) { // Update an existing delegate wrapper foreach (DelegateWrapper wrapper in _delegateWrappers) { if (wrapper.Delegate.GetType() == d.GetType() && wrapper.WrapArgs == wrapArgs) { wrapper.Delegate = Delegate.Combine(wrapper.Delegate, d); return; } } var newWrapper = new DelegateWrapper(d, wrapArgs); _delegateWrappers.Add(newWrapper); } }
/// <summary> /// Adds a subscription. Use this to register subscriptions by code instead of using attributes. /// </summary> /// <typeparam name="TEventArgs">The type of the event arguments.</typeparam> /// <param name="topic">The topic.</param> /// <param name="subscriber">The subscriber.</param> /// <param name="handlerMethod">The handler method.</param> /// <param name="handler">The handler.</param> /// <param name="matchers">The subscription matchers.</param> public void AddSubscription <TEventArgs>(string topic, object subscriber, EventHandler <TEventArgs> handlerMethod, IHandler handler, params ISubscriptionMatcher[] matchers) where TEventArgs : EventArgs { Ensure.ArgumentNotNull(handler, "handler"); Ensure.ArgumentNotNull(handlerMethod, "handlerMethod"); IEventTopic eventTopic = this.eventTopicHost.GetEventTopic(topic); handler.Initialize(subscriber, handlerMethod.Method, this.extensionHost); DelegateWrapper delegateWrapper = GetDelegateWrapper(handlerMethod.Method); ISubscription subscription = this.factory.CreateSubscription( subscriber, delegateWrapper, handler, matchers != null ? new List <ISubscriptionMatcher>(matchers) : new List <ISubscriptionMatcher>()); eventTopic.AddSubscription(subscription); }
public void AddDelegate(Delegate d) { lock (_delegateWrappers) { // Update an existing delegate wrapper foreach (DelegateWrapper wrapper in _delegateWrappers) { if (wrapper.Delegate.GetType() == d.GetType()) { wrapper.Delegate = Delegate.Combine(wrapper.Delegate, d) !; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 return; } } var newWrapper = new DelegateWrapper(d); _delegateWrappers.Add(newWrapper); } }
public void AddDelegate(Delegate d) { lock (_delegateWrappers) { // Update an existing delegate wrapper foreach (DelegateWrapper wrapper in _delegateWrappers) { if (wrapper.Delegate.GetType() == d.GetType()) { wrapper.Delegate = Delegate.Combine(wrapper.Delegate, d) !; // TODO-NULLABLE: Remove ! when [NotNullIfNotNull] respected return; } } var newWrapper = new DelegateWrapper(d); _delegateWrappers.Add(newWrapper); } }
private void RegisterPropertySubscriptions(object item, ScanResult scanResult) { foreach (PropertySubscriptionScanResult propertySubscription in scanResult.Subscription) { IEventTopic eventTopic = this.eventTopicHost.GetEventTopic(propertySubscription.Topic); var subscriptionMatchers = from subscriptionMatcherType in propertySubscription.SubscriptionMatcherTypes select this.factory.CreateSubscriptionMatcher(subscriptionMatcherType); var handler = this.factory.CreateHandler(propertySubscription.HandlerType); handler.Initialize(item, propertySubscription.Method, this.extensionHost); DelegateWrapper delegateWrapper = GetDelegateWrapper(propertySubscription.Method); ISubscription subscription = this.factory.CreateSubscription(item, delegateWrapper, handler, subscriptionMatchers.ToList()); eventTopic.AddSubscription(subscription); } }
public void SameInterceptor() { Func <int, int, int> calcFunc = (a, b) => a + b; var container = new WindsorContainer(); container.Register(Component.For <ICalcInterface>().UsingFactoryMethod((kernel, _, context) => DelegateWrapper.GetCalcInterfaceFromDelegate(calcFunc), true)); var calcService = container.Resolve <ICalcInterface>(); var intrc1 = calcService as IProxyTargetAccessor; var calcService2 = container.Resolve <ICalcInterface>(); var intrc2 = calcService2 as IProxyTargetAccessor; Assert.Equal( container.Kernel.GetHandler(typeof(ICalcInterface)).ComponentModel.GetLifestyleDescriptionLong(), "Undefined (default lifestyle Singleton will be used)"); Assert.Same(intrc1, intrc2); }
internal void RemoveDelegate(Delegate d) { int length = this._delegateWrappers.Length; int index = -1; for (int i = 0; i < length; i++) { if (this._delegateWrappers[i].Delegate.GetType() == d.GetType()) { index = i; break; } } if (index >= 0) { Delegate delegate2 = Delegate.Remove(this._delegateWrappers[index].Delegate, d); if (delegate2 != null) { this._delegateWrappers[index].Delegate = delegate2; } else if (length == 1) { this._delegateWrappers = null; } else { DelegateWrapper[] wrapperArray = new DelegateWrapper[length - 1]; int num4 = 0; while (num4 < index) { wrapperArray[num4] = this._delegateWrappers[num4]; num4++; } while (num4 < (length - 1)) { wrapperArray[num4] = this._delegateWrappers[num4 + 1]; num4++; } this._delegateWrappers = wrapperArray; } } }
public void CallMethodOnInterface() { Func <int, int, int> calcFunc = (a, b) => a + b; var calcService = DelegateWrapper.GetCalcInterfaceFromDelegate(calcFunc); var result = calcService.CalcExpression(5, 2); Assert.Equal(7, result); Func <int, int, int> calcFunc2 = (a, b) => a * b; var calcService2 = DelegateWrapper.GetCalcInterfaceFromDelegate(calcFunc2); var result2 = calcService2.CalcExpression(5, 2); Assert.Equal(10, result2); Func <int, int, int> calcFunc3 = (a, b) => a % b; var calcService3 = DelegateWrapper.GetCalcInterfaceFromDelegate(calcFunc3); var result3 = calcService3.CalcExpression(5, 2); Assert.Equal(1, result3); }
public void RemoveDelegate(Delegate d, bool wrapArgs = false) { lock (_delegateWrappers) { // Find delegate wrapper index int removeIdx = -1; DelegateWrapper?wrapper = null; for (int i = 0; i < _delegateWrappers.Count; i++) { DelegateWrapper wrapperMaybe = _delegateWrappers[i]; if (wrapperMaybe.Delegate.GetType() == d.GetType() && wrapperMaybe.WrapArgs == wrapArgs) { removeIdx = i; wrapper = wrapperMaybe; break; } } if (removeIdx < 0) { // Not present in collection return; } // Update wrapper or remove from collection Delegate?newDelegate = Delegate.Remove(wrapper !.Delegate, d); if (newDelegate != null) { wrapper.Delegate = newDelegate; } else { _delegateWrappers.RemoveAt(removeIdx); } } }
internal void AddDelegate(Delegate d) { int index = 0; if (this._delegateWrappers != null) { index = this._delegateWrappers.Length; } for (int i = 0; i < index; i++) { if (this._delegateWrappers[i].Delegate.GetType() == d.GetType()) { this._delegateWrappers[i].Delegate = Delegate.Combine(this._delegateWrappers[i].Delegate, d); return; } } DelegateWrapper[] array = new DelegateWrapper[index + 1]; if (index > 0) { this._delegateWrappers.CopyTo(array, 0); } array[index] = new DelegateWrapper(d); this._delegateWrappers = array; }
private static void AddMethod(ObjectInstance instance, string name, DelegateWrapper delegateWrapper) { instance.FastAddProperty(name, delegateWrapper, true, false, true); }
internal void RemoveDelegate(Delegate d) { int count = _delegateWrappers.Length; int removeIdx = -1; for (int i = 0; i < count; i++) { if (_delegateWrappers[i].Delegate.GetType() == d.GetType()) { removeIdx = i; break; } } if (removeIdx < 0) return; Delegate newDelegate = Delegate.Remove(_delegateWrappers[removeIdx].Delegate, d); if (newDelegate != null) { _delegateWrappers[removeIdx].Delegate = newDelegate; return; } // now remove the found entry from the _delegates array if (count == 1) { _delegateWrappers = null; return; } DelegateWrapper [] newDelegateWrappers = new DelegateWrapper[count - 1]; int j = 0; while (j < removeIdx) { newDelegateWrappers[j] = _delegateWrappers[j]; j++; } while (j < count-1) { newDelegateWrappers[j] = _delegateWrappers[j + 1]; j++; } _delegateWrappers = newDelegateWrappers; }
private static object InvokeInternal(Delegate d, object[] args) { var wrapper = new DelegateWrapper(d, args); invoker.InvokeOnMainThread(wrapper.Invoke); return wrapper.Result; }