call_get.js (4355B)
1 "use strict"; 2 var cr = Object.create; 3 if (cr) { 4 var callerCache = cr(null); 5 var getterCache = cr(null); 6 callerCache[" size"] = getterCache[" size"] = 0; 7 } 8 9 module.exports = function(Promise) { 10 var util = require("./util"); 11 var canEvaluate = util.canEvaluate; 12 var isIdentifier = util.isIdentifier; 13 14 var getMethodCaller; 15 var getGetter; 16 if (!false) { 17 var makeMethodCaller = function (methodName) { 18 return new Function("ensureMethod", " \n\ 19 return function(obj) { \n\ 20 'use strict' \n\ 21 var len = this.length; \n\ 22 ensureMethod(obj, 'methodName'); \n\ 23 switch(len) { \n\ 24 case 1: return obj.methodName(this[0]); \n\ 25 case 2: return obj.methodName(this[0], this[1]); \n\ 26 case 3: return obj.methodName(this[0], this[1], this[2]); \n\ 27 case 0: return obj.methodName(); \n\ 28 default: \n\ 29 return obj.methodName.apply(obj, this); \n\ 30 } \n\ 31 }; \n\ 32 ".replace(/methodName/g, methodName))(ensureMethod); 33 }; 34 35 var makeGetter = function (propertyName) { 36 return new Function("obj", " \n\ 37 'use strict'; \n\ 38 return obj.propertyName; \n\ 39 ".replace("propertyName", propertyName)); 40 }; 41 42 var getCompiled = function(name, compiler, cache) { 43 var ret = cache[name]; 44 if (typeof ret !== "function") { 45 if (!isIdentifier(name)) { 46 return null; 47 } 48 ret = compiler(name); 49 cache[name] = ret; 50 cache[" size"]++; 51 if (cache[" size"] > 512) { 52 var keys = Object.keys(cache); 53 for (var i = 0; i < 256; ++i) delete cache[keys[i]]; 54 cache[" size"] = keys.length - 256; 55 } 56 } 57 return ret; 58 }; 59 60 getMethodCaller = function(name) { 61 return getCompiled(name, makeMethodCaller, callerCache); 62 }; 63 64 getGetter = function(name) { 65 return getCompiled(name, makeGetter, getterCache); 66 }; 67 } 68 69 function ensureMethod(obj, methodName) { 70 var fn; 71 if (obj != null) fn = obj[methodName]; 72 if (typeof fn !== "function") { 73 var message = "Object " + util.classString(obj) + " has no method '" + 74 util.toString(methodName) + "'"; 75 throw new Promise.TypeError(message); 76 } 77 return fn; 78 } 79 80 function caller(obj) { 81 var methodName = this.pop(); 82 var fn = ensureMethod(obj, methodName); 83 return fn.apply(obj, this); 84 } 85 Promise.prototype.call = function (methodName) { 86 var $_len = arguments.length;var args = new Array(Math.max($_len - 1, 0)); for(var $_i = 1; $_i < $_len; ++$_i) {args[$_i - 1] = arguments[$_i];}; 87 if (!false) { 88 if (canEvaluate) { 89 var maybeCaller = getMethodCaller(methodName); 90 if (maybeCaller !== null) { 91 return this._then( 92 maybeCaller, undefined, undefined, args, undefined); 93 } 94 } 95 } 96 args.push(methodName); 97 return this._then(caller, undefined, undefined, args, undefined); 98 }; 99 100 function namedGetter(obj) { 101 return obj[this]; 102 } 103 function indexedGetter(obj) { 104 var index = +this; 105 if (index < 0) index = Math.max(0, index + obj.length); 106 return obj[index]; 107 } 108 Promise.prototype.get = function (propertyName) { 109 var isIndex = (typeof propertyName === "number"); 110 var getter; 111 if (!isIndex) { 112 if (canEvaluate) { 113 var maybeGetter = getGetter(propertyName); 114 getter = maybeGetter !== null ? maybeGetter : namedGetter; 115 } else { 116 getter = namedGetter; 117 } 118 } else { 119 getter = indexedGetter; 120 } 121 return this._then(getter, undefined, undefined, propertyName, undefined); 122 }; 123 };