on master help sign in
Call mainFun even if window and document aren't defined
6db171cJosh Vera
src/IRTS/CodegenJavaScript.hs+25−10
@@ -296,6 +296,8 @@ jsInstanceOf = JSBinOp "instanceof"
296jsEq :: JS -> JS -> JS
296jsEq :: JS -> JS -> JS
297jsEq = JSBinOp "=="
297jsEq = JSBinOp "=="
298
298
299+jsNotEq :: JS -> JS -> JS
300+jsNotEq = JSBinOp "!="
299
301
300jsAnd :: JS -> JS -> JS
302jsAnd :: JS -> JS -> JS
301jsAnd = JSBinOp "&&"
303jsAnd = JSBinOp "&&"
@@ -1601,23 +1603,36 @@ codegenJS_all target definitions includes filename outputType = do
1601
1603
1602 mainLoop :: JS
1604 mainLoop :: JS
1603 mainLoop =
1605 mainLoop =
1604− JSAlloc "main" $ Just $ JSFunction [] (
1606+ JSAlloc "main" $ Just $ JSFunction [] (
1605− case target of
1607+ case target of
1606− Node -> mainFun
1608+ Node -> mainFun
1607− JavaScript -> JSCond [(isReady, mainFun), (JSTrue, jsMeth (JSIdent "window") "addEventListener" [
1609+ JavaScript -> JSCond [ (exists document `jsAnd` isReady, mainFun)
1608− JSString "DOMContentLoaded", JSFunction [] (
1610+ , (exists window, windowMainFun)
1609− mainFun
1611+ , (JSTrue, mainFun)
1610− ), JSFalse
1611− ])]
1612+ ]
1612 )
1613 )
1613 where
1614 where
1615+ exists :: JS -> JS
1616+ exists js = (JSPreOp "typeof " js) `jsNotEq` JSString "undefined"
1617+
1614 mainFun :: JS
1618 mainFun :: JS
1615 mainFun = jsTailcall $ jsCall runMain []
1619 mainFun = jsTailcall $ jsCall runMain []
1616
1620
1621+ window :: JS
1622+ window = JSIdent "window"
1617
1623
1618− isReady :: JS
1624+ document :: JS
1619− isReady = readyState `jsEq` JSString "complete" `jsOr` readyState `jsEq` JSString "loaded"
1625+ document = JSIdent "document"
1620
1626
1627+ windowMainFun :: JS
1628+ windowMainFun = jsMeth window "addEventListener" [
1629+ JSString "DOMContentLoaded"
1630+ , JSFunction [] ( mainFun )
1631+ , JSFalse
1632+ ]
1633+
1634+ isReady :: JS
1635+ isReady = JSParens $ readyState `jsEq` JSString "complete" `jsOr` readyState `jsEq` JSString "loaded"
1621
1636
1622 readyState :: JS
1637 readyState :: JS
1623 readyState = JSProj (JSIdent "document") "readyState"
1638 readyState = JSProj (JSIdent "document") "readyState"