-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add `Resource::take` method to `wit_bindgen::rt` This allows the guest to take back ownership of an exported resource from the host; you can think of it as the reverse of `Resource::new`. It's a bit awkward to do this for the time being; WebAssembly/component-model#238 will improve the situation if accepted. Signed-off-by: Joel Dice <[email protected]> * address review feedback - add `type RawRep<T> = Option<T>` alias - rename `Resource::take` to `Resource::into_inner` - add `Resource::lift_borrow` and use it in code generator Signed-off-by: Joel Dice <[email protected]> --------- Signed-off-by: Joel Dice <[email protected]>
- Loading branch information
Showing
7 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
include!("../../../../tests/runtime/resource_into_inner/wasm.rs"); | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use wasmtime::Store; | ||
|
||
wasmtime::component::bindgen!(in "tests/runtime/resource_into_inner"); | ||
|
||
use exports::test::resource_into_inner::test::Test; | ||
|
||
#[test] | ||
fn run() -> anyhow::Result<()> { | ||
crate::run_test( | ||
"resource_into_inner", | ||
|_| Ok(()), | ||
|store, component, linker| { | ||
let (u, e) = ResourceIntoInner::instantiate(store, component, linker)?; | ||
Ok((u.interface0, e)) | ||
}, | ||
run_test, | ||
) | ||
} | ||
|
||
fn run_test(instance: Test, store: &mut Store<crate::Wasi<()>>) -> anyhow::Result<()> { | ||
instance.call_test(&mut *store) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
wit_bindgen::generate!({ | ||
path: "../../tests/runtime/resource_into_inner", | ||
exports: { | ||
world: Test, | ||
"test:resource-into-inner/test": Test, | ||
"test:resource-into-inner/test/thing": MyThing, | ||
}, | ||
}); | ||
|
||
use exports::test::resource_into_inner::test::{Guest, GuestThing}; | ||
use wit_bindgen::rt::Resource; | ||
|
||
pub struct Test; | ||
|
||
impl Guest for Test { | ||
fn test() { | ||
let text = "Jabberwocky"; | ||
assert_eq!( | ||
text, | ||
&Resource::into_inner(Resource::new(MyThing(text.to_string()))).0 | ||
); | ||
} | ||
} | ||
|
||
pub struct MyThing(String); | ||
|
||
impl GuestThing for MyThing { | ||
fn new(text: String) -> Self { | ||
Self(text) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package test:resource-into-inner; | ||
|
||
interface test { | ||
resource thing { | ||
constructor(text: string); | ||
} | ||
|
||
test: func(); | ||
} | ||
|
||
world resource-into-inner { | ||
export test; | ||
} |