Skip to content

Commit

Permalink
Add github actions
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <[email protected]>
  • Loading branch information
rylev committed Sep 12, 2023
1 parent 4695ade commit 68e4c15
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 27 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Build and Test
on:
push:
branches:
- main
pull_request: {}

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout the source code
uses: actions/checkout@v2
with:
fetch-depth: 1
submodules: recursive

- name: Installs nightly toolchain
run: |
rustup update nightly
rustup default nightly
- name: Formatting check
run: cargo fmt --all -- --check

- name: Run tests
run: cargo test --all

11 changes: 6 additions & 5 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<'a> Cmd<'a> {
pub fn run(
self,
runtime: &mut Runtime,
querier: &Querier,
querier: &mut Querier,
scope: &mut HashMap<String, Val>,
) -> anyhow::Result<bool> {
match self {
Expand Down Expand Up @@ -143,6 +143,7 @@ impl<'a> Cmd<'a> {
let adapter =
std::fs::read(path).context("could not read path to adapter module")?;
runtime.compose(&adapter)?;
*querier = Querier::from_bytes(runtime.component_bytes())?;
}
Cmd::BuiltIn { name: "link", args } => {
let &[func_name, component] = args.as_slice() else {
Expand All @@ -166,11 +167,11 @@ impl<'a> Cmd<'a> {
}
}

fn eval<'a>(
fn eval(
runtime: &mut Runtime,
querier: &Querier,
scope: &HashMap<String, Val>,
expr: parser::Expr<'a>,
expr: parser::Expr<'_>,
preferred_type: Option<&wit_parser::Type>,
) -> anyhow::Result<Val> {
match expr {
Expand All @@ -196,12 +197,12 @@ fn lookup_in_scope(scope: &HashMap<String, Val>, ident: &str) -> anyhow::Result<
.cloned()
}

fn call_func<'a>(
fn call_func(
runtime: &mut Runtime,
querier: &Querier,
scope: &HashMap<String, Val>,
name: &str,
args: Vec<parser::Expr<'a>>,
args: Vec<parser::Expr<'_>>,
) -> anyhow::Result<Vec<Val>> {
log::debug!("Calling function: {name} with args: {args:?}");
let func_def = querier
Expand Down
4 changes: 2 additions & 2 deletions src/command/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ impl<'a> Literal<'a> {
}
}

fn assignment<'a>(input: &'a str) -> nom::IResult<&str, (&str, Expr<'a>)> {
fn assignment(input: &str) -> nom::IResult<&str, (&str, Expr<'_>)> {
let (rest, ident) = ident(input)?;
let (rest, _) = delimited(multispace0, tag("="), multispace0)(rest)?;
let (r, value) = cut(Expr::parse)(rest)?;
Ok((r, (ident, value)))
}

pub fn function_call<'a>(input: &'a str) -> nom::IResult<&str, (&str, Vec<Expr<'a>>)> {
pub fn function_call(input: &str) -> nom::IResult<&str, (&str, Vec<Expr<'_>>)> {
let (rest, ident) = ident(input)?;
let args = separated_list0(tag(","), Expr::parse);
let (rest, args) = delimited(tag("("), args, tag(")"))(rest)?;
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn _main() -> anyhow::Result<()> {

let cli = Cli::parse();
let component_bytes = std::fs::read(cli.component)?;
let querier = wit::Querier::from_bytes(&component_bytes)?;
let mut querier = wit::Querier::from_bytes(&component_bytes)?;
let mut runtime = runtime::Runtime::init(component_bytes, &querier, |import_name| {
print_error_prefix();
eprintln!("unimplemented import: {import_name}");
Expand All @@ -45,7 +45,7 @@ fn _main() -> anyhow::Result<()> {
let line = command::Cmd::parse(&line);
match line {
Ok(cmd) => {
match cmd.run(&mut runtime, &querier, &mut scope) {
match cmd.run(&mut runtime, &mut querier, &mut scope) {
Err(e) => {
print_error_prefix();
eprintln!("{e}");
Expand Down
19 changes: 18 additions & 1 deletion src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ impl Runtime {
Ok(())
})?;
}
wit_parser::WorldItem::Interface(i) => {
let interface = querier.interface(*i).unwrap();
let mut root = linker.root();
let mut instance = root.instance(&import_name)?;
for (_, f) in interface.functions.iter() {
let stub_import = stub_import.clone();
let import_name = import_name.clone();
instance.func_new(&component, &f.name, move |_ctx, _args, _rets| {
stub_import(&import_name);
Ok(())
})?;
}
}
i => todo!("Implement import: {i:?}"),
}
}
Expand Down Expand Up @@ -87,7 +100,7 @@ impl Runtime {
/// This function does not check that the component in `components_bytes` has the
/// export needed and that it doesn't have any non-wasi imports.
pub fn stub_function(&mut self, name: String, component_bytes: &[u8]) -> anyhow::Result<()> {
let component = load_component(&self.engine, &component_bytes)?;
let component = load_component(&self.engine, component_bytes)?;
let mut linker = Linker::<ImportImplsContext>::new(&self.engine);
wasmtime_wasi::preview2::command::sync::add_to_linker(&mut linker)?;
let instance =
Expand Down Expand Up @@ -134,6 +147,10 @@ impl Runtime {
self.set_component(bytes)
}

pub fn component_bytes(&self) -> &[u8] {
&self.component.1
}

/// Get a new instance
pub fn refresh(&mut self) -> anyhow::Result<()> {
self.store = build_store(&self.engine);
Expand Down
32 changes: 15 additions & 17 deletions src/wit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;

use anyhow::Context;
use wit_component::DecodedWasm;
use wit_parser::{Function, Resolve, World, WorldId, WorldItem, WorldKey};
use wit_parser::{Function, InterfaceId, Resolve, World, WorldId, WorldItem, WorldKey};

pub struct Querier {
resolve: Resolve,
Expand All @@ -21,7 +21,7 @@ impl Querier {
}

pub fn from_bytes(component_bytes: &[u8]) -> anyhow::Result<Self> {
let (resolve, world) = match wit_component::decode(&component_bytes)
let (resolve, world) = match wit_component::decode(component_bytes)
.context("could not decode given file as a WebAssembly component")?
{
DecodedWasm::Component(r, w) => (r, w),
Expand Down Expand Up @@ -54,6 +54,10 @@ impl Querier {
get_world_item_by_name(self.world().imports.iter(), name)
}

pub fn interface(&self, id: InterfaceId) -> Option<&wit_parser::Interface> {
self.resolve.interfaces.get(id)
}

pub fn display_wit_type<'a>(&self, param_type: &wit_parser::Type) -> Cow<'a, str> {
let str = match param_type {
wit_parser::Type::Bool => "bool",
Expand Down Expand Up @@ -87,7 +91,7 @@ impl Querier {
match (ok, err) {
(Some(ok), Some(err)) => format!("result<{ok}, {err}>"),
(Some(t), _) | (_, Some(t)) => format!("result<{t}>"),
_ => format!("result"),
_ => "result".into(),
}
}
wit_parser::TypeDefKind::Type(t) => return self.display_wit_type(t),
Expand Down Expand Up @@ -132,22 +136,16 @@ impl Querier {
.imports
.iter()
.filter_map(|(import_name, import)| {
let import_name = match import_name {
WorldKey::Name(n) => n.clone(),
WorldKey::Interface(i) => {
let interface = self.resolve.interfaces.get(*i).unwrap();
match &interface.package {
Some(package_id) => {
let package = self.resolve.packages.get(*package_id).unwrap();
if package.name.namespace == "wasi" {
return None;
}
format!("{}", package.name)
}
None => todo!(),
if let WorldKey::Interface(i) = import_name {
let interface = self.resolve.interfaces.get(*i).unwrap();
if let Some(package_id) = &interface.package {
let package = self.resolve.packages.get(*package_id).unwrap();
if package.name.namespace == "wasi" {
return None;
}
}
};
}
let import_name = self.resolve.name_world_key(import_name);
Some((import_name, import))
})
}
Expand Down

0 comments on commit 68e4c15

Please sign in to comment.