Skip to content

Commit 5993d80

Browse files
committed
refactor(codegen): Move flow control of methods
1 parent 927b417 commit 5993d80

File tree

4 files changed

+44
-43
lines changed

4 files changed

+44
-43
lines changed

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

+9-20
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,6 @@ func (gen GojaTargetGenerators) CreatePrototypeInitializerBody(
118118
return body
119119
}
120120

121-
func (gen GojaTargetGenerators) CreateMethodCallback(
122-
data ESConstructorData,
123-
op ESOperation,
124-
) g.Generator {
125-
naming := GojaNamingStrategy{data}
126-
callArgument := g.Id("c")
127-
return g.StatementList(
128-
g.Line,
129-
g.FunctionDefinition{
130-
Receiver: g.FunctionArgument{
131-
Name: g.Id(naming.ReceiverName()),
132-
Type: g.Id(naming.PrototypeWrapperTypeName()),
133-
},
134-
Name: op.CallbackMethodName(),
135-
Args: g.Arg(callArgument, gojaFc),
136-
RtnTypes: g.List(gojaValue),
137-
Body: gen.CreateMethodCallbackBody(data, op),
138-
})
139-
}
140-
141121
func (gen GojaTargetGenerators) CreateMethodCallbackBody(
142122
data ESConstructorData,
143123
op ESOperation,
@@ -232,6 +212,15 @@ func (g GojaTargetGenerators) HostType() Generator {
232212
return generators.NewType("GojaContext").Pointer()
233213
}
234214

215+
func (g GojaTargetGenerators) CallbackMethodArgs() generators.FunctionArgumentList {
216+
callArgument := generators.Id("c")
217+
return generators.Arg(callArgument, gojaFc)
218+
}
219+
220+
func (g GojaTargetGenerators) CallbackMethodRetTypes() []generators.Generator {
221+
return []generators.Generator{gojaValue}
222+
}
223+
235224
func panicOnNotNil(lhs g.Generator) g.Generator {
236225
return g.IfStmt{
237226
Condition: g.Neq{Lhs: lhs, Rhs: g.Nil},

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

+27-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type PlatformWrapperStructGenerators interface {
1616
WrapperStructConstructorRetType(interfaceName string) Generator
1717
EmbeddedType(wrappedType Generator) Generator
1818
EmbeddedTypeConstructor(wrappedType Generator) generators.Value
19+
CallbackMethodArgs() generators.FunctionArgumentList
20+
CallbackMethodRetTypes() []generators.Generator
1921
HostArg() Generator
2022
HostType() Generator
2123
}
@@ -38,10 +40,8 @@ type TargetGenerators interface {
3840
// CreateConstructorCallback generates the function to be called whan
3941
// JavaScript code constructs an instance.
4042
CreateConstructorCallback(ESConstructorData) Generator
41-
// CreateMethodCallback generates the function to be called when
42-
// JavaScript code calls a method on an instance.
43-
CreateMethodCallback(ESConstructorData, ESOperation) Generator
4443

44+
CreateMethodCallbackBody(ESConstructorData, ESOperation) Generator
4545
WrapperStructGenerators() PlatformWrapperStructGenerators
4646
}
4747

@@ -77,7 +77,30 @@ func (g PrototypeWrapperGenerator) Generate() *jen.Statement {
7777
func (g PrototypeWrapperGenerator) MethodCallbacks(data ESConstructorData) Generator {
7878
list := generators.StatementList()
7979
for op := range data.WrapperFunctionsToGenerate() {
80-
list.Append(g.Platform.CreateMethodCallback(data, op))
80+
list.Append(
81+
generators.Line,
82+
MethodCallback{data, op, g.Platform},
83+
)
8184
}
8285
return list
8386
}
87+
88+
type MethodCallback struct {
89+
data ESConstructorData
90+
op ESOperation
91+
platform TargetGenerators
92+
}
93+
94+
func (c MethodCallback) Generate() *jen.Statement {
95+
typeGenerators := c.platform.WrapperStructGenerators()
96+
return generators.FunctionDefinition{
97+
Receiver: generators.FunctionArgument{
98+
Name: generators.Id("w"),
99+
Type: typeGenerators.WrapperStructType(c.data.Name()),
100+
},
101+
Name: c.op.CallbackMethodName(),
102+
Args: typeGenerators.CallbackMethodArgs(), // generators.Arg(generators.Id("info"), v8FunctionCallbackInfoPtr),
103+
RtnTypes: typeGenerators.CallbackMethodRetTypes(),
104+
Body: c.platform.CreateMethodCallbackBody(c.data, c.op),
105+
}.Generate()
106+
}

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

-19
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,6 @@ func (gen V8TargetGenerators) CreateConstructorCallback(data ESConstructorData)
109109
)
110110
}
111111

112-
func (gen V8TargetGenerators) CreateMethodCallback(
113-
data ESConstructorData,
114-
op ESOperation,
115-
) JenGenerator {
116-
naming := V8NamingStrategy{data}
117-
return g.StatementList(
118-
g.Line,
119-
g.FunctionDefinition{
120-
Receiver: g.FunctionArgument{
121-
Name: g.Id(naming.Receiver()),
122-
Type: g.Id(naming.PrototypeWrapperName()),
123-
},
124-
Name: op.CallbackMethodName(),
125-
Args: g.Arg(g.Id("info"), v8FunctionCallbackInfoPtr),
126-
RtnTypes: g.List(v8Value, g.Id("error")),
127-
Body: gen.CreateMethodCallbackBody(data, op),
128-
})
129-
}
130-
131112
func (gen V8TargetGenerators) CreateMethodCallbackBody(
132113
data ESConstructorData,
133114
op ESOperation,

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

+8
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,11 @@ func (g V8WrapperStructGenerators) HostArg() Generator {
3838
func (g V8WrapperStructGenerators) HostType() Generator {
3939
return scriptHostPtr
4040
}
41+
42+
func (g V8WrapperStructGenerators) CallbackMethodArgs() generators.FunctionArgumentList {
43+
return generators.Arg(generators.Id("info"), v8FunctionCallbackInfoPtr)
44+
}
45+
46+
func (g V8WrapperStructGenerators) CallbackMethodRetTypes() []generators.Generator {
47+
return generators.List(v8Value, generators.Id("error"))
48+
}

0 commit comments

Comments
 (0)