Class: PAC::Runtimes::RubyRhinoRuntime::Context

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

Instance Method Summary collapse

Constructor Details

#initialize(source = "") ⇒ Context

Returns a new instance of Context.



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

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

  @rhino_context = Rhino::Context.new
  fix_memory_limit! @rhino_context
  @rhino_context.eval(source)
end

Instance Method Details

#call(properties, *args) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/pac/runtimes/rubyrhino.rb', line 19

def call(properties, *args)
  unbox @rhino_context.eval(properties).call(*args)
rescue ::Rhino::JavascriptError => e
  if e.message == "syntax error"
    raise RuntimeError, e.message
  else
    raise ProgramError, e.message
  end
end

#include(mod) ⇒ Object



13
14
15
16
17
# File 'lib/pac/runtimes/rubyrhino.rb', line 13

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

#unbox(value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pac/runtimes/rubyrhino.rb', line 29

def unbox(value)
  case value = ::Rhino::To.ruby(value)
  when ::Rhino::NativeFunction
    nil
  when ::Rhino::NativeObject
    value.inject({}) do |vs, (k, v)|
      case v
      when ::Rhino::NativeFunction, ::Rhino::J::Function
        nil
      else
        vs[k] = unbox(v)
      end
      vs
    end
  when Array
    value.map { |v| unbox(v) }
  else
    value
  end
end