Skip to content

Commit 64c62ef

Browse files
committed
some BytecodeWriter need a Context, except ClassBytecodeWriter
1 parent 303d69f commit 64c62ef

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

src/dotty/tools/dotc/backend/jvm/BCodeHelpers.scala

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ abstract class BCodeHelpers extends BCodeTypes with BytecodeWriters {
223223
log("No Main-Class designated or discovered.")
224224
case name :: Nil =>
225225
log(s"Unique entry point: setting Main-Class to $name")
226-
settings.mainClass.value = name
226+
settings.mainClass.update(name)
227227
case names =>
228228
log(s"No Main-Class due to multiple entry points:\n ${names.mkString("\n ")}")
229229
}
@@ -232,7 +232,7 @@ abstract class BCodeHelpers extends BCodeTypes with BytecodeWriters {
232232

233233
new DirectToJarfileWriter(f.file)
234234

235-
case _ => factoryNonJarBytecodeWriter()
235+
case _ => factoryNonJarBytecodeWriter(ctx)
236236
}
237237
}
238238

@@ -299,17 +299,6 @@ abstract class BCodeHelpers extends BCodeTypes with BytecodeWriters {
299299

300300
} // end of method addInnerClassesASM()
301301

302-
/**
303-
* All components (e.g. BCPickles, BCInnerClassGen) of the builder classes
304-
* extend this trait to have access to the context.
305-
*
306-
* The context is provided by the three leaf classes (PlainClassBuilder,
307-
* JMirrorBuilder and JBeanInfoBuilder) as class parameter.
308-
*/
309-
trait HasContext {
310-
implicit protected val ctx: Context
311-
}
312-
313302
/*
314303
* Custom attribute (JVMS 4.7.1) "ScalaSig" used as marker only
315304
* i.e., the pickle is contained in a custom annotation, see:

src/dotty/tools/dotc/backend/jvm/BytecodeWriters.scala

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ trait BytecodeWriters {
4141
def getFile(sym: Symbol, clsName: String, suffix: String): AbstractFile =
4242
getFile(outputDirectory(sym), clsName, suffix)
4343

44-
def factoryNonJarBytecodeWriter(implicit ctx: Context): BytecodeWriter = {
45-
val emitAsmp = ctx.base.settings.Ygenasmp.isSetByUser
46-
val doDump = ctx.base.settings.Ydumpclasses.isSetByUser
44+
def factoryNonJarBytecodeWriter(implicit ctx0: Context): BytecodeWriter = {
45+
val emitAsmp = ctx0.base.settings.Ygenasmp.isSetByUser
46+
val doDump = ctx0.base.settings.Ydumpclasses.isSetByUser
4747
(emitAsmp, doDump) match {
4848
case (false, false) => new ClassBytecodeWriter { }
49-
case (false, true ) => new ClassBytecodeWriter with DumpBytecodeWriter { }
50-
case (true, false) => new ClassBytecodeWriter with AsmpBytecodeWriter
51-
case (true, true ) => new ClassBytecodeWriter with AsmpBytecodeWriter with DumpBytecodeWriter { }
49+
case (false, true ) => new ClassBytecodeWriter with DumpBytecodeWriter { val ctx = ctx0 }
50+
case (true, false) => new ClassBytecodeWriter with AsmpBytecodeWriter { val ctx = ctx0 }
51+
case (true, true ) => new ClassBytecodeWriter with AsmpBytecodeWriter with DumpBytecodeWriter { val ctx = ctx0 }
5252
}
5353
}
5454

@@ -57,10 +57,12 @@ trait BytecodeWriters {
5757
def close(): Unit = ()
5858
}
5959

60-
class DirectToJarfileWriter(jfile: JFile) extends BytecodeWriter {
60+
class DirectToJarfileWriter(jfile: JFile)(implicit protected val ctx: Context)
61+
extends BytecodeWriter
62+
with HasContext {
6163
val jarMainAttrs = (
62-
if (settings.mainClass.isDefault) Nil
63-
else List(Name.MAIN_CLASS -> settings.mainClass.value)
64+
if (ctx.base.settings.mainClass.isDefault) Nil
65+
else List(Name.MAIN_CLASS -> ctx.base.settings.mainClass.value)
6466
)
6567
val writer = new Jar(jfile).jarWriter(jarMainAttrs: _*)
6668

@@ -86,10 +88,10 @@ trait BytecodeWriters {
8688
* their expansion by ASM is more readable.
8789
*
8890
* */
89-
trait AsmpBytecodeWriter extends BytecodeWriter {
91+
trait AsmpBytecodeWriter extends BytecodeWriter with HasContext {
9092
import dotty.tools.asm
9193

92-
private val baseDir = Directory(settings.Ygenasmp.value).createDirectory()
94+
private val baseDir = Directory(ctx.base.settings.Ygenasmp.value).createDirectory()
9395

9496
private def emitAsmp(jclassBytes: Array[Byte], asmpFile: io.File): Unit = {
9597
val pw = asmpFile.printWriter()
@@ -127,8 +129,8 @@ trait BytecodeWriters {
127129
}
128130
}
129131

130-
trait DumpBytecodeWriter extends BytecodeWriter {
131-
val baseDir = Directory(settings.Ydumpclasses.value).createDirectory()
132+
trait DumpBytecodeWriter extends BytecodeWriter with HasContext {
133+
val baseDir = Directory(ctx.base.settings.Ydumpclasses.value).createDirectory()
132134

133135
abstract override def writeClass(label: String, jclassName: String, jclassBytes: Array[Byte], outfile: AbstractFile): Unit = {
134136
super.writeClass(label, jclassName, jclassBytes, outfile)

src/dotty/tools/dotc/backend/jvm/GenBCode.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ object GenBCode extends BCodeSyncAndTry {
297297

298298
// clearing maps
299299
clearBCodeTypes()
300+
301+
// free the Context instance reachable from BytecodeWriter
302+
bytecodeWriter = null
300303
}
301304

302305
override def run(implicit ctx: Context): Unit = unsupported("run()")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* NSC -- new Scala compiler
2+
* Copyright 2005-2012 LAMP/EPFL
3+
* @author Martin Odersky
4+
*/
5+
6+
package dotty.tools.dotc
7+
package backend.jvm
8+
9+
import core.Contexts.Context
10+
11+
/**
12+
* All components (e.g. BCPickles, BCInnerClassGen) of the builder classes
13+
* extend this trait to have access to the context.
14+
*
15+
* The context is provided by the three leaf classes (PlainClassBuilder,
16+
* JMirrorBuilder and JBeanInfoBuilder) as class parameter.
17+
*
18+
* Same goes for BytecodeWriter
19+
*/
20+
trait HasContext {
21+
implicit protected val ctx: Context
22+
}
23+
24+

0 commit comments

Comments
 (0)