2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-01-13 02:51:32 -05:00
|
|
|
|
2021-06-21 13:53:52 -04:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
2023-09-14 17:05:18 -04:00
|
|
|
use deno_core::op2;
|
2022-03-14 13:44:15 -04:00
|
|
|
|
2023-01-14 23:18:58 -05:00
|
|
|
use deno_core::CancelFuture;
|
|
|
|
use deno_core::CancelHandle;
|
2022-07-11 11:27:33 -04:00
|
|
|
use deno_core::DetachedBuffer;
|
2023-01-14 23:18:58 -05:00
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_core::RcRef;
|
|
|
|
use deno_core::Resource;
|
|
|
|
use deno_core::ResourceId;
|
2024-04-16 14:41:03 -04:00
|
|
|
use futures::future::poll_fn;
|
2021-06-21 13:53:52 -04:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
2024-03-10 19:23:06 -04:00
|
|
|
use tokio::sync::mpsc::error::TryRecvError;
|
2021-06-21 13:53:52 -04:00
|
|
|
use tokio::sync::mpsc::unbounded_channel;
|
|
|
|
use tokio::sync::mpsc::UnboundedReceiver;
|
|
|
|
use tokio::sync::mpsc::UnboundedSender;
|
|
|
|
|
2024-03-15 20:59:18 -04:00
|
|
|
pub enum Transferable {
|
2021-06-21 13:53:52 -04:00
|
|
|
MessagePort(MessagePort),
|
2021-08-25 07:48:53 -04:00
|
|
|
ArrayBuffer(u32),
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
|
|
|
|
2022-07-11 11:27:33 -04:00
|
|
|
type MessagePortMessage = (DetachedBuffer, Vec<Transferable>);
|
2021-06-21 13:53:52 -04:00
|
|
|
|
|
|
|
pub struct MessagePort {
|
|
|
|
rx: RefCell<UnboundedReceiver<MessagePortMessage>>,
|
2021-06-22 10:30:16 -04:00
|
|
|
tx: RefCell<Option<UnboundedSender<MessagePortMessage>>>,
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MessagePort {
|
|
|
|
pub fn send(
|
|
|
|
&self,
|
|
|
|
state: &mut OpState,
|
|
|
|
data: JsMessageData,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let transferables =
|
|
|
|
deserialize_js_transferables(state, data.transferables)?;
|
|
|
|
|
|
|
|
// Swallow the failed to send error. It means the channel was disentangled,
|
|
|
|
// but not cleaned up.
|
2021-06-22 10:30:16 -04:00
|
|
|
if let Some(tx) = &*self.tx.borrow() {
|
2022-07-11 11:27:33 -04:00
|
|
|
tx.send((data.data, transferables)).ok();
|
2021-06-22 10:30:16 -04:00
|
|
|
}
|
2021-06-21 13:53:52 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn recv(
|
|
|
|
&self,
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
) -> Result<Option<JsMessageData>, AnyError> {
|
2024-04-16 14:41:03 -04:00
|
|
|
let rx = &self.rx;
|
|
|
|
|
|
|
|
let maybe_data = poll_fn(|cx| {
|
|
|
|
let mut rx = rx.borrow_mut();
|
|
|
|
rx.poll_recv(cx)
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
if let Some((data, transferables)) = maybe_data {
|
2021-06-21 13:53:52 -04:00
|
|
|
let js_transferables =
|
|
|
|
serialize_transferables(&mut state.borrow_mut(), transferables);
|
|
|
|
return Ok(Some(JsMessageData {
|
2022-07-11 11:27:33 -04:00
|
|
|
data,
|
2021-06-21 13:53:52 -04:00
|
|
|
transferables: js_transferables,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}
|
2021-06-22 10:30:16 -04:00
|
|
|
|
|
|
|
/// This forcefully disconnects the message port from its paired port. This
|
|
|
|
/// will wake up the `.recv` on the paired port, which will return `Ok(None)`.
|
|
|
|
pub fn disentangle(&self) {
|
|
|
|
let mut tx = self.tx.borrow_mut();
|
|
|
|
tx.take();
|
|
|
|
}
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_entangled_message_port() -> (MessagePort, MessagePort) {
|
|
|
|
let (port1_tx, port2_rx) = unbounded_channel::<MessagePortMessage>();
|
|
|
|
let (port2_tx, port1_rx) = unbounded_channel::<MessagePortMessage>();
|
|
|
|
|
|
|
|
let port1 = MessagePort {
|
|
|
|
rx: RefCell::new(port1_rx),
|
2021-06-22 10:30:16 -04:00
|
|
|
tx: RefCell::new(Some(port1_tx)),
|
2021-06-21 13:53:52 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
let port2 = MessagePort {
|
|
|
|
rx: RefCell::new(port2_rx),
|
2021-06-22 10:30:16 -04:00
|
|
|
tx: RefCell::new(Some(port2_tx)),
|
2021-06-21 13:53:52 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
(port1, port2)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MessagePortResource {
|
|
|
|
port: MessagePort,
|
|
|
|
cancel: CancelHandle,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for MessagePortResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"messagePort".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.cancel.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 17:05:18 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-06-21 13:53:52 -04:00
|
|
|
pub fn op_message_port_create_entangled(
|
|
|
|
state: &mut OpState,
|
2022-05-13 04:36:31 -04:00
|
|
|
) -> (ResourceId, ResourceId) {
|
2021-06-21 13:53:52 -04:00
|
|
|
let (port1, port2) = create_entangled_message_port();
|
|
|
|
|
|
|
|
let port1_id = state.resource_table.add(MessagePortResource {
|
|
|
|
port: port1,
|
|
|
|
cancel: CancelHandle::new(),
|
|
|
|
});
|
|
|
|
|
|
|
|
let port2_id = state.resource_table.add(MessagePortResource {
|
|
|
|
port: port2,
|
|
|
|
cancel: CancelHandle::new(),
|
|
|
|
});
|
|
|
|
|
2022-05-13 04:36:31 -04:00
|
|
|
(port1_id, port2_id)
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
#[serde(tag = "kind", content = "data", rename_all = "camelCase")]
|
|
|
|
pub enum JsTransferable {
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
MessagePort(ResourceId),
|
2021-08-25 07:48:53 -04:00
|
|
|
ArrayBuffer(u32),
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
|
|
|
|
2024-03-15 20:59:18 -04:00
|
|
|
pub fn deserialize_js_transferables(
|
2021-06-21 13:53:52 -04:00
|
|
|
state: &mut OpState,
|
|
|
|
js_transferables: Vec<JsTransferable>,
|
|
|
|
) -> Result<Vec<Transferable>, AnyError> {
|
|
|
|
let mut transferables = Vec::with_capacity(js_transferables.len());
|
|
|
|
for js_transferable in js_transferables {
|
|
|
|
match js_transferable {
|
|
|
|
JsTransferable::MessagePort(id) => {
|
|
|
|
let resource = state
|
|
|
|
.resource_table
|
|
|
|
.take::<MessagePortResource>(id)
|
2021-08-15 07:29:19 -04:00
|
|
|
.map_err(|_| type_error("Invalid message port transfer"))?;
|
2021-06-21 13:53:52 -04:00
|
|
|
resource.cancel.cancel();
|
|
|
|
let resource = Rc::try_unwrap(resource)
|
|
|
|
.map_err(|_| type_error("Message port is not ready for transfer"))?;
|
|
|
|
transferables.push(Transferable::MessagePort(resource.port));
|
|
|
|
}
|
2021-08-25 07:48:53 -04:00
|
|
|
JsTransferable::ArrayBuffer(id) => {
|
|
|
|
transferables.push(Transferable::ArrayBuffer(id));
|
|
|
|
}
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(transferables)
|
|
|
|
}
|
|
|
|
|
2024-03-15 20:59:18 -04:00
|
|
|
pub fn serialize_transferables(
|
2021-06-21 13:53:52 -04:00
|
|
|
state: &mut OpState,
|
|
|
|
transferables: Vec<Transferable>,
|
|
|
|
) -> Vec<JsTransferable> {
|
|
|
|
let mut js_transferables = Vec::with_capacity(transferables.len());
|
|
|
|
for transferable in transferables {
|
|
|
|
match transferable {
|
|
|
|
Transferable::MessagePort(port) => {
|
|
|
|
let rid = state.resource_table.add(MessagePortResource {
|
|
|
|
port,
|
|
|
|
cancel: CancelHandle::new(),
|
|
|
|
});
|
|
|
|
js_transferables.push(JsTransferable::MessagePort(rid));
|
|
|
|
}
|
2021-08-25 07:48:53 -04:00
|
|
|
Transferable::ArrayBuffer(id) => {
|
|
|
|
js_transferables.push(JsTransferable::ArrayBuffer(id));
|
|
|
|
}
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
js_transferables
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
pub struct JsMessageData {
|
2024-03-15 20:59:18 -04:00
|
|
|
pub data: DetachedBuffer,
|
|
|
|
pub transferables: Vec<JsTransferable>,
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
|
|
|
|
2023-09-14 17:05:18 -04:00
|
|
|
#[op2]
|
2021-06-21 13:53:52 -04:00
|
|
|
pub fn op_message_port_post_message(
|
|
|
|
state: &mut OpState,
|
2023-09-14 17:05:18 -04:00
|
|
|
#[smi] rid: ResourceId,
|
|
|
|
#[serde] data: JsMessageData,
|
2021-06-21 13:53:52 -04:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
for js_transferable in &data.transferables {
|
2021-08-25 07:48:53 -04:00
|
|
|
if let JsTransferable::MessagePort(id) = js_transferable {
|
|
|
|
if *id == rid {
|
|
|
|
return Err(type_error("Can not transfer self message port"));
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 07:29:19 -04:00
|
|
|
let resource = state.resource_table.get::<MessagePortResource>(rid)?;
|
2021-06-21 13:53:52 -04:00
|
|
|
resource.port.send(state, data)
|
|
|
|
}
|
|
|
|
|
2023-09-14 17:05:18 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2021-06-21 13:53:52 -04:00
|
|
|
pub async fn op_message_port_recv_message(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-14 17:05:18 -04:00
|
|
|
#[smi] rid: ResourceId,
|
2021-06-21 13:53:52 -04:00
|
|
|
) -> Result<Option<JsMessageData>, AnyError> {
|
|
|
|
let resource = {
|
|
|
|
let state = state.borrow();
|
|
|
|
match state.resource_table.get::<MessagePortResource>(rid) {
|
2021-08-15 07:29:19 -04:00
|
|
|
Ok(resource) => resource,
|
|
|
|
Err(_) => return Ok(None),
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let cancel = RcRef::map(resource.clone(), |r| &r.cancel);
|
2021-06-22 10:30:16 -04:00
|
|
|
resource.port.recv(state).or_cancel(cancel).await?
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
2024-03-10 19:23:06 -04:00
|
|
|
|
|
|
|
#[op2]
|
|
|
|
#[serde]
|
|
|
|
pub fn op_message_port_recv_message_sync(
|
|
|
|
state: &mut OpState, // Rc<RefCell<OpState>>,
|
|
|
|
#[smi] rid: ResourceId,
|
|
|
|
) -> Result<Option<JsMessageData>, AnyError> {
|
|
|
|
let resource = state.resource_table.get::<MessagePortResource>(rid)?;
|
2024-04-15 19:06:39 -04:00
|
|
|
resource.cancel.cancel();
|
2024-03-10 19:23:06 -04:00
|
|
|
let mut rx = resource.port.rx.borrow_mut();
|
|
|
|
|
|
|
|
match rx.try_recv() {
|
|
|
|
Ok((d, t)) => Ok(Some(JsMessageData {
|
|
|
|
data: d,
|
|
|
|
transferables: serialize_transferables(state, t),
|
|
|
|
})),
|
|
|
|
Err(TryRecvError::Empty) => Ok(None),
|
|
|
|
Err(TryRecvError::Disconnected) => Ok(None),
|
|
|
|
}
|
|
|
|
}
|