Class: ProxyPacRb::RubyRacerRuntime::Context

Inherits:
ProxyPacRb::Runtime::Context show all
Defined in:
lib/proxy_pac_rb/runtimes/rubyracer.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 = '', _environment = nil) ⇒ Context

Returns a new instance of Context.



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

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

  lock do
    self.context = ::V8::Context.new
    context.eval(source)
  end
end

Instance Method Details

#call(properties, *args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/proxy_pac_rb/runtimes/rubyracer.rb', line 42

def call(properties, *args)
  lock do
    begin
      unbox context.eval(properties).call(*args)
    rescue ::V8::JSError => e
      if e.value['name'] == 'SyntaxError'
        raise e.value.to_s
      else
        raise ProgramError, e.value.to_s
      end
    end
  end
end

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/proxy_pac_rb/runtimes/rubyracer.rb', line 24

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

  return nil unless /\S/ =~ source

  lock do
    begin
      unbox context.eval("(#{source})")
    rescue ::V8::JSError => e
      if e.value['name'] == 'SyntaxError'
        raise e.value.to_s
      else
        raise ProgramError, e.value.to_s
      end
    end
  end
end

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



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

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

  # rubocop:disable Lint/Eval
  eval "(function(){#{source}})()", options if /\S/ =~ source
  # rubocop:enable Lint/Eval
end

#unbox(value) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/proxy_pac_rb/runtimes/rubyracer.rb', line 56

def unbox(value)
  case value
  when ::V8::Function
    nil
  when ::V8::Array
    value.map { |v| unbox(v) }
  when ::V8::Object
    # rubocop:disable Style/EachWithObject
    value.reduce({}) do |vs, (k, v)|
      vs[k] = unbox(v) unless v.is_a?(::V8::Function)
      vs
    end
    # rubocop:enable Style/EachWithObject
  when String
    if value.respond_to?(:force_encoding)
      value.force_encoding('UTF-8')
    else
      value
    end
  else
    value
  end
end