all files / lib/ protectBuiltins.js

70.15% Statements 47/67
54.76% Branches 23/42
71.43% Functions 5/7
70.15% Lines 47/67
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                   32× 32×     32× 32× 451× 451× 419× 419× 419× 370×             32×     17× 17× 17× 17× 15× 15×   17×                                                   17×     17× 17×   17× 17× 17×     17× 17×                        
(function() {
  var addGlobal, addedGlobals, builtinClones, builtinNames, builtinObjectNames, builtinReal, cloneBuiltin, copy, copyBuiltin, defineProperty, getOwnPropertyDescriptor, getOwnPropertyNames, globalScope, name, problems, replaceBuiltin, _, _i, _len, _ref, _ref1, _ref2;
 
  _ = (_ref = (_ref1 = (_ref2 = typeof window !== "undefined" && window !== null ? window._ : void 0) != null ? _ref2 : typeof self !== "undefined" && self !== null ? self._ : void 0) != null ? _ref1 : typeof global !== "undefined" && global !== null ? global._ : void 0) != null ? _ref : require('lodash');
 
  problems = require('./problems');
 
  module.exports.builtinObjectNames = builtinObjectNames = ['Object', 'Function', 'Array', 'String', 'Boolean', 'Number', 'Date', 'RegExp', 'Math', 'JSON', 'Error', 'EvalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError'];
 
  module.exports.builtinNames = builtinNames = builtinObjectNames.concat(['NaN', 'Infinity', 'undefined', 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'decodeURI', 'decodeURIComponent', 'encodeURI', 'encodeURIComponent']);
 
  Object.freeze(Error);
 
  getOwnPropertyNames = Object.getOwnPropertyNames;
 
  getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
 
  defineProperty = Object.defineProperty.bind(Object);
 
  copy = function(source, target) {
    var desc, name, _i, _len, _ref3;
    Iif (target == null) {
      return target;
    }
    _ref3 = getOwnPropertyNames(source);
    for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
      name = _ref3[_i];
      if (name !== 'caller' && name !== 'arguments') {
        Eif (getOwnPropertyDescriptor) {
          desc = getOwnPropertyDescriptor(source, name);
          if (!desc.get) {
            target[name] = desc.value;
          }
        } else {
          target[name] = source[name];
        }
      }
    }
    return target;
  };
 
  cloneBuiltin = function(obj) {
    var masked;
    masked = {};
    copy(obj, masked);
    if (obj.prototype) {
      masked.prototype = {};
      copy(obj.prototype, masked.prototype);
    }
    return masked;
  };
 
  copyBuiltin = function(source, target, resetPrototype) {
    var name, _i, _len, _ref3, _results;
    copy(source, target);
    if (source.prototype) {
      copy(source.prototype, target.prototype);
      if (resetPrototype) {
        _ref3 = getOwnPropertyNames(target.prototype);
        _results = [];
        for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
          name = _ref3[_i];
          if (source.prototype[name] == null) {
            _results.push(delete target.prototype[name]);
          }
        }
        return _results;
      }
    }
  };
 
  globalScope = (function() {
    return this;
  })();
 
  builtinClones = [];
 
  builtinReal = [];
 
  module.exports.addedGlobals = addedGlobals = {};
 
  module.exports.addGlobal = addGlobal = function(name, value) {
    Iif (addedGlobals[name] != null) {
      return;
    }
    Eif (value == null) {
      value = globalScope[name];
    }
    builtinClones.push(cloneBuiltin(value));
    builtinReal.push(value);
    return addedGlobals[name] = value;
  };
 
  for (_i = 0, _len = builtinObjectNames.length; _i < _len; _i++) {
    name = builtinObjectNames[_i];
    addGlobal(name);
  }
 
  module.exports.replaceBuiltin = replaceBuiltin = function(name, value) {
    var builtinIndex;
    builtinIndex = _.indexOf(builtinReal, value);
    if (builtinIndex === -1) {
      return console.error("We can't replace builtin " + name + ", because we never added it:", value);
    }
    return builtinClones[builtinIndex] = cloneBuiltin(value);
  };
 
}).call(this);