git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #639 from purescript/no-runtime-type-checks
5a20c09Phil Freeman
docs/source/start.rst−1
@@ -35,7 +35,6 @@ The following options are supported:
35--stdin Read input from standard input instead of from files.
35--stdin Read input from standard input instead of from files.
36--output Write the generated Javascript to the specified file.
36--output Write the generated Javascript to the specified file.
37--externs Write a list of foreign imports declarations to the specified file in addition to generating Javascript output.
37--externs Write a list of foreign imports declarations to the specified file in addition to generating Javascript output.
38−--runtime-type-checks Generate simple runtime type checks for function arguments with simple types.
39--no-tco Turn off tail-call elimination.
38--no-tco Turn off tail-call elimination.
40--no-prelude Do not include the Prelude in the generated Javascript.
39--no-prelude Do not include the Prelude in the generated Javascript.
41--no-magic-do Turn off optimizations which inline calls to ``>>=`` for the ``Eff`` monad.
40--no-magic-do Turn off optimizations which inline calls to ``>>=`` for the ``Eff`` monad.
psc-make/Main.hs+1−5
@@ -104,10 +104,6 @@ noTco :: Term Bool
104noTco = value $ flag $ (optInfo [ "no-tco" ])
104noTco = value $ flag $ (optInfo [ "no-tco" ])
105 { optDoc = "Disable tail call optimizations" }
105 { optDoc = "Disable tail call optimizations" }
106
106
107−performRuntimeTypeChecks :: Term Bool
108−performRuntimeTypeChecks = value $ flag $ (optInfo [ "runtime-type-checks" ])
109− { optDoc = "Generate runtime type checks" }
110−
111noPrelude :: Term Bool
107noPrelude :: Term Bool
112noPrelude = value $ flag $ (optInfo [ "no-prelude" ])
108noPrelude = value $ flag $ (optInfo [ "no-prelude" ])
113 { optDoc = "Omit the Prelude" }
109 { optDoc = "Omit the Prelude" }
@@ -125,7 +121,7 @@ verboseErrors = value $ flag $ (optInfo [ "v", "verbose-errors" ])
125 { optDoc = "Display verbose error messages" }
121 { optDoc = "Display verbose error messages" }
126
122
127options :: Term (P.Options P.Make)
123options :: Term (P.Options P.Make)
128−options = P.Options <$> noPrelude <*> noTco <*> performRuntimeTypeChecks <*> noMagicDo <*> pure Nothing <*> noOpts <*> verboseErrors <*> pure P.MakeOptions
124+options = P.Options <$> noPrelude <*> noTco <*> noMagicDo <*> pure Nothing <*> noOpts <*> verboseErrors <*> pure P.MakeOptions
129
125
130noPrefix :: Term Bool
126noPrefix :: Term Bool
131noPrefix = value $ flag $ (optInfo ["p", "no-prefix" ])
127noPrefix = value $ flag $ (optInfo ["p", "no-prefix" ])
psc/Main.hs+1−5
@@ -96,10 +96,6 @@ noTco :: Term Bool
96noTco = value $ flag $ (optInfo [ "no-tco" ])
96noTco = value $ flag $ (optInfo [ "no-tco" ])
97 { optDoc = "Disable tail call optimizations" }
97 { optDoc = "Disable tail call optimizations" }
98
98
99−performRuntimeTypeChecks :: Term Bool
100−performRuntimeTypeChecks = value $ flag $ (optInfo [ "runtime-type-checks" ])
101− { optDoc = "Generate runtime type checks" }
102−
103noPrelude :: Term Bool
99noPrelude :: Term Bool
104noPrelude = value $ flag $ (optInfo [ "no-prelude" ])
100noPrelude = value $ flag $ (optInfo [ "no-prelude" ])
105 { optDoc = "Omit the Prelude" }
101 { optDoc = "Omit the Prelude" }
@@ -137,7 +133,7 @@ noPrefix = value $ flag $ (optInfo ["no-prefix" ])
137 { optDoc = "Do not include comment header"}
133 { optDoc = "Do not include comment header"}
138
134
139options :: Term (P.Options P.Compile)
135options :: Term (P.Options P.Compile)
140−options = P.Options <$> noPrelude <*> noTco <*> performRuntimeTypeChecks <*> noMagicDo <*> runMain <*> noOpts <*> verboseErrors <*> additionalOptions
136+options = P.Options <$> noPrelude <*> noTco <*> noMagicDo <*> runMain <*> noOpts <*> verboseErrors <*> additionalOptions
141 where
137 where
142 additionalOptions = P.CompileOptions <$> browserNamespace <*> dceModules <*> codeGenModules
138 additionalOptions = P.CompileOptions <$> browserNamespace <*> dceModules <*> codeGenModules
143
139
psci/Main.hs+2−2
@@ -114,7 +114,7 @@ findNodeProcess = runMaybeT . msum $ map (MaybeT . findExecutable) names
114--
114--
115getHistoryFilename :: IO FilePath
115getHistoryFilename :: IO FilePath
116getHistoryFilename = do
116getHistoryFilename = do
117− home <- getHomeDirectory
117+ home <- getHomeDirectory
118 let filename = home </> ".purescript" </> "psci_history"
118 let filename = home </> ".purescript" </> "psci_history"
119 mkdirp filename
119 mkdirp filename
120 return filename
120 return filename
@@ -202,7 +202,7 @@ completion = completeWord Nothing " \t\n\r" findCompletions
202-- | Compilation options.
202-- | Compilation options.
203--
203--
204options :: P.Options P.Make
204options :: P.Options P.Make
205−options = P.Options False False False False Nothing False False P.MakeOptions
205+options = P.Options False False False Nothing False False P.MakeOptions
206
206
207-- |
207-- |
208-- PSCI monad
208-- PSCI monad
src/Language/PureScript/CodeGen/JS.hs−37
@@ -33,7 +33,6 @@ import Language.PureScript.Names
33import Language.PureScript.Declarations
33import Language.PureScript.Declarations
34import Language.PureScript.Options
34import Language.PureScript.Options
35import Language.PureScript.CodeGen.JS.AST as AST
35import Language.PureScript.CodeGen.JS.AST as AST
36−import Language.PureScript.Types
37import Language.PureScript.Optimizer
36import Language.PureScript.Optimizer
38import Language.PureScript.CodeGen.Common
37import Language.PureScript.CodeGen.Common
39import Language.PureScript.Environment
38import Language.PureScript.Environment
@@ -228,10 +227,6 @@ valueToJs opts m e (Let ds val) = do
228valueToJs opts m e (Abs (Left arg) val) = do
227valueToJs opts m e (Abs (Left arg) val) = do
229 ret <- valueToJs opts m e val
228 ret <- valueToJs opts m e val
230 return $ JSFunction Nothing [identToJs arg] (JSBlock [JSReturn ret])
229 return $ JSFunction Nothing [identToJs arg] (JSBlock [JSReturn ret])
231−valueToJs opts m e (TypedValue _ (Abs (Left arg) val) ty) | optionsPerformRuntimeTypeChecks opts = do
232− let arg' = identToJs arg
233− ret <- valueToJs opts m e val
234− return $ JSFunction Nothing [arg'] (JSBlock $ runtimeTypeChecks arg' ty ++ [JSReturn ret])
235valueToJs _ m _ (Var ident) = return $ varToJs m ident
230valueToJs _ m _ (Var ident) = return $ varToJs m ident
236valueToJs opts m e (TypedValue _ val _) = valueToJs opts m e val
231valueToJs opts m e (TypedValue _ val _) = valueToJs opts m e val
237valueToJs opts m e (PositionedValue _ val) = valueToJs opts m e val
232valueToJs opts m e (PositionedValue _ val) = valueToJs opts m e val
@@ -257,38 +252,6 @@ extendObj obj sts = do
257 extend = map stToAssign sts
252 extend = map stToAssign sts
258 return $ JSApp (JSFunction Nothing [] block) []
253 return $ JSApp (JSFunction Nothing [] block) []
259
254
260−-- |
261−-- Generate code in the simplified Javascript intermediate representation for runtime type checks.
262−--
263−runtimeTypeChecks :: String -> Type -> [JS]
264−runtimeTypeChecks arg ty =
265− let
266− argTy = getFunctionArgumentType ty
267− in
268− maybe [] (argumentCheck (JSVar arg)) argTy
269− where
270− getFunctionArgumentType :: Type -> Maybe Type
271− getFunctionArgumentType (TypeApp (TypeApp t funArg) _) | t == tyFunction = Just funArg
272− getFunctionArgumentType (ForAll _ ty' _) = getFunctionArgumentType ty'
273− getFunctionArgumentType _ = Nothing
274− argumentCheck :: JS -> Type -> [JS]
275− argumentCheck val t | t == tyNumber = [typeCheck val "number"]
276− argumentCheck val t | t == tyString = [typeCheck val "string"]
277− argumentCheck val t | t == tyBoolean = [typeCheck val "boolean"]
278− argumentCheck val (TypeApp t _) | t == tyArray = [arrayCheck val]
279− argumentCheck val (TypeApp t row) | t == tyObject =
280− let
281− (pairs, _) = rowToList row
282− in
283− typeCheck val "object" : concatMap (\(prop, ty') -> argumentCheck (accessorString prop val) ty') pairs
284− argumentCheck val (TypeApp (TypeApp t _) _) | t == tyFunction = [typeCheck val "function"]
285− argumentCheck val (ForAll _ ty' _) = argumentCheck val ty'
286− argumentCheck _ _ = []
287− typeCheck :: JS -> String -> JS
288− typeCheck js ty' = JSIfElse (JSBinary NotEqualTo (JSTypeOf js) (JSStringLiteral ty')) (JSBlock [JSThrow (JSStringLiteral $ ty' ++ " expected")]) Nothing
289− arrayCheck :: JS -> JS
290− arrayCheck js = JSIfElse (JSUnary Not (JSApp (JSAccessor "isArray" (JSVar "Array")) [js])) (JSBlock [JSThrow (JSStringLiteral "Array expected")]) Nothing
291−
292-- |
255-- |
293-- Generate code in the simplified Javascript intermediate representation for a reference to a
256-- Generate code in the simplified Javascript intermediate representation for a reference to a
294-- variable.
257-- variable.
src/Language/PureScript/Options.hs+2−6
@@ -52,10 +52,6 @@ data Options mode = Options {
52 -- Disable tail-call elimination
52 -- Disable tail-call elimination
53 --
53 --
54 , optionsNoTco :: Bool
54 , optionsNoTco :: Bool
55− -- |
56− -- Perform type checks at runtime
57− --
58− , optionsPerformRuntimeTypeChecks :: Bool
59 -- |
55 -- |
60 -- Disable inlining of calls to return and bind for the Eff monad
56 -- Disable inlining of calls to return and bind for the Eff monad
61 --
57 --
@@ -84,10 +80,10 @@ data Options mode = Options {
84-- Default compiler options
80-- Default compiler options
85--
81--
86defaultCompileOptions :: Options Compile
82defaultCompileOptions :: Options Compile
87−defaultCompileOptions = Options False False False False Nothing False False (CompileOptions "PS" [] [])
83+defaultCompileOptions = Options False False False Nothing False False (CompileOptions "PS" [] [])
88
84
89-- |
85-- |
90-- Default make options
86-- Default make options
91--
87--
92defaultMakeOptions :: Options Make
88defaultMakeOptions :: Options Make
93−defaultMakeOptions = Options False False False False Nothing False False MakeOptions
89+defaultMakeOptions = Options False False False Nothing False False MakeOptions