Skip to content

Commit

Permalink
tinygo wip
Browse files Browse the repository at this point in the history
  • Loading branch information
robherley committed Jan 23, 2025
1 parent f1b80f4 commit 3e9b40a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 22 deletions.
15 changes: 15 additions & 0 deletions examples/go-hello-world/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
module github.com/robherley/func.gg/examples/go-hello-world

go 1.23.1

require (
github.com/coreos/go-semver v0.3.1 // indirect
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/regclient/regclient v0.7.2 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/urfave/cli/v3 v3.0.0-beta1 // indirect
go.bytecodealliance.org v0.5.0 // indirect
go.bytecodealliance.org/cm v0.1.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sys v0.28.0 // indirect
)
33 changes: 33 additions & 0 deletions examples/go-hello-world/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 h1:UhxFibDNY/bfvqU5CAUmr9zpesgbU6SWc8/B4mflAE4=
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7/go.mod h1:cyGadeNEkKy96OOhEzfZl+yxihPEzKnqJwvfuSUqbZE=
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/regclient/regclient v0.7.2 h1:vcldDAwBMLtighYVMeb6qNt5+0hKg3AN2IkCc0JIJNM=
github.com/regclient/regclient v0.7.2/go.mod h1:QlA7W9/pvmbblOXM4d49JgfuOTwVXcUMKt3bFuOSVIQ=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/urfave/cli/v3 v3.0.0-beta1 h1:6DTaaUarcM0wX7qj5Hcvs+5Dm3dyUTBbEwIWAjcw9Zg=
github.com/urfave/cli/v3 v3.0.0-beta1/go.mod h1:FnIeEMYu+ko8zP1F9Ypr3xkZMIDqW3DR92yUtY39q1Y=
go.bytecodealliance.org v0.5.0 h1:ywhCpF0JdqGkqct5JqSY1Me8lz001UIJXUaCSS32cew=
go.bytecodealliance.org v0.5.0/go.mod h1:8kYTSxmQr8DU3dKOKCOHH1Ap1gWX/61qlFSbIuIno2Q=
go.bytecodealliance.org/cm v0.1.0 h1:78Rk4d5rgir5Hm+LMFpDWhjmFBWrKDFPSKUwDBj+nwo=
go.bytecodealliance.org/cm v0.1.0/go.mod h1:NZ2UT0DyGhBfpIPOxPMCuG6g1YTR4YF3xweD7mHX5VQ=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
31 changes: 19 additions & 12 deletions examples/go-hello-world/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@ package main

import (
"fmt"
"time"
"os"
)

func main() {
fmt.Println("{")
func init() {}

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)
//go:wasmexport foo
func main() {
for _, v := range os.Environ() {
fmt.Println(v)
}

fmt.Println("}")
// 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("}")
}
2 changes: 1 addition & 1 deletion script/build-examples
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ build_go_hello_world() {

if [ -z "$CI" ]; then
echo "Building $DIST/main.tinygo.wasm"
GOOS=wasip1 GOARCH=wasm tinygo build -target wasi -o "$DIST/main.tinygo.wasm" "$DIR/main.go"
tinygo build -target=wasip2 -o "$DIST/main.tinygo.wasm" "$DIR/main.go"
fi
}

Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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.wasm");
let binary = include_bytes!("../examples/go-hello-world/dist/main.tinygo.wasm");
let mut sandbox = Sandbox::new(binary.to_vec())?;

let (stdin, input_tx) = InputStream::new();
Expand Down Expand Up @@ -44,7 +44,9 @@ async fn handle(mut body: web::Payload) -> Result<impl Responder, Error> {
});

spawn(async move {
sandbox.call(stdin, stdout).await?;
if let Err(err) = sandbox.call(stdin, stdout).await {
error!("sandbox error: {:?}", err);
}
Ok::<(), Error>(())
});

Expand Down
16 changes: 9 additions & 7 deletions src/runtime/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,7 @@ impl Sandbox {
let mut store = Store::new(&self.engine, state);
store.set_epoch_deadline(1);

let instance = self
.linker
.instantiate_async(&mut store, &self.component)
.await?;

let func = instance.get_typed_func::<(), ()>(&mut store, "_start")?;
let (finished_tx, finished_rx) = tokio::sync::oneshot::channel::<()>();

spawn({
let weak_engine = self.engine.weak();
async move {
Expand All @@ -119,6 +112,15 @@ impl Sandbox {
}
});

warn!("init");
let instance = self
.linker
.instantiate_async(&mut store, &self.component)
.await?;
warn!("running...");

let func = instance.get_typed_func::<(), ()>(&mut store, "wasi:cli/[email protected]#run")?;

let result = func.call_async(&mut store, ()).await;
_ = finished_tx.send(());

Expand Down

0 comments on commit 3e9b40a

Please sign in to comment.