Class: PAC::Runtimes::RubyRacerRuntime::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/pac/runtimes/rubyracer.rb

Instance Method Summary collapse

Constructor Details

#initialize(source = "") ⇒ Context

Returns a new instance of Context.



5
6
7
8
9
10
11
12
# File 'lib/pac/runtimes/rubyracer.rb', line 5

def initialize(source = "")
  source = source.encode("UTF-8") if source.respond_to?(:encode)

  lock do
    @v8_context = V8::Context.new
    @v8_context.eval(source)
  end
end

Instance Method Details

#call(properties, *args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pac/runtimes/rubyracer.rb', line 20

def call(properties, *args)
  lock do
    begin
      unbox @v8_context.eval(properties).call(*args)
    rescue ::V8::JSError => e
      if e.value["name"] == "SyntaxError"
        raise RuntimeError, e.message
      else
        raise ProgramError, e.message
      end
    end
  end
end

#include(mod) ⇒ Object



14
15
16
17
18
# File 'lib/pac/runtimes/rubyracer.rb', line 14

def include(mod)
  (mod.methods - Module.methods).each do |name|
    @v8_context[name] = mod.method(name)
  end
end

#unbox(value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pac/runtimes/rubyracer.rb', line 34

def unbox(value)
  case value
  when ::V8::Function
    nil
  when ::V8::Array
    value.map { |v| unbox(v) }
  when ::V8::Object
    value.inject({}) do |vs, (k, v)|
      vs[k] = unbox(v) unless v.is_a?(::V8::Function)
      vs
    end
  when String
    value.respond_to?(:force_encoding) ? value.force_encoding("UTF-8") : value
  else
    value
  end
end