Skip to content

Commit

Permalink
switch to wit impl, add rust and tinygo examples
Browse files Browse the repository at this point in the history
  • Loading branch information
robherley committed Jan 27, 2025
1 parent 3e9b40a commit 16f3e71
Show file tree
Hide file tree
Showing 24 changed files with 242 additions and 145 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ on:

env:
CARGO_TERM_COLOR: always
TINYGO_VERSION: "0.35.0"

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- run: |
wget https://github.com/tinygo-org/tinygo/releases/download/v$TINYGO_VERSION/tinygo_$TINYGO_VERSION_amd64.deb
sudo dpkg -i tinygo_$TINYGO_VERSION_amd64.deb
- run: script/build-examples
- run: cargo build --verbose
- run: cargo test --verbose
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
examples/*/dist/

wit/deps/
tmp/

# Created by https://www.toptal.com/developers/gitignore/api/rust,visualstudiocode,macos
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ log = "0.4.22"
thiserror = "2.0.5"
tokio = { version = "1.42.0", features = ["io-util", "rt", "macros"] }
tokio-stream = "0.1.17"
wasi-common = "28.0.0"
wasmtime = { version = "28.0.0", features = ["async", "cache"] }
wasmtime-wasi = "28.0.0"
wasi-common = "29.0.1"
wasmtime = { version = "29.0.1", features = ["async", "cache"] }
wasmtime-wasi = "29.0.1"
18 changes: 0 additions & 18 deletions examples/go-hello-world/go.mod

This file was deleted.

33 changes: 0 additions & 33 deletions examples/go-hello-world/go.sum

This file was deleted.

29 changes: 0 additions & 29 deletions examples/go-hello-world/main.go

This file was deleted.

7 changes: 7 additions & 0 deletions examples/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "funcgg_example_rust"
version = "0.1.0"
edition = "2021"

[dependencies]
wit-bindgen = "0.38.0"
16 changes: 16 additions & 0 deletions examples/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
wit_bindgen::generate!({
world: "run",
path: "../../wit/funcgg.wit",
});

// https://github.com/bytecodealliance/wasi-rs

fn main() {
funcgg::runtime::responder::set_header("X-Foo", "bar");
funcgg::runtime::responder::set_status(200);

println!("Environment variables:");
for (key, value) in std::env::vars() {
println!("{}: {}", key, value);
}
}
3 changes: 3 additions & 0 deletions examples/tinygo/gen/funcgg/runtime/responder/empty.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file exists for testing this package without WebAssembly,
// allowing empty function bodies with a //go:wasmimport directive.
// See https://pkg.go.dev/cmd/compile for more information.
13 changes: 13 additions & 0 deletions examples/tinygo/gen/funcgg/runtime/responder/responder.wasm.go

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

56 changes: 56 additions & 0 deletions examples/tinygo/gen/funcgg/runtime/responder/responder.wit.go

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

4 changes: 4 additions & 0 deletions examples/tinygo/gen/funcgg/runtime/run/run.wit.go

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

5 changes: 5 additions & 0 deletions examples/tinygo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/robherley/func.gg/examples/tinygo

go 1.23.1

require go.bytecodealliance.org/cm v0.1.0
2 changes: 2 additions & 0 deletions examples/tinygo/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go.bytecodealliance.org/cm v0.1.0 h1:78Rk4d5rgir5Hm+LMFpDWhjmFBWrKDFPSKUwDBj+nwo=
go.bytecodealliance.org/cm v0.1.0/go.mod h1:NZ2UT0DyGhBfpIPOxPMCuG6g1YTR4YF3xweD7mHX5VQ=
29 changes: 29 additions & 0 deletions examples/tinygo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
"time"

"github.com/robherley/func.gg/examples/tinygo/gen/funcgg/runtime/responder"
)

//go:generate wit-bindgen-go generate -o gen/ ../../wit/

func main() {
responder.SetStatus(200)
responder.SetHeader("Content-Type", "application/json")

fmt.Println("{")

x := 26
for i := range x {
fmt.Printf(" %q: %d", string('A'+i), i)
if i < x-1 {
fmt.Print(",")
}
fmt.Println()
time.Sleep(1 * time.Second)
}

fmt.Println("}")
}
29 changes: 15 additions & 14 deletions script/build-examples
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
set -eo pipefail

ROOT=$(realpath "$(dirname "$0")/..")
TMP="$ROOT/tmp"

build_go_hello_world() {
DIR="$ROOT/examples/go-hello-world"
build_tinygo() {
DIR="$ROOT/examples/tinygo"
DIST="$DIR/dist"
mkdir -p "$DIST"
echo "Building $DIST/main.go"
GOOS=wasip1 GOARCH=wasm go build -o "$DIST/main.wasm" "$DIR/main.go"

if [ -z "$CI" ]; then
echo "Building $DIST/main.tinygo.wasm"
tinygo build -target=wasip2 -o "$DIST/main.tinygo.wasm" "$DIR/main.go"
fi
pushd "$DIR" > /dev/null
echo "Building tinygo example..."
tinygo build -target=wasip2 -o "$DIST/example.tinygo.wasm" --wit-package ../../wit/ --wit-world functions
popd > /dev/null
}

build_go_hello_world

mkdir -p "$TMP"
rm -rf "$TMP/compile-cache"
build_rust() {
DIR="$ROOT/examples/rust"
pushd "$DIR" > /dev/null
echo "Building rust example..."
cargo build --release --target wasm32-wasip2
popd > /dev/null
}

# build_tinygo
build_rust
17 changes: 17 additions & 0 deletions script/init
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

set -eo pipefail

ROOT=$(realpath "$(dirname "$0")/..")

dependencies=(cargo wasm-tools wkg tinygo)

for dep in "${dependencies[@]}"; do
if ! command -v "$dep" &> /dev/null; then
echo "error: $dep is not installed." >&2
exit 1
fi
done

"$ROOT/script/wit-fetch"
"$ROOT/script/build-examples"
5 changes: 5 additions & 0 deletions script/server
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

set -eo pipefail

cargo run
14 changes: 14 additions & 0 deletions script/wit-fetch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

set -eo pipefail

ROOT=$(realpath "$(dirname "$0")/..")

if ! command -v wkg &> /dev/null; then
echo "wkg is not installed: https://github.com/bytecodealliance/wasm-pkg-tools"
exit 1
fi

pushd "$ROOT" > /dev/null
wkg wit fetch
popd > /dev/null
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ pub mod streams;

pub mod wit {
wasmtime::component::bindgen!({
world: "runner",
path: "wit/funcgg.wit",
world: "run",
path: "wit/",
// with: {
// "wasi": wasmtime_wasi::bindings,
// },
// include_generated_code_from_file: true,
async: true,
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use tokio::sync::mpsc::channel;
// https://tokio.rs/tokio/topics/shutdown
#[post("/")] // note: default payload limit is 256kB from actix-web, but is configurable with PayloadConfig
async fn handle(mut body: web::Payload) -> Result<impl Responder, Error> {
let binary = include_bytes!("../examples/go-hello-world/dist/main.tinygo.wasm");
let binary =
include_bytes!("../examples/rust/target/wasm32-wasip2/release/funcgg_example_rust.wasm");
let mut sandbox = Sandbox::new(binary.to_vec())?;

let (stdin, input_tx) = InputStream::new();
Expand Down
Loading

0 comments on commit 16f3e71

Please sign in to comment.