-
Notifications
You must be signed in to change notification settings - Fork 97
Add Component Model composite ValType wrappers: list / record / tuple / enum / flags #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
glassmonkey
wants to merge
6
commits into
bytecodealliance:main
Choose a base branch
from
glassmonkey:component-composite-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+521
−21
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5c1b4fc
Add Component Model composite ValType wrappers: list/record/tuple/enu…
glassmonkey 0984a21
Rename composite-type accessors to singular form for consistency
glassmonkey 0ddf7c6
Rename composite-type tests to match existing variation naming
glassmonkey f85bfa4
Consolidate composite-type tests to match pre-existing wasmtime-go style
glassmonkey 12cd8cd
Tighten composite ValType doc comments and dedupe downcast nil probes
glassmonkey 347f574
Align composite ValType doc comments and tests with pre-existing wasm…
glassmonkey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -5,17 +5,27 @@ package wasmtime | |
| // static inline uint8_t go_component_valtype_kind(const wasmtime_component_valtype_t *vt) { | ||
| // return vt->kind; | ||
| // } | ||
| // static inline wasmtime_component_list_type_t *go_component_valtype_list(const wasmtime_component_valtype_t *vt) { | ||
| // return vt->of.list; | ||
| // } | ||
| // static inline wasmtime_component_record_type_t *go_component_valtype_record(const wasmtime_component_valtype_t *vt) { | ||
| // return vt->of.record; | ||
| // } | ||
| // static inline wasmtime_component_tuple_type_t *go_component_valtype_tuple(const wasmtime_component_valtype_t *vt) { | ||
| // return vt->of.tuple; | ||
| // } | ||
| // static inline wasmtime_component_enum_type_t *go_component_valtype_enum(const wasmtime_component_valtype_t *vt) { | ||
| // return vt->of.enum_; | ||
| // } | ||
| // static inline wasmtime_component_flags_type_t *go_component_valtype_flags(const wasmtime_component_valtype_t *vt) { | ||
| // return vt->of.flags; | ||
| // } | ||
| import "C" | ||
|
|
||
| import "runtime" | ||
|
|
||
| // ComponentValTypeKind discriminates the WIT type that a [ComponentValType] | ||
| // represents. Only constants for the 13 primitive kinds (bool through | ||
| // string) are exposed in this release; constants for the composite kinds | ||
| // (list / record / tuple / variant / enum / option / result / flags / own / | ||
| // borrow / future / stream / error-context / map) are intentionally | ||
| // commented out below — they will be uncommented when each composite kind | ||
| // gets a dedicated payload accessor and a test path that exercises it. | ||
| // represents. | ||
| type ComponentValTypeKind uint8 | ||
|
|
||
| const ( | ||
|
|
@@ -32,19 +42,17 @@ const ( | |
| ComponentValTypeKindF64 ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_F64 | ||
| ComponentValTypeKindChar ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_CHAR | ||
| ComponentValTypeKindString ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_STRING | ||
| ComponentValTypeKindList ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_LIST | ||
| ComponentValTypeKindRecord ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_RECORD | ||
| ComponentValTypeKindTuple ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_TUPLE | ||
| ComponentValTypeKindEnum ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_ENUM | ||
| ComponentValTypeKindFlags ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_FLAGS | ||
|
|
||
| // Composite-kind constants are deferred until each gets a payload | ||
| // accessor that returns the corresponding sub-type wrapper, with a | ||
| // test path. Uncomment as each one lands. | ||
| // Remaining composite kinds: each lands with its payload accessor. | ||
| // | ||
| // ComponentValTypeKindList ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_LIST | ||
| // ComponentValTypeKindRecord ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_RECORD | ||
| // ComponentValTypeKindTuple ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_TUPLE | ||
| // ComponentValTypeKindVariant ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_VARIANT | ||
| // ComponentValTypeKindEnum ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_ENUM | ||
| // ComponentValTypeKindOption ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_OPTION | ||
| // ComponentValTypeKindResult ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_RESULT | ||
| // ComponentValTypeKindFlags ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_FLAGS | ||
| // ComponentValTypeKindOwn ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_OWN | ||
| // ComponentValTypeKindBorrow ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_BORROW | ||
| // ComponentValTypeKindFuture ComponentValTypeKind = C.WASMTIME_COMPONENT_VALTYPE_FUTURE | ||
|
|
@@ -54,13 +62,9 @@ const ( | |
| ) | ||
|
|
||
| // ComponentValType describes the WIT type of a value in the component | ||
| // model. | ||
| // | ||
| // In this release [ComponentValType.Kind] returns one of the 13 primitive | ||
| // [ComponentValTypeKind] constants. If the underlying value type is a | ||
| // composite kind, the returned uint8 will not match any exposed constant | ||
| // — those, along with payload accessors that return their sub-type | ||
| // wrappers, arrive in follow-up work. | ||
| // model. For composite kinds, the matching accessor | ||
| // ([ComponentValType.List], [ComponentValType.Record], etc.) returns an | ||
| // independently-owned wrapper, or nil if the kind does not match. | ||
| type ComponentValType struct { | ||
| val C.wasmtime_component_valtype_t | ||
| closed bool | ||
|
|
@@ -83,6 +87,56 @@ func (vt *ComponentValType) Kind() ComponentValTypeKind { | |
| return ComponentValTypeKind(C.go_component_valtype_kind(&vt.val)) | ||
| } | ||
|
|
||
| // List returns the underlying [ComponentListType] if this is a list type. Otherwise returns nil. | ||
| func (vt *ComponentValType) List() *ComponentListType { | ||
| if vt.Kind() != ComponentValTypeKindList { | ||
| return nil | ||
| } | ||
| cloned := C.wasmtime_component_list_type_clone(C.go_component_valtype_list(&vt.val)) | ||
| runtime.KeepAlive(vt) | ||
| return mkComponentListType(cloned) | ||
| } | ||
|
|
||
| // Record returns the underlying [ComponentRecordType] if this is a record type. Otherwise returns nil. | ||
| func (vt *ComponentValType) Record() *ComponentRecordType { | ||
| if vt.Kind() != ComponentValTypeKindRecord { | ||
| return nil | ||
| } | ||
| cloned := C.wasmtime_component_record_type_clone(C.go_component_valtype_record(&vt.val)) | ||
| runtime.KeepAlive(vt) | ||
| return mkComponentRecordType(cloned) | ||
| } | ||
|
|
||
| // Tuple returns the underlying [ComponentTupleType] if this is a tuple type. Otherwise returns nil. | ||
| func (vt *ComponentValType) Tuple() *ComponentTupleType { | ||
| if vt.Kind() != ComponentValTypeKindTuple { | ||
| return nil | ||
| } | ||
| cloned := C.wasmtime_component_tuple_type_clone(C.go_component_valtype_tuple(&vt.val)) | ||
| runtime.KeepAlive(vt) | ||
| return mkComponentTupleType(cloned) | ||
| } | ||
|
|
||
| // Enum returns the underlying [ComponentEnumType] if this is an enum type. Otherwise returns nil. | ||
| func (vt *ComponentValType) Enum() *ComponentEnumType { | ||
| if vt.Kind() != ComponentValTypeKindEnum { | ||
| return nil | ||
| } | ||
| cloned := C.wasmtime_component_enum_type_clone(C.go_component_valtype_enum(&vt.val)) | ||
| runtime.KeepAlive(vt) | ||
| return mkComponentEnumType(cloned) | ||
| } | ||
|
|
||
| // Flags returns the underlying [ComponentFlagsType] if this is a flags type. Otherwise returns nil. | ||
| func (vt *ComponentValType) Flags() *ComponentFlagsType { | ||
| if vt.Kind() != ComponentValTypeKindFlags { | ||
| return nil | ||
| } | ||
| cloned := C.wasmtime_component_flags_type_clone(C.go_component_valtype_flags(&vt.val)) | ||
| runtime.KeepAlive(vt) | ||
| return mkComponentFlagsType(cloned) | ||
| } | ||
|
|
||
| // Close deallocates this value type explicitly. | ||
| func (vt *ComponentValType) Close() { | ||
| if vt.closed { | ||
|
|
@@ -92,3 +146,243 @@ func (vt *ComponentValType) Close() { | |
| C.wasmtime_component_valtype_delete(&vt.val) | ||
| vt.closed = true | ||
| } | ||
|
|
||
| // ComponentListType is the payload of a `list<T>` value type. | ||
| type ComponentListType struct { | ||
| _ptr *C.wasmtime_component_list_type_t | ||
| } | ||
|
|
||
| func mkComponentListType(p *C.wasmtime_component_list_type_t) *ComponentListType { | ||
| lt := &ComponentListType{_ptr: p} | ||
| runtime.SetFinalizer(lt, func(lt *ComponentListType) { | ||
| lt.Close() | ||
| }) | ||
| return lt | ||
| } | ||
|
|
||
| func (lt *ComponentListType) ptr() *C.wasmtime_component_list_type_t { | ||
| if lt._ptr == nil { | ||
| panic("object has been closed already") | ||
| } | ||
| maybeGC() | ||
| return lt._ptr | ||
| } | ||
|
|
||
| // Element returns the element type of this list. | ||
| func (lt *ComponentListType) Element() *ComponentValType { | ||
| var out C.wasmtime_component_valtype_t | ||
| C.wasmtime_component_list_type_element(lt.ptr(), &out) | ||
| runtime.KeepAlive(lt) | ||
| return mkComponentValType(out) | ||
| } | ||
|
|
||
| // Close deallocates this list type explicitly. | ||
| func (lt *ComponentListType) Close() { | ||
| if lt._ptr == nil { | ||
| return | ||
| } | ||
| runtime.SetFinalizer(lt, nil) | ||
| C.wasmtime_component_list_type_delete(lt._ptr) | ||
| lt._ptr = nil | ||
| } | ||
|
|
||
| // ComponentRecordType is the payload of a `record { ... }` value type. | ||
| type ComponentRecordType struct { | ||
| _ptr *C.wasmtime_component_record_type_t | ||
| } | ||
|
|
||
| func mkComponentRecordType(p *C.wasmtime_component_record_type_t) *ComponentRecordType { | ||
| rt := &ComponentRecordType{_ptr: p} | ||
| runtime.SetFinalizer(rt, func(rt *ComponentRecordType) { | ||
| rt.Close() | ||
| }) | ||
| return rt | ||
| } | ||
|
|
||
| func (rt *ComponentRecordType) ptr() *C.wasmtime_component_record_type_t { | ||
| if rt._ptr == nil { | ||
| panic("object has been closed already") | ||
| } | ||
| maybeGC() | ||
| return rt._ptr | ||
| } | ||
|
|
||
| // FieldCount returns the number of fields in this record. | ||
| func (rt *ComponentRecordType) FieldCount() int { | ||
| n := C.wasmtime_component_record_type_field_count(rt.ptr()) | ||
| runtime.KeepAlive(rt) | ||
| return int(n) | ||
| } | ||
|
|
||
| // FieldNth returns the name and type of the `i`-th field, or `("", nil)` | ||
| // if `i` is out of range. | ||
| func (rt *ComponentRecordType) FieldNth(i int) (string, *ComponentValType) { | ||
| var nameP *C.char | ||
| var nameLen C.size_t | ||
| var out C.wasmtime_component_valtype_t | ||
| found := C.wasmtime_component_record_type_field_nth(rt.ptr(), C.size_t(i), &nameP, &nameLen, &out) | ||
| runtime.KeepAlive(rt) | ||
| if !bool(found) { | ||
| return "", nil | ||
| } | ||
| return C.GoStringN(nameP, C.int(nameLen)), mkComponentValType(out) | ||
| } | ||
|
|
||
| // Close deallocates this record type explicitly. | ||
| func (rt *ComponentRecordType) Close() { | ||
| if rt._ptr == nil { | ||
| return | ||
| } | ||
| runtime.SetFinalizer(rt, nil) | ||
| C.wasmtime_component_record_type_delete(rt._ptr) | ||
| rt._ptr = nil | ||
| } | ||
|
|
||
| // ComponentTupleType is the payload of a `tuple<T1, T2, ...>` value type. | ||
| type ComponentTupleType struct { | ||
| _ptr *C.wasmtime_component_tuple_type_t | ||
| } | ||
|
|
||
| func mkComponentTupleType(p *C.wasmtime_component_tuple_type_t) *ComponentTupleType { | ||
| tt := &ComponentTupleType{_ptr: p} | ||
| runtime.SetFinalizer(tt, func(tt *ComponentTupleType) { | ||
| tt.Close() | ||
| }) | ||
| return tt | ||
| } | ||
|
|
||
| func (tt *ComponentTupleType) ptr() *C.wasmtime_component_tuple_type_t { | ||
| if tt._ptr == nil { | ||
| panic("object has been closed already") | ||
| } | ||
| maybeGC() | ||
| return tt._ptr | ||
| } | ||
|
|
||
| // TypeCount returns the number of types in this tuple. | ||
| func (tt *ComponentTupleType) TypeCount() int { | ||
| n := C.wasmtime_component_tuple_type_types_count(tt.ptr()) | ||
| runtime.KeepAlive(tt) | ||
| return int(n) | ||
| } | ||
|
|
||
| // TypeNth returns the type at position `i`, or nil if `i` is out of range. | ||
| func (tt *ComponentTupleType) TypeNth(i int) *ComponentValType { | ||
| var out C.wasmtime_component_valtype_t | ||
| found := C.wasmtime_component_tuple_type_types_nth(tt.ptr(), C.size_t(i), &out) | ||
| runtime.KeepAlive(tt) | ||
| if !bool(found) { | ||
| return nil | ||
| } | ||
| return mkComponentValType(out) | ||
| } | ||
|
|
||
| // Close deallocates this tuple type explicitly. | ||
| func (tt *ComponentTupleType) Close() { | ||
| if tt._ptr == nil { | ||
| return | ||
| } | ||
| runtime.SetFinalizer(tt, nil) | ||
| C.wasmtime_component_tuple_type_delete(tt._ptr) | ||
| tt._ptr = nil | ||
| } | ||
|
|
||
| // ComponentEnumType is the payload of an `enum "a" "b" ...` value type. | ||
| type ComponentEnumType struct { | ||
| _ptr *C.wasmtime_component_enum_type_t | ||
| } | ||
|
|
||
| func mkComponentEnumType(p *C.wasmtime_component_enum_type_t) *ComponentEnumType { | ||
| et := &ComponentEnumType{_ptr: p} | ||
| runtime.SetFinalizer(et, func(et *ComponentEnumType) { | ||
| et.Close() | ||
| }) | ||
| return et | ||
| } | ||
|
|
||
| func (et *ComponentEnumType) ptr() *C.wasmtime_component_enum_type_t { | ||
| if et._ptr == nil { | ||
| panic("object has been closed already") | ||
| } | ||
| maybeGC() | ||
| return et._ptr | ||
| } | ||
|
|
||
| // CaseCount returns the number of cases in this enum. | ||
| func (et *ComponentEnumType) CaseCount() int { | ||
| n := C.wasmtime_component_enum_type_names_count(et.ptr()) | ||
| runtime.KeepAlive(et) | ||
| return int(n) | ||
| } | ||
|
|
||
| // CaseNth returns the name of the `i`-th case, or "" if `i` is out of range. | ||
| func (et *ComponentEnumType) CaseNth(i int) string { | ||
| var nameP *C.char | ||
| var nameLen C.size_t | ||
| found := C.wasmtime_component_enum_type_names_nth(et.ptr(), C.size_t(i), &nameP, &nameLen) | ||
| runtime.KeepAlive(et) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to above, this needs to be below the |
||
| if !bool(found) { | ||
| return "" | ||
| } | ||
| return C.GoStringN(nameP, C.int(nameLen)) | ||
| } | ||
|
|
||
| // Close deallocates this enum type explicitly. | ||
| func (et *ComponentEnumType) Close() { | ||
| if et._ptr == nil { | ||
| return | ||
| } | ||
| runtime.SetFinalizer(et, nil) | ||
| C.wasmtime_component_enum_type_delete(et._ptr) | ||
| et._ptr = nil | ||
| } | ||
|
|
||
| // ComponentFlagsType is the payload of a `flags "a" "b" ...` value type. | ||
| type ComponentFlagsType struct { | ||
| _ptr *C.wasmtime_component_flags_type_t | ||
| } | ||
|
|
||
| func mkComponentFlagsType(p *C.wasmtime_component_flags_type_t) *ComponentFlagsType { | ||
| ft := &ComponentFlagsType{_ptr: p} | ||
| runtime.SetFinalizer(ft, func(ft *ComponentFlagsType) { | ||
| ft.Close() | ||
| }) | ||
| return ft | ||
| } | ||
|
|
||
| func (ft *ComponentFlagsType) ptr() *C.wasmtime_component_flags_type_t { | ||
| if ft._ptr == nil { | ||
| panic("object has been closed already") | ||
| } | ||
| maybeGC() | ||
| return ft._ptr | ||
| } | ||
|
|
||
| // FlagCount returns the number of flags in this flags type. | ||
| func (ft *ComponentFlagsType) FlagCount() int { | ||
| n := C.wasmtime_component_flags_type_names_count(ft.ptr()) | ||
| runtime.KeepAlive(ft) | ||
| return int(n) | ||
| } | ||
|
|
||
| // FlagNth returns the name of the `i`-th flag, or "" if `i` is out of range. | ||
| func (ft *ComponentFlagsType) FlagNth(i int) string { | ||
| var nameP *C.char | ||
| var nameLen C.size_t | ||
| found := C.wasmtime_component_flags_type_names_nth(ft.ptr(), C.size_t(i), &nameP, &nameLen) | ||
| runtime.KeepAlive(ft) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto to above |
||
| if !bool(found) { | ||
| return "" | ||
| } | ||
| return C.GoStringN(nameP, C.int(nameLen)) | ||
| } | ||
|
|
||
| // Close deallocates this flags type explicitly. | ||
| func (ft *ComponentFlagsType) Close() { | ||
| if ft._ptr == nil { | ||
| return | ||
| } | ||
| runtime.SetFinalizer(ft, nil) | ||
| C.wasmtime_component_flags_type_delete(ft._ptr) | ||
| ft._ptr = nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This'll want to be below the
C.GoStringNcall below because thertvalue owns the returned string and needs to stay aliveThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, fix