konsoletyper / teavm

Compiles Java bytecode to JavaScript, WebAssembly and C
https://teavm.org
Apache License 2.0
2.55k stars 260 forks source link

Regression with svg elements #916

Closed Ihromant closed 1 month ago

Ihromant commented 1 month ago

After some recent changes new issue appeared:

HTMLElement svg = HTMLDocument.current().createElementNS("http://www.w3.org/2000/svg", "svg").cast();
        svg.setAttribute("width", "10");
        HTMLDocument.current().appendChild(svg);

fails with CCE. It compiles to

var$2 = otj_JSObject_cast$static((otjdh_HTMLDocument_current()).createElementNS("http://www.w3.org/2000/svg", "svg"));
    var$3 = $rt_instanceOfOrNull(var$2, HTMLElement);
    $svg = $rt_throwCCEIfFalse(var$3, var$2);
    $svg.setAttribute("width", "10");
    (otjdh_HTMLDocument_current()).appendChild($svg);

It fails because svg is not instance of HTMLElement. I overcame it with

@JSBody(script = "return document.createElementNS('http://www.w3.org/2000/svg', 'svg');")
    private static native HTMLElement svg();

but earlier it worked well.

konsoletyper commented 1 month ago

This is expected behaviour. Unfortunately, previous versions of TeaVM did not behave correctly with run-time type checking in case of JSO, so you did not identify this issue on your side. The proper way would be to upcast HTMLDocument to just Document and then use it to create SVG elements. I know, standard does not re-define return value of createElement to return HTMLElement, but it looked convenient and assuming most users would work with HTML DOM and removing override to createElement is a breaking change, I won't remove it.