Class: ProxyPacRb::RubyRhinoRuntime::Context

Inherits:
ProxyPacRb::Runtime::Context show all
Defined in:
lib/proxy_pac_rb/runtimes/rubyrhino.rb

Overview

Context

Instance Attribute Summary

Attributes inherited from ProxyPacRb::Runtime::Context

#context

Instance Method Summary collapse

Methods inherited from ProxyPacRb::Runtime::Context

#include

Methods included from Encoding

#encode

Constructor Details

#initialize(_runtime, source = '') ⇒ Context

Returns a new instance of Context.



7
8
9
10
11
12
13
# File 'lib/proxy_pac_rb/runtimes/rubyrhino.rb', line 7

def initialize(_runtime, source = '')
  source = encode(source)

  self.context = ::Rhino::Context.new
  fix_memory_limit! context
  context.eval(source)
end

Instance Method Details

#call(properties, *args) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/proxy_pac_rb/runtimes/rubyrhino.rb', line 37

def call(properties, *args)
  unbox context.eval(properties).call(*args)
rescue ::Rhino::JSError => e
  if e.message == 'syntax error'
    raise e.message
  else
    raise ProgramError, e.message
  end
end

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



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/proxy_pac_rb/runtimes/rubyrhino.rb', line 25

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

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

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



15
16
17
18
19
20
21
22
23
# File 'lib/proxy_pac_rb/runtimes/rubyrhino.rb', line 15

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

  return nil unless /\S/ =~ source

  # rubocop:disable Lint/Eval
  eval "(function(){#{source}})()", options
  # rubocop:enable Lint/Eval
end

#unbox(value) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/proxy_pac_rb/runtimes/rubyrhino.rb', line 47

def unbox(value)
  case value = ::Rhino.to_ruby(value)
  when Java::OrgMozillaJavascript::NativeFunction
    nil
  when Java::OrgMozillaJavascript::NativeObject
    # rubocop:disable Style/EachWithObject
    value.reduce({}) do |vs, (k, v)|
      case v
      when Java::OrgMozillaJavascript::NativeFunction, ::Rhino::JS::Function
        nil
      else
        vs[k] = unbox(v)
      end
      vs
    end
    # rubocop:enable Style/EachWithObject
  when Array
    value.map { |v| unbox(v) }
  else
    value
  end
end