diff --git a/lib/class-factory.js b/lib/class-factory.js index c066ad4c..4e45b107 100644 --- a/lib/class-factory.js +++ b/lib/class-factory.js @@ -1386,30 +1386,26 @@ dispatcherPrototype = Object.create(Function.prototype, { invoke: { value (receiver, args) { const overloads = this._o; + const methodsThatCanBeInvoked = overloads.filter(method => method.canInvokeWith(args)); + if (methodsThatCanBeInvoked.length === 0) { + throwOverloadError(this.methodName, this.overloads, 'argument types do not match any of:'); + } else if (methodsThatCanBeInvoked.length > 1) { + throwIfDispatcherAmbiguous(this); + } + const method = methodsThatCanBeInvoked[0]; const isInstance = receiver.$h !== null; + if (method.type === INSTANCE_METHOD && !isInstance) { + const name = this.methodName; - for (let i = 0; i !== overloads.length; i++) { - const method = overloads[i]; - - if (!method.canInvokeWith(args)) { - continue; - } - - if (method.type === INSTANCE_METHOD && !isInstance) { - const name = this.methodName; - - if (name === 'toString') { - return ``; - } - - throw new Error(name + ': cannot call instance method without an instance'); + if (name === 'toString') { + return ``; } - return method.apply(receiver, args); + throw new Error(name + ': cannot call instance method without an instance'); } - throwOverloadError(this.methodName, this.overloads, 'argument types do not match any of:'); + return method.apply(receiver, args); } } });