forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScalaJSLink.scala
62 lines (46 loc) · 1.99 KB
/
ScalaJSLink.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package dotty.tools.dotc
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext}
import java.io.File
import java.nio.file.{Files, Path}
import com.google.common.jimfs.Jimfs
import org.scalajs.linker.*
import org.scalajs.linker.interface.*
import org.scalajs.logging.*
object ScalaJSLink:
private val compliantSemantics: Semantics =
Semantics.Defaults
.withAsInstanceOfs(CheckedBehavior.Compliant)
.withArrayIndexOutOfBounds(CheckedBehavior.Compliant)
.withArrayStores(CheckedBehavior.Compliant)
.withNegativeArraySizes(CheckedBehavior.Compliant)
.withNullPointers(CheckedBehavior.Compliant)
.withStringIndexOutOfBounds(CheckedBehavior.Compliant)
.withModuleInit(CheckedBehavior.Compliant)
end compliantSemantics
def link(classPath: String, useCompliantSemantics: Boolean)(using ExecutionContext): Path =
val cpEntries = classPath.split(File.pathSeparatorChar)
val logger = new ScalaConsoleLogger(Level.Warn)
val moduleInitializers = Seq(ModuleInitializer.mainMethodWithArgs(
"Test", "main", Nil))
val semantics = if useCompliantSemantics then compliantSemantics else Semantics.Defaults
val linkerConfig = StandardConfig()
.withCheckIR(true)
.withSourceMap(false)
.withBatchMode(true)
.withSemantics(semantics)
val linker = StandardImpl.linker(linkerConfig)
val dir = Jimfs.newFileSystem().getPath("tmp")
Files.createDirectory(dir)
val cache = StandardImpl.irFileCache().newCache
val result = PathIRContainer
.fromClasspath(cpEntries.toSeq.map(entry => new File(entry).toPath()))
.map(_._1)
.flatMap(cache.cached)
.flatMap(linker.link(_, moduleInitializers, PathOutputDirectory(dir), logger))
val report = Await.result(result, Duration.Inf)
if (report.publicModules.size != 1)
throw new AssertionError(s"got other than 1 module: $report")
dir.resolve(report.publicModules.head.jsFileName)
end link
end ScalaJSLink