From 4a951d562c339c1d4d192df41c422c864efd2b32 Mon Sep 17 00:00:00 2001 From: Petka Antonov Date: Sat, 15 Feb 2014 14:57:51 +0200 Subject: [PATCH] Release v1.0.5 --- browser/bundle.js | 209 +++++++++++++++++++++++++++++------------ js/browser/bluebird.js | 96 +++++++++++++------ package.json | 2 +- 3 files changed, 215 insertions(+), 92 deletions(-) diff --git a/browser/bundle.js b/browser/bundle.js index dd2272fea..e0c255465 100644 --- a/browser/bundle.js +++ b/browser/bundle.js @@ -6878,11 +6878,48 @@ var inheritedMethods = (function() { } })(); -function makeNodePromisifiedEval(callback, receiver, originalName) { - function getCall(count) { +function switchCaseArgumentOrder(likelyArgumentCount) { + var ret = [likelyArgumentCount]; + var min = Math.max(0, likelyArgumentCount - 1 - 5); + for(var i = likelyArgumentCount - 1; i >= min; --i) { + if (i === likelyArgumentCount) continue; + ret.push(i); + } + for(var i = likelyArgumentCount + 1; i <= 5; ++i) { + ret.push(i); + } + return ret; +} + +function parameterDeclaration(parameterCount) { + var ret = new Array(parameterCount); + for(var i = 0; i < ret.length; ++i) { + ret[i] = "_arg" + i; + } + return ret.join(", "); +} + +function parameterCount(fn) { + if (typeof fn.length === "number") { + return Math.max(Math.min(fn.length, 1023 + 1), 0); + } + return 0; +} + +function makeNodePromisifiedEval(callback, receiver, originalName, fn) { + ASSERT(((typeof fn) === "function"), + "typeof fn === \u0022function\u0022"); + var newParameterCount = Math.max(0, parameterCount(fn) - 1); + var argumentOrder = switchCaseArgumentOrder(newParameterCount); + + var callbackName = (typeof originalName === "string" ? + originalName + "Async" : + "promisified"); + + function generateCallForArgumentCount(count) { var args = new Array(count); for (var i = 0, len = args.length; i < len; ++i) { - args[i] = "a" + (i+1); + args[i] = "arguments[" + i + "]"; } var comma = count > 0 ? "," : ""; @@ -6900,42 +6937,40 @@ function makeNodePromisifiedEval(callback, receiver, originalName) { "break;"; } - function getArgs() { - return "var args = new Array(len + 1);" + - "var i = 0;" + - "for (var i = 0; i < len; ++i) { " + - " args[i] = arguments[i];" + - "}" + - "args[i] = fn;"; - } + function generateArgumentSwitchCase() { + var ret = ""; + for(var i = 0; i < argumentOrder.length; ++i) { + ret += "case " + argumentOrder[i] +":" + + generateCallForArgumentCount(argumentOrder[i]); + } + ret += "default: var args = new Array(len + 1);" + + "var i = 0;" + + "for (var i = 0; i < len; ++i) { " + + " args[i] = arguments[i];" + + "}" + + "args[i] = fn;" + - var callbackName = (typeof originalName === "string" ? - originalName + "Async" : - "promisified"); + (typeof callback === "string" + ? "this['" + callback + "'].apply(" + : "callback.apply(") + + + (receiver === THIS ? "this" : "receiver") + + ", args); break;"; + return ret; + } return new Function("Promise", "callback", "receiver", "withAppended", "maybeWrapAsError", "nodebackForPromise", "INTERNAL", "var ret = function " + callbackName + - "(a1, a2, a3, a4, a5) {\"use strict\";" + + "(" + parameterDeclaration(newParameterCount) + ") {\"use strict\";" + "var len = arguments.length;" + "var promise = new Promise(INTERNAL);"+ "promise._setTrace(" + callbackName + ", void 0);" + "var fn = nodebackForPromise(promise);"+ - "try{" + + "try {" + "switch(len) {" + - "case 1:" + getCall(1) + - "case 2:" + getCall(2) + - "case 3:" + getCall(3) + - "case 0:" + getCall(0) + - "case 4:" + getCall(4) + - "case 5:" + getCall(5) + - "default: " + getArgs() + (typeof callback === "string" - ? "this['" + callback + "'].apply(" - : "callback.apply(" - ) + - (receiver === THIS ? "this" : "receiver") + - ", args); break;" + + generateArgumentSwitchCase() + "}" + "}" + "catch(e){ " + @@ -6991,13 +7026,14 @@ function _promisify(callback, receiver, isAll) { var promisifiedKey = key + "Async"; notEnumerableProp(callback, originalKey, fn); callback[promisifiedKey] = - makeNodePromisified(originalKey, THIS, key); + makeNodePromisified(originalKey, THIS, + key, fn); } if (methods.length > 16) f.prototype = callback; return callback; } else { - return makeNodePromisified(callback, receiver, void 0); + return makeNodePromisified(callback, receiver, void 0, callback); } } @@ -10968,11 +11004,46 @@ var inheritedMethods = (function() { } })(); -function makeNodePromisifiedEval(callback, receiver, originalName) { - function getCall(count) { +function switchCaseArgumentOrder(likelyArgumentCount) { + var ret = [likelyArgumentCount]; + var min = Math.max(0, likelyArgumentCount - 1 - 5); + for(var i = likelyArgumentCount - 1; i >= min; --i) { + if (i === likelyArgumentCount) continue; + ret.push(i); + } + for(var i = likelyArgumentCount + 1; i <= 5; ++i) { + ret.push(i); + } + return ret; +} + +function parameterDeclaration(parameterCount) { + var ret = new Array(parameterCount); + for(var i = 0; i < ret.length; ++i) { + ret[i] = "_arg" + i; + } + return ret.join(", "); +} + +function parameterCount(fn) { + if (typeof fn.length === "number") { + return Math.max(Math.min(fn.length, 1023 + 1), 0); + } + return 0; +} + +function makeNodePromisifiedEval(callback, receiver, originalName, fn) { + var newParameterCount = Math.max(0, parameterCount(fn) - 1); + var argumentOrder = switchCaseArgumentOrder(newParameterCount); + + var callbackName = (typeof originalName === "string" ? + originalName + "Async" : + "promisified"); + + function generateCallForArgumentCount(count) { var args = new Array(count); for (var i = 0, len = args.length; i < len; ++i) { - args[i] = "a" + (i+1); + args[i] = "arguments[" + i + "]"; } var comma = count > 0 ? "," : ""; @@ -10990,42 +11061,40 @@ function makeNodePromisifiedEval(callback, receiver, originalName) { "break;"; } - function getArgs() { - return "var args = new Array(len + 1);" + - "var i = 0;" + - "for (var i = 0; i < len; ++i) { " + - " args[i] = arguments[i];" + - "}" + - "args[i] = fn;"; - } + function generateArgumentSwitchCase() { + var ret = ""; + for(var i = 0; i < argumentOrder.length; ++i) { + ret += "case " + argumentOrder[i] +":" + + generateCallForArgumentCount(argumentOrder[i]); + } + ret += "default: var args = new Array(len + 1);" + + "var i = 0;" + + "for (var i = 0; i < len; ++i) { " + + " args[i] = arguments[i];" + + "}" + + "args[i] = fn;" + - var callbackName = (typeof originalName === "string" ? - originalName + "Async" : - "promisified"); + (typeof callback === "string" + ? "this['" + callback + "'].apply(" + : "callback.apply(") + + + (receiver === THIS ? "this" : "receiver") + + ", args); break;"; + return ret; + } return new Function("Promise", "callback", "receiver", "withAppended", "maybeWrapAsError", "nodebackForPromise", "INTERNAL", "var ret = function " + callbackName + - "(a1, a2, a3, a4, a5) {\"use strict\";" + + "(" + parameterDeclaration(newParameterCount) + ") {\"use strict\";" + "var len = arguments.length;" + "var promise = new Promise(INTERNAL);"+ "promise._setTrace(" + callbackName + ", void 0);" + "var fn = nodebackForPromise(promise);"+ - "try{" + + "try {" + "switch(len) {" + - "case 1:" + getCall(1) + - "case 2:" + getCall(2) + - "case 3:" + getCall(3) + - "case 0:" + getCall(0) + - "case 4:" + getCall(4) + - "case 5:" + getCall(5) + - "default: " + getArgs() + (typeof callback === "string" - ? "this['" + callback + "'].apply(" - : "callback.apply(" - ) + - (receiver === THIS ? "this" : "receiver") + - ", args); break;" + + generateArgumentSwitchCase() + "}" + "}" + "catch(e){ " + @@ -11079,13 +11148,14 @@ function _promisify(callback, receiver, isAll) { var promisifiedKey = key + "Async"; notEnumerableProp(callback, originalKey, fn); callback[promisifiedKey] = - makeNodePromisified(originalKey, THIS, key); + makeNodePromisified(originalKey, THIS, + key, fn); } if (methods.length > 16) f.prototype = callback; return callback; } else { - return makeNodePromisified(callback, receiver, void 0); + return makeNodePromisified(callback, receiver, void 0, callback); } } @@ -27848,6 +27918,25 @@ describe("RejectionError wrapping", function() { }); }); +if (typeof function(c){}.length === "number") { + describe("arity", function() { + specify("should be original - 1", function(done) { + var fn = function(a, b, c, callback) {}; + + assert.equal(Promise.promisify(fn).length, 3); + + var o = { + fn: function(a, b, c, callback) { + + } + }; + assert.equal(Promise.promisifyAll(o).fnAsync.length, 3); + + done(); + }) + }) +} + },{"../../js/debug/bluebird.js":20,"assert":2}],152:[function(require,module,exports){ var assert = require("assert"); diff --git a/js/browser/bluebird.js b/js/browser/bluebird.js index 168f51503..8a1d26c0f 100644 --- a/js/browser/bluebird.js +++ b/js/browser/bluebird.js @@ -1,5 +1,5 @@ /** - * bluebird build version 1.0.4 + * bluebird build version 1.0.5 * Features enabled: core, timers, race, any, call_get, filter, generators, map, nodeify, promisify, props, reduce, settle, some, progress, cancel, synchronous_inspection */ /** @@ -3451,11 +3451,46 @@ var inheritedMethods = (function() { } })(); -function makeNodePromisifiedEval(callback, receiver, originalName) { - function getCall(count) { +function switchCaseArgumentOrder(likelyArgumentCount) { + var ret = [likelyArgumentCount]; + var min = Math.max(0, likelyArgumentCount - 1 - 5); + for(var i = likelyArgumentCount - 1; i >= min; --i) { + if (i === likelyArgumentCount) continue; + ret.push(i); + } + for(var i = likelyArgumentCount + 1; i <= 5; ++i) { + ret.push(i); + } + return ret; +} + +function parameterDeclaration(parameterCount) { + var ret = new Array(parameterCount); + for(var i = 0; i < ret.length; ++i) { + ret[i] = "_arg" + i; + } + return ret.join(", "); +} + +function parameterCount(fn) { + if (typeof fn.length === "number") { + return Math.max(Math.min(fn.length, 1023 + 1), 0); + } + return 0; +} + +function makeNodePromisifiedEval(callback, receiver, originalName, fn) { + var newParameterCount = Math.max(0, parameterCount(fn) - 1); + var argumentOrder = switchCaseArgumentOrder(newParameterCount); + + var callbackName = (typeof originalName === "string" ? + originalName + "Async" : + "promisified"); + + function generateCallForArgumentCount(count) { var args = new Array(count); for (var i = 0, len = args.length; i < len; ++i) { - args[i] = "a" + (i+1); + args[i] = "arguments[" + i + "]"; } var comma = count > 0 ? "," : ""; @@ -3473,42 +3508,40 @@ function makeNodePromisifiedEval(callback, receiver, originalName) { "break;"; } - function getArgs() { - return "var args = new Array(len + 1);" + - "var i = 0;" + - "for (var i = 0; i < len; ++i) { " + - " args[i] = arguments[i];" + - "}" + - "args[i] = fn;"; - } + function generateArgumentSwitchCase() { + var ret = ""; + for(var i = 0; i < argumentOrder.length; ++i) { + ret += "case " + argumentOrder[i] +":" + + generateCallForArgumentCount(argumentOrder[i]); + } + ret += "default: var args = new Array(len + 1);" + + "var i = 0;" + + "for (var i = 0; i < len; ++i) { " + + " args[i] = arguments[i];" + + "}" + + "args[i] = fn;" + - var callbackName = (typeof originalName === "string" ? - originalName + "Async" : - "promisified"); + (typeof callback === "string" + ? "this['" + callback + "'].apply(" + : "callback.apply(") + + + (receiver === THIS ? "this" : "receiver") + + ", args); break;"; + return ret; + } return new Function("Promise", "callback", "receiver", "withAppended", "maybeWrapAsError", "nodebackForPromise", "INTERNAL", "var ret = function " + callbackName + - "(a1, a2, a3, a4, a5) {\"use strict\";" + + "(" + parameterDeclaration(newParameterCount) + ") {\"use strict\";" + "var len = arguments.length;" + "var promise = new Promise(INTERNAL);"+ "promise._setTrace(" + callbackName + ", void 0);" + "var fn = nodebackForPromise(promise);"+ - "try{" + + "try {" + "switch(len) {" + - "case 1:" + getCall(1) + - "case 2:" + getCall(2) + - "case 3:" + getCall(3) + - "case 0:" + getCall(0) + - "case 4:" + getCall(4) + - "case 5:" + getCall(5) + - "default: " + getArgs() + (typeof callback === "string" - ? "this['" + callback + "'].apply(" - : "callback.apply(" - ) + - (receiver === THIS ? "this" : "receiver") + - ", args); break;" + + generateArgumentSwitchCase() + "}" + "}" + "catch(e){ " + @@ -3562,13 +3595,14 @@ function _promisify(callback, receiver, isAll) { var promisifiedKey = key + "Async"; notEnumerableProp(callback, originalKey, fn); callback[promisifiedKey] = - makeNodePromisified(originalKey, THIS, key); + makeNodePromisified(originalKey, THIS, + key, fn); } if (methods.length > 16) f.prototype = callback; return callback; } else { - return makeNodePromisified(callback, receiver, void 0); + return makeNodePromisified(callback, receiver, void 0, callback); } } diff --git a/package.json b/package.json index 95a2b213e..e9a124c10 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "bluebird", "description": "Full featured Promises/A+ implementation with exceptionally good performance", - "version": "1.0.4", + "version": "1.0.5", "keywords": [ "promise", "performance",