dominikh / go-js-dom

MIT License
252 stars 42 forks source link

v2 does not compile with go1.14 #74

Closed inliquid closed 4 years ago

inliquid commented 4 years ago

When building a v2-based program with go1.14:

# honnef.co/go/js/dom/v2
..\..\..\..\..\..\pkg\mod\honnef.co\go\js\dom\v2@v2.0.0-20190526011328-ebc4cf92d81f\dom.go:121:7: invalid operation: o == js.Null() (struct containing [0]func() cannot be compared)
..\..\..\..\..\..\pkg\mod\honnef.co\go\js\dom\v2@v2.0.0-20190526011328-ebc4cf92d81f\dom.go:121:25: invalid operation: o == js.Undefined() (struct containing [0]func() cannot be compared)
..\..\..\..\..\..\pkg\mod\honnef.co\go\js\dom\v2@v2.0.0-20190526011328-ebc4cf92d81f\dom.go:144:27: invalid operation: n != js.Undefined() (struct containing [0]func() cannot be compared)
..\..\..\..\..\..\pkg\mod\honnef.co\go\js\dom\v2@v2.0.0-20190526011328-ebc4cf92d81f\dom.go:160:26: invalid operation: o.Get("constructor") == js.Global().Get("Array") (struct containing [0]func() cannot be compared)
..\..\..\..\..\..\pkg\mod\honnef.co\go\js\dom\v2@v2.0.0-20190526011328-ebc4cf92d81f\dom.go:218:2: cannot switch on elementConstructor(o) (type js.Value) (struct containing [0]func() cannot be compared)
..\..\..\..\..\..\pkg\mod\honnef.co\go\js\dom\v2@v2.0.0-20190526011328-ebc4cf92d81f\dom.go:227:2: cannot switch on elementConstructor(o) (type js.Value) (struct containing [0]func() cannot be compared)
..\..\..\..\..\..\pkg\mod\honnef.co\go\js\dom\v2@v2.0.0-20190526011328-ebc4cf92d81f\dom.go:235:7: invalid operation: o == js.Null() (struct containing [0]func() cannot be compared)
..\..\..\..\..\..\pkg\mod\honnef.co\go\js\dom\v2@v2.0.0-20190526011328-ebc4cf92d81f\dom.go:235:25: invalid operation: o == js.Undefined() (struct containing [0]func() cannot be compared)
..\..\..\..\..\..\pkg\mod\honnef.co\go\js\dom\v2@v2.0.0-20190526011328-ebc4cf92d81f\dom.go:238:2: cannot switch on elementConstructor(o) (type js.Value) (struct containing [0]func() cannot be compared)
..\..\..\..\..\..\pkg\mod\honnef.co\go\js\dom\v2@v2.0.0-20190526011328-ebc4cf92d81f\dom.go:248:7: invalid operation: o == js.Null() (struct containing [0]func() cannot be compared)
..\..\..\..\..\..\pkg\mod\honnef.co\go\js\dom\v2@v2.0.0-20190526011328-ebc4cf92d81f\dom.go:248:7: too many errors
dmitshur commented 4 years ago

Thanks for reporting.

This is due to syscall/js API changes in Go 1.14 (documented here). In order to fix this issue and preserve compatibility with 1.13, we must use build constraints. Either by making a copy of the entire dom.go file, or by creating relevant helpers.

dmitshur commented 4 years ago

/cc @neelance @cherrymui FYI, the Go 1.14 change to require use of Equal method to compare js.Value values has a noticeable effect on on large switches. E.g.:

switch c := o.Get("constructor"); c {
case js.Global().Get("AnimationEvent"):
    return &AnimationEvent{ev}
case js.Global().Get("AudioProcessingEvent"):
    return &AudioProcessingEvent{ev}
case js.Global().Get("BeforeInputEvent"):
    return &BeforeInputEvent{ev}
case js.Global().Get("BeforeUnloadEvent"):
    return &BeforeUnloadEvent{ev}
case js.Global().Get("BlobEvent"):
    return &BlobEvent{ev}
case js.Global().Get("ClipboardEvent"):
    return &ClipboardEvent{ev}
case js.Global().Get("CloseEvent"):
    return &CloseEvent{BasicEvent: ev}
case js.Global().Get("CompositionEvent"):
    return &CompositionEvent{ev}
case js.Global().Get("CSSFontFaceLoadEvent"):
    return &CSSFontFaceLoadEvent{ev}
case js.Global().Get("CustomEvent"):
    return &CustomEvent{ev}
...

Needs to be rewritten into something like:

switch c := o.Get("constructor"); {
case c.Equal(js.Global().Get("AnimationEvent"):
    return &AnimationEvent{ev}
case c.Equal(js.Global().Get("AudioProcessingEvent")):
    return &AudioProcessingEvent{ev}
case c.Equal(js.Global().Get("BeforeInputEvent")):
    return &BeforeInputEvent{ev}
case c.Equal(js.Global().Get("BeforeUnloadEvent")):
    return &BeforeUnloadEvent{ev}
case c.Equal(js.Global().Get("BlobEvent")):
    return &BlobEvent{ev}
case c.Equal(js.Global().Get("ClipboardEvent")):
    return &ClipboardEvent{ev}
case c.Equal(js.Global().Get("CloseEvent")):
    return &CloseEvent{BasicEvent: ev}
case c.Equal(js.Global().Get("CompositionEvent")):
    return &CompositionEvent{ev}
case c.Equal(js.Global().Get("CSSFontFaceLoadEvent")):
    return &CSSFontFaceLoadEvent{ev}
case c.Equal(js.Global().Get("CustomEvent")):
    return &CustomEvent{ev}
...

Just sharing this observation.