@@ -17,125 +17,25 @@ import wasm.utils.MemClassDefIRFile
17
17
/** Patches that we apply to the standard library classes to make them wasm-friendly. */
18
18
object LibraryPatches {
19
19
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 =>
24
21
val irFileImpl = IRFileImpl .fromIRFile(irFile)
25
22
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)))
40
27
}
41
- case Some (patches) =>
42
- irFileImpl.tree.map(classDef => MemClassDefIRFile (applyMethodPatches(classDef, patches)) )
28
+ case _ =>
29
+ Future .successful( None )
43
30
}
44
31
}
45
32
}
46
33
47
- patched1.map { irFiles1 =>
48
- val extra = List (FloatingPointBitsIRFile , derivedCharBox.get(), derivedLongBox.get())
49
- extra ++ irFiles1
34
+ derivedIRFiles.map { derived =>
35
+ derived.flatten ++ irFiles
50
36
}
51
37
}
52
38
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
-
139
39
/** Generates the accompanying Box class of `Character` or `Long`.
140
40
*
141
41
* These box classes will be used as the generic representation of `char`s
0 commit comments