96 lines
3.3 KiB
Rust
96 lines
3.3 KiB
Rust
#![allow(dead_code)]
|
|
use prometeu_bytecode::{
|
|
assemble, load_limits::LoadBudget, registry::*, registry_codec::PbxModule,
|
|
};
|
|
use prometeu_vm::resource_budget::RuntimeLimits;
|
|
use prometeu_vm::{
|
|
HostContext, HostReturn, NativeInterface, ValidatedProgramImage, VirtualMachine, VmInitError,
|
|
};
|
|
use std::sync::Arc;
|
|
pub struct Native;
|
|
impl NativeInterface for Native {
|
|
fn syscall(
|
|
&mut self,
|
|
_: u32,
|
|
_: &[prometeu_bytecode::Value],
|
|
_: &mut HostReturn,
|
|
_: &mut HostContext,
|
|
) -> Result<(), prometeu_hal::vm_fault::VmFault> {
|
|
panic!("random must not invoke host syscalls")
|
|
}
|
|
}
|
|
pub fn sig(params: &[u32], results: &[u32]) -> Signature {
|
|
Signature {
|
|
parameters: params.iter().map(|&t| NamedType { type_id: TypeId(t), name: None }).collect(),
|
|
results: results.iter().map(|&t| TypeId(t)).collect(),
|
|
}
|
|
}
|
|
pub fn declarations() -> Vec<TypeDecl> {
|
|
let mut t: Vec<_> = [BuiltinId::INT64, BuiltinId::RANDOM, BuiltinId::FLOAT, BuiltinId::DYNAMIC]
|
|
.into_iter()
|
|
.map(|b| TypeDecl { name: None, kind: TypeKind::Builtin(b) })
|
|
.collect();
|
|
t.extend([
|
|
TypeDecl {
|
|
name: None,
|
|
kind: TypeKind::Struct(vec![NamedType { type_id: TypeId(1), name: None }]),
|
|
},
|
|
TypeDecl { name: None, kind: TypeKind::Array(TypeId(1)) },
|
|
TypeDecl { name: None, kind: TypeKind::Fn(sig(&[], &[0])) },
|
|
TypeDecl { name: None, kind: TypeKind::Struct(vec![]) },
|
|
TypeDecl { name: None, kind: TypeKind::Trait(vec![]) },
|
|
TypeDecl { name: None, kind: TypeKind::Fn(sig(&[1], &[1])) },
|
|
]);
|
|
t
|
|
}
|
|
pub type Body<'a> = (&'a str, Signature, Vec<TypeId>, u32);
|
|
pub fn bytes(bodies: &[Body<'_>]) -> Vec<u8> {
|
|
let mut code = Vec::new();
|
|
let mut functions = Vec::new();
|
|
for (text, signature, captures, locals) in bodies {
|
|
let body = assemble(text).unwrap();
|
|
functions.push(FunctionDecl {
|
|
lifecycle_role: Default::default(),
|
|
signature: signature.clone(),
|
|
captures: captures.clone(),
|
|
local_slots: *locals,
|
|
max_stack_slots: 64,
|
|
code_offset: code.len() as u32,
|
|
code_len: body.len() as u32,
|
|
..Default::default()
|
|
});
|
|
code.extend(body);
|
|
}
|
|
let mut b = LoadBudget::new(Default::default());
|
|
let registry = Registry::resolve(
|
|
RegistryDeclarations { types: declarations(), functions, implementations: vec![] },
|
|
code.len(),
|
|
&mut b,
|
|
)
|
|
.unwrap();
|
|
PbxModule {
|
|
registry: ®istry,
|
|
constants: &[],
|
|
code: &code,
|
|
debug: None,
|
|
exports: &[],
|
|
syscalls: &[],
|
|
}
|
|
.serialize(&mut b)
|
|
.unwrap()
|
|
}
|
|
pub fn image(bodies: &[Body<'_>]) -> Result<Arc<ValidatedProgramImage>, VmInitError> {
|
|
ValidatedProgramImage::load(&bytes(bodies), &mut LoadBudget::new(Default::default()), 0)
|
|
}
|
|
pub fn vm(bodies: &[Body<'_>]) -> VirtualMachine {
|
|
let mut v = VirtualMachine::from_image(image(bodies).unwrap(), RuntimeLimits::default());
|
|
v.prepare_boot_call().unwrap();
|
|
v
|
|
}
|
|
pub fn run(v: &mut VirtualMachine, budget: u64) -> prometeu_vm::BudgetReport {
|
|
v.run_budget(budget, &mut Native, &mut HostContext::new(None)).unwrap()
|
|
}
|
|
pub fn step(v: &mut VirtualMachine) {
|
|
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
}
|