Skip to content

Commit

Permalink
cm: initial support for Component Model async types
Browse files Browse the repository at this point in the history
  • Loading branch information
ydnar committed Dec 25, 2024
1 parent c34236d commit 397cb99
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Initial support for Component Model [async](https://github.com/WebAssembly/component-model/blob/main/design/mvp/Async.md) types `stream`, `future`, and `error-context`.

## [v0.1.0] — 2024-12-14

Initial version, extracted into module [`go.bytecodealliance.org/cm`](https://pkg.go.dev/go.bytecodealliance.org/cm).
Expand Down
3 changes: 3 additions & 0 deletions cm/empty.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file exists for testing this package without WebAssembly,
// allowing empty function bodies with a //go:wasmimport directive.
// See https://pkg.go.dev/cmd/compile for more information.
46 changes: 46 additions & 0 deletions cm/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cm

// ErrorContext represents the Component Model [error-context] type,
// an immutable, non-deterministic, host-defined value meant to aid in debugging.
//
// [error-context]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md#error-context-type
type ErrorContext struct {
_ HostLayout
errorContext
}

type errorContext uint32

// Error implements the [error] interface. It returns the debug message associated with err.
func (err errorContext) Error() string {
return err.DebugMessage()
}

// String implements [fmt.Stringer].
func (err errorContext) String() string {
return err.DebugMessage()
}

// DebugMessage represents the Canonical ABI [error-context.debug-message] function.
//
// [error-context.debug-message]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md#error-contextdebug-message
func (err errorContext) DebugMessage() string {
var s string
errorContextDebugMessage(err, &s)
return s
}

//go:wasmimport canon error-context.debug-message
//go:noescape
func errorContextDebugMessage(err errorContext, msg *string)

// Drop represents the Canonical ABI [error-context.drop] function.
//
// [error-context.drop]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md#error-contextdrop
func (err errorContext) Drop() {
errorContextDrop(err)
}

//go:wasmimport canon error-context.drop
//go:noescape
func errorContextDrop(err errorContext)
15 changes: 15 additions & 0 deletions cm/future.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cm

// Future represents the Component Model [future] type.
// A future is a special case of stream. In non-error cases,
// a future delivers exactly one value before being automatically closed.
//
// [future]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md#asynchronous-value-types
type Future[T any] struct {
_ HostLayout
future[T]
}

type future[T any] uint32

// TODO: implement methods on type future
15 changes: 15 additions & 0 deletions cm/stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cm

// Stream represents the Component Model [stream] type.
// A stream is a special case of stream. In non-error cases,
// a stream delivers exactly one value before being automatically closed.
//
// [stream]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md#asynchronous-value-types
type Stream[T any] struct {
_ HostLayout
stream[T]
}

type stream[T any] uint32

// TODO: implement methods on type stream

0 comments on commit 397cb99

Please sign in to comment.