git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
When defining FFI calls with inline JS, copy the JS into the module body #145
commita3e07b6f971113fa1837e222c3ef95be8ecd6ee4 authorPhil Freeman <paf31@cantab.net> dateTue 28 Jan 2014, 21:35 (13 years ago) parentd698508
3 files changed+10−9
src/Language/PureScript/CodeGen/JS.hs+1−4 src/Language/PureScript/Declarations.hs+4 src/Language/PureScript/Parser/Declarations.hs+5−5
src/Language/PureScript/CodeGen/JS.hs+1−4
@@ -47,7 +47,6 @@ import Language.PureScript.CodeGen.Optimize
4747--
4848moduleToJs :: Options -> Module -> Environment -> [JS]
4949moduleToJs opts (Module pname@(ProperName name) decls) env =
50− mapMaybe filterRawDecls decls ++
5150 [ JSVariableIntroduction (Ident name) Nothing
5251 , JSApp (JSFunction Nothing [Ident name]
5352 (JSBlock (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts (ModuleName pname) decl env) (sortBy typeClassesLast decls))))
@@ -55,8 +54,6 @@ moduleToJs opts (Module pname@(ProperName name) decls) env =
5554 (JSBinary Or (JSVar (Ident name)) (JSObjectLiteral []))]
5655 ]
5756 where
58− filterRawDecls (ExternDeclaration ForeignImport _ (Just js) _) = Just js
59− filterRawDecls _ = Nothing
6057 typeClassesLast (ExternDeclaration TypeClassDictionaryImport _ _ _) (ExternDeclaration TypeClassDictionaryImport _ _ _) = EQ
6158 typeClassesLast (ExternDeclaration TypeClassDictionaryImport _ _ _) _ = GT
6259 typeClassesLast _ (ExternDeclaration TypeClassDictionaryImport _ _ _) = LT
@@ -87,7 +84,7 @@ declToJs _ mp (DataDeclaration _ _ ctors) _ =
8784 in [ ctorJs, setProperty ctor (JSVar (Ident ctor)) mp ]
8885declToJs opts mp (DataBindingGroupDeclaration ds) e =
8986 Just $ concat $ mapMaybe (flip (declToJs opts mp) e) ds
90−declToJs _ mp (ExternDeclaration importTy ident (Just js) _) _ | importTy /= ForeignImport =
87+declToJs _ mp (ExternDeclaration importTy ident (Just js) _) _ =
9188 Just [ js
9289 , setProperty (identToJs ident) (JSVar ident) mp ]
9390declToJs _ _ _ _ = Nothing
src/Language/PureScript/Declarations.hs+4
@@ -53,6 +53,10 @@ data ForeignImportType
5353 --
5454 = ForeignImport
5555 -- |
56+ -- A foreign import which contains inline Javascript as a string literal
57+ --
58+ | InlineJavascript
59+ -- |
5660 -- A type class dictionary import, generated during desugaring of type class declarations
5761 --
5862 | TypeClassDictionaryImport
src/Language/PureScript/Parser/Declarations.hs+5−5
@@ -19,7 +19,7 @@ module Language.PureScript.Parser.Declarations (
1919 parseModules
2020) where
2121
22−import Data.Maybe (fromMaybe)
22+import Data.Maybe (isJust, fromMaybe)
2323import Control.Applicative
2424import qualified Text.Parsec as P
2525
@@ -68,10 +68,10 @@ parseExternDeclaration :: P.Parsec String ParseState Declaration
6868parseExternDeclaration = P.try (reserved "foreign") *> indented *> (reserved "import") *> indented *>
6969 (ExternDataDeclaration <$> (P.try (reserved "data") *> indented *> properName)
7070 <*> (lexeme (indented *> P.string "::") *> parseKind)
71− <|> ExternDeclaration ForeignImport <$> parseIdent
72− <*> P.optionMaybe (parseJSLiteral <$> stringLiteral)
73− <*> (lexeme (indented *> P.string "::") *> parsePolyType))
74−
71+ <|> do ident <- parseIdent
72+ js <- P.optionMaybe (parseJSLiteral <$> stringLiteral)
73+ ty <- (lexeme (indented *> P.string "::") *> parsePolyType)
74+ return $ ExternDeclaration (if isJust js then InlineJavascript else ForeignImport) ident js ty)
7575parseJSLiteral :: String -> JS
7676parseJSLiteral s = either (const $ JSRaw s) id $ P.runParser parseJS () "Javascript" s
7777