-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
push a working LibArith example to the repo
- Loading branch information
Showing
4 changed files
with
63 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,19 @@ | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
|
||
module LibArith where | ||
|
||
import Data.Maybe | ||
import Foreign.C.Types | ||
|
||
import Scripting.Lua | ||
import Scripting.Lua.Raw | ||
|
||
foreign export ccall | ||
add :: LuaState -> IO CInt | ||
|
||
add :: LuaState -> IO CInt | ||
add l = do | ||
i1 :: LuaNumber <- fromJust `fmap` peek l 1 | ||
i2 :: LuaNumber <- fromJust `fmap` peek l 2 | ||
push l (i1 + i2 :: LuaNumber) | ||
return 1 |
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,13 @@ | ||
ALL: libarith.so lualibhelper.so | ||
|
||
libarith.so: LibArith.hs | ||
ghc $^ -o $@ -shared -fPIC -dynamic -lHSrts-ghc7.10.1 | ||
|
||
lualibhelper.so: libarith.so libarithhelper.c | ||
ghc libarithhelper.c -no-hs-main -o $@ -shared -fPIC -dynamic -lHSrts-ghc7.10.1 -L. -larith -I../../lua-5.1.5 | ||
|
||
clean: | ||
rm -f *.hi | ||
rm -f *.o | ||
rm -f *.so | ||
rm -f *_stub.h |
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,25 @@ | ||
#include "LibArith_stub.h" | ||
#include "lua.h" | ||
|
||
int hs_init_lua(lua_State *L) | ||
{ | ||
hs_init(NULL, NULL); | ||
return 0; | ||
} | ||
|
||
int hs_exit_lua(lua_State *L) | ||
{ | ||
hs_exit(); | ||
return 0; | ||
} | ||
|
||
int luaopen_lualibhelper(lua_State *L) | ||
{ | ||
lua_pushcfunction(L, add); | ||
lua_setglobal(L, "add_in_haskell"); | ||
lua_pushcfunction(L, hs_init_lua); | ||
lua_setglobal(L, "hs_init"); | ||
lua_pushcfunction(L, hs_exit_lua); | ||
lua_setglobal(L, "hs_exit"); | ||
return 0; | ||
} |
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,6 @@ | ||
require "lualibhelper" | ||
|
||
hs_init() | ||
print(add_in_haskell(1, 2)) | ||
print(add_in_haskell(-10, 20)) | ||
hs_exit() |