eigenclass logo
MAIN  Index  Search  Changes  PageRank  Login

rocaml-0.6.1: fast, easy Ruby extensions in Objective Caml

rocaml: fast, easy Ruby extensions in Objective Caml allows you to write Ruby extensions in Objective Caml.

rocaml 0.6.1 fixes a couple bugs in 0.6.0 involving camlp[45] and compiler detection. Thanks to Daniel Berger.

Developing Ruby extensions with rocaml is easier and more convenient than writing a plain old C extension because rocaml performs Ruby<->OCaml conversions for a wide range of types, including abstract types and arrays, tuples, variants and records of values of any supported type (e.g. arrays of arrays of variants of tuples of ...). Moreover, exceptions raised in the OCaml code are captured by the generated extension and raised inside Ruby.

Making an extension with rocaml involves two steps:

  • implementing the desired functionality in Objective Caml, and registering the functions to be exported (using Callback.register : string -> 'a -> unit or the included camlp4 extension)
  • creating the extconf.rb file (just modify the sample extconf.rb distributed with rocaml) defining the interface of your Objective Caml code.

At no point is there any need to write a single line of C code when using rocaml.

The mandatory trivial example

This example doesn't do justice to the usefulness of rocaml because the extension is beyond trivial and you could as well have written it in C using RubyInline. The advantages of rocaml (and of Objective Caml) usually become visible when the extension takes more than two lines (take a look at the 3-line Marshal replacement that is 3 times faster than Ruby's, though...). Here follows a minimal example however, merely to show how easily rocaml extensions can be made.

Here's the OCaml code placed in fib.ml:

let rec fib n = if n < 2 then 1 else fib (n-1) + fib (n-2)
export fib

Here's the interface declaration in your extconf.rb:

Interface.generate("fib") do
  def_module("Fib") do
    fun "fib", INT => INT

That's it. The extension can be built like any run-of-the-mill C extension with

 ruby extconf.rb

The resulting Ruby extension that can be used as usual:

 require 'fib'
 p Fib.fib 10


You can get rocaml here.

Sweet! - Tuxie (2007-10-17 (Wed) 17:45:00)

rocaml is damn cool!


Mac problem with setreuid? - tony (2007-10-20 (Sam) 03:52:50)

Very interesting, but does not seem to work out of the box on Mac OS X.

My system config:

  • Mac OS X 10.4.10, build version 8R218
  • 8.10.0 Darwin Kernel Version
  • Ruby 1.8.6, 2007-03-13 patchlevel 0, powerpc-darwin8.9.0
  • OCaml 3.10.0
  • /usr/local/bin/ocaml
  • /usr/local/lib/ocaml/libasmrun.a
  • /usr/local/lib/ocaml/caml/

Here's what i did:

  • cd ~/Desktop/rocaml-0.6.1/test/
  • sudo cp ~/Desktop/rocaml-0.6.1/rocaml_extconf.rb ~/Desktop/rocaml-0.6.1/rocaml.rb ~/Desktop/rocaml-0.6.1/test
  • sudo ln -s /usr/local/lib/ocaml/caml ~/Desktop/rocaml-0.6.1/test
  • sudo ruby extconf.rb
  • optional: sudo mv ~/Desktop/rocaml-0.6.1/test/depend ~/Desktop/rocaml-0.6.1/test/.depend
  • sudo make


$ sudo make

Make218: .depend: No such file or directory

ocamldep.opt conversions.ml rubyOCamlUtil.ml > .depend

gcc -I. -I/usr/local/lib/ruby/1.8/powerpc-darwin8.9.0 -I/usr/local/lib/ruby/1.8/powerpc-darwin8.9.0 -I. -fno-common -g -O2 -fno-common -pipe -fno-common -c rocaml_tests_rocaml_wrapper.c

ocamlc -c -I +camlp4 -pp "camlp4orf -loc _loc" pa_rocaml.ml

ocamlopt.opt -c -pp 'camlp4o -I . pa_rocaml.cmo' conversions.ml

ocamlopt.opt -c -pp 'camlp4o -I . pa_rocaml.cmo' rubyOCamlUtil.ml

ocamlopt.opt -output-obj -o rocaml_tests_rocaml_runtime.o nums.cmxa conversions.cmx rubyOCamlUtil.cmx conversions.o rubyOCamlUtil.o

cc -dynamic -bundle -undefined suppress -flat_namespace -L"/usr/local/lib" -o rocaml_tests.bundle rocaml_tests_rocaml_wrapper.o rocaml_tests_rocaml_runtime.o /usr/local/lib/ocaml/libasmrun.a /usr/local/lib/ocaml/libunix.a /usr/local/lib/ocaml/libnums.a -lruby -lpthread -ldl -lobjc

/usr/bin/ld: warning multiple definitions of symbol _setregid

/usr/local/lib/libruby.dylib(process.o) definition of _setregid

/usr/lib/gcc/powerpc-apple-darwin8/4.0.1/../../../libpthread.dylib(setregid.So) definition of _setregid

/usr/bin/ld: warning multiple definitions of symbol _setreuid

/usr/local/lib/libruby.dylib(process.o) definition of _setreuid

/usr/lib/gcc/powerpc-apple-darwin8/4.0.1/../../../libpthread.dylib(setreuid.So) definition of _setreuid

/usr/bin/ld: rocaml_tests_rocaml_runtime.o has local relocation entries in non-writable section (__TEXT,__text)

collect2: ld returned 1 exit status

make: *** [rocaml_tests.bundle] Error 1


Seems to be a problem with setreuid, http://www.ruby-forum.com/topic/78991 .

Any ideas how to fix this?


mfp 2007-10-20 (Sat) 08:01:24

It's never been tested on OSX; AFAIK you're the first to try. I'll try to set up an OSX environment and see what can be done.

The _setreuid thing doesn't seem too serious; I'm rather concerned about

/usr/bin/ld: rocaml_tests_rocaml_runtime.o has local relocation entries in non-writable section (__TEXT,__text)

This means that the linker is having problems with the non-PIC code generated by ocamlopt. This could cause serious trouble until the natdynlink OCaml branch is mature (leaving static extensions and maybe bytecode OCaml as the only choices on OSX for a while).


Use this form to create a new top-level comment; for direct replies to existing comments, use the text entries you'll find below.


Last modified:2007/10/17 15:43:30
Keyword(s):[ruby] [ocaml] [rocaml] [release] [0.6.0] [frontpage] [blog]