Skip to content

Commit 25373d4

Browse files
committed
refactor(codegen): Wrapper method gen control flow
This also moves the generated log statements to the top of the generated functions; where they really should have been all along.
1 parent 24c3ed0 commit 25373d4

22 files changed

+145
-128
lines changed

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

+1-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/dave/jennifer/jen"
77
. "github.com/gost-dom/code-gen/internal"
8-
"github.com/gost-dom/code-gen/packagenames"
98
"github.com/gost-dom/generators"
109
g "github.com/gost-dom/generators"
1110
)
@@ -120,7 +119,7 @@ func (gen GojaTargetGenerators) CreatePrototypeInitializerBody(
120119
return body
121120
}
122121

123-
func (gen GojaTargetGenerators) ReturnError(errGen g.Generator) g.Generator {
122+
func (gen GojaTargetGenerators) ReturnErrMsg(errGen g.Generator) g.Generator {
124123
return g.Raw(jen.Panic(errGen.Generate()))
125124
}
126125

@@ -129,15 +128,6 @@ func (gen GojaTargetGenerators) CreateMethodCallbackBody(
129128
op ESOperation,
130129
) g.Generator {
131130
callArgument := g.Id("c")
132-
if op.NotImplemented {
133-
msg := fmt.Sprintf(
134-
"%s.%s: Not implemented. Create an issue: %s",
135-
data.Name(),
136-
op.Name,
137-
packagenames.ISSUE_URL,
138-
)
139-
return g.Raw(jen.Panic(jen.Lit(msg)))
140-
}
141131
naming := GojaNamingStrategy{data}
142132
receiver := g.NewValue(naming.ReceiverName())
143133
instance := g.NewValue("instance")

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

+28-21
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/packagenames"
8+
"github.com/gost-dom/code-gen/stdgen"
89
"github.com/gost-dom/generators"
910
g "github.com/gost-dom/generators"
1011
)
@@ -47,7 +48,11 @@ type TargetGenerators interface {
4748

4849
CreateMethodCallbackBody(ESConstructorData, ESOperation) Generator
4950
WrapperStructGenerators() PlatformWrapperStructGenerators
50-
ReturnError(Generator) Generator
51+
52+
// Generate a return statement with an error messages. Goja is
53+
// non-idiomatic, and the return value is converted to a panic. V8go is
54+
// idiomatic, and this generates a return nil, errors.New(msg)
55+
ReturnErrMsg(Generator) Generator
5156
PlatformInfoArg() Generator
5257
}
5358

@@ -106,7 +111,7 @@ func (c MethodCallback) Generate() *jen.Statement {
106111
Type: typeGenerators.WrapperStructType(c.data.Name()),
107112
},
108113
Name: c.op.CallbackMethodName(),
109-
Args: typeGenerators.CallbackMethodArgs(), // generators.Arg(generators.Id("info"), v8FunctionCallbackInfoPtr),
114+
Args: typeGenerators.CallbackMethodArgs(),
110115
RtnTypes: typeGenerators.CallbackMethodRetTypes(),
111116
Body: MethodCallbackBody{c.data, c.op, receiver, c.platform},
112117
}.Generate()
@@ -119,24 +124,26 @@ type MethodCallbackBody struct {
119124
platform TargetGenerators
120125
}
121126

122-
func (c MethodCallbackBody) Generate() *jen.Statement {
123-
debug := g.NewValuePackage("Debug", packagenames.Log).Call(
124-
g.ValueOf(c.receiver).Field("logger").Call(c.platform.PlatformInfoArg()),
125-
g.Lit(fmt.Sprintf("V8 Function call: %s.%s", c.data.Name(), c.op.Name)))
126-
127-
if c.op.NotImplemented {
128-
errMsg := fmt.Sprintf(
129-
"%s.%s: Not implemented. Create an issue: %s",
130-
c.data.Name(),
131-
c.op.Name,
132-
packagenames.ISSUE_URL,
133-
)
134-
return g.StatementList(
135-
debug,
136-
c.platform.ReturnError(
137-
(g.Lit(errMsg)))).Generate()
138-
// jen.Qual("errors", "New").Call(jen.Lit(errMsg))))
139-
// g.Return(g.Nil, g.Raw(jen.Qual("errors", "New").Call(jen.Lit(errMsg))))).Generate()
127+
func (b MethodCallbackBody) Generate() (res *jen.Statement) {
128+
statements := g.StatementList()
129+
defer func() { res = statements.Generate() }()
130+
131+
statements.Append(stdgen.LogDebug(
132+
g.ValueOf(b.receiver).Field("logger").Call(b.platform.PlatformInfoArg()),
133+
g.Lit(fmt.Sprintf("V8 Function call: %s.%s", b.data.Name(), b.op.Name))))
134+
135+
if b.op.NotImplemented {
136+
statements.Append(b.ReturnNotImplementedError())
137+
return
140138
}
141-
return c.platform.CreateMethodCallbackBody(c.data, c.op).Generate()
139+
statements.Append(b.platform.CreateMethodCallbackBody(b.data, b.op))
140+
return
141+
}
142+
143+
func (b MethodCallbackBody) ReturnNotImplementedError() g.Generator {
144+
errMsg := fmt.Sprintf(
145+
"%s.%s: Not implemented. Create an issue: %s",
146+
b.data.Name(), b.op.Name, packagenames.ISSUE_URL,
147+
)
148+
return b.platform.ReturnErrMsg(g.Lit(errMsg))
142149
}

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

+1-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55

66
. "github.com/gost-dom/code-gen/internal"
7-
"github.com/gost-dom/code-gen/packagenames"
87
g "github.com/gost-dom/generators"
98

109
"github.com/dave/jennifer/jen"
@@ -48,9 +47,8 @@ func (gen V8TargetGenerators) CreateInitFunction(data ESConstructorData) g.Gener
4847
}
4948
}
5049

51-
func (gen V8TargetGenerators) ReturnError(errGen g.Generator) g.Generator {
50+
func (gen V8TargetGenerators) ReturnErrMsg(errGen g.Generator) g.Generator {
5251
return g.Return(g.Nil,
53-
// jen.Qual("errors", "New").Call(jen.Lit(errMsg))
5452
g.NewValuePackage("New", "errors").Call(errGen),
5553
)
5654
}
@@ -123,20 +121,6 @@ func (gen V8TargetGenerators) CreateMethodCallbackBody(
123121
op ESOperation,
124122
) JenGenerator {
125123
naming := V8NamingStrategy{data}
126-
debug := g.NewValuePackage("Debug", packagenames.Log).Call(
127-
g.NewValue(naming.Receiver()).Field("logger").Call(g.Id("info")),
128-
g.Lit(fmt.Sprintf("V8 Function call: %s.%s", data.Name(), op.Name)))
129-
// if op.NotImplemented {
130-
// errMsg := fmt.Sprintf(
131-
// "%s.%s: Not implemented. Create an issue: %s",
132-
// data.Name(),
133-
// op.Name,
134-
// packagenames.ISSUE_URL,
135-
// )
136-
// return g.StatementList(
137-
// debug,
138-
// g.Return(g.Nil, g.Raw(jen.Qual("errors", "New").Call(jen.Lit(errMsg)))))
139-
// }
140124
receiver := WrapperInstance{g.NewValue(naming.Receiver())}
141125
instance := g.NewValue("instance")
142126
readArgsResult := ReadArguments(data, op)
@@ -157,7 +141,6 @@ func (gen V8TargetGenerators) CreateMethodCallbackBody(
157141
return callInstance.Generator
158142
}
159143
statements := g.StatementList(
160-
debug,
161144
AssignArgs(data, op),
162145
GetInstanceAndError(instance, err, data),
163146
readArgsResult,
@@ -329,7 +312,6 @@ func V8RequireContext(wrapper WrapperInstance) g.Generator {
329312
return g.Assign(
330313
g.Id("ctx"),
331314
wrapper.MustGetContext(info),
332-
// wrapper.GetScriptHost().Method("mustGetContext").Call(info.GetV8Context()),
333315
)
334316
}
335317

internal/code-gen/stdgen/package.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Package stdgen contains "standard" code generators, i.e., not tied to a specific case.
2+
//
3+
// The name might be slightly misleading. "std" normally refers to Go's standard
4+
// library.
5+
package stdgen

internal/code-gen/stdgen/stdgen.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package stdgen
2+
3+
import (
4+
"github.com/gost-dom/code-gen/packagenames"
5+
g "github.com/gost-dom/generators"
6+
)
7+
8+
func LogDebug(args ...g.Generator) g.Generator {
9+
return g.NewValuePackage("Debug", packagenames.Log).Call(args...)
10+
}

scripting/gojahost/location_generated.go

+9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func (w locationWrapper) reload(c g.FunctionCall) g.Value {
5252
}
5353

5454
func (w locationWrapper) href(c g.FunctionCall) g.Value {
55+
log.Debug(w.logger(c), "V8 Function call: Location.href")
5556
instance := w.getInstance(c)
5657
result := instance.Href()
5758
return w.toUSVString(result)
@@ -63,12 +64,14 @@ func (w locationWrapper) setHref(c g.FunctionCall) g.Value {
6364
}
6465

6566
func (w locationWrapper) origin(c g.FunctionCall) g.Value {
67+
log.Debug(w.logger(c), "V8 Function call: Location.origin")
6668
instance := w.getInstance(c)
6769
result := instance.Origin()
6870
return w.toUSVString(result)
6971
}
7072

7173
func (w locationWrapper) protocol(c g.FunctionCall) g.Value {
74+
log.Debug(w.logger(c), "V8 Function call: Location.protocol")
7275
instance := w.getInstance(c)
7376
result := instance.Protocol()
7477
return w.toUSVString(result)
@@ -80,6 +83,7 @@ func (w locationWrapper) setProtocol(c g.FunctionCall) g.Value {
8083
}
8184

8285
func (w locationWrapper) host(c g.FunctionCall) g.Value {
86+
log.Debug(w.logger(c), "V8 Function call: Location.host")
8387
instance := w.getInstance(c)
8488
result := instance.Host()
8589
return w.toUSVString(result)
@@ -91,6 +95,7 @@ func (w locationWrapper) setHost(c g.FunctionCall) g.Value {
9195
}
9296

9397
func (w locationWrapper) hostname(c g.FunctionCall) g.Value {
98+
log.Debug(w.logger(c), "V8 Function call: Location.hostname")
9499
instance := w.getInstance(c)
95100
result := instance.Hostname()
96101
return w.toUSVString(result)
@@ -102,6 +107,7 @@ func (w locationWrapper) setHostname(c g.FunctionCall) g.Value {
102107
}
103108

104109
func (w locationWrapper) port(c g.FunctionCall) g.Value {
110+
log.Debug(w.logger(c), "V8 Function call: Location.port")
105111
instance := w.getInstance(c)
106112
result := instance.Port()
107113
return w.toUSVString(result)
@@ -113,6 +119,7 @@ func (w locationWrapper) setPort(c g.FunctionCall) g.Value {
113119
}
114120

115121
func (w locationWrapper) pathname(c g.FunctionCall) g.Value {
122+
log.Debug(w.logger(c), "V8 Function call: Location.pathname")
116123
instance := w.getInstance(c)
117124
result := instance.Pathname()
118125
return w.toUSVString(result)
@@ -124,6 +131,7 @@ func (w locationWrapper) setPathname(c g.FunctionCall) g.Value {
124131
}
125132

126133
func (w locationWrapper) search(c g.FunctionCall) g.Value {
134+
log.Debug(w.logger(c), "V8 Function call: Location.search")
127135
instance := w.getInstance(c)
128136
result := instance.Search()
129137
return w.toUSVString(result)
@@ -135,6 +143,7 @@ func (w locationWrapper) setSearch(c g.FunctionCall) g.Value {
135143
}
136144

137145
func (w locationWrapper) hash(c g.FunctionCall) g.Value {
146+
log.Debug(w.logger(c), "V8 Function call: Location.hash")
138147
instance := w.getInstance(c)
139148
result := instance.Hash()
140149
return w.toUSVString(result)

scripting/gojahost/node_generated.go

+14
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,39 @@ func (w nodeWrapper) initializePrototype(prototype *g.Object, vm *g.Runtime) {
4141
}
4242

4343
func (w nodeWrapper) getRootNode(c g.FunctionCall) g.Value {
44+
log.Debug(w.logger(c), "V8 Function call: Node.getRootNode")
4445
instance := w.getInstance(c)
4546
options := w.decodeGetRootNodeOptions(c.Arguments[0])
4647
result := instance.GetRootNode(options)
4748
return w.toNode(result)
4849
}
4950

5051
func (w nodeWrapper) cloneNode(c g.FunctionCall) g.Value {
52+
log.Debug(w.logger(c), "V8 Function call: Node.cloneNode")
5153
instance := w.getInstance(c)
5254
subtree := w.decodeboolean(c.Arguments[0])
5355
result := instance.CloneNode(subtree)
5456
return w.toNode(result)
5557
}
5658

5759
func (w nodeWrapper) isSameNode(c g.FunctionCall) g.Value {
60+
log.Debug(w.logger(c), "V8 Function call: Node.isSameNode")
5861
instance := w.getInstance(c)
5962
otherNode := w.decodeNode(c.Arguments[0])
6063
result := instance.IsSameNode(otherNode)
6164
return w.toBoolean(result)
6265
}
6366

6467
func (w nodeWrapper) contains(c g.FunctionCall) g.Value {
68+
log.Debug(w.logger(c), "V8 Function call: Node.contains")
6569
instance := w.getInstance(c)
6670
other := w.decodeNode(c.Arguments[0])
6771
result := instance.Contains(other)
6872
return w.toBoolean(result)
6973
}
7074

7175
func (w nodeWrapper) insertBefore(c g.FunctionCall) g.Value {
76+
log.Debug(w.logger(c), "V8 Function call: Node.insertBefore")
7277
instance := w.getInstance(c)
7378
node := w.decodeNode(c.Arguments[0])
7479
child := w.decodeNode(c.Arguments[1])
@@ -80,6 +85,7 @@ func (w nodeWrapper) insertBefore(c g.FunctionCall) g.Value {
8085
}
8186

8287
func (w nodeWrapper) appendChild(c g.FunctionCall) g.Value {
88+
log.Debug(w.logger(c), "V8 Function call: Node.appendChild")
8389
instance := w.getInstance(c)
8490
node := w.decodeNode(c.Arguments[0])
8591
result, err := instance.AppendChild(node)
@@ -90,6 +96,7 @@ func (w nodeWrapper) appendChild(c g.FunctionCall) g.Value {
9096
}
9197

9298
func (w nodeWrapper) removeChild(c g.FunctionCall) g.Value {
99+
log.Debug(w.logger(c), "V8 Function call: Node.removeChild")
93100
instance := w.getInstance(c)
94101
child := w.decodeNode(c.Arguments[0])
95102
result, err := instance.RemoveChild(child)
@@ -100,24 +107,28 @@ func (w nodeWrapper) removeChild(c g.FunctionCall) g.Value {
100107
}
101108

102109
func (w nodeWrapper) nodeName(c g.FunctionCall) g.Value {
110+
log.Debug(w.logger(c), "V8 Function call: Node.nodeName")
103111
instance := w.getInstance(c)
104112
result := instance.NodeName()
105113
return w.toDOMString(result)
106114
}
107115

108116
func (w nodeWrapper) isConnected(c g.FunctionCall) g.Value {
117+
log.Debug(w.logger(c), "V8 Function call: Node.isConnected")
109118
instance := w.getInstance(c)
110119
result := instance.IsConnected()
111120
return w.toBoolean(result)
112121
}
113122

114123
func (w nodeWrapper) ownerDocument(c g.FunctionCall) g.Value {
124+
log.Debug(w.logger(c), "V8 Function call: Node.ownerDocument")
115125
instance := w.getInstance(c)
116126
result := instance.OwnerDocument()
117127
return w.toDocument(result)
118128
}
119129

120130
func (w nodeWrapper) parentElement(c g.FunctionCall) g.Value {
131+
log.Debug(w.logger(c), "V8 Function call: Node.parentElement")
121132
instance := w.getInstance(c)
122133
result := instance.ParentElement()
123134
return w.toElement(result)
@@ -129,18 +140,21 @@ func (w nodeWrapper) childNodes(c g.FunctionCall) g.Value {
129140
}
130141

131142
func (w nodeWrapper) firstChild(c g.FunctionCall) g.Value {
143+
log.Debug(w.logger(c), "V8 Function call: Node.firstChild")
132144
instance := w.getInstance(c)
133145
result := instance.FirstChild()
134146
return w.toNode(result)
135147
}
136148

137149
func (w nodeWrapper) previousSibling(c g.FunctionCall) g.Value {
150+
log.Debug(w.logger(c), "V8 Function call: Node.previousSibling")
138151
instance := w.getInstance(c)
139152
result := instance.PreviousSibling()
140153
return w.toNode(result)
141154
}
142155

143156
func (w nodeWrapper) nextSibling(c g.FunctionCall) g.Value {
157+
log.Debug(w.logger(c), "V8 Function call: Node.nextSibling")
144158
instance := w.getInstance(c)
145159
result := instance.NextSibling()
146160
return w.toNode(result)

0 commit comments

Comments
 (0)