Misc Go interop fixes

This commit is contained in:
2024-04-27 12:16:04 -07:00
parent 042f2dca79
commit c124e60168
13 changed files with 77 additions and 58 deletions

View File

@ -1,9 +1,9 @@
use std::fmt::Display;
use viperid::VResult;
use wasmi::{core::{HostError, Trap}, Caller, Extern, Linker};
use wasmi::{core::{HostError, Trap}, Caller, Linker};
use crate::executor::ExecutorState;
use crate::{executor::ExecutorState, HOSTED_DEVICE};
pub(crate) fn integrate(linker: &mut Linker<ExecutorState>) -> VResult<()> {
linker.func_wrap("viperid", "Pset", pset)?;
@ -13,14 +13,11 @@ pub(crate) fn integrate(linker: &mut Linker<ExecutorState>) -> VResult<()> {
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 pset(mut _caller: Caller<ExecutorState>, x: i32, y: i32, color: i32) -> Result<(), Trap> {
HOSTED_DEVICE.with(|dev| {
dev.shared.screen.pset(x, y, (color & 0xff) as u8);
Ok(())
})
}
fn yield_frame(_caller: Caller<ExecutorState>) -> Result<(), Trap> {

View File

@ -1,6 +1,6 @@
use std::mem;
use viperid::{Device, VResult};
use viperid::VResult;
use wasmi::{core::Trap, Engine, Linker, Module, Store, TypedFunc, TypedResumableCall, TypedResumableInvocation};
use crate::{engine_api::{self, YieldFrame}, wasi::{self, StockWasi}};
@ -12,14 +12,12 @@ pub struct Executor {
pub(crate) struct ExecutorState {
wasi: wasi::StockWasi,
pub(crate) device: Device
}
impl ExecutorState {
fn new(device: Device) -> Self {
fn new() -> Self {
ExecutorState {
wasi: StockWasi::new(),
device
}
}
@ -27,12 +25,12 @@ impl ExecutorState {
impl Executor {
pub fn new(device: Device, wasm: &[u8]) -> VResult<Self> {
pub fn new(wasm: &[u8]) -> VResult<Self> {
let engine = Engine::default();
let module = Module::new(&engine, wasm)?;
let mut store = Store::new(&engine, ExecutorState::new(device));
let mut store = Store::new(&engine, ExecutorState::new());
let mut linker = <Linker<ExecutorState>>::new(&engine);
wasi::integrate(&mut linker, |hs| &mut hs.wasi)?;

View File

@ -0,0 +1,7 @@
use std::thread_local;
use viperid::Device;
thread_local! {
pub static HOSTED_DEVICE: Device = Device::new();
}

View File

@ -1,5 +1,7 @@
mod engine_api;
mod executor;
mod wasi;
mod hosted_device;
pub use executor::Executor;
pub use executor::Executor;
pub use hosted_device::HOSTED_DEVICE;

View File

@ -1,5 +1,3 @@
use std::time::SystemTime;
use wasmi::core::Trap;
use super::MinimalPreview1;