This repository was archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCompiler.scala
137 lines (119 loc) · 3.76 KB
/
Compiler.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package wasm
import wasm.ir2wasm._
import wasm.wasm4s._
import org.scalajs.ir
import org.scalajs.ir.Trees._
import org.scalajs.ir.Types._
import org.scalajs.linker.frontend.LinkerFrontendImpl
import org.scalajs.linker.interface.{IRFile, ModuleInitializer}
import org.scalajs.linker.standard.{LinkedClass, SymbolRequirement}
import org.scalajs.logging.{Level, ScalaConsoleLogger}
import scala.concurrent.{ExecutionContext, Future}
import scala.scalajs.js
import scala.scalajs.js.annotation._
import scala.scalajs.js.typedarray._
object Compiler {
def compileIRFiles(
irFiles: Seq[IRFile],
moduleInitializers: List[ModuleInitializer],
outputName: String
)(implicit ec: ExecutionContext): Future[Unit] = {
val module = new WasmModule
val builder = new WasmBuilder()
implicit val context: WasmContext = new WasmContext(module)
println("compiling... ")
val config = LinkerFrontendImpl
.Config()
.withOptimizer(false)
val linkerFrontend = LinkerFrontendImpl(config)
val symbolRequirements = SymbolRequirement.factory("none").none()
val logger = new ScalaConsoleLogger(Level.Info)
for {
patchedIRFiles <- LibraryPatches.patchIRFiles(irFiles)
moduleSet <- linkerFrontend.link(
patchedIRFiles,
moduleInitializers,
symbolRequirements,
logger
)
} yield {
val onlyModule = moduleSet.modules.head
val filteredClasses = onlyModule.classDefs.filter { c =>
!ExcludedClasses.contains(c.className)
}
filteredClasses.sortBy(_.className).foreach(showLinkedClass(_))
Preprocessor.preprocess(filteredClasses)(context)
println("preprocessed")
filteredClasses.foreach { clazz =>
builder.transformClassDef(clazz)
}
onlyModule.topLevelExports.foreach { tle =>
builder.transformTopLevelExport(tle)
}
val textOutput = new converters.WasmTextWriter().write(module)
FS.writeFileSync(s"./target/$outputName.wat", textOutput.getBytes().toTypedArray)
val binaryOutput = new converters.WasmBinaryWriter(module).write()
FS.writeFileSync(s"./target/$outputName.wasm", binaryOutput.toTypedArray)
}
}
private val ExcludedClasses: Set[ir.Names.ClassName] = {
import ir.Names._
HijackedClasses ++ // hijacked classes
Set(
ClassClass // java.lang.Class
)
}
private def showLinkedClass(clazz: LinkedClass): Unit = {
val writer = new java.io.PrintWriter(System.out)
val printer = new LinkedClassPrinter(writer)
printer.print(clazz)
printer.println()
writer.flush()
}
private class LinkedClassPrinter(_out: java.io.Writer) extends ir.Printers.IRTreePrinter(_out) {
def print(clazz: LinkedClass): Unit = {
print("linked ")
print(clazz.kind.toString())
print(" ")
print(clazz.className)
clazz.superClass.foreach { cls =>
print(" extends ")
print(cls)
clazz.jsSuperClass.foreach { tree =>
print(" (via ")
print(tree)
print(")")
}
}
if (clazz.interfaces.nonEmpty) {
print(" implements ")
var rest = clazz.interfaces
while (rest.nonEmpty) {
print(rest.head)
rest = rest.tail
if (rest.nonEmpty)
print(", ")
}
}
clazz.jsNativeLoadSpec.foreach { spec =>
print(" loadfrom ")
print(spec)
}
print(" ")
printColumn(
clazz.fields
::: clazz.methods
::: clazz.jsConstructorDef.toList
::: clazz.exportedMembers
::: clazz.jsNativeMembers,
"{",
"",
"}"
)
}
}
private object FS {
@js.native @JSImport("fs")
def writeFileSync(file: String, data: Int8Array): Unit = js.native
}
}