|
1 | 1 | #include "node_messaging.h" |
| 2 | +#include "node_contextify.h" |
2 | 3 | #include "node_internals.h" |
3 | 4 | #include "node_buffer.h" |
4 | 5 | #include "util.h" |
@@ -698,6 +699,37 @@ void MessagePort::StartBinding(const FunctionCallbackInfo<Value>& args) { |
698 | 699 | port->Start(); |
699 | 700 | } |
700 | 701 |
|
| 702 | +void MessagePort::MoveToContext(const FunctionCallbackInfo<Value>& args) { |
| 703 | + Environment* env = Environment::GetCurrent(args); |
| 704 | + MessagePort* port; |
| 705 | + if (!args[0]->IsObject() || |
| 706 | + (port = Unwrap<MessagePort>(args[0].As<Object>())) == nullptr) { |
| 707 | + env->ThrowTypeError("First argument needs to be a MessagePort instance"); |
| 708 | + } |
| 709 | + if (!port->data_) { |
| 710 | + env->ThrowError("Cannot transfer a closed MessagePort"); |
| 711 | + return; |
| 712 | + } |
| 713 | + if (port->is_privileged_ || port->fm_listener_) { |
| 714 | + env->ThrowError("Cannot transfer MessagePort with special semantics"); |
| 715 | + return; |
| 716 | + } |
| 717 | + Local<Value> context_arg = args[1]; |
| 718 | + Local<Context> context; |
| 719 | + if (!context_arg->IsObject() || |
| 720 | + !ContextFromContextifiedSandbox(env, context_arg.As<Object>()) |
| 721 | + .ToLocal(&context)) { |
| 722 | + env->ThrowError("Invalid context argument"); |
| 723 | + return; |
| 724 | + } |
| 725 | + Context::Scope context_scope(context); |
| 726 | + MessagePort* target = |
| 727 | + MessagePort::New(env, context, nullptr, std::move(port->data_)); |
| 728 | + if (target) { |
| 729 | + args.GetReturnValue().Set(target->object()); |
| 730 | + } |
| 731 | +} |
| 732 | + |
701 | 733 | size_t MessagePort::self_size() const { |
702 | 734 | Mutex::ScopedLock lock(data_->mutex_); |
703 | 735 | size_t sz = sizeof(*this) + sizeof(*data_); |
@@ -781,6 +813,8 @@ static void InitMessaging(Local<Object> target, |
781 | 813 | templ->GetFunction(context).ToLocalChecked()).FromJust(); |
782 | 814 | } |
783 | 815 |
|
| 816 | + env->SetMethod(target, "moveMessagePortToContext", |
| 817 | + MessagePort::MoveToContext); |
784 | 818 | target->Set(context, |
785 | 819 | env->message_port_constructor_string(), |
786 | 820 | GetMessagePortConstructor(env, context).ToLocalChecked()) |
|
0 commit comments