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
    raise TypeError, "can't dump anonymous class"
  end

  if singleton_class?
    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

#inherited(cls) ⇒ Object



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

def inherited(cls)
end

#initialize_dup(original) ⇒ Object



28
29
30
31
32
33
34
# File 'opal/opal/corelib/class.rb', line 28

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

#new(*args, &block) ⇒ Object



36
37
38
39
40
41
42
# File 'opal/opal/corelib/class.rb', line 36

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

#superclassObject



44
45
46
# File 'opal/opal/corelib/class.rb', line 44

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

#to_sObject



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

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