Skip to content

Commit cac8e9e

Browse files
committed
update Go runtime tests to use run export
Signed-off-by: Joel Dice <[email protected]>
1 parent edfa198 commit cac8e9e

File tree

21 files changed

+157
-371
lines changed

21 files changed

+157
-371
lines changed

crates/go/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ impl Go {
285285
}
286286
}
287287

288+
#[expect(clippy::too_many_arguments)]
288289
fn future_or_stream(
289290
&mut self,
290291
resolve: &Resolve,

crates/test/src/go.rs

Lines changed: 16 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use crate::{Compile, LanguageMethods, Runner, Verify};
22
use anyhow::Result;
3-
use std::borrow::Cow;
43
use std::env;
54
use std::fs;
65
use std::path::{Path, PathBuf};
76
use std::process::Command;
8-
use wasm_encoder::{Encode as _, Section as _};
9-
use wit_component::StringEncoding;
107

118
pub struct Go;
129

@@ -52,54 +49,24 @@ impl LanguageMethods for Go {
5249
}
5350

5451
fn compile(&self, runner: &Runner<'_>, compile: &Compile<'_>) -> Result<()> {
55-
enum CliVersion {
56-
P2,
57-
P3,
58-
}
59-
6052
let output = compile.output.with_extension("core.wasm");
6153

62-
// As of this writing, Go-generated modules are not compatible with the
63-
// `wasi_snapshot_preview1.command` adapter (we get unbounded recursion
64-
// during initialization), we we must use the
65-
// `wasi_snapshot_preview1.reactor` one instead. For `runner.go`, that
66-
// means we must export `wasi:cli/run` explicitly and thus embed an
67-
// extra component type custom section to cover that. For non-async
68-
// tests, that will be `wasi:cli/[email protected]#run`, but for async ones it
69-
// will be `wasi:cli/[email protected]#run`.
70-
71-
let cli_version = if let Some(name @ "runner.go") =
72-
compile.component.path.file_name().unwrap().to_str()
73-
{
74-
let runner = fs::read_to_string(&compile.component.path)?;
75-
fs::write(compile.bindings_dir.join(name), runner.as_bytes())?;
76-
Some(
77-
if runner.contains("//go:wasmexport wasi:cli/[email protected]#run") {
78-
CliVersion::P2
79-
} else {
80-
CliVersion::P3
81-
},
82-
)
83-
} else {
84-
// Tests which involve importing and/or exporting more than one
85-
// interface may require more than one file since we can't define
86-
// more than one package in a single file in Go (AFAICT). Here we
87-
// search for files related to `compile.component.path` based on a
88-
// made-up naming convention. For example, if the filename is
89-
// `test.go`, then we'll also include `${prefix}+test.go` for any
90-
// value of `${prefix}`.
91-
for path in all_paths(&compile.component.path)? {
92-
let test = fs::read_to_string(&path)?;
93-
let package_name = package_name(&test);
94-
let package_dir = compile.bindings_dir.join(package_name);
95-
fs::create_dir_all(&package_dir)?;
96-
fs::write(
97-
&package_dir.join(path.file_name().unwrap()),
98-
test.as_bytes(),
99-
)?;
100-
}
101-
None
102-
};
54+
// Tests which involve importing and/or exporting more than one
55+
// interface may require more than one file since we can't define more
56+
// than one package in a single file in Go (AFAICT). Here we search for
57+
// files related to `compile.component.path` based on a made-up naming
58+
// convention. For example, if the filename is `test.go`, then we'll
59+
// also include `${prefix}+test.go` for any value of `${prefix}`.
60+
for path in all_paths(&compile.component.path)? {
61+
let test = fs::read_to_string(&path)?;
62+
let package_name = package_name(&test);
63+
let package_dir = compile.bindings_dir.join(package_name);
64+
fs::create_dir_all(&package_dir)?;
65+
fs::write(
66+
&package_dir.join(path.file_name().unwrap()),
67+
test.as_bytes(),
68+
)?;
69+
}
10370

10471
runner.run_command(
10572
Command::new("go")
@@ -113,57 +80,6 @@ impl LanguageMethods for Go {
11380
.arg("-ldflags=-checklinkname=0"),
11481
)?;
11582

116-
if let Some(version) = cli_version {
117-
let mut resolve = wit_parser::Resolve::default();
118-
let pkg = resolve.push_str(
119-
"run.wit",
120-
match version {
121-
CliVersion::P2 => {
122-
"package wasi:[email protected];
123-
124-
interface run {
125-
run: func() -> result;
126-
}
127-
128-
world command {
129-
export run;
130-
}"
131-
}
132-
CliVersion::P3 => {
133-
"package wasi:[email protected];
134-
135-
interface run {
136-
run: async func() -> result;
137-
}
138-
139-
world command {
140-
export run;
141-
}"
142-
}
143-
},
144-
)?;
145-
let cli = resolve.select_world(&[pkg], Some(&"command"))?;
146-
147-
let (pkg, _) = resolve.push_path(&compile.component.bindgen.wit_path)?;
148-
let test = resolve.select_world(&[pkg], Some(&compile.component.bindgen.world))?;
149-
150-
let mut module = fs::read(&output)?;
151-
for (world, name) in [
152-
(cli, "component-type-wasi-cli"),
153-
(test, "component-type-test"),
154-
] {
155-
let encoded =
156-
wit_component::metadata::encode(&resolve, world, StringEncoding::UTF8, None)?;
157-
let section = wasm_encoder::CustomSection {
158-
name: Cow::Borrowed(name),
159-
data: Cow::Borrowed(&encoded),
160-
};
161-
module.push(section.id());
162-
section.encode(&mut module);
163-
}
164-
fs::write(&output, &module)?;
165-
}
166-
16783
runner.convert_p1_to_component(&output, compile)?;
16884

16985
Ok(())

tests/runtime-async/async/ping-pong/runner.go

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,51 @@
1-
package main
1+
package export_wit_world
22

33
import (
44
"fmt"
55
test "wit_component/my_test_i"
6-
"wit_component/wit_async"
76
"wit_component/wit_types"
87
)
98

109
type Unit struct{}
1110

12-
//go:wasmimport [export]wasi:cli/[email protected] [task-return]run
13-
func taskReturn(result int32)
11+
func Run() {
12+
{
13+
f1 := make(chan *wit_types.FutureReader[string])
14+
f2 := make(chan Unit)
1415

15-
//go:wasmexport [async-lift]wasi:cli/[email protected]#run
16-
func run() uint32 {
17-
return wit_async.Run(func() {
18-
{
19-
f1 := make(chan *wit_types.FutureReader[string])
20-
f2 := make(chan Unit)
16+
tx, rx := test.MakeFutureString()
17+
go func() {
18+
f1 <- test.Ping(rx, "world")
19+
}()
2120

22-
tx, rx := test.MakeFutureString()
23-
go func() {
24-
f1 <- test.Ping(rx, "world")
25-
}()
21+
go func() {
22+
tx.Write("hello")
23+
f2 <- Unit{}
24+
}()
2625

27-
go func() {
28-
tx.Write("hello")
29-
f2 <- Unit{}
30-
}()
31-
32-
(<-f2)
33-
rx2 := (<-f1)
34-
assertEqual(rx2.Read(), "helloworld")
35-
}
36-
37-
{
38-
f1 := make(chan Unit)
39-
f2 := make(chan Unit)
40-
41-
tx, rx := test.MakeFutureString()
42-
go func() {
43-
assertEqual(test.Pong(rx), "helloworld")
44-
f1 <- Unit{}
45-
}()
26+
(<-f2)
27+
rx2 := (<-f1)
28+
assertEqual(rx2.Read(), "helloworld")
29+
}
4630

47-
go func() {
48-
tx.Write("helloworld")
49-
f2 <- Unit{}
50-
}()
31+
{
32+
f1 := make(chan Unit)
33+
f2 := make(chan Unit)
5134

52-
(<-f2)
53-
(<-f1)
54-
}
35+
tx, rx := test.MakeFutureString()
36+
go func() {
37+
assertEqual(test.Pong(rx), "helloworld")
38+
f1 <- Unit{}
39+
}()
5540

56-
taskReturn(0)
57-
})
58-
}
41+
go func() {
42+
tx.Write("helloworld")
43+
f2 <- Unit{}
44+
}()
5945

60-
//go:wasmexport [callback][async-lift]wasi:cli/[email protected]#run
61-
func callback(event0 uint32, event1 uint32, event2 uint32) uint32 {
62-
return wit_async.Callback(event0, event1, event2)
46+
(<-f2)
47+
(<-f1)
48+
}
6349
}
6450

6551
func assertEqual[T comparable](a, b T) {
Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
1-
package main
1+
package export_wit_world
22

3-
import (
4-
test "wit_component/a_b_i"
5-
"wit_component/wit_async"
6-
)
3+
import test "wit_component/a_b_i"
74

8-
//go:wasmimport [export]wasi:cli/[email protected] [task-return]run
9-
func taskReturn(result int32)
10-
11-
//go:wasmexport [async-lift]wasi:cli/[email protected]#run
12-
func run() uint32 {
13-
return wit_async.Run(func() {
14-
test.F()
15-
taskReturn(0)
16-
})
17-
}
18-
19-
//go:wasmexport [callback][async-lift]wasi:cli/[email protected]#run
20-
func callback(event0 uint32, event1 uint32, event2 uint32) uint32 {
21-
return wit_async.Callback(event0, event1, event2)
5+
func Run() {
6+
test.F()
227
}

tests/runtime-async/async/simple-future/runner.go

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,39 @@
1-
package main
1+
package export_wit_world
22

33
import (
44
test "wit_component/my_test_i"
5-
"wit_component/wit_async"
65
"wit_component/wit_types"
76
)
87

9-
//go:wasmimport [export]wasi:cli/[email protected] [task-return]run
10-
func taskReturn(result int32)
11-
12-
//go:wasmexport [async-lift]wasi:cli/[email protected]#run
13-
func run() uint32 {
14-
return wit_async.Run(func() {
15-
write := make(chan bool)
16-
read := make(chan wit_types.Unit)
17-
18-
{
19-
tx, rx := test.MakeFutureUnit()
20-
go func() {
21-
write <- tx.Write(wit_types.Unit{})
22-
}()
23-
go func() {
24-
test.ReadFuture(rx)
25-
read <- wit_types.Unit{}
26-
}()
27-
(<-read)
28-
assert(<-write)
29-
}
30-
31-
{
32-
tx, rx := test.MakeFutureUnit()
33-
go func() {
34-
write <- tx.Write(wit_types.Unit{})
35-
}()
36-
go func() {
37-
test.DropFuture(rx)
38-
read <- wit_types.Unit{}
39-
}()
40-
(<-read)
41-
assert(!(<-write))
42-
}
43-
44-
taskReturn(0)
45-
})
46-
}
8+
func Run() {
9+
write := make(chan bool)
10+
read := make(chan wit_types.Unit)
11+
12+
{
13+
tx, rx := test.MakeFutureUnit()
14+
go func() {
15+
write <- tx.Write(wit_types.Unit{})
16+
}()
17+
go func() {
18+
test.ReadFuture(rx)
19+
read <- wit_types.Unit{}
20+
}()
21+
(<-read)
22+
assert(<-write)
23+
}
4724

48-
//go:wasmexport [callback][async-lift]wasi:cli/[email protected]#run
49-
func callback(event0 uint32, event1 uint32, event2 uint32) uint32 {
50-
return wit_async.Callback(event0, event1, event2)
25+
{
26+
tx, rx := test.MakeFutureUnit()
27+
go func() {
28+
write <- tx.Write(wit_types.Unit{})
29+
}()
30+
go func() {
31+
test.DropFuture(rx)
32+
read <- wit_types.Unit{}
33+
}()
34+
(<-read)
35+
assert(!(<-write))
36+
}
5137
}
5238

5339
func assert(v bool) {

tests/runtime-async/async/simple-import-params-results/runner.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
1-
package main
1+
package export_wit_world
22

33
import (
44
"fmt"
55
test "wit_component/a_b_i"
6-
"wit_component/wit_async"
76
)
87

9-
//go:wasmimport [export]wasi:cli/[email protected] [task-return]run
10-
func taskReturn(result int32)
11-
12-
//go:wasmexport [async-lift]wasi:cli/[email protected]#run
13-
func run() uint32 {
14-
return wit_async.Run(func() {
15-
test.OneArgument(1)
16-
assertEqual(test.OneResult(), 2)
17-
assertEqual(test.OneArgumentAndResult(3), 4)
18-
test.TwoArguments(5, 6)
19-
assertEqual(test.TwoArgumentsAndResult(7, 8), 9)
20-
taskReturn(0)
21-
})
22-
}
23-
24-
//go:wasmexport [callback][async-lift]wasi:cli/[email protected]#run
25-
func callback(event0 uint32, event1 uint32, event2 uint32) uint32 {
26-
return wit_async.Callback(event0, event1, event2)
8+
func Run() {
9+
test.OneArgument(1)
10+
assertEqual(test.OneResult(), 2)
11+
assertEqual(test.OneArgumentAndResult(3), 4)
12+
test.TwoArguments(5, 6)
13+
assertEqual(test.TwoArgumentsAndResult(7, 8), 9)
2714
}
2815

2916
func assertEqual[T comparable](a T, b T) {

0 commit comments

Comments
 (0)