on master help sign in
javascript: implement %lib directive for node
9d3822draichoo
src/IRTS/CodegenJavaScript.hs+19−6
@@ -338,21 +338,24 @@ compileJS' indent (JSWord word)
338 | JSWord64 b <- word = idrRTNamespace ++ "bigInt(\"" ++ show b ++ "\")"
338 | JSWord64 b <- word = idrRTNamespace ++ "bigInt(\"" ++ show b ++ "\")"
339
339
340codegenJavaScript :: CodeGenerator
340codegenJavaScript :: CodeGenerator
341+codegenJavaScript ci =
341−codegenJavaScript ci = codegenJS_all JavaScript (simpleDecls ci)
342+ codegenJS_all JavaScript (simpleDecls ci)
342− (includes ci) (outputFile ci) (outputType ci)
343+ (includes ci) [] (outputFile ci) (outputType ci)
343
344
344codegenNode :: CodeGenerator
345codegenNode :: CodeGenerator
346+codegenNode ci =
345−codegenNode ci = codegenJS_all Node (simpleDecls ci)
347+ codegenJS_all Node (simpleDecls ci)
346− (includes ci) (outputFile ci) (outputType ci)
348+ (includes ci) (compileLibs ci) (outputFile ci) (outputType ci)
347
349
348codegenJS_all
350codegenJS_all
349 :: JSTarget
351 :: JSTarget
350 -> [(Name, SDecl)]
352 -> [(Name, SDecl)]
351 -> [FilePath]
353 -> [FilePath]
354+ -> [String]
352 -> FilePath
355 -> FilePath
353 -> OutputType
356 -> OutputType
354 -> IO ()
357 -> IO ()
355−codegenJS_all target definitions includes filename outputType = do
358+codegenJS_all target definitions includes libs filename outputType = do
356 let bytecode = map toBC definitions
359 let bytecode = map toBC definitions
357 let info = initCompileInfo bytecode
360 let info = initCompileInfo bytecode
358 let js = concatMap (translateDecl info) bytecode
361 let js = concatMap (translateDecl info) bytecode
@@ -366,13 +369,23 @@ codegenJS_all target definitions includes filename outputType = do
366 idrRuntime <- readFile $ path ++ "Runtime-common.js"
369 idrRuntime <- readFile $ path ++ "Runtime-common.js"
367 tgtRuntime <- readFile $ concat [path, "Runtime", rt, ".js"]
370 tgtRuntime <- readFile $ concat [path, "Runtime", rt, ".js"]
368 jsbn <- readFile $ path ++ "jsbn/jsbn.js"
371 jsbn <- readFile $ path ++ "jsbn/jsbn.js"
369− let runtime = header ++ included ++ jsbn ++ idrRuntime ++ tgtRuntime
372+ let runtime = ( header
373+ ++ includeLibs libs
374+ ++ included
375+ ++ jsbn
376+ ++ idrRuntime
377+ ++ tgtRuntime
378+ )
370 writeFile filename $ runtime ++ concat code ++ main ++ invokeMain
379 writeFile filename $ runtime ++ concat code ++ main ++ invokeMain
371 setPermissions filename (emptyPermissions { readable = True
380 setPermissions filename (emptyPermissions { readable = True
372 , executable = target == Node
381 , executable = target == Node
373 , writable = True
382 , writable = True
374 })
383 })
375 where
384 where
385+ includeLibs :: [String] -> String
386+ includeLibs =
387+ concatMap (\lib -> "var " ++ lib ++ " = require(\"" ++ lib ++"\");\n")
388+
376 getIncludes :: [FilePath] -> IO [String]
389 getIncludes :: [FilePath] -> IO [String]
377 getIncludes = mapM readFile
390 getIncludes = mapM readFile
378
391