Skip to content

Commit 4536acf

Browse files
authored
Migrate resource_import_and_export to wit-bindgen test (#1257)
* Migrate `resource_import_and_export` to `wit-bindgen test` Made easier through the refactoring made by the `xcrate` test with a custom composition script and support for multiple components. That enables the tests here to mostly be a simple copy from the original test. * Remove test directive
1 parent 040a771 commit 4536acf

File tree

15 files changed

+178
-179
lines changed

15 files changed

+178
-179
lines changed

crates/test-rust-wasm/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ test = false
3131
name = "resource_floats"
3232
test = false
3333

34-
[[bin]]
35-
name = "resource_import_and_export"
36-
test = false
37-
3834
[[bin]]
3935
name = "resource_with_lists"
4036
test = false

crates/test-rust-wasm/src/bin/resource_import_and_export.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

crates/test/src/c.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ fn compile(runner: &Runner<'_>, compile: &Compile<'_>, compiler: PathBuf) -> Res
143143
cmd.arg(
144144
compile
145145
.bindings_dir
146-
.join(format!("{}.c", compile.component.kind)),
146+
.join(format!("{}.c", compile.component.bindgen.world)),
147147
)
148148
.arg("-I")
149149
.arg(&compile.bindings_dir)
@@ -161,11 +161,10 @@ fn compile(runner: &Runner<'_>, compile: &Compile<'_>, compiler: PathBuf) -> Res
161161
let mut cmd = Command::new(compiler);
162162
cmd.arg(&compile.component.path)
163163
.arg(&bindings_object)
164-
.arg(
165-
compile
166-
.bindings_dir
167-
.join(format!("{}_component_type.o", compile.component.kind)),
168-
)
164+
.arg(compile.bindings_dir.join(format!(
165+
"{}_component_type.o",
166+
compile.component.bindgen.world
167+
)))
169168
.arg("-I")
170169
.arg(&compile.bindings_dir)
171170
.arg("-Wall")

crates/test/src/rust.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,10 @@ path = 'lib.rs'
201201
let mut cmd = rustc(&compile.component.path, &output);
202202
cmd.env(
203203
"BINDINGS",
204-
compile
205-
.bindings_dir
206-
.join(format!("{}.rs", compile.component.bindgen.world)),
204+
compile.bindings_dir.join(format!(
205+
"{}.rs",
206+
compile.component.bindgen.world.replace('-', "_")
207+
)),
207208
);
208209
for (name, path) in externs {
209210
let arg = format!("--extern={name}={}", path.display());
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package example:composition;
2+
3+
let leaf-thing = new test:leaf-thing { ... };
4+
let leaf-toplevel = new test:leaf-toplevel {
5+
test: leaf-thing.test,
6+
thing: leaf-thing.test.thing,
7+
...
8+
};
9+
let intermediate = new test:intermediate {
10+
test: leaf-thing.test,
11+
toplevel-import: leaf-toplevel.toplevel-export,
12+
thing: leaf-thing.test.thing,
13+
...
14+
};
15+
let runner = new test:runner { test: intermediate.test, ... };
16+
17+
export runner...;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//@ args = '--rename test:resource-import-and-export/test=test'
2+
3+
#include <assert.h>
4+
#include "intermediate.h"
5+
#include <stdlib.h>
6+
#include <string.h>
7+
8+
struct exports_test_thing_t {
9+
test_own_thing_t thing;
10+
};
11+
12+
intermediate_own_thing_t
13+
exports_intermediate_toplevel_export(intermediate_own_thing_t a) {
14+
return intermediate_toplevel_import(a);
15+
}
16+
17+
exports_test_own_thing_t
18+
exports_test_constructor_thing(uint32_t v) {
19+
exports_test_thing_t *val =
20+
(exports_test_thing_t *)
21+
malloc(sizeof(exports_test_thing_t));
22+
assert(val != NULL);
23+
val->thing = test_constructor_thing(v + 1);
24+
return exports_test_thing_new(val);
25+
}
26+
27+
uint32_t
28+
exports_test_method_thing_foo(exports_test_borrow_thing_t self) {
29+
test_borrow_thing_t borrow =
30+
test_borrow_thing(self->thing);
31+
return test_method_thing_foo(borrow) + 2;
32+
}
33+
34+
void
35+
exports_test_method_thing_bar(exports_test_borrow_thing_t self, uint32_t v) {
36+
test_borrow_thing_t borrow =
37+
test_borrow_thing(self->thing);
38+
test_method_thing_bar(borrow, v + 3);
39+
}
40+
41+
exports_test_own_thing_t
42+
exports_test_static_thing_baz(exports_test_own_thing_t a, exports_test_own_thing_t b) {
43+
exports_test_thing_t *a_rep =
44+
exports_test_thing_rep(a);
45+
exports_test_thing_t *b_rep =
46+
exports_test_thing_rep(b);
47+
48+
test_own_thing_t tmp =
49+
test_static_thing_baz(a_rep->thing, b_rep->thing);
50+
test_borrow_thing_t tmp_borrow =
51+
test_borrow_thing(tmp);
52+
uint32_t ret = test_method_thing_foo(tmp_borrow) + 4;
53+
test_thing_drop_own(tmp);
54+
55+
return exports_test_constructor_thing(ret);
56+
}
57+
58+
void
59+
exports_test_thing_destructor(exports_test_thing_t *rep) {
60+
free(rep);
61+
}

tests/runtime/resource_import_and_export/wasm.cs renamed to tests/runtime-new/resource-import-and-export/intermediate.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using Import = ResourceImportAndExportWorld.wit.imports.test.resourceImportAndExport.ITest;
2-
using Host = ResourceImportAndExportWorld.wit.imports.test.resourceImportAndExport.TestInterop;
1+
using Import = IntermediateWorld.wit.imports.test.resourceImportAndExport.ITest;
2+
using Host = IntermediateWorld.wit.imports.test.resourceImportAndExport.TestInterop;
33

4-
namespace ResourceImportAndExportWorld.wit.exports.test.resourceImportAndExport
4+
namespace IntermediateWorld.wit.exports.test.resourceImportAndExport
55
{
66
public class TestImpl : ITest {
77
public class Thing : ITest.Thing, ITest.IThing {
@@ -26,10 +26,10 @@ public static ITest.Thing Baz(ITest.Thing a, ITest.Thing b) {
2626
}
2727
}
2828

29-
namespace ResourceImportAndExportWorld {
30-
public class ResourceImportAndExportWorldImpl : IResourceImportAndExportWorld {
29+
namespace IntermediateWorld {
30+
public class IntermediateWorldImpl : IIntermediateWorld {
3131
public static Import.Thing ToplevelExport(Import.Thing things) {
32-
return exports.ResourceImportAndExportWorld.ToplevelImport(things);
32+
return exports.IntermediateWorld.ToplevelImport(things);
3333
}
3434
}
3535
}

tests/runtime/resource_import_and_export/wasm.rs renamed to tests/runtime-new/resource-import-and-export/intermediate.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
use std::cell::RefCell;
2-
3-
wit_bindgen::generate!({
4-
path: "../../tests/runtime/resource_import_and_export",
5-
});
1+
include!(env!("BINDINGS"));
62

73
use exports::test::resource_import_and_export::test::{GuestThing, Thing as ExportThing};
4+
use std::cell::RefCell;
85

96
pub struct Test {}
107

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
include!(env!("BINDINGS"));
2+
3+
use crate::exports::test::resource_import_and_export::test::{Guest, GuestThing, Thing};
4+
use std::cell::Cell;
5+
6+
struct Component;
7+
8+
export!(Component);
9+
10+
struct MyThing(Cell<u32>);
11+
12+
impl Guest for Component {
13+
type Thing = MyThing;
14+
}
15+
16+
impl GuestThing for MyThing {
17+
fn new(v: u32) -> MyThing {
18+
MyThing(Cell::new(v + 1))
19+
}
20+
21+
fn foo(&self) -> u32 {
22+
self.0.get() + 2
23+
}
24+
25+
fn bar(&self, v: u32) {
26+
self.0.set(v + 3);
27+
}
28+
29+
fn baz(a: Thing, b: Thing) -> Thing {
30+
let a = a.get::<MyThing>();
31+
let b = b.get::<MyThing>();
32+
Thing::new(MyThing::new(a.foo() + b.foo() + 4))
33+
}
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
include!(env!("BINDINGS"));
2+
3+
export!(Component);
4+
5+
struct Component;
6+
7+
impl Guest for Component {
8+
fn toplevel_export(a: Thing) -> Thing {
9+
a
10+
}
11+
}

0 commit comments

Comments
 (0)