Class: Class

Inherits:
Object show all
Defined in:
opal/opal/corelib/class.rb,
opal/opal/corelib/marshal/write_buffer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(superclass = Object, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'opal/opal/corelib/class.rb', line 4

def self.new(superclass = Object, &block)
  %x{
    if (!superclass.$$is_class) {
      throw Opal.TypeError.$new("superclass must be a Class");
    }

    var klass = Opal.allocate_class(nil, superclass);
    superclass.$inherited(klass);
    #{`klass`.class_eval(&block) if block_given?}
    return klass;
  }
end

Instance Method Details

#__marshal__(buffer) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 125

def __marshal__(buffer)
  unless name
    ::Kernel.raise ::TypeError, "can't dump anonymous class"
  end

  if singleton_class?
    ::Kernel.raise ::TypeError, "singleton class can't be dumped"
  end

  buffer.save_link(self)
  buffer.append('c')
  buffer.write_class(self)
end

#allocateObject



17
18
19
20
21
22
23
# File 'opal/opal/corelib/class.rb', line 17

def allocate
  %x{
    var obj = new self.$$constructor();
    obj.$$id = Opal.uid();
    return obj;
  }
end

#attached_objectObject



86
87
88
89
90
91
92
93
94
95
# File 'opal/opal/corelib/class.rb', line 86

def attached_object
  %x{
    if (self.$$singleton_of != null) {
      return self.$$singleton_of;
    }
    else {
      #{::Kernel.raise ::TypeError, "`#{self}' is not a singleton class"}
    }
  }
end

#descendantsObject



25
26
27
# File 'opal/opal/corelib/class.rb', line 25

def descendants
  subclasses + subclasses.map(&:descendants).flatten
end

#inherited(cls) ⇒ Object



29
30
# File 'opal/opal/corelib/class.rb', line 29

def inherited(cls)
end

#initialize_dup(original) ⇒ Object



32
33
34
35
36
37
38
# File 'opal/opal/corelib/class.rb', line 32

def initialize_dup(original)
  initialize_copy(original)
  %x{
    self.$$name = null;
    self.$$full_name = null;
  }
end

#new(*args, &block) ⇒ Object



40
41
42
43
44
45
46
# File 'opal/opal/corelib/class.rb', line 40

def new(*args, &block)
  %x{
    var object = #{allocate};
    Opal.send(object, object.$initialize, args, block);
    return object;
  }
end

#subclassesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'opal/opal/corelib/class.rb', line 48

def subclasses
  %x{
    if (typeof WeakRef !== 'undefined') {
      var i, subclass, out = [];
      for (i = 0; i < self.$$subclasses.length; i++) {
        subclass = self.$$subclasses[i].deref();
        if (subclass !== undefined) {
          out.push(subclass);
        }
      }
      return out;
    }
    else {
      return self.$$subclasses;
    }
  }
end

#superclassObject



66
67
68
# File 'opal/opal/corelib/class.rb', line 66

def superclass
  `self.$$super || nil`
end

#to_sObject Also known as: inspect



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'opal/opal/corelib/class.rb', line 70

def to_s
  %x{
    var singleton_of = self.$$singleton_of;

    if (singleton_of && singleton_of.$$is_a_module) {
      return #{"#<Class:#{`singleton_of`.name}>"};
    }
    else if (singleton_of) {
      // a singleton class created from an object
      return #{"#<Class:#<#{`singleton_of.$$class`.name}:0x#{`Opal.id(singleton_of)`.to_s(16)}>>"};
    }

    return #{super()};
  }
end