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



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

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



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

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



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

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

#attached_objectObject



106
107
108
109
110
111
112
113
114
115
# File 'opal/opal/corelib/class.rb', line 106

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

#clone(freeze: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'opal/opal/corelib/class.rb', line 27

def clone(freeze: nil)
  unless freeze.nil? || freeze == true || freeze == false
    raise ArgumentError, "unexpected value for freeze: #{freeze.class}"
  end

  copy = `Opal.allocate_class(nil, self.$$super)`
  copy.copy_instance_variables(self)
  copy.copy_singleton_methods(self)
  copy.initialize_clone(self, freeze: freeze)

  if freeze == true || (freeze.nil? && frozen?)
    copy.freeze
  end

  copy
end

#descendantsObject



53
54
55
# File 'opal/opal/corelib/class.rb', line 53

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

#dupObject



44
45
46
47
48
49
50
51
# File 'opal/opal/corelib/class.rb', line 44

def dup
  copy = `Opal.allocate_class(nil, self.$$super)`

  copy.copy_instance_variables(self)
  copy.initialize_dup(self)

  copy
end

#inherited(cls) ⇒ Object



57
58
# File 'opal/opal/corelib/class.rb', line 57

def inherited(cls)
end

#new(*args, &block) ⇒ Object



60
61
62
63
64
65
66
# File 'opal/opal/corelib/class.rb', line 60

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

#subclassesObject



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

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



86
87
88
# File 'opal/opal/corelib/class.rb', line 86

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

#to_sObject Also known as: inspect



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'opal/opal/corelib/class.rb', line 90

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