Permalink
Browse files

--no-exports flag; arguments can be specified in any order

  • Loading branch information...
1 parent e11a0f2 commit f0d5aaff4b782c47626c7c0b30ee7da7824573eb Arjun Guha committed Sep 15, 2009
Showing with 23 additions and 10 deletions.
  1. +11 −7 src/BrownPLT/JavaScript/Contracts/Compiler.hs
  2. +12 −3 src/Jscc.hs
View
18 src/BrownPLT/JavaScript/Contracts/Compiler.hs
@@ -187,10 +187,11 @@ libraryHeader =
compileRelease :: String -- ^implementation
-> String -- ^implementation source
-> String -- ^contract library
+ -> Bool -- ^export?
-> [InterfaceItem] -- ^the interface
-> Maybe String -- ^the namespace name
-> String -- ^encapsulated implementation
-compileRelease rawImpl implSource boilerplate interface namespace =
+compileRelease rawImpl implSource boilerplate isExport interface namespace =
libraryHeader ++ (renderStatements $ escapeGlobals impl exportNames)
++ rawImpl ++ exposeStatements ++ "\n}).apply(impl,[]);\n"
++ exportStatements ++ namespaceStatements ++ "\n})();" where
@@ -199,8 +200,9 @@ compileRelease rawImpl implSource boilerplate interface namespace =
Right (Script _ stmts) -> stmts
exports = filter isInterfaceExport interface
instances = filter isInterfaceInstance interface
- exportStatements =
- renderStatements (map exportRelease $ exports ++ instances)
+ exportStatements = case isExport of
+ True -> renderStatements (map exportRelease $ exports ++ instances)
+ False -> ""
exportNames = [n | InterfaceExport n _ _ <- exports ]
instanceNames = [n | InterfaceInstance n _ _ <- instances]
exposeStatements = renderStatements
@@ -212,9 +214,10 @@ compileRelease rawImpl implSource boilerplate interface namespace =
compileFormatted :: String -- ^implementation
-> String -- ^implementation source
-> String -- ^contract library
+ -> Bool -- ^export?
-> [InterfaceItem] -- ^the interface
-> String -- ^encapsulated implementation
-compileFormatted rawImpl implSource boilerplate interface =
+compileFormatted rawImpl implSource boilerplate isExport interface =
libraryHeader ++ (renderStatements $ escapeGlobals impl exportNames)
++ rawImpl
++ exposeStatements ++ "\n}).apply(impl,[]);\n" ++ boilerplate
@@ -224,10 +227,11 @@ compileFormatted rawImpl implSource boilerplate interface =
impl = case parseScriptFromString implSource rawImpl of
Left err -> error (show err)
Right (Script _ stmts) -> stmts
- exports = filter isInterfaceExport interface
+ exports =filter isInterfaceExport interface
instances = filter isInterfaceInstance interface
- exportStatements =
- renderStatements (concatMap makeExportStatements interface)
+ exportStatements = case isExport of
+ True -> renderStatements (concatMap makeExportStatements interface)
+ False -> ""
exportNames = [n | InterfaceExport n _ _ <- exports ]
aliases = filter isInterfaceAlias interface
aliasStatements = renderStatements (compileAliases interface)
View
15 src/Jscc.hs
@@ -10,13 +10,15 @@ import Control.Monad
import BrownPLT.JavaScript.Contracts
import Paths_JsContracts -- created by Cabal
import BrownPLT.JavaScript.Parser (parseJavaScriptFromFile)
+import Data.List
data Flag
= Help
| Release
| Debug
| Namespace String
| Interface String
+ | NoExport
deriving (Eq,Ord,Show)
@@ -30,6 +32,8 @@ options =
"enable contracts and encapsulate (default)"
, Option ['n'] ["namespace"] (ReqArg Namespace "NAMESPACE")
"exports names to the namespace"
+ , Option [] ["no-export"] (NoArg NoExport)
+ "do not export names to the global object"
, Option ['i'] ["interface"] (ReqArg Interface "PATH")
"path to the interface; uses module.jsi by default"
]
@@ -40,14 +44,16 @@ usage = usageInfo
main = do
args <- getArgs
dataDir <- getDataDir
- let (opts, nonOpts, errors) = getOpt RequireOrder options args
+ let (opts', nonOpts, errors) = getOpt Permute options args
+ let opts = sort opts'
unless (null errors) $ do
mapM_ putStrLn errors
fail "jscc terminated"
checkHelp opts
(isDebugMode, opts) <- getDebugMode opts
(namespace, opts) <- getNamespace opts
(ifacePath, opts) <- getInterfacePath opts nonOpts
+ (isExport, opts) <- getExportGlobals opts
when (not $ null opts) $ do
putStrLn $ "spurious arguments: " ++ (show opts)
fail "jscc terminated"
@@ -60,9 +66,9 @@ main = do
interface <- parseInterface ifacePath
let result = if isDebugMode
then compileFormatted rawImpl implPath rawBoilerplate
- interface
+ isExport interface
else compileRelease rawImpl implPath rawBoilerplate
- interface namespace
+ isExport interface namespace
putStrLn result
return ()
otherwise -> do
@@ -87,6 +93,9 @@ checkHelp (Help:_) = do
exitSuccess
checkHelp _ = return ()
+getExportGlobals (NoExport:rest) = return (False, rest)
+getExportGlobals rest = return (True, rest)
+
getInterfacePath :: [Flag] -> [String] -> IO (FilePath,[Flag])
getInterfacePath (Interface path:rest) _ = do
checkFile path

0 comments on commit f0d5aaf

Please sign in to comment.