Skip to content
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

feat(go): add resources support #759

Merged
merged 14 commits into from
Nov 27, 2023
Merged

Conversation

Mossaka
Copy link
Member

@Mossaka Mossaka commented Nov 17, 2023

[Moved from PR #737]

Introduction

This PR refactors the wit-bindgen-go to mainly support WIT Resources.

Should close #696 and #615

Resources

This PR has enabled WIT resources in the Go binding generator. The user interface for dealing with imported and exported resources look something like this

Exported Resources

  • Unlike other types that each interface generates a Go interface to implement, exported resource generates its own Go interface. Suppose we have a WIT that looks like
export exports: interface {
    resource x {
      constructor(a: s32);
      get-a: func() -> s32;
      set-a: func(a: s32);
      add: static func(x: x, a: s32) -> x;
    }
}

Then the generated Go code contains an interface ExportsX waiting to be implemented. This interface has all the methods GetA and SetA in it except constructor and static functions, which belong to the WIT interface's generated Go interface. The Go code looks like:

type ExportsImpl struct {}

type MyX struct {
	a int32
}

func (e ExportsImpl) ConstructorX(a int32) ExportsX {
	return &MyX{a: a}
}

func (x *MyX) MethodXGetA() int32 {
	return x.a
}

func (x *MyX) MethodXSetA(a int32) {
	x.a = a
}

func (e ExportsImpl) StaticXAdd(x ExportsX, a int32) ExportsX {
	return &MyX{a: x.MethodXGetA() + a}
}

Note that if there are no methods in the exported resource, than the generated Go interface is just an empty interface. Any struct implementation would work.

Imported Resources

Now moving on to the imported resources. Imported resources have a better Go semantics than exported ones. For example, given the following WIT resource

import imports: interface {
    resource y {
      constructor(a: s32);
      get-a: func() -> s32;
      set-a: func(a: s32);
      add: static func(y: y, a: s32) -> y;
    }
  }

You can use the Go APIs to interact with resource Y in the following way:

y := NewY(1)
if y.GetA() != 1 {
panic("y.GetA() != 1")
}
y.SetA(2)
if y.GetA() != 2 {
panic("y.GetA() != 2")
}

y2 := StaticYAdd(y, 3)
if y2.GetA() != 5 {
panic("y2.GetA() != 5")
}

y.SetA(5)

if y.GetA() != 5 {
panic("y.GetA() != 5")
}

Note that if the resource is empty (meaning it doesn't have any methods), then that resource's representation is just a int32.

More Improvement

  • This PR refactors the lib.rs file into smaller files, each dealing with their own responsibility. For instance, there is a bindgen.rs file dealing with function generator, and interface.rs dealing with interface generator.
  • It adds a new gofmt flag that runs gofmt to generated Go files.

Breaking Changes

  • It separated exported types from imported types. This is BREAKING change that the existing usage of exported types need to be prefixed with a Export to fix some minor bugs. More on this later with the package refactoring work.

@Mossaka Mossaka marked this pull request as draft November 17, 2023 07:11
@Mossaka Mossaka mentioned this pull request Nov 17, 2023
8 tasks
@Mossaka Mossaka marked this pull request as ready for review November 19, 2023 07:45
@Mossaka
Copy link
Member Author

Mossaka commented Nov 20, 2023

I think this PR is ready to review. I will write more in detail what this PR has changed.

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'm not reviewing the Go bits too too closely but all the other bits look good to me. There's some comments of mine from #757 that are applicable here as well, but otherwise this looks good to go

crates/c/src/lib.rs Outdated Show resolved Hide resolved
@Mossaka
Copy link
Member Author

Mossaka commented Nov 20, 2023

Yea, I will resolve the comments in #757 and rebase onto it.

@Mossaka
Copy link
Member Author

Mossaka commented Nov 27, 2023

One more commit in - now all the resource tests including codegen and runtime are passing 🎉

@Mossaka Mossaka force-pushed the go-resources2 branch 2 times, most recently from b35bea1 to 98e2974 Compare November 27, 2023 21:39
@alexcrichton
Copy link
Member

@Mossaka want to rebase this and I'll do a final pass?

Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>
Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>

feat(go): add resources support

Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>

test(c): add a runtime test for resource_borrow_simple

Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>

temp

Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>
Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>
Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>
Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>
Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>
…exported_interfaces

Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>
Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>
@Mossaka
Copy link
Member Author

Mossaka commented Nov 27, 2023

Rebased

crates/c/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to merge as-is, I can follow-up with the cleanup I mentioned

Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>
@Mossaka
Copy link
Member Author

Mossaka commented Nov 27, 2023

I will merge once the CI passes. Thanks for reviewing @alexcrichton

@alexcrichton
Copy link
Member

And thank you again for your work on this!

@alexcrichton
Copy link
Member

sorry wrong button

@Mossaka Mossaka merged commit 1af7e87 into bytecodealliance:main Nov 27, 2023
14 of 18 checks passed
@Mossaka Mossaka deleted the go-resources2 branch November 27, 2023 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wit-bindgen go: support resources
2 participants