implements PLN-0053
This commit is contained in:
@@ -13,10 +13,34 @@ use prometeu_hal::{
|
||||
AudioOpStatus, ComposerOpStatus, HostContext, HostReturn, NativeInterface, SyscallId,
|
||||
expect_bool, expect_int,
|
||||
};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::panic::{AssertUnwindSafe, catch_unwind};
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
impl VirtualMachineRuntime {
|
||||
pub(crate) struct VmRuntimeHost<'a> {
|
||||
pub runtime: &'a mut VirtualMachineRuntime,
|
||||
pub log_service: &'a mut LogService,
|
||||
}
|
||||
|
||||
impl Deref for VmRuntimeHost<'_> {
|
||||
type Target = VirtualMachineRuntime;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.runtime
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for VmRuntimeHost<'_> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self.runtime
|
||||
}
|
||||
}
|
||||
|
||||
impl VmRuntimeHost<'_> {
|
||||
fn log(&mut self, level: LogLevel, source: LogSource, tag: u16, msg: String) {
|
||||
self.runtime.log(self.log_service, level, source, tag, msg);
|
||||
}
|
||||
|
||||
fn panic_payload_to_string(payload: Box<dyn std::any::Any + Send>) -> String {
|
||||
if let Some(message) = payload.downcast_ref::<String>() {
|
||||
return message.clone();
|
||||
@@ -40,8 +64,8 @@ impl VirtualMachineRuntime {
|
||||
let app_id = self.current_app_id;
|
||||
let count = *self.logs_written_this_frame.get(&app_id).unwrap_or(&0);
|
||||
|
||||
if count >= Self::MAX_LOGS_PER_FRAME {
|
||||
if count == Self::MAX_LOGS_PER_FRAME {
|
||||
if count >= VirtualMachineRuntime::MAX_LOGS_PER_FRAME {
|
||||
if count == VirtualMachineRuntime::MAX_LOGS_PER_FRAME {
|
||||
self.logs_written_this_frame.insert(app_id, count + 1);
|
||||
self.log(
|
||||
LogLevel::Warn,
|
||||
@@ -56,8 +80,8 @@ impl VirtualMachineRuntime {
|
||||
self.logs_written_this_frame.insert(app_id, count + 1);
|
||||
|
||||
let mut final_msg = msg;
|
||||
if final_msg.len() > Self::MAX_LOG_LEN {
|
||||
final_msg.truncate(Self::MAX_LOG_LEN);
|
||||
if final_msg.len() > VirtualMachineRuntime::MAX_LOG_LEN {
|
||||
final_msg.truncate(VirtualMachineRuntime::MAX_LOG_LEN);
|
||||
}
|
||||
|
||||
self.log(level, LogSource::App { app_id }, tag, final_msg);
|
||||
@@ -122,7 +146,7 @@ impl VirtualMachineRuntime {
|
||||
}
|
||||
}
|
||||
|
||||
impl NativeInterface for VirtualMachineRuntime {
|
||||
impl NativeInterface for VmRuntimeHost<'_> {
|
||||
fn syscall(
|
||||
&mut self,
|
||||
id: SyscallId,
|
||||
@@ -410,8 +434,9 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
let path = self
|
||||
.open_files
|
||||
.get(&handle)
|
||||
.ok_or_else(|| VmFault::Panic("Invalid handle".into()))?;
|
||||
match self.fs.read_file(path) {
|
||||
.ok_or_else(|| VmFault::Panic("Invalid handle".into()))?
|
||||
.clone();
|
||||
match self.fs.read_file(&path) {
|
||||
Ok(data) => ret.push_string(String::from_utf8_lossy(&data).into_owned()),
|
||||
Err(_) => ret.push_null(),
|
||||
}
|
||||
@@ -423,8 +448,9 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
let path = self
|
||||
.open_files
|
||||
.get(&handle)
|
||||
.ok_or_else(|| VmFault::Panic("Invalid handle".into()))?;
|
||||
match self.fs.write_file(path, &content) {
|
||||
.ok_or_else(|| VmFault::Panic("Invalid handle".into()))?
|
||||
.clone();
|
||||
match self.fs.write_file(&path, &content) {
|
||||
Ok(_) => ret.push_bool(true),
|
||||
Err(_) => ret.push_bool(false),
|
||||
}
|
||||
@@ -516,8 +542,14 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
return Ok(());
|
||||
}
|
||||
let payload = hex_decode(&payload_hex)?;
|
||||
let write =
|
||||
self.memcard.slot_write(&self.fs, self.current_app_id, slot, offset, &payload);
|
||||
let runtime = &mut self.runtime;
|
||||
let write = runtime.memcard.slot_write(
|
||||
&runtime.fs,
|
||||
runtime.current_app_id,
|
||||
slot,
|
||||
offset,
|
||||
&payload,
|
||||
);
|
||||
ret.push_int(write.status as i64);
|
||||
ret.push_int(write.bytes_written as i64);
|
||||
Ok(())
|
||||
@@ -529,9 +561,10 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
return Ok(());
|
||||
}
|
||||
let status = {
|
||||
let memcard = &mut self.memcard;
|
||||
let fs = &mut self.fs;
|
||||
memcard.slot_commit(fs, self.current_app_id, slot)
|
||||
let runtime = &mut self.runtime;
|
||||
let memcard = &mut runtime.memcard;
|
||||
let fs = &mut runtime.fs;
|
||||
memcard.slot_commit(fs, runtime.current_app_id, slot)
|
||||
};
|
||||
ret.push_int(status as i64);
|
||||
Ok(())
|
||||
@@ -543,9 +576,10 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
return Ok(());
|
||||
}
|
||||
let status = {
|
||||
let memcard = &mut self.memcard;
|
||||
let fs = &mut self.fs;
|
||||
memcard.slot_clear(fs, self.current_app_id, slot)
|
||||
let runtime = &mut self.runtime;
|
||||
let memcard = &mut runtime.memcard;
|
||||
let fs = &mut runtime.fs;
|
||||
memcard.slot_clear(fs, runtime.current_app_id, slot)
|
||||
};
|
||||
ret.push_int(status as i64);
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user