valderman / haste-compiler

A GHC-based Haskell to JavaScript compiler
http://haste-lang.org
BSD 3-Clause "New" or "Revised" License
1.45k stars 109 forks source link

Trying to bind click event results in TypeError: a is undefined - testcase.js:5:48 #328

Closed hikari-no-yume closed 9 years ago

hikari-no-yume commented 9 years ago
import Haste (alert)
import Haste.DOM (Elem, newElem, with, attr, (=:), addChild, documentBody)
import Haste.Events (MouseEvent(Click), onEvent)

void :: IO a -> IO ()
void = (>>= (\_ -> return ()))

main :: IO ()
main = do
    button <- newElem "input" `with` [
            attr "type" =: "submit",
            attr "value" =: "Click me!"
        ]
    void $ button `onEvent` Click $ \_ -> alert "hello!"

    addChild button documentBody
<!doctype html>
<meta charset=utf-8>
<title>test</title>

<script src=testcase.js></script>
testcase.js: testcase.hs
    hastec testcase.hs --opt-minify
valderman commented 9 years ago

In Chromium 43 I don't get a TypeError but instead the bound action is performed immediately on page load, which is arguably even worse. It seems that the Closure compiler is optimizing away the unused RealWorld# argument that IO computations carry, which causes trouble when the eval/apply machinery tries to use arity info in function applications.

I'm not entirely convinced that Closure should really be doing that, but I'll look into a workaround.

Posted related issue to closure-compiler: https://github.com/google/closure-compiler/issues/1046

hikari-no-yume commented 9 years ago

Ah, I should've mentioned I was using Fx, that's where I was getting this behaviour. Odd that the two JS engines would behave differently here.

valderman commented 9 years ago

Patching Closure to not mangle Function.length turned out to be the simplest option. Running haste-boot --force --no-libs should fetch the new Closure binary without forcing a rebuild of any libraries.