Example #1
0
  public void getAdoptedByLexicalParent(
      DynamicObject lexicalParent, String name, Node currentNode) {
    assert RubyGuards.isRubyModule(lexicalParent);

    Layouts.MODULE
        .getFields(lexicalParent)
        .setConstantInternal(currentNode, name, rubyModuleObject, false);
    Layouts.MODULE.getFields(lexicalParent).addLexicalDependent(rubyModuleObject);

    if (this.name == null) {
      // Tricky, we need to compare with the Object class, but we only have a Class at hand.
      final DynamicObject classClass = Layouts.BASIC_OBJECT.getLogicalClass(getLogicalClass());
      final DynamicObject objectClass =
          ClassNodes.getSuperClass(ClassNodes.getSuperClass(classClass));

      if (lexicalParent == objectClass) {
        this.name = name;
        updateAnonymousChildrenModules();
      } else if (Layouts.MODULE.getFields(lexicalParent).hasName()) {
        this.name = Layouts.MODULE.getFields(lexicalParent).getName() + "::" + name;
        updateAnonymousChildrenModules();
      }
      // else: Our lexicalParent is also an anonymous module
      // and will name us when it gets named via updateAnonymousChildrenModules()
    }
  }
  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;
  }
Example #3
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);
  }
Example #4
0
 @Specialization
 public Object getSuperClass(DynamicObject rubyClass) {
   final DynamicObject superclass = ClassNodes.getSuperClass(rubyClass);
   if (superclass == null) {
     return nil();
   } else {
     return superclass;
   }
 }
Example #5
0
    private DynamicObject initializeGeneralWithoutBlock(
        VirtualFrame frame, DynamicObject rubyClass, DynamicObject superclass) {
      assert RubyGuards.isRubyClass(rubyClass);
      assert RubyGuards.isRubyClass(superclass);

      ClassNodes.initialize(rubyClass, superclass);
      triggerInheritedHook(frame, rubyClass, superclass);

      return rubyClass;
    }
 @Specialization(guards = "isRubyClass(rubyClass)")
 protected DynamicObject singletonClassClass(DynamicObject rubyClass) {
   return ClassNodes.getSingletonClass(rubyClass);
 }