class V8::Portal::ConstructorAdapter

Attributes

exposed[R]
exposed?[R]
template[R]

Public Class Methods

new(templates, class_id) click to toggle source
# File lib/v8/portal/constructor.rb, line 8
def initialize(templates, class_id)
  @exposed = false
  @class_id = class_id
  @templates = templates
  @invoke = method(:invoke)
  @template = C::FunctionTemplate::New(@invoke)
  portal.interceptors.setup(@template.InstanceTemplate())
  cls = self.ruby_class
  superclass = cls.superclass
  if cls != ::Object && superclass != ::Object && superclass != ::Class
    @template.Inherit(templates.to_constructor(superclass).template)
  end
  if cls.name && cls.name =~ /(::)?(\w+?)$/
    template.SetClassName(C::String::NewSymbol("rb::" + $2))
  else
    template.SetClassName("Ruby")
  end
end

Public Instance Methods

allocate(object) click to toggle source
# File lib/v8/portal/constructor.rb, line 31
def allocate(object)
  arguments = C::Array::New(1)
  arguments.Set(0, C::External::New(object))
  function.NewInstance(arguments)
end
disable() click to toggle source
# File lib/v8/portal/constructor.rb, line 37
def disable
  @disabled = true
end
enable() click to toggle source
# File lib/v8/portal/constructor.rb, line 41
def enable
  @disabled = nil
end
exposed=(exposed) click to toggle source
# File lib/v8/portal/constructor.rb, line 71
def exposed=(exposed)
  if exposed && !@augmented
    #create a prototype so that this constructor also acts like a ruby object
    prototype_template = C::ObjectTemplate::New()
    portal.interceptors.setup(prototype_template)
    prototype = prototype_template.NewInstance()
    #set *that* object's prototype to an empty function so that it will look and behave like a function.
    prototype.SetPrototype(C::FunctionTemplate::New().GetFunction())
    template.GetFunction().SetPrototype(prototype)
    @augmented = true
  end
  @exposed = exposed
end
function() click to toggle source
# File lib/v8/portal/constructor.rb, line 27
def function
  @template.GetFunction()
end
invoke(arguments) click to toggle source
# File lib/v8/portal/constructor.rb, line 45
def invoke(arguments)
  return if @disabled
  if !@exposed
    unless arguments.Length() == 1 && arguments[0].kind_of?(C::External)
      C::ThrowException(C::Exception::Error(C::String::New("cannot call native constructor from javascript")))
    else
      object = arguments[0].Value()
      proxies.register_javascript_proxy arguments.This(), :for => object
    end
  else
    instance = nil
    if arguments.Length() > 0 && arguments[0].kind_of?(C::External)
      instance = arguments[0].Value()
    else
      rbargs = []
      for i in 0..arguments.Length() - 1
        rbargs << @templates.portal.rb(arguments[i])
      end
      instance = portal.caller.raw do
        self.ruby_class.new(*rbargs)
      end
    end
    proxies.register_javascript_proxy arguments.This(), :for => instance
  end
end
portal() click to toggle source
# File lib/v8/portal/constructor.rb, line 93
def portal
  @templates.portal
end
proxies() click to toggle source
# File lib/v8/portal/constructor.rb, line 89
def proxies
  @templates.proxies
end
ruby_class() click to toggle source
# File lib/v8/portal/constructor.rb, line 85
def ruby_class
  ObjectSpace._id2ref(@class_id)
end