on master help sign in
Call mainFun even if window and document aren't defined
commit6db171c72ecc982ba9c7019d0194f63d0c692c00 authorJosh Vera <vera@github.com> dateSun 18 May 2014, 15:43 (12 years ago) parentd581842
1 file changed+25−10
src/IRTS/CodegenJavaScript.hs+25−10
src/IRTS/CodegenJavaScript.hs+25−10
@@ -296,6 +296,8 @@ jsInstanceOf = JSBinOp "instanceof"
296296jsEq :: JS -> JS -> JS
297297jsEq = JSBinOp "=="
298298
299+jsNotEq :: JS -> JS -> JS
300+jsNotEq = JSBinOp "!="
299301
300302jsAnd :: JS -> JS -> JS
301303jsAnd = JSBinOp "&&"
@@ -1601,23 +1603,36 @@ codegenJS_all target definitions includes filename outputType = do
16011603
16021604 mainLoop :: JS
16031605 mainLoop =
1604− JSAlloc "main" $ Just $ JSFunction [] (
1605− case target of
1606− Node -> mainFun
1607− JavaScript -> JSCond [(isReady, mainFun), (JSTrue, jsMeth (JSIdent "window") "addEventListener" [
1608− JSString "DOMContentLoaded", JSFunction [] (
1609− mainFun
1610− ), JSFalse
1611− ])]
1606+ JSAlloc "main" $ Just $ JSFunction [] (
1607+ case target of
1608+ Node -> mainFun
1609+ JavaScript -> JSCond [ (exists document `jsAnd` isReady, mainFun)
1610+ , (exists window, windowMainFun)
1611+ , (JSTrue, mainFun)
1612+ ]
16121613 )
16131614 where
1615+ exists :: JS -> JS
1616+ exists js = (JSPreOp "typeof " js) `jsNotEq` JSString "undefined"
1617+
16141618 mainFun :: JS
16151619 mainFun = jsTailcall $ jsCall runMain []
16161620
1621+ window :: JS
1622+ window = JSIdent "window"
16171623
1618− isReady :: JS
1619− isReady = readyState `jsEq` JSString "complete" `jsOr` readyState `jsEq` JSString "loaded"
1624+ document :: JS
1625+ document = JSIdent "document"
16201626
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"
16211636
16221637 readyState :: JS
16231638 readyState = JSProj (JSIdent "document") "readyState"