heyujie
2021-05-24 4885600ecc369aa2e30a65de8dd7a410f13c34df
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
/* eslint no-proto: "off" */
 
// Big thanks to @WebReflection for sorting this out
// https://gist.github.com/WebReflection/5593554
 
"use strict";
 
var isObject         = require("../is-object")
  , value            = require("../valid-value")
  , objIsPrototypeOf = Object.prototype.isPrototypeOf
  , defineProperty   = Object.defineProperty
  , nullDesc         = { configurable: true, enumerable: false, writable: true, value: undefined }
  , validate;
 
validate = function (obj, prototype) {
    value(obj);
    if (prototype === null || isObject(prototype)) return obj;
    throw new TypeError("Prototype must be null or an object");
};
 
module.exports = (function (status) {
    var fn, set;
    if (!status) return null;
    if (status.level === 2) {
        if (status.set) {
            set = status.set;
            fn = function (obj, prototype) {
                set.call(validate(obj, prototype), prototype);
                return obj;
            };
        } else {
            fn = function (obj, prototype) {
                validate(obj, prototype).__proto__ = prototype;
                return obj;
            };
        }
    } else {
        fn = function self(obj, prototype) {
            var isNullBase;
            validate(obj, prototype);
            isNullBase = objIsPrototypeOf.call(self.nullPolyfill, obj);
            if (isNullBase) delete self.nullPolyfill.__proto__;
            if (prototype === null) prototype = self.nullPolyfill;
            obj.__proto__ = prototype;
            if (isNullBase) defineProperty(self.nullPolyfill, "__proto__", nullDesc);
            return obj;
        };
    }
    return Object.defineProperty(fn, "level", {
        configurable: false,
        enumerable: false,
        writable: false,
        value: status.level
    });
})(
    (function () {
        var tmpObj1 = Object.create(null)
          , tmpObj2 = {}
          , set
          , desc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__");
 
        if (desc) {
            try {
                set = desc.set; // Opera crashes at this point
                set.call(tmpObj1, tmpObj2);
            } catch (ignore) {}
            if (Object.getPrototypeOf(tmpObj1) === tmpObj2) return { set: set, level: 2 };
        }
 
        tmpObj1.__proto__ = tmpObj2;
        if (Object.getPrototypeOf(tmpObj1) === tmpObj2) return { level: 2 };
 
        tmpObj1 = {};
        tmpObj1.__proto__ = tmpObj2;
        if (Object.getPrototypeOf(tmpObj1) === tmpObj2) return { level: 1 };
 
        return false;
    })()
);
 
require("../create");