starlane_core/config/
wasm.rs

1use crate::resource::{ResourceKind, ResourceAddress, ArtifactKind};
2use crate::artifact::ArtifactRef;
3use crate::cache::{Cacheable, Data};
4use crate::resource::config::{ResourceConfig, Parser};
5use std::sync::Arc;
6use crate::error::Error;
7use std::str::FromStr;
8use std::convert::TryInto;
9use wasmer::{Cranelift, Universal, Store, Module};
10use starlane_resources::ResourcePath;
11
12pub struct Wasm {
13    pub artifact: ResourcePath,
14    pub module: Arc<Module>
15}
16
17impl Cacheable for Wasm {
18    fn artifact(&self) -> ArtifactRef {
19        ArtifactRef {
20            path: self.artifact.clone(),
21            kind: ArtifactKind::Wasm,
22        }
23    }
24
25    fn references(&self) -> Vec<ArtifactRef> {
26        vec![]
27    }
28}
29
30pub struct WasmCompiler {
31    store: Store
32}
33
34impl WasmCompiler {
35    pub fn new() -> Self {
36        let engine = Universal::new(Cranelift::default()).engine();
37        let store = Store::new(&engine);
38        Self {store}
39    }
40}
41
42impl Parser<Wasm> for WasmCompiler{
43    fn parse(&self, artifact: ArtifactRef, data: Data) -> Result<Arc<Wasm>, Error> {
44
45       let module = Arc::new(Module::new( &self.store, data.as_ref() )?);
46       Ok(Arc::new(Wasm{
47            artifact: artifact.path,
48            module
49        }))
50    }
51}
52
53
54
55