Skip to content
This repository was archived by the owner on Jul 12, 2024. It is now read-only.

Commit 45a43a9

Browse files
committed
Patch j.l.Object.toString() not to use getClass().
This way, `j.l.Class` is not reachable, nor the other problematic methods of `j.l.Object`. This means we can remove the last filters and only rely on reachability analysis.
1 parent fb2ed63 commit 45a43a9

File tree

4 files changed

+23
-32
lines changed

4 files changed

+23
-32
lines changed

Diff for: wasm/src/main/scala/Compiler.scala

+5-13
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,14 @@ object Compiler {
4949
} yield {
5050
val onlyModule = moduleSet.modules.head
5151

52-
val filteredClasses = onlyModule.classDefs.filter { c =>
53-
!ExcludedClasses.contains(c.className)
54-
}
52+
// Sort for stability
53+
val sortedClasses = onlyModule.classDefs.sortBy(_.className)
5554

56-
filteredClasses.sortBy(_.className).foreach(showLinkedClass(_))
55+
sortedClasses.foreach(showLinkedClass(_))
5756

58-
Preprocessor.preprocess(filteredClasses)(context)
57+
Preprocessor.preprocess(sortedClasses)(context)
5958
println("preprocessed")
60-
filteredClasses.foreach { clazz =>
59+
sortedClasses.foreach { clazz =>
6160
builder.transformClassDef(clazz)
6261
}
6362
onlyModule.topLevelExports.foreach { tle =>
@@ -71,13 +70,6 @@ object Compiler {
7170
}
7271
}
7372

74-
private val ExcludedClasses: Set[ir.Names.ClassName] = {
75-
import ir.Names._
76-
Set(
77-
ClassClass // java.lang.Class
78-
)
79-
}
80-
8173
private def showLinkedClass(clazz: LinkedClass): Unit = {
8274
val writer = new java.io.PrintWriter(System.out)
8375
val printer = new LinkedClassPrinter(writer)

Diff for: wasm/src/main/scala/ir2wasm/LibraryPatches.scala

+9
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ object LibraryPatches {
6565

6666
private val MethodPatches: Map[ClassName, List[MethodDef]] = {
6767
Map(
68+
ObjectClass -> List(
69+
// TODO Remove this patch when we support getClass() and full string concatenation
70+
MethodDef(
71+
EMF, m("toString", Nil, T), NON,
72+
Nil, ClassType(BoxedStringClass),
73+
Some(StringLiteral("[object]"))
74+
)(EOH, NOV)
75+
),
76+
6877
BoxedCharacterClass.withSuffix("$") -> List(
6978
MethodDef(
7079
EMF, m("toString", List(C), T), NON,

Diff for: wasm/src/main/scala/ir2wasm/Preprocessor.scala

+5-10
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ object Preprocessor {
1717
for (clazz <- classes)
1818
preprocess(clazz)
1919

20-
for (clazz <- classes) {
21-
if (clazz.className != IRNames.ObjectClass)
22-
collectAbstractMethodCalls(clazz)
23-
}
20+
for (clazz <- classes)
21+
collectAbstractMethodCalls(clazz)
2422
}
2523

2624
private def preprocess(clazz: LinkedClass)(implicit ctx: WasmContext): Unit = {
@@ -34,12 +32,9 @@ object Preprocessor {
3432
}
3533

3634
private def collectMethods(clazz: LinkedClass)(implicit ctx: WasmContext): Unit = {
37-
val infos =
38-
if (clazz.name.name == IRNames.ObjectClass) Nil
39-
else
40-
clazz.methods.filterNot(_.flags.namespace.isConstructor).map { method =>
41-
makeWasmFunctionInfo(clazz, method)
42-
}
35+
val infos = clazz.methods.filterNot(_.flags.namespace.isConstructor).map { method =>
36+
makeWasmFunctionInfo(clazz, method)
37+
}
4338
ctx.putClassInfo(
4439
clazz.name.name,
4540
new WasmClassInfo(

Diff for: wasm/src/main/scala/ir2wasm/WasmBuilder.scala

+4-9
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,10 @@ class WasmBuilder {
6969
)
7070
ctx.addGCType(structType)
7171

72-
// Do not generate methods in Object for now
73-
if (clazz.name.name == IRNames.ObjectClass)
74-
clazz.methods.filter(_.name.name == IRNames.NoArgConstructorName).foreach { method =>
75-
genFunction(clazz, method)
76-
}
77-
else
78-
clazz.methods.foreach { method =>
79-
genFunction(clazz, method)
80-
}
72+
// implementation of methods
73+
clazz.methods.foreach { method =>
74+
genFunction(clazz, method)
75+
}
8176

8277
structType
8378
}

0 commit comments

Comments
 (0)