Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

worker: implement MessagePort and MessageChannel #98

Closed
wants to merge 2 commits into from

Conversation

addaleax
Copy link
Contributor

@addaleax addaleax commented Oct 8, 2017

Implement MessagePort and MessageChannel along the lines of
the DOM classes of the same names. MessagePorts initially
support transferring only ArrayBuffers.

Checklist
Affected core subsystem(s)

worker

@addaleax addaleax added the worker label Oct 8, 2017
src/env.h Outdated
@@ -92,6 +92,7 @@ class ModuleWrap;
V(decorated_private_symbol, "node:decorated") \
V(npn_buffer_private_symbol, "node:npnBuffer") \
V(processed_private_symbol, "node:processed") \
V(sab_lifetimepartner_symbol, "node:sharedArrayBufferLiftimePartner") \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo? node:sharedArrayBufferLiftimePartner -> node:sharedArrayBufferLifetimePartner

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a) yes, thank you and b) it wasn’t even meant to be in this commit 😄 I’ve fixed it in #58, though

@jkrems
Copy link
Contributor

jkrems commented Oct 9, 2017

References:

@jkrems
Copy link
Contributor

jkrems commented Oct 9, 2017

I assume "along the lines" is mostly referring to the differences in event listener interfaces (EventEmitter vs. EventTarget)? Any other intended limitations / deviations?

@TimothyGu TimothyGu assigned TimothyGu and unassigned TimothyGu Oct 9, 2017
@TimothyGu TimothyGu self-requested a review October 9, 2017 17:42
Copy link
Member

@TimothyGu TimothyGu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More review coming.

}

Object.defineProperty(MessagePort.prototype, 'onmessage', {
enumerable: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it enumerable for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm – the idea here was consistency with ES6 class methods, but I guess you’re right, this should match the native ones

src/env.h Outdated
@@ -317,6 +322,7 @@ class ModuleWrap;
V(promise_wrap_template, v8::ObjectTemplate) \
V(push_values_to_array_function, v8::Function) \
V(randombytes_constructor_template, v8::ObjectTemplate) \
V(sab_lifetimepartner_constructor_template, v8::FunctionTemplate) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be in this commit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope :)

using v8::ValueSerializer;

namespace node {
namespace worker {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it namespace messaging for consistency with the file name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well … it’s still conceptually part of the worker code, isn’t it?

@TimothyGu TimothyGu self-assigned this Oct 9, 2017
@TimothyGu
Copy link
Member

@jkrems

I assume "along the lines" is mostly referring to the differences in event listener interfaces (EventEmitter vs. EventTarget)?

There's that, and also some additional APIs like oninit, stop(), ref(), unref(), and hasRef().

// Factor generating the MessagePort JS constructor into its own piece
// of code, because it is needed early on in the child environment setup.
Local<FunctionTemplate> templ;
templ = env->message_port_constructor_template();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge these two lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done :)

Copy link
Member

@Qard Qard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than one question, it all LGTM.

this.ref();
this.start();
}
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing onmessage will stop the message and flaggedMessage events from being emitted. Is that intended behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I would say so. This is only there to match the Web spec more closely, but in that case you don’t want the EventEmitter interface anyways I guess.

@jkrems
Copy link
Contributor

jkrems commented Oct 10, 2017

@TimothyGu For context - I was trying to figure out how hard it would be to write portable code against this, so I wasn't worried too much about additional properties and more about meaningful API overlap. I assume the absence of .addEventListener in node makes this a bit annoying. But short of extending EventEmitter itself I don't think that can be fixed. :(

@addaleax
Copy link
Contributor Author

@jkrems Hm, yes. We could add missing methods to the MessagePort prototype itself, if we want?

@jkrems
Copy link
Contributor

jkrems commented Oct 10, 2017

I'm somewhat partial towards "make it a clean superset of the existing/spec browser API" but I'm not sure how realistic that is (and how much value it would provide in practice). Adding .addEventListener to these classes might be a neat cheap win in that regard.

@addaleax
Copy link
Contributor Author

@jkrems Do you want to try & take a stab at this yourself? The trickiest thing I guess would be the interaction of all 3 event emitter systems (.on(), .addEventListener(), raw .onmessage = foo)…

Implement `MessagePort` and `MessageChannel` along the lines of
the DOM classes of the same names. `MessagePort`s initially
support transferring only `ArrayBuffer`s.
@Qard
Copy link
Member

Qard commented Oct 15, 2017

The missing bits could probably also just be added later.

@addaleax
Copy link
Contributor Author

Landed in f65bb3d

addaleax added a commit that referenced this pull request Oct 15, 2017
Implement `MessagePort` and `MessageChannel` along the lines of
the DOM classes of the same names. `MessagePort`s initially
support transferring only `ArrayBuffer`s.

PR-URL: #98
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
@addaleax addaleax closed this Oct 15, 2017
@addaleax addaleax deleted the messageport-start branch October 15, 2017 19:52
addaleax added a commit to addaleax/node that referenced this pull request May 18, 2018
Implement `MessagePort` and `MessageChannel` along the lines of
the DOM classes of the same names. `MessagePort`s initially
support transferring only `ArrayBuffer`s.

Thanks to Stephen Belanger for reviewing this change in its
original form, to Benjamin Gruenbaum for reviewing the
added tests in their original form, and to Olivia Hugger
for reviewing the documentation in its original form.

Refs: ayojs/ayo#98
addaleax added a commit to addaleax/node that referenced this pull request May 22, 2018
Implement `MessagePort` and `MessageChannel` along the lines of
the DOM classes of the same names. `MessagePort`s initially
support transferring only `ArrayBuffer`s.

Thanks to Stephen Belanger for reviewing this change in its
original form, to Benjamin Gruenbaum for reviewing the
added tests in their original form, and to Olivia Hugger
for reviewing the documentation in its original form.

Refs: ayojs/ayo#98
addaleax added a commit to addaleax/node that referenced this pull request Jun 5, 2018
Implement `MessagePort` and `MessageChannel` along the lines of
the DOM classes of the same names. `MessagePort`s initially
support transferring only `ArrayBuffer`s.

Thanks to Stephen Belanger for reviewing this change in its
original form, to Benjamin Gruenbaum for reviewing the
added tests in their original form, and to Olivia Hugger
for reviewing the documentation in its original form.

Refs: ayojs/ayo#98
addaleax added a commit to addaleax/node that referenced this pull request Jun 5, 2018
Implement `MessagePort` and `MessageChannel` along the lines of
the DOM classes of the same names. `MessagePort`s initially
support transferring only `ArrayBuffer`s.

Thanks to Stephen Belanger for reviewing this change in its
original form, to Benjamin Gruenbaum for reviewing the
added tests in their original form, and to Olivia Hugger
for reviewing the documentation in its original form.

Refs: ayojs/ayo#98
addaleax added a commit to nodejs/node that referenced this pull request Jun 6, 2018
Implement `MessagePort` and `MessageChannel` along the lines of
the DOM classes of the same names. `MessagePort`s initially
support transferring only `ArrayBuffer`s.

Thanks to Stephen Belanger for reviewing this change in its
original form, to Benjamin Gruenbaum for reviewing the
added tests in their original form, and to Olivia Hugger
for reviewing the documentation in its original form.

Refs: ayojs/ayo#98

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
targos pushed a commit to nodejs/node that referenced this pull request Jun 7, 2018
Implement `MessagePort` and `MessageChannel` along the lines of
the DOM classes of the same names. `MessagePort`s initially
support transferring only `ArrayBuffer`s.

Thanks to Stephen Belanger for reviewing this change in its
original form, to Benjamin Gruenbaum for reviewing the
added tests in their original form, and to Olivia Hugger
for reviewing the documentation in its original form.

Refs: ayojs/ayo#98

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
targos pushed a commit to nodejs/node that referenced this pull request Jun 13, 2018
Implement `MessagePort` and `MessageChannel` along the lines of
the DOM classes of the same names. `MessagePort`s initially
support transferring only `ArrayBuffer`s.

Thanks to Stephen Belanger for reviewing this change in its
original form, to Benjamin Gruenbaum for reviewing the
added tests in their original form, and to Olivia Hugger
for reviewing the documentation in its original form.

Refs: ayojs/ayo#98

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants