Beispiel #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);
  }
  public DynamicObject getNormalObjectSingletonClass(DynamicObject object) {
    CompilerAsserts.neverPartOfCompilation();

    if (RubyGuards.isRubyClass(object)) { // For the direct caller
      return ClassNodes.getSingletonClass(object);
    }

    if (Layouts.CLASS.getIsSingleton(Layouts.BASIC_OBJECT.getMetaClass(object))) {
      return Layouts.BASIC_OBJECT.getMetaClass(object);
    }

    CompilerDirectives.transferToInterpreter();
    final DynamicObject logicalClass = BasicObjectNodes.getLogicalClass(object);

    DynamicObject attached = null;
    if (RubyGuards.isRubyModule(object)) {
      attached = object;
    }

    final String name =
        String.format(
            "#<Class:#<%s:0x%x>>",
            Layouts.MODULE.getFields(logicalClass).getName(),
            BasicObjectNodes.verySlowGetObjectID(object));
    final DynamicObject singletonClass =
        ClassNodes.createSingletonClassOfObject(getContext(), logicalClass, attached, name);
    propagateFrozen(object, singletonClass);

    Layouts.BASIC_OBJECT.setMetaClass(object, singletonClass);

    return singletonClass;
  }
Beispiel #3
0
 public Object isDefined(VirtualFrame frame) {
   return Layouts.STRING.createString(
       Layouts.CLASS.getInstanceFactory(getContext().getCoreLibrary().getStringClass()),
       StringOperations.encodeByteList("expression", UTF8Encoding.INSTANCE),
       StringSupport.CR_7BIT,
       null);
 }
Beispiel #4
0
  public static DynamicObject createOneSingletonClass(DynamicObject rubyClass) {
    CompilerAsserts.neverPartOfCompilation();

    if (Layouts.CLASS.getIsSingleton(Layouts.BASIC_OBJECT.getMetaClass(rubyClass))) {
      return Layouts.BASIC_OBJECT.getMetaClass(rubyClass);
    }

    final DynamicObject singletonSuperclass;
    if (getSuperClass(rubyClass) == null) {
      singletonSuperclass = Layouts.BASIC_OBJECT.getLogicalClass(rubyClass);
    } else {
      singletonSuperclass = createOneSingletonClass(getSuperClass(rubyClass));
    }

    String name = String.format("#<Class:%s>", Layouts.MODULE.getFields(rubyClass).getName());
    Layouts.BASIC_OBJECT.setMetaClass(
        rubyClass,
        ClassNodes.createRubyClass(
            Layouts.MODULE.getFields(Layouts.BASIC_OBJECT.getLogicalClass(rubyClass)).getContext(),
            Layouts.BASIC_OBJECT.getLogicalClass(rubyClass),
            null,
            singletonSuperclass,
            name,
            true,
            rubyClass));

    return Layouts.BASIC_OBJECT.getMetaClass(rubyClass);
  }
Beispiel #5
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;
  }
Beispiel #6
0
  /**
   * This constructor supports initialization and solves boot-order problems and should not normally
   * be used from outside this class.
   */
  public static DynamicObject createBootClass(
      DynamicObject classClass, DynamicObject superclass, String name) {
    assert RubyGuards.isRubyClass(classClass);
    assert superclass == null || RubyGuards.isRubyClass(superclass);
    final ModuleFields model =
        new ModuleFields(
            Layouts.MODULE.getFields(Layouts.BASIC_OBJECT.getLogicalClass(classClass)).getContext(),
            null,
            name);

    final DynamicObject rubyClass =
        Layouts.CLASS.createClass(
            Layouts.CLASS.getInstanceFactory(classClass), model, false, null, 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();
    }

    return rubyClass;
  }
Beispiel #7
0
  /** Special constructor for class Class */
  public static DynamicObject createClassClass(RubyContext context) {
    final ModuleFields model = new ModuleFields(context, null, "Class");

    final DynamicObject rubyClass = LAYOUT.newInstance(LAYOUT.createShape(new ObjectType()));

    final DynamicObjectFactory factory = Layouts.CLASS.createClassShape(rubyClass, rubyClass);

    rubyClass.setShapeAndGrow(rubyClass.getShape(), factory.getShape());
    assert RubyGuards.isRubyModule(rubyClass);
    assert RubyGuards.isRubyClass(rubyClass);

    model.rubyModuleObject = rubyClass;
    Layouts.CLASS.setInstanceFactoryUnsafe(rubyClass, factory);
    Layouts.MODULE.setFields(rubyClass, model);
    model.name = model.givenBaseName;

    assert RubyGuards.isRubyModule(rubyClass);
    assert RubyGuards.isRubyClass(rubyClass);
    assert Layouts.MODULE.getFields(rubyClass) == model;
    assert Layouts.BASIC_OBJECT.getLogicalClass(rubyClass) == rubyClass;

    return rubyClass;
  }
 public static DynamicObject createRubyMatchData(
     DynamicObject rubyClass,
     DynamicObject source,
     DynamicObject regexp,
     Region region,
     Object[] values,
     DynamicObject pre,
     DynamicObject post,
     DynamicObject global,
     int begin,
     int end) {
   return Layouts.MATCH_DATA.createMatchData(
       Layouts.CLASS.getInstanceFactory(rubyClass),
       new MatchDataFields(source, regexp, region, values, pre, post, global, begin, end));
 }
Beispiel #9
0
 @Specialization
 public DynamicObject allocate(DynamicObject rubyClass) {
   return Layouts.SIZED_QUEUE.createSizedQueue(
       Layouts.CLASS.getInstanceFactory(rubyClass), null);
 }
Beispiel #10
0
 protected DynamicObjectFactory getInstanceFactory(DynamicObject rubyClass) {
   return Layouts.CLASS.getInstanceFactory(rubyClass);
 }