Skip to content

Instantly share code, notes, and snippets.

@space-shell
Last active July 3, 2019 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save space-shell/d8902a9d380e4b36a80a2448c2e621cc to your computer and use it in GitHub Desktop.
Save space-shell/d8902a9d380e4b36a80a2448c2e621cc to your computer and use it in GitHub Desktop.
Asterius `read` function error
module Main where
import Data.List
import Asterius.Types (JSVal(..))
{-# INLINEABLE emptyJSString #-}
emptyJSString :: JSVal
emptyJSString = js_string_empty
{-# INLINEABLE concatJSString #-}
concatJSString :: JSVal -> JSVal -> JSVal
concatJSString = js_concat
{-# INLINEABLE indexJSString #-}
indexJSString :: JSVal -> Int -> Char
indexJSString = js_string_tochar
{-# INLINEABLE toJSString #-}
toJSString :: String -> JSVal
toJSString = foldl' (\s c -> js_concat s (js_string_fromchar c)) js_string_empty
{-# INLINEABLE fromJSString #-}
fromJSString :: JSVal -> String
fromJSString s = [js_string_tochar s i | i <- [0 .. js_length s - 1]]
{-# INLINEABLE emptyJSArray #-}
emptyJSArray :: JSVal
emptyJSArray = js_array_empty
{-# INLINEABLE concatJSArray #-}
concatJSArray :: JSVal -> JSVal -> JSVal
concatJSArray = js_concat
{-# INLINEABLE indexJSArray #-}
indexJSArray :: JSVal -> Int -> JSVal
indexJSArray = js_index_by_int
{-# INLINEABLE toJSArray #-}
toJSArray :: [JSVal] -> JSVal
toJSArray = foldl' js_concat js_array_empty
{-# INLINEABLE fromJSArray #-}
fromJSArray :: JSVal -> [JSVal]
fromJSArray arr = [js_index_by_int arr i | i <- [0 .. js_length arr - 1]]
{-# INLINEABLE emptyJSObject #-}
emptyJSObject :: JSVal
emptyJSObject = js_object_empty
{-# INLINEABLE indexJSObject #-}
indexJSObject :: JSVal -> String -> JSVal
indexJSObject obj k = js_index_by_jsref obj (toJSString k)
{-# INLINEABLE callJSObjectMethod #-}
callJSObjectMethod :: JSVal -> String -> [JSVal] -> JSVal
callJSObjectMethod obj f args =
js_function_apply (js_index_by_jsref obj (toJSString f)) obj (toJSArray args)
{-# INLINEABLE json #-}
json :: JSVal
json = js_json
{-# INLINEABLE callJSFunction #-}
callJSFunction :: JSVal -> [JSVal] -> JSVal
callJSFunction f args = js_function_apply f js_object_empty (toJSArray args)
foreign import javascript "\"\"" js_string_empty :: JSVal
foreign import javascript "${1}.concat(${2})" js_concat
:: JSVal -> JSVal -> JSVal
foreign import javascript "${1}.length" js_length :: JSVal -> Int
foreign import javascript "String.fromCodePoint(${1})" js_string_fromchar
:: Char -> JSVal
foreign import javascript "${1}.codePointAt(${2})" js_string_tochar
:: JSVal -> Int -> Char
foreign import javascript "[]" js_array_empty :: JSVal
foreign import javascript "${1}[${2}]" js_index_by_int :: JSVal -> Int -> JSVal
foreign import javascript "{}" js_object_empty :: JSVal
foreign import javascript "${1}[${2}]" js_index_by_jsref
:: JSVal -> JSVal -> JSVal
foreign import javascript "JSON" js_json :: JSVal
foreign import javascript "${1}.apply(${2}, ${3})" js_function_apply :: JSVal -> JSVal -> JSVal -> JSVal
foreign import javascript "console.log(${1})" js_print_string :: JSVal -> IO ()
foreign import javascript "console.log(${1})" js_print_float :: Float -> IO ()
foreign import javascript "console.log(${1})" js_print_double :: Double -> IO ()
main :: IO ()
main =
do
let dbl = read "1.2" :: Double
let dbldbl = dbl * 2
js_print_double dbl
js_print_double dbldbl
let flt = read "1.2" :: Float
let fltdbl = flt * 2
js_print_float flt
js_print_float fltdbl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment