lib: add support for JSTransferable as a mixin#38383
Closed
jasnell wants to merge 1 commit intonodejs:masterfrom
Closed
lib: add support for JSTransferable as a mixin#38383jasnell wants to merge 1 commit intonodejs:masterfrom
jasnell wants to merge 1 commit intonodejs:masterfrom
Conversation
Member
|
@jasnell I think this is fine to do, yes 👍 |
Member
|
maybe i'm misunderstanding what kConstructor does but would it be possible to instead of doing the following: class X {
constructor() {
this.a = 1;
}
[kConstructor]() { return makeTransferable(X) }
}do this? class X {
constructor() {
this.a = 1;
return makeTransferable(this);
}
} |
Member
Author
Yep, absolutely could. I wasn't yet sure if we'd always want |
Member
Author
|
@devsnek ... updated to use the simpler constructor form! This should be ready for review. |
This comment has been minimized.
This comment has been minimized.
addaleax
reviewed
Apr 25, 2021
5ca4d52 to
c915542
Compare
This comment has been minimized.
This comment has been minimized.
Adds a new `makeTransferable()` utility that can construct a
`JSTransferable` object that does not directly extend the
`JSTransferable` JavaScript class.
Because JavaScript does not support multiple inheritance, it is
not possible (without help) to implement a class that extends
both `JSTransferable` and, for instance, `EventTarget` without
incurring a significant additional complexity and performance
cost by making all `EventTarget` instances extend `JSTransferable`...
That is, we *don't* want:
```js
class EventTarget extends JSTransferable { ... }
```
The `makeTransferable()` allows us to create objects that are
backed internally by `JSTransferable` without having to actually
extend it by leveraging the magic of `Reflect.construct()`.
```js
const {
JSTransferable,
kClone,
kDeserialize,
kConstructor,
makeTransferable,
} = require('internal/worker/js_transferable');
class E {
constructor(b) {
this.b = b;
}
}
class F extends E {
[kClone]() { /** ... **/ }
[kDeserialize]() { /** ... **/ }
static [kConstructor]() { return makeTransferable(F); }
}
const f = makeTransferable(F, 1);
f instanceof F; // true
f instanceof E; // true
f instanceof JSTransferable; // false
const mc = new MessageChannel();
mc.port1.onmessage = ({ data }) => {
data instanceof F; // true
data instanceof E; // true
data instanceof JSTransferable; // false
};
mc.port2.postMessage(f); // works!
```
The additional `internal/test/transfer.js` file is required for the
test because successfully deserializing transferable classes requires
that they be located in `lib/internal` for now.
Signed-off-by: James M Snell <jasnell@gmail.com>
c915542 to
ad99a04
Compare
This comment has been minimized.
This comment has been minimized.
addaleax
approved these changes
Apr 25, 2021
Collaborator
XadillaX
approved these changes
Apr 26, 2021
jasnell
added a commit
that referenced
this pull request
Apr 26, 2021
Adds a new `makeTransferable()` utility that can construct a
`JSTransferable` object that does not directly extend the
`JSTransferable` JavaScript class.
Because JavaScript does not support multiple inheritance, it is
not possible (without help) to implement a class that extends
both `JSTransferable` and, for instance, `EventTarget` without
incurring a significant additional complexity and performance
cost by making all `EventTarget` instances extend `JSTransferable`...
That is, we *don't* want:
```js
class EventTarget extends JSTransferable { ... }
```
The `makeTransferable()` allows us to create objects that are
backed internally by `JSTransferable` without having to actually
extend it by leveraging the magic of `Reflect.construct()`.
```js
const {
JSTransferable,
kClone,
kDeserialize,
kConstructor,
makeTransferable,
} = require('internal/worker/js_transferable');
class E {
constructor(b) {
this.b = b;
}
}
class F extends E {
[kClone]() { /** ... **/ }
[kDeserialize]() { /** ... **/ }
static [kConstructor]() { return makeTransferable(F); }
}
const f = makeTransferable(F, 1);
f instanceof F; // true
f instanceof E; // true
f instanceof JSTransferable; // false
const mc = new MessageChannel();
mc.port1.onmessage = ({ data }) => {
data instanceof F; // true
data instanceof E; // true
data instanceof JSTransferable; // false
};
mc.port2.postMessage(f); // works!
```
The additional `internal/test/transfer.js` file is required for the
test because successfully deserializing transferable classes requires
that they be located in `lib/internal` for now.
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: #38383
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Khaidi Chu <i@2333.moe>
Member
Author
|
Landed in bbe24e2 |
targos
pushed a commit
that referenced
this pull request
Apr 29, 2021
Adds a new `makeTransferable()` utility that can construct a
`JSTransferable` object that does not directly extend the
`JSTransferable` JavaScript class.
Because JavaScript does not support multiple inheritance, it is
not possible (without help) to implement a class that extends
both `JSTransferable` and, for instance, `EventTarget` without
incurring a significant additional complexity and performance
cost by making all `EventTarget` instances extend `JSTransferable`...
That is, we *don't* want:
```js
class EventTarget extends JSTransferable { ... }
```
The `makeTransferable()` allows us to create objects that are
backed internally by `JSTransferable` without having to actually
extend it by leveraging the magic of `Reflect.construct()`.
```js
const {
JSTransferable,
kClone,
kDeserialize,
kConstructor,
makeTransferable,
} = require('internal/worker/js_transferable');
class E {
constructor(b) {
this.b = b;
}
}
class F extends E {
[kClone]() { /** ... **/ }
[kDeserialize]() { /** ... **/ }
static [kConstructor]() { return makeTransferable(F); }
}
const f = makeTransferable(F, 1);
f instanceof F; // true
f instanceof E; // true
f instanceof JSTransferable; // false
const mc = new MessageChannel();
mc.port1.onmessage = ({ data }) => {
data instanceof F; // true
data instanceof E; // true
data instanceof JSTransferable; // false
};
mc.port2.postMessage(f); // works!
```
The additional `internal/test/transfer.js` file is required for the
test because successfully deserializing transferable classes requires
that they be located in `lib/internal` for now.
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: #38383
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Khaidi Chu <i@2333.moe>
Member
|
I'm removing the
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@addaleax ... Very interested in what you think on this... the use case is that I want to define internal classes that are both transferable and extend
NodeEventTargetbut without forcing allNodeEventTargetinstances to be cloneable or incur the cost of extendingJSTransferable. Because we can't use multiple inheritance this usesJSTransferableas a kind of mixin.This is not a public facing API. It is inteded for internal use only for now.
Adds a new
makeTransferable()utility that can construct aJSTransferableobject that does not directly extend theJSTransferableJavaScript class.Because JavaScript does not support multiple inheritance, it is
not possible (without help) to implement a class that extends
both
JSTransferableand, for instance,EventTargetwithoutincurring a significant additional complexity and performance
cost by making all
EventTargetinstances extendJSTransferable...That is, we don't want:
The
makeTransferable()allows us to create objects that arebacked internally by
JSTransferablewithout having to actuallyextend it by leveraging the magic of
Reflect.construct().The additional
internal/test/transfer.jsfile is required for thetest because successfully deserializing transferable classes requires
that they be located in
lib/internalfor now.Signed-off-by: James M Snell jasnell@gmail.com