MetaRuby

Posted by kev Sat, 15 Oct 2005 02:54:00 GMT

Eric Hodel

Once upon a time…

  • Eric and Ryan were hacking some Ruby related C
  • And it sucked
  • *

MetaRuby

  • Will implement Ruby in Ruby
    • Core libarireis, parser, interpeter

MetaRuby Architecture

  • Parser
  • Interpeter
  • GC

Why?

  • Writing Ruby internals in C requries mental context switch every time you change bwetween RUby and C

Example of C code vs Ruby code.

  • More Familiar
  • More approachable
  • Less to do
    • No NULL termination
    • No tainting or freezing

Inspirationsal Projects

  • Sqeak Smalltalk
  • Self
  • Pascal, Modula-2, Oberon by Wirth
  • All of these are written in themselves

Related Projects

  • Matju’s MetaRuby
  • YARV
  • JRuby

Matju’s MetaRuby

  • Different goal much more complex
  • Abstracted core classes

YARV

  • Ruby interpreter replacement

Rubidium

  • Ruby interpreter replacement
  • Rubidium is an optimizing Ruby interpreter

Rubytests

  • Unit tests for Ruby
  • Not comprehensive enough for our goals
  • Not much work making it more complex

JRuby

  • A 1.8.2 compatible Ruby interpreter
  • Most builtin Ruby classes provided
  • Support for interfacing and defining Java classes in Ruby
  • Uses Rubytests

Current Work

Methodology

  • Generate a stubbed class to overlay
  • Drive unit tests to failure
    • Identify core methods (primitives) that have to exist
    • Fix bad tests that pass despite no implementation
  • Drive all tests to green
    • Hack, hack, hack

Passing Tests

  • TrueClass, FalseClass and NilClass
  • Time
  • Range
  • NilClass
  • Array
  • String

Overlaid Classes

These classes overlay their core classes using Ruby’s C allocation and initialization methods replacing as many methods as possible

  • TrueClass
  • NilClass
  • Array
  • String

Replaced Classes

  • Time
  • Range
  • Hash

Rubytests

  • Stale
    • Mostly tests Ruby 1.6 language features
  • Low test coverage
  • Not fully converted to Test::Unit
    • Way too much code from pre-testunit

Test::Unit

  • Needs lots of methods to work
  • Too complicated to refactor
  • Working on core classes is hard

Future Work

Primitives

  • Will be automatically translated to C
  • What is a primitive?
    • Implement as much as possible in Ruby
    • Whatever is left becomes a primitive ** Unless we can break it down
  • Choosing primitives is a discovery process

Ruby2c Translation

  • Ryan will cover this a lot more
  • Only necessary for primitives

Memory Allocation (Objects)

  • Currently Array and String sit on top of C Ruby
  • Write object allocation in pure Ruby using current memory system for all objects
  • Then we will replace the memory system with a pure Ruby system

Replace core ruby library

  • Works!
  • Well.. kind of..
  • Compiles
  • Links!
  • Segfaults!
  • Needs alot of ping pong

Far Future Work

h5. The Groveling Commences

Parser

  • Ripper is our best target
  • Almost entirely Ruby already
  • Just one file is in C, which we can rewrite

Object System & Garbage Collector

  • Steal ideas from Sqeuak Smalltalk, Self, current Ruby
  • In theory it should be easy to do
  • In reality it will be hard to do well
  • We’d love someone to work on this

Interpreter

  • YARV or eval.c (Ruby 1.8)?
  • Rubidium?
  • Needs to we written in Ruby
  • We’d love someone to work on this

C Extensions & C Standard Library

  • Why are you writing pure C anyways?
    • Use RubyInline or DL
  • Probably need Ruby/C compatability stubs
    • Easy to generate
  • Will need to follow current Ruby/C naming conventions

Array#fill

Eight ways to call

  • array.fill(obj)
  • array.fill(obj, start[, length])
  • array.fill(obj.range)
  • array.fill {|index| block }
  • array.fill(start…

“foo”.sub(/f(o)o/) { $1 }

  • $1 is a “magick” read only global
  • $1 can’t be set from pure Ruby
  • So the interpreter needs to help us out
  • Applies to all match variables

String#split

  • Easy
  • “a b”.split # => [‘a’, ‘b’]
  • “a|b”.split # => [
  • “a1b”.split(/*\d)/) # => [‘a’,’1’,’c’]

  • Hard

Time.rb Needs Metal

  • Easy
    • the_time.month
    • thetime.tof
    • etc
  • Hard
    • Time.now requries calling libc’s gettime method
    • Currently we have libcwrap.rb that uses RubyInline to call into C funcitons

2 comments | 2 trackbacks

Comments

  1. Avatar Jordan said 26 minutes later:

    Good stuff Kev.

  2. Avatar Ulysses said about 2 hours later:

    OH MY GOD THAT IS RADICAL!!!

    DID YOU KNOW YOU CAN ASSIGN TO $~ WHICH SETS $1

Trackbacks

Use the following link to trackback from your own site:
http://glu.ttono.us/articles/trackback/17

  1. From solar hydrogen fuel cell
    solar hydrogen fuel cell
    solar hydrogen fuel cell
  2. From arcelor
    arcelor
    arcelor

Comments are disabled