kernel: wit 1.0 new, has_blob, last_blob

This commit is contained in:
dr-frmr 2024-12-16 16:14:21 -05:00
parent f89c9ae266
commit 2f911f3be5
No known key found for this signature in database
2 changed files with 37 additions and 9 deletions

View File

@ -45,6 +45,7 @@ pub struct ProcessState {
/// received from the event loop.
/// the prompting_message won't have a blob, rather it is stored in last_blob.
pub prompting_message: Option<t::KernelMessage>,
pub last_message_blobbed: bool,
pub last_blob: Option<t::LazyLoadBlob>,
/// store the contexts and timeout task of all outstanding requests
pub contexts: HashMap<u64, (ProcessContext, JoinHandle<()>)>,
@ -337,6 +338,7 @@ pub async fn make_process_loop(
send_to_loop: send_to_loop.clone(),
send_to_terminal: send_to_terminal.clone(),
prompting_message: None,
last_message_blobbed: false,
last_blob: None,
contexts: HashMap::new(),
message_queue: VecDeque::new(),

View File

@ -111,6 +111,9 @@ impl process::ProcessState {
if km.lazy_load_blob.is_some() {
self.last_blob = km.lazy_load_blob;
km.lazy_load_blob = None;
self.last_message_blobbed = true;
} else {
self.last_message_blobbed = false;
}
if expects_response.is_some() || km.rsvp.is_some() {
// update prompting_message iff there is someone to reply to
@ -123,6 +126,9 @@ impl process::ProcessState {
if km.lazy_load_blob.is_some() {
self.last_blob = km.lazy_load_blob;
km.lazy_load_blob = None;
self.last_message_blobbed = true;
} else {
self.last_message_blobbed = false;
}
self.prompting_message = context.prompting_message;
(km, context.context)
@ -131,19 +137,25 @@ impl process::ProcessState {
if km.lazy_load_blob.is_some() {
self.last_blob = km.lazy_load_blob;
km.lazy_load_blob = None;
self.last_message_blobbed = true;
} else {
self.last_message_blobbed = false;
}
self.prompting_message = Some(km.clone());
(km, None)
}
},
},
Err(e) => match self.contexts.remove(&e.id) {
None => return Err((t::en_wit_send_error_v1(e.error), None)),
Some((context, _timeout_handle)) => {
self.prompting_message = context.prompting_message;
return Err((t::en_wit_send_error_v1(e.error), context.context));
Err(e) => {
self.last_message_blobbed = false;
match self.contexts.remove(&e.id) {
None => return Err((t::en_wit_send_error_v1(e.error), None)),
Some((context, _timeout_handle)) => {
self.prompting_message = context.prompting_message;
return Err((t::en_wit_send_error_v1(e.error), context.context));
}
}
},
}
};
let pk = signature::UnparsedPublicKey::new(
@ -899,10 +911,24 @@ impl StandardHost for process::ProcessWasiV1 {
Ok(self.process.get_next_message_for_process_v1().await)
}
/// from a process: grab the blob part of the current prompting message.
/// if the prompting message did not have a blob, will return None.
/// will also return None if there is no prompting message.
/// from a process: check if the last message received had a blob.
async fn has_blob(&mut self) -> Result<bool> {
Ok(self.process.last_message_blobbed)
}
/// from a process: grab the blob part of the last message received.
/// if the last message did not have a blob, will return None.
async fn get_blob(&mut self) -> Result<Option<wit::LazyLoadBlob>> {
Ok(if self.process.last_message_blobbed {
t::en_wit_blob_v1(self.process.last_blob.clone())
} else {
None
})
}
/// from a process: grab the **most recent** blob that has ever been received.
/// if no blobs have ever been received, will return None.
async fn last_blob(&mut self) -> Result<Option<wit::LazyLoadBlob>> {
Ok(t::en_wit_blob_v1(self.process.last_blob.clone()))
}