81 lines
2.4 KiB
Rust
81 lines
2.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;
|
|
|
|
static SIMS: Lazy<RwLock<sim::Sims>> = Lazy::new(|| RwLock::new(sim::Sims::new()));
|
|
|
|
fn main() {
|
|
let mut sim = Sim {
|
|
board: Board {
|
|
origin: (0, 0),
|
|
size: (4, 4),
|
|
},
|
|
entities: Default::default(),
|
|
entity_counter: 0,
|
|
};
|
|
sim.add_entity(Entity::Ferris(Box::new(Ferris { position: (0, 0) })));
|
|
let sim_id = SIMS.read().new_sim(sim);
|
|
|
|
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::HostFunctions>(
|
|
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());
|
|
|
|
dbg!(SIMS.read().sims.remove(&sim_id));
|
|
|
|
/*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());*/
|
|
}
|