Class: ExecJS::RubyRhinoRuntime::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/execjs/ruby_rhino_runtime.rb

Instance Method Summary collapse

Constructor Details

#initialize(source = "") ⇒ Context

Returns a new instance of Context.



4
5
6
7
8
9
10
# File 'lib/execjs/ruby_rhino_runtime.rb', line 4

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



34
35
36
37
38
39
40
41
42
# File 'lib/execjs/ruby_rhino_runtime.rb', line 34

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

#eval(source, options = {}) ⇒ Object



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

def eval(source, options = {})
  source = source.encode('UTF-8') if source.respond_to?(:encode)

  if /\S/ =~ source
    unbox @rhino_context.eval("(#{source})")
  end
rescue ::Rhino::JavascriptError => e
  if e.message == "syntax error"
    raise RuntimeError, e.message
  else
    raise ProgramError, e.message
  end
end

#exec(source, options = {}) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/execjs/ruby_rhino_runtime.rb', line 12

def exec(source, options = {})
  source = source.encode('UTF-8') if source.respond_to?(:encode)

  if /\S/ =~ source
    eval "(function(){#{source}})()", options
  end
end

#unbox(value) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/execjs/ruby_rhino_runtime.rb', line 44

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