"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/es5-ext/object/set-prototype-of/shim.js" (11 Apr 2017, 2030 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Big thanks to @WebReflection for sorting this out
    2 // https://gist.github.com/WebReflection/5593554
    3 
    4 'use strict';
    5 
    6 var isObject      = require('../is-object')
    7   , value         = require('../valid-value')
    8 
    9   , isPrototypeOf = Object.prototype.isPrototypeOf
   10   , defineProperty = Object.defineProperty
   11   , nullDesc = { configurable: true, enumerable: false, writable: true,
   12         value: undefined }
   13   , validate;
   14 
   15 validate = function (obj, prototype) {
   16     value(obj);
   17     if ((prototype === null) || isObject(prototype)) return obj;
   18     throw new TypeError('Prototype must be null or an object');
   19 };
   20 
   21 module.exports = (function (status) {
   22     var fn, set;
   23     if (!status) return null;
   24     if (status.level === 2) {
   25         if (status.set) {
   26             set = status.set;
   27             fn = function (obj, prototype) {
   28                 set.call(validate(obj, prototype), prototype);
   29                 return obj;
   30             };
   31         } else {
   32             fn = function (obj, prototype) {
   33                 validate(obj, prototype).__proto__ = prototype;
   34                 return obj;
   35             };
   36         }
   37     } else {
   38         fn = function self(obj, prototype) {
   39             var isNullBase;
   40             validate(obj, prototype);
   41             isNullBase = isPrototypeOf.call(self.nullPolyfill, obj);
   42             if (isNullBase) delete self.nullPolyfill.__proto__;
   43             if (prototype === null) prototype = self.nullPolyfill;
   44             obj.__proto__ = prototype;
   45             if (isNullBase) defineProperty(self.nullPolyfill, '__proto__', nullDesc);
   46             return obj;
   47         };
   48     }
   49     return Object.defineProperty(fn, 'level', { configurable: false,
   50         enumerable: false, writable: false, value: status.level });
   51 }((function () {
   52     var x = Object.create(null), y = {}, set
   53       , desc = Object.getOwnPropertyDescriptor(Object.prototype, '__proto__');
   54 
   55     if (desc) {
   56         try {
   57             set = desc.set; // Opera crashes at this point
   58             set.call(x, y);
   59         } catch (ignore) { }
   60         if (Object.getPrototypeOf(x) === y) return { set: set, level: 2 };
   61     }
   62 
   63     x.__proto__ = y;
   64     if (Object.getPrototypeOf(x) === y) return { level: 2 };
   65 
   66     x = {};
   67     x.__proto__ = y;
   68     if (Object.getPrototypeOf(x) === y) return { level: 1 };
   69 
   70     return false;
   71 }())));
   72 
   73 require('../create');