Class: RVM::Library

Inherits:
Object
  • Object
show all
Defined in:
lib/rvm/library.rb

Overview

The Library class is made to provide library loading capabilites using any rVM supporte langauges and build a frontend between the rVM code and the Ruby code

A Library can hold the content of more then one, file and the loaded files are not required to have the same langauge

Constant Summary collapse

CLASS_MAP =

This maps rVM classes to usual Ruby classes so you won’t need to take care of it

{
  String => RVM::Classes::String,
  Fixnum => RVM::Classes::Number,
  Bignum => RVM::Classes::Number,
  Float => RVM::Classes::Number,
  FalseClass => RVM::Classes::Boolean,
  TrueClass => RVM::Classes::Boolean,
  Array => RVM::Classes::List
}

Instance Method Summary collapse

Constructor Details

#initialize(language, file = nil, safety = RVM::Safety.new) ⇒ Library

Initializes library, if anguage and file are given, they are loaded Optionally a saftey object can be passed to handle securing the interpreted code



25
26
27
28
29
30
# File 'lib/rvm/library.rb', line 25

def initialize(language, file = nil, safety = RVM::Safety.new)
  @lang = RVM::Languages[language].new
  @env = RVM::Interpreter.env #@lang.env
  @safety = safety
  load_library(file) if file
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

This is the most important part, so to say, the ruby magic in here If a unknown method is called this gets executed to try to resole it form the loaded libraires

Priorities are:

1) check the functions
2) check if it was a variable
3) check if the call ended with a '=' then set it's as variable
4) call super


66
67
68
69
70
71
72
# File 'lib/rvm/library.rb', line 66

def method_missing(m, *args)
  begin
    call_function(m, args)
  rescue Exception => e
    super(m, args)
  end
end

Instance Method Details

#call_function(name, *args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rvm/library.rb', line 37

def call_function(name, *args)
  # First we convert the args
  vmargs = args.map do |arg|
    if c = CLASS_MAP[arg.class]
      c.new(arg)
    else
      arg
    end
  end
  n = name.to_s
  if fun = @env.function(n)
    fun.call(vmargs, @env)
  elsif v = @env[n]
    v.val
  elsif n =~ /=$/
    @env[n.gsub(/=$/,'')] = vmargs.first
  else
    raise "oops!"
  end
end

#load_library(file) ⇒ Object

Loads a file into the Library, attention it is executed!



33
34
35
# File 'lib/rvm/library.rb', line 33

def load_library(file)
  @safety.execute(@lang.compile(File.read(file)),@env)
end