on master help sign in
javascript: start javascript code when DOM is ready
commit46ab3691db2cc2e28c527c6fc8bd96c560a90a4e authorraichoo <raichoo@googlemail.com> dateMon 7 Oct 2013, 12:16 (13 years ago) parent7b4ca03
1 file changed+25−19
src/IRTS/CodegenJavaScript.hs+25−19
src/IRTS/CodegenJavaScript.hs+25−19
@@ -268,10 +268,11 @@ codegenJavaScript target definitions filename outputType = do
268268 path <- getDataDir
269269 idrRuntime <- readFile $ path ++ "/js/Runtime-common.js"
270270 tgtRuntime <- readFile $ concat [path, "/js/Runtime", runtime, ".js"]
271− writeFile filename $ intercalate "\n" $ [ header
272− , idrRuntime
273− , tgtRuntime
274− ] ++ functions ++ [compileJS mainLoop]
271+ writeFile filename $ header ++ (
272+ intercalate "\n" $ [ idrRuntime
273+ , tgtRuntime
274+ ] ++ functions ++ [mainLoop, invokeLoop]
275+ )
275276
276277 setPermissions filename (emptyPermissions { readable = True
277278 , executable = target == Node
@@ -282,23 +283,28 @@ codegenJavaScript target definitions filename outputType = do
282283 def = map (first translateNamespace) definitions
283284
284285 functions :: [String]
285− functions = map (compileJS . translateDeclaration) def
286−
287− mainLoop :: JS
288− mainLoop =
289− if target == Node
290− then JSSeq [ loop
291− , jsCall "main" []
292− ]
293− else loop
286+ functions = map (compileJS . optJS 1 . translateDeclaration) def
287+
288+ mainLoop :: String
289+ mainLoop = compileJS $
290+ JSAlloc "main" $ Just $ JSFunction [] (
291+ case target of
292+ Node -> mainFun
293+ JavaScript -> jsMeth (JSRaw "window") "addEventListener" [
294+ JSString "DOMContentLoaded", JSFunction [] (
295+ mainFun
296+ ), JSFalse
297+ ]
298+ )
294299 where
295− loop :: JS
296− loop = JSAlloc "main" $ Just $ JSFunction [] (
297− jsTailcall $ jsCall mainFun []
298− )
300+ mainFun :: JS
301+ mainFun = jsTailcall $ jsCall runMain []
302+
303+ runMain :: String
304+ runMain = idrNamespace ++ translateName (MN 0 "runMain")
299305
300− mainFun :: String
301− mainFun = idrNamespace ++ translateName (MN 0 "runMain")
306+ invokeLoop :: String
307+ invokeLoop = compileJS $ jsCall "main" []
302308
303309translateIdentifier :: String -> String
304310translateIdentifier =