Skip to content

Commit 75e9e57

Browse files
authored
Revert "remove go ABI for now" (#65)
* Revert "remove go ABI for now (#26)" This reverts commit b4fc035. * Update README.md * ln upgrade, cleanups * remove this broken test for now * internal/abi/wasmgo: update, support go 1.12.x * update xena/wasm-rust-tools * fix build * use modules * oh no * cwa/tests/olintest/http: ping christine.website instead of printerfacts * fix travis better * internal/abi/wasmgo: rip out dagger, add CWA * enable go for cmd/cwa * cmd/cwa: support running go modules * final fixes * remove last unused function * fix build
1 parent ae5c523 commit 75e9e57

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+856
-10202
lines changed

.travis.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
sudo: required
22

3+
env:
4+
- GOPROXY=https://cache.greedo.xeserv.us GO111MODULE=on
5+
36
services:
47
- docker
58

@@ -8,4 +11,4 @@ language: go
811
go:
912
- "1.x"
1013

11-
script: docker build .
14+
script: docker build --no-cache .

Dockerfile

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ RUN wasm-gc ./olinfetch.wasm \
1414
&& wasm-gc ./cwa-tests.wasm \
1515
&& du -hs ./*.wasm
1616

17-
FROM xena/go:1.11.2 AS go
17+
FROM xena/go:1.12.1 AS go
1818
RUN apk add --no-cache build-base
19-
COPY . /root/go/src/github.com/Xe/olin
20-
WORKDIR /root/go/src/github.com/Xe/olin
19+
ENV GOPROXY https://cache.greedo.xeserv.us
20+
WORKDIR /olin
21+
COPY . .
2122
COPY --from=rust-wasm-tools /olin/cwagi.wasm ./cmd/cwa-cgi/testdata/test.wasm
2223
COPY --from=rust-wasm-tools /olin/cwa-tests.wasm ./cmd/cwa/testdata/test.wasm
24+
RUN GOARCH=wasm GOOS=js go build -o ./cmd/cwa/testdata/go.wasm ./internal/abi/wasmgo/testdata/nothing.go
2325
RUN go test -v ./cmd/... ./internal/...
24-
RUN GOBIN=/usr/local/bin go install -tags heroku ./cmd/cwa-cgi
26+
RUN GOBIN=/usr/local/bin go install ./cmd/cwa-cgi
2527
RUN GOBIN=/usr/local/bin go install ./cmd/cwa
2628

2729
FROM xena/alpine

README.md

+41
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,47 @@ extern int write(int fd, void *buf, int nbyte);
7272
extern int sync(int fd);
7373
```
7474
75+
### Go
76+
77+
Olin also includes support for running webassembly modules created by [Go 1.12's webassembly support](https://golang.org/wiki/WebAssembly).
78+
It uses [the `wasmgo` ABI][wasmgo] package in order to do things. Right now
79+
this is incredibly basic, but should be extendable to more things in the future.
80+
81+
As an example:
82+
83+
```go
84+
// +build js,wasm ignore
85+
// hello_world.go
86+
87+
package main
88+
89+
func main() {
90+
println("Hello, world!")
91+
}
92+
```
93+
94+
when compiled like this:
95+
96+
```console
97+
$ GOARCH=wasm GOOS=js go1.12.1 build -o hello_world.wasm hello_world.go
98+
```
99+
100+
produces the following output when run with the testing shim:
101+
102+
```
103+
=== RUN TestWasmGo/github.com/Xe/olin/internal/abi/wasmgo.testHelloWorld
104+
Hello, world!
105+
--- PASS: TestWasmGo (1.66s)
106+
--- PASS: TestWasmGo/github.com/Xe/olin/internal/abi/wasmgo.testHelloWorld (1.66s)
107+
```
108+
109+
Currently Go binaries cannot interface with the Dagger ABI. There is [an issue](https://github.com/Xe/olin/issues/5)
110+
open to track the solution to this.
111+
112+
Future posts will include more detail about using Go on top of Olin.
113+
114+
Under the hood, the Olin implementation of the Go ABI currently uses Dagger.
115+
75116
## Project Meta
76117

77118
To follow the project, check it on GitHub [here][olin]. To talk about it on Slack,

cmd/cwa-cgi/heroku.go

-21
This file was deleted.

cmd/cwa-cgi/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
"net/http"
1010
"os"
1111

12-
"github.com/Xe/ln"
13-
"github.com/Xe/ln/opname"
1412
"github.com/Xe/olin/internal/cwagi"
13+
"within.website/ln"
14+
"within.website/ln/opname"
1515
)
1616

1717
var (

cmd/cwa-cgi/main_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import (
1313
"testing"
1414
"time"
1515

16-
"github.com/Xe/ln"
17-
"github.com/Xe/ln/opname"
1816
"github.com/Xe/olin/internal/abi/cwa"
1917
"github.com/pborman/uuid"
2018
"github.com/perlin-network/life/compiler"
2119
"github.com/perlin-network/life/exec"
20+
"within.website/ln"
21+
"within.website/ln/opname"
2222
)
2323

2424
type vmServer struct {

cmd/cwa/main.go

+59-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/Xe/olin/internal/abi/cwa"
14+
"github.com/Xe/olin/internal/abi/wasmgo"
1415
"github.com/perlin-network/life/compiler"
1516
"github.com/perlin-network/life/exec"
1617
)
@@ -22,6 +23,7 @@ var (
2223
doTest = flag.Bool("test", false, "unit testing?")
2324
vmStats = flag.Bool("vm-stats", false, "dump VM statistics?")
2425
gas = flag.Int("gas", 65536*64, "number of instructions the VM can perform")
26+
goMode = flag.Bool("go", false, "run in Go mode?")
2527
)
2628

2729
func init() {
@@ -71,7 +73,7 @@ func main() {
7173
}
7274
}
7375

74-
p := cwa.NewProcess(fname, argv, environment)
76+
p := wasmgo.New(fname, argv, environment)
7577

7678
if *doTest {
7779
p.Stdin = bytes.NewBuffer([]byte("cwa test environment"))
@@ -90,39 +92,81 @@ func main() {
9092
log.Fatalf("%s: %v", fname, err)
9193
}
9294

95+
var ret int32
96+
if *goMode {
97+
ret, err = runGo(p, vm)
98+
} else {
99+
ret, err = runCWA(p.Process, vm)
100+
}
101+
vmRunTime := time.Since(t0)
102+
if err != nil {
103+
log.Fatalf("%s: vm error: %v", fname, err)
104+
}
105+
if *vmStats || *doTest {
106+
log.Printf("reading file time: %s", readingFileTime)
107+
log.Printf("vm init time: %s", vmInitTime)
108+
log.Printf("vm gas limit: %v", *gas)
109+
log.Printf("vm gas used: %v", vm.Gas)
110+
log.Printf("vm gas percentage: %v", float64(float64(vm.Gas) / float64(*gas)))
111+
log.Printf("execution time: %s", vmRunTime)
112+
}
113+
114+
if ret != 0 {
115+
log.Fatalf("%s: exit status %d", fname, ret)
116+
}
117+
118+
if *vmStats {
119+
log.Printf("memory pages: %d", len(vm.Memory)/65536)
120+
}
121+
122+
os.Exit(int(ret))
123+
}
124+
125+
func runCWA(p *cwa.Process, vm *exec.VirtualMachine) (int32, error) {
93126
if *doTest {
94127
log.Printf("loading function %s", *mainFunc)
95128
}
96129

97130
main, ok := vm.GetFunctionExport(*mainFunc)
98131
if !ok {
99-
log.Fatalf("%s: no main function exported", fname)
132+
return -1, fmt.Errorf("%s: no main function exported", p.Name())
100133
}
101134

102135
if *doTest {
103136
log.Printf("executing %s (%d)", *mainFunc, main)
104137
}
105138

106-
t0 = time.Now()
107139
ret, err := vm.RunWithGasLimit(main, *gas)
108-
vmRunTime := time.Since(t0)
109140
if err != nil {
110-
log.Fatalf("%s: vm error: %v", fname, err)
141+
return 1, err
111142
}
112-
if *vmStats || *doTest {
113-
log.Printf("reading file time: %s", readingFileTime)
114-
log.Printf("vm init time: %s", vmInitTime)
115-
log.Printf("vm gas used: %v", vm.Gas)
116-
log.Printf("execution time: %s", vmRunTime)
143+
144+
return int32(ret), nil
145+
}
146+
147+
func runGo(w *wasmgo.WasmGo, vm *exec.VirtualMachine) (int32, error) {
148+
log.Printf("starting wasmgo...")
149+
w.Memory.Data = vm.Memory
150+
151+
run, ok := vm.GetFunctionExport("run")
152+
if !ok {
153+
panic("function not found: run")
117154
}
118155

119-
if ret != 0 {
120-
log.Fatalf("%s: exit status %d", fname, ret)
156+
resume, ok := vm.GetFunctionExport("resume")
157+
if !ok {
158+
panic("function not found: resume")
121159
}
122160

123-
if *vmStats {
124-
log.Printf("memory pages: %d", len(vm.Memory)/65536)
161+
if _, err := vm.RunWithGasLimit(run, *gas, 0, 0); err != nil {
162+
return 1, err
125163
}
126164

127-
os.Exit(int(ret))
165+
for !vm.Exited {
166+
if _, err := vm.RunWithGasLimit(resume, *gas); err != nil {
167+
return w.StatusCode, err
168+
}
169+
}
170+
171+
return w.StatusCode, nil
128172
}

cmd/cwa/test.sh

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
#!/bin/sh
22

3+
set -e
4+
set -x
5+
36
go run main.go -vm-stats -test -main-func cwa_main ./testdata/test.wasm foo bar
7+
go run main.go -vm-stats -test -go ./testdata/go.wasm foo bar

cwa/olin/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ path = "src/bin/olinfetch.rs"
1919
chrono = "0.4"
2020
http = "0.1"
2121
httparse = "1.3.2"
22-
wee_alloc = "0.4"
22+
wee_alloc = "0.4.3"

cwa/olin/src/bin/olinfetch.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ pub extern "C" fn cwa_main() -> i32 {
1818
1
1919
}).unwrap();
2020

21-
write!(out, "CPU:\t\t{}\n", "wasm32");
21+
write!(out, "CPU:\t\t{}\n", "wasm32").unwrap();
2222
write!(
2323
out,
2424
"Runtime:\t{} {}.{}\n",
2525
runtime_name,
2626
runtime::spec_major(),
2727
runtime::spec_minor()
28-
);
29-
write!(out, "Now:\t\t{}\n", time::now().to_rfc3339());
28+
).unwrap();
29+
write!(out, "Now:\t\t{}\n", time::now().to_rfc3339()).unwrap();
3030

3131
0
3232
}

cwa/tests/src/main.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ mod scheme;
1111
pub extern "C" fn cwa_main() -> i32 {
1212
olin::panic::set_hook();
1313

14+
let mut fail_count = 0;
15+
1416
let funcs = [
1517
ns::env::test,
1618
ns::random::test,
@@ -33,11 +35,14 @@ pub extern "C" fn cwa_main() -> i32 {
3335
for func in &funcs {
3436
match func() {
3537
Ok(()) => {}
36-
Err(e) => return e as i32,
38+
Err(e) => {
39+
olin::log::error(&format!("test error: {:?}", e));
40+
fail_count += 1;
41+
},
3742
}
3843
}
3944

40-
0
45+
fail_count
4146
}
4247

4348
fn main() {}

cwa/tests/src/olintest/http.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ pub extern "C" fn test() -> Result<(), i32> {
1111
let mut resp_body = Vec::<u8>::new();
1212
let mut req_body = Vec::<u8>::new();
1313
let req = http::Request::builder()
14-
.uri("https://printerfacts.herokuapp.com")
15-
.header("User-Agent", "my-awesome-agent/1.0")
14+
.uri("http://bsnk.minipaas.xeserv.us")
15+
.header("User-Agent", "Olin/dev")
1616
.body(&mut req_body)
1717
.map_err(|e| {
1818
log::error(&format!("request error: {:?}", e));

dagger/doc.go

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package dagger is a runtime library for programs run on dagger.
2+
package dagger

dagger/file_js.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// +build js,wasm go1.12
2+
3+
package dagger
4+
5+
import (
6+
"errors"
7+
"strconv"
8+
)
9+
10+
func OpenFile(furl string, flags int32) int32 {
11+
return openFD(furl, flags)
12+
}
13+
14+
func openFD(furl string, flags int32) int32
15+
16+
type file struct {
17+
fd int
18+
}
19+
20+
func read(fd int, buf []byte) int
21+
22+
func (f file) Read(buf []byte) (int, error) {
23+
n := read(f.fd, buf)
24+
if n < 0 {
25+
return n, errors.New("dagger: error code " + strconv.Itoa(n*-1))
26+
}
27+
28+
return n, nil
29+
}

dagger/file_js.s

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "textflag.h"
2+
3+
// +build js,wasm go1.11
4+
5+
TEXT ·openFD(SB), NOSPLIT, $0
6+
CallImport
7+
RET
8+
9+
TEXT ·read(SB), NOSPLIT, $0
10+
CallImport
11+
RET
12+

go.mod

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
module github.com/Xe/olin
22

3-
replace github.com/go-interpreter/wagon v0.0.0 => github.com/losfair/wagon v0.0.0-20180825141017-f8cb99b55a39
3+
replace github.com/go-interpreter/wagon v0.0.0 => github.com/perlin-network/wagon v0.3.1-0.20180825141017-f8cb99b55a39
44

55
require (
6-
github.com/Xe/ln v0.1.2
76
github.com/google/uuid v1.0.0 // indirect
8-
github.com/heroku/x v0.0.0-20180719221634-1de401e1ba0e
97
github.com/pborman/uuid v0.0.0-20180906182336-adf5a7427709
10-
github.com/perlin-network/life v0.0.0-20181005150058-1eb3d14cb6e7
8+
github.com/perlin-network/life v0.0.0-20190204091834-d05763d11050
9+
within.website/ln v0.5.2
1110
)

0 commit comments

Comments
 (0)