Description
Currently, when passing a function as an argument to another method a ss.Delegate
object is created for each call. This will cause an unpredictable behavior when comparing methods received as a parameter. For example, take the fallowing method from an Event
class.
public void Subscribe(EventHandler handler, Nullable<Boolean> unique) {
if (unique == true && this._handlers.Contains(handler))
return;
this.._handlers.Add(handler);
}
the method adds another function to a list which will be used later when firing the event. This compiles to :
subscribe: function GLSharp_Core_Event$subscribe(handler, unique) {
if (!!unique && this._handlers.contains(handler)) {
return;
}
this._handlers.add(handler);
}
The fallowing line is an example of calling the Subscribe
method:
test.Subscribe(Handler, false);
Which compiles into:
test.subscribe(ss.Delegate.create(this, this._handler), false);
As you can see, the condition this._handlers.Contains(handler)
always evaluates to false, because the list does not contain the actual functions but the ss.Delegate
wrappers, which are created for every call of the Subscribe
method. Currently, One workaround that I can think of is to create a compare method:
private Boolean MethodsEqual(EventHandler m1, EventHandler m2) {
return (bool) Script.Literal("(m1._targets[0] == m2._targets[0]) && (m1._targets[1] == m2._targets[1])", null);
}