Skip to content

Commit

Permalink
push a working LibArith example to the repo
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Oct 5, 2015
1 parent b20c63a commit a55b75a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
19 changes: 19 additions & 0 deletions examples/lualib_in_haskell/LibArith.hs
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
13 changes: 13 additions & 0 deletions examples/lualib_in_haskell/Makefile
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
25 changes: 25 additions & 0 deletions examples/lualib_in_haskell/libarithhelper.c
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;
}
6 changes: 6 additions & 0 deletions examples/lualib_in_haskell/main.lua
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()

0 comments on commit a55b75a

Please sign in to comment.