Skip to content

Commit

Permalink
feat(go): implement futures
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Volosatovs <[email protected]>
  • Loading branch information
rvolosatovs committed May 15, 2024
1 parent 7214188 commit 5e020a7
Show file tree
Hide file tree
Showing 99 changed files with 23,477 additions and 223 deletions.
841 changes: 620 additions & 221 deletions crates/wit-bindgen-go/src/interface.rs

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions crates/wit-bindgen-go/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ impl WorldGenerator for GoWrpc {
gen.gen.name_interface(resolve, id, name, false);
gen.types(id);

let identifier = Identifier::Interface(id, name);
let interface = &resolve.interfaces[id];
let name = match name {
WorldKey::Name(s) => s.to_string(),
Expand All @@ -393,7 +394,11 @@ impl WorldGenerator for GoWrpc {
} else {
name
};
gen.generate_imports(&instance, resolve.interfaces[id].functions.values());
gen.generate_imports(
identifier,
&instance,
resolve.interfaces[id].functions.values(),
);

gen.finish_append_submodule(&snake, module_path);
}
Expand All @@ -416,7 +421,11 @@ impl WorldGenerator for GoWrpc {
} else {
name.to_string()
};
gen.generate_imports(&instance, funcs.iter().map(|(_, func)| *func));
gen.generate_imports(
Identifier::World(world),
&instance,
funcs.iter().map(|(_, func)| *func),
);

let src = gen.finish();
for (k, v) in gen.deps.map {
Expand Down
3 changes: 3 additions & 0 deletions examples/go/http-outgoing-client/bindings/client.wrpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Generated by `wit-bindgen-wrpc-go` 0.1.0. DO NOT EDIT!
// client package contains wRPC bindings for `client` world
package client
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// Generated by `wit-bindgen-wrpc-go` 0.1.0. DO NOT EDIT!
package monotonic_clock

import (
bytes "bytes"
context "context"
binary "encoding/binary"
errors "errors"
fmt "fmt"
wasi__io__poll "github.com/wrpc/wrpc/examples/go/http-outgoing-client/bindings/wasi/io/poll"
wrpc "github.com/wrpc/wrpc/go"
io "io"
slog "log/slog"
)

type Pollable = wasi__io__poll.Pollable

// An instant in time, in nanoseconds. An instant is relative to an
// unspecified initial value, and can only be compared to instances from
// the same monotonic-clock.
type Instant = uint64

// A duration of time, in nanoseconds.
type Duration = uint64

// Read the current value of the clock.
//
// The clock is monotonic, therefore calling this function repeatedly will
// produce a sequence of non-decreasing values.
func Now(ctx__ context.Context, wrpc__ wrpc.Client) (r0__ uint64, close__ func() error, err__ error) {
if err__ = wrpc__.Invoke(ctx__, "wasi:clocks/[email protected]", "now", func(w__ wrpc.IndexWriter, r__ wrpc.IndexReadCloser) error {
close__ = r__.Close
_, err__ = w__.Write(nil)
if err__ != nil {
return fmt.Errorf("failed to write empty parameters: %w", err__)
}
r0__, err__ = func(r io.ByteReader) (uint64, error) {
var x uint64
var s uint
for i := 0; i < 10; i++ {
slog.Debug("reading u64 byte", "i", i)
b, err := r.ReadByte()
if err != nil {
if i > 0 && err == io.EOF {
err = io.ErrUnexpectedEOF
}
return x, fmt.Errorf("failed to read u64 byte: %w", err)
}
if b < 0x80 {
if i == 9 && b > 1 {
return x, errors.New("varint overflows a 64-bit integer")
}
return x | uint64(b)<<s, nil
}
x |= uint64(b&0x7f) << s
s += 7
}
return x, errors.New("varint overflows a 64-bit integer")
}(r__)
if err__ != nil {
return fmt.Errorf("failed to read result 0: %w", err__)
}
return nil
}); err__ != nil {
err__ = fmt.Errorf("failed to invoke `now`: %w", err__)
return
}
return
}

// Query the resolution of the clock. Returns the duration of time
// corresponding to a clock tick.
func Resolution(ctx__ context.Context, wrpc__ wrpc.Client) (r0__ uint64, close__ func() error, err__ error) {
if err__ = wrpc__.Invoke(ctx__, "wasi:clocks/[email protected]", "resolution", func(w__ wrpc.IndexWriter, r__ wrpc.IndexReadCloser) error {
close__ = r__.Close
_, err__ = w__.Write(nil)
if err__ != nil {
return fmt.Errorf("failed to write empty parameters: %w", err__)
}
r0__, err__ = func(r io.ByteReader) (uint64, error) {
var x uint64
var s uint
for i := 0; i < 10; i++ {
slog.Debug("reading u64 byte", "i", i)
b, err := r.ReadByte()
if err != nil {
if i > 0 && err == io.EOF {
err = io.ErrUnexpectedEOF
}
return x, fmt.Errorf("failed to read u64 byte: %w", err)
}
if b < 0x80 {
if i == 9 && b > 1 {
return x, errors.New("varint overflows a 64-bit integer")
}
return x | uint64(b)<<s, nil
}
x |= uint64(b&0x7f) << s
s += 7
}
return x, errors.New("varint overflows a 64-bit integer")
}(r__)
if err__ != nil {
return fmt.Errorf("failed to read result 0: %w", err__)
}
return nil
}); err__ != nil {
err__ = fmt.Errorf("failed to invoke `resolution`: %w", err__)
return
}
return
}

// Create a `pollable` which will resolve once the specified instant
// occured.
func SubscribeInstant(ctx__ context.Context, wrpc__ wrpc.Client, when uint64) (r0__ Pollable, close__ func() error, err__ error) {
if err__ = wrpc__.Invoke(ctx__, "wasi:clocks/[email protected]", "subscribe-instant", func(w__ wrpc.IndexWriter, r__ wrpc.IndexReadCloser) error {
close__ = r__.Close
var buf__ bytes.Buffer
writes__ := make(map[uint32]func(wrpc.IndexWriter) error, 1)
write0__, err__ := (func(wrpc.IndexWriter) error)(nil), func(v uint64, w interface {
io.ByteWriter
io.Writer
}) (err error) { b := make([]byte, binary.MaxVarintLen64); i := binary.PutUvarint(b, uint64(v)); slog.Debug("writing u64"); _, err = w.Write(b[:i]); return err }(when, &buf__)
if err__ != nil {
return fmt.Errorf("failed to write `when` parameter: %w", err__)
}
if write0__ != nil {
writes__[0] = write0__
}
_, err__ = w__.Write(buf__.Bytes())
if err__ != nil {
return fmt.Errorf("failed to write parameters: %w", err__)
}
r0__, err__ = (Pollable)(nil), errors.New("reading owned handles not supported yet")
if err__ != nil {
return fmt.Errorf("failed to read result 0: %w", err__)
}
return nil
}); err__ != nil {
err__ = fmt.Errorf("failed to invoke `subscribe-instant`: %w", err__)
return
}
return
}

// Create a `pollable` which will resolve once the given duration has
// elapsed, starting at the time at which this function was called.
// occured.
func SubscribeDuration(ctx__ context.Context, wrpc__ wrpc.Client, when uint64) (r0__ Pollable, close__ func() error, err__ error) {
if err__ = wrpc__.Invoke(ctx__, "wasi:clocks/[email protected]", "subscribe-duration", func(w__ wrpc.IndexWriter, r__ wrpc.IndexReadCloser) error {
close__ = r__.Close
var buf__ bytes.Buffer
writes__ := make(map[uint32]func(wrpc.IndexWriter) error, 1)
write0__, err__ := (func(wrpc.IndexWriter) error)(nil), func(v uint64, w interface {
io.ByteWriter
io.Writer
}) (err error) { b := make([]byte, binary.MaxVarintLen64); i := binary.PutUvarint(b, uint64(v)); slog.Debug("writing u64"); _, err = w.Write(b[:i]); return err }(when, &buf__)
if err__ != nil {
return fmt.Errorf("failed to write `when` parameter: %w", err__)
}
if write0__ != nil {
writes__[0] = write0__
}
_, err__ = w__.Write(buf__.Bytes())
if err__ != nil {
return fmt.Errorf("failed to write parameters: %w", err__)
}
r0__, err__ = (Pollable)(nil), errors.New("reading owned handles not supported yet")
if err__ != nil {
return fmt.Errorf("failed to read result 0: %w", err__)
}
return nil
}); err__ != nil {
err__ = fmt.Errorf("failed to invoke `subscribe-duration`: %w", err__)
return
}
return
}
Loading

0 comments on commit 5e020a7

Please sign in to comment.