hruby-0.3.8: Embed a Ruby intepreter in your Haskell program !

Safe HaskellNone
LanguageHaskell2010

Foreign.Ruby.Safe

Contents

Description

This modules materializes the ruby interpreters as the RubyInterpreter data type. All the calls using these APIs are garanteed to run in the OS thread that the interpreter expects.

Synopsis

Initialization and finalization

startRubyInterpreter :: IO RubyInterpreter Source #

Initializes a Ruby interpreter. This should only be called once. It actually runs an internal server in a dedicated OS thread.

closeRubyInterpreter :: RubyInterpreter -> IO () Source #

This will shut the internal server down.

withRubyInterpreter :: (RubyInterpreter -> IO a) -> IO a Source #

This is basically :

bracket startRubyInterpreter closeRubyInterpreter

Data types

type RValue = Ptr CULong Source #

This is the type of Ruby values. It is defined as a pointer to some unsigned long, just like Ruby does. The actual value is either pointed to, or encoded in the pointer.

data RubyInterpreter Source #

This is actually a newtype around a TQueue.

Safe variants of other funtions

embedHaskellValue :: RubyInterpreter -> a -> IO (Either RubyError RValue) Source #

This transforms any Haskell value into a Ruby big integer encoding the address of the corresponding StablePtr. This is useful when you want to pass such values to a Ruby program that will call Haskell functions.

This is probably a bad idea to do this. The use case is for calling Haskell functions from Ruby, using values generated from the Haskell world. If your main program is in Haskell, you should probably wrap a function partially applied with the value you would want to embed.

safeMethodCall :: RubyInterpreter -> String -> String -> [RValue] -> IO (Either RubyError RValue) Source #

A safe version of the corresponding Foreign.Ruby.Helper function.

safeFunCall :: RubyInterpreter -> RValue -> String -> [RValue] -> IO (Either RubyError RValue) Source #

A safe version of the corresponding Foreign.Ruby.Helper function.

makeSafe :: RubyInterpreter -> IO a -> IO (Either RubyError a) Source #

Runs an arbitrary computation in the Ruby interpreter thread. This is useful if you want to embed calls from lower level functions. You still need to be careful about the GC's behavior.

fromRuby :: FromRuby a => RubyInterpreter -> RValue -> IO (Either RubyError a) Source #

Converts a Ruby value to some Haskell type..

toRuby :: ToRuby a => RubyInterpreter -> a -> IO (Either RubyError RValue) Source #

Insert a value in the Ruby runtime. You must always use such a function and the resulting RValue ina freezeGC call.

freezeGC :: RubyInterpreter -> IO a -> IO a Source #

Runs a computation with the Ruby GC disabled. Once the computation is over, GC will be re-enabled and the startGC function run.

Wrapping Haskell function and registering them

type RubyFunction1 = RValue -> IO RValue Source #

All those function types can be used to register functions to the Ruby runtime. Please note that the first argument is always set (it is "self"). For this reason, there is no RubyFunction0 type.