Skip to content

Commit

Permalink
Removing Truffle API from runtime-compiler project dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Apr 26, 2024
1 parent 102f58b commit 3c87142
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 86 deletions.
1 change: 0 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -1963,7 +1963,6 @@ lazy val `runtime-compiler` =
(Test / fork) := true,
libraryDependencies ++= Seq(
"com.chuusai" %% "shapeless" % shapelessVersion,
"org.graalvm.truffle" % "truffle-api" % graalMavenPackagesVersion,
"junit" % "junit" % junitVersion % Test,
"com.github.sbt" % "junit-interface" % junitIfVersion % Test,
"org.scalatest" %% "scalatest" % scalatestVersion % Test,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.enso.compiler

import com.oracle.truffle.api.TruffleFile
import org.enso.editions.LibraryName
import org.enso.compiler.context.CompilerContext
import org.enso.compiler.core.ir.{Module => IRModule}
import org.enso.pkg.{ComponentGroups, Package, QualifiedName}

import scala.collection.immutable.ListSet
import scala.jdk.OptionConverters.RichOption

/** Manages loaded packages and modules. */
trait PackageRepository {
type TFile

/** Initialize the package repository.
*
Expand All @@ -34,10 +33,7 @@ trait PackageRepository {
def isPackageLoaded(libraryName: LibraryName): Boolean

/** Get a sequence of currently loaded packages. */
def getLoadedPackages: Seq[Package[TruffleFile]]

/** Get a sequence of currently loaded packages. */
def getLoadedPackagesJava: java.lang.Iterable[Package[TruffleFile]]
def getLoadedPackages: Seq[Package[TFile]]

/** Get a sequence of currently loaded modules. */
def getLoadedModules: Seq[CompilerContext.Module]
Expand Down Expand Up @@ -65,12 +61,12 @@ trait PackageRepository {
/** Register the main project package. */
def registerMainProjectPackage(
libraryName: LibraryName,
pkg: Package[TruffleFile]
pkg: Package[TFile]
): Unit

/** @return the main project package, if it exists
*/
def getMainProjectPackage: Option[Package[TruffleFile]]
def getMainProjectPackage: Option[Package[TFile]]

/** Register a single module, outside of any packages or part of an already
* loaded package, that has been created manually during runtime.
Expand All @@ -95,13 +91,7 @@ trait PackageRepository {
def isNamespaceRegistered(namespace: String): Boolean

/** Returns a package directory corresponding to the requested library */
def getPackageForLibrary(lib: LibraryName): Option[Package[TruffleFile]]

/** Returns a package directory corresponding to the requested library */
def getPackageForLibraryJava(
libraryName: LibraryName
): java.util.Optional[Package[TruffleFile]] =
getPackageForLibrary(libraryName).toJava
def getPackageForLibrary(lib: LibraryName): Option[Package[TFile]]

/** Returns all loaded modules of the requested library */
def getModulesForLibrary(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public Optional<Cache.Roots> getCacheRoots(EnsoContext context) {
.getPackageForLibraryJava(libraryName)
.map(
pkg -> {
var bindingsCacheRoot = pkg.getBindingsCacheRootForPackage(Info.ensoVersion());
TruffleFile bindingsCacheRoot =
pkg.getBindingsCacheRootForPackage(Info.ensoVersion());
var localCacheRoot = bindingsCacheRoot.resolve(libraryName.namespace());
var distribution = context.getDistributionManager();
var pathSegments =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
import java.util.logging.Level;
import org.enso.common.LanguageInfo;
import org.enso.compiler.Compiler;
import org.enso.compiler.PackageRepository;
import org.enso.compiler.PackageRepositoryUtils;
import org.enso.compiler.data.CompilerConfig;
import org.enso.distribution.DistributionManager;
import org.enso.distribution.locking.LockManager;
Expand Down Expand Up @@ -85,7 +83,7 @@ public final class EnsoContext {
private final PrintStream err;
private final InputStream in;
private final BufferedReader inReader;
private @CompilationFinal PackageRepository packageRepository;
private @CompilationFinal DefaultPackageRepository packageRepository;
private @CompilationFinal TopLevelScope topScope;
private final ThreadManager threadManager;
private final ThreadExecutors threadExecutors;
Expand Down Expand Up @@ -365,7 +363,11 @@ public Optional<QualifiedName> getModuleNameForFile(File path) {
* @return a qualified name of the module corresponding to the file, if exists.
*/
public Optional<QualifiedName> getModuleNameForFile(TruffleFile file) {
return PackageRepositoryUtils.getModuleNameForFile(packageRepository, file);
return scala.jdk.javaapi.CollectionConverters.asJava(packageRepository.getLoadedPackages())
.stream()
.filter(pkg -> file.startsWith(pkg.sourceDir()))
.map(pkg -> pkg.moduleNameForFile(file))
.findFirst();
}

/**
Expand Down Expand Up @@ -603,7 +605,7 @@ private Object findGuestJava() throws IllegalStateException {
* @return {@code module}'s package, if exists
*/
public Optional<Package<TruffleFile>> getPackageOf(TruffleFile file) {
return PackageRepositoryUtils.getPackageOf(packageRepository, file);
return TruffleCompilerContext.getPackageOf(packageRepository, file);
}

/**
Expand Down Expand Up @@ -803,7 +805,7 @@ public TruffleLogger getLogger() {
/**
* @return the package repository
*/
public PackageRepository getPackageRepository() {
public DefaultPackageRepository getPackageRepository() {
return packageRepository;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.enso.common.CompilationStage;
import org.enso.common.LanguageInfo;
import org.enso.compiler.Compiler;
import org.enso.compiler.PackageRepository;
import org.enso.compiler.Passes;
import org.enso.compiler.context.CompilerContext;
import org.enso.compiler.context.FreshNameSupply;
Expand Down Expand Up @@ -50,6 +50,7 @@
import scala.collection.immutable.SetOps;

final class TruffleCompilerContext implements CompilerContext {
private static final Logger LOG = Logger.getLogger(TruffleCompilerContext.class.getName());

private final EnsoContext context;
private final TruffleLogger loggerCompiler;
Expand Down Expand Up @@ -86,7 +87,7 @@ public boolean isInteractiveMode() {
}

@Override
public PackageRepository getPackageRepository() {
public DefaultPackageRepository getPackageRepository() {
return context.getPackageRepository();
}

Expand Down Expand Up @@ -163,6 +164,13 @@ public boolean isInteractive(CompilerContext.Module module) {
return ((Module) module).unsafeModule().isInteractive();
}

@Override
public boolean isModuleInRootPackage(CompilerContext.Module module) {
var file = ((Module) module).getSourceFile();
var pkg = getPackageOf(getPackageRepository(), file);
return pkg.isPresent() && pkg.get() == getPackageRepository().getMainProjectPackage().get();
}

@Override
public boolean wasLoadedFromCache(CompilerContext.Module module) {
return ((Module) module).unsafeModule().wasLoadedFromCache();
Expand Down Expand Up @@ -775,8 +783,7 @@ public BindingsMap getBindingsMap() {
return bindings;
}

@Override
public TruffleFile getSourceFile() {
final TruffleFile getSourceFile() {
return module.getSourceFile();
}

Expand Down Expand Up @@ -847,4 +854,28 @@ private static QualifiedName toQualifiedName(LibraryName libraryName) {
var namespace = cons(libraryName.namespace(), nil());
return new QualifiedName(namespace, libraryName.name());
}

/**
* Finds the package the provided module belongs to.
*
* @param packageRepository repository to work on
* @param file the module to find the package of
* @return {@code module}'s package, if exists
*/
static Optional<Package<TruffleFile>> getPackageOf(
DefaultPackageRepository packageRepository, TruffleFile file) {
try {
if (file != null) {
file = file.getCanonicalFile();
for (var pkg : packageRepository.getLoadedPackagesJava()) {
if (file.startsWith(pkg.root().getCanonicalFile())) {
return Optional.of(pkg);
}
}
}
} catch (IOException e) {
LOG.log(Level.WARNING, null, e);
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.enso.interpreter.runtime

import scala.jdk.OptionConverters.RichOption

import org.enso.compiler.PackageRepository
import org.enso.compiler.context.CompilerContext
import org.enso.compiler.core.ir.{Module => IRModule}
Expand Down Expand Up @@ -52,6 +54,7 @@ private class DefaultPackageRepository(
builtins: Builtins,
notificationHandler: NotificationHandler
) extends PackageRepository {
type TFile = TruffleFile

private val logger = Logger[DefaultPackageRepository]

Expand Down Expand Up @@ -176,10 +179,17 @@ private class DefaultPackageRepository(
}

/** @inheritdoc */
override def getMainProjectPackage: Option[Package[TruffleFile]] = {
override def getMainProjectPackage
: Option[Package[com.oracle.truffle.api.TruffleFile]] = {
projectPackage
}

/** Returns a package directory corresponding to the requested library */
def getPackageForLibraryJava(
libraryName: LibraryName
): java.util.Optional[Package[TruffleFile]] =
getPackageForLibrary(libraryName).toJava

private def registerPackageInternal(
libraryName: LibraryName,
libraryVersion: LibraryVersion,
Expand Down Expand Up @@ -447,7 +457,7 @@ private class DefaultPackageRepository(
override def getLoadedPackages: Seq[Package[TruffleFile]] =
loadedPackages.values.toSeq.flatten

override def getLoadedPackagesJava: java.lang.Iterable[Package[TruffleFile]] =
def getLoadedPackagesJava: java.lang.Iterable[Package[TruffleFile]] =
loadedPackages.flatMap(_._2).asJava

/** @inheritdoc */
Expand Down Expand Up @@ -638,7 +648,7 @@ private object DefaultPackageRepository {
context: EnsoContext,
builtins: Builtins,
notificationHandler: NotificationHandler
): PackageRepository = {
): DefaultPackageRepository = {
val rawEdition = editionOverride
.map(v => Editions.Raw.Edition(parent = Some(v)))
.orElse(
Expand Down

0 comments on commit 3c87142

Please sign in to comment.