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

[v3] New binding generator #3468

Merged
merged 158 commits into from May 19, 2024
Merged

Conversation

fbbdev
Copy link

@fbbdev fbbdev commented May 8, 2024

Description

This PR proposes an entirely new binding generator for Wails v3, based upon the official type-checker package go/types.

Details have been (and still are being) discussed in the dedicated forum on the Discord server. A preliminary feature list is available here (combine all features marked 'essential' and 'optional'). An improved and more complete feature list is being prepared.

The PR branch has a tortuous commit history that goes through multiple refactorings and rewrites. Things are very entangled and cleaning it up would not be easy. Older commits hide sometimes massive amounts of test data with multiple updates. Should the PR be merged at some point, I suggest squashing it into a single commit and erasing the whole commit history from the commit message.

A few heads-ups for testers and reviewers:

  • this patch requires an up-to-date Go distribution (v1.22.3) and golang.org/x/tools module (v0.21.0) due to bugs that have been patched only recently; the go.mod file has been updated accordingly;
  • on some systems, running tests and generating bindings may take a very long time on the first run or just after any change to the Wails application package. Subsequent runs will be much faster thanks to caching. The cause is cgo preprocessing of the application package: in the future maintainers might want to consider moving cgo code to an internal subpackage to mitigate this issue;
  • the generate bindings command now generates calls by ID by default; the -names option can be used to switch to calls by name;
  • the patch introduces a new package layout that will break JS imports tailored to the current layout;
  • the patch introduces a new approach to binding configuration: service instances must be wrapped in calls to application.NewService (see the examples/binding for an example).

This PR introduces some (hopefully non-breaking) changes to the binding backend and the JS runtime:

  • the binding runtime now supports services of any named type, not just structs;
  • calls by name use full paths instead of package names;
  • the name-to-method map, as well as structs BoundMethod and CallOptions have been changed to store fully qualified names (strings of the form "[path].[type].[method]") instead of path-type-method triples;
  • the JS runtime has been extended with supporting functions for return value translation.

Todos

  • Integrate the new generator with project templates.
  • Update changelog.
  • Improve and update feature list.
  • Write developer documentation. Some suggested topics off the top of my head:
    • CLI options;
    • output code layout;
    • service discovery logic;
    • supported directives;
    • describe in detail how Go types are translated to TS types;
    • describe the differences between class and interface mode.
  • Write maintainer documentation:
    • generator code layout;
    • how to use the test suite;
    • ...?
  • Increase test coverage?
  • Test logs for expected/unexpected messages + add tests triggering expected errors/warnings?
  • Deduplicate JS/TS template code.

The last point is not essential for functionality but I've come to see it as critical for maintainability: there is too much duplicated code and it is way too easy for the various copies to get out of sync. The resulting bugs are often hard to notice because they do not produce hard failures or warning messages. This has happened to me many many times while working on the patch and I fear it is going to happen to others too. Templates are based upon the text/template package. Knowledgeable people are welcome to contribute!

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Dedicated test suite (that has been enriched with new tests) passes fully. Its output has been examined carefully (please do review it!). The binding example works fine.

  • Windows
  • macOS
  • Linux

Test Configuration

# System
┌──────────────────────────────────────────────────────────┐
| Name          | MacOS                                    |
| Version       | 12.6.6                                   |
| ID            | 21G646                                   |
| Branding      | Monterey                                 |
| Platform      | darwin                                   |
| Architecture  | amd64                                    |
| Apple Silicon | unknown                                  |
| CPU           | Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz |
| CPU           | Unknown                                  |
| GPU           | Unknown                                  |
| Memory        | Unknown                                  |
└──────────────────────────────────────────────────────────┘

# Build Environment
┌─────────────────────────────────────────────────────────┐
| Wails CLI    | v3.0.0-alpha.4                           |
| Go Version   | go1.22.3                                 |
| Revision     | 02d9bfe0b3e9362863a638429dc5f2242f627fa5 |
| Modified     | false                                    |
| -buildmode   | exe                                      |
| -compiler    | gc                                       |
| CGO_CFLAGS   |                                          |
| CGO_CPPFLAGS |                                          |
| CGO_CXXFLAGS |                                          |
| CGO_ENABLED  | 1                                        |
| CGO_LDFLAGS  |                                          |
| GOAMD64      | v1                                       |
| GOARCH       | amd64                                    |
| GOOS         | darwin                                   |
| vcs          | git                                      |
| vcs.modified | false                                    |
| vcs.revision | 02d9bfe0b3e9362863a638429dc5f2242f627fa5 |
| vcs.time     | 2024-05-08T19:47:46Z                     |
└─────────────────────────────────────────────────────────┘

# Dependencies
┌───────────────────────────┐
| *NSIS           | v3.09   |
| Xcode cli tools | 2395    |
| npm             | 10.5.0  |
└─ * - Optional Dependency ─┘

Checklist:

  • I have updated website/src/pages/changelog.mdx with details of this PR
  • My code follows the general coding style of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

fbbdev and others added 30 commits May 8, 2024 14:58
Additionally:
* fixes generation of tuple return type
* improves imports and namespacing in JS mode
* general cleanup of generated code
Improves support for unknown types (encoded as any) and maps (using
Typescript index signatures)
* Makes call by ID the default
@fbbdev fbbdev force-pushed the feat/bindgen_v2_alltypes branch 2 times, most recently from 609611c to 6d42438 Compare May 18, 2024 13:16
Copy link
Member

@leaanthony leaanthony left a comment

Choose a reason for hiding this comment

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

Legendary!

@leaanthony leaanthony self-requested a review May 19, 2024 01:31
Copy link
Member

@leaanthony leaanthony left a comment

Choose a reason for hiding this comment

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

Looks like there's some newline issues when running the tests on Windows

@leaanthony leaanthony merged commit 90b7ea9 into wailsapp:v3-alpha May 19, 2024
4 checks passed
@fbbdev fbbdev deleted the feat/bindgen_v2_alltypes branch May 19, 2024 14:56
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.

None yet

3 participants