-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scala.js: Isolate linker in a separate classloader (#629)
- Remove Scala.js compiler dependency from mdoc - Remove direct linker/linker-interface dependency - Isolate linker in a separate class loader - this should address issues with conflicting IR versions on the classpath This introduces two new modules: jsWorkerApi (Java) and jsWorker (cross Scala) - Auto-detect Scala.js version from the SBT plugin - Mild cleanup of interactions with SJS linker inside the modifier - Remove usage of deprecated linker APIs - Updates Scala.js to 1.9.0 - Updates Scaal to 3.1.1
- Loading branch information
Showing
26 changed files
with
513 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package mdoc.js.interfaces; | ||
|
||
public enum LogLevel { | ||
Debug(1), Info(2), Warning(3), Error(4); | ||
|
||
private final int order; | ||
private LogLevel(int order) { | ||
this.order = order; | ||
} | ||
public int getOrder() { | ||
return order; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package mdoc.js.interfaces; | ||
|
||
public enum ModuleType { | ||
NoModule, ESModule, CommonJSModule; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package mdoc.js.interfaces; | ||
|
||
public class ScalajsConfig { | ||
public ModuleType moduleType; | ||
public boolean fullOpt; | ||
public boolean sourceMap; | ||
public boolean batchMode; | ||
public boolean closureCompiler; | ||
|
||
public ScalajsConfig() { | ||
} | ||
|
||
public ScalajsConfig withModuleKind(ModuleType kind) { | ||
if(kind == ModuleType.ESModule) | ||
this.moduleType = ModuleType.ESModule; | ||
else if (kind == ModuleType.NoModule) | ||
this.moduleType = ModuleType.NoModule; | ||
else if (kind == ModuleType.CommonJSModule) | ||
this.moduleType = ModuleType.CommonJSModule; | ||
return this; | ||
} | ||
|
||
public ScalajsConfig withOptimized(boolean enabled) { | ||
this.fullOpt = enabled; | ||
return this; | ||
} | ||
|
||
public ScalajsConfig withSourceMap(boolean enabled) { | ||
this.sourceMap = enabled; | ||
return this; | ||
} | ||
|
||
public ScalajsConfig withBatchMode(boolean enabled) { | ||
this.batchMode = enabled; | ||
return this; | ||
} | ||
|
||
public ScalajsConfig withClosureCompiler(boolean enabled) { | ||
this.closureCompiler = enabled; | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package mdoc.js.interfaces; | ||
|
||
public interface ScalajsLogger{ | ||
public void log(LogLevel level, String message); | ||
public void trace(Throwable ex); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package mdoc.js.interfaces; | ||
|
||
import java.nio.file.Path; | ||
|
||
public interface ScalajsWorkerApi { | ||
|
||
public interface IRFile { | ||
} | ||
|
||
public void cache(Path[] classPath); | ||
|
||
public java.util.Map<String, byte[]> link(IRFile[] in); | ||
|
||
public IRFile inMemory(String path, byte[] contents); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package mdoc.js.interfaces; | ||
|
||
public interface ScalajsWorkerProvider { | ||
public ScalajsWorkerApi create(ScalajsConfig config, ScalajsLogger logger); | ||
} |
1 change: 1 addition & 0 deletions
1
mdoc-js-worker/src/main/resources/META-INF/services/mdoc.js.interfaces.ScalajsWorkerProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mdoc.js.worker.ScalaJSWorkerProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package mdoc.js.worker; | ||
|
||
import mdoc.js.interfaces._ | ||
import java.nio.file.Path | ||
import org.scalajs.linker.MemOutputDirectory | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import org.scalajs.linker.StandardImpl | ||
import org.scalajs.linker.interface.StandardConfig | ||
import org.scalajs.linker.interface.ModuleKind | ||
import ModuleType._ | ||
import org.scalajs.linker.PathIRContainer | ||
import org.scalajs.linker.interface.IRFile | ||
import scala.concurrent.Await | ||
import scala.concurrent.duration.Duration | ||
import java.{util => ju} | ||
import org.scalajs.logging.Logger | ||
import org.scalajs.logging.Level | ||
import org.scalajs.linker.standard.MemIRFileImpl | ||
import org.scalajs.linker.interface.Semantics | ||
|
||
class ScalaJSWorker( | ||
config: ScalajsConfig, | ||
logger: Logger | ||
) extends ScalajsWorkerApi { | ||
case class IFile(mem: IRFile) extends ScalajsWorkerApi.IRFile | ||
|
||
val linker = { | ||
val cfg = | ||
StandardConfig() | ||
.withSemantics { | ||
if (config.fullOpt) Semantics.Defaults.optimized | ||
else Semantics.Defaults | ||
} | ||
.withBatchMode(config.batchMode) | ||
.withClosureCompilerIfAvailable(config.closureCompiler) | ||
.withSourceMap(config.sourceMap) | ||
.withModuleKind { | ||
config.moduleType match { | ||
case NoModule => ModuleKind.NoModule | ||
case ESModule => ModuleKind.ESModule | ||
case CommonJSModule => ModuleKind.CommonJSModule | ||
} | ||
} | ||
StandardImpl.clearableLinker(cfg) | ||
} | ||
|
||
var cachedFiles = Seq.empty[org.scalajs.linker.interface.IRFile] | ||
|
||
val cache = StandardImpl.irFileCache().newCache | ||
|
||
override def cache(x: Array[Path]): Unit = | ||
cachedFiles = Await.result( | ||
PathIRContainer | ||
.fromClasspath(x.toSeq) | ||
.map(_._1) | ||
.flatMap(cache.cached), | ||
Duration.Inf | ||
) | ||
|
||
override def link( | ||
in: Array[ScalajsWorkerApi.IRFile] | ||
): ju.Map[String, Array[Byte]] = { | ||
val mem = MemOutputDirectory() | ||
val report = Await.result( | ||
linker.link( | ||
cachedFiles.toSeq ++ | ||
in.toSeq.collect { case IFile(o) => | ||
o | ||
}, | ||
Seq.empty, | ||
mem, | ||
logger | ||
), | ||
Duration.Inf | ||
) | ||
|
||
val javaMap: ju.Map[String, Array[Byte]] = new ju.HashMap[String, Array[Byte]] | ||
|
||
report.publicModules.foreach { m => | ||
mem.content(m.jsFileName).foreach { content => | ||
javaMap.put(m.jsFileName, content) | ||
} | ||
} | ||
|
||
javaMap | ||
} | ||
|
||
override def inMemory(path: String, contents: Array[Byte]): ScalajsWorkerApi.IRFile = IFile( | ||
new MemIRFileImpl(path, None, contents) | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package mdoc.js.worker; | ||
|
||
import mdoc.js.{interfaces => i} | ||
|
||
import org.scalajs.{logging => sjslogging} | ||
|
||
class ScalaJSWorkerProvider extends i.ScalajsWorkerProvider { | ||
def mapping(level: sjslogging.Level): i.LogLevel = { | ||
import i.LogLevel._ | ||
import sjslogging.Level | ||
|
||
level match { | ||
case Level.Debug => Debug | ||
case Level.Info => Info | ||
case Level.Warn => Warning | ||
case Level.Error => Error | ||
} | ||
} | ||
def create(config: i.ScalajsConfig, logger: i.ScalajsLogger): i.ScalajsWorkerApi = { | ||
val wrappedLogger = new sjslogging.Logger { | ||
def log(level: sjslogging.Level, message: => String) = | ||
logger.log(mapping(level), message) | ||
|
||
def trace(ex: => Throwable) = | ||
logger.trace(ex) | ||
} | ||
|
||
new ScalaJSWorker(config, wrappedLogger) | ||
} | ||
} |
Oops, something went wrong.