Class: ExecJS::RubyRhinoRuntime::Context

Inherits:
ExecJS::Runtime::Context show all
Defined in:
lib/execjs/ruby_rhino_runtime.rb

Instance Method Summary collapse

Methods included from Encoding

#encode

Constructor Details

#initialize(runtime, source = "", options = {}) ⇒ Context

Returns a new instance of Context.



6
7
8
9
10
11
12
13
14
# File 'lib/execjs/ruby_rhino_runtime.rb', line 6

def initialize(runtime, source = "", options = {})
  source = encode(source)

  @rhino_context = ::Rhino::Context.new
  fix_memory_limit! @rhino_context
  @rhino_context.eval(source)
rescue Exception => e
  raise wrap_error(e)
end

Instance Method Details

#call(properties, *args) ⇒ Object



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

def call(properties, *args)
  unbox @rhino_context.eval(properties).call(*args)
rescue Exception => e
  raise wrap_error(e)
end

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



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

def eval(source, options = {})
  source = encode(source)

  if /\S/ =~ source
    unbox @rhino_context.eval("(#{source})")
  end
rescue Exception => e
  raise wrap_error(e)
end

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



16
17
18
19
20
21
22
# File 'lib/execjs/ruby_rhino_runtime.rb', line 16

def exec(source, options = {})
  source = encode(source)

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

#unbox(value) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/execjs/ruby_rhino_runtime.rb', line 40

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

#wrap_error(e) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/execjs/ruby_rhino_runtime.rb', line 61

def wrap_error(e)
  return e unless e.is_a?(::Rhino::JSError)

  error_class = e.message == "syntax error" ? RuntimeError : ProgramError

  stack = e.backtrace
  stack = stack.map { |line| line.sub(" at ", "").sub("<eval>", "(execjs)").strip }
  stack.unshift("(execjs):1") if e.javascript_backtrace.empty?

  error = error_class.new(e.value.to_s)
  error.set_backtrace(stack)
  error
end