101 lines
3.4 KiB
Rust
101 lines
3.4 KiB
Rust
#![allow(dead_code)]
|
|
use prometeu_bytecode::load_limits::LoadBudget;
|
|
use prometeu_bytecode::registry::*;
|
|
use prometeu_bytecode::registry_codec::PbxModule;
|
|
use prometeu_hal::{HostContext, HostReturn, NativeInterface};
|
|
use prometeu_vm::ValidatedProgramImage;
|
|
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!("no native call expected")
|
|
}
|
|
}
|
|
pub fn declarations(code_len: usize) -> RegistryDeclarations {
|
|
let mut types: Vec<_> = [
|
|
BuiltinId::INT32,
|
|
BuiltinId::INT64,
|
|
BuiltinId::FLOAT,
|
|
BuiltinId::BOOL,
|
|
BuiltinId::STRING,
|
|
BuiltinId::VEC2,
|
|
BuiltinId::COLOR,
|
|
BuiltinId::PIXEL,
|
|
BuiltinId::DYNAMIC,
|
|
]
|
|
.into_iter()
|
|
.map(|id| TypeDecl { name: None, kind: TypeKind::Builtin(id) })
|
|
.collect();
|
|
let field = |id| NamedType { type_id: TypeId(id), name: None };
|
|
types.extend([
|
|
TypeDecl {
|
|
name: Some("Node".into()),
|
|
kind: TypeKind::Struct((0..=9).chain([10, 11]).map(field).collect()),
|
|
},
|
|
TypeDecl { name: None, kind: TypeKind::Trait(vec![]) },
|
|
TypeDecl { name: None, kind: TypeKind::Array(TypeId(0)) },
|
|
TypeDecl { name: None, kind: TypeKind::Array(TypeId(5)) },
|
|
TypeDecl { name: None, kind: TypeKind::Array(TypeId(7)) },
|
|
TypeDecl { name: None, kind: TypeKind::Array(TypeId(4)) },
|
|
TypeDecl { name: None, kind: TypeKind::Array(TypeId(8)) },
|
|
TypeDecl { name: None, kind: TypeKind::Array(TypeId(10)) },
|
|
TypeDecl { name: None, kind: TypeKind::Array(TypeId(9)) },
|
|
TypeDecl { name: None, kind: TypeKind::Array(TypeId(11)) },
|
|
TypeDecl { name: None, kind: TypeKind::Struct(vec![]) },
|
|
TypeDecl { name: None, kind: TypeKind::Struct(vec![]) },
|
|
]);
|
|
types.push(TypeDecl {
|
|
name: None,
|
|
kind: TypeKind::Struct([0, 5, 10].into_iter().map(field).collect()),
|
|
});
|
|
for id in [1, 2, 3, 6] {
|
|
types.push(TypeDecl { name: None, kind: TypeKind::Array(TypeId(id)) });
|
|
}
|
|
RegistryDeclarations {
|
|
types,
|
|
functions: vec![FunctionDecl {
|
|
lifecycle_role: Default::default(),
|
|
code_len: code_len as u32,
|
|
local_slots: 0,
|
|
max_stack_slots: 32,
|
|
..Default::default()
|
|
}],
|
|
implementations: vec![ImplementationDecl {
|
|
owner: TypeId(9),
|
|
trait_id: TypeId(10),
|
|
methods: vec![],
|
|
}],
|
|
}
|
|
}
|
|
pub fn bytes(code: &[u8]) -> Vec<u8> {
|
|
bytes_with(code, declarations(code.len()))
|
|
}
|
|
pub fn bytes_with(code: &[u8], declarations: RegistryDeclarations) -> Vec<u8> {
|
|
let registry =
|
|
Registry::resolve(declarations, code.len(), &mut LoadBudget::new(Default::default()))
|
|
.unwrap();
|
|
PbxModule {
|
|
registry: ®istry,
|
|
code,
|
|
constants: &[],
|
|
debug: None,
|
|
exports: &[],
|
|
syscalls: &[],
|
|
}
|
|
.serialize(&mut LoadBudget::new(Default::default()))
|
|
.unwrap()
|
|
}
|
|
pub fn load(code: &[u8]) -> Result<Arc<ValidatedProgramImage>, prometeu_vm::VmInitError> {
|
|
ValidatedProgramImage::load(
|
|
&bytes(code),
|
|
&mut LoadBudget::new(Default::default()),
|
|
prometeu_hal::syscalls::caps::NONE,
|
|
)
|
|
}
|