@@ -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.
0 commit comments