-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
75 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*~ | ||
.#* | ||
\#*# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import Distribution.Simple | ||
main = defaultMain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ++ "'" |