@@ -39,9 +39,13 @@ import sbt.{Logger, MessageOnlyException}
39
39
* Afterwards, we check that the IR does not contain any reference to classes
40
40
* under the `scala.*` package.
41
41
*/
42
- final class JavalibIRCleaner (baseDirectoryURI : URI ) {
42
+ final class JavalibIRCleaner (baseDirectoryURI : URI , forScalalib : Boolean ) {
43
43
import JavalibIRCleaner ._
44
44
45
+ private val classNameSubstitutions : Map [ClassName , ClassName ] =
46
+ if (forScalalib) ClassNameSubstitutionsForScalalib
47
+ else ClassNameSubstitutionsForJavalib
48
+
45
49
def cleanIR (dependencyFiles : Seq [File ], libFileMappings : Seq [(File , File )],
46
50
logger : Logger ): Set [File] = {
47
51
@@ -80,11 +84,19 @@ final class JavalibIRCleaner(baseDirectoryURI: URI) {
80
84
}
81
85
82
86
if (errorManager.hasErrors) {
83
- throw new MessageOnlyException (
84
- s " There were ${errorManager.errorCount} errors while " +
85
- " postprocessing the IR of the javalanglib. " +
86
- " The javalanglib must be written in a style that does not leak any " +
87
- " reference to the Scala library." )
87
+ if (forScalalib) {
88
+ throw new MessageOnlyException (
89
+ s " There were ${errorManager.errorCount} errors while " +
90
+ " postprocessing the IR of the scalalib. " +
91
+ " The scalalib must be written in a style that does not leak any " +
92
+ " reference to the Scala.js standard library." )
93
+ } else {
94
+ throw new MessageOnlyException (
95
+ s " There were ${errorManager.errorCount} errors while " +
96
+ " postprocessing the IR of the javalib. " +
97
+ " The javalib must be written in a style that does not leak any " +
98
+ " reference to the Scala library." )
99
+ }
88
100
}
89
101
90
102
resultBuilder.result()
@@ -146,10 +158,12 @@ final class JavalibIRCleaner(baseDirectoryURI: URI) {
146
158
// Preprocess the super interface list
147
159
val newInterfaces = transformInterfaceList(interfaces)
148
160
149
- /* Remove the `private def writeReplace__O` generated by scalac 2.13+
150
- * in the companion of serializable classes.
161
+ /* javalib only: Remove the `private def writeReplace__O` generated by
162
+ * scalac 2.13+ in the companion of serializable classes.
151
163
*/
152
- val newMethods = methods.filter(_.name.name != writeReplaceMethodName)
164
+ val newMethods =
165
+ if (forScalalib) methods
166
+ else methods.filter(_.name.name != writeReplaceMethodName)
153
167
154
168
val preprocessedTree = ClassDef (name, originalName, kind, jsClassCaptures,
155
169
superClass, newInterfaces, jsSuperClass, jsNativeLoadSpec, fields,
@@ -177,9 +191,13 @@ final class JavalibIRCleaner(baseDirectoryURI: URI) {
177
191
/* Replace references to scala.Serializable by java.io.Serializable.
178
192
* This works around the fact that scalac adds scala.Serializable to the
179
193
* companion object of any class that extends java.io.Serializable.
194
+ *
195
+ * Don't do it for the scalalib.
180
196
*/
181
197
182
- if (! interfaces.exists(_.name == ScalaSerializable )) {
198
+ if (forScalalib) {
199
+ interfaces
200
+ } else if (! interfaces.exists(_.name == ScalaSerializable )) {
183
201
interfaces
184
202
} else if (interfaces.exists(_.name == JavaIOSerializable )) {
185
203
interfaces.filter(_.name != ScalaSerializable )
@@ -321,13 +339,13 @@ final class JavalibIRCleaner(baseDirectoryURI: URI) {
321
339
implicit val pos = tree.pos
322
340
323
341
tree match {
324
- // new AnonFunctionN(closure) --> closure
325
- case New (AnonFunctionNClass (n), _, List (closure)) =>
342
+ // javalib only: new AnonFunctionN(closure) --> closure
343
+ case New (AnonFunctionNClass (n), _, List (closure)) if ! forScalalib =>
326
344
closure
327
345
328
- // someFunctionN.apply(args) --> someFunctionN(args)
346
+ // javalib only: someFunctionN.apply(args) --> someFunctionN(args)
329
347
case Apply (ApplyFlags .empty, fun, MethodIdent (FunctionApplyMethodName (n)), args)
330
- if isFunctionNType(n, fun.tpe) =>
348
+ if ! forScalalib && isFunctionNType(n, fun.tpe) =>
331
349
JSFunctionApply (fun, args)
332
350
333
351
// <= 2.12 : toJSVarArgs(jsArrayOps(jsArray).toSeq) -> jsArray
@@ -582,7 +600,7 @@ final class JavalibIRCleaner(baseDirectoryURI: URI) {
582
600
}
583
601
584
602
private def transformClassName (cls : ClassName )(implicit pos : Position ): ClassName = {
585
- ClassNameSubstitutions .getOrElse(cls, {
603
+ classNameSubstitutions .getOrElse(cls, {
586
604
validateClassName(cls)
587
605
cls
588
606
})
@@ -600,8 +618,13 @@ final class JavalibIRCleaner(baseDirectoryURI: URI) {
600
618
def isAnException : Boolean =
601
619
isJavaScriptExceptionWithinItself || isTypedArrayBufferBridgeWithinItself
602
620
603
- if (cls.nameString.startsWith(" scala." ) && ! isAnException)
604
- reportError(s " Illegal reference to Scala class ${cls.nameString}" )
621
+ if (forScalalib) {
622
+ if (cls.nameString.startsWith(" scala.scalajs." ))
623
+ reportError(s " Illegal reference to Scala.js class ${cls.nameString}" )
624
+ } else {
625
+ if (cls.nameString.startsWith(" scala." ) && ! isAnException)
626
+ reportError(s " Illegal reference to Scala class ${cls.nameString}" )
627
+ }
605
628
}
606
629
607
630
private def transformNonJSClassName (cls : ClassName )(implicit pos : Position ): ClassName = {
@@ -729,7 +752,7 @@ object JavalibIRCleaner {
729
752
false
730
753
}
731
754
732
- private val ClassNameSubstitutions : Map [ClassName , ClassName ] = {
755
+ private val ClassNameSubstitutionsForJavalib : Map [ClassName , ClassName ] = {
733
756
val functionTypePairs = for {
734
757
funClass <- FunctionNClasses ++ AnonFunctionNClasses
735
758
} yield {
@@ -762,4 +785,15 @@ object JavalibIRCleaner {
762
785
val allPairs = functionTypePairs ++ refPairs ++ tuplePairs ++ otherPairs
763
786
allPairs.toMap
764
787
}
788
+
789
+ private val ClassNameSubstitutionsForScalalib : Map [ClassName , ClassName ] = {
790
+ val anonFunctionPairs = for {
791
+ n <- (0 to 22 ).toList
792
+ } yield {
793
+ ClassName (s " scala.scalajs.runtime.AnonFunction $n" ) -> ClassName (s " scala.runtime.AnonFunction $n" )
794
+ }
795
+
796
+ val allPairs = anonFunctionPairs
797
+ allPairs.toMap
798
+ }
765
799
}
0 commit comments