Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nh2 committed Jan 8, 2013
1 parent 1df2068 commit 656075a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Example.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{-# LANGUAGE ForeignFunctionInterface #-}

module Example where

import Foreign.C.Types

fibonacci :: Int -> Int
fibonacci n = fibs !! n
where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

fibonacci_hs :: CInt -> CInt
fibonacci_hs = fromIntegral . fibonacci . fromIntegral

foreign export ccall fibonacci_hs :: CInt -> CInt
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
GHC=ghc-6.12.1

libffi-example.so: Example.o wrapper.o Example_stub.o
$(GHC) -o $@ -shared -dynamic -fPIC $^ -lHSrts-ghc6.12.1

Example_stub.c Example_stub.h Example.o: Example.hs
$(GHC) -c -dynamic -fPIC Example.hs

Example_stub.o: Example_stub.c
$(GHC) -c -dynamic -fPIC Example_stub.c

wrapper.o: wrapper.c Example_stub.h
$(GHC) -c -dynamic -fPIC wrapper.c

clean:
rm -f *.hi *.o *_stub.[ch]

clean-all:
rm -f *.hi *.o *_stub.[ch] *.so
11 changes: 11 additions & 0 deletions program.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#program.py

from ctypes import *

lib = cdll.LoadLibrary("./libffi-example.so")

lib.example_init()
x=lib.fibonacci_hs(42)
print x
lib.example_exit()

15 changes: 15 additions & 0 deletions wrapper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdlib.h>

#include "HsFFI.h"

void
example_init (void)
{
hs_init (NULL, NULL);
}

void
example_exit (void)
{
hs_exit ();
}

0 comments on commit 656075a

Please sign in to comment.