-
-
Notifications
You must be signed in to change notification settings - Fork 101
Port set-prototype-of to ext #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: ext
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Workaround for http://code.google.com/p/v8/issues/detail?id=2804 | ||
|
|
||
| "use strict"; | ||
|
|
||
| var create = Object.create, shim; | ||
|
|
||
| if (!require("./set-prototype-of/is-implemented")()) { | ||
| shim = require("./set-prototype-of/implementation"); | ||
| } | ||
|
|
||
| module.exports = (function () { | ||
| var nullObject, polyProps, desc; | ||
| if (!shim) return create; | ||
| if (shim.level !== 1) return create; | ||
|
|
||
| nullObject = {}; | ||
| polyProps = {}; | ||
| desc = { configurable: false, enumerable: false, writable: true, value: undefined }; | ||
| Object.getOwnPropertyNames(Object.prototype).forEach(function (name) { | ||
| if (name === "__proto__") { | ||
| polyProps[name] = { | ||
| configurable: true, | ||
| enumerable: false, | ||
| writable: true, | ||
| value: undefined | ||
| }; | ||
| return; | ||
| } | ||
| polyProps[name] = desc; | ||
| }); | ||
| Object.defineProperties(nullObject, polyProps); | ||
|
|
||
| Object.defineProperty(shim, "nullPolyfill", { | ||
| configurable: false, | ||
| enumerable: false, | ||
| writable: false, | ||
| value: nullObject | ||
| }); | ||
|
|
||
| return function (prototype, props) { | ||
| return create(prototype === null ? nullObject : prototype, props); | ||
| }; | ||
| })(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| "use strict"; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not be introduced (we should rely on |
||
|
|
||
| var isValue = require("./is-value"); | ||
|
|
||
| var map = { function: true, object: true }; | ||
|
|
||
| module.exports = function (value) { return (isValue(value) && map[typeof value]) || false; }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| "use strict"; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those modules should be moved from |
||
|
|
||
| if (!require("./is-implemented")()) { | ||
| Object.defineProperty(Object, "setPrototypeOf", { | ||
| value: require("./implementation"), | ||
| configurable: true, | ||
| enumerable: false, | ||
| writable: true | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| "use strict"; | ||
|
|
||
| module.exports = require("./is-implemented")() ? Object.setPrototypeOf : require("./implementation"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| "use strict"; | ||
|
|
||
| var create = Object.create, getPrototypeOf = Object.getPrototypeOf, plainObject = {}; | ||
|
|
||
| module.exports = function (/* CustomCreate*/) { | ||
| var setPrototypeOf = Object.setPrototypeOf, customCreate = arguments[0] || create; | ||
| if (typeof setPrototypeOf !== "function") return false; | ||
| return getPrototypeOf(setPrototypeOf(customCreate(null), plainObject)) === plainObject; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| "use strict"; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not be introduced (we should rely on |
||
|
|
||
| var isValue = require("./is-value"); | ||
|
|
||
| module.exports = function (value) { | ||
| if (!isValue(value)) throw new TypeError("Cannot use null or undefined"); | ||
| return value; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To maintain changes history, ideally, if this is moved module from
_es5-ext/object/create.js