all files / javascript/ software.bytepushers.utils.Reflection.js

93.39% Statements 113/121
69.81% Branches 37/53
81.25% Functions 13/16
93.39% Lines 113/121
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253                                                                                                            12×                                                                                                                                                                            
/**
 * Created by tonte on 8/1/16.
 */
/*global window, BytePushers */
/*jslint regexp: true*/
(function (BytePushers) {
    "use strict";
    var EVAL_IS_BAD__AVOID_THIS = eval,
        FUNCTION_CONSTRUCTOR_IS_BAD__AVOID_THIS = Function;
 
    BytePushers = BytePushers || {};
    BytePushers.util = BytePushers.util || BytePushers.namespace("software.bytepushers.util");
    BytePushers.util.Reflection = function () {
        var getFunctionArguments = function (func) {
 
                var functionArgumentMatch = func.toString().match(/\(.+?(?=\))\)/),
                    functionArguments = functionArgumentMatch[0].substring(1, functionArgumentMatch[0].length - 1).split(",").map(function (arg) {
                        // Ensure no inline comments are parsed and trim the whitespace.
                        return arg.replace(/\/\*.*\*\//, '').trim();
                    }).filter(function (arg) {
                        // Ensure no undefined values are added.
                        return arg;
                    }),
                    functionArgumentList = "";
 
                functionArguments.forEach(function (arg, argIndex, argArray) {
                    functionArgumentList +=  arg + ((argIndex < argArray.length - 1) ? "," : "");
                });
 
                return functionArgumentList;
            },
            replaceConstructor = function (originalFunctionString, ReflectedConstructor) {
                var reflectedFunctionAsString = originalFunctionString,
                    reflectedFunction,
                    matchResults,
                    superMethodCalls = [];
 
                matchResults = reflectedFunctionAsString.match(/[A-Za-z0-9\.]+\.prototype\.[A-Za-z0-9]+\.apply\(this,\s*\[\s*.*\]\s*\)/g);
                if (matchResults) {
                    matchResults.forEach(function (match) {
                        Eif (match) {
                            superMethodCalls.push = {
                                key: match,
                                value: EVAL_IS_BAD__AVOID_THIS(match.replace(match, ReflectedConstructor +  match.substring(0, match.indexOf(".prototype")))) // jshint ignore:line
                            };
                        }
                    });
 
                    superMethodCalls.forEach(function (superCallMethod) {
                        reflectedFunctionAsString = reflectedFunctionAsString.replace(new RegExp(superCallMethod.key), superCallMethod.value);
                    });
                }
 
 
                matchResults = reflectedFunctionAsString.match(/[A-Za-z0-9\.]+\.prototype\.[A-Za-z0-9]+\.call\(this\s*,?(\s\w*,?)*\s*\)/g);
                Iif (matchResults) {
                    matchResults.forEach(function (match) {
                        if (match) {
                            superMethodCalls.push = {
                                key: match,
                                value: EVAL_IS_BAD__AVOID_THIS(match.replace(match, ReflectedConstructor +  match.substring(0, match.indexOf(".prototype")))) // jshint ignore:line
                            };
                        }
                    });
 
                    superMethodCalls.forEach(function (superCallMethod) {
                        reflectedFunctionAsString = reflectedFunctionAsString.replace(new RegExp(superCallMethod.key), superCallMethod.value);
                    });
                }
                reflectedFunction =  new FUNCTION_CONSTRUCTOR_IS_BAD__AVOID_THIS('return ' + reflectedFunctionAsString)(); // jshint ignore:line
 
                return reflectedFunction;
            },
            getClassRefPrototypeMethods = function (ClassRef, ReflectedConstructor) {
                var classRefPrototypeMethods = {},
                    classRefPrototypeMethod,
                    classRefPrototypeMethodName;
 
                for (classRefPrototypeMethodName in ClassRef.prototype) {
                    if (ClassRef.prototype.hasOwnProperty(classRefPrototypeMethodName)) {
                        Eif (classRefPrototypeMethodName !== "constructor" &&
                                classRefPrototypeMethodName !== "superclass" &&
                                Object.isFunction(ClassRef.prototype[classRefPrototypeMethodName])) {
                            classRefPrototypeMethod = ClassRef.prototype[classRefPrototypeMethodName];
                            classRefPrototypeMethods[classRefPrototypeMethodName] = replaceConstructor(classRefPrototypeMethod.toString(), ReflectedConstructor);
                        }
                    }
                }
 
                return classRefPrototypeMethods;
            },
            wrapConstructorWithReflectionCapabilities = function (ClassRef) {
                var WrappedSuperClassWithReflectionCapabilitiesConstructor,
                    wrappedSuperClassPrototypeMethods,
                    wrappedSuperClassPrototypeMethodName,
                    WrappedClassWithReflectionCapabilitiesConstructor,
                    wrappedClassWithReflectionCapabilitiesAsString = "",
                    functionArguments = getFunctionArguments(ClassRef),
                    originalClassAsString = ClassRef.toString(); //.replace(/\(.+?(?=\))\)/, "()");
 
                if (ClassRef.prototype.superclass) {
                    WrappedSuperClassWithReflectionCapabilitiesConstructor = wrapConstructorWithReflectionCapabilities(ClassRef.prototype.superclass);
                    wrappedSuperClassPrototypeMethods = getClassRefPrototypeMethods(ClassRef.prototype.superclass, WrappedSuperClassWithReflectionCapabilitiesConstructor);
                    originalClassAsString = originalClassAsString.replace(/.*superclass\.apply\(this,\s*\[\s*.*\]\s*\)\s*;*/, "\t\tthis.__proto__.superclass.apply(this, [" + functionArguments + "]);");
                    originalClassAsString = originalClassAsString.replace(/.*superclass\.call\(this\s*,?(\s\w*,?)*\s*\)\s*;?/, "\t\tthis.__proto__.superclass.call(this " + (functionArguments ? "," + functionArguments : "") + ");");
                    Object.defineProperty(WrappedSuperClassWithReflectionCapabilitiesConstructor.prototype, 'getMethod', {
                        enumerable: false,
                        value: function (methodName) {
                            var reflectionMethod;
 
                            Iif (WrappedSuperClassWithReflectionCapabilitiesConstructor.prototype.superclass) {
                                WrappedSuperClassWithReflectionCapabilitiesConstructor.prototype.superclass.prototype.getMethod.apply(this, [methodName]);
                            }
 
                            Eif (!reflectionMethod) {
                                reflectionMethod = this["_privates" + WrappedSuperClassWithReflectionCapabilitiesConstructor.name][methodName];//SuperClassConstructor.prototype.getReflectionMethod.apply(this, [methodName]);
                            }
 
                            return reflectionMethod;
                        }
                    });
 
                    for (wrappedSuperClassPrototypeMethodName in wrappedSuperClassPrototypeMethods) {
                        Eif (wrappedSuperClassPrototypeMethods.hasOwnProperty(wrappedSuperClassPrototypeMethodName)) {
                            Eif (wrappedSuperClassPrototypeMethodName) {
                                Object.defineProperty(WrappedSuperClassWithReflectionCapabilitiesConstructor.prototype, wrappedSuperClassPrototypeMethodName, {
                                    enumerable: false,
                                    value: wrappedSuperClassPrototypeMethods[wrappedSuperClassPrototypeMethodName]
                                });
                            }
                        }
                    }
                }
 
                // To expose the private functions, we create
                // a new function that goes trough the functions string
                // we could have done all string parsing in this class and
                // only associate the functions directly with string
                // manipulation here and not inside the new class,
                // but then we would have to expose the functions as string
                // in the code, which could lead to problems in the eval since
                // string might have semicolons, line breaks etc.
 
                //funcString += "new (";
                //funcString += "return ";
                wrappedClassWithReflectionCapabilitiesAsString += originalClassAsString.substring(0, originalClassAsString.length - 3) + "\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\tthis._privates" + ClassRef.name + " = {};\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\tthis._initPrivates = function(f) {\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\tvar fs = f.toString();\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\tthis._privates" + ClassRef.name + " = {};\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\tvar pf = fs.match(/function\\s*?(\\w.*?)\\(/g);\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\tif (pf != null){\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\tfor (var i = 0, ii = pf.length; i < ii; i++) {\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\tvar fn = pf[i].replace(/function\\s+/, '').replace('(', '').trim();\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\tif (f.name != fn) {\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\ttry {\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t\tthis._privates" + ClassRef.name + "[fn] = eval(fn);\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t} catch (e) {\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t\tif (e.name == 'ReferenceError') {\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t\t\tcontinue;\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t\t} else {\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t\t\tthrow e;\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t\t}\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t}\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t}\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t}\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t}\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\tpf = fs.match(/\\w*?\\s+=\\s+function/g);\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\tif (pf != null) {\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\tfor (var i = 0, ii = pf.length; i < ii; i++) {\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\tvar fn = pf[i].replace(/var\\s*/, '').replace(' ', '').replace('=', '').replace(' ', '').replace('function', '').replace(' ', '').replace('(', '');\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\ttry {\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t\tthis._privates" + ClassRef.name + "[fn] = eval(fn);\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t} catch (e) {\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t\tif (e.name == 'ReferenceError') {\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t\t\tcontinue;\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t\t} else {\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t\t\tthrow e;\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t\t}\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t\t\t}\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t\t}\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t\t}\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\t\t};\n\n";
 
                if (!ClassRef.prototype.superclass) {
                    wrappedClassWithReflectionCapabilitiesAsString += "\t\tthis._initPrivates(this.__proto__.superclass);\n";
                } else {
                    wrappedClassWithReflectionCapabilitiesAsString += "\t\tthis._initPrivates(this.__proto__.constructor);\n";
                }
 
                wrappedClassWithReflectionCapabilitiesAsString += "\t\tdelete this._initPrivates;\n";
                wrappedClassWithReflectionCapabilitiesAsString += "\n\t}";
                //funcString +=")();";
                WrappedClassWithReflectionCapabilitiesConstructor = new FUNCTION_CONSTRUCTOR_IS_BAD__AVOID_THIS(functionArguments, 'return ' + wrappedClassWithReflectionCapabilitiesAsString)(); // jshint ignore:line
 
                if (WrappedSuperClassWithReflectionCapabilitiesConstructor) {
                    WrappedClassWithReflectionCapabilitiesConstructor.prototype = BytePushers.inherit(WrappedSuperClassWithReflectionCapabilitiesConstructor.prototype);
                }
                if (ClassRef.prototype.superclass  && ClassRef.prototype.constructor) {
                    WrappedClassWithReflectionCapabilitiesConstructor.prototype.constructor = WrappedClassWithReflectionCapabilitiesConstructor;
                }
                if (WrappedSuperClassWithReflectionCapabilitiesConstructor) {
                    WrappedClassWithReflectionCapabilitiesConstructor.prototype.superclass = WrappedSuperClassWithReflectionCapabilitiesConstructor;
                }
 
                return WrappedClassWithReflectionCapabilitiesConstructor;
            };
 
        this.getInstance = function (ClassRef, classConstructorArguments) {
            // get the functions as a string
            var WrappedClassWithReflectionCapabilitiesConstructor = wrapConstructorWithReflectionCapabilities(ClassRef),
                wrappedClassPrototypeMethods = getClassRefPrototypeMethods(ClassRef, WrappedClassWithReflectionCapabilitiesConstructor),
                wrappedClassPrototypeMethodName,
                wrappedClassWithReflectionCapabilitiesInstance;
 
 
            Eif (WrappedClassWithReflectionCapabilitiesConstructor) {
                Object.defineProperty(WrappedClassWithReflectionCapabilitiesConstructor.prototype, 'getMethod', {
                    enumerable: false,
                    value: function (methodName) {
                        var reflectionMethod;
 
                        Eif (WrappedClassWithReflectionCapabilitiesConstructor.prototype.superclass) {
                            reflectionMethod = WrappedClassWithReflectionCapabilitiesConstructor.prototype.superclass.prototype.getMethod.apply(this, [methodName]);
                        }
 
                        if (!reflectionMethod) {
                            reflectionMethod = this["_privates" + WrappedClassWithReflectionCapabilitiesConstructor.name][methodName];
                        }
 
                        return reflectionMethod;
                    }
                });
 
                for (wrappedClassPrototypeMethodName in wrappedClassPrototypeMethods) {
                    Eif (wrappedClassPrototypeMethods.hasOwnProperty(wrappedClassPrototypeMethodName)) {
                        Eif (wrappedClassPrototypeMethodName) {
                            Object.defineProperty(WrappedClassWithReflectionCapabilitiesConstructor.prototype, wrappedClassPrototypeMethodName, {
                                enumerable: false,
                                value: wrappedClassPrototypeMethods[wrappedClassPrototypeMethodName]
                            });
                        }
                    }
                }
 
                wrappedClassWithReflectionCapabilitiesInstance = new WrappedClassWithReflectionCapabilitiesConstructor(classConstructorArguments); //var instance = eval(funcString);
                return wrappedClassWithReflectionCapabilitiesInstance;
            }
 
            throw "WrappedClassWithReflectionCapabilitiesConstructor was not successfully created.";
        };
    };
}(BytePushers));