bytecodealliance / bytecodealliance/go-modules

Resource export is unclear

Aperta
#337 6 commenti 1 reazione 0 assegnatari Vedi su GitHub
Lingua principale
Go
Stelle
148
Fork
20
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Hi!
I'm in the middle of migrating from the old binding generator in `wit-bindgen` to the latest version of `wit-bindgen-go` and got confused by what exactly new way of exporting resources is.

Let's take an example where we define and export a _counter_ resource:

```wit
package resource:example;

interface api {
resource counter {
constructor(name: string);
add: func(value: u64);
get: func() -> u64;
}
}

world example {
export api;
}
```

In the old binding generator to export this, it was possible to write a Go struct with methods on it and a constructor on the exported type:

```go
// struct representing the component
type ResourceExampleImpl struct {
}

// struct representing an instance of the resource
type Counter struct {
name string
current uint64
}

func (e *ResourceExampleImpl) ConstructorCounter(name string) resource_example.ExportsResourceExampleApiCounter {
return &Counter{
name: name,
current: 0,
}
}

func (e *Counter) MethodCounterAdd(value uint64) {
e.current += value
}

func (e *Counter) MethodCounterGet() uint64 {
return e.current
}

func init() {
resource_example.SetExportsResourceExampleApi(&ResourceExampleImpl{})
}
```

With the latest `wit-bindgen-go` the only way I found to do the same is to manually maintain a map of the generated resources indexed by `cm.Rep`:

```go
type Counter struct {
name string
current uint64
}

func NewCounter(name string) *Counter {
return &Counter{
name: name,
current: 0,
}
}

func (e *Counter) Add(value uint64) {
e.current += value
}

func (e *Counter) Get() uint64 {
return e.current
}

var counters = make(map[cm.Rep]*Counter)
var lastId = cm.Rep(0)

func init() {
resourceexampleapi.Exports.Counter.Constructor = func(name string) (result resourceexampleapi.Counter) {
counter := NewCounter(name)
id := lastId + 1
lastId = id
counters[id] = counter
return resourceexampleapi.CounterResourceNew(id)
}

resourceexampleapi.Exports.Counter.Destructor = func(id cm.Rep) {
delete(counters, id)
}

resourceexampleapi.Exports.Counter.Add = func(id cm.Rep, value uint64) {
counters[id].Add(value)
}

resourceexampleapi.Exports.Counter.Get = func(id cm.Rep) uint64 {
return counters[id].Get()
}
}
```

Is this how it is supposed to work or am I missing something?
The new binding generator version is much nicer than the previous one in many ways, but having to manually keep track of resources seems weird to me.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia confrontando le API di esportazione delle risorse precedente e più recente di wit-bindgen-go usando l'esempio del contatore WIT e gli handler resourceexampleapi.Exports.Counter generati. Determina se cm.Rep e la mappa counters manuale sono previsti, quindi documenta o proponi il comportamento previsto del ciclo di vita della risorsa e convalidalo con un test applicabile di generazione dei binding, se ne esiste uno.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
go, wasm
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.