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
a3e07b6Phil Freeman
src/Language/PureScript/CodeGen/JS.hs+1−4
@@ -47,7 +47,6 @@ import Language.PureScript.CodeGen.Optimize
47--
47--
48moduleToJs :: Options -> Module -> Environment -> [JS]
48moduleToJs :: Options -> Module -> Environment -> [JS]
49moduleToJs opts (Module pname@(ProperName name) decls) env =
49moduleToJs opts (Module pname@(ProperName name) decls) env =
50− mapMaybe filterRawDecls decls ++
51 [ JSVariableIntroduction (Ident name) Nothing
50 [ JSVariableIntroduction (Ident name) Nothing
52 , JSApp (JSFunction Nothing [Ident name]
51 , JSApp (JSFunction Nothing [Ident name]
53 (JSBlock (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts (ModuleName pname) decl env) (sortBy typeClassesLast decls))))
52 (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 =
55 (JSBinary Or (JSVar (Ident name)) (JSObjectLiteral []))]
54 (JSBinary Or (JSVar (Ident name)) (JSObjectLiteral []))]
56 ]
55 ]
57 where
56 where
58− filterRawDecls (ExternDeclaration ForeignImport _ (Just js) _) = Just js
59− filterRawDecls _ = Nothing
60 typeClassesLast (ExternDeclaration TypeClassDictionaryImport _ _ _) (ExternDeclaration TypeClassDictionaryImport _ _ _) = EQ
57 typeClassesLast (ExternDeclaration TypeClassDictionaryImport _ _ _) (ExternDeclaration TypeClassDictionaryImport _ _ _) = EQ
61 typeClassesLast (ExternDeclaration TypeClassDictionaryImport _ _ _) _ = GT
58 typeClassesLast (ExternDeclaration TypeClassDictionaryImport _ _ _) _ = GT
62 typeClassesLast _ (ExternDeclaration TypeClassDictionaryImport _ _ _) = LT
59 typeClassesLast _ (ExternDeclaration TypeClassDictionaryImport _ _ _) = LT
@@ -87,7 +84,7 @@ declToJs _ mp (DataDeclaration _ _ ctors) _ =
87 in [ ctorJs, setProperty ctor (JSVar (Ident ctor)) mp ]
84 in [ ctorJs, setProperty ctor (JSVar (Ident ctor)) mp ]
88declToJs opts mp (DataBindingGroupDeclaration ds) e =
85declToJs opts mp (DataBindingGroupDeclaration ds) e =
89 Just $ concat $ mapMaybe (flip (declToJs opts mp) e) ds
86 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) _) _ =
91 Just [ js
88 Just [ js
92 , setProperty (identToJs ident) (JSVar ident) mp ]
89 , setProperty (identToJs ident) (JSVar ident) mp ]
93declToJs _ _ _ _ = Nothing
90declToJs _ _ _ _ = Nothing
src/Language/PureScript/Declarations.hs+4
@@ -53,6 +53,10 @@ data ForeignImportType
53 --
53 --
54 = ForeignImport
54 = ForeignImport
55 -- |
55 -- |
56+ -- A foreign import which contains inline Javascript as a string literal
57+ --
58+ | InlineJavascript
59+ -- |
56 -- A type class dictionary import, generated during desugaring of type class declarations
60 -- A type class dictionary import, generated during desugaring of type class declarations
57 --
61 --
58 | TypeClassDictionaryImport
62 | TypeClassDictionaryImport
src/Language/PureScript/Parser/Declarations.hs+5−5
@@ -19,7 +19,7 @@ module Language.PureScript.Parser.Declarations (
19 parseModules
19 parseModules
20) where
20) where
21
21
22−import Data.Maybe (fromMaybe)
22+import Data.Maybe (isJust, fromMaybe)
23import Control.Applicative
23import Control.Applicative
24import qualified Text.Parsec as P
24import qualified Text.Parsec as P
25
25
@@ -68,10 +68,10 @@ parseExternDeclaration :: P.Parsec String ParseState Declaration
68parseExternDeclaration = P.try (reserved "foreign") *> indented *> (reserved "import") *> indented *>
68parseExternDeclaration = P.try (reserved "foreign") *> indented *> (reserved "import") *> indented *>
69 (ExternDataDeclaration <$> (P.try (reserved "data") *> indented *> properName)
69 (ExternDataDeclaration <$> (P.try (reserved "data") *> indented *> properName)
70 <*> (lexeme (indented *> P.string "::") *> parseKind)
70 <*> (lexeme (indented *> P.string "::") *> parseKind)
71− <|> ExternDeclaration ForeignImport <$> parseIdent
71+ <|> do ident <- parseIdent
72− <*> P.optionMaybe (parseJSLiteral <$> stringLiteral)
72+ js <- P.optionMaybe (parseJSLiteral <$> stringLiteral)
73− <*> (lexeme (indented *> P.string "::") *> parsePolyType))
73+ ty <- (lexeme (indented *> P.string "::") *> parsePolyType)
74−
74+ return $ ExternDeclaration (if isJust js then InlineJavascript else ForeignImport) ident js ty)
75parseJSLiteral :: String -> JS
75parseJSLiteral :: String -> JS
76parseJSLiteral s = either (const $ JSRaw s) id $ P.runParser parseJS () "Javascript" s
76parseJSLiteral s = either (const $ JSRaw s) id $ P.runParser parseJS () "Javascript" s
77
77