{-# LANGUAGE OverloadedStrings, OverloadedStrings #-}

module Main where

import Foreign.Ruby.Bindings

import Data.Aeson

import Data.Attoparsec.Number

-- this is an external function that will be executed from the Ruby interpreter

-- the first parameter to the function is probably some reference to some top object

-- my knowledge of ruby is close to nonexistent, so I can't say for sure ...

extfunc :: RValue -> RValue -> IO RValue

extfunc _ v = do

-- deserialize the Ruby value into some JSON Value

onv <- fromRuby v :: IO ( Maybe Value )

-- and display it

print onv

-- now let's create a JSON object containing all kind of data types

let nv = object [ ( "bigint" , Number ( I 16518656116889898998656112323135664684684 ))

, ( "int" , Number ( I 12 ))

, ( "double" , Number ( D 0.123 ))

, ( "null" , "Null" )

, ( "string" , String "string" )

, ( "true" , Bool True )

, ( "false" , Bool False )

, ( "array" , toJSON ([ 1 , 2 , 3 , 4 , 5 ] :: [ Int ]))

, ( "object" , object [ ( "k" , String "v" ) ] )

]

-- turn it into Ruby values, and return this

toRuby nv

-- this is the function that is called if everything was loaded properly

nextThings :: IO ()

nextThings = do

-- turn the extfunc function into something that can be called by the Ruby interpreter

myfunc <- mkRegistered2 extfunc

-- and bind it to the global 'hsfunction' function

rb_define_global_function "hsfunction" myfunc 1

-- now call a method in the Ruby interpreter

o <- safeMethodCall "MyClass" "testfunc" []

case o of

Right v -> ( fromRuby v :: IO ( Maybe Value )) >>= print

Left r -> putStrLn r

main :: IO ()

main = do

-- initialize stuff

ruby_init

ruby_init_loadpath

-- and load "test.rb"

s <- rb_load_protect "test.rb" 0

if s == 0

then nextThings