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
22
23
24
# 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;

    // inherit scope from parent
    Opal.create_scope(superclass.$$scope, klass);

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

    return klass;
  }
end

Instance Method Details

#__marshal__(buffer) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 120

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



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

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

#inherited(cls) ⇒ Object



34
35
# File 'opal/opal/corelib/class.rb', line 34

def inherited(cls)
end

#new(*args, &block) ⇒ Object



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

def new(*args, &block)
  %x{
    var obj = #{allocate};

    obj.$initialize.$$p = block;
    obj.$initialize.apply(obj, args);
    return obj;
  }
end

#superclassObject



47
48
49
# File 'opal/opal/corelib/class.rb', line 47

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

#to_sObject



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

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#{`singleton_of.$$id`.to_s(16)}>>"};
    }
    return #{super()};
  }
end