An odd InvalidProgramException

Sep 12, 2011 at 12:30 PM

I'm seeing an InvalidProgramException in UserDefinedFunction.CallLateBound() when I try to bind a javascript event binding/triggering method.

The offending js code is:

    trigger: function (event, data) {
        var handlers = this._events[event]
        // If the event doesn't exist, just return.
        if (!exists(handlers))
           return true;
 
        // Trigger all callbacks for this event.
        for (var i = 0; i < 1; ++i) {
            var callback = handlers[i];
            if (callback(data) === false)
                return false;
        }
 
        return true;
    }

I can make the problem go away by modifying the code to:
function hackhack(callback, data) {
    if (isFunction(callback))
        if (callback(data) === false)
            return false;
    return true;
}

// ....

    trigger: function (event, data) {
        var handlers = this._events[event]
        // If the event doesn't exist, just return.
        if (!exists(handlers))
            return true;
 
        // Trigger all callbacks for this event.
        for (var i = 0; i < 1; ++i) {
            var callback = handlers[i];
            hackhack(callback, data);
        }
 
        return true;
    }

But obviously I'd prefer to try to identify and fix the real problem. I'm running on WP7 - so that may be the culprit.

I'll keep digging, but I wonder if anyone has got any suggestions about where to dig?

Thanks

Stuart

Sep 14, 2011 at 1:06 AM

OK... got this reproducible now. I think it's something to do with returning from "for" loops within WP7 only.

e.g. this code runs:

                var _events = {};
                function exists( thing ) {
                    var a;
                    return thing !== a && thing !== null;
                }
                function trigger(event, data) {
                    var result = true;
                    var handlers = _events[event];

                   if (!exists(handlers))
                        return result;

                    // Trigger all callbacks for this event.
                    for (var i = 0; i < 1; ++i) {
                        var callback = handlers[i];
                        result = callback(data);
                        if (result === false) {
                            break;
                        }
                    }

                    return result;
                }    
                trigger('test');

but this code causes an invalid program exception:

                var _events = {};
                function exists( thing ) {
                    var a;
                    return thing !== a && thing !== null;
                }
                function trigger(event, data) {
                    var handlers = _events[event];

                   if (!exists(handlers))
                        return true;

                    // Trigger all callbacks for this event.
                    for (var i = 0; i < 1; ++i) {
                        var callback = handlers[i];
                        result = callback(data);
                        if (result === false) {
                            return false;
                        }
                    }

                    return true;
                }    
                trigger('test');