1 // Big thanks to @WebReflection for sorting this out
2 // https://gist.github.com/WebReflection/5593554
6 var isObject = require('../is-object')
7 , value = require('../valid-value')
9 , isPrototypeOf = Object.prototype.isPrototypeOf
10 , defineProperty = Object.defineProperty
11 , nullDesc = { configurable: true, enumerable: false, writable: true,
15 validate = function (obj, prototype) {
17 if ((prototype === null) || isObject(prototype)) return obj;
18 throw new TypeError('Prototype must be null or an object');
21 module.exports = (function (status) {
23 if (!status) return null;
24 if (status.level === 2) {
27 fn = function (obj, prototype) {
28 set.call(validate(obj, prototype), prototype);
32 fn = function (obj, prototype) {
33 validate(obj, prototype).__proto__ = prototype;
38 fn = function self(obj, prototype) {
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);
49 return Object.defineProperty(fn, 'level', { configurable: false,
50 enumerable: false, writable: false, value: status.level });
52 var x = Object.create(null), y = {}, set
53 , desc = Object.getOwnPropertyDescriptor(Object.prototype, '__proto__');
57 set = desc.set; // Opera crashes at this point
60 if (Object.getPrototypeOf(x) === y) return { set: set, level: 2 };
64 if (Object.getPrototypeOf(x) === y) return { level: 2 };
68 if (Object.getPrototypeOf(x) === y) return { level: 1 };