Skip to content

Commit b4512a6

Browse files
committed
refactor(codegen): Extract model generators
1 parent 3ea5d30 commit b4512a6

13 files changed

+195
-152
lines changed

internal/code-gen/script-wrappers/constructor_builder.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package wrappers
22

33
import (
4+
. "github.com/gost-dom/code-gen/script-wrappers/model"
45
g "github.com/gost-dom/generators"
56
)
67

@@ -29,8 +30,7 @@ func (builder ConstructorBuilder) NewFunctionTemplateOfWrappedMethod(name string
2930

3031
type PrototypeInstaller struct {
3132
v8Iso
32-
Proto v8PrototypeTemplate
33-
// InstanceTmpl v8InstanceTemplate
33+
Proto v8PrototypeTemplate
3434
Wrapper WrapperInstance
3535
}
3636

internal/code-gen/script-wrappers/es_wrapper_generator.go

+1-142
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ package wrappers
22

33
import (
44
"fmt"
5-
"iter"
65
"log/slog"
76
"strings"
87

98
"github.com/gost-dom/code-gen/customrules"
109
. "github.com/gost-dom/code-gen/internal"
11-
"github.com/gost-dom/code-gen/packagenames"
1210
"github.com/gost-dom/code-gen/script-wrappers/configuration"
11+
. "github.com/gost-dom/code-gen/script-wrappers/model"
1312
"github.com/gost-dom/code-gen/stdgen"
1413
g "github.com/gost-dom/generators"
1514
"github.com/gost-dom/webref/idl"
16-
legacy "github.com/gost-dom/webref/idl/legacy"
1715

1816
"github.com/dave/jennifer/jen"
1917
)
@@ -175,145 +173,6 @@ func createOperation(
175173
return op
176174
}
177175

178-
type ESOperationArgument struct {
179-
Name string
180-
Type string
181-
Optional bool
182-
Variadic bool
183-
IdlType legacy.IdlTypes
184-
ArgumentSpec configuration.ESMethodArgument
185-
Ignore bool
186-
}
187-
188-
func (a ESOperationArgument) OptionalInGo() bool {
189-
hasDefault := a.ArgumentSpec.HasDefault
190-
return a.Optional && !hasDefault
191-
}
192-
193-
func (a ESOperationArgument) DefaultValueInGo() (name string, ok bool) {
194-
ok = a.Optional && a.ArgumentSpec.HasDefault
195-
if defaultValue := a.ArgumentSpec.DefaultValue; defaultValue != "" {
196-
name = defaultValue
197-
} else {
198-
name = fmt.Sprintf("default%s", a.Type)
199-
}
200-
return
201-
}
202-
203-
type ESOperation struct {
204-
Name string
205-
NotImplemented bool
206-
RetType idl.RetType
207-
HasError bool
208-
CustomImplementation bool
209-
MethodCustomization configuration.ESMethodWrapper
210-
Arguments []ESOperationArgument
211-
}
212-
213-
func (o ESOperation) CallbackMethodName() string {
214-
return idl.SanitizeName(o.Name)
215-
}
216-
217-
func (op ESOperation) GetHasError() bool {
218-
return op.HasError
219-
}
220-
221-
func (op ESOperation) HasResult() bool {
222-
return op.RetType.IsDefined()
223-
}
224-
225-
func (o ESOperation) Encoder() string {
226-
if e := o.MethodCustomization.Encoder; e != "" {
227-
return e
228-
}
229-
converter := "to"
230-
if o.RetType.Nullable {
231-
converter += "Nullable"
232-
}
233-
converter += idlNameToGoName(o.RetType.TypeName)
234-
return converter
235-
}
236-
237-
type ESAttribute struct {
238-
Name string
239-
Getter *ESOperation
240-
Setter *ESOperation
241-
}
242-
243-
type ESConstructorData struct {
244-
Spec *configuration.IdlInterfaceConfiguration
245-
IdlInterface idl.Interface
246-
IdlInterfaceName string
247-
Inheritance string
248-
Operations []ESOperation
249-
Attributes []ESAttribute
250-
Constructor *ESOperation
251-
RunCustomCode bool
252-
}
253-
254-
// Return the idl mixin interfaces included in this interface AND that has been
255-
// included in the configuration
256-
func (d ESConstructorData) Includes() []idl.Interface {
257-
var result []idl.Interface
258-
for _, i := range d.IdlInterface.Includes {
259-
if _, configured := d.Spec.DomSpec.Interfaces[i.Name]; configured {
260-
result = append(result, i)
261-
}
262-
}
263-
return result
264-
}
265-
266-
func (d ESConstructorData) GetInternalPackage() string {
267-
if d.Name() == "Event" {
268-
return packagenames.Events
269-
}
270-
return packagenames.PackageName(d.Spec.DomSpec.Name)
271-
}
272-
273-
func (d ESConstructorData) WrapperFunctionsToInstall() iter.Seq[ESOperation] {
274-
return func(yield func(ESOperation) bool) {
275-
for _, op := range d.Operations {
276-
if !op.MethodCustomization.Ignored && !yield(op) {
277-
return
278-
}
279-
}
280-
}
281-
}
282-
283-
func (d ESConstructorData) AttributesToInstall() iter.Seq[ESAttribute] {
284-
return func(yield func(ESAttribute) bool) {
285-
for _, a := range d.Attributes {
286-
if !yield(a) {
287-
return
288-
}
289-
}
290-
}
291-
}
292-
293-
func (d ESConstructorData) WrapperFunctionsToGenerate() iter.Seq[ESOperation] {
294-
return func(yield func(ESOperation) bool) {
295-
for op := range d.WrapperFunctionsToInstall() {
296-
if !op.MethodCustomization.CustomImplementation && !yield(op) {
297-
return
298-
}
299-
}
300-
for _, a := range d.Attributes {
301-
if a.Getter != nil && !a.Getter.CustomImplementation {
302-
if !yield(*a.Getter) {
303-
return
304-
}
305-
}
306-
if a.Setter != nil && !a.Setter.CustomImplementation {
307-
if !yield(*a.Setter) {
308-
return
309-
}
310-
}
311-
}
312-
}
313-
}
314-
315-
func (d ESConstructorData) Name() string { return d.Spec.TypeName }
316-
317176
func ReturnOnAnyError(errNames []g.Generator) g.Generator {
318177
if len(errNames) == 0 {
319178
return g.Noop

internal/code-gen/script-wrappers/goja_generators.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/dave/jennifer/jen"
77
. "github.com/gost-dom/code-gen/internal"
8+
"github.com/gost-dom/code-gen/script-wrappers/model"
89
"github.com/gost-dom/generators"
910
g "github.com/gost-dom/generators"
1011
)
@@ -18,7 +19,7 @@ var (
1819
)
1920

2021
type GojaNamingStrategy struct {
21-
ESConstructorData
22+
model.ESConstructorData
2223
}
2324

2425
func (s GojaNamingStrategy) PrototypeWrapperBaseName() string {
@@ -44,15 +45,15 @@ func (gen GojaTargetGenerators) PlatformInfoArg() g.Generator { return g.Id("c")
4445
// CreateConstructor has no effect for Goja. It's currently based on a system
4546
// that it automatically creates the constructors based on whether or not they
4647
// call the ingerface
47-
func (gen GojaTargetGenerators) CreateHostInitializer(ESConstructorData) g.Generator {
48+
func (gen GojaTargetGenerators) CreateHostInitializer(model.ESConstructorData) g.Generator {
4849
return g.Noop
4950
}
5051

51-
func (gen GojaTargetGenerators) CreateConstructorCallback(ESConstructorData) g.Generator {
52+
func (gen GojaTargetGenerators) CreateConstructorCallback(model.ESConstructorData) g.Generator {
5253
return g.Noop
5354
}
5455

55-
func (gen GojaTargetGenerators) CreateInitFunction(data ESConstructorData) g.Generator {
56+
func (gen GojaTargetGenerators) CreateInitFunction(data model.ESConstructorData) g.Generator {
5657
naming := GojaNamingStrategy{data}
5758
return g.FunctionDefinition{
5859
Name: "init",
@@ -68,7 +69,7 @@ func (gen GojaTargetGenerators) CreateInitFunction(data ESConstructorData) g.Gen
6869
// CreatePrototypeInitializer creates the "initializePrototype" method, which
6970
// sets all the properties on the prototypes on this class.
7071
func (gen GojaTargetGenerators) CreatePrototypeInitializer(
71-
data ESConstructorData,
72+
data model.ESConstructorData,
7273
body g.Generator,
7374
) g.Generator {
7475
naming := GojaNamingStrategy{data}
@@ -87,7 +88,7 @@ func (gen GojaTargetGenerators) CreatePrototypeInitializer(
8788
}
8889

8990
func (gen GojaTargetGenerators) CreatePrototypeInitializerBody(
90-
data ESConstructorData,
91+
data model.ESConstructorData,
9192
) g.Generator {
9293
naming := GojaNamingStrategy{data}
9394
receiver := g.NewValue(naming.ReceiverName())
@@ -124,8 +125,8 @@ func (gen GojaTargetGenerators) ReturnErrMsg(errGen g.Generator) g.Generator {
124125
}
125126

126127
func (gen GojaTargetGenerators) CreateMethodCallbackBody(
127-
data ESConstructorData,
128-
op ESOperation,
128+
data model.ESConstructorData,
129+
op model.ESOperation,
129130
) g.Generator {
130131
callArgument := g.Id("c")
131132
naming := GojaNamingStrategy{data}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package model
2+
3+
type ESAttribute struct {
4+
Name string
5+
Getter *ESOperation
6+
Setter *ESOperation
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package model
2+
3+
import (
4+
"iter"
5+
6+
"github.com/gost-dom/code-gen/packagenames"
7+
"github.com/gost-dom/code-gen/script-wrappers/configuration"
8+
"github.com/gost-dom/webref/idl"
9+
)
10+
11+
type ESConstructorData struct {
12+
Spec *configuration.IdlInterfaceConfiguration
13+
IdlInterface idl.Interface
14+
IdlInterfaceName string
15+
Inheritance string
16+
Operations []ESOperation
17+
Attributes []ESAttribute
18+
Constructor *ESOperation
19+
RunCustomCode bool
20+
}
21+
22+
// Return the idl mixin interfaces included in this interface AND that has been
23+
// included in the configuration
24+
func (d ESConstructorData) Includes() []idl.Interface {
25+
var result []idl.Interface
26+
for _, i := range d.IdlInterface.Includes {
27+
if _, configured := d.Spec.DomSpec.Interfaces[i.Name]; configured {
28+
result = append(result, i)
29+
}
30+
}
31+
return result
32+
}
33+
34+
func (d ESConstructorData) GetInternalPackage() string {
35+
if d.Name() == "Event" {
36+
return packagenames.Events
37+
}
38+
return packagenames.PackageName(d.Spec.DomSpec.Name)
39+
}
40+
41+
func (d ESConstructorData) WrapperFunctionsToInstall() iter.Seq[ESOperation] {
42+
return func(yield func(ESOperation) bool) {
43+
for _, op := range d.Operations {
44+
if !op.MethodCustomization.Ignored && !yield(op) {
45+
return
46+
}
47+
}
48+
}
49+
}
50+
51+
func (d ESConstructorData) AttributesToInstall() iter.Seq[ESAttribute] {
52+
return func(yield func(ESAttribute) bool) {
53+
for _, a := range d.Attributes {
54+
if !yield(a) {
55+
return
56+
}
57+
}
58+
}
59+
}
60+
61+
func (d ESConstructorData) WrapperFunctionsToGenerate() iter.Seq[ESOperation] {
62+
return func(yield func(ESOperation) bool) {
63+
for op := range d.WrapperFunctionsToInstall() {
64+
if !op.MethodCustomization.CustomImplementation && !yield(op) {
65+
return
66+
}
67+
}
68+
for _, a := range d.Attributes {
69+
if a.Getter != nil && !a.Getter.CustomImplementation {
70+
if !yield(*a.Getter) {
71+
return
72+
}
73+
}
74+
if a.Setter != nil && !a.Setter.CustomImplementation {
75+
if !yield(*a.Setter) {
76+
return
77+
}
78+
}
79+
}
80+
}
81+
}
82+
83+
func (d ESConstructorData) Name() string { return d.Spec.TypeName }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package model
2+
3+
import (
4+
"github.com/gost-dom/code-gen/script-wrappers/configuration"
5+
"github.com/gost-dom/webref/idl"
6+
)
7+
8+
type ESOperation struct {
9+
Name string
10+
NotImplemented bool
11+
RetType idl.RetType
12+
HasError bool
13+
CustomImplementation bool
14+
MethodCustomization configuration.ESMethodWrapper
15+
Arguments []ESOperationArgument
16+
}
17+
18+
func (o ESOperation) CallbackMethodName() string {
19+
return idl.SanitizeName(o.Name)
20+
}
21+
22+
func (op ESOperation) GetHasError() bool {
23+
return op.HasError
24+
}
25+
26+
func (op ESOperation) HasResult() bool {
27+
return op.RetType.IsDefined()
28+
}
29+
30+
func (o ESOperation) Encoder() string {
31+
if e := o.MethodCustomization.Encoder; e != "" {
32+
return e
33+
}
34+
converter := "to"
35+
if o.RetType.Nullable {
36+
converter += "Nullable"
37+
}
38+
converter += idlNameToGoName(o.RetType.TypeName)
39+
return converter
40+
}

0 commit comments

Comments
 (0)