Class: ExecJS::RubyRacerRuntime::Context

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

Instance Method Summary collapse

Constructor Details

#initialize(source = "") ⇒ Context

Returns a new instance of Context.



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

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

  lock do
    @v8_context = ::V8::Context.new
    @v8_context.eval(source)
  end
end

Instance Method Details

#call(properties, *args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/execjs/ruby_racer_runtime.rb', line 37

def call(properties, *args)
  lock do
    unbox @v8_context.eval(properties).call(*args)
  end
rescue ::V8::JSError => e
  if e.value["name"] == "SyntaxError"
    raise RuntimeError, e.message
  else
    raise ProgramError, e.message
  end
end

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/execjs/ruby_racer_runtime.rb', line 21

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

  if /\S/ =~ source
    lock do
      unbox @v8_context.eval("(#{source})")
    end
  end
rescue ::V8::JSError => e
  if e.value["name"] == "SyntaxError"
    raise RuntimeError, e.message
  else
    raise ProgramError, e.message
  end
end

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



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

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/execjs/ruby_racer_runtime.rb', line 49

def unbox(value)
  case value
  when ::V8::Function
    nil
  when ::V8::Array
    value.map { |v| unbox(v) }
  when ::V8::Object
    value.inject({}) do |vs, (k, v)|
      vs[k] = unbox(v) unless v.is_a?(::V8::Function)
      vs
    end
  when String
    value.respond_to?(:force_encoding) ?
      value.force_encoding('UTF-8') :
      value
  else
    value
  end
end