Skip to content

Commit 2dae8a6

Browse files
authored
go: add --stackful-exports for async exports (#1784)
Add an option that makes async exports stackful. The generated code uses the [async-lift-stackful] name, does not export a [callback] function and returns nothing from the wrapper, since the result goes through task.return. This is for runtimes that can block a thread, such as TinyGo with cooperative threads. In the core ABI, lower a result to memory for stackful exports the same way as for the other export variants. This replaces a todo.
1 parent d276c62 commit 2dae8a6

2 files changed

Lines changed: 55 additions & 22 deletions

File tree

‎crates/core/src/abi.rs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,9 @@ impl<'a, B: Bindgen> Generator<'a, B> {
13541354
// (statically) and then write the result into that
13551355
// memory, returning the pointer at the end.
13561356
(_lower_to_memory @ true, _, variant) => match variant {
1357-
AbiVariant::GuestExport | AbiVariant::GuestExportAsync => {
1357+
AbiVariant::GuestExport
1358+
| AbiVariant::GuestExportAsync
1359+
| AbiVariant::GuestExportAsyncStackful => {
13581360
let ElementInfo { size, align } =
13591361
self.bindgen.sizes().params(&func.result);
13601362
let ptr = self.bindgen.return_pointer(size, align);
@@ -1372,9 +1374,6 @@ impl<'a, B: Bindgen> Generator<'a, B> {
13721374
func_name = func.name,
13731375
)
13741376
}
1375-
AbiVariant::GuestExportAsyncStackful => {
1376-
todo!("stackful exports are not yet supported")
1377-
}
13781377
},
13791378
}
13801379

‎crates/go/src/lib.rs‎

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ pub struct Opts {
149149
#[cfg_attr(feature = "clap", clap(long))]
150150
pub print_remote_pkg_version: bool,
151151

152+
/// If true, async exports are stackful (`[async-lift-stackful]`) instead of
153+
/// callback-based.
154+
///
155+
/// A stackful export runs on its own stack and may block on a waitable
156+
/// set. The generated code does not export a `[callback]` function. This
157+
/// needs a runtime that can block a thread, such as TinyGo with
158+
/// cooperative threads.
159+
#[cfg_attr(feature = "clap", clap(long))]
160+
pub stackful_exports: bool,
161+
152162
/// When generating Go package names, include the WIT package version even
153163
/// if only one version of that package is referenced by the specified
154164
/// world.
@@ -1313,7 +1323,14 @@ func {camel}({go_params}) {go_results} {{
13131323

13141324
let async_ = self.opts.async_.is_async(resolve, interface, func, false);
13151325

1316-
let (variant, prefix) = if async_ {
1326+
let stackful = async_ && self.opts.stackful_exports;
1327+
1328+
let (variant, prefix) = if stackful {
1329+
(
1330+
AbiVariant::GuestExportAsyncStackful,
1331+
"[async-lift-stackful]",
1332+
)
1333+
} else if async_ {
13171334
(AbiVariant::GuestExportAsync, "[async-lift]")
13181335
} else {
13191336
(AbiVariant::GuestExport, "")
@@ -1398,30 +1415,47 @@ func {camel}({go_params}) {go_results} {{
13981415
.collect::<Vec<_>>()
13991416
.join(", ");
14001417

1401-
(
1402-
if abi::guest_export_needs_post_return(resolve, func) {
1403-
format!(
1404-
"{PINNER} := &runtime.Pinner{{}}
1405-
defer {PINNER}.Unpin()"
1406-
)
1407-
} else {
1408-
String::new()
1409-
},
1418+
let pinner = if abi::guest_export_needs_post_return(resolve, func) {
14101419
format!(
1411-
"
1420+
"{PINNER} := &runtime.Pinner{{}}
1421+
defer {PINNER}.Unpin()"
1422+
)
1423+
} else {
1424+
String::new()
1425+
};
1426+
let task_return = format!(
1427+
"//go:wasmimport [export]{module} [task-return]{function}
1428+
func wasm_export_task_return_{name}({task_return_params})
1429+
"
1430+
);
1431+
if stackful {
1432+
(
1433+
pinner,
1434+
format!(
1435+
"
1436+
1437+
{task_return}"
1438+
),
1439+
"witAsync.Run(func() {",
1440+
"})",
1441+
)
1442+
} else {
1443+
(
1444+
pinner,
1445+
format!(
1446+
"
14121447
14131448
//go:wasmexport [callback]{prefix}{export_name}
14141449
func wasm_export_callback_{name}(event0 uint32, event1 uint32, event2 uint32) uint32 {{
14151450
return witAsync.Callback(event0, event1, event2)
14161451
}}
14171452
1418-
//go:wasmimport [export]{module} [task-return]{function}
1419-
func wasm_export_task_return_{name}({task_return_params})
1420-
"
1421-
),
1422-
"return int32(witAsync.Run(func() {",
1423-
"}))",
1424-
)
1453+
{task_return}"
1454+
),
1455+
"return int32(witAsync.Run(func() {",
1456+
"}))",
1457+
)
1458+
}
14251459
} else if abi::guest_export_needs_post_return(resolve, func) {
14261460
(
14271461
format!("{PINNER} := &{SYNC_EXPORT_PINNER}"),

0 commit comments

Comments
 (0)