Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/Neurogami/Hubris
Browse files Browse the repository at this point in the history
  • Loading branch information
mwotton committed Sep 29, 2009
2 parents 0965f3e + 8bd483f commit 91f5185
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 118 deletions.
24 changes: 22 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
.jhci-hist
# stupid emacs lock files
stupid emacs lock files
.#*
# jhc stuff
jhc stuff
hs.out_code.c
hs.out
examples/simple_inline/RubyMap.hs

examples/simple_inline/rshim.h
examples/simple_inline/stubs.c
examples/simple_inline/stubs.o
examples/simple_rack_app/stubs.c
examples/simple_rack_app/tmp.old/
examples/simple_rack_app/tmp/
pkg/
RubyMap.hi
RubyMap.hs
RubyMap.o
lib/RubyMap.chi
lib/RubyMap.chs.h
lib/RubyMap.hi
lib/RubyMap.hs
lib/RubyMap.o
lib/rshim.o
sample/tmp
sample/tmp.old
11 changes: 8 additions & 3 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ PostInstall.txt
README.markdown
Rakefile
bin/jhc_builder
bin/ghc_builder
bin-scripts/jhc_builder
bin-scripts/ghc_builder
lib/hubris.rb
lib/Mapper.hs
lib/rshim.c
lib/rshim.h
lib/RubyMap.hs
lib/RubyMap.chs
lib/RubyMap.chs.h
lib/hubris_constants.rb
sample/Makefile
sample/Test.hs
sample/hsload.rb
spec/Hubris_spec.rb
spec/spec.opts
spec/spec_helper.rb
tasks/rspec.rake
29 changes: 29 additions & 0 deletions bin-scripts/ghc_builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@


warn "Have ARGV #{ARGV.inspect}"

sh = %~
#!/bin/sh

# $1 is our source haskell
rm -rf tmp.old
mv tmp tmp.old
mkdir tmp
tmp="tmp/$1"
cat $1 >> $tmp
echo "main :: IO ()" >> $tmp
echo "main = return ()" >> $tmp

cd tmp
jhc -dc "$1"
sed -i 's/^main(/disregard_main(/' hs.out_code.c

# for linux
gcc '-std=gnu99' -D_GNU_SOURCE '-falign-functions=4' -ffast-math\
-Wshadow -Wextra -Wall -Wno-unused-parameter -o libdynhs.so\
hs.out_code.c -DNDEBUG -O3 -fPIC -shared '-D_JHC_STANDALONE=0'
# for mac
#gcc '-std=gnu99' -D_GNU_SOURCE '-falign-functions=4' -ffast-math\
# -Wshadow -Wextra -Wall -Wno-unused-parameter -o libdynhs.so\
# hs.out_code.c -DNDEBUG -O3 -fPIC -shared '-D_JHC_STANDALONE=0' -dynamiclib
#mv libdynhs.so ..
24 changes: 24 additions & 0 deletions bin-scripts/jhc_builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

# $1 is our source haskell
rm -rf tmp.old
mv tmp tmp.old
mkdir tmp
tmp="tmp/$1"
cat $1 >> $tmp
echo "main :: IO ()" >> $tmp
echo "main = return ()" >> $tmp

cd tmp
jhc -dc "$1"
sed -i 's/^main(/disregard_main(/' hs.out_code.c

# for linux
gcc '-std=gnu99' -D_GNU_SOURCE '-falign-functions=4' -ffast-math\
-Wshadow -Wextra -Wall -Wno-unused-parameter -o libdynhs.so\
hs.out_code.c -DNDEBUG -O3 -fPIC -shared '-D_JHC_STANDALONE=0'
for mac
#gcc '-std=gnu99' -D_GNU_SOURCE '-falign-functions=4' -ffast-math\
# -Wshadow -Wextra -Wall -Wno-unused-parameter -o libdynhs.so\
# hs.out_code.c -DNDEBUG -O3 -fPIC -shared '-D_JHC_STANDALONE=0' -dynamiclib
#mv libdynhs.so ..
2 changes: 2 additions & 0 deletions bin/ghc_builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
here = File.expand_path(File.dirname(__FILE__))
exec "sh #{here}/../bin-scripts/ghc_builder #{ARGV[0]}"
26 changes: 2 additions & 24 deletions bin/jhc_builder
Original file line number Diff line number Diff line change
@@ -1,24 +1,2 @@
#!/bin/sh

# $1 is our source haskell
rm -rf tmp.old
mv tmp tmp.old
mkdir tmp
tmp="tmp/$1"
cat $1 >> $tmp
echo "main :: IO ()" >> $tmp
echo "main = return ()" >> $tmp

cd tmp
jhc -dc "$1"
sed -i 's/^main(/disregard_main(/' hs.out_code.c

# for linux
#gcc '-std=gnu99' -D_GNU_SOURCE '-falign-functions=4' -ffast-math\
# -Wshadow -Wextra -Wall -Wno-unused-parameter -o libdynhs.so\
# hs.out_code.c -DNDEBUG -O3 -fPIC -shared '-D_JHC_STANDALONE=0'
# for mac
gcc '-std=gnu99' -D_GNU_SOURCE '-falign-functions=4' -ffast-math\
-Wshadow -Wextra -Wall -Wno-unused-parameter -o libdynhs.so\
hs.out_code.c -DNDEBUG -O3 -fPIC -shared '-D_JHC_STANDALONE=0' -dynamiclib
mv libdynhs.so ..
here = File.expand_path(File.dirname(__FILE__))
exec "sh #{here}/../bin-scripts/jhc_builder #{ARGV[0]}"
5 changes: 5 additions & 0 deletions examples/simple_inline/clean_and_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env sh
# Delete existing binaries to see that the lib correctly recreates them
clear;
rm -rf ~/.hubris_cache/*; rm -rf *.o; rm -rf *.hi; rm -rf *.c;
ruby haskell_math.rb
29 changes: 29 additions & 0 deletions examples/simple_inline/haskell_math.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/local/bin/ruby

here = File.expand_path( File.dirname(__FILE__))
$:.unshift here + '/../../lib'
require 'hubris'

#Hubris.ruby_header = '/home/james/data/vendor/ruby-1.8.6-p383/'
#Hubris.ghc_cmd = 'ghc'
#Hubris.ghc_version = '6.11.20090913'

class Target

include Hubris

def negate(i)
return -i
end
end

t = Target.new

t.inline "mydouble (T_FIXNUM i) = T_FIXNUM (i + i)
my_double _ = T_NIL", :no_strict => true

#t.inline "mytriple (T_FIXNUM i) = T_FIXNUM (i * 3)
# mytriple _ = T_NIL", :no_strict => true
puts t.negate(3)
puts t.mydouble(2)
# puts t.mytriple(8)
28 changes: 28 additions & 0 deletions examples/simple_rack_app/Test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{-# LANGUAGE ForeignFunctionInterface #-}

-- module Test where

import Foreign.C.Types
-- import Data.Map
import Maybe

-- main = putStrLn "11"

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

-- local_map = Data.Map.fromList [(1,2), (3,4)]



-- lookup_hs ::CInt -> CInt
-- lookup_hs = fromIntegral . Maybe.fromJust . ((flip Data.Map.lookup) local_map) . fromIntegral
-- foreign export ccall lookup_hs :: CInt -> CInt

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

foreign export ccall fibonacci_hs :: CInt -> CInt

-- main = putStrLn "foo"
24 changes: 24 additions & 0 deletions examples/simple_rack_app/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#\ -w -p 8765
use Rack::Reloader, 0
use Rack::ContentLength

require 'pp'
require 'dl/import'

module HaskyPants
extend DL::Importable
dlload "./libdynhs.so"
extern "int fibonacci_hs(int)"
end


def arg_from env
env['REQUEST_URI'] ? env['REQUEST_URI'].to_s.sub(/^\//, '').to_i : 0
end

app = proc do |env|
value = HaskyPants.fibonacci_hs( arg_from env )
[ 200, {'Content-Type' => 'text/plain'}, "The fib number is #{value }" ]
end

run app
16 changes: 16 additions & 0 deletions examples/simple_rack_app/hsload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'dl/import'

module HaskyPants
if /^1\.8/ =~ RUBY_VERSION
extend DL::Importable
else
extend DL::Importer
end

dlload "./libdynhs.so"
["int fibonacci_hs(int)"].each do |f|
extern f
end
end

puts HaskyPants.fibonacci_hs(12)
Loading

0 comments on commit 91f5185

Please sign in to comment.