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
16
17
18
19
20
21
# 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 alloc = Opal.boot_class_alloc(null, function(){}, superclass);
    var klass = Opal.setup_class_object(null, alloc, superclass.$$name, superclass.constructor);

    klass.$$super  = superclass;
    klass.$$parent = superclass;

    superclass.$inherited(klass);
    Opal.module_initialize(klass, block);

    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



23
24
25
26
27
28
29
# File 'opal/opal/corelib/class.rb', line 23

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

#inherited(cls) ⇒ Object



31
32
# File 'opal/opal/corelib/class.rb', line 31

def inherited(cls)
end

#initialize_dup(original) ⇒ Object



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

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

#new(*args, &block) ⇒ Object



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

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

#superclassObject



50
51
52
# File 'opal/opal/corelib/class.rb', line 50

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

#to_sObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'opal/opal/corelib/class.rb', line 54

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

    if (singleton_of && (singleton_of.$$is_class || singleton_of.$$is_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