Class: ExecJS::RubyRacerRuntime::Context

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

Instance Method Summary (collapse)

Methods included from Encoding

#encode

Constructor Details

- (Context) initialize(runtime, source = "")



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

def initialize(runtime, source = "")
  source = encode(source)

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

Instance Method Details

- (Object) call(properties, *args)



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/execjs/ruby_racer_runtime.rb', line 41

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

- (Object) eval(source, options = {})



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

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

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

- (Object) exec(source, options = {})



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

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

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

- (Object) unbox(value)



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/execjs/ruby_racer_runtime.rb', line 55

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