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

generate cgo Objective-C bindings #56

Merged
merged 4 commits into from
Oct 8, 2021
Merged

Conversation

mgood
Copy link
Collaborator

@mgood mgood commented Jul 2, 2021

Generates cgo code and Go wrappers to call the Objective-C methods. Type information in api is generated using macschema to fetch and parse from the docs.

go run ./gen/cmd generates the *_objc.gen.go files containing the cgo code to invoke each Objective-C method, and Go wrappers on top of it.

The generated types are meant to be embedded by the author in a struct to include the generated methods, but allow the author to augment them with additional methods, like:

type NSFoo struct { gen_NSFoo }

@mgood
Copy link
Collaborator Author

mgood commented Jul 2, 2021

The NSString definition was generated with:

go run ./cmd gen > generated/foundation/nsstring.go 

And then run through go fmt to clean it up slightly (though this should likely be automated later).

objc2/objc.go Outdated
@@ -0,0 +1,52 @@
package objc2
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I started a new package for this to experiment with the minimal amount of code needed to support the generated definitions. Based on how this goes we could either integrate the dynamic Send methods directly, or move that into a separate package that could be called on an object Send(obj, "selector:")

@progrium
Copy link
Owner

progrium commented Jul 6, 2021

Can we schedule a call on Discord to go over this.

obj.Retain()
obj.Button().SetTitle("▶️ Ready")
// TODO implement inheritance to get "title" prop from button
appkit.NSButton_setTitle_(obj.Button(), foundation.NSString_FromString("▶️ Ready"))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@progrium I guess ideally we'd want macschema to parse out the "Inherits From" info, but for now I could implement a quick hack to parse this info out of the schema on the generator side:

    "Declaration": "@interface NSStatusBarButton : NSButton",

Copy link
Owner

Choose a reason for hiding this comment

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

👍

@progrium
Copy link
Owner

looks like its starting to come together!

@mgood mgood changed the title WIP generator generate cgo Objective-C bindings Oct 7, 2021
@mgood mgood marked this pull request as ready for review October 7, 2021 20:29
Generates cgo code and Go wrappers to call the Objective-C methods. Type information in `api` is generated using `macschema` to fetch and parse from the docs.

`go run ./gen/cmd` generates the `*_objc.gen.go` files containing the cgo code to invoke each Objective-C method, and Go wrappers on top of it.

The generated types are meant to be embedded by the author in a struct to include the generated methods, but allow the author to augment them with additional methods, like:
```
type NSFoo struct { gen_NSFoo }
```
@progrium
Copy link
Owner

progrium commented Oct 8, 2021

pls remove the 1.15 test and add 1.17 so checks pass as something here is using io/fs

@progrium progrium merged commit a9ab20f into progrium:main Oct 8, 2021
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.

2 participants