Search in sources :

Example 6 with ModuleEntry

use of com.oracle.truffle.espresso.impl.ModuleTable.ModuleEntry in project graal by oracle.

the class VM method defineModule.

@SuppressWarnings("try")
public void defineModule(StaticObject module, String moduleName, boolean is_open, String[] packages, SubstitutionProfiler profiler) {
    Meta meta = getMeta();
    StaticObject loader = meta.java_lang_Module_loader.getObject(module);
    if (loader != nonReflectionClassLoader(loader)) {
        profiler.profile(15);
        throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "Class loader is an invalid delegating class loader");
    }
    // Prepare variables
    ClassRegistry registry = getRegistries().getClassRegistry(loader);
    assert registry != null;
    PackageTable packageTable = registry.packages();
    ModuleTable moduleTable = registry.modules();
    assert moduleTable != null && packageTable != null;
    boolean loaderIsBootOrPlatform = ClassRegistry.loaderIsBootOrPlatform(loader, meta);
    ArrayList<Symbol<Name>> pkgSymbols = new ArrayList<>();
    try (EntryTable.BlockLock block = packageTable.write()) {
        for (String str : packages) {
            // Extract the package symbols. Also checks for duplicates.
            if (!loaderIsBootOrPlatform && (str.equals("java") || str.startsWith("java/"))) {
                // Only modules defined to either the boot or platform class loader, can define
                // a "java/" package.
                profiler.profile(14);
                throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, cat("Class loader (", loader.getKlass().getType(), ") tried to define prohibited package name: ", str));
            }
            Symbol<Name> symbol = getNames().getOrCreate(str);
            if (packageTable.lookup(symbol) != null) {
                profiler.profile(13);
                throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, cat("Package ", str, " is already defined."));
            }
            pkgSymbols.add(symbol);
        }
        Symbol<Name> moduleSymbol = getNames().getOrCreate(moduleName);
        // Try define module
        ModuleEntry moduleEntry = moduleTable.createAndAddEntry(moduleSymbol, registry, is_open, module);
        if (moduleEntry == null) {
            // Module already defined
            profiler.profile(12);
            throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, cat("Module ", moduleName, " is already defined"));
        }
        // Register packages
        for (Symbol<Name> pkgSymbol : pkgSymbols) {
            PackageEntry pkgEntry = packageTable.createAndAddEntry(pkgSymbol, moduleEntry);
            // should have been checked before
            assert pkgEntry != null;
        }
        // Link guest module to its host representation
        meta.HIDDEN_MODULE_ENTRY.setHiddenObject(module, moduleEntry);
    }
    if (StaticObject.isNull(loader) && getContext().getVmProperties().bootClassPathType().isExplodedModule()) {
        profiler.profile(11);
        // If we have an exploded build, and the module is defined to the bootloader, prepend a
        // class path entry for this module.
        prependModuleClasspath(moduleName);
    }
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) ClassRegistry(com.oracle.truffle.espresso.impl.ClassRegistry) Symbol(com.oracle.truffle.espresso.descriptors.Symbol) ModuleEntry(com.oracle.truffle.espresso.impl.ModuleTable.ModuleEntry) ArrayList(java.util.ArrayList) ModuleTable(com.oracle.truffle.espresso.impl.ModuleTable) PackageTable(com.oracle.truffle.espresso.impl.PackageTable) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) EntryTable(com.oracle.truffle.espresso.impl.EntryTable) PackageEntry(com.oracle.truffle.espresso.impl.PackageTable.PackageEntry)

Aggregations

ModuleEntry (com.oracle.truffle.espresso.impl.ModuleTable.ModuleEntry)6 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)3 Meta (com.oracle.truffle.espresso.meta.Meta)3 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)3 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)2 EntryTable (com.oracle.truffle.espresso.impl.EntryTable)2 PackageTable (com.oracle.truffle.espresso.impl.PackageTable)2 PackageEntry (com.oracle.truffle.espresso.impl.PackageTable.PackageEntry)2 Symbol (com.oracle.truffle.espresso.descriptors.Symbol)1 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)1 ClassRegistry (com.oracle.truffle.espresso.impl.ClassRegistry)1 Klass (com.oracle.truffle.espresso.impl.Klass)1 Method (com.oracle.truffle.espresso.impl.Method)1 ModuleTable (com.oracle.truffle.espresso.impl.ModuleTable)1 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)1 ArrayList (java.util.ArrayList)1