Skip to content

Commit 24c3ed0

Browse files
committed
refactor(codegen): Extract wrapper method flow
This also unifies generated JS handler logging.
1 parent 5993d80 commit 24c3ed0

9 files changed

+105
-14
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ func (s GojaNamingStrategy) ReceiverName() string {
4040

4141
type GojaTargetGenerators struct{}
4242

43+
func (gen GojaTargetGenerators) PlatformInfoArg() g.Generator { return g.Id("c") }
44+
4345
// CreateConstructor has no effect for Goja. It's currently based on a system
4446
// that it automatically creates the constructors based on whether or not they
4547
// call the ingerface
@@ -118,6 +120,10 @@ func (gen GojaTargetGenerators) CreatePrototypeInitializerBody(
118120
return body
119121
}
120122

123+
func (gen GojaTargetGenerators) ReturnError(errGen g.Generator) g.Generator {
124+
return g.Raw(jen.Panic(errGen.Generate()))
125+
}
126+
121127
func (gen GojaTargetGenerators) CreateMethodCallbackBody(
122128
data ESConstructorData,
123129
op ESOperation,

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

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package wrappers
22

33
import (
4+
"fmt"
5+
46
"github.com/dave/jennifer/jen"
7+
"github.com/gost-dom/code-gen/packagenames"
58
"github.com/gost-dom/generators"
9+
g "github.com/gost-dom/generators"
610
)
711

812
type Generator = generators.Generator
@@ -43,6 +47,8 @@ type TargetGenerators interface {
4347

4448
CreateMethodCallbackBody(ESConstructorData, ESOperation) Generator
4549
WrapperStructGenerators() PlatformWrapperStructGenerators
50+
ReturnError(Generator) Generator
51+
PlatformInfoArg() Generator
4652
}
4753

4854
// PrototypeWrapperGenerator generates code to create a JavaScript prototype
@@ -93,14 +99,44 @@ type MethodCallback struct {
9399

94100
func (c MethodCallback) Generate() *jen.Statement {
95101
typeGenerators := c.platform.WrapperStructGenerators()
102+
receiver := generators.Id("w")
96103
return generators.FunctionDefinition{
97104
Receiver: generators.FunctionArgument{
98-
Name: generators.Id("w"),
105+
Name: receiver,
99106
Type: typeGenerators.WrapperStructType(c.data.Name()),
100107
},
101108
Name: c.op.CallbackMethodName(),
102109
Args: typeGenerators.CallbackMethodArgs(), // generators.Arg(generators.Id("info"), v8FunctionCallbackInfoPtr),
103110
RtnTypes: typeGenerators.CallbackMethodRetTypes(),
104-
Body: c.platform.CreateMethodCallbackBody(c.data, c.op),
111+
Body: MethodCallbackBody{c.data, c.op, receiver, c.platform},
105112
}.Generate()
106113
}
114+
115+
type MethodCallbackBody struct {
116+
data ESConstructorData
117+
op ESOperation
118+
receiver g.Generator
119+
platform TargetGenerators
120+
}
121+
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()
140+
}
141+
return c.platform.CreateMethodCallbackBody(c.data, c.op).Generate()
142+
}

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

+20-11
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func (t NewV8FunctionTemplate) Generate() *jen.Statement {
3636

3737
type V8TargetGenerators struct{}
3838

39+
func (gen V8TargetGenerators) PlatformInfoArg() Generator { return g.Id("info") }
40+
3941
func (gen V8TargetGenerators) CreateInitFunction(data ESConstructorData) g.Generator {
4042
return g.FunctionDefinition{
4143
Name: "init",
@@ -46,6 +48,13 @@ func (gen V8TargetGenerators) CreateInitFunction(data ESConstructorData) g.Gener
4648
}
4749
}
4850

51+
func (gen V8TargetGenerators) ReturnError(errGen g.Generator) g.Generator {
52+
return g.Return(g.Nil,
53+
// jen.Qual("errors", "New").Call(jen.Lit(errMsg))
54+
g.NewValuePackage("New", "errors").Call(errGen),
55+
)
56+
}
57+
4958
func (gen V8TargetGenerators) WrapperStructGenerators() PlatformWrapperStructGenerators {
5059
return V8WrapperStructGenerators{}
5160
}
@@ -117,17 +126,17 @@ func (gen V8TargetGenerators) CreateMethodCallbackBody(
117126
debug := g.NewValuePackage("Debug", packagenames.Log).Call(
118127
g.NewValue(naming.Receiver()).Field("logger").Call(g.Id("info")),
119128
g.Lit(fmt.Sprintf("V8 Function call: %s.%s", data.Name(), op.Name)))
120-
if op.NotImplemented {
121-
errMsg := fmt.Sprintf(
122-
"%s.%s: Not implemented. Create an issue: %s",
123-
data.Name(),
124-
op.Name,
125-
packagenames.ISSUE_URL,
126-
)
127-
return g.StatementList(
128-
debug,
129-
g.Return(g.Nil, g.Raw(jen.Qual("errors", "New").Call(jen.Lit(errMsg)))))
130-
}
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+
// }
131140
receiver := WrapperInstance{g.NewValue(naming.Receiver())}
132141
instance := g.NewValue("instance")
133142
readArgsResult := ReadArguments(data, op)

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

+2
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,5 @@ func (g WrapperStructGenerator) Body() Generator {
9292
wrapperType := structGens.WrapperStructType(idlInterfaceName)
9393
return generators.Return(wrapperType.CreateInstance(fieldInitializers...).Reference())
9494
}
95+
96+
func (g WrapperStructGenerator) PlatformInfoArg() Generator { return generators.Id("info") }

scripting/gojahost/base_instance_wrapper.go

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gojahost
22

33
import (
4+
"log/slog"
45
"strings"
56

67
"github.com/dop251/goja"
@@ -19,6 +20,10 @@ func (w baseInstanceWrapper[T]) vm() *goja.Runtime {
1920
return w.ctx.vm
2021
}
2122

23+
func (w baseInstanceWrapper[T]) logger(c g.FunctionCall) *slog.Logger {
24+
return w.ctx.window.Logger()
25+
}
26+
2227
func newBaseInstanceWrapper[T any](instance *GojaContext) baseInstanceWrapper[T] {
2328
return baseInstanceWrapper[T]{instance}
2429
}

scripting/gojahost/location_generated.go

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package gojahost
55
import (
66
g "github.com/dop251/goja"
77
html "github.com/gost-dom/browser/html"
8+
log "github.com/gost-dom/browser/internal/log"
89
)
910

1011
func init() {
@@ -36,14 +37,17 @@ func (w locationWrapper) initializePrototype(prototype *g.Object, vm *g.Runtime)
3637
}
3738

3839
func (w locationWrapper) assign(c g.FunctionCall) g.Value {
40+
log.Debug(w.logger(c), "V8 Function call: Location.assign")
3941
panic("Location.assign: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
4042
}
4143

4244
func (w locationWrapper) replace(c g.FunctionCall) g.Value {
45+
log.Debug(w.logger(c), "V8 Function call: Location.replace")
4346
panic("Location.replace: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
4447
}
4548

4649
func (w locationWrapper) reload(c g.FunctionCall) g.Value {
50+
log.Debug(w.logger(c), "V8 Function call: Location.reload")
4751
panic("Location.reload: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
4852
}
4953

@@ -54,6 +58,7 @@ func (w locationWrapper) href(c g.FunctionCall) g.Value {
5458
}
5559

5660
func (w locationWrapper) setHref(c g.FunctionCall) g.Value {
61+
log.Debug(w.logger(c), "V8 Function call: Location.setHref")
5762
panic("Location.setHref: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
5863
}
5964

@@ -70,6 +75,7 @@ func (w locationWrapper) protocol(c g.FunctionCall) g.Value {
7075
}
7176

7277
func (w locationWrapper) setProtocol(c g.FunctionCall) g.Value {
78+
log.Debug(w.logger(c), "V8 Function call: Location.setProtocol")
7379
panic("Location.setProtocol: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
7480
}
7581

@@ -80,6 +86,7 @@ func (w locationWrapper) host(c g.FunctionCall) g.Value {
8086
}
8187

8288
func (w locationWrapper) setHost(c g.FunctionCall) g.Value {
89+
log.Debug(w.logger(c), "V8 Function call: Location.setHost")
8390
panic("Location.setHost: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
8491
}
8592

@@ -90,6 +97,7 @@ func (w locationWrapper) hostname(c g.FunctionCall) g.Value {
9097
}
9198

9299
func (w locationWrapper) setHostname(c g.FunctionCall) g.Value {
100+
log.Debug(w.logger(c), "V8 Function call: Location.setHostname")
93101
panic("Location.setHostname: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
94102
}
95103

@@ -100,6 +108,7 @@ func (w locationWrapper) port(c g.FunctionCall) g.Value {
100108
}
101109

102110
func (w locationWrapper) setPort(c g.FunctionCall) g.Value {
111+
log.Debug(w.logger(c), "V8 Function call: Location.setPort")
103112
panic("Location.setPort: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
104113
}
105114

@@ -110,6 +119,7 @@ func (w locationWrapper) pathname(c g.FunctionCall) g.Value {
110119
}
111120

112121
func (w locationWrapper) setPathname(c g.FunctionCall) g.Value {
122+
log.Debug(w.logger(c), "V8 Function call: Location.setPathname")
113123
panic("Location.setPathname: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
114124
}
115125

@@ -120,6 +130,7 @@ func (w locationWrapper) search(c g.FunctionCall) g.Value {
120130
}
121131

122132
func (w locationWrapper) setSearch(c g.FunctionCall) g.Value {
133+
log.Debug(w.logger(c), "V8 Function call: Location.setSearch")
123134
panic("Location.setSearch: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
124135
}
125136

@@ -130,9 +141,11 @@ func (w locationWrapper) hash(c g.FunctionCall) g.Value {
130141
}
131142

132143
func (w locationWrapper) setHash(c g.FunctionCall) g.Value {
144+
log.Debug(w.logger(c), "V8 Function call: Location.setHash")
133145
panic("Location.setHash: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
134146
}
135147

136148
func (w locationWrapper) ancestorOrigins(c g.FunctionCall) g.Value {
149+
log.Debug(w.logger(c), "V8 Function call: Location.ancestorOrigins")
137150
panic("Location.ancestorOrigins: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
138151
}

scripting/gojahost/node_generated.go

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package gojahost
55
import (
66
g "github.com/dop251/goja"
77
dom "github.com/gost-dom/browser/dom"
8+
log "github.com/gost-dom/browser/internal/log"
89
)
910

1011
func init() {
@@ -123,6 +124,7 @@ func (w nodeWrapper) parentElement(c g.FunctionCall) g.Value {
123124
}
124125

125126
func (w nodeWrapper) childNodes(c g.FunctionCall) g.Value {
127+
log.Debug(w.logger(c), "V8 Function call: Node.childNodes")
126128
panic("Node.childNodes: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
127129
}
128130

scripting/gojahost/pointerevents4_generated.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
package gojahost
44

5-
import g "github.com/dop251/goja"
5+
import (
6+
g "github.com/dop251/goja"
7+
log "github.com/gost-dom/browser/internal/log"
8+
)
69

710
func init() {
811
installClass("PointerEvent", "MouseEvent", newPointerEventWrapper)
@@ -16,17 +19,21 @@ func (w pointerEventWrapper) initializePrototype(prototype *g.Object, vm *g.Runt
1619
}
1720

1821
func (w pointerEventWrapper) width(c g.FunctionCall) g.Value {
22+
log.Debug(w.logger(c), "V8 Function call: PointerEvent.width")
1923
panic("PointerEvent.width: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
2024
}
2125

2226
func (w pointerEventWrapper) height(c g.FunctionCall) g.Value {
27+
log.Debug(w.logger(c), "V8 Function call: PointerEvent.height")
2328
panic("PointerEvent.height: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
2429
}
2530

2631
func (w pointerEventWrapper) pressure(c g.FunctionCall) g.Value {
32+
log.Debug(w.logger(c), "V8 Function call: PointerEvent.pressure")
2733
panic("PointerEvent.pressure: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
2834
}
2935

3036
func (w pointerEventWrapper) tangentialPressure(c g.FunctionCall) g.Value {
37+
log.Debug(w.logger(c), "V8 Function call: PointerEvent.tangentialPressure")
3138
panic("PointerEvent.tangentialPressure: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
3239
}

scripting/gojahost/uievents_generated.go

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package gojahost
44

55
import (
66
g "github.com/dop251/goja"
7+
log "github.com/gost-dom/browser/internal/log"
78
uievents "github.com/gost-dom/browser/internal/uievents"
89
)
910

@@ -23,34 +24,42 @@ func (w mouseEventWrapper) initializePrototype(prototype *g.Object, vm *g.Runtim
2324
}
2425

2526
func (w mouseEventWrapper) getModifierState(c g.FunctionCall) g.Value {
27+
log.Debug(w.logger(c), "V8 Function call: MouseEvent.getModifierState")
2628
panic("MouseEvent.getModifierState: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
2729
}
2830

2931
func (w mouseEventWrapper) screenX(c g.FunctionCall) g.Value {
32+
log.Debug(w.logger(c), "V8 Function call: MouseEvent.screenX")
3033
panic("MouseEvent.screenX: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
3134
}
3235

3336
func (w mouseEventWrapper) screenY(c g.FunctionCall) g.Value {
37+
log.Debug(w.logger(c), "V8 Function call: MouseEvent.screenY")
3438
panic("MouseEvent.screenY: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
3539
}
3640

3741
func (w mouseEventWrapper) clientX(c g.FunctionCall) g.Value {
42+
log.Debug(w.logger(c), "V8 Function call: MouseEvent.clientX")
3843
panic("MouseEvent.clientX: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
3944
}
4045

4146
func (w mouseEventWrapper) clientY(c g.FunctionCall) g.Value {
47+
log.Debug(w.logger(c), "V8 Function call: MouseEvent.clientY")
4248
panic("MouseEvent.clientY: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
4349
}
4450

4551
func (w mouseEventWrapper) layerX(c g.FunctionCall) g.Value {
52+
log.Debug(w.logger(c), "V8 Function call: MouseEvent.layerX")
4653
panic("MouseEvent.layerX: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
4754
}
4855

4956
func (w mouseEventWrapper) layerY(c g.FunctionCall) g.Value {
57+
log.Debug(w.logger(c), "V8 Function call: MouseEvent.layerY")
5058
panic("MouseEvent.layerY: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
5159
}
5260

5361
func (w mouseEventWrapper) relatedTarget(c g.FunctionCall) g.Value {
62+
log.Debug(w.logger(c), "V8 Function call: MouseEvent.relatedTarget")
5463
panic("MouseEvent.relatedTarget: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
5564
}
5665

@@ -72,9 +81,11 @@ func (w uIEventWrapper) initializePrototype(prototype *g.Object, vm *g.Runtime)
7281
}
7382

7483
func (w uIEventWrapper) view(c g.FunctionCall) g.Value {
84+
log.Debug(w.logger(c), "V8 Function call: UIEvent.view")
7585
panic("UIEvent.view: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
7686
}
7787

7888
func (w uIEventWrapper) detail(c g.FunctionCall) g.Value {
89+
log.Debug(w.logger(c), "V8 Function call: UIEvent.detail")
7990
panic("UIEvent.detail: Not implemented. Create an issue: https://github.com/gost-dom/browser/issues")
8091
}

0 commit comments

Comments
 (0)