Repository navigation
Allow extending native classes - #7081
nicolo-ribaudo wants to merge 6 commits into
Conversation
Use `Reflect.construct(Super, ...)` instead of `Super.apply`
when possible, because native classes can't be `.apply`ed.
If a browser doesn't support `Reflect.construct`, it also doesn't
support `class {}`.
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/6309/ |
| export default function _constructSuperInstance(Derived, args, existingThis) { | ||
| const Super = Derived.__proto__ || Object.getPrototypeOf(Derived); | ||
| return typeof Reflect !== "undefined" && typeof Reflect.construct === "function" | ||
| ? Reflect.construct(Super, args, existingThis.constructor || Derived) |
There was a problem hiding this comment.
I also said this in the comment where I suggested this to begin with, but this has the same caveat as the new.target transform: the this.constructor property must not be tampered with, as it's the only hint we have as to what the instance's constructor is in ES5. All other methods available that appear to work at first glance would break if the class is subclassed.
|
|
||
| babelHelpers.classCallCheck(this, Foo); | ||
| return babelHelpers.possibleConstructorReturn(_this, (_temp = _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this, ...args)), Object.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", { | ||
| return babelHelpers.possibleConstructorReturn(_this, (_temp = _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.constructSuperInstance(Foo, [...args], this)), Object.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", { |
There was a problem hiding this comment.
Possible future microoptimization: [...args] -> args
Only possible if we can prove args is arraylike, though 🤷♀️
There was a problem hiding this comment.
Is there something that prevents doing it now? We could use Array.isArray and its helper.
There was a problem hiding this comment.
@xtuc the idea is to do the optimization statically, though. It'd also miss out on arraylike that are not arrays
| // Ensure that we don't try to call super() in the default constructor. | ||
| class D extends null {} | ||
| var d = new D(); | ||
| assert.throws(() => new D()); |
There was a problem hiding this comment.
I wonder what happened with TC39's proposal to actually make extends null work.
As it is specified right now (and implemented by engines), extends null is explicitly allowed as a special case, but it's impossible to actually construct such a class.
| helpers.constructSuperInstance = defineHelper(` | ||
| export default function _constructSuperInstance(Derived, args, existingThis) { | ||
| const Super = Derived.__proto__ || Object.getPrototypeOf(Derived); | ||
| return typeof Reflect !== "undefined" && typeof Reflect.construct === "function" |
There was a problem hiding this comment.
While checking if my assumptions were really correct, I found that, while Node 4 has an (incomplete and almost unusable) implementation of classes, it also does not have Reflect, at all.
However, all that changes with this helper is that nothing changes on Node 4, you still can't extend native classes 😄
I also checked whether spreading arguments was safe; while Node 4 has no support for spread at all, arguments does have a working Symbol.iterator, so we should be safe on that side as well if the user uses transform-spread.
Jessidhia
left a comment
There was a problem hiding this comment.
👀👌
Just want to add something about this on the README: #7081 (comment)
| - Babel transpiles classes using `SuperClass.apply(/* ... */)`, but native | ||
| classes aren't callable and thus throw in this case. | ||
| - When extending native classes the `this.constructor` must not be tampered, | ||
| since it is the only way that Babel has of obtaining the class constructor. |
There was a problem hiding this comment.
This affects all classes, not just the native ones 🤔
| - Babel transpiles classes using `SuperClass.apply(/* ... */)`, but native | ||
| classes aren't callable and thus throw in this case. | ||
| - When extending native classes the `this.constructor` must not be tampered, | ||
| since it is the only way that Babel has of obtaining the class constructor. |
There was a problem hiding this comment.
This affects all classes, not just the native ones 🤔
There was a problem hiding this comment.
Oh dang, would it be a regression?
There was a problem hiding this comment.
A tradeoff. The ability to mess with this.constructor for the ability to extend native classes.
I think it's fine to do it as a breaking change in babel 7.
Erasing this.constructor will break subclassing, unless it's defined to undefined or erased in all prototype levels, because of the || Derived in the helper:
class A {}
class B extends A {}
delete B.prototype.constructor
;(new B()) instanceof B // false; is an `A`
class C extends B {}
C.prototype.constructor = undefined
;(new C()) instanceof C // trueDefining constructor to be something else usually summons Cthulhu.
|
|
||
| babelHelpers.classCallCheck(this, Foo); | ||
| return babelHelpers.possibleConstructorReturn(_this, (_temp = _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this, ...args)), Object.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", { | ||
| return babelHelpers.possibleConstructorReturn(_this, (_temp = _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.constructSuperInstance(Foo, [...args], this)), Object.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", { |
There was a problem hiding this comment.
Is there something that prevents doing it now? We could use Array.isArray and its helper.
|
Is this something we're still pursuing? |
|
Yes: this PR allows libraries which are distributed with transpiled classes to be used alongise with untranspiled classes. |
|
I will open another PR; this is too hard to rebase |
This is needed when an external modules ships untranspiled classes but the consumer code transpiles them. (This should't happen with preset-env correctly configured, but if you use preset-es2015 your classes might be transpiled even if not needed)
Ref #7078 #7020 (comment)
cc @Kovensky