Skip to content

Commit

Permalink
Added ghcjs-base-stub to compile using GHC
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis Pan committed Mar 12, 2017
1 parent 6cd2242 commit 1aa227e
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 78 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
.stack-work
stack.ghcjs.yaml
16 changes: 16 additions & 0 deletions Makefile
@@ -0,0 +1,16 @@
# This makefile is for building using GHCJS
SHELL := /bin/bash

.PHONY: all install clean build

all: build
@true # dependency tracking doesn't work for empty @targets

stack.ghcjs.yaml: stack.yaml ghcjs.yaml
cat stack.yaml ghcjs.yaml > stack.ghcjs.yaml

build: stack.ghcjs.yaml
stack build --stack-yaml stack.ghcjs.yaml

clean: stack.ghcjs.yaml
stack clean --stack-yaml stack.ghcjs.yaml
9 changes: 9 additions & 0 deletions ghcjs.yaml
@@ -0,0 +1,9 @@
compiler: ghcjs-0.2.1.9007019_ghc-8.0.1
compiler-check: match-exact

setup-info:
ghcjs:
source:
ghcjs-0.2.1.9007019_ghc-8.0.1:
url: http://ghcjs.tolysz.org/ghc-8.0-2017-02-05-lts-7.19-9007019.tar.gz
sha1: d2cfc25f9cda32a25a87d9af68891b2186ee52f9
2 changes: 2 additions & 0 deletions glazier-react.cabal
Expand Up @@ -51,6 +51,8 @@ library
ghc-options: -Wall
if impl(ghcjs)
build-depends: ghcjs-base
if !impl(ghcjs)
build-depends: ghcjs-base-stub >= 0.1.0.1 && < 1

source-repository head
type: git
Expand Down
10 changes: 10 additions & 0 deletions src/Glazier/React/Command.hs
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}

Expand Down Expand Up @@ -27,6 +28,15 @@ basicRenderCmd frameNum componentRef fcmd = do
sm <- get
pure $ fcmd sm [("frameNum", i)] r

#ifdef __GHCJS__

foreign import javascript unsafe
"Number.MAX_SAFE_INTEGER"
js_maxSafeInt :: Int

#else

js_maxSafeInt :: Int
js_maxSafeInt = 0

#endif
10 changes: 10 additions & 0 deletions src/Glazier/React/Command/Run.hs
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}

-- | Common functions used by command interpreters
Expand All @@ -22,6 +23,15 @@ componentSetState sm props j = do
let dict' = J.pToJSVal $ JE.PureJSVal dict
js_componentSetState dict' j

#ifdef __GHCJS__

foreign import javascript unsafe
"if ($2 && $2['setState']) { $2['setState']($1); }"
js_componentSetState :: J.JSVal -> J.JSVal -> IO ()

#else

js_componentSetState :: J.JSVal -> J.JSVal -> IO ()
js_componentSetState _ _ = pure ()

#endif
12 changes: 12 additions & 0 deletions src/Glazier/React/Component.hs
@@ -1,3 +1,5 @@
{-# LANGUAGE CPP #-}

module Glazier.React.Component
( ReactComponent
, mkComponent
Expand All @@ -21,7 +23,17 @@ instance J.PToJSVal ReactComponent where
mkComponent :: IO ReactComponent
mkComponent = ReactComponent <$> js_mkComponent

#ifdef __GHCJS__

foreign import javascript unsafe
"$r = hgr$mkClass();"
js_mkComponent
:: IO J.JSVal

#else

js_mkComponent :: IO J.JSVal
js_mkComponent = pure J.nullRef


#endif
64 changes: 42 additions & 22 deletions src/Glazier/React/Element.hs
@@ -1,4 +1,4 @@
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}

-- | 'Lucid.HtmlT' inspired monad for creating 'ReactElement's
Expand All @@ -21,18 +21,6 @@ instance J.IsJSVal ReactElement
instance J.PToJSVal ReactElement where
pToJSVal = J.jsval

-- | This is an IO action because even if the same args was used
-- a different ReactElement may be created, because JSVal
-- and JSArray are mutable.
foreign import javascript unsafe
"$r = React.createElement($1, $2, $3);"
js_mkBranchElement :: J.JSVal -> J.JSVal -> JA.JSArray -> IO ReactElement

foreign import javascript unsafe
"$r = React.createElement($1, $2);"
js_mkLeafElement :: J.JSVal -> J.JSVal -> IO ReactElement


-- | Unfortunately, ReactJS did not export an easy way to check if something is a ReactElement,
-- although they do so in the internal code with REACT_ELEMENT_TYPE.
-- This function allow coercing a ReactElement from a JSVal
Expand All @@ -54,21 +42,53 @@ mkLeafElement n props = do
props' <- JE.toMaybeJSObject props
js_mkLeafElement n (J.pToJSVal (JE.PureJSVal <$> props'))

foreign import javascript unsafe
"$r = $1;"
js_textElement :: J.JSString -> ReactElement

-- | Not an IO action because JSString is immutable
textElement :: J.JSString -> ReactElement
textElement = js_textElement

-- | Wrap a list of ReactElements inside a 'div'
foreign import javascript unsafe
"$r = hgr$mkCombinedElements($1);"
js_mkCombinedElements :: JA.JSArray -> IO ReactElement

-- | React only allows a single top most element.
-- Provide a handly function to wrap a list of ReactElements inside a 'div' if required.
-- If there is only one element in the list, then nothing is changed.
mkCombinedElements :: [ReactElement] -> IO ReactElement
mkCombinedElements xs = js_mkCombinedElements (JA.fromList $ J.jsval <$> xs)

#ifdef __GHCJS__

-- | This is an IO action because even if the same args was used
-- a different ReactElement may be created, because JSVal
-- and JSArray are mutable.
foreign import javascript unsafe
"$r = React.createElement($1, $2, $3);"
js_mkBranchElement :: J.JSVal -> J.JSVal -> JA.JSArray -> IO ReactElement

foreign import javascript unsafe
"$r = React.createElement($1, $2);"
js_mkLeafElement :: J.JSVal -> J.JSVal -> IO ReactElement

foreign import javascript unsafe
"$r = $1;"
js_textElement :: J.JSString -> ReactElement

-- | Wrap a list of ReactElements inside a 'div'
foreign import javascript unsafe
"$r = hgr$mkCombinedElements($1);"
js_mkCombinedElements :: JA.JSArray -> IO ReactElement

#else

-- | This is an IO action because even if the same args was used
-- a different ReactElement may be created, because JSVal
-- and JSArray are mutable.
js_mkBranchElement :: J.JSVal -> J.JSVal -> JA.JSArray -> IO ReactElement
js_mkBranchElement _ _ _ = pure (ReactElement J.nullRef)

js_mkLeafElement :: J.JSVal -> J.JSVal -> IO ReactElement
js_mkLeafElement _ _ = pure (ReactElement J.nullRef)

js_textElement :: J.JSString -> ReactElement
js_textElement _ = ReactElement J.nullRef

js_mkCombinedElements :: JA.JSArray -> IO ReactElement
js_mkCombinedElements _ = pure (ReactElement J.nullRef)

#endif

0 comments on commit 1aa227e

Please sign in to comment.