Skip to content

Commit

Permalink
Import files.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvlk committed Jun 21, 2015
1 parent b736f09 commit e3ccf15
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
21 changes: 6 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
*~
.#*
\#*#
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
27 changes: 27 additions & 0 deletions ghcjs-nodejs-hello.cabal
Original file line number Diff line number Diff line change
@@ -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: [email protected]
-- 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
16 changes: 16 additions & 0 deletions src/Main.hs
Original file line number Diff line number Diff line change
@@ -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) ++ "'"

0 comments on commit e3ccf15

Please sign in to comment.