Skip to content

Commit

Permalink
wip: import module support
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohe-Am committed Apr 24, 2024
1 parent b582a43 commit 30ff471
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 175 deletions.
2 changes: 1 addition & 1 deletion .ghjk/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
envs
hash.json
hash.json
93 changes: 0 additions & 93 deletions .ghjk/lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -392,37 +392,6 @@
"crateName": "whiz",
"locked": true
},
"c9f51792ad7f2c2d6f64976a8b712ae3e37f07b568ff409013dacbd285ac19a0": {
"version": "17.0.3",
"depConfigs": {
"cargo_binstall_ghrel": {
"version": "v1.6.4",
"depConfigs": {},
"portRef": "cargo_binstall_ghrel@0.1.0"
},
"rust_rustup": {
"version": "1.77.2",
"depConfigs": {
"rustup_rustlang": {
"version": "1.27.0",
"depConfigs": {
"git_aa": {
"version": "2.40.1",
"depConfigs": {},
"portRef": "git_aa@0.1.0"
}
},
"portRef": "rustup_rustlang@0.1.0"
}
},
"portRef": "rust_rustup@0.1.0",
"profile": "minimal"
}
},
"portRef": "cargobi_cratesio@0.1.0",
"crateName": "wasmtime-cli",
"locked": true
},
"3552829e559260faef01394d084d1c64d8da0b0fde33000127ae676322402254": {
"version": "0.13.3",
"depConfigs": {
Expand Down Expand Up @@ -462,68 +431,6 @@
}
},
"portRef": "cpy_bs_ghrel@0.1.0"
},
"f1552d80bc05967757fc8f6530cfb4a0f875a6e6d46167eedc2530b533abac26": {
"version": "19.0.2",
"depConfigs": {
"cargo_binstall_ghrel": {
"version": "v1.6.4",
"depConfigs": {},
"portRef": "cargo_binstall_ghrel@0.1.0"
},
"rust_rustup": {
"version": "1.77.2",
"depConfigs": {
"rustup_rustlang": {
"version": "1.27.0",
"depConfigs": {
"git_aa": {
"version": "2.40.1",
"depConfigs": {},
"portRef": "git_aa@0.1.0"
}
},
"portRef": "rustup_rustlang@0.1.0"
}
},
"portRef": "rust_rustup@0.1.0",
"profile": "minimal"
}
},
"portRef": "cargobi_cratesio@0.1.0",
"crateName": "wasmtime-cli",
"locked": true
},
"d7e75b813748aaecfc889b73be7e9615eadceb4790eacb449f0c3a1bb24a5c42": {
"version": "19.0.0",
"depConfigs": {
"cargo_binstall_ghrel": {
"version": "v1.6.4",
"depConfigs": {},
"portRef": "cargo_binstall_ghrel@0.1.0"
},
"rust_rustup": {
"version": "1.77.2",
"depConfigs": {
"rustup_rustlang": {
"version": "1.27.0",
"depConfigs": {
"git_aa": {
"version": "2.40.1",
"depConfigs": {},
"portRef": "git_aa@0.1.0"
}
},
"portRef": "rustup_rustlang@0.1.0"
}
},
"portRef": "rust_rustup@0.1.0",
"profile": "minimal"
}
},
"portRef": "cargobi_cratesio@0.1.0",
"crateName": "wasmtime-cli",
"locked": true
}
}
},
Expand Down
17 changes: 9 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions typegate/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ typescript.workspace = true

wasmtime = { workspace = true, features = ["component-model"] }
wasmtime-wasi.workspace = true
# remove when bumping wasmtime to 20
cap-std = "3"

shadow-rs.workspace = true
serde.workspace = true
Expand Down
8 changes: 6 additions & 2 deletions typegate/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ impl OpDepInjector {
.unwrap(),
)
.expect("invalid wasmtime engine config");
state.put(runtimes::wit_wire::Ctx::new(engine));
state.put(runtimes::prisma::Ctx::new(self.tmp_dir.unwrap()));
let tmp_dir = self.tmp_dir.unwrap();
state.put(runtimes::wit_wire::Ctx::new(
engine,
tmp_dir.join("wit_wire_workdir"),
));
state.put(runtimes::prisma::Ctx::new(tmp_dir));
}
}

Expand Down
61 changes: 35 additions & 26 deletions typegate/engine/src/runtimes/wit_wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,19 @@ pub struct Ctx {
engine: wasmtime::Engine,
instances: Arc<DashMap<String, Instance>>,
components: Arc<DashMap<String, LinkedComponent>>,
instance_workdir: PathBuf,
}

#[derive(Clone)]
struct LinkedComponent(Component, Arc<Linker<TypegateHost>>);

/* static WASI_CTX: once_cell::sync::Lazy<wasmtime_wasi::WasiCtx> = once_cell::sync::Lazy::new(|| {
wasmtime_wasi::WasiCtxBuilder::new()
.allow_ip_name_lookup(true)
.build()
}); */
struct LinkedComponent(Component, Arc<Linker<InstanceState>>);

impl Ctx {
pub fn new(engine: wasmtime::Engine) -> Self {
pub fn new(engine: wasmtime::Engine, instance_workdir: PathBuf) -> Self {
Self {
instances: Default::default(),
components: Default::default(),
engine,
instance_workdir,
}
}

Expand Down Expand Up @@ -88,11 +84,11 @@ impl Ctx {
})?
}
};
let mut linker = Linker::<TypegateHost>::new(&self.engine);
let mut linker = Linker::<InstanceState>::new(&self.engine);

for res in [
wasmtime_wasi::bindings::Imports::add_to_linker(&mut linker, |host| host),
wit::Pyrt::add_to_linker(&mut linker, |host| host),
wasmtime_wasi::bindings::Imports::add_to_linker(&mut linker, |state| state),
wit::Pyrt::add_to_linker(&mut linker, |state| &mut state.tg_host),
] {
res.map_err(|err| format!("erorr trying to link component: {err}"))?;
}
Expand All @@ -108,37 +104,37 @@ impl Ctx {
struct Instance {
bindings: wit::Pyrt,
_instance: wasmtime::component::Instance,
store: wasmtime::Store<TypegateHost>,
store: wasmtime::Store<InstanceState>,
}

struct TypegateHost {
struct InstanceState {
table: wasmtime_wasi::ResourceTable,
ctx: wasmtime_wasi::WasiCtx,
tg_host: TypegateHost,
}

impl TypegateHost {
fn new() -> Self {
impl InstanceState {
fn new(preopen_dir: impl AsRef<Path>, tg_host: TypegateHost) -> Self {
Self {
ctx: wasmtime_wasi::WasiCtxBuilder::new()
.allow_ip_name_lookup(true)
.preopened_dir(
cap_std::fs::Dir::open_ambient_dir(preopen_dir, cap_std::ambient_authority())
.unwrap(),
wasmtime_wasi::DirPerms::all(),
wasmtime_wasi::FilePerms::all(),
".",
)
// TODO: stream stdio to debug log
.inherit_stdio()
.build(),
table: Default::default(),
tg_host,
}
}
}

#[async_trait::async_trait]
impl Host for TypegateHost {
async fn hostcall(&mut self, _req: HostReq) -> wasmtime::Result<HostRes> {
todo!()
}
}

impl wit::metatype::pyrt::shared::Host for TypegateHost {}

impl wasmtime_wasi::WasiView for TypegateHost {
impl wasmtime_wasi::WasiView for InstanceState {
fn table(&mut self) -> &mut wasmtime_wasi::ResourceTable {
&mut self.table
}
Expand All @@ -153,6 +149,16 @@ pub struct WitWireInitArgs {
metatype_version: String,
expected_ops: Vec<WitWireMatInfo>,
}
struct TypegateHost {}

#[async_trait::async_trait]
impl Host for TypegateHost {
async fn hostcall(&mut self, _req: HostReq) -> wasmtime::Result<HostRes> {
todo!()
}
}

impl wit::metatype::pyrt::shared::Host for TypegateHost {}

impl From<WitWireInitArgs> for InitArgs {
fn from(value: WitWireInitArgs) -> Self {
Expand Down Expand Up @@ -240,7 +246,10 @@ pub async fn op_wit_wire_init(
.await
.map_err(WitWireInitError::ModuleErr)?;

let mut store = wasmtime::Store::new(&ctx.engine, TypegateHost::new());
let mut store = wasmtime::Store::new(
&ctx.engine,
InstanceState::new(ctx.instance_workdir.join(&instance_id), TypegateHost {}),
);
let (bindings, instance) = wit::Pyrt::instantiate_async(&mut store, component, linker)
.await
.map_err(|err| {
Expand Down
Loading

0 comments on commit 30ff471

Please sign in to comment.