@@ -529,33 +529,26 @@ void MessagePort::Close(v8::Local<v8::Value> close_callback) {
529529}
530530
531531void 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).
532535 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);
542537}
543538
544539MessagePort* MessagePort::New (
545540 Environment* env,
546541 Local<Context> context,
547542 std::unique_ptr<MessagePortData> data) {
548543 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);
552545
553546 // Construct a new instance, then assign the listener instance and possibly
554547 // the MessagePortData to it.
555548 Local<Object> instance;
556- if (!ctor ->NewInstance (context).ToLocal (&instance))
549+ if (!ctor_templ-> InstanceTemplate () ->NewInstance (context).ToLocal (&instance))
557550 return nullptr ;
558- MessagePort* port = Unwrap< MessagePort>( instance);
551+ MessagePort* port = new MessagePort (env, context, instance);
559552 CHECK_NOT_NULL (port);
560553 if (data) {
561554 port->Detach ();
@@ -830,13 +823,12 @@ void MessagePort::Entangle(MessagePort* a, MessagePortData* b) {
830823 MessagePortData::Entangle (a->data_ .get (), b);
831824}
832825
833- MaybeLocal<Function> GetMessagePortConstructor (
834- Environment* env, Local<Context> context) {
826+ Local<FunctionTemplate> GetMessagePortConstructorTemplate (Environment* env) {
835827 // Factor generating the MessagePort JS constructor into its own piece
836828 // of code, because it is needed early on in the child environment setup.
837829 Local<FunctionTemplate> templ = env->message_port_constructor_template ();
838830 if (!templ.IsEmpty ())
839- return templ-> GetFunction (context) ;
831+ return templ;
840832
841833 Isolate* isolate = env->isolate ();
842834
@@ -859,7 +851,7 @@ MaybeLocal<Function> GetMessagePortConstructor(
859851 env->set_message_event_object_template (e);
860852 }
861853
862- return GetMessagePortConstructor (env, context );
854+ return GetMessagePortConstructorTemplate (env);
863855}
864856
865857namespace {
@@ -902,8 +894,8 @@ static void InitMessaging(Local<Object> target,
902894
903895 target->Set (context,
904896 env->message_port_constructor_string (),
905- GetMessagePortConstructor (env, context). ToLocalChecked () )
906- .Check ();
897+ GetMessagePortConstructorTemplate (env)
898+ -> GetFunction (context). ToLocalChecked ()) .Check ();
907899
908900 // These are not methods on the MessagePort prototype, because
909901 // the browser equivalents do not provide them.
0 commit comments