Skip to content

Commit

Permalink
Release v1.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
petkaantonov committed Feb 15, 2014
1 parent 2245c86 commit 4a951d5
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 92 deletions.
209 changes: 149 additions & 60 deletions browser/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? "," : "";

Expand All @@ -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){ " +
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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 ? "," : "";

Expand All @@ -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){ " +
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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");

Expand Down
Loading

0 comments on commit 4a951d5

Please sign in to comment.