Skip to content

Commit

Permalink
feat: bundle passthrough component in Go directly
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Volosatovs <[email protected]>
  • Loading branch information
rvolosatovs committed Sep 16, 2024
1 parent d5d6293 commit 3c36dc9
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

9 changes: 2 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "west"
version = "0.1.0-alpha.1"
version = "0.1.0-alpha.2"
description = "WebAssembly component test runtime"

authors.workspace = true
Expand Down Expand Up @@ -31,11 +31,6 @@ wasmtime-wasi = { workspace = true }
wasmtime-wasi-http = { workspace = true }
wit-component = { workspace = true }

[build-dependencies]
anyhow = { workspace = true }
wasi-preview1-component-adapter-provider = { workspace = true }
wit-component = { workspace = true }

[workspace.dependencies]
anyhow = "1"
cbindgen = "0.27"
Expand All @@ -49,6 +44,6 @@ wasmtime = "24"
wasmtime-wasi = "24"
wasmtime-wasi-http = "24"
west-passthrough = { version = "0.1.0-alpha.1", path = "./crates/passthrough" }
west = { version = "0.1.0-alpha.1", path = "." }
west = { version = "0.1.0-alpha.2", path = "." }
wit-bindgen = "0.32"
wit-component = "0.217"
53 changes: 0 additions & 53 deletions build.rs

This file was deleted.

12 changes: 1 addition & 11 deletions crates/sys/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::ptr::{self, null_mut};
use std::ffi::CString;
use std::sync::{LazyLock, Mutex};

use crate::{call, instantiate, Config, List, PASSTHROUGH_LEN, PASSTHROUGH_PTR};
use crate::{call, instantiate, Config};

static ERROR: LazyLock<Mutex<Option<CString>>> = LazyLock::new(Mutex::default);

Expand All @@ -15,16 +15,6 @@ fn store_error(err: anyhow::Error) {
.insert(CString::new(format!("{err:?}")).expect("failed to construct error string"));
}

#[no_mangle]
pub extern "C" fn default_config() -> Config {
Config {
wasm: List {
ptr: PASSTHROUGH_PTR,
len: PASSTHROUGH_LEN,
},
}
}

#[no_mangle]
pub extern "C" fn error_take(buf: *mut c_char, len: usize) -> usize {
if let Some(err) = ERROR.lock().unwrap().take() {
Expand Down
3 changes: 0 additions & 3 deletions crates/sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ mod ffi;

static ENGINE: LazyLock<wasmtime::Engine> = LazyLock::new(wasmtime::Engine::default);

pub const PASSTHROUGH_PTR: *const u8 = west::PASSTHROUGH.as_ptr();
pub const PASSTHROUGH_LEN: usize = west::PASSTHROUGH.len();

#[repr(C)]
#[derive(Debug)]
pub struct List<T> {
Expand Down
2 changes: 0 additions & 2 deletions include/west.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ typedef struct Config {
struct List_u8 wasm;
} Config;

struct Config default_config(void);

uintptr_t error_take(char *buf, uintptr_t len);

uintptr_t error_len(void);
Expand Down
11 changes: 0 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ mod bindings {
});
}

pub const PASSTHROUGH: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/west_passthrough.wasm"));

struct Ctx {
wasi: WasiCtx,
http: WasiHttpCtx,
Expand Down Expand Up @@ -175,15 +173,6 @@ pub struct Config<'a> {
pub wasm: &'a [u8],
}

impl Default for Config<'_> {
fn default() -> Self {
Self {
engine: Engine::default(),
wasm: PASSTHROUGH,
}
}
}

pub struct Func<'a> {
func: wasmtime::component::Func,
store: &'a mut Store<Ctx>,
Expand Down
25 changes: 16 additions & 9 deletions west.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//go:generate cargo build -p west-sys --release
//go:generate cargo build -p west-passthrough --target wasm32-unknown-unknown --release
//go:generate wasm-tools component new target/wasm32-unknown-unknown/release/west_passthrough.wasm -o lib/passthrough.wasm
//go:generate go run github.com/ydnar/wasm-tools-go/cmd/[email protected] generate -w imports -o bindings ./wit

package west
Expand All @@ -13,6 +15,7 @@ package west
import "C"

import (
_ "embed"
"errors"
"fmt"
"log"
Expand All @@ -23,6 +26,9 @@ import (
"unsafe"
)

//go:embed lib/passthrough.wasm
var Passthrough []byte

var (
errorHandlerMu sync.RWMutex
errorHandler atomic.Value
Expand Down Expand Up @@ -148,19 +154,20 @@ func NewInstance(conf *Config) (*Instance, error) {
var pinner runtime.Pinner
defer pinner.Unpin()

c := C.default_config()
wasm := Passthrough
if conf != nil {
if len(conf.Wasm) > 0 {
ptr := unsafe.SliceData(conf.Wasm)
pinner.Pin(ptr)

c.wasm = C.List_u8{
ptr: (*C.uchar)(ptr),
len: C.ulong(len(conf.Wasm)),
}
wasm = conf.Wasm
}
}
ptr := C.instance_new(c)
wasmPtr := unsafe.SliceData(wasm)
pinner.Pin(wasmPtr)
ptr := C.instance_new(C.Config{
wasm: C.List_u8{
ptr: (*C.uchar)(wasmPtr),
len: C.ulong(len(wasm)),
},
})
if ptr == nil {
n := C.error_len()
buf := make([]C.char, n)
Expand Down

0 comments on commit 3c36dc9

Please sign in to comment.