git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
fixes #2172: allow escape seqs in JS FFI exported using strings
17c486dMichael Ficarra
examples/passing/2172.jsnew file+5
@@ -0,0 +1,5 @@
1+exports['a\''] = 0;
2+exports["\x62\x27"] = 1;
3+// NOTE: I wanted to use "\c'" here, but langauge-javascript doesn't support it...
4+exports["c'"] = 2;
5+exports["\u0064\u0027"] = 3;
examples/passing/2172.pursnew file+10
@@ -0,0 +1,10 @@
1+module Main where
2+
3+import Control.Monad.Eff.Console (log)
4+
5+foreign import a' :: Number
6+foreign import b' :: Number
7+foreign import c' :: Number
8+foreign import d' :: Number
9+
10+main = log "Done"
purescript.cabal+1
@@ -20,6 +20,7 @@ author: Phil Freeman <paf31@cantab.net>,
20tested-with: GHC==7.10.3
20tested-with: GHC==7.10.3
21
21
22extra-source-files: examples/passing/*.purs
22extra-source-files: examples/passing/*.purs
23+ , examples/passing/*.js
23 , examples/passing/2018/*.purs
24 , examples/passing/2018/*.purs
24 , examples/passing/2138/*.purs
25 , examples/passing/2138/*.purs
25 , examples/passing/ClassRefSyntax/*.purs
26 , examples/passing/ClassRefSyntax/*.purs
src/Language/PureScript/Bundle.hs+28−4
@@ -19,6 +19,7 @@ import Prelude.Compat
19import Control.Monad
19import Control.Monad
20import Control.Monad.Error.Class
20import Control.Monad.Error.Class
21
21
22+import Data.Char (chr, digitToInt)
22import Data.Generics (everything, everywhere, mkQ, mkT)
23import Data.Generics (everything, everywhere, mkQ, mkT)
23import Data.Graph
24import Data.Graph
24import Data.List (nub, stripPrefix)
25import Data.List (nub, stripPrefix)
@@ -186,11 +187,34 @@ withDeps (Module modulePath es) = Module modulePath (map expandDeps es)
186
187
187-- String literals include the quote chars
188-- String literals include the quote chars
188fromStringLiteral :: JSExpression -> Maybe String
189fromStringLiteral :: JSExpression -> Maybe String
189−fromStringLiteral (JSStringLiteral _ str) = Just $ trimStringQuotes str
190+fromStringLiteral (JSStringLiteral _ str) = Just $ strValue str
190fromStringLiteral _ = Nothing
191fromStringLiteral _ = Nothing
191
192
192−trimStringQuotes :: String -> String
193+strValue :: String -> String
193−trimStringQuotes str = reverse $ drop 1 $ reverse $ drop 1 $ str
194+strValue str = go $ drop 1 str
195+ where
196+ go ('\\' : 'b' : xs) = '\b' : go xs
197+ go ('\\' : 'f' : xs) = '\f' : go xs
198+ go ('\\' : 'n' : xs) = '\n' : go xs
199+ go ('\\' : 'r' : xs) = '\r' : go xs
200+ go ('\\' : 't' : xs) = '\t' : go xs
201+ go ('\\' : 'v' : xs) = '\v' : go xs
202+ go ('\\' : '0' : xs) = '\0' : go xs
203+ go ('\\' : 'x' : a : b : xs) = chr (a' + b') : go xs
204+ where
205+ a' = 16 * digitToInt a
206+ b' = digitToInt b
207+ go ('\\' : 'u' : a : b : c : d : xs) = chr (a' + b' + c' + d') : go xs
208+ where
209+ a' = 16 * 16 * 16 * digitToInt a
210+ b' = 16 * 16 * digitToInt b
211+ c' = 16 * digitToInt c
212+ d' = digitToInt d
213+ go ('\\' : x : xs) = x : go xs
214+ go "\"" = ""
215+ go "'" = ""
216+ go (x : xs) = x : go xs
217+ go "" = ""
194
218
195commaList :: JSCommaList a -> [a]
219commaList :: JSCommaList a -> [a]
196commaList JSLNil = []
220commaList JSLNil = []
@@ -332,7 +356,7 @@ matchExportsAssignment stmt
332 = Nothing
356 = Nothing
333
357
334extractLabel :: JSPropertyName -> Maybe String
358extractLabel :: JSPropertyName -> Maybe String
335−extractLabel (JSPropertyString _ nm) = Just (trimStringQuotes nm)
359+extractLabel (JSPropertyString _ nm) = Just $ strValue nm
336extractLabel (JSPropertyIdent _ nm) = Just nm
360extractLabel (JSPropertyIdent _ nm) = Just nm
337extractLabel _ = Nothing
361extractLabel _ = Nothing
338
362