Basic output system
This commit is contained in:
@ -1,16 +1,28 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use viperid::VResult;
|
||||
use wasmi::{core::{HostError, Trap}, Caller, Linker};
|
||||
use wasmi::{core::{HostError, Trap}, Caller, Extern, Linker};
|
||||
|
||||
use crate::executor::ExecutorState;
|
||||
|
||||
pub(crate) fn integrate(linker: &mut Linker<ExecutorState>) -> VResult<()> {
|
||||
linker.func_wrap("viperid", "Pset", pset)?;
|
||||
|
||||
linker.func_wrap("viperid", "YieldFrame", yield_frame)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pset(mut caller: Caller<ExecutorState>, x: i32, y: i32, color: i32) -> Result<(), Trap> {
|
||||
let memory = match caller.get_export("memory") {
|
||||
Some(Extern::Memory(m)) => m,
|
||||
_ => return Err(wasmi::core::Trap::new(String::from("missing required memory export"))),
|
||||
};
|
||||
let(_, ctx) = memory.data_and_store_mut(&mut caller);
|
||||
ctx.device.shared.screen.pset(x, y, (color & 0xff) as u8);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn yield_frame(_caller: Caller<ExecutorState>) -> Result<(), Trap> {
|
||||
Err(Trap::from(YieldFrame {}))
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use std::mem;
|
||||
|
||||
use viperid::VResult;
|
||||
use viperid::{Device, VResult};
|
||||
use wasmi::{core::Trap, Engine, Linker, Module, Store, TypedFunc, TypedResumableCall, TypedResumableInvocation};
|
||||
|
||||
use crate::{engine_api::{self, YieldFrame}, wasi::{self, StockWasi}};
|
||||
@ -11,13 +11,15 @@ pub struct Executor {
|
||||
}
|
||||
|
||||
pub(crate) struct ExecutorState {
|
||||
wasi: wasi::StockWasi
|
||||
wasi: wasi::StockWasi,
|
||||
pub(crate) device: Device
|
||||
}
|
||||
|
||||
impl ExecutorState {
|
||||
fn new() -> Self {
|
||||
fn new(device: Device) -> Self {
|
||||
ExecutorState {
|
||||
wasi: StockWasi::new()
|
||||
wasi: StockWasi::new(),
|
||||
device
|
||||
}
|
||||
|
||||
}
|
||||
@ -25,12 +27,12 @@ impl ExecutorState {
|
||||
|
||||
|
||||
impl Executor {
|
||||
pub fn new(wasm: &[u8]) -> VResult<Self> {
|
||||
pub fn new(device: Device, wasm: &[u8]) -> VResult<Self> {
|
||||
let engine = Engine::default();
|
||||
|
||||
let module = Module::new(&engine, wasm)?;
|
||||
|
||||
let mut store = Store::new(&engine, ExecutorState::new());
|
||||
let mut store = Store::new(&engine, ExecutorState::new(device));
|
||||
|
||||
let mut linker = <Linker<ExecutorState>>::new(&engine);
|
||||
wasi::integrate(&mut linker, |hs| &mut hs.wasi)?;
|
||||
|
Reference in New Issue
Block a user