class V8::Portal::Proxies

Constants

DoubleProxyError

Public Class Methods

new() click to toggle source
# File lib/v8/portal/proxies.rb, line 5
def initialize
  @js_proxies_rb2js = {}
  @js_proxies_js2rb = {}
  @rb_proxies_rb2js = {}
  @rb_proxies_js2rb = {}
  @clear_js_proxy = ClearJSProxy.new(@js_proxies_rb2js, @js_proxies_js2rb)
  @clear_rb_proxy = ClearRubyProxy.new(@rb_proxies_rb2js, @rb_proxies_js2rb)
end

Public Instance Methods

empty?() click to toggle source
# File lib/v8/portal/proxies.rb, line 101
def empty?
  js_empty? && rb_empty?
end
js2rb(js) { |js| ... } click to toggle source
# File lib/v8/portal/proxies.rb, line 14
def js2rb(js)
  if rb = js_proxy_2_rb_object(js)
    return rb
  elsif rb = js_object_2_rb_proxy(js)
    return rb
  else
    proxy = block_given? ? yield(js) : Object.new
    register_ruby_proxy proxy, :for => js if proxy && js && js.kind_of?(V8::C::Handle)
    return proxy
  end
end
js_empty?() click to toggle source
# File lib/v8/portal/proxies.rb, line 93
def js_empty?
  @js_proxies_rb2js.empty? && @js_proxies_js2rb.empty?
end
js_object_2_rb_proxy(object) click to toggle source

Looks up the Ruby proxy for an object that is natively JavaScript @param [V8::C::Handle] object the JavaScript whose proxy we want @return [Object] the Ruby proxy for `object`

# File lib/v8/portal/proxies.rb, line 73
def js_object_2_rb_proxy(object)
  if id = @rb_proxies_js2rb[object]
    ObjectSpace._id2ref id
  end
rescue RangeError
  # sometimes, the Ruby proxy has been garbage collected, but
  # the finalizer which runs has not been called. That's OK
  # we just clear out the entry, and return nil so that a new
  # proxy can be created.
  @clear_rb_proxy.call(id)
  return nil
end
js_proxy_2_rb_object(proxy) click to toggle source

Look up a natively Ruby object given its JavaScript proxy @param [V8::C::Handle] proxy the JavaScript proxy @return [Object] the Ruby object represented by `proxy`

# File lib/v8/portal/proxies.rb, line 58
def js_proxy_2_rb_object(proxy)
  @js_proxies_js2rb[proxy]
end
rb2js(rb) { |rb| ... } click to toggle source
# File lib/v8/portal/proxies.rb, line 26
def rb2js(rb)
  if js = rb_proxy_2_js_object(rb)
    return js
  elsif js = rb_object_2_js_proxy(rb)
    return js
  else
    proxy = block_given? ? yield(rb) : V8::C::Object::New()
    register_javascript_proxy proxy, :for => rb unless @js_proxies_rb2js[rb]
    return proxy
  end
end
rb_empty?() click to toggle source
# File lib/v8/portal/proxies.rb, line 97
def rb_empty?
  @rb_proxies_rb2js.empty? && @rb_proxies_js2rb.empty?
end
rb_object_2_js_proxy(object) click to toggle source

Lookup the JavaScript proxy for a natively Ruby object @param [Object] object the Ruby object @return [V8::C::Handle] the JavaScript proxy representing `object`

# File lib/v8/portal/proxies.rb, line 51
def rb_object_2_js_proxy(object)
  @js_proxies_rb2js[object]
end
rb_proxy_2_js_object(proxy) click to toggle source

Looks up a native JavaScript object by its Ruby proxy @param [Object] proxy the Ruby proxy @return [V8::C::Handle] the native JavaScript object

# File lib/v8/portal/proxies.rb, line 89
def rb_proxy_2_js_object(proxy)
  @rb_proxies_rb2js[proxy.object_id]
end
register_javascript_proxy(proxy, options = {}) click to toggle source
# File lib/v8/portal/proxies.rb, line 38
def register_javascript_proxy(proxy, options = {})
  target = options[:for] or fail ArgumentError, "must specify the object that you're proxying with the :for => param"
  fail ArgumentError, "javascript proxy must be a V8::C::Handle, not #{proxy.class}" unless proxy.kind_of?(V8::C::Handle)
  fail DoubleProxyError, "target already has a registered proxy" if @js_proxies_rb2js[target]

  @js_proxies_js2rb[proxy] = target
  @js_proxies_rb2js[target] = proxy
  proxy.MakeWeak(nil, @clear_js_proxy)
end
register_ruby_proxy(proxy, options = {}) click to toggle source
# File lib/v8/portal/proxies.rb, line 62
def register_ruby_proxy(proxy, options = {})
  target = options[:for] or fail ArgumentError, "must specify the object that you're proxying with the :for => param"
  fail ArgumentError, "'#{proxy.inspect}' is not a Handle to an actual V8 object" unless target.kind_of?(V8::C::Handle)
  @rb_proxies_rb2js[proxy.object_id] = target
  @rb_proxies_js2rb[target] = proxy.object_id
  ObjectSpace.define_finalizer(proxy, @clear_rb_proxy)
end