on master help sign in
javascript: start javascript code when DOM is ready
46ab369raichoo
src/IRTS/CodegenJavaScript.hs+25−19
@@ -268,10 +268,11 @@ codegenJavaScript target definitions filename outputType = do
268 path <- getDataDir
268 path <- getDataDir
269 idrRuntime <- readFile $ path ++ "/js/Runtime-common.js"
269 idrRuntime <- readFile $ path ++ "/js/Runtime-common.js"
270 tgtRuntime <- readFile $ concat [path, "/js/Runtime", runtime, ".js"]
270 tgtRuntime <- readFile $ concat [path, "/js/Runtime", runtime, ".js"]
271− writeFile filename $ intercalate "\n" $ [ header
271+ writeFile filename $ header ++ (
272− , idrRuntime
272+ intercalate "\n" $ [ idrRuntime
273− , tgtRuntime
273+ , tgtRuntime
274− ] ++ functions ++ [compileJS mainLoop]
274+ ] ++ functions ++ [mainLoop, invokeLoop]
275+ )
275
276
276 setPermissions filename (emptyPermissions { readable = True
277 setPermissions filename (emptyPermissions { readable = True
277 , executable = target == Node
278 , executable = target == Node
@@ -282,23 +283,28 @@ codegenJavaScript target definitions filename outputType = do
282 def = map (first translateNamespace) definitions
283 def = map (first translateNamespace) definitions
283
284
284 functions :: [String]
285 functions :: [String]
285− functions = map (compileJS . translateDeclaration) def
286+ functions = map (compileJS . optJS 1 . translateDeclaration) def
286−
287+
287− mainLoop :: JS
288+ mainLoop :: String
288− mainLoop =
289+ mainLoop = compileJS $
290+ JSAlloc "main" $ Just $ JSFunction [] (
289− if target == Node
291+ case target of
290− then JSSeq [ loop
292+ Node -> mainFun
291− , jsCall "main" []
293+ JavaScript -> jsMeth (JSRaw "window") "addEventListener" [
294+ JSString "DOMContentLoaded", JSFunction [] (
295+ mainFun
296+ ), JSFalse
292− ]
297+ ]
293− else loop
298+ )
294 where
299 where
295− loop :: JS
300+ mainFun :: JS
296− loop = JSAlloc "main" $ Just $ JSFunction [] (
297− jsTailcall $ jsCall mainFun []
301+ mainFun = jsTailcall $ jsCall runMain []
298− )
302+
303+ runMain :: String
304+ runMain = idrNamespace ++ translateName (MN 0 "runMain")
299
305
300− mainFun :: String
306+ invokeLoop :: String
301− mainFun = idrNamespace ++ translateName (MN 0 "runMain")
307+ invokeLoop = compileJS $ jsCall "main" []
302
308
303translateIdentifier :: String -> String
309translateIdentifier :: String -> String
304translateIdentifier =
310translateIdentifier =