Class: V8::Portal

Inherits:
Object show all
Defined in:
lib/v8/portal.rb,
lib/v8/portal/caller.rb,
lib/v8/portal/proxies.rb,
lib/v8/portal/function.rb,
lib/v8/portal/templates.rb,
lib/v8/portal/constructor.rb,
lib/v8/portal/interceptors.rb

Defined Under Namespace

Classes: Caller, ConstructorAdapter, FunctionAdapter, Interceptors, Proxies, Templates

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, access) ⇒ Portal

Returns a new instance of Portal.



6
7
8
9
10
11
12
# File 'lib/v8/portal.rb', line 6

def initialize(context, access)
  @context, @access = context, access
  @proxies = Proxies.new
  @templates = Templates.new(self)
  @interceptors = Interceptors.new(self)
  @caller = Caller.new(self)
end

Instance Attribute Details

#accessObject (readonly)

Returns the value of attribute access.



4
5
6
# File 'lib/v8/portal.rb', line 4

def access
  @access
end

#callerObject (readonly)

Returns the value of attribute caller.



4
5
6
# File 'lib/v8/portal.rb', line 4

def caller
  @caller
end

#contextObject (readonly)

Returns the value of attribute context.



4
5
6
# File 'lib/v8/portal.rb', line 4

def context
  @context
end

#interceptorsObject (readonly)

Returns the value of attribute interceptors.



4
5
6
# File 'lib/v8/portal.rb', line 4

def interceptors
  @interceptors
end

#proxiesObject (readonly)

Returns the value of attribute proxies.



4
5
6
# File 'lib/v8/portal.rb', line 4

def proxies
  @proxies
end

#templatesObject (readonly)

Returns the value of attribute templates.



4
5
6
# File 'lib/v8/portal.rb', line 4

def templates
  @templates
end

Instance Method Details

#lockObject



14
15
16
17
18
19
# File 'lib/v8/portal.rb', line 14

def lock
  lock = V8::C::Locker.new
  yield
ensure
  lock.delete
end

#openObject



21
22
23
24
25
26
27
# File 'lib/v8/portal.rb', line 21

def open
  lock do
    @context.native.enter do
      yield(self)
    end if block_given?
  end
end

#rb(value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/v8/portal.rb', line 29

def rb(value)
  @proxies.js2rb(value) do
    case value
      when V8::C::Function    then V8::Function.new(value, self)
      when V8::C::Array       then V8::Array.new(value, self)
      when V8::C::Object      then V8::Object.new(value, self)
      when V8::C::String      then value.Utf8Value.tap {|s| return s.respond_to?(:force_encoding) ? s.force_encoding("UTF-8") : s}
      when V8::C::Date        then Time.at(value.NumberValue() / 1000)
      when V8::C::StackTrace  then V8::StackTrace.new(self, value)
      when V8::C::Value       then nil if value.IsEmpty()
    else
      value
    end
  end
end

#v8(value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/v8/portal.rb', line 45

def v8(value)
  case value
  when V8::Object
    value.instance_eval {@native}
  when String
    C::String::New(value)
  when Symbol
    C::String::NewSymbol(value.to_s)
  when Proc,Method,UnboundMethod
    @proxies.rb2js(value) do
      @templates.to_function(value).function
    end
  when ::Array
    C::Array::New(value.length).tap do |a|
      value.each_with_index do |item, i|
        a.Set(i, v8(item))
      end
    end
  when ::Hash
    C::Object::New().tap do |o|
      value.each do |key, val|
        o.Set(v8(key), v8(val))
      end
    end
  when ::Time
    C::Date::New(value.to_f * 1000)
  when ::Class
    @proxies.rb2js(value) do
      constructor = @templates.to_constructor(value)
      constructor.exposed = true
      constructor.function
    end
  when nil,Numeric,TrueClass,FalseClass, C::Value
    value
  else
    @proxies.rb2js(value) do
      @templates.to_constructor(value.class).allocate(value)
    end
  end
end