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

Commit 2a3a7e0

Browse files
authored
Merge pull request #20 from sjrd/remove-patches
Remove library patches; keep only our box classes.
2 parents f378cf4 + ce7e306 commit 2a3a7e0

File tree

3 files changed

+14
-111
lines changed

3 files changed

+14
-111
lines changed

Diff for: test-suite/src/main/scala/testsuite/core/HijackedClassesDispatchTest.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ object HijackedClassesDispatchTest {
1212
testToString(true, "true") &&
1313
testToString(54321, "54321") &&
1414
testToString(obj, "Test class") &&
15-
testToString(obj2, "[object]") &&
15+
testToStringStartsWith(obj2, "testsuite.core.HijackedClassesDispatchTest$Test2@") &&
1616
testToString('A', "A") &&
1717
testHashCode(true, 1231) &&
1818
testHashCode(54321, 54321) &&
@@ -43,6 +43,9 @@ object HijackedClassesDispatchTest {
4343
def testToString(x: Any, expected: String): Boolean =
4444
x.toString() == expected
4545

46+
def testToStringStartsWith(x: Any, expectedPrefix: String): Boolean =
47+
x.toString().startsWith(expectedPrefix)
48+
4649
def testHashCode(x: Any, expected: Int): Boolean =
4750
x.hashCode() == expected
4851

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

+9-109
Original file line numberDiff line numberDiff line change
@@ -17,125 +17,25 @@ import wasm.utils.MemClassDefIRFile
1717
/** Patches that we apply to the standard library classes to make them wasm-friendly. */
1818
object LibraryPatches {
1919
def patchIRFiles(irFiles: Seq[IRFile])(implicit ec: ExecutionContext): Future[Seq[IRFile]] = {
20-
val derivedCharBox = new java.util.concurrent.atomic.AtomicReference[IRFile](null)
21-
val derivedLongBox = new java.util.concurrent.atomic.AtomicReference[IRFile](null)
22-
23-
val patched1: Future[Seq[IRFile]] = Future.traverse(irFiles) { irFile =>
20+
val derivedIRFiles: Future[Seq[Option[IRFile]]] = Future.traverse(irFiles) { irFile =>
2421
val irFileImpl = IRFileImpl.fromIRFile(irFile)
2522
irFileImpl.entryPointsInfo.flatMap { entryPointsInfo =>
26-
MethodPatches.get(entryPointsInfo.className) match {
27-
case None =>
28-
entryPointsInfo.className match {
29-
case BoxedCharacterClass | BoxedLongClass =>
30-
irFileImpl.tree.map { classDef =>
31-
val derivedBox = MemClassDefIRFile(deriveBoxClass(classDef))
32-
if (classDef.className == BoxedCharacterClass)
33-
derivedCharBox.set(derivedBox)
34-
else
35-
derivedLongBox.set(derivedBox)
36-
irFile
37-
}
38-
case _ =>
39-
Future.successful(irFile)
23+
entryPointsInfo.className match {
24+
case BoxedCharacterClass | BoxedLongClass =>
25+
irFileImpl.tree.map { classDef =>
26+
Some(MemClassDefIRFile(deriveBoxClass(classDef)))
4027
}
41-
case Some(patches) =>
42-
irFileImpl.tree.map(classDef => MemClassDefIRFile(applyMethodPatches(classDef, patches)))
28+
case _ =>
29+
Future.successful(None)
4330
}
4431
}
4532
}
4633

47-
patched1.map { irFiles1 =>
48-
val extra = List(FloatingPointBitsIRFile, derivedCharBox.get(), derivedLongBox.get())
49-
extra ++ irFiles1
34+
derivedIRFiles.map { derived =>
35+
derived.flatten ++ irFiles
5036
}
5137
}
5238

53-
private val FloatingPointBitsIRFile: IRFile = {
54-
val classDef = ClassDef(
55-
ClassIdent("java.lang.FloatingPointBits$"),
56-
NON,
57-
ModuleClass,
58-
None,
59-
Some(ClassIdent(ObjectClass)),
60-
Nil,
61-
None,
62-
None,
63-
Nil,
64-
List(
65-
trivialCtor("java.lang.FloatingPointBits$"),
66-
MethodDef(
67-
EMF, MethodIdent(m("numberHashCode", List(D), I)), NON,
68-
List(paramDef("value", DoubleType)), IntType,
69-
Some(Block(
70-
// TODO This is not a compliant but it's good enough for now
71-
UnaryOp(UnaryOp.DoubleToInt, VarRef("value")(DoubleType))
72-
))
73-
)(EOH, NOV)
74-
),
75-
None,
76-
Nil,
77-
Nil,
78-
Nil
79-
)(EOH)
80-
81-
MemClassDefIRFile(classDef)
82-
}
83-
84-
private val MethodPatches: Map[ClassName, List[MethodDef]] = {
85-
Map(
86-
ObjectClass -> List(
87-
// TODO Remove this patch when we support getClass() and full string concatenation
88-
MethodDef(
89-
EMF, m("toString", Nil, T), NON,
90-
Nil, ClassType(BoxedStringClass),
91-
Some(StringLiteral("[object]"))
92-
)(EOH, NOV)
93-
),
94-
95-
BoxedCharacterClass.withSuffix("$") -> List(
96-
MethodDef(
97-
EMF, m("toString", List(C), T), NON,
98-
List(paramDef("c", CharType)), ClassType(BoxedStringClass),
99-
Some(BinaryOp(BinaryOp.String_+, StringLiteral(""), VarRef("c")(CharType)))
100-
)(EOH, NOV)
101-
),
102-
103-
BoxedIntegerClass.withSuffix("$") -> List(
104-
MethodDef(
105-
EMF, m("toHexString", List(I), T), NON,
106-
List(paramDef("i", IntType)), ClassType(BoxedStringClass),
107-
Some(
108-
// TODO Write a compliant implementation
109-
BinaryOp(BinaryOp.String_+, StringLiteral(""), VarRef("i")(IntType))
110-
)
111-
)(EOH, NOV)
112-
)
113-
)
114-
}
115-
116-
private def applyMethodPatches(classDef: ClassDef, patches: List[MethodDef]): ClassDef = {
117-
val patchesMap = patches.map(m => m.name.name -> m).toMap
118-
val patchedMethods = classDef.methods.map(m => patchesMap.getOrElse(m.name.name, m))
119-
120-
import classDef._
121-
ClassDef(
122-
name,
123-
originalName,
124-
kind,
125-
jsClassCaptures,
126-
superClass,
127-
interfaces,
128-
jsSuperClass,
129-
jsNativeLoadSpec,
130-
fields,
131-
patchedMethods,
132-
jsConstructor,
133-
jsMethodProps,
134-
jsNativeMembers,
135-
topLevelExportDefs
136-
)(EOH)(pos)
137-
}
138-
13939
/** Generates the accompanying Box class of `Character` or `Long`.
14040
*
14141
* These box classes will be used as the generic representation of `char`s

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ private class WasmExpressionBuilder private (
697697
case CharToInt | ByteToInt | ShortToInt =>
698698
() // these are no-ops because they are all represented as i32's with the right mathematical value
699699
case IntToLong =>
700-
instrs += I64_EXTEND32_S
700+
instrs += I64_EXTEND_I32_S
701701
case IntToDouble =>
702702
instrs += F64_CONVERT_I32_S
703703
case FloatToDouble =>

0 commit comments

Comments
 (0)