| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c67642a commit 303a9a3
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -703,6 +703,14 @@ non-writable `stdout` or `stderr` stream. | |||
| 703 | 703 | ||
| 704 | 704 | A constructor for a class was called without `new`. | |
| 705 | 705 | ||
| 706 | + <a id="ERR_CONSTRUCT_CALL_INVALID"></a> | ||
| 707 | + ### ERR_CONSTRUCT_CALL_INVALID | ||
| 708 | + <!-- | ||
| 709 | + added: REPLACEME | ||
| 710 | + --> | ||
| 711 | + | ||
| 712 | + A class constructor was called that is not callable. | ||
| 713 | + | ||
| 706 | 714 | <a id="ERR_CPU_USAGE"></a> | |
| 707 | 715 | ### ERR_CPU_USAGE | |
| 708 | 716 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -54,7 +54,8 @@ void FatalException(v8::Isolate* isolate, | |||
| 54 | 54 | V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, Error) \ | |
| 55 | 55 | V(ERR_BUFFER_OUT_OF_BOUNDS, RangeError) \ | |
| 56 | 56 | V(ERR_BUFFER_TOO_LARGE, Error) \ | |
| 57 | - V(ERR_CONSTRUCT_CALL_REQUIRED, Error) \ | ||
| 57 | + V(ERR_CONSTRUCT_CALL_REQUIRED, TypeError) \ | ||
| 58 | + V(ERR_CONSTRUCT_CALL_INVALID, TypeError) \ | ||
| 58 | 59 | V(ERR_INVALID_ARG_VALUE, TypeError) \ | |
| 59 | 60 | V(ERR_INVALID_ARG_TYPE, TypeError) \ | |
| 60 | 61 | V(ERR_INVALID_MODULE_SPECIFIER, TypeError) \ | |
@@ -99,6 +100,7 @@ void FatalException(v8::Isolate* isolate, | |||
| 99 | 100 | #define PREDEFINED_ERROR_MESSAGES(V) \ | |
| 100 | 101 | V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, \ | |
| 101 | 102 | "Buffer is not available for the current Context") \ | |
| 103 | + V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called") \ | ||
| 102 | 104 | V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \ | |
| 103 | 105 | V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \ | |
| 104 | 106 | V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -529,33 +529,26 @@ void MessagePort::Close(v8::Local<v8::Value> close_callback) { | |||
| 529 | 529 | } | |
| 530 | 530 | ||
| 531 | 531 | void MessagePort::New(const FunctionCallbackInfo<Value>& args) { | |
| 532 | + // This constructor just throws an error. Unfortunately, we can’t use V8’s | ||
| 533 | + // ConstructorBehavior::kThrow, as that also removes the prototype from the | ||
| 534 | + // class (i.e. makes it behave like an arrow function). | ||
| 532 | 535 | Environment* env = Environment::GetCurrent(args); | |
| 533 | - if (!args.IsConstructCall()) { | ||
| 534 | - THROW_ERR_CONSTRUCT_CALL_REQUIRED(env); | ||
| 535 | - return; | ||
| 536 | - } | ||
| 537 | - | ||
| 538 | - Local<Context> context = args.This()->CreationContext(); | ||
| 539 | - Context::Scope context_scope(context); | ||
| 540 | - | ||
| 541 | - new MessagePort(env, context, args.This()); | ||
| 536 | + THROW_ERR_CONSTRUCT_CALL_INVALID(env); | ||
| 542 | 537 | } | |
| 543 | 538 | ||
| 544 | 539 | MessagePort* MessagePort::New( | |
| 545 | 540 | Environment* env, | |
| 546 | 541 | Local<Context> context, | |
| 547 | 542 | std::unique_ptr<MessagePortData> data) { | |
| 548 | 543 | Context::Scope context_scope(context); | |
| 549 | - Local<Function> ctor; | ||
| 550 | - if (!GetMessagePortConstructor(env, context).ToLocal(&ctor)) | ||
| 551 | - return nullptr; | ||
| 544 | + Local<FunctionTemplate> ctor_templ = GetMessagePortConstructorTemplate(env); | ||
| 552 | 545 | ||
| 553 | 546 | // Construct a new instance, then assign the listener instance and possibly | |
| 554 | 547 | // the MessagePortData to it. | |
| 555 | 548 | Local<Object> instance; | |
| 556 | - if (!ctor->NewInstance(context).ToLocal(&instance)) | ||
| 549 | + if (!ctor_templ->InstanceTemplate()->NewInstance(context).ToLocal(&instance)) | ||
| 557 | 550 | return nullptr; | |
| 558 | - MessagePort* port = Unwrap<MessagePort>(instance); | ||
| 551 | + MessagePort* port = new MessagePort(env, context, instance); | ||
| 559 | 552 | CHECK_NOT_NULL(port); | |
| 560 | 553 | if (data) { | |
| 561 | 554 | port->Detach(); | |
@@ -830,13 +823,12 @@ void MessagePort::Entangle(MessagePort* a, MessagePortData* b) { | |||
| 830 | 823 | MessagePortData::Entangle(a->data_.get(), b); | |
| 831 | 824 | } | |
| 832 | 825 | ||
| 833 | - MaybeLocal<Function> GetMessagePortConstructor( | ||
| 834 | - Environment* env, Local<Context> context) { | ||
| 826 | + Local<FunctionTemplate> GetMessagePortConstructorTemplate(Environment* env) { | ||
| 835 | 827 | // Factor generating the MessagePort JS constructor into its own piece | |
| 836 | 828 | // of code, because it is needed early on in the child environment setup. | |
| 837 | 829 | Local<FunctionTemplate> templ = env->message_port_constructor_template(); | |
| 838 | 830 | if (!templ.IsEmpty()) | |
| 839 | - return templ->GetFunction(context); | ||
| 831 | + return templ; | ||
| 840 | 832 | ||
| 841 | 833 | Isolate* isolate = env->isolate(); | |
| 842 | 834 | ||
@@ -859,7 +851,7 @@ MaybeLocal<Function> GetMessagePortConstructor( | |||
| 859 | 851 | env->set_message_event_object_template(e); | |
| 860 | 852 | } | |
| 861 | 853 | ||
| 862 | - return GetMessagePortConstructor(env, context); | ||
| 854 | + return GetMessagePortConstructorTemplate(env); | ||
| 863 | 855 | } | |
| 864 | 856 | ||
| 865 | 857 | namespace { | |
@@ -902,8 +894,8 @@ static void InitMessaging(Local<Object> target, | |||
| 902 | 894 | ||
| 903 | 895 | target->Set(context, | |
| 904 | 896 | env->message_port_constructor_string(), | |
| 905 | - GetMessagePortConstructor(env, context).ToLocalChecked()) | ||
| 906 | - .Check(); | ||
| 897 | + GetMessagePortConstructorTemplate(env) | ||
| 898 | + ->GetFunction(context).ToLocalChecked()).Check(); | ||
| 907 | 899 | ||
| 908 | 900 | // These are not methods on the MessagePort prototype, because | |
| 909 | 901 | // the browser equivalents do not provide them. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -211,8 +211,8 @@ class MessagePort : public HandleWrap { | |||
| 211 | 211 | friend class MessagePortData; | |
| 212 | 212 | }; | |
| 213 | 213 | ||
| 214 | - v8::MaybeLocal<v8::Function> GetMessagePortConstructor( | ||
| 215 | - Environment* env, v8::Local<v8::Context> context); | ||
| 214 | + v8::Local<v8::FunctionTemplate> GetMessagePortConstructorTemplate( | ||
| 215 | + Environment* env); | ||
| 216 | 216 | ||
| 217 | 217 | } // namespace worker | |
| 218 | 218 | } // namespace node | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,27 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + | ||
| 5 | + const { MessageChannel, MessagePort } = require('worker_threads'); | ||
| 6 | + | ||
| 7 | + // Make sure that `MessagePort` is the constructor for MessagePort instances, | ||
| 8 | + // but not callable. | ||
| 9 | + const { port1 } = new MessageChannel(); | ||
| 10 | + | ||
| 11 | + assert(port1 instanceof MessagePort); | ||
| 12 | + assert.strictEqual(port1.constructor, MessagePort); | ||
| 13 | + | ||
| 14 | + assert.throws(() => MessagePort(), { | ||
| 15 | + constructor: TypeError, | ||
| 16 | + code: 'ERR_CONSTRUCT_CALL_INVALID' | ||
| 17 | + }); | ||
| 18 | + | ||
| 19 | + assert.throws(() => new MessagePort(), { | ||
| 20 | + constructor: TypeError, | ||
| 21 | + code: 'ERR_CONSTRUCT_CALL_INVALID' | ||
| 22 | + }); | ||
| 23 | + | ||
| 24 | + assert.throws(() => MessageChannel(), { | ||
| 25 | + constructor: TypeError, | ||
| 26 | + code: 'ERR_CONSTRUCT_CALL_REQUIRED' | ||
| 27 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,13 +53,6 @@ vm.runInContext('(' + function() { | |||
| 53 | 53 | } | |
| 54 | 54 | assert(threw); | |
| 55 | 55 | } | |
| 56 | - | ||
| 57 | - { | ||
| 58 | - const newDummyPort = new (port.constructor)(); | ||
| 59 | - assert(!(newDummyPort instanceof MessagePort)); | ||
| 60 | - assert(newDummyPort.close instanceof Function); | ||
| 61 | - newDummyPort.close(); | ||
| 62 | - } | ||
| 63 | 56 | } + ')()', context); | |
| 64 | 57 | ||
| 65 | 58 | port2.on('message', common.mustCall((msg) => { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments