<commit_before>/**
 * Copyright 2005-2007 <NAME>, <NAME>
 * Distributed under the GNU General Public License 2.0
 */

package com.xruby.runtime.lang;

import com.xruby.runtime.value.RubyString;
import com.xruby.runtime.value.RubyArray;

public class RubyModule extends MethodCollection {

    private RubyModule owner_ = null;//owner is where is the module is defined under.
    protected RubyClass superclass_;

    public RubyModule(String name, RubyModule owner) {
        super(null);
        super.name_ = name;
        owner_ = owner;
    }

    public boolean isRealModule() {
        return true;
    }

    public boolean isRealClass() {
        return false;
    }

    public RubyValue defineMethod(String name, RubyMethod m) {
        m.setOwner(this);
        RubyID mid = StringMap.intern(name);
        return addMethod(mid, m);
    }

    /// This method is only used by java classes in package 'com.xruby.runtime.builtin'.
    /// It has less overhead than 'defineClass' (no hash table lookup).
    /// This method is NOT used by classes compiled from ruby script.
    public RubyClass defineNewClass(String name, RubyClass parent) {
        if (parent == null) {
            parent = RubyRuntime.ObjectClass;
        }

        RubyClass c = new RubyClass(name, parent, this);
        c.setName(name);
        c.setRubyClass(RubyRuntime.ClassClass);
        new RubySingletonClass(c, parent.getRubyClass());

        constants_.put(name, c);
        return c;
    }

    private RubyClass defineClass(String name, RubyClass parent) {
        RubyValue v = constants_.get(name);
        if (null == v) {
            return defineNewClass(name, (null == parent) ? RubyRuntime.ObjectClass : parent);
        }

        if (!(v instanceof RubyClass)) {
            throw new RubyException(RubyRuntime.TypeErrorClass, name + " is not a class");
        }

        RubyClass c = (RubyClass)v;

        if (null != parent) {
            if (!c.isMyParent(parent)){
                throw new RubyException(RubyRuntime.TypeErrorClass, "superclass mismatch for class "+ name);
            }
        }

        c.setAccessPublic();
        return c;
    }

    /// define a new class or get a old one
    public RubyClass defineClass(String name, RubyValue parent) {
        if (null != parent && !(parent instanceof RubyClass)) {
            throw new RubyException(RubyRuntime.TypeErrorClass, "superclass must be a Class (" + parent.getRubyClass().getName() + " given)");
        }

        return defineClass(name, null == parent ? null : (RubyClass)parent);
    }

    /// For compile-time recognizable builtin class, just do sanity checks...
    public RubyClass defineBuiltInClass(RubyClass c, RubyValue parent) {
        if (null != parent) {
            if (!(parent instanceof RubyClass)) {
                throw new RubyException(RubyRuntime.TypeErrorClass, "superclass must be a Class (" + parent.getRubyClass().getName() + " given)");
            }

            if (!c.isMyParent((RubyClass)parent)){
                throw new RubyException(RubyRuntime.TypeErrorClass, "superclass mismatch for class "+ c.getName());
            }
        }

        c.setAccessPublic();
        return c;
    }

    public RubyModule defineNewModule(String name) {
        RubyModule m = new RubyModule(name, this);
        m.setName(name);
        m.setRubyClass(RubyRuntime.ModuleClass);
        constants_.put(name, m);//NOTE, do not use ObjectFactory.createClass, it will cause initialization issue
        return m;
    }

    public RubyModule defineModule(String name) {
        RubyValue v = constants_.get(name);
        if (null == v) {
            return defineNewModule(name);
        }

        if (!(v instanceof RubyModule) || (v instanceof RubyClass)) {
            throw new RubyException(RubyRuntime.TypeErrorClass, name + " is not a module");
        }

        return (RubyModule)v;
    }

    RubyValue getConstant(String name) {
        RubyValue v = super.getOwnConstant(name);
        if (null != v) {
            return v;
        }

        if (null != this.superclass_) {
            v = this.superclass_.getConstant(name);
            if (null != v) {
                return v;
            }
        }<commit_msg>change getConstant access modifier from default to public<commit_after>/**
 * Copyright 2005-2007 <NAME>, <NAME>
 * Distributed under the GNU General Public License 2.0
 */

package com.xruby.runtime.lang;

import com.xruby.runtime.value.RubyString;
import com.xruby.runtime.value.RubyArray;

public class RubyModule extends MethodCollection {

    private RubyModule owner_ = null;//owner is where is the module is defined under.
    protected RubyClass superclass_;

    public RubyModule(String name, RubyModule owner) {
        super(null);
        super.name_ = name;
        owner_ = owner;
    }

    public boolean isRealModule() {
        return true;
    }

    public boolean isRealClass() {
        return false;
    }

    public RubyValue defineMethod(String name, RubyMethod m) {
        m.setOwner(this);
        RubyID mid = StringMap.intern(name);
        return addMethod(mid, m);
    }

    /// This method is only used by java classes in package 'com.xruby.runtime.builtin'.
    /// It has less overhead than 'defineClass' (no hash table lookup).
    /// This method is NOT used by classes compiled from ruby script.
    public RubyClass defineNewClass(String name, RubyClass parent) {
        if (parent == null) {
            parent = RubyRuntime.ObjectClass;
        }

        RubyClass c = new RubyClass(name, parent, this);
        c.setName(name);
        c.setRubyClass(RubyRuntime.ClassClass);
        new RubySingletonClass(c, parent.getRubyClass());

        constants_.put(name, c);
        return c;
    }

    private RubyClass defineClass(String name, RubyClass parent) {
        RubyValue v = constants_.get(name);
        if (null == v) {
            return defineNewClass(name, (null == parent) ? RubyRuntime.ObjectClass : parent);
        }

        if (!(v instanceof RubyClass)) {
            throw new RubyException(RubyRuntime.TypeErrorClass, name + " is not a class");
        }

        RubyClass c = (RubyClass)v;

        if (null != parent) {
            if (!c.isMyParent(parent)){
                throw new RubyException(RubyRuntime.TypeErrorClass, "superclass mismatch for class "+ name);
            }
        }

        c.setAccessPublic();
        return c;
    }

    /// define a new class or get a old one
    public RubyClass defineClass(String name, RubyValue parent) {
        if (null != parent && !(parent instanceof RubyClass)) {
            throw new RubyException(RubyRuntime.TypeErrorClass, "superclass must be a Class (" + parent.getRubyClass().getName() + " given)");
        }

        return defineClass(name, null == parent ? null : (RubyClass)parent);
    }

    /// For compile-time recognizable builtin class, just do sanity checks...
    public RubyClass defineBuiltInClass(RubyClass c, RubyValue parent) {
        if (null != parent) {
            if (!(parent instanceof RubyClass)) {
                throw new RubyException(RubyRuntime.TypeErrorClass, "superclass must be a Class (" + parent.getRubyClass().getName() + " given)");
            }

            if (!c.isMyParent((RubyClass)parent)){
                throw new RubyException(RubyRuntime.TypeErrorClass, "superclass mismatch for class "+ c.getName());
            }
        }

        c.setAccessPublic();
        return c;
    }

    public RubyModule defineNewModule(String name) {
        RubyModule m = new RubyModule(name, this);
        m.setName(name);
        m.setRubyClass(RubyRuntime.ModuleClass);
        constants_.put(name, m);//NOTE, do not use ObjectFactory.createClass, it will cause initialization issue
        return m;
    }

    public RubyModule defineModule(String name) {
        RubyValue v = constants_.get(name);
        if (null == v) {
            return defineNewModule(name);
        }

        if (!(v instanceof RubyModule) || (v instanceof RubyClass)) {
            throw new RubyException(RubyRuntime.TypeErrorClass, name + " is not a module");
        }

        return (RubyModule)v;
    }

    public RubyValue getConstant(String name) {
        RubyValue v = super.getOwnConstant(name);
        if (null != v) {
            return v;
        }

        if (null != this.superclass_) {
            v = this.superclass_.getConstant(name);
            if (null != v) {
                return v;
            }
        }
{'fraction_non_alphanumeric': 0.10258812507319358, 'fraction_numerical': 0.002342194636374283, 'mean_word_length': 2.2632785632403514, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5314040', 'n_tokens_mistral': 2413, 'n_tokens_neox': 2248, 'n_words': 678}