Skip to content

Commit 9e4cd47

Browse files
committed
refactor(codegen): Add idl.Type return type
This is a better abstraction over types in IDL specs
1 parent d92b1f2 commit 9e4cd47

File tree

6 files changed

+44
-20
lines changed

6 files changed

+44
-20
lines changed

internal/code-gen/script-wrappers/configuration/wrapper_type_spec.go

-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package configuration
22

3-
import "github.com/gost-dom/webref/idl"
4-
53
// IdlInterfaceConfiguration contains information about how to generate
64
// prototype objects for an interface in the IDL.
75
//
86
// All classes will be generated using a set of defaults. Data in this structure
97
// will allow deviating from the defaults.
108
type IdlInterfaceConfiguration struct {
119
DomSpec *WebIdlConfiguration
12-
IdlInterface idl.Interface
1310
TypeName string
1411
RunCustomCode bool
1512
WrapperType string
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package wrappers
2+
3+
import "github.com/gost-dom/webref/idl"
4+
5+
var IdlTypeUndefined = idl.Type{Name: "undefined", Kind: idl.KindSimple}

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

+29-11
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,59 @@ func createData(
2121
if !ok {
2222
panic("Missing type")
2323
}
24+
// if interfaceConfig.IdlInterface.Name != interfaceConfig.TypeName {
25+
// panic(
26+
// fmt.Sprintf(
27+
// "Type mismatch, %s = %s",
28+
// interfaceConfig.IdlInterface.Name,
29+
// interfaceConfig.TypeName,
30+
// ),
31+
// )
32+
// }
2433
idlInterface := idlName.IdlInterface
2534
wrappedTypeName := idlInterface.Name
35+
if idlInterface.Name != interfaceConfig.TypeName {
36+
panic(fmt.Sprintf("createData error: %s = %s", idlInterface.Name, interfaceConfig.TypeName))
37+
}
2638
return ESConstructorData{
2739
Spec: interfaceConfig,
2840
IdlInterfaceName: wrappedTypeName,
2941
RunCustomCode: interfaceConfig.RunCustomCode,
3042
Inheritance: idlInterface.Inheritance,
3143
IdlInterface: idlInterface,
32-
Constructor: CreateConstructor(interfaceConfig, idlName),
33-
Operations: CreateInstanceMethods(interfaceConfig, idlName),
34-
Attributes: CreateAttributes(interfaceConfig, idlName),
44+
Constructor: CreateConstructor(idlInterface, interfaceConfig, idlName),
45+
Operations: CreateInstanceMethods(idlInterface, interfaceConfig, idlName),
46+
Attributes: CreateAttributes(idlInterface, interfaceConfig, idlName),
3547
}
3648
}
3749

3850
func CreateConstructor(
51+
idlInterface idl.Interface,
3952
interfaceConfig *configuration.IdlInterfaceConfiguration,
4053
idlName idl.TypeSpec) *ESOperation {
4154
if c, ok := idlName.Constructor(); ok {
4255
fmt.Printf("Create constructor %s '%s'\n", interfaceConfig.TypeName, c.Name)
4356
c.Name = "constructor"
44-
result := createOperation(interfaceConfig, c)
57+
result := createOperation(idlInterface, interfaceConfig, c)
4558
return &result
4659
} else {
4760
return nil
4861
}
4962
}
5063

5164
func CreateInstanceMethods(
65+
idlInterface idl.Interface,
5266
interfaceConfig *configuration.IdlInterfaceConfiguration,
5367
idlName idl.TypeSpec) (result []ESOperation) {
5468
for instanceMethod := range idlName.InstanceMethods() {
55-
op := createOperation(interfaceConfig, instanceMethod)
69+
op := createOperation(idlInterface, interfaceConfig, instanceMethod)
5670
result = append(result, op)
5771
}
5872
return
5973
}
6074

6175
func CreateAttributes(
76+
idlInterface idl.Interface,
6277
interfaceConfig *configuration.IdlInterfaceConfiguration,
6378
idlName idl.TypeSpec,
6479
) (res []ESAttribute) {
@@ -71,16 +86,15 @@ func CreateAttributes(
7186
getter *ESOperation
7287
setter *ESOperation
7388
)
74-
// r := attribute.AttributeType()
75-
// rtnType := r.TypeName
7689
getter = &ESOperation{
7790
Name: attribute.Name,
7891
NotImplemented: methodCustomization.NotImplemented,
7992
CustomImplementation: methodCustomization.CustomImplementation,
80-
RetType: idl.RetType{
93+
LegacyRetType: idl.RetType{
8194
TypeName: attribute.Type.Name,
8295
Nullable: attribute.Type.Nullable,
8396
},
97+
RetType: attribute.Type,
8498
MethodCustomization: methodCustomization,
8599
}
86100
if !attribute.Readonly {
@@ -91,7 +105,8 @@ func CreateAttributes(
91105
setter.NotImplemented = setter.NotImplemented || methodCustomization.NotImplemented
92106
setter.CustomImplementation = setter.CustomImplementation ||
93107
methodCustomization.CustomImplementation
94-
setter.RetType = idl.NewRetTypeUndefined()
108+
setter.LegacyRetType = idl.NewRetTypeUndefined()
109+
setter.RetType = IdlTypeUndefined
95110
setter.Arguments = []ESOperationArgument{{
96111
Name: "val",
97112
Type: IdlNameToGoName(attribute.Type.Name),
@@ -109,19 +124,22 @@ func CreateAttributes(
109124
}
110125

111126
func createOperation(
127+
idlInterface idl.Interface,
112128
typeSpec *configuration.IdlInterfaceConfiguration,
113129
member idl.MemberSpec,
114130
) ESOperation {
115131
specRules := customrules.GetSpecRules(typeSpec.DomSpec.Name)
116132
intfRules := specRules[typeSpec.TypeName]
117133
opRules := intfRules.Operations[member.Name]
118-
119134
methodCustomization := typeSpec.GetMethodCustomization(member.Name)
135+
idlOperation, _ := idlInterface.GetOperation(member.Name)
136+
120137
op := ESOperation{
121138
Name: member.Name,
122139
NotImplemented: methodCustomization.NotImplemented,
123140
CustomImplementation: methodCustomization.CustomImplementation,
124-
RetType: member.ReturnType(),
141+
LegacyRetType: member.ReturnType(),
142+
RetType: idlOperation.ReturnType,
125143
MethodCustomization: methodCustomization,
126144
HasError: opRules.HasError,
127145
Arguments: []ESOperationArgument{},

internal/code-gen/script-wrappers/model/es_operation.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
type ESOperation struct {
99
Name string
1010
NotImplemented bool
11-
RetType idl.RetType
11+
LegacyRetType idl.RetType
12+
RetType idl.Type
1213
HasError bool
1314
CustomImplementation bool
1415
MethodCustomization configuration.ESMethodWrapper
@@ -24,21 +25,21 @@ func (op ESOperation) GetHasError() bool {
2425
}
2526

2627
func (op ESOperation) HasResult() bool {
27-
return op.RetType.IsDefined()
28+
return op.RetType.Name != "undefined"
2829
}
2930

3031
func (o ESOperation) Encoder() string {
3132
if e := o.MethodCustomization.Encoder; e != "" {
3233
return e
3334
}
3435
converter := "to"
35-
if o.RetType.Nullable {
36+
if o.LegacyRetType.Nullable {
3637
converter += "Nullable"
3738
}
38-
converter += IdlNameToGoName(o.RetType.TypeName)
39+
converter += IdlNameToGoName(o.LegacyRetType.TypeName)
3940
return converter
4041
}
4142

4243
func (o ESOperation) RetTypeName() string {
43-
return IdlNameToGoName(o.RetType.TypeName)
44+
return IdlNameToGoName(o.LegacyRetType.TypeName)
4445
}

internal/code-gen/script-wrappers/model/functions.go

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"strings"
55

66
. "github.com/gost-dom/code-gen/internal"
7+
"github.com/gost-dom/webref/idl"
78
)
89

910
func IdlNameToGoName(s string) string {
@@ -13,3 +14,5 @@ func IdlNameToGoName(s string) string {
1314
}
1415
return strings.Join(words, "")
1516
}
17+
18+
func IsDefined(t idl.Type) bool { return t.Name != "undefined" }

internal/code-gen/script-wrappers/v8gen/generators.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ func (c V8InstanceInvocation) GetGenerator() V8InstanceInvocationResult {
374374
list.Append(g.Return(g.Nil, g.Nil))
375375
}
376376
} else {
377-
retType := c.Op.RetType
377+
retType := c.Op.LegacyRetType
378378
if retType.IsNode() {
379379
genRes.RequireContext = true
380380
valueReturn := (g.Return(g.Raw(jen.Id("ctx").Dot("getInstanceForNode").Call(jen.Id("result")))))

0 commit comments

Comments
 (0)