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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
158 commits
Select commit Hold shift + click to select a range
1e45168
Support variadic arguments and slice, pointer types
fbbdev May 8, 2024
fe9aa66
Fix computation of type namespaces
fbbdev May 8, 2024
e892af5
Improve comments and general formatting
fbbdev May 8, 2024
04c4f2b
Set default values correctly for composite types
fbbdev May 8, 2024
e4eb212
Add templates for bindings
fbbdev May 8, 2024
4b24f9c
Simplify import list construction
fbbdev May 8, 2024
f1d50bb
Refactor type generation code
fbbdev May 8, 2024
dd47295
Support slices with pointer elements
fbbdev May 8, 2024
df10fa4
Match encoding/json behaviour in struct parser
fbbdev May 8, 2024
7a2b6a8
Update tests and example
fbbdev May 8, 2024
cc40377
Add tests for complex method signatures and json tag parsing
fbbdev May 8, 2024
cb06f0d
Add test `function_multiple_files`
abichinger May 8, 2024
1690c08
Attempt looking up idents with missing denotation
fbbdev May 8, 2024
2bba00e
Update test data
fbbdev May 8, 2024
6ec6a6f
fix quoted bool field
abichinger May 8, 2024
724815a
Test quoted booleans
fbbdev May 8, 2024
31c42fc
Delete old parser code
fbbdev May 8, 2024
3dee519
Remove old test data
fbbdev May 8, 2024
2e8f99c
Update bindgen flags
fbbdev May 8, 2024
fc1d6a1
Add package loading code
fbbdev May 8, 2024
d3cd7bf
Add static analyser
fbbdev May 8, 2024
23e18cc
Temporarily ignore binding generation code
fbbdev May 8, 2024
c6f9588
Add complex slice expressions test
fbbdev May 8, 2024
f95acf6
Fix variable reference analysis
fbbdev May 8, 2024
f7100ff
Unwrap casts to interface types
fbbdev May 8, 2024
8db8128
Complete code comments
fbbdev May 8, 2024
8057caa
Refactor static analyser
fbbdev May 8, 2024
c5f99cd
Restrict options struct usage
fbbdev May 8, 2024
066502b
Update tests
fbbdev May 8, 2024
000e2d9
Fix method selector sink and source processing
fbbdev May 8, 2024
e811316
Improve Set API
fbbdev May 8, 2024
42b1bab
Add package info collector
fbbdev May 8, 2024
9e1435a
Rename analyser package to analyse
fbbdev May 8, 2024
4e5f28c
Improve template functions
fbbdev May 8, 2024
ebecb14
Add index file templates
fbbdev May 8, 2024
807d547
Add glue code for binding generation
fbbdev May 8, 2024
df6d624
Refactor collection and rendering code
fbbdev May 8, 2024
cbc6b65
Implement binding generator
fbbdev May 8, 2024
13f635b
Implement global index generation
fbbdev May 8, 2024
e09e7e1
Improve marshaler and alias handling
fbbdev May 8, 2024
875b317
Use package path in binding calls by name
fbbdev May 8, 2024
23c4cc3
Implement model collection and rendering
fbbdev May 8, 2024
0c02416
Fix wrong exit condition in analyser
fbbdev May 8, 2024
f1dbbb1
Fix enum rendering
fbbdev May 8, 2024
21bb5f4
Generate shortcuts for all packages.
fbbdev May 8, 2024
980115d
Implement generator tests
fbbdev May 8, 2024
ef2357f
Ignore non-pointer bound types
fbbdev May 8, 2024
6b6cb6e
Treat main package specially
fbbdev May 8, 2024
84d402c
Compute stats
fbbdev May 8, 2024
d5480ae
Plug new API into generate command
fbbdev May 8, 2024
cb328ab
Support all named types
fbbdev May 8, 2024
2f05489
Update JS runtime
fbbdev May 8, 2024
de67f63
Report dual role types
fbbdev May 8, 2024
7c4c14b
Remove go1.22 syntax
fbbdev May 8, 2024
6146338
Fix type assertion in TS bindings
fbbdev May 8, 2024
50c0318
encoding/json compliance for arrays and slices
fbbdev May 8, 2024
a2b9a17
Ignore got files in testdata
fbbdev May 8, 2024
9f0594b
Cleanup type rendering mechanism
fbbdev May 8, 2024
cf8b38c
Update JS runtime
fbbdev May 8, 2024
a1dc557
Implement generic models
fbbdev May 8, 2024
81729ba
Add missing field in renderer initialisation
fbbdev May 8, 2024
ae85f72
Improve generic creation code
fbbdev May 8, 2024
1e39789
Add generic model test
fbbdev May 8, 2024
932bd39
Add error reporting infrastructure
fbbdev May 8, 2024
76fb0ba
Support configurable file names
fbbdev May 8, 2024
e7fce3c
Detect file naming collisions
fbbdev May 8, 2024
44c4bab
Print final error report
fbbdev May 8, 2024
2293a8f
New shortcut file structure + collision detection
fbbdev May 8, 2024
0fbf5fb
Update test layout and data
fbbdev May 8, 2024
61769be
Autoconfiguration for analyser tests
fbbdev May 8, 2024
60e6da3
Live progress reporting
fbbdev May 8, 2024
4a201e7
Update code comments
fbbdev May 8, 2024
4924f74
Fix model doc rendering
fbbdev May 8, 2024
f2ce52f
Simplify name resolution
fbbdev May 8, 2024
28263bf
Add test for out of tree types
fbbdev May 8, 2024
675578e
Fix generic creation code
fbbdev May 8, 2024
8229fa6
Fix potential collisions between methods and models
fbbdev May 8, 2024
a29e805
Fix generic class alias rendering
fbbdev May 8, 2024
06c4116
Report model discovery in debug mode
fbbdev May 8, 2024
4eb9995
Add interface mode for JS
fbbdev May 8, 2024
e9b3e6b
Collect interface method comments
fbbdev May 8, 2024
8cbcbe1
Add interface methods test
fbbdev May 8, 2024
66c164a
Unwrap generic instantiations in method receivers
fbbdev May 8, 2024
863e019
Fix rendering of nullable types in interface mode
fbbdev May 8, 2024
9f04593
Fix rendering of class aliases
fbbdev May 8, 2024
403426f
Expose promise cancel method to typescript
fbbdev May 8, 2024
396ae1e
Update test data
fbbdev May 8, 2024
a65868f
Update binding example
fbbdev May 8, 2024
9f0d661
Fix rendering of aliased quoted type params
fbbdev May 8, 2024
a9c1f5c
Move to strongly typed bindings
fbbdev May 8, 2024
130219a
Implement lightweight analyser
fbbdev May 8, 2024
c35ef2b
Update test cases
fbbdev May 8, 2024
115b9d2
Update binding example
fbbdev May 8, 2024
280c6eb
Add complex instantiation test
fbbdev May 8, 2024
fc51b15
Load full dependency tree
fbbdev May 8, 2024
5edcd3d
Rewrite collector
fbbdev May 8, 2024
ad89edf
Update renderer to match new collector
fbbdev May 8, 2024
26f9669
Update generator to match new collector
fbbdev May 8, 2024
0b67380
Update test data
fbbdev May 8, 2024
1817541
Update binding example
fbbdev May 8, 2024
7aa5f4a
Configure includes and injections by language
fbbdev May 8, 2024
e44ac7c
Improve system path resolution
fbbdev May 8, 2024
8b8f469
Support rich conditions in inject/include directives
fbbdev May 8, 2024
ac7a25a
Fix error handling in Generator.Generate
fbbdev May 8, 2024
2fbeb75
Retrieve compiled go file paths from fileset
fbbdev May 8, 2024
e5bcbb3
Do not rely on struct info in struct flattening algorithm
fbbdev May 8, 2024
f337c9b
Fix doc comment for findDeclaraion
fbbdev May 8, 2024
7914127
Fix bugs in embedded field handling
fbbdev May 8, 2024
954abf4
Fix bugs and comments in package collection
fbbdev May 8, 2024
9964bb8
Remove useless fields from ServiceInfo
fbbdev May 8, 2024
eb1e7c8
Fix empty line at the beginning of TS indexes
fbbdev May 8, 2024
e620326
Remove global index and shortcuts
fbbdev May 8, 2024
419de8f
Remove generation tests for individual packages
fbbdev May 8, 2024
d450a28
Enforce lower-case file names
fbbdev May 8, 2024
0c88ac6
Update test data
fbbdev May 8, 2024
5f3330e
Improve error reporting
fbbdev May 8, 2024
cb60a3f
Update binding example
fbbdev May 8, 2024
532416a
Reintroduce go1.22 syntax
fbbdev May 8, 2024
1a00c64
Improve relative import path computation
fbbdev May 8, 2024
42e59c5
Improve alias support
fbbdev May 8, 2024
3334c08
Add alias test
fbbdev May 8, 2024
155701f
Update test data
fbbdev May 8, 2024
007a5c7
Remove no services error
fbbdev May 8, 2024
c96b816
Rename global analyser test
fbbdev May 8, 2024
8e9983a
Add workaround and test for bug in typeutil.Map
fbbdev May 8, 2024
6946136
Update test data
fbbdev May 8, 2024
2d11008
Do not split fully qualified names
fbbdev May 8, 2024
d6a83b3
Update typeutil package and remove workaround
fbbdev May 8, 2024
d81656c
Unify alias/named type handling
fbbdev May 8, 2024
939909a
Fix rendering of generic named class aliases
fbbdev May 8, 2024
d21411d
Fix rendering of array types
fbbdev May 9, 2024
dd69cce
Minor tweaks and cleanups
fbbdev May 9, 2024
7b3d2dc
Rmove namespaced export construct
fbbdev May 10, 2024
92719ee
Update test data
fbbdev May 10, 2024
f502615
Update binding example
fbbdev May 10, 2024
4dc2715
Break type cycles
fbbdev May 12, 2024
72077b7
Fix typo in comment
fbbdev May 12, 2024
d2d6061
Fix creation code for cyclic types
fbbdev May 12, 2024
f741cb2
Fix type of variadic params in interface mode
fbbdev May 12, 2024
db2e652
Update test data
fbbdev May 12, 2024
a3ccd3c
Fix bad whitespace
fbbdev May 12, 2024
a1ff3cb
Refactor type assertions inside bound methods
fbbdev May 12, 2024
8e81fcc
Update test data
fbbdev May 12, 2024
d3d0b00
Rename field application.Options.Bind to Services
fbbdev May 17, 2024
a12edba
Rename parser package to generator
fbbdev May 17, 2024
44fc228
Update binding example
fbbdev May 17, 2024
70ac09a
Update test data
fbbdev May 17, 2024
ff46b29
Update generator readme
fbbdev May 18, 2024
ef32bce
Add typescript test harness
fbbdev May 18, 2024
26f6430
Move test output to new subfolder
fbbdev May 18, 2024
6f0194d
Fix code generation bugs
fbbdev May 18, 2024
189a495
Use .js extensions in TS mode imports
fbbdev May 18, 2024
2ecd200
Update test data
fbbdev May 18, 2024
77e522c
Revert default generator output dir to frontend/bindings
fbbdev May 18, 2024
66677b6
Bump runtime package version
fbbdev May 18, 2024
8f282c3
Update templates
fbbdev May 18, 2024
39031c4
Update changelog
fbbdev May 18, 2024
bc598cb
Improve newline handling
fbbdev May 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions mkdocs-website/docs/en/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The `@wailsio/runtime` package does not publish its API on the `window.wails` object, and does not start the WML system. This has been done to improve encapsulation. The WML system can be started manually if desired by calling the new `WML.Enable` method. The bundled JS runtime script still performs both operations automatically. By [@fbbdev](https://github.com/fbbdev) in [#3295](https://github.com/wailsapp/wails/pull/3295)
- The Window API module `@wailsio/runtime/src/window` now exposes the containing window object as a default export. It is not possible anymore to import individual methods through ESM named or namespace import syntax.
- The JS window API has been updated to match the current Go `WebviewWindow` API. Some methods have changed name or prototype, specifically: `Screen` becomes `GetScreen`; `GetZoomLevel`/`SetZoomLevel` become `GetZoom`/`SetZoom`; `GetZoom`, `Width` and `Height` now return values directly instead of wrapping them within objects. By [@fbbdev](https://github.com/fbbdev) in [#3295](https://github.com/wailsapp/wails/pull/3295)
- The binding generator now uses calls by ID by default. The `-id` CLI option has been removed. Use the `-names` CLI option to switch back to calls by name. By [@fbbdev](https://github.com/fbbdev) in [#3468](https://github.com/wailsapp/wails/pull/3468)
- New binding code layout: output files were previously organised in folders named after their containing package; now full Go import paths are used, including the module path. By [@fbbdev](https://github.com/fbbdev) in [#3468](https://github.com/wailsapp/wails/pull/3468)
- The struct field `application.Options.Bind` has been renamed to `application.Options.Services`. By [@fbbdev](https://github.com/fbbdev) in [#3468](https://github.com/wailsapp/wails/pull/3468)
- New syntax for binding services: service instances must now be wrapped in a call to `application.NewService`. By [@fbbdev](https://github.com/fbbdev) in [#3468](https://github.com/wailsapp/wails/pull/3468)

### Removed

Expand Down
4 changes: 4 additions & 0 deletions v3/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
version: "3"

includes:
generator:
taskfile: ./internal/generator
dir: ./internal/generator

runtime:
taskfile: ./internal/runtime
dir: ./internal/runtime
Expand Down
14 changes: 11 additions & 3 deletions v3/cmd/wails3/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"github.com/pterm/pterm"
"github.com/samber/lo"
"os"
"runtime/debug"

"github.com/pterm/pterm"
"github.com/samber/lo"

"github.com/leaanthony/clir"
"github.com/wailsapp/wails/v3/internal/commands"
"github.com/wailsapp/wails/v3/internal/flags"
)

func init() {
Expand Down Expand Up @@ -42,7 +44,13 @@ func main() {
generate.NewSubCommandFunction("build-assets", "Generate build assets", commands.GenerateBuildAssets)
generate.NewSubCommandFunction("icons", "Generate icons", commands.GenerateIcons)
generate.NewSubCommandFunction("syso", "Generate Windows .syso file", commands.GenerateSyso)
generate.NewSubCommandFunction("bindings", "Generate bindings + models", commands.GenerateBindings)
bindgen := generate.NewSubCommand("bindings", "Generate bindings + models")
var bindgenFlags flags.GenerateBindingsOptions
bindgen.AddFlags(&bindgenFlags)
bindgen.Action(func() error {
return commands.GenerateBindings(&bindgenFlags, bindgen.OtherArgs())
})
bindgen.LongDescription("\nUsage: wails3 generate bindings [flags] [patterns...]\n\nPatterns match packages to scan for bound types.\nPattern format is analogous to that of the Go build tool,\ne.g. './...' matches packages in the current directory and all descendants.\nIf no pattern is given, the tool will fall back to the current directory.")
generate.NewSubCommandFunction("constants", "Generate JS constants from Go", commands.GenerateConstants)
generate.NewSubCommandFunction(".desktop", "Generate .desktop file", commands.GenerateDotDesktop)
generate.NewSubCommandFunction("appimage", "Generate Linux AppImage", commands.GenerateAppImage)
Expand Down
32 changes: 27 additions & 5 deletions v3/examples/binding/GreetService.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
package main

import "github.com/wailsapp/wails/v3/examples/binding/data"
import (
"strconv"

"github.com/wailsapp/wails/v3/examples/binding/data"
)

// GreetService is a service that greets people
type GreetService struct {
}

// Greet greets a person
func (*GreetService) Greet(name string) string {
return "Hello " + name
func (*GreetService) Greet(name string, counts ...int) string {
times := " "

for index, count := range counts {
if index > 0 {
times += ", "
}
times += strconv.Itoa(count)
}

if len(counts) > 0 {
times += " times "
}

return "Hello" + times + name
}

// GreetPerson greets a person
func (*GreetService) GreetPerson(person data.Person) string {
return "Hello " + person.Name
func (srv *GreetService) GreetPerson(person data.Person) string {
return srv.Greet(person.Name, person.Counts...)
}

// GetPerson returns a person with the given name.
func (srv *GreetService) GetPerson(name string) data.Person {
return data.Person{Name: name}
}
28 changes: 0 additions & 28 deletions v3/examples/binding/assets/bindings/data/models.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT


export interface Address {
street: string;
state: string;
country: string;
}
export * from "./models.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT

import {Create as $Create} from "/wails/runtime.js";

/**
* Person holds someone's most important attributes
*/
export class Person {
/**
* Creates a new Person instance.
* @param {Partial<Person>} [$$source = {}] - The source object to create the Person.
*/
constructor($$source = {}) {
if (!("name" in $$source)) {
/**
* Name is the person's name
* @member
* @type {string}
*/
this["name"] = "";
}
if (!("counts" in $$source)) {
/**
* Counts tracks the number of time the person
* has been greeted in various ways
* @member
* @type {number[]}
*/
this["counts"] = [];
}

Object.assign(this, $$source);
}

/**
* Creates a new Person instance from a string or object.
* @param {any} [$$source = {}]
* @returns {Person}
*/
static createFrom($$source = {}) {
const $$createField1_0 = $$createType0;
let $$parsedSource = typeof $$source === 'string' ? JSON.parse($$source) : $$source;
if ("counts" in $$parsedSource) {
$$parsedSource["counts"] = $$createField1_0($$parsedSource["counts"]);
}
return new Person(/** @type {Partial<Person>} */($$parsedSource));
}
}

// Private type creation functions
const $$createType0 = $Create.Array($Create.Any);
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT

/**
* GreetService is a service that greets people
* @module
*/

import {Call as $Call, Create as $Create} from "/wails/runtime.js";

import * as data$0 from "./data/models.js";

/**
* GetPerson returns a person with the given name.
* @param {string} name
* @returns {Promise<data$0.Person> & { cancel(): void }}
*/
export function GetPerson(name) {
let $resultPromise = /** @type {any} */($Call.ByID(2952413357, name));
let $typingPromise = /** @type {any} */($resultPromise.then(($result) => {
return $$createType0($result);
}));
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);
return $typingPromise;
}

/**
* Greet greets a person
* @param {string} name
* @param {number[]} counts
* @returns {Promise<string> & { cancel(): void }}
*/
export function Greet(name, ...counts) {
let $resultPromise = /** @type {any} */($Call.ByID(1411160069, name, counts));
return $resultPromise;
}

/**
* GreetPerson greets a person
* @param {data$0.Person} person
* @returns {Promise<string> & { cancel(): void }}
*/
export function GreetPerson(person) {
let $resultPromise = /** @type {any} */($Call.ByID(4021313248, person));
return $resultPromise;
}

// Private type creation functions
const $$createType0 = data$0.Person.createFrom;
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT


export interface Address {
street: string;
state: string;
country: string;
}
import * as GreetService from "./greetservice.js";
export {
GreetService
};
28 changes: 0 additions & 28 deletions v3/examples/binding/assets/bindings/main/GreetService.js

This file was deleted.

42 changes: 39 additions & 3 deletions v3/examples/binding/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,52 @@
<div class="input-box" id="input">
<input autofocus autocomplete="off" class="input" id="name" type="text"/>
<button class="btn" id="greet-btn">Greet</button>
<button class="btn" id="greetperson-btn">GreetPerson</button>
</div>
<script type="module">
import * as GreetService from "./bindings/main/GreetService.js";
import {GreetService} from "./bindings/github.com/wailsapp/wails/v3/examples/binding/index.js";
import * as data from "./bindings/github.com/wailsapp/wails/v3/examples/binding/data/index.js";

let greetCount = 0, greetPersonCount = 0;

async function greet() {
document.getElementById("result").innerHTML =
await GreetService.Greet(document.getElementById("name").value);
++greetCount;
if (greetPersonCount < 1) {
if (greetCount < 2) {
document.getElementById("result").innerHTML =
await GreetService.Greet(document.getElementById("name").value);
} else {
document.getElementById("result").innerHTML =
await GreetService.Greet(document.getElementById("name").value, greetCount);
}
} else {
document.getElementById("result").innerHTML =
await GreetService.Greet(document.getElementById("name").value, greetCount, greetPersonCount);
}
}

async function greetPerson() {
++greetPersonCount;

const name = document.getElementById("name").value;

let person;
if (name === "Quincy") {
person = await GreetService.GetPerson(name);
} else {
person = new data.Person({ name });
}

if (greetCount > 0)
person.counts.push(greetCount);
if (greetCount > 0 || greetPersonCount > 1)
person.counts.push(greetPersonCount);

document.getElementById("result").innerHTML = await GreetService.GreetPerson(person);
}

document.getElementById("greet-btn").onclick = greet;
document.getElementById("greetperson-btn").onclick = greetPerson;
</script>
</body>
</html>
4 changes: 4 additions & 0 deletions v3/examples/binding/data/person.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ package data
type Person struct {
// Name is the person's name
Name string `json:"name"`

// Counts tracks the number of time the person
// has been greeted in various ways
Counts []int `json:"counts"`
}
5 changes: 2 additions & 3 deletions v3/examples/binding/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"embed"
_ "embed"
"log"

"github.com/wailsapp/wails/v3/pkg/application"
Expand All @@ -13,8 +12,8 @@ var assets embed.FS

func main() {
app := application.New(application.Options{
Bind: []any{
&GreetService{},
Services: []application.Service{
application.NewService(&GreetService{}),
},
Assets: application.AssetOptions{
Handler: application.BundledAssetFileServer(assets),
Expand Down
Loading
Loading