README.txt

Path: README.txt
Last Update: Wed May 07 13:57:21 +0200 2008
dot/f_0.png

weave

DESCRIPTION:

weave is "live native monkey patching", it‘s

  • just for fun
  • proof of concept
  • an educational tool
  • something to wow people with

So what is live native monkey patching? It‘s basically a Ruby Native wrapper of Ruby‘s native API that allows you to construct objects in Ruby just like you would construct them in C.

FEATURES/PROBLEMS:

Weave has hardly any features and many problems

  • Feature: Load Library using `dlopen`
  • Feature: Load Symbols using `dlsym`
  • Feature: Provide access to native `rb_define_method`, `rb_define_singleton` C functions from within Ruby.
  • Problem: not tested on a wide range of plattforms
  • Problem: does Windows even have `dlopen`
  • Problem: not properly packaged as a gem
  • Problem: can‘t get rdoc to work properly (may be an rdoc bug…)

SYNOPSIS:

Excursion: writing Ruby native extensions in a thimble for people with very small fingers.

to hook up some native (aka C) functionality with Ruby (in MRI) you need to write your native code in a C function with a signature like:

    static VALUE my_function (VALUE self, VALUE param) {
        /* do stuff here */
    }

then you can attach the function to a ruby class using:

    rb_define_method(rb_class, "my_method", my_function, 1)

and almost as if by magic, the C `my_function` is now available as a ruby method of whatever class you attached it to.

As you may imagine, there are a few nuances I didn‘t cover, but you can read all about those in the README.EXT file that‘s included in the Ruby distribution or check the Pickaxe book, which contains a chapter on writing native extensions here: www.rubycentral.com/pickaxe/ext_ruby.html

END OF EXCURSION

`weave` hooks up the native C functions for hooking up native C functions and provides access to them from within Ruby. So, let‘s say for example you have a C function that does (nearly) nothing:

    VALUE do_nearly_nothing (VALUE self) {
        return self;
    }

And have compiled it into a library called `lib_do_nearly_nothing.so`. You can now hook up that function as a regular Ruby method from within Ruby. Like so:

    require 'weave'
    lib = Weave::Library.new "lib_do_nearly_nothing.so"
    sym = lib.symbol "do_nearly_nothing"

This loaded the library and located the function within the library. Next, we can hook this function up to an existing class, for example to REALLY optimise, eh, speed up `Array.sort` .…

    old_sort = Weave.rb_define_method Array, "sort", sym, 0

Let‘s have look if it worked:

      >> arr = [3,2,1]
      => [3, 2, 1]
      >> arr.sort
      => [3, 2, 1]
      >> arr.sort == arr
      => true

It worked! `rb_define_method` returns the previous definition, in case the method was already defined. This way, if it turns out the optimization wasn‘t such a good idea. So, if we want to change it back:

    new_sort = Weave.rb_define_method Array, "sort", old_sort, 0

Just to make sure:

      >> arr.sort
      => [1, 2, 3]
      >> arr.sort == arr
      => false

REQUIREMENTS:

  • I tried out `newgem` to create the infrastructure for this project, but I didn‘t realize that `newgem` is a runtime dependancy :( `newgem` also has a bunch of dependancies of it‘s own …
  • You‘ll need a C compiler and a development environment with make and friends. You‘ll also want a source distribution of Ruby 1.8.x somewhere for reference.

INSTALL:

LICENSE:

(The MIT License)

Copyright (c) 2008 Tim Becker (tim.becker@gmx.net)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

[Validate]