diff --git a/.gitignore b/.gitignore index 3f4aa15..8b8af9f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1,7 @@ dist -cabal-dev -*.o -*.hi -*.chi -*.chs.h -*.dyn_o -*.dyn_hi -.virtualenv -.hpc -.hsenv -.cabal-sandbox/ -cabal.sandbox.config -*.prof -*.aux -*.hp +*hi +*o +*.jsexe +*~ +.#* +\#*# diff --git a/README.md b/README.md index f0ae746..27b7848 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,25 @@ -# ghcjs-node-minimal +# Haskell + Node.js Minimal Sample Demonstrates a minimal code to produce a working (JavaScript only) nodejs application, also showing off how to depend on other nodejs modules. + +## Basic test, no JS specific stuff. +This is easily done following the ghcjs doc, the Usage section: +https://github.com/ghcjs/ghcjs + +Create a standard Haskell executable project with cabal. + +Create a Main.hs module, put a basic Hello World output into main. + +Build with: ghcjs -o hello src/Main.hs + +Test with: node hello.jsexe/all.js + +## Now make JavaScript calls via FFI +Just import a standard node module and call a function like this in JavaScript: + +var os = require("os"); +console.log(os.hostname()); + +Inspiration from: +http://weblog.luite.com/wordpress/ +https://nodejs.org/api/os.html + diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/ghcjs-nodejs-hello.cabal b/ghcjs-nodejs-hello.cabal new file mode 100644 index 0000000..4677b55 --- /dev/null +++ b/ghcjs-nodejs-hello.cabal @@ -0,0 +1,27 @@ +-- Initial ghcjs-nodejs-hello.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/ + +name: ghcjs-node-minimal +version: 0.1.0.0 +synopsis: Demonstrates a minimal code to produce a working (JavaScript only) + nodejs application, also showing off how to depend on other nodejs + modules. +-- description: +homepage: github +license: GPL-3 +license-file: LICENSE +author: Martin Vlk +maintainer: martin@vlkk.cz +-- copyright: +category: Development +build-type: Simple +-- extra-source-files: +cabal-version: >=1.10 + +executable ghcjs-node-minimal + main-is: Main.hs + -- other-modules: + -- other-extensions: + build-depends: base >=4.8 && <4.9 + hs-source-dirs: src + default-language: Haskell2010 diff --git a/src/Main.hs b/src/Main.hs new file mode 100644 index 0000000..f0441be --- /dev/null +++ b/src/Main.hs @@ -0,0 +1,16 @@ +{-# LANGUAGE JavaScriptFFI, + OverloadedStrings #-} + +module Main (main) where + +import GHCJS.Types +import GHCJS.Foreign + +foreign import javascript unsafe "require($1)" + require :: JSString -> IO (JSRef a) +foreign import javascript unsafe "$1.hostname()" + hostname :: JSRef a -> IO JSString + +main :: IO () +main = require "os" >>= hostname + >>= \h -> putStrLn $ "Our hostname is '" ++ (fromJSString h) ++ "'"