Sha256: 21ab448332a088742f7b6e6c89514b72ed551f8d73c95fcdd8735865e78b479e

Contents?: true

Size: 1.46 KB

Versions: 7

Compression:

Stored size: 1.46 KB

Contents

require 'corelib/module'

class Class
  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

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

  def inherited(cls)
  end

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

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

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

  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
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
opal-0.11.4 opal/corelib/class.rb
opal-0.11.3 opal/corelib/class.rb
opal-0.11.2 opal/corelib/class.rb
opal-0.11.1 opal/corelib/class.rb
opal-0.11.1.pre opal/corelib/class.rb
opal-0.11.0 opal/corelib/class.rb
opal-0.11.0.rc1 opal/corelib/class.rb