| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f408d78 commit 77a944c
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -61,11 +61,11 @@ MessagePort.prototype.unref = MessagePortPrototype.unref; | |||
| 61 | 61 | // uv_async_t) which can receive information from other threads and emits | |
| 62 | 62 | // .onmessage events, and a function used for sending data to a MessagePort | |
| 63 | 63 | // in some other thread. | |
| 64 | - MessagePort.prototype[kOnMessageListener] = function onmessage(payload) { | ||
| 65 | - if (payload.type !== messageTypes.STDIO_WANTS_MORE_DATA) | ||
| 66 | - debug(`[${threadId}] received message`, payload); | ||
| 64 | + MessagePort.prototype[kOnMessageListener] = function onmessage(event) { | ||
| 65 | + if (event.data && event.data.type !== messageTypes.STDIO_WANTS_MORE_DATA) | ||
| 66 | + debug(`[${threadId}] received message`, event); | ||
| 67 | 67 | // Emit the deserialized object to userland. | |
| 68 | - this.emit('message', payload); | ||
| 68 | + this.emit('message', event.data); | ||
| 69 | 69 | }; | |
| 70 | 70 | ||
| 71 | 71 | // This is for compatibility with the Web's MessagePort API. It makes sense to | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -146,6 +146,7 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2; | |||
| 146 | 146 | V(crypto_ec_string, "ec") \ | |
| 147 | 147 | V(crypto_rsa_string, "rsa") \ | |
| 148 | 148 | V(cwd_string, "cwd") \ | |
| 149 | + V(data_string, "data") \ | ||
| 149 | 150 | V(dest_string, "dest") \ | |
| 150 | 151 | V(destroyed_string, "destroyed") \ | |
| 151 | 152 | V(detached_string, "detached") \ | |
@@ -291,6 +292,7 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2; | |||
| 291 | 292 | V(subject_string, "subject") \ | |
| 292 | 293 | V(subjectaltname_string, "subjectaltname") \ | |
| 293 | 294 | V(syscall_string, "syscall") \ | |
| 295 | + V(target_string, "target") \ | ||
| 294 | 296 | V(thread_id_string, "threadId") \ | |
| 295 | 297 | V(ticketkeycallback_string, "onticketkeycallback") \ | |
| 296 | 298 | V(timeout_string, "timeout") \ | |
@@ -359,6 +361,7 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2; | |||
| 359 | 361 | V(inspector_console_extension_installer, v8::Function) \ | |
| 360 | 362 | V(libuv_stream_wrap_ctor_template, v8::FunctionTemplate) \ | |
| 361 | 363 | V(message_port, v8::Object) \ | |
| 364 | + V(message_event_object_template, v8::ObjectTemplate) \ | ||
| 362 | 365 | V(message_port_constructor_template, v8::FunctionTemplate) \ | |
| 363 | 366 | V(native_module_require, v8::Function) \ | |
| 364 | 367 | V(performance_entry_callback, v8::Function) \ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,6 +25,7 @@ using v8::Maybe; | |||
| 25 | 25 | using v8::MaybeLocal; | |
| 26 | 26 | using v8::Nothing; | |
| 27 | 27 | using v8::Object; | |
| 28 | + using v8::ObjectTemplate; | ||
| 28 | 29 | using v8::SharedArrayBuffer; | |
| 29 | 30 | using v8::String; | |
| 30 | 31 | using v8::Value; | |
@@ -589,12 +590,19 @@ void MessagePort::OnMessage() { | |||
| 589 | 590 | // Call the JS .onmessage() callback. | |
| 590 | 591 | HandleScope handle_scope(env()->isolate()); | |
| 591 | 592 | Context::Scope context_scope(context); | |
| 592 | - Local<Value> args[] = { | ||
| 593 | - received.Deserialize(env(), context).FromMaybe(Local<Value>()) | ||
| 594 | - }; | ||
| 595 | 593 | ||
| 596 | - if (args[0].IsEmpty() || | ||
| 597 | - MakeCallback(env()->onmessage_string(), 1, args).IsEmpty()) { | ||
| 594 | + Local<Object> event; | ||
| 595 | + Local<Value> payload; | ||
| 596 | + Local<Value> cb_args[1]; | ||
| 597 | + if (!received.Deserialize(env(), context).ToLocal(&payload) || | ||
| 598 | + !env()->message_event_object_template()->NewInstance(context) | ||
| 599 | + .ToLocal(&event) || | ||
| 600 | + event->Set(context, env()->data_string(), payload).IsNothing() || | ||
| 601 | + event->Set(context, env()->target_string(), object()).IsNothing() || | ||
| 602 | + (cb_args[0] = event, false) || | ||
| 603 | + MakeCallback(env()->onmessage_string(), | ||
| 604 | + arraysize(cb_args), | ||
| 605 | + cb_args).IsEmpty()) { | ||
| 598 | 606 | // Re-schedule OnMessage() execution in case of failure. | |
| 599 | 607 | if (data_) | |
| 600 | 608 | TriggerAsync(); | |
@@ -763,6 +771,8 @@ MaybeLocal<Function> GetMessagePortConstructor( | |||
| 763 | 771 | if (!templ.IsEmpty()) | |
| 764 | 772 | return templ->GetFunction(context); | |
| 765 | 773 | ||
| 774 | + Isolate* isolate = env->isolate(); | ||
| 775 | + | ||
| 766 | 776 | { | |
| 767 | 777 | Local<FunctionTemplate> m = env->NewFunctionTemplate(MessagePort::New); | |
| 768 | 778 | m->SetClassName(env->message_port_constructor_string()); | |
@@ -775,6 +785,13 @@ MaybeLocal<Function> GetMessagePortConstructor( | |||
| 775 | 785 | env->SetProtoMethod(m, "drain", MessagePort::Drain); | |
| 776 | 786 | ||
| 777 | 787 | env->set_message_port_constructor_template(m); | |
| 788 | + | ||
| 789 | + Local<FunctionTemplate> event_ctor = FunctionTemplate::New(isolate); | ||
| 790 | + event_ctor->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "MessageEvent")); | ||
| 791 | + Local<ObjectTemplate> e = event_ctor->InstanceTemplate(); | ||
| 792 | + e->Set(env->data_string(), Null(isolate)); | ||
| 793 | + e->Set(env->target_string(), Null(isolate)); | ||
| 794 | + env->set_message_event_object_template(e); | ||
| 778 | 795 | } | |
| 779 | 796 | ||
| 780 | 797 | return GetMessagePortConstructor(env, context); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,7 +25,7 @@ assert.throws(common.mustCall(() => { | |||
| 25 | 25 | ||
| 26 | 26 | // The failed transfer should not affect the ports in anyway. | |
| 27 | 27 | port2.onmessage = common.mustCall((message) => { | |
| 28 | - assert.strictEqual(message, 2); | ||
| 28 | + assert.strictEqual(message.data, 2); | ||
| 29 | 29 | ||
| 30 | 30 | const inspectedPort1 = util.inspect(port1); | |
| 31 | 31 | const inspectedPort2 = util.inspect(port2); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,14 +21,15 @@ const { MessageChannel, MessagePort } = require('worker_threads'); | |||
| 21 | 21 | const { port1, port2 } = new MessageChannel(); | |
| 22 | 22 | ||
| 23 | 23 | port1.onmessage = common.mustCall((message) => { | |
| 24 | - assert.strictEqual(message, 4); | ||
| 24 | + assert.strictEqual(message.data, 4); | ||
| 25 | + assert.strictEqual(message.target, port1); | ||
| 25 | 26 | port2.close(common.mustCall()); | |
| 26 | 27 | }); | |
| 27 | 28 | ||
| 28 | 29 | port1.postMessage(2); | |
| 29 | 30 | ||
| 30 | 31 | port2.onmessage = common.mustCall((message) => { | |
| 31 | - port2.postMessage(message * 2); | ||
| 32 | + port2.postMessage(message.data * 2); | ||
| 32 | 33 | }); | |
| 33 | 34 | } | |
| 34 | 35 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,6 @@ if (!process.env.HAS_STARTED_WORKER) { | |||
| 14 | 14 | w.postMessage(2); | |
| 15 | 15 | } else { | |
| 16 | 16 | parentPort.onmessage = common.mustCall((message) => { | |
| 17 | - parentPort.postMessage(message * 2); | ||
| 17 | + parentPort.postMessage(message.data * 2); | ||
| 18 | 18 | }); | |
| 19 | 19 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments