Skip to content

Commit

Permalink
feat: introduce snapshot2 wasi context building
Browse files Browse the repository at this point in the history
  • Loading branch information
ereslibre committed Sep 29, 2023
1 parent efc787a commit bed0714
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 158 deletions.
1 change: 1 addition & 0 deletions crates/runtimes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use errors::Result;

mod modules;
mod runtime;
pub use runtime::CtxBuilder;

use modules::{external::ExternalRuntime, javascript::JavaScriptRuntime, native::NativeRuntime};
use std::path::Path;
Expand Down
34 changes: 24 additions & 10 deletions crates/runtimes/src/modules/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

use crate::errors::{self, Result};

use crate::runtime::Runtime;
use crate::runtime::{CtxBuilder, Runtime};
use std::{
fs,
path::{Path, PathBuf},
};
use wasmtime_wasi::{ambient_authority, Dir, WasiCtxBuilder};
use wasmtime_wasi::{ambient_authority, preview2, Dir};
use wws_project::metadata::Runtime as RuntimeMetadata;
use wws_store::Store;

Expand Down Expand Up @@ -90,14 +90,28 @@ impl Runtime for ExternalRuntime {

/// Mount the source code in the WASI context so it can be
/// processed by the engine
fn prepare_wasi_ctx(&self, builder: &mut WasiCtxBuilder) -> Result<()> {
builder
.preopened_dir(
Dir::open_ambient_dir(&self.store.folder, ambient_authority())?,
"/src",
)?
.args(&self.metadata.args)
.map_err(|_| errors::RuntimeError::WasiContextError)?;
fn prepare_wasi_ctx(&self, builder: &mut CtxBuilder) -> Result<()> {
match builder {
CtxBuilder::Preview1(ref mut builder) => {
builder
.preopened_dir(
Dir::open_ambient_dir(&self.store.folder, ambient_authority())?,
"/src",
)?
.args(&self.metadata.args)
.map_err(|_| errors::RuntimeError::WasiContextError)?;
}
CtxBuilder::Preview2(ref mut builder) => {
builder
.preopened_dir(
Dir::open_ambient_dir(&self.store.folder, ambient_authority())?,
preview2::DirPerms::all(),
preview2::FilePerms::all(),
"/src",
)
.args(&self.metadata.args);
}
}

Ok(())
}
Expand Down
26 changes: 19 additions & 7 deletions crates/runtimes/src/modules/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

use crate::errors::Result;
use crate::runtime::Runtime;
use crate::runtime::{CtxBuilder, Runtime};

use std::path::{Path, PathBuf};
use wasmtime_wasi::{ambient_authority, Dir, WasiCtxBuilder};
use wasmtime_wasi::{ambient_authority, preview2, Dir};
use wws_store::Store;

static JS_ENGINE_WASM: &[u8] =
Expand Down Expand Up @@ -46,11 +46,23 @@ impl Runtime for JavaScriptRuntime {

/// Mount the source code in the WASI context so it can be
/// processed by the engine
fn prepare_wasi_ctx(&self, builder: &mut WasiCtxBuilder) -> Result<()> {
builder.preopened_dir(
Dir::open_ambient_dir(&self.store.folder, ambient_authority())?,
"/src",
)?;
fn prepare_wasi_ctx(&self, builder: &mut CtxBuilder) -> Result<()> {
match builder {
CtxBuilder::Preview1(ref mut builder) => {
builder.preopened_dir(
Dir::open_ambient_dir(&self.store.folder, ambient_authority())?,
"/src",
)?;
}
CtxBuilder::Preview2(ref mut builder) => {
builder.preopened_dir(
Dir::open_ambient_dir(&self.store.folder, ambient_authority())?,
preview2::DirPerms::all(),
preview2::FilePerms::all(),
"/src",
);
}
}

Ok(())
}
Expand Down
9 changes: 7 additions & 2 deletions crates/runtimes/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

use crate::errors::Result;

use wasmtime_wasi::WasiCtxBuilder;
use wasmtime_wasi::{preview2, WasiCtxBuilder};

pub enum CtxBuilder {
Preview1(WasiCtxBuilder),
Preview2(preview2::WasiCtxBuilder),
}

/// Define the behavior a Runtime must have. This includes methods
/// to initialize the environment for the given runtime as well as
Expand All @@ -21,7 +26,7 @@ pub trait Runtime {
/// WASI context builder. This allow runtimes to mount
/// specific lib folders, source code and adding
/// environment variables.
fn prepare_wasi_ctx(&self, _builder: &mut WasiCtxBuilder) -> Result<()> {
fn prepare_wasi_ctx(&self, _builder: &mut CtxBuilder) -> Result<()> {
Ok(())
}

Expand Down
3 changes: 2 additions & 1 deletion crates/server/src/handlers/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ pub async fn handle_worker(req: HttpRequest, body: Bytes) -> HttpResponse {
None => None,
};

let (handler_result, handler_success) = match worker.run(&req, &body_str, store, vars) {
let (handler_result, handler_success) = match worker.run(&req, &body_str, store, vars).await
{
Ok(output) => (output, true),
Err(err) => (WasmOutput::failed(err), false),
};
Expand Down
Loading

0 comments on commit bed0714

Please sign in to comment.