Class: V8::Portal::ConstructorAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/v8/portal/constructor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(templates, class_id) ⇒ ConstructorAdapter

Returns a new instance of ConstructorAdapter.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Attribute Details

#exposedObject Also known as: exposed?

Returns the value of attribute exposed.



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

def exposed
  @exposed
end

#templateObject (readonly)

Returns the value of attribute template.



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

def template
  @template
end

Instance Method Details

#allocate(object) ⇒ Object



31
32
33
34
35
# 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

#disableObject



37
38
39
# File 'lib/v8/portal/constructor.rb', line 37

def disable
  @disabled = true
end

#enableObject



41
42
43
# File 'lib/v8/portal/constructor.rb', line 41

def enable
  @disabled = nil
end

#functionObject



27
28
29
# File 'lib/v8/portal/constructor.rb', line 27

def function
  @template.GetFunction()
end

#invoke(arguments) ⇒ 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
# 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

#portalObject



93
94
95
# File 'lib/v8/portal/constructor.rb', line 93

def portal
  @templates.portal
end

#proxiesObject



89
90
91
# File 'lib/v8/portal/constructor.rb', line 89

def proxies
  @templates.proxies
end

#ruby_classObject



85
86
87
# File 'lib/v8/portal/constructor.rb', line 85

def ruby_class
  ObjectSpace._id2ref(@class_id)
end