Skip to content
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

lib: add support for JSTransferable as a mixin #38383

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions lib/internal/test/transfer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

const {
makeTransferable,
kClone,
kDeserialize,
} = require('internal/worker/js_transferable');

process.emitWarning(
'These APIs are for internal testing only. Do not use them.',
'internal/test/transfer');

// Used as part of parallel/test-messaging-maketransferable.
// This has to exist within the lib/internal/ path in order
// for deserialization to work.

class E {
constructor(b) {
this.b = b;
}
}

class F extends E {
constructor(b) {
super(b);
/* eslint-disable-next-line no-constructor-return */
return makeTransferable(this);
}

[kClone]() {
return {
data: { b: this.b },
deserializeInfo: 'internal/test/transfer:F'
};
}

[kDeserialize]({ b }) {
this.b = b;
}
}

module.exports = { E, F };
18 changes: 16 additions & 2 deletions lib/internal/worker/js_transferable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
'use strict';
const {
Error,
ObjectDefineProperties,
ObjectGetOwnPropertyDescriptors,
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
ReflectConstruct,
StringPrototypeSplit,
} = primordials;
const {
Expand All @@ -22,21 +27,30 @@ function setup() {
const { 0: module, 1: ctor } = StringPrototypeSplit(deserializeInfo, ':');
const Ctor = require(module)[ctor];
if (typeof Ctor !== 'function' ||
!(Ctor.prototype instanceof JSTransferable)) {
typeof Ctor.prototype[messaging_deserialize_symbol] !== 'function') {
// Not one of the official errors because one should not be able to get
// here without messing with Node.js internals.
// eslint-disable-next-line no-restricted-syntax
throw new Error(`Unknown deserialize spec ${deserializeInfo}`);
}

return new Ctor();
});
}

function makeTransferable(obj) {
const inst = ReflectConstruct(JSTransferable, [], obj.constructor);
ObjectDefineProperties(inst, ObjectGetOwnPropertyDescriptors(obj));
ObjectSetPrototypeOf(inst, ObjectGetPrototypeOf(obj));
return inst;
}

module.exports = {
makeTransferable,
setup,
JSTransferable,
kClone: messaging_clone_symbol,
kDeserialize: messaging_deserialize_symbol,
kTransfer: messaging_transfer_symbol,
kTransferList: messaging_transfer_list_symbol
kTransferList: messaging_transfer_list_symbol,
};
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
'lib/internal/source_map/source_map.js',
'lib/internal/source_map/source_map_cache.js',
'lib/internal/test/binding.js',
'lib/internal/test/transfer.js',
'lib/internal/timers.js',
'lib/internal/tls.js',
'lib/internal/trace_events_async_hooks.js',
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-messaging-maketransferable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Flags: --expose-internals
'use strict';

const common = require('../common');

const assert = require('assert');
const {
JSTransferable,
} = require('internal/worker/js_transferable');
const { E, F } = require('internal/test/transfer');

// Tests that F is transferable even tho it does not directly,
// observably extend the JSTransferable class.

const mc = new MessageChannel();

mc.port1.onmessageerror = common.mustNotCall();

mc.port1.onmessage = common.mustCall(({ data }) => {
assert(!(data instanceof JSTransferable));
assert(data instanceof F);
assert(data instanceof E);
assert.strictEqual(data.b, 1);
mc.port1.close();
});

mc.port2.postMessage(new F(1));