Exemple #1
0
  public static void initialize(DynamicObject rubyClass, DynamicObject superclass) {
    assert RubyGuards.isRubyClass(superclass);

    Layouts.MODULE.getFields(rubyClass).parentModule = Layouts.MODULE.getFields(superclass).start;
    Layouts.MODULE.getFields(superclass).addDependent(rubyClass);

    Layouts.MODULE.getFields(rubyClass).newVersion();
    ensureSingletonConsistency(rubyClass);

    DynamicObjectFactory factory = Layouts.CLASS.getInstanceFactory(superclass);
    factory = Layouts.BASIC_OBJECT.setLogicalClass(factory, rubyClass);
    factory = Layouts.BASIC_OBJECT.setMetaClass(factory, rubyClass);
    Layouts.CLASS.setInstanceFactoryUnsafe(rubyClass, factory);
  }
Exemple #2
0
  public static DynamicObject createRubyClass(
      RubyContext context,
      DynamicObject classClass,
      DynamicObject lexicalParent,
      DynamicObject superclass,
      String name,
      boolean isSingleton,
      DynamicObject attached) {
    final ModuleFields model = new ModuleFields(context, lexicalParent, name);

    final DynamicObject rubyClass =
        Layouts.CLASS.createClass(
            Layouts.CLASS.getInstanceFactory(classClass), model, isSingleton, attached, null);
    assert RubyGuards.isRubyClass(rubyClass) : classClass.getShape().getObjectType().getClass();
    assert RubyGuards.isRubyModule(rubyClass) : classClass.getShape().getObjectType().getClass();

    model.rubyModuleObject = rubyClass;

    if (model.lexicalParent == null) { // bootstrap or anonymous module
      Layouts.MODULE.getFields(rubyClass).name = Layouts.MODULE.getFields(rubyClass).givenBaseName;
    } else {
      Layouts.MODULE
          .getFields(rubyClass)
          .getAdoptedByLexicalParent(model.lexicalParent, model.givenBaseName, null);
    }

    if (superclass != null) {
      assert RubyGuards.isRubyClass(superclass);
      assert RubyGuards.isRubyClass(Layouts.MODULE.getFields(rubyClass).rubyModuleObject);

      Layouts.MODULE.getFields(rubyClass).parentModule = Layouts.MODULE.getFields(superclass).start;
      Layouts.MODULE
          .getFields(superclass)
          .addDependent(Layouts.MODULE.getFields(rubyClass).rubyModuleObject);

      Layouts.MODULE.getFields(rubyClass).newVersion();
    }

    DynamicObjectFactory factory = Layouts.CLASS.getInstanceFactory(superclass);
    factory = Layouts.BASIC_OBJECT.setLogicalClass(factory, rubyClass);
    factory = Layouts.BASIC_OBJECT.setMetaClass(factory, rubyClass);
    Layouts.CLASS.setInstanceFactoryUnsafe(rubyClass, factory);

    return rubyClass;
  }