cultivar/executor/src/main.rs

140 lines
3.4 KiB
Rust

mod api;
mod runtime_interface;
mod sim;
use cultivar_common::prelude::*;
use once_cell::sync::Lazy;
use parity_scale_codec::Encode;
use parking_lot::RwLock;
use sc_executor_common::wasm_runtime::WasmModule;
use sp_wasm_interface::{Function, FunctionContext, HostFunctions, Signature};
use std::cell::Cell;
static sims: Lazy<RwLock<sim::Sims>> = Lazy::new(|| RwLock::new(sim::Sims::new()));
/*use sp_runtime_interface::runtime_interface;
#[runtime_interface]
pub trait Api {
/*fn print_hello(nb: u32) -> u32 {
println!("hello {nb}");
nb + 42
}
fn walk(sim_id: SimId, entity_id: EntityId, direction: Direction) {
if let Some(sim) = sims.read().get_mut(&sim_id) {
if let Some(entity) = sim.entities.get_mut(&entity_id) {
entity.walk(direction)
}
}
}*/
}*/
/*struct FungiHostFunctions;
impl HostFunctions for FungiHostFunctions {
fn host_functions() -> Vec<&'static dyn Function> {
vec![
&FooFunction
]
}
fn register_static<T>(registry: &mut T) -> Result<(), T::Error>
where T: HostFunctionRegistry {
}
}
struct FooFunction;
impl Function for FooFunction {
fn name(&self) -> &str {
"foo"
}
fn signature(&self) -> Signature {
Signature {
args: std::borrow::Cow::from(&vec![]),
return_value: Some(sp_wasm_interface::ValueType::I32)
}
}
fn execute(&self, context: &mut dyn FunctionContext,args: &mut dyn Iterator<Item = sp_wasm_interface::Value>
) -> sp_wasm_interface::Result<Option<sp_wasm_interface::Value>> {
Ok(None)
}
}
fn foo() -> u8 {
44
}*/
fn main() {
let sim_id = {
sims.read().new_sim(Sim {
board: Board {
size: (4, 4)
},
entities: Default::default(),
entity_counter: 0,
})
};
let ai_code = std::fs::read("target/wasm32-unknown-unknown/release/runtime.wasm")
.expect("Cannot read runtime file");
/*let runtime = sc_executor_wasmtime::create_runtime::<
api::api::ApiHostFunctions,
>(
sc_executor_common::runtime_blob::RuntimeBlob::new(&ai_code).unwrap(),
sc_executor_wasmtime::Config {
allow_missing_func_imports: false,
cache_path: Some("/dev/shm".into()),
semantics: sc_executor_wasmtime::Semantics {
instantiation_strategy:
sc_executor_wasmtime::InstantiationStrategy::PoolingCopyOnWrite,
deterministic_stack_limit: Some(sc_executor_wasmtime::DeterministicStackLimit {
logical_max: 4 * 1024 * 1024,
native_stack_max: 8 * 1024 * 1024,
}),
canonicalize_nans: true,
parallel_compilation: true,
heap_alloc_strategy: sc_executor_common::wasm_runtime::HeapAllocStrategy::Dynamic {
maximum_pages: Some(32),
},
},
},
)
.unwrap();
let mut instance = runtime.new_instance().unwrap();
dbg!(instance
.call_export("run", &sim_id.encode())
.unwrap());*/
/*let mut state = ();
let mut env = sp_sandbox::EnvironmentDefinitionBuilder::new();
let mut instance = sp_sandbox::Instance::new(&ai_code, &env, &mut state).unwrap();
dbg!(instance.invoke("run", &[], &mut state).unwrap());*/
/*let engine = Engine::default();
let module = Module::new(&engine, ai_code).unwrap();
let mut store = Store::new(&engine, 4);
/*let host_foo = Func::wrap(&mut store, |caller: Caller<'_, u32>, param: u32| {
println!("Got {} from WebAssembly", param);
println!("my host state is: {}", caller.data());
param + 10
});*/
let instance = Instance::new(&mut store, &module, &[/*host_foo.into()*/]).unwrap();
let ai_run = instance
.get_typed_func::<(), (), _>(&mut store, "run")
.unwrap();
dbg!(ai_run
.call(&mut store, ())
.unwrap());*/
}