]> git.r.bdr.sh - rbdr/dotfiles/blob
4ec944675e5685a9fc40dcc1838b36e413d921c1
[rbdr/dotfiles] /
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');