-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial import from https://bitbucket.org/darq/haskellfrompython/
- Loading branch information
Showing
4 changed files
with
59 additions
and
0 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 |
---|---|---|
@@ -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 |
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,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 |
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,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() | ||
|
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,15 @@ | ||
#include <stdlib.h> | ||
|
||
#include "HsFFI.h" | ||
|
||
void | ||
example_init (void) | ||
{ | ||
hs_init (NULL, NULL); | ||
} | ||
|
||
void | ||
example_exit (void) | ||
{ | ||
hs_exit (); | ||
} |